From 42e8a8590f36b2506daa0c1c498fc465c181465f Mon Sep 17 00:00:00 2001
From: Krisjanis Rijnieks <krisjanis.rijnieks@gmail.com>
Date: Mon, 26 Sep 2016 20:13:43 +0300
Subject: [PATCH] Make Gui the owner of TextureEditorWidget

---
 .../States/TextureMappingState.cpp            | 61 +++++++++++++------
 src/Commands/SetApplicationStateCmd.cpp       |  3 +
 src/Commands/SetApplicationStateCmd.h         |  1 +
 src/Gui/Gui.cpp                               |  4 ++
 src/Gui/Gui.h                                 |  4 ++
 src/Gui/Widgets/TextureEditorWidget.cpp       |  8 ++-
 src/Gui/Widgets/TextureEditorWidget.h         | 10 +--
 src/Surfaces/SurfaceManagerGui.cpp            |  4 ++
 src/Surfaces/SurfaceManagerGui.h              |  6 +-
 9 files changed, 69 insertions(+), 32 deletions(-)

diff --git a/src/Application/States/TextureMappingState.cpp b/src/Application/States/TextureMappingState.cpp
index 7520f49..9ff6bb2 100644
--- a/src/Application/States/TextureMappingState.cpp
+++ b/src/Application/States/TextureMappingState.cpp
@@ -20,7 +20,8 @@ TextureMappingState::TextureMappingState(){
 }
 
 void TextureMappingState::update(Application * app){
-	app->getGui()->getTextureEditor()->update();
+	//app->getGui()->getTextureEditor()->update();
+	Gui::instance()->getTextureEditorWidget().update();
 }
 
 void TextureMappingState::draw(Application * app){
@@ -70,7 +71,8 @@ void TextureMappingState::draw(Application * app){
 	Gui::instance()->getTextureHighlightWidget().draw();
 	
 	// TODO: Replace with a transform widget.
-	app->getGui()->getTextureEditor()->draw();
+	//app->getGui()->getTextureEditor()->draw();
+	Gui::instance()->getTextureEditorWidget().draw();
 	
 	ofPopMatrix();
 }
@@ -88,29 +90,35 @@ void TextureMappingState::onKeyPressed(Application * app, ofKeyEventArgs & args)
 	switch(args.key){
 
 	 case OF_KEY_LEFT:
-		 app->getGui()->getTextureEditor()->moveSelection(ofVec2f(-moveStep, 0.0f));
+		 //app->getGui()->getTextureEditor()->moveSelection(ofVec2f(-moveStep, 0.0f));
+		 Gui::instance()->getTextureEditorWidget().moveSelection(ofVec2f(-moveStep, 0.0f));
 		 break;
 
 	 case OF_KEY_RIGHT:
-		 app->getGui()->getTextureEditor()->moveSelection(ofVec2f(moveStep, 0.0f));
+		 //app->getGui()->getTextureEditor()->moveSelection(ofVec2f(moveStep, 0.0f));
+		 Gui::instance()->getTextureEditorWidget().moveSelection(ofVec2f(moveStep, 0.0f));
 		 break;
 
 	 case OF_KEY_UP:
-		 app->getGui()->getTextureEditor()->moveSelection(ofVec2f(0.0f, -moveStep));
+		 //app->getGui()->getTextureEditor()->moveSelection(ofVec2f(0.0f, -moveStep));
+		 Gui::instance()->getTextureEditorWidget().moveSelection(ofVec2f(0.0f, -moveStep));
 		 break;
 
 	 case OF_KEY_DOWN:
-		 app->getGui()->getTextureEditor()->moveSelection(ofVec2f(0.0f, moveStep));
+		 //app->getGui()->getTextureEditor()->moveSelection(ofVec2f(0.0f, moveStep));
+		 Gui::instance()->getTextureEditorWidget().moveSelection(ofVec2f(0.0f, moveStep));
 		 break;
 
 	 case '>':
 		 app->getCmdManager()->exec(
-			 new SelNextTexCoordCmd(app->getGui()->getTextureEditor()));
+			 //new SelNextTexCoordCmd(app->getGui()->getTextureEditor()));
+			 new SelNextTexCoordCmd(&Gui::instance()->getTextureEditorWidget()));
 		 break;
 		 
 	 case '<':
 		 app->getCmdManager()->exec(
-			 new SelPrevTexCoordCmd(app->getGui()->getTextureEditor()));
+			 //new SelPrevTexCoordCmd(app->getGui()->getTextureEditor()));
+			 new SelPrevTexCoordCmd(&Gui::instance()->getTextureEditorWidget()));
 		 break;
 	 
 	 case ' ':
@@ -151,8 +159,10 @@ void TextureMappingState::onKeyPressed(Application * app, ofKeyEventArgs & args)
 void TextureMappingState::onBackgroundPressed(Application * app, GuiBackgroundEvent & e){
 	// Exec the command only if a joint is selected.
 	bool selected = false;
-	for(unsigned int i = 0; i < app->getGui()->getTextureEditor()->getJoints().size(); ++i){
-		if(app->getGui()->getTextureEditor()->getJoints()[i]->selected){
+	//for(unsigned int i = 0; i < app->getGui()->getTextureEditor()->getJoints().size(); ++i){
+	for(unsigned int i = 0; i < Gui::instance()->getTextureEditorWidget().getJoints().size(); ++i){
+		//if(app->getGui()->getTextureEditor()->getJoints()[i]->selected){
+		if(Gui::instance()->getTextureEditorWidget().getJoints()[i]->selected){
 			selected = true;
 			break;
 		}
@@ -160,7 +170,8 @@ void TextureMappingState::onBackgroundPressed(Application * app, GuiBackgroundEv
 	
 	if(selected){
 		app->getCmdManager()->exec(
-			new DeselectTexCoordCmd(app->getGui()->getTextureEditor()));
+			//new DeselectTexCoordCmd(app->getGui()->getTextureEditor()));
+			new DeselectTexCoordCmd(&Gui::instance()->getTextureEditorWidget()));
 	}
 	
 	_bTranslateCanvas = true;
@@ -179,26 +190,32 @@ void TextureMappingState::onMousePressed(Application * app, ofMouseEventArgs & a
 	}
 	
 	// Old code from SurfaceManagerGui
-	CircleJoint * hitJoint = app->getGui()->getTextureEditor()->hitTestJoints(ofVec2f(args.x, args.y));
+	//CircleJoint * hitJoint = app->getGui()->getTextureEditor()->hitTestJoints(ofVec2f(args.x, args.y));
+	CircleJoint * hitJoint =
+		Gui::instance()->getTextureEditorWidget().hitTestJoints(ofVec2f(args.x, args.y));
 	
 	if(hitJoint != 0){
 	
 		hitJoint->mousePressed(args);
 		
-		app->getGui()->getTextureEditor()->unselectAllJoints();
+		//app->getGui()->getTextureEditor()->unselectAllJoints();
+		Gui::instance()->getTextureEditorWidget().unselectAllJoints();
 		hitJoint->select();
 		hitJoint->startDrag();
 		int jointIndex;
 		
-		for(int i = 0; i < app->getGui()->getTextureEditor()->getJoints().size(); i++){
-			if(app->getGui()->getTextureEditor()->getJoints()[i] == hitJoint){
+		//for(int i = 0; i < app->getGui()->getTextureEditor()->getJoints().size(); i++){
+		for(int i = 0; i < Gui::instance()->getTextureEditorWidget().getJoints().size(); i++){
+			//if(app->getGui()->getTextureEditor()->getJoints()[i] == hitJoint){
+			if(Gui::instance()->getTextureEditorWidget().getJoints()[i] == hitJoint){
 				jointIndex = i;
 				break;
 			}
 		}
 
 		app->getCmdManager()->exec(
-			new MvTexCoordCmd(jointIndex, app->getGui()->getTextureEditor()));
+			//new MvTexCoordCmd(jointIndex, app->getGui()->getTextureEditor()));
+			new MvTexCoordCmd(jointIndex, &Gui::instance()->getTextureEditorWidget()));
 		
 	}else if(app->getSurfaceManager()->getSelectedSurface()->getTextureHitArea().inside(args.x, args.y)){
 		
@@ -208,7 +225,8 @@ void TextureMappingState::onMousePressed(Application * app, ofMouseEventArgs & a
 		// TODO: emit event through the gui singleton
 		app->getCmdManager()->exec(new MvAllTexCoordsCmd(
 			app->getSurfaceManager()->getSelectedSurface(),
-			app->getGui()->getTextureEditor()));
+			//app->getGui()->getTextureEditor()));
+			&Gui::instance()->getTextureEditorWidget()));
 
 	}else{
            Gui::instance()->notifyBackgroundPressed(args);
@@ -233,7 +251,8 @@ void TextureMappingState::onMouseReleased(Application * app, ofMouseEventArgs &
 	args.y -= _canvasTranslate.y;
 
 	app->getGui()->stopDrag();
-	app->getGui()->getTextureEditor()->stopDragJoints();
+	//app->getGui()->getTextureEditor()->stopDragJoints();
+	Gui::instance()->getTextureEditorWidget().stopDragJoints();
 }
 
 // TODO: Handle app->getGui()->clickPosition and app->getGui()->bDrag locally.
@@ -241,12 +260,14 @@ void TextureMappingState::onMouseDragged(Application * app, ofMouseEventArgs & a
 	if(!_bTranslateCanvas){
 		args.x -= _canvasTranslate.x;
 		args.y -= _canvasTranslate.y;
-		app->getGui()->getTextureEditor()->mouseDragged(args);
+		//app->getGui()->getTextureEditor()->onMouseDragged(args);
+		Gui::instance()->getTextureEditorWidget().onMouseDragged(args);
 		
 		if(app->getGui()->bDrag){
 			ofVec2f mousePosition = ofVec2f(args.x, args.y);
 			ofVec2f distance = mousePosition - app->getGui()->clickPosition;
-			app->getGui()->getTextureEditor()->moveTexCoords(distance);
+			//app->getGui()->getTextureEditor()->moveTexCoords(distance);
+			Gui::instance()->getTextureEditorWidget().moveTexCoords(distance);
 			app->getGui()->clickPosition = mousePosition;
 		}
 	}else{
diff --git a/src/Commands/SetApplicationStateCmd.cpp b/src/Commands/SetApplicationStateCmd.cpp
index 8246b84..f9a2f8e 100644
--- a/src/Commands/SetApplicationStateCmd.cpp
+++ b/src/Commands/SetApplicationStateCmd.cpp
@@ -23,6 +23,9 @@ void SetApplicationStateCmd::exec(){
 	_application->setState(_applicationState);
 	_applicationState->setTranslation(ofPoint(0, 0));
 	_translation = _prevApplicationState->getTranslation();
+	
+	Gui::instance()->getTextureEditorWidget().setSurface(
+		_application->getSurfaceManager()->getSelectedSurface());
 
 	// TODO: To be removed.
 	_prevGuiMode = _gui->getMode();
diff --git a/src/Commands/SetApplicationStateCmd.h b/src/Commands/SetApplicationStateCmd.h
index 61daf9a..cf386f3 100644
--- a/src/Commands/SetApplicationStateCmd.h
+++ b/src/Commands/SetApplicationStateCmd.h
@@ -3,6 +3,7 @@
 #include "BaseCmd.h"
 #include "Application.h"
 #include "SurfaceManagerGui.h"
+#include "Gui.h"
 
 namespace ofx {
 namespace piMapper {
diff --git a/src/Gui/Gui.cpp b/src/Gui/Gui.cpp
index 5688009..a4af37e 100644
--- a/src/Gui/Gui.cpp
+++ b/src/Gui/Gui.cpp
@@ -96,6 +96,10 @@ TextureHighlightWidget & Gui::getTextureHighlightWidget(){
 	return _textureHighlightWidget;
 }
 
+TextureEditorWidget & Gui::getTextureEditorWidget(){
+	return _textureEditorWidget;
+}
+
 void Gui::onScaleWidgetEvent(GuiWidgetEvent & event){
 	GuiEvent e;
 	e.args = event.args;
diff --git a/src/Gui/Gui.h b/src/Gui/Gui.h
index e9de336..3b6672a 100644
--- a/src/Gui/Gui.h
+++ b/src/Gui/Gui.h
@@ -8,6 +8,7 @@
 #include "LayerPanelWidget.h"
 #include "SurfaceHighlightWidget.h"
 #include "TextureHighlightWidget.h"
+#include "TextureEditorWidget.h"
 
 namespace ofx {
 namespace piMapper {
@@ -67,7 +68,9 @@ class Gui {
 		LayerPanelWidget & getLayerPanelWidget();
 		SurfaceHighlightWidget & getSurfaceHighlightWidget();
 		TextureHighlightWidget & getTextureHighlightWidget();
+		TextureEditorWidget & getTextureEditorWidget();
 	
+		// Consider these as a part of the application states/modes.
 		void onMousePressed(ofMouseEventArgs & args);
 		void onMouseReleased(ofMouseEventArgs & args);
 		void onMouseDragged(ofMouseEventArgs & args);
@@ -86,6 +89,7 @@ class Gui {
 		LayerPanelWidget _layerPanelWidget;
 		SurfaceHighlightWidget _surfaceHighlightWidget;
 		TextureHighlightWidget _textureHighlightWidget;
+		TextureEditorWidget _textureEditorWidget;
 };
 
 } // piMapper
diff --git a/src/Gui/Widgets/TextureEditorWidget.cpp b/src/Gui/Widgets/TextureEditorWidget.cpp
index a77462c..21b17c2 100644
--- a/src/Gui/Widgets/TextureEditorWidget.cpp
+++ b/src/Gui/Widgets/TextureEditorWidget.cpp
@@ -79,13 +79,17 @@ void TextureEditorWidget::update(){
 	} // else
 }
 
-void TextureEditorWidget::mousePressed(ofMouseEventArgs & args){
+void TextureEditorWidget::onMousePressed(ofMouseEventArgs & args){
 	for(unsigned int i = 0; i < joints.size(); ++i){
 		joints[i]->mousePressed(args);
 	}
 }
 
-void TextureEditorWidget::mouseDragged(ofMouseEventArgs & args){
+void TextureEditorWidget::onMouseReleased(ofMouseEventArgs & args){
+	// Nothing
+}
+
+void TextureEditorWidget::onMouseDragged(ofMouseEventArgs & args){
 	for(unsigned int i = 0; i < joints.size(); ++i){
 		joints[i]->mouseDragged(args);
 	}
diff --git a/src/Gui/Widgets/TextureEditorWidget.h b/src/Gui/Widgets/TextureEditorWidget.h
index 6024091..f904410 100644
--- a/src/Gui/Widgets/TextureEditorWidget.h
+++ b/src/Gui/Widgets/TextureEditorWidget.h
@@ -20,13 +20,9 @@ class TextureEditorWidget : public GuiBaseWidget {
 		void update(); // Maybe the Application pointer would make sense there. Not sure yet.
 		void draw();
 	
-		// These come from GuiBaseWidget
-		void onMousePressed(ofMouseEventArgs & e){}
-		void onMouseReleased(ofMouseEventArgs & e){}
-		void onMouseDragged(ofMouseEventArgs & e){}
-	
-		void mousePressed(ofMouseEventArgs & args);
-		void mouseDragged(ofMouseEventArgs & args);
+		void onMousePressed(ofMouseEventArgs & e);
+		void onMouseReleased(ofMouseEventArgs & e);
+		void onMouseDragged(ofMouseEventArgs & e);
 	
 		bool inside(float x, float y){ return false; }
 	
diff --git a/src/Surfaces/SurfaceManagerGui.cpp b/src/Surfaces/SurfaceManagerGui.cpp
index 1291f7d..4ebf4cf 100644
--- a/src/Surfaces/SurfaceManagerGui.cpp
+++ b/src/Surfaces/SurfaceManagerGui.cpp
@@ -63,9 +63,11 @@ void SurfaceManagerGui::setMode(int newGuiMode){
 		sourcesEditor.disable();
 	}
 
+	/*
 	if(guiMode == GuiMode::TEXTURE_MAPPING){
 		textureEditor.setSurface(surfaceManager->getSelectedSurface());
 	}
+	*/
 
 	if(guiMode == GuiMode::PROJECTION_MAPPING){
 		projectionEditor.enable();
@@ -90,9 +92,11 @@ ProjectionEditor * SurfaceManagerGui::getProjectionEditor(){
 	return &projectionEditor;
 }
 
+/*
 TextureEditorWidget * SurfaceManagerGui::getTextureEditor(){
 	return &textureEditor;
 }
+*/
 
 SourcesEditor * SurfaceManagerGui::getSourcesEditor(){
 	return &sourcesEditor;
diff --git a/src/Surfaces/SurfaceManagerGui.h b/src/Surfaces/SurfaceManagerGui.h
index cd16300..7f56914 100644
--- a/src/Surfaces/SurfaceManagerGui.h
+++ b/src/Surfaces/SurfaceManagerGui.h
@@ -6,7 +6,7 @@
 #include "ofGraphics.h"
 
 #include "SurfaceManager.h"
-#include "TextureEditorWidget.h"
+//#include "TextureEditorWidget.h"
 #include "ProjectionEditor.h"
 #include "SourcesEditor.h"
 #include "GuiMode.h"
@@ -37,7 +37,7 @@ class SurfaceManagerGui {
 		void stopDrag();
 	
 		ProjectionEditor * getProjectionEditor();
-		TextureEditorWidget * getTextureEditor();
+		//TextureEditorWidget * getTextureEditor();
 		SourcesEditor * getSourcesEditor();
 	
 		void onVertexChanged(int & i);
@@ -52,7 +52,7 @@ class SurfaceManagerGui {
 	private:
 		SurfaceManager * surfaceManager;
 		MediaServer * mediaServer;
-		TextureEditorWidget textureEditor;
+		//TextureEditorWidget textureEditor;
 		ProjectionEditor projectionEditor;
 		SourcesEditor sourcesEditor;