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