1 | /*
|
---|
2 | * TranslateAction.cpp
|
---|
3 | *
|
---|
4 | * Created on: May 10, 2010
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "Helpers/MemDebug.hpp"
|
---|
9 |
|
---|
10 | #include "Actions/AtomAction/TranslateAction.hpp"
|
---|
11 | #include "Actions/ActionRegistry.hpp"
|
---|
12 | #include "Helpers/Log.hpp"
|
---|
13 | #include "atom.hpp"
|
---|
14 | #include "LinearAlgebra/Vector.hpp"
|
---|
15 | #include "Helpers/Verbose.hpp"
|
---|
16 | #include "World.hpp"
|
---|
17 |
|
---|
18 | #include <iostream>
|
---|
19 | #include <fstream>
|
---|
20 | #include <string>
|
---|
21 |
|
---|
22 | using namespace std;
|
---|
23 |
|
---|
24 | #include "UIElements/UIFactory.hpp"
|
---|
25 | #include "UIElements/Dialog.hpp"
|
---|
26 | #include "Actions/ValueStorage.hpp"
|
---|
27 |
|
---|
28 | /****** AtomTranslateAction *****/
|
---|
29 |
|
---|
30 | // memento to remember the state when undoing
|
---|
31 |
|
---|
32 | class AtomTranslateState : public ActionState {
|
---|
33 | public:
|
---|
34 | AtomTranslateState(std::vector<atom*> _selectedAtoms, Vector &_v) :
|
---|
35 | selectedAtoms(_selectedAtoms),
|
---|
36 | v(_v)
|
---|
37 | {}
|
---|
38 | std::vector<atom*> selectedAtoms;
|
---|
39 | Vector v;
|
---|
40 | };
|
---|
41 |
|
---|
42 | const char AtomTranslateAction::NAME[] = "translate-atoms";
|
---|
43 |
|
---|
44 | AtomTranslateAction::AtomTranslateAction() :
|
---|
45 | Action(NAME)
|
---|
46 | {}
|
---|
47 |
|
---|
48 | AtomTranslateAction::~AtomTranslateAction()
|
---|
49 | {}
|
---|
50 |
|
---|
51 | void AtomTranslate(Vector &x) {
|
---|
52 | ValueStorage::getInstance().setCurrentValue(AtomTranslateAction::NAME, x);
|
---|
53 | ActionRegistry::getInstance().getActionByName(AtomTranslateAction::NAME)->call(Action::NonInteractive);
|
---|
54 | };
|
---|
55 |
|
---|
56 | Dialog* AtomTranslateAction::fillDialog(Dialog *dialog) {
|
---|
57 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
58 |
|
---|
59 | dialog->queryVector(NAME, false, ValueStorage::getInstance().getDescription(NAME));
|
---|
60 |
|
---|
61 | return dialog;
|
---|
62 | }
|
---|
63 |
|
---|
64 | Action::state_ptr AtomTranslateAction::performCall() {
|
---|
65 | Vector v;
|
---|
66 | std::vector<atom *> selectedAtoms = World::getInstance().getSelectedAtoms();
|
---|
67 |
|
---|
68 | ValueStorage::getInstance().queryCurrentValue(NAME, v);
|
---|
69 |
|
---|
70 | // TODO: use AtomSet::translate
|
---|
71 | for (std::vector<atom *>::iterator iter = selectedAtoms.begin(); iter != selectedAtoms.end(); ++iter) {
|
---|
72 | *(*iter) += v;
|
---|
73 | }
|
---|
74 | return Action::state_ptr(new AtomTranslateState(selectedAtoms, v));
|
---|
75 | }
|
---|
76 |
|
---|
77 | Action::state_ptr AtomTranslateAction::performUndo(Action::state_ptr _state) {
|
---|
78 | AtomTranslateState *state = assert_cast<AtomTranslateState*>(_state.get());
|
---|
79 |
|
---|
80 | for (std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter) {
|
---|
81 | *(*iter) -= state->v;
|
---|
82 | }
|
---|
83 |
|
---|
84 | return Action::state_ptr(new AtomTranslateState(state->selectedAtoms, state->v));
|
---|
85 | }
|
---|
86 |
|
---|
87 | Action::state_ptr AtomTranslateAction::performRedo(Action::state_ptr _state){
|
---|
88 | AtomTranslateState *state = assert_cast<AtomTranslateState*>(_state.get());
|
---|
89 |
|
---|
90 | for (std::vector<atom *>::iterator iter = state->selectedAtoms.begin(); iter != state->selectedAtoms.end(); ++iter) {
|
---|
91 | *(*iter) += state->v;
|
---|
92 | }
|
---|
93 |
|
---|
94 | return Action::state_ptr(new AtomTranslateState(state->selectedAtoms, state->v));
|
---|
95 | }
|
---|
96 |
|
---|
97 | bool AtomTranslateAction::canUndo() {
|
---|
98 | return true;
|
---|
99 | }
|
---|
100 |
|
---|
101 | bool AtomTranslateAction::shouldUndo() {
|
---|
102 | return true;
|
---|
103 | }
|
---|
104 |
|
---|
105 | const string AtomTranslateAction::getName() {
|
---|
106 | return NAME;
|
---|
107 | }
|
---|