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
------------
None so far.
ofxUI
ofxXmlSettings
Compatibility
------------

113
src/ofxSourcesEditor.cpp

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

1
src/ofxSurfaceManager.h

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

33
src/ofxSurfaceManagerGui.cpp

@ -6,11 +6,13 @@ ofxSurfaceManagerGui::ofxSurfaceManagerGui()
guiMode = ofxGuiMode::NONE;
bDrag = false;
registerMouseEvents();
ofRegisterGetMessages(this);
}
ofxSurfaceManagerGui::~ofxSurfaceManagerGui()
{
unregisterMouseEvents();
ofUnregisterGetMessages(this);
surfaceManager = NULL;
}
@ -68,6 +70,12 @@ void ofxSurfaceManagerGui::draw()
projectionEditor.draw();
} else if ( guiMode == ofxGuiMode::SOURCE_SELECTION ) {
// draw projection surfaces first
surfaceManager->draw();
// highlight selected surface
drawSelectedSurfaceHighlight();
sourcesEditor.draw();
}
}
@ -173,8 +181,16 @@ void ofxSurfaceManagerGui::setMode(int newGuiMode)
guiMode = newGuiMode;
// refresh texture editor surface reference
textureEditor.setSurface(surfaceManager->getSelectedSurface());
if ( guiMode == ofxGuiMode::SOURCE_SELECTION ) {
sourcesEditor.enable();
} else {
sourcesEditor.disable();
}
if ( guiMode == ofxGuiMode::TEXTURE_MAPPING ) {
// refresh texture editor surface reference
textureEditor.setSurface(surfaceManager->getSelectedSurface());
}
}
void ofxSurfaceManagerGui::drawSelectedSurfaceHighlight()
@ -212,4 +228,17 @@ void ofxSurfaceManagerGui::startDrag()
void ofxSurfaceManagerGui::stopDrag()
{
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 startDrag();
void stopDrag();
void gotMessage(ofMessage& msg);
private:
ofxSurfaceManager* surfaceManager;

Loading…
Cancel
Save