Browse Source

Add posibility to select texture for selected surface

master
Krisjanis Rijnieks 11 years ago
parent
commit
765b8eaae6
  1. 3
      README.md
  2. 113
      src/ofxSourcesEditor.cpp
  3. 21
      src/ofxSourcesEditor.h
  4. 1
      src/ofxSurfaceManager.h
  5. 33
      src/ofxSurfaceManagerGui.cpp
  6. 1
      src/ofxSurfaceManagerGui.h

3
README.md

@ -22,7 +22,8 @@ git clone https://github.com/kr15h/ofxPiMapper.git
Dependencies Dependencies
------------ ------------
None so far. ofxUI
ofxXmlSettings
Compatibility Compatibility
------------ ------------

113
src/ofxSourcesEditor.cpp

@ -2,15 +2,124 @@
ofxSourcesEditor::ofxSourcesEditor() ofxSourcesEditor::ofxSourcesEditor()
{ {
defImgDir = DEFAULT_IMAGES_DIR;
registerAppEvents();
} }
ofxSourcesEditor::~ofxSourcesEditor() ofxSourcesEditor::~ofxSourcesEditor()
{ {
unregisterAppEvents();
delete gui;
while ( images.size() ) {
delete images.back();
images.pop_back();
}
}
void ofxSourcesEditor::registerAppEvents()
{
ofAddListener(ofEvents().setup, this, &ofxSourcesEditor::setup);
}
void ofxSourcesEditor::unregisterAppEvents()
{
ofRemoveListener(ofEvents().setup, this, &ofxSourcesEditor::setup);
}
void ofxSourcesEditor::setup(ofEventArgs& args)
{
gui = new ofxUICanvas();
gui->disable();
gui->disableAppDrawCallback();
// read directory contents
ofDirectory imgDir;
imgDir.listDir(defImgDir);
imgDir.sort();
vector<string> vnames;
for(int i = 0; i < (int)imgDir.size(); i++){
//images[i].loadImage(imgDir.getPath(i));
vnames.push_back(imgDir.getName(i));
}
gui->addLabel(defImgDir, OFX_UI_FONT_SMALL);
ofxUIRadio *radio = gui->addRadio("VR", vnames, OFX_UI_ORIENTATION_VERTICAL);
radio->activateToggle("image0.png");
ofAddListener(gui->newGUIEvent,this,&ofxSourcesEditor::guiEvent);
} }
void ofxSourcesEditor::draw() void ofxSourcesEditor::draw()
{ {
ofDrawBitmapString("Sources Editor", ofPoint(10, 20)); gui->draw();
}
void ofxSourcesEditor::loadImage( string name, string path )
{
images.push_back(new ofImage());
images.back()->loadImage(path);
imageNames.push_back(name);
ofSendMessage("imageLoaded");
}
void ofxSourcesEditor::disable()
{
gui->disable();
}
void ofxSourcesEditor::enable()
{
gui->enable();
}
int ofxSourcesEditor::getLoadedTexCount()
{
return images.size();
}
ofTexture* ofxSourcesEditor::getTexture(int index)
{
if (index >= images.size()){
throw std::runtime_error("Texture index out of bounds.");
}
return &images[index]->getTextureReference();
}
void ofxSourcesEditor::guiEvent(ofxUIEventArgs &e)
{
string name = e.widget->getName();
int kind = e.widget->getKind();
if(kind == OFX_UI_WIDGET_TOGGLE){
ofxUIToggle *toggle = (ofxUIToggle *) e.widget;
cout << name << "\t value: " << toggle->getValue() << endl;
}
// search for matching loaded image name
for ( int i=0; i<images.size(); i++ ) {
cout << "loaded: " << imageNames[i] << endl;
if ( imageNames[i] == name ) {
// image already loaded
ofImage* img = images[i];
images.erase(images.begin()+i);
images.push_back(img);
string imgName = imageNames[i];
imageNames.erase(imageNames.begin()+i);
imageNames.push_back(imgName);
cout << "image already loaded" << endl;
ofSendMessage("imageLoaded");
return;
}
}
stringstream ss;
ss << defImgDir << name;
loadImage(name, ss.str());
} }

21
src/ofxSourcesEditor.h

@ -2,6 +2,10 @@
#define H_OFX_SOURCES_EDITOR #define H_OFX_SOURCES_EDITOR
#include "ofGraphics.h" #include "ofGraphics.h"
#include "ofEvents.h"
#include "ofxUI.h"
#define DEFAULT_IMAGES_DIR "sources/images/";
class ofxSourcesEditor class ofxSourcesEditor
{ {
@ -9,10 +13,27 @@ public:
ofxSourcesEditor(); ofxSourcesEditor();
~ofxSourcesEditor(); ~ofxSourcesEditor();
void registerAppEvents();
void unregisterAppEvents();
void setup(ofEventArgs& args);
void draw(); void draw();
void loadImage( string name, string path );
void disable();
void enable();
int getLoadedTexCount();
ofTexture* getTexture(int index);
private: private:
string defImgDir;
ofxUICanvas *gui;
void guiEvent(ofxUIEventArgs &e);
vector<ofImage*> images;
vector<string> imageNames;
//ofxPanel imgSrcPanel;
//void onSourceSelect(bool& value);
}; };
#endif #endif

1
src/ofxSurfaceManager.h

@ -4,6 +4,7 @@
#include "ofxBaseSurface.h" #include "ofxBaseSurface.h"
#include "ofxTriangleSurface.h" #include "ofxTriangleSurface.h"
#include "ofxSurfaceType.h" #include "ofxSurfaceType.h"
#include "ofEvents.h"
using namespace std; using namespace std;

33
src/ofxSurfaceManagerGui.cpp

@ -6,11 +6,13 @@ ofxSurfaceManagerGui::ofxSurfaceManagerGui()
guiMode = ofxGuiMode::NONE; guiMode = ofxGuiMode::NONE;
bDrag = false; bDrag = false;
registerMouseEvents(); registerMouseEvents();
ofRegisterGetMessages(this);
} }
ofxSurfaceManagerGui::~ofxSurfaceManagerGui() ofxSurfaceManagerGui::~ofxSurfaceManagerGui()
{ {
unregisterMouseEvents(); unregisterMouseEvents();
ofUnregisterGetMessages(this);
surfaceManager = NULL; surfaceManager = NULL;
} }
@ -68,6 +70,12 @@ void ofxSurfaceManagerGui::draw()
projectionEditor.draw(); projectionEditor.draw();
} else if ( guiMode == ofxGuiMode::SOURCE_SELECTION ) { } else if ( guiMode == ofxGuiMode::SOURCE_SELECTION ) {
// draw projection surfaces first
surfaceManager->draw();
// highlight selected surface
drawSelectedSurfaceHighlight();
sourcesEditor.draw(); sourcesEditor.draw();
} }
} }
@ -173,8 +181,16 @@ void ofxSurfaceManagerGui::setMode(int newGuiMode)
guiMode = newGuiMode; guiMode = newGuiMode;
// refresh texture editor surface reference if ( guiMode == ofxGuiMode::SOURCE_SELECTION ) {
textureEditor.setSurface(surfaceManager->getSelectedSurface()); sourcesEditor.enable();
} else {
sourcesEditor.disable();
}
if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) {
// refresh texture editor surface reference
textureEditor.setSurface(surfaceManager->getSelectedSurface());
}
} }
void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight() void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight()
@ -212,4 +228,17 @@ void ofxSurfaceManagerGui::startDrag()
void ofxSurfaceManagerGui::stopDrag() void ofxSurfaceManagerGui::stopDrag()
{ {
bDrag = false; bDrag = false;
}
void ofxSurfaceManagerGui::gotMessage(ofMessage& msg)
{
cout << msg.message << endl;
if ( msg.message == "imageLoaded" ) {
// assign texture to selected source
if (surfaceManager->getSelectedSurface() == NULL){
return;
}
surfaceManager->getSelectedSurface()->setTexture( sourcesEditor.getTexture(sourcesEditor.getLoadedTexCount()-1) );
}
} }

1
src/ofxSurfaceManagerGui.h

@ -30,6 +30,7 @@ public:
void drawSelectedSurfaceTextureHighlight(); void drawSelectedSurfaceTextureHighlight();
void startDrag(); void startDrag();
void stopDrag(); void stopDrag();
void gotMessage(ofMessage& msg);
private: private:
ofxSurfaceManager* surfaceManager; ofxSurfaceManager* surfaceManager;

Loading…
Cancel
Save