From 099e25baffcd9a6f36c063d9d4a601a2bee7067e Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Sun, 30 Oct 2016 21:04:29 +0200 Subject: [PATCH] Optimize TextureMappingMode tex coord moving exec/undo --- src/Application/Modes/TextureMappingMode.cpp | 57 ++++++++++++++------ src/Application/Modes/TextureMappingMode.h | 2 + src/Commands/MvTexCoordCmd.cpp | 21 +++++--- src/Commands/MvTexCoordCmd.h | 10 ++-- src/Commands/SaveTexCoordPosCmd.cpp | 22 ++++++++ src/Commands/SaveTexCoordPosCmd.h | 29 ++++++++++ src/Commands/SelTexCoordCmd.cpp | 24 +++++++++ src/Commands/SelTexCoordCmd.h | 27 ++++++++++ src/Gui/Widgets/TextureEditorWidget.cpp | 14 +++++ src/Gui/Widgets/TextureEditorWidget.h | 2 + 10 files changed, 178 insertions(+), 30 deletions(-) create mode 100644 src/Commands/SaveTexCoordPosCmd.cpp create mode 100644 src/Commands/SaveTexCoordPosCmd.h create mode 100644 src/Commands/SelTexCoordCmd.cpp create mode 100644 src/Commands/SelTexCoordCmd.h diff --git a/src/Application/Modes/TextureMappingMode.cpp b/src/Application/Modes/TextureMappingMode.cpp index b422cd4..5b19da2 100644 --- a/src/Application/Modes/TextureMappingMode.cpp +++ b/src/Application/Modes/TextureMappingMode.cpp @@ -179,30 +179,39 @@ void TextureMappingMode::onMousePressed(Application * app, ofMouseEventArgs & ar Gui::instance()->getTextureEditorWidget().hitTestJoints(ofVec2f(args.x, args.y)); if(hitJoint != 0){ - hitJoint->mousePressed(args); + //Gui::instance()->getTextureEditorWidget().unselectAllJoints(); - Gui::instance()->getTextureEditorWidget().unselectAllJoints(); - hitJoint->select(); - hitJoint->startDrag(); - int jointIndex; + //hitJoint->select(); - for(int i = 0; i < Gui::instance()->getTextureEditorWidget().getJoints().size(); i++){ - if(Gui::instance()->getTextureEditorWidget().getJoints()[i] == hitJoint){ - jointIndex = i; + int selectedTexCoord = -1; + for(int i = 0; i < Gui::instance()->getTextureEditorWidget().getJoints().size(); ++i){ + if(hitJoint == Gui::instance()->getTextureEditorWidget().getJoints()[i]){ + selectedTexCoord = i; break; } } - - app->getCmdManager()->exec( - new MvTexCoordCmd(jointIndex, &Gui::instance()->getTextureEditorWidget())); + if(!hitJoint->isSelected()){ + app->getCmdManager()->exec( + new SelTexCoordCmd( + &Gui::instance()->getTextureEditorWidget(), + selectedTexCoord)); + } + + hitJoint->startDrag(); + // TODO: Make the following better, more direct. + // Move this to mouseReleased part as we nee to save the previous location only + // if the move has happened. + ofVec2f tex = app->getSurfaceManager()->getSelectedSurface()->getTexCoords()[selectedTexCoord]; + app->getCmdManager()->exec(new SaveTexCoordPosCmd(selectedTexCoord, tex)); }else if(app->getSurfaceManager()->getSelectedSurface()->getTextureHitArea().inside(args.x, args.y)){ _clickPosition = ofPoint(args.x, args.y); _bCropAreaDrag = true; - // TODO: emit event through the gui singleton + // TODO: emit event through the gui singleton. + // TODO: create command only on mouse release. app->getCmdManager()->exec(new MvAllTexCoordsCmd( app->getSurfaceManager()->getSelectedSurface(), &Gui::instance()->getTextureEditorWidget())); @@ -224,6 +233,20 @@ void TextureMappingMode::onMouseReleased(Application * app, ofMouseEventArgs & a _canvasTranslate)); } + // If selected joint is being dragged and the mouse position has been changed + // create an undoable move tex coord command. + int selectedTexCoord = Gui::instance()->getTextureEditorWidget().getSelectedTexCoord(); + if(selectedTexCoord >= 0){ + ofPoint mouseReleasePosition = ofPoint(args.x, args.y); + if(_clickPosition != mouseReleasePosition){ + ofVec2f moveBy = ofVec2f( + mouseReleasePosition.x - _clickPosition.x, + mouseReleasePosition.y - _clickPosition.y); + //app->getCmdManager()->exec( + // new MvTexCoordCmd(selectedTexCoord, moveBy)); + } + } + _clickCanvasTranslate = _canvasTranslate; args.x -= _canvasTranslate.x; @@ -270,18 +293,18 @@ void TextureMappingMode::drawTexture(Application * app){ void TextureMappingMode::moveSelection(Application * app, ofVec2f by){ int selectedTexCoord = Gui::instance()->getTextureEditorWidget().getSelectedTexCoord(); + // TODO: Do the moving only through commands not like now. + if(selectedTexCoord >= 0){ app->getCmdManager()->exec( - new MvTexCoordCmd( - selectedTexCoord, - &Gui::instance()->getTextureEditorWidget())); + new MvTexCoordCmd(selectedTexCoord, by)); }else{ app->getCmdManager()->exec(new MvAllTexCoordsCmd( app->getSurfaceManager()->getSelectedSurface(), &Gui::instance()->getTextureEditorWidget())); + + Gui::instance()->getTextureEditorWidget().moveSelection(by); } - - Gui::instance()->getTextureEditorWidget().moveSelection(by); } ofPoint TextureMappingMode::getTranslation(){ diff --git a/src/Application/Modes/TextureMappingMode.h b/src/Application/Modes/TextureMappingMode.h index 997c7bb..ed7c42f 100644 --- a/src/Application/Modes/TextureMappingMode.h +++ b/src/Application/Modes/TextureMappingMode.h @@ -14,6 +14,8 @@ #include "SetTexMapDrawModeCmd.h" #include "MvTexCoordCmd.h" #include "MvAllTexCoordsCmd.h" +#include "SaveTexCoordPosCmd.h" +#include "SelTexCoordCmd.h" #include "Gui.h" namespace ofx { diff --git a/src/Commands/MvTexCoordCmd.cpp b/src/Commands/MvTexCoordCmd.cpp index 80cfc8c..762a09d 100644 --- a/src/Commands/MvTexCoordCmd.cpp +++ b/src/Commands/MvTexCoordCmd.cpp @@ -3,22 +3,27 @@ namespace ofx { namespace piMapper { -MvTexCoordCmd::MvTexCoordCmd(int jointIndex, TextureEditorWidget * texEditor){ - _jointIndex = jointIndex; - _texEditor = texEditor; +MvTexCoordCmd::MvTexCoordCmd(int texCoordIndex, ofVec2f by){ + _texCoordIndex = texCoordIndex; + _moveBy = by; } void MvTexCoordCmd::exec(){ ofLogNotice("MvTexCoordCmd", "exec"); - _jointPosition = _texEditor->getJoints()[_jointIndex]->position; + _positionBefore = + Gui::instance()->getTextureEditorWidget().getJoints()[_texCoordIndex]->position; + Gui::instance()->getTextureEditorWidget().moveSelection(_moveBy); } void MvTexCoordCmd::undo(){ ofLogNotice("MvTexCoordCmd", "undo"); - _texEditor->unselectAllJoints(); - _texEditor->getJoints()[_jointIndex]->select(); - _texEditor->getJoints()[_jointIndex]->position = _jointPosition; - _texEditor = 0; + // TODO: Set position exactly to the one stored in _positionBefore + Gui::instance()->getTextureEditorWidget().moveSelection(-_moveBy); + + //_texEditor->unselectAllJoints(); + //_texEditor->getJoints()[_jointIndex]->select(); + //_texEditor->getJoints()[_jointIndex]->position = _jointPosition; + //_texEditor = 0; } } // namespace piMapper diff --git a/src/Commands/MvTexCoordCmd.h b/src/Commands/MvTexCoordCmd.h index 8841b31..beb046f 100644 --- a/src/Commands/MvTexCoordCmd.h +++ b/src/Commands/MvTexCoordCmd.h @@ -6,7 +6,7 @@ #include "BaseCmd.h" #include "CircleJoint.h" -#include "TextureEditorWidget.h" +#include "Gui.h" namespace ofx { namespace piMapper { @@ -14,14 +14,14 @@ namespace piMapper { class MvTexCoordCmd : public BaseUndoCmd { public: - MvTexCoordCmd(int jointIndex, TextureEditorWidget * texEditor); + MvTexCoordCmd(int texCoordIndex, ofVec2f by); void exec(); void undo(); private: - ofVec2f _jointPosition; - int _jointIndex; - TextureEditorWidget * _texEditor; + int _texCoordIndex; + ofVec2f _moveBy; + ofVec2f _positionBefore; }; diff --git a/src/Commands/SaveTexCoordPosCmd.cpp b/src/Commands/SaveTexCoordPosCmd.cpp new file mode 100644 index 0000000..0c255e6 --- /dev/null +++ b/src/Commands/SaveTexCoordPosCmd.cpp @@ -0,0 +1,22 @@ +#include "SaveTexCoordPosCmd.h" + +namespace ofx { +namespace piMapper { + +SaveTexCoordPosCmd::SaveTexCoordPosCmd(int texCoordIndex, ofVec2f position){ + _texCoordIndex = texCoordIndex; + _position = position; +} + +void SaveTexCoordPosCmd::exec(){ + ofLogNotice("SaveTexCoordPosCmd", "exec"); +} + +void SaveTexCoordPosCmd::undo(){ + ofLogNotice("SaveTexCoordPosCmd", "undo"); + Gui::instance()->getTextureEditorWidget().moveTexCoordTo(_texCoordIndex, _position); +} + +} // namespace piMapper +} // namespace ofx + diff --git a/src/Commands/SaveTexCoordPosCmd.h b/src/Commands/SaveTexCoordPosCmd.h new file mode 100644 index 0000000..4b9ed5f --- /dev/null +++ b/src/Commands/SaveTexCoordPosCmd.h @@ -0,0 +1,29 @@ +// SaveTexCoordPosCmd +// Saves current position of the texture coordinate and resets it. +// Created by Krisjanis Rijnieks 2016-10-30 + +#pragma once + +#include "BaseCmd.h" +#include "CircleJoint.h" +#include "Gui.h" + +namespace ofx { +namespace piMapper { + +class SaveTexCoordPosCmd : public BaseUndoCmd { + + public: + SaveTexCoordPosCmd(int texCoordIndex, ofVec2f position); + void exec(); + void undo(); + + private: + int _texCoordIndex; + ofVec2f _position; + +}; + +} // namespace piMapper +} // namespace ofx + diff --git a/src/Commands/SelTexCoordCmd.cpp b/src/Commands/SelTexCoordCmd.cpp new file mode 100644 index 0000000..f414e63 --- /dev/null +++ b/src/Commands/SelTexCoordCmd.cpp @@ -0,0 +1,24 @@ +#include "SelTexCoordCmd.h" + +namespace ofx { +namespace piMapper { + +SelTexCoordCmd::SelTexCoordCmd(TextureEditorWidget * te, int texCoordIndex){ + _textureEditor = te; + _texCoordIndex = texCoordIndex; +} + +void SelTexCoordCmd::exec(){ + ofLogNotice("SelTexCoordCmd", "exec"); + _prevSelectionIndex = _textureEditor->getSelectedTexCoord(); + _textureEditor->selectTexCoord(_texCoordIndex); +} + +void SelTexCoordCmd::undo(){ + ofLogNotice("SelTexCoordCmd", "undo"); + _textureEditor->selectTexCoord(_prevSelectionIndex); +} + +} // namespace piMapper +} // namespace ofx + diff --git a/src/Commands/SelTexCoordCmd.h b/src/Commands/SelTexCoordCmd.h new file mode 100644 index 0000000..a297de3 --- /dev/null +++ b/src/Commands/SelTexCoordCmd.h @@ -0,0 +1,27 @@ +#pragma once + +#include "BaseCmd.h" +#include "TextureEditorWidget.h" + +class ofxPiMapper; + +namespace ofx { +namespace piMapper { + +class SelTexCoordCmd : public BaseUndoCmd { + + public: + SelTexCoordCmd(TextureEditorWidget * te, int texCoordIndex); + void exec(); + void undo(); + + private: + TextureEditorWidget * _textureEditor; + int _texCoordIndex; + int _prevSelectionIndex; + +}; + +} // namespace piMapper +} // namespace ofx + diff --git a/src/Gui/Widgets/TextureEditorWidget.cpp b/src/Gui/Widgets/TextureEditorWidget.cpp index f72d767..2f0a3c0 100644 --- a/src/Gui/Widgets/TextureEditorWidget.cpp +++ b/src/Gui/Widgets/TextureEditorWidget.cpp @@ -312,6 +312,20 @@ void TextureEditorWidget::moveTexCoords(ofVec2f by){ } } +void TextureEditorWidget::moveTexCoordTo(int texCoordIndex, ofVec2f position){ + if(surface == 0){ + return; + } + + ofLogNotice("TextureEditorWidget::moveTexCoordTo") << texCoordIndex << ", " << position.x << ", " << position.y; + surface->setTexCoord(texCoordIndex, position); + + ofVec2f textureSize = ofVec2f( + surface->getSource()->getTexture()->getWidth(), + surface->getSource()->getTexture()->getHeight()); + joints[texCoordIndex]->position = position * textureSize; +} + void TextureEditorWidget::stopDragJoints(){ for(int i = 0; i < joints.size(); i++){ joints[i]->stopDrag(); diff --git a/src/Gui/Widgets/TextureEditorWidget.h b/src/Gui/Widgets/TextureEditorWidget.h index 9008719..2495ad6 100644 --- a/src/Gui/Widgets/TextureEditorWidget.h +++ b/src/Gui/Widgets/TextureEditorWidget.h @@ -39,6 +39,8 @@ class TextureEditorWidget : public GuiBaseWidget { void selectPrevTexCoord(); void moveTexCoords(ofVec2f by); + void moveTexCoordTo(int texCoordIndex, ofVec2f position); + void stopDragJoints(); void moveSelection(ofVec2f by); void constrainJointsToQuad(int selectedJointIndex);