[97ebf8] | 1 | /*
|
---|
| 2 | * AddAction.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: May 9, 2010
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[112b09] | 8 | #include "Helpers/MemDebug.hpp"
|
---|
| 9 |
|
---|
[97ebf8] | 10 | #include "Actions/AtomAction/AddAction.hpp"
|
---|
[0430e3] | 11 | #include "Actions/ActionRegistry.hpp"
|
---|
[ea2830] | 12 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[97ebf8] | 13 | #include "atom.hpp"
|
---|
| 14 | #include "element.hpp"
|
---|
[952f38] | 15 | #include "Helpers/Log.hpp"
|
---|
[f0a3ec] | 16 | #include "molecule.hpp"
|
---|
[57f243] | 17 | #include "LinearAlgebra/Vector.hpp"
|
---|
[952f38] | 18 | #include "Helpers/Verbose.hpp"
|
---|
[97ebf8] | 19 | #include "World.hpp"
|
---|
| 20 |
|
---|
| 21 | #include <iostream>
|
---|
| 22 | #include <string>
|
---|
| 23 |
|
---|
| 24 | using namespace std;
|
---|
| 25 |
|
---|
| 26 | #include "UIElements/UIFactory.hpp"
|
---|
| 27 | #include "UIElements/Dialog.hpp"
|
---|
[861874] | 28 | #include "Actions/ValueStorage.hpp"
|
---|
[97ebf8] | 29 |
|
---|
[ea2830] | 30 | // memento to remember the state when undoing
|
---|
| 31 |
|
---|
| 32 | class AtomAddState : public ActionState {
|
---|
| 33 | public:
|
---|
| 34 | AtomAddState(const Vector &_position, const element *_elemental, const atomId_t _id) :
|
---|
| 35 | position(_position),
|
---|
| 36 | elemental(_elemental),
|
---|
| 37 | id(_id)
|
---|
| 38 | {}
|
---|
| 39 | Vector position;
|
---|
| 40 | const element *elemental;
|
---|
| 41 | atomId_t id;
|
---|
| 42 | };
|
---|
| 43 |
|
---|
[97ebf8] | 44 | const char AtomAddAction::NAME[] = "add-atom";
|
---|
| 45 |
|
---|
| 46 | AtomAddAction::AtomAddAction() :
|
---|
| 47 | Action(NAME)
|
---|
| 48 | {}
|
---|
| 49 |
|
---|
| 50 | AtomAddAction::~AtomAddAction()
|
---|
| 51 | {}
|
---|
| 52 |
|
---|
[1a8fbf6] | 53 | void AtomAdd(element *elemental, Vector &position) {
|
---|
| 54 | ValueStorage::getInstance().setCurrentValue(AtomAddAction::NAME, elemental);
|
---|
| 55 | ValueStorage::getInstance().setCurrentValue("position", elemental);
|
---|
| 56 | ActionRegistry::getInstance().getActionByName(AtomAddAction::NAME)->call(Action::NonInteractive);
|
---|
| 57 | };
|
---|
| 58 |
|
---|
[047878] | 59 | Dialog * AtomAddAction::fillDialog(Dialog *dialog) {
|
---|
| 60 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
[9d33ba] | 61 |
|
---|
| 62 | dialog->queryElement(NAME, ValueStorage::getInstance().getDescription(NAME));
|
---|
| 63 | dialog->queryVector("position", true, ValueStorage::getInstance().getDescription("position"));
|
---|
| 64 |
|
---|
| 65 | return dialog;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | Action::state_ptr AtomAddAction::performCall() {
|
---|
[e5c0a1] | 69 | const element * elemental = NULL;
|
---|
[97ebf8] | 70 | Vector position;
|
---|
| 71 |
|
---|
[9d33ba] | 72 | // obtain information
|
---|
[85537a] | 73 | ValueStorage::getInstance().queryCurrentValue(NAME, elemental);
|
---|
[9d33ba] | 74 | ValueStorage::getInstance().queryCurrentValue("position", position);
|
---|
| 75 |
|
---|
| 76 | // execute action
|
---|
[85537a] | 77 | atom * first = World::getInstance().createAtom();
|
---|
[d74077] | 78 | first->setType(elemental);
|
---|
| 79 | first->setPosition(position);
|
---|
[b5c53d] | 80 | DoLog(1) && (Log() << Verbose(1) << "Adding new atom with element " << first->getType()->getName() << " at " << (first->getPosition()) << "." << endl);
|
---|
[85537a] | 81 | // TODO: remove when all of World's atoms are stored.
|
---|
| 82 | std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
|
---|
| 83 | if (!molecules.empty()) {
|
---|
| 84 | std::vector<molecule *>::iterator iter = molecules.begin();
|
---|
| 85 | (*iter)->AddAtom(first);
|
---|
[97ebf8] | 86 | }
|
---|
[ea2830] | 87 | return Action::state_ptr(new AtomAddState(position, elemental, first->getId()));
|
---|
[97ebf8] | 88 | }
|
---|
| 89 |
|
---|
| 90 | Action::state_ptr AtomAddAction::performUndo(Action::state_ptr _state) {
|
---|
[ea2830] | 91 | AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
|
---|
[97ebf8] | 92 |
|
---|
[ea2830] | 93 | DoLog(1) && (Log() << Verbose(1) << "Removing atom with id " << state->id << "." << endl);
|
---|
| 94 | World::getInstance().destroyAtom(state->id);
|
---|
| 95 |
|
---|
| 96 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 97 | }
|
---|
| 98 |
|
---|
| 99 | Action::state_ptr AtomAddAction::performRedo(Action::state_ptr _state){
|
---|
[ea2830] | 100 | AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
|
---|
| 101 |
|
---|
| 102 | atom * first = World::getInstance().createAtom();
|
---|
| 103 | first->setType(state->elemental);
|
---|
| 104 | first->setPosition(state->position);
|
---|
| 105 | DoLog(1) && (Log() << Verbose(1) << "Re-adding new atom with element " << state->elemental->getName() << " at " << state->position << "." << endl);
|
---|
| 106 | // TODO: remove when all of World's atoms are stored.
|
---|
| 107 | std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
|
---|
| 108 | if (!molecules.empty()) {
|
---|
| 109 | std::vector<molecule *>::iterator iter = molecules.begin();
|
---|
| 110 | (*iter)->AddAtom(first);
|
---|
| 111 | }
|
---|
| 112 | if (first->getId() != state->id)
|
---|
| 113 | if (!first->changeId(state->id))
|
---|
| 114 | return Action::failure;
|
---|
| 115 | return Action::state_ptr(_state);
|
---|
[97ebf8] | 116 | }
|
---|
| 117 |
|
---|
| 118 | bool AtomAddAction::canUndo() {
|
---|
[ea2830] | 119 | return true;
|
---|
[97ebf8] | 120 | }
|
---|
| 121 |
|
---|
| 122 | bool AtomAddAction::shouldUndo() {
|
---|
[ea2830] | 123 | return true;
|
---|
[97ebf8] | 124 | }
|
---|
| 125 |
|
---|
| 126 | const string AtomAddAction::getName() {
|
---|
| 127 | return NAME;
|
---|
| 128 | }
|
---|