Browse Source

Optimize TextureMappingMode tex coord moving exec/undo

master
Krisjanis Rijnieks 9 years ago
parent
commit
099e25baff
  1. 57
      src/Application/Modes/TextureMappingMode.cpp
  2. 2
      src/Application/Modes/TextureMappingMode.h
  3. 21
      src/Commands/MvTexCoordCmd.cpp
  4. 10
      src/Commands/MvTexCoordCmd.h
  5. 22
      src/Commands/SaveTexCoordPosCmd.cpp
  6. 29
      src/Commands/SaveTexCoordPosCmd.h
  7. 24
      src/Commands/SelTexCoordCmd.cpp
  8. 27
      src/Commands/SelTexCoordCmd.h
  9. 14
      src/Gui/Widgets/TextureEditorWidget.cpp
  10. 2
      src/Gui/Widgets/TextureEditorWidget.h

57
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(){

2
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 {

21
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

10
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;
};

22
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

29
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

24
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

27
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

14
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();

2
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);

Loading…
Cancel
Save