|
| 1 | +/*============================================================================== |
| 2 | +
|
| 3 | + ofxVisualProgramming: A visual programming patching environment for OF |
| 4 | +
|
| 5 | + Copyright (c) 2021 Emanuele Mazza aka n3m3da <emanuelemazza@d3cod3.org> |
| 6 | +
|
| 7 | + ofxVisualProgramming is distributed under the MIT License. |
| 8 | + This gives everyone the freedoms to use ofxVisualProgramming in any context: |
| 9 | + commercial or non-commercial, public or private, open or closed source. |
| 10 | +
|
| 11 | + Permission is hereby granted, free of charge, to any person obtaining a |
| 12 | + copy of this software and associated documentation files (the "Software"), |
| 13 | + to deal in the Software without restriction, including without limitation |
| 14 | + the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 15 | + and/or sell copies of the Software, and to permit persons to whom the |
| 16 | + Software is furnished to do so, subject to the following conditions: |
| 17 | +
|
| 18 | + The above copyright notice and this permission notice shall be included |
| 19 | + in all copies or substantial portions of the Software. |
| 20 | +
|
| 21 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 22 | + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 27 | + DEALINGS IN THE SOFTWARE. |
| 28 | +
|
| 29 | + See https://github.com/d3cod3/ofxVisualProgramming for documentation |
| 30 | +
|
| 31 | +==============================================================================*/ |
| 32 | + |
| 33 | +#if defined(TARGET_WIN32) || defined(TARGET_LINUX) |
| 34 | + // Unavailable on windows and linux |
| 35 | +#elif !defined(OFXVP_BUILD_WITH_MINIMAL_OBJECTS) |
| 36 | + |
| 37 | +#include "SyphonReceiver.h" |
| 38 | + |
| 39 | +//-------------------------------------------------------------- |
| 40 | +SyphonReceiver::SyphonReceiver() : PatchObject("syphon receiver"){ |
| 41 | + |
| 42 | + this->numInlets = 0; |
| 43 | + this->numOutlets = 1; |
| 44 | + |
| 45 | + _outletParams[0] = new ofTexture(); // input |
| 46 | + |
| 47 | + this->initInletsState(); |
| 48 | + |
| 49 | + posX = posY = drawW = drawH = 0.0f; |
| 50 | + |
| 51 | + needToGrab = true; |
| 52 | + |
| 53 | + this->setIsTextureObj(true); |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +//-------------------------------------------------------------- |
| 58 | +void SyphonReceiver::newObject(){ |
| 59 | + PatchObject::setName( this->objectName ); |
| 60 | + |
| 61 | + this->addOutlet(VP_LINK_TEXTURE,"textureReceived"); |
| 62 | +} |
| 63 | + |
| 64 | +//-------------------------------------------------------------- |
| 65 | +void SyphonReceiver::setupObjectContent(shared_ptr<ofAppGLFWWindow> &mainWindow){ |
| 66 | + |
| 67 | + //setup our directory |
| 68 | + dir.setup(); |
| 69 | + //setup our client |
| 70 | + syphonClient.setup(); |
| 71 | + |
| 72 | + //register for our directory's callbacks |
| 73 | + ofAddListener(dir.events.serverAnnounced, this, &SyphonReceiver::serverAnnounced); |
| 74 | + ofAddListener(dir.events.serverRetired, this, &SyphonReceiver::serverRetired); |
| 75 | + |
| 76 | + dirIdx = -1; |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +//-------------------------------------------------------------- |
| 81 | +void SyphonReceiver::updateObjectContent(map<int,shared_ptr<PatchObject>> &patchObjects){ |
| 82 | + |
| 83 | + syphonClient.bind(); |
| 84 | + syphonTexture = syphonClient.getTexture(); |
| 85 | + syphonClient.unbind(); |
| 86 | + |
| 87 | + if(needToGrab && dir.isValidIndex(dirIdx) && dir.size() > 0 && syphonTexture.isAllocated() && syphonTexture.getWidth() > 0 && syphonTexture.getHeight() > 0){ |
| 88 | + needToGrab = false; |
| 89 | + static_cast<ofTexture *>(_outletParams[0])->allocate(syphonTexture.getWidth(), syphonTexture.getHeight(), GL_RGB ); |
| 90 | + } |
| 91 | + |
| 92 | + if(static_cast<ofTexture *>(_outletParams[0])->isAllocated() && dir.isValidIndex(dirIdx)){ |
| 93 | + *static_cast<ofTexture *>(_outletParams[0]) = syphonTexture; |
| 94 | + } |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +//-------------------------------------------------------------- |
| 99 | +void SyphonReceiver::drawObjectContent(ofTrueTypeFont *font, shared_ptr<ofBaseGLRenderer>& glRenderer){ |
| 100 | + |
| 101 | + ofSetColor(255); |
| 102 | + if(static_cast<ofTexture *>(_outletParams[0])->isAllocated()){ |
| 103 | + // draw node texture preview with OF |
| 104 | + if(scaledObjW*canvasZoom > 90.0f){ |
| 105 | + drawNodeOFTexture(*static_cast<ofTexture *>(_outletParams[0]), posX, posY, drawW, drawH, objOriginX, objOriginY, scaledObjW, scaledObjH, canvasZoom, this->scaleFactor); |
| 106 | + } |
| 107 | + }else{ |
| 108 | + // background |
| 109 | + if(scaledObjW*canvasZoom > 90.0f){ |
| 110 | + ofSetColor(34,34,34); |
| 111 | + ofDrawRectangle(objOriginX - (IMGUI_EX_NODE_PINS_WIDTH_NORMAL*this->scaleFactor/canvasZoom), objOriginY-(IMGUI_EX_NODE_HEADER_HEIGHT*this->scaleFactor/canvasZoom),scaledObjW + (IMGUI_EX_NODE_PINS_WIDTH_NORMAL*this->scaleFactor/canvasZoom),scaledObjH + (((IMGUI_EX_NODE_HEADER_HEIGHT+IMGUI_EX_NODE_FOOTER_HEIGHT)*this->scaleFactor)/canvasZoom) ); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +//-------------------------------------------------------------- |
| 117 | +void SyphonReceiver::drawObjectNodeGui( ImGuiEx::NodeCanvas& _nodeCanvas ){ |
| 118 | + |
| 119 | + // CONFIG GUI inside Menu |
| 120 | + if(_nodeCanvas.BeginNodeMenu()){ |
| 121 | + ImGui::Separator(); |
| 122 | + ImGui::Separator(); |
| 123 | + ImGui::Separator(); |
| 124 | + |
| 125 | + if (ImGui::BeginMenu("CONFIG")) |
| 126 | + { |
| 127 | + |
| 128 | + drawObjectNodeConfig(); this->configMenuWidth = ImGui::GetWindowWidth(); |
| 129 | + |
| 130 | + ImGui::EndMenu(); |
| 131 | + } |
| 132 | + _nodeCanvas.EndNodeMenu(); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + // Visualize (Object main view) |
| 137 | + if( _nodeCanvas.BeginNodeContent(ImGuiExNodeView_Visualise) ){ |
| 138 | + |
| 139 | + // get imgui node translated/scaled position/dimension for drawing textures in OF |
| 140 | + objOriginX = (ImGui::GetWindowPos().x + ((IMGUI_EX_NODE_PINS_WIDTH_NORMAL - 1)*this->scaleFactor) - _nodeCanvas.GetCanvasTranslation().x)/_nodeCanvas.GetCanvasScale(); |
| 141 | + objOriginY = (ImGui::GetWindowPos().y - _nodeCanvas.GetCanvasTranslation().y)/_nodeCanvas.GetCanvasScale(); |
| 142 | + scaledObjW = this->width - (IMGUI_EX_NODE_PINS_WIDTH_NORMAL*this->scaleFactor/_nodeCanvas.GetCanvasScale()); |
| 143 | + scaledObjH = this->height - ((IMGUI_EX_NODE_HEADER_HEIGHT+IMGUI_EX_NODE_FOOTER_HEIGHT)*this->scaleFactor/_nodeCanvas.GetCanvasScale()); |
| 144 | + |
| 145 | + |
| 146 | + _nodeCanvas.EndNodeContent(); |
| 147 | + } |
| 148 | + |
| 149 | + // get imgui canvas zoom |
| 150 | + canvasZoom = _nodeCanvas.GetCanvasScale(); |
| 151 | + |
| 152 | +} |
| 153 | + |
| 154 | +//-------------------------------------------------------------- |
| 155 | +void SyphonReceiver::drawObjectNodeConfig(){ |
| 156 | + ImGuiEx::ObjectInfo( |
| 157 | + "Receive (local network) video from the syphon. It will automatically receive the last syphon server published.", |
| 158 | + "https://mosaic.d3cod3.org/reference.php?r=syphon-receiver", scaleFactor); |
| 159 | +} |
| 160 | + |
| 161 | +//-------------------------------------------------------------- |
| 162 | +void SyphonReceiver::removeObjectContent(bool removeFileFromData){ |
| 163 | + |
| 164 | +} |
| 165 | + |
| 166 | +//-------------------------------------------------------------- |
| 167 | +void SyphonReceiver::serverAnnounced(ofxSyphonServerDirectoryEventArgs &arg){ |
| 168 | + for( auto& dir : arg.servers ){ |
| 169 | + ofLogNotice("ofxSyphonServerDirectory Server Announced")<<" Server Name: "<<dir.serverName <<" | App Name: "<<dir.appName; |
| 170 | + syphonClient.setServerName(dir.serverName); |
| 171 | + syphonClient.setApplicationName(dir.appName); |
| 172 | + } |
| 173 | + dirIdx = 0; |
| 174 | +} |
| 175 | + |
| 176 | +//-------------------------------------------------------------- |
| 177 | +void SyphonReceiver::serverRetired(ofxSyphonServerDirectoryEventArgs &arg){ |
| 178 | + for( auto& dir : arg.servers ){ |
| 179 | + ofLogNotice("ofxSyphonServerDirectory Server Retired")<<" Server Name: "<<dir.serverName <<" | App Name: "<<dir.appName; |
| 180 | + } |
| 181 | + dirIdx = 0; |
| 182 | +} |
| 183 | + |
| 184 | +OBJECT_REGISTER( SyphonReceiver, "syphon receiver", OFXVP_OBJECT_CAT_TEXTURE) |
| 185 | + |
| 186 | +#endif |
0 commit comments