1//////////////////////////////////////////////////////////////////////
2// This file is part of Remere's Map Editor
3//////////////////////////////////////////////////////////////////////
4// Remere's Map Editor is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Remere's Map Editor is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//////////////////////////////////////////////////////////////////////
17
18#include "main.h"
19
20#include "live_action.h"
21#include "editor.h"
22
23NetworkedAction::NetworkedAction(Editor& editor, ActionIdentifier ident) :
24 Action(editor, ident),
25 owner(0) {
26 ;
27}
28
29NetworkedAction::~NetworkedAction() {
30 ;
31}
32
33NetworkedBatchAction::NetworkedBatchAction(Editor& editor, NetworkedActionQueue& queue, ActionIdentifier ident) :
34 BatchAction(editor, ident),
35 queue(queue) {
36 ;
37}
38
39NetworkedBatchAction::~NetworkedBatchAction() {
40 ;
41}
42
43void NetworkedBatchAction::addAndCommitAction(Action* action) {
44 // If empty, do nothing.
45 if (action->size() == 0) {
46 delete action;
47 return;
48 }
49
50 // Track changed nodes...
51 DirtyList dirty_list;
52 NetworkedAction* netact = dynamic_cast<NetworkedAction*>(action);
53 if (netact) {
54 dirty_list.owner = netact->owner;
55 }
56
57 // Add it!
58 action->commit(type != ACTION_SELECT ? &dirty_list : nullptr);
59 batch.push_back(action);
60 timestamp = time(nullptr);
61
62 // Broadcast changes!
63 queue.broadcast(dirty_list);
64}
65
66void NetworkedBatchAction::commit() {
67 // Track changed nodes...
68 DirtyList dirty_list;
69
70 for (ActionVector::iterator it = batch.begin(); it != batch.end(); ++it) {
71 NetworkedAction* action = static_cast<NetworkedAction*>(*it);
72 if (!action->isCommited()) {
73 action->commit(type != ACTION_SELECT ? &dirty_list : nullptr);
74 if (action->owner != 0) {
75 dirty_list.owner = action->owner;
76 }
77 }
78 }
79 // Broadcast changes!
80 queue.broadcast(dirty_list);
81}
82
83void NetworkedBatchAction::undo() {
84 // Track changed nodes...
85 DirtyList dirty_list;
86
87 for (ActionVector::reverse_iterator it = batch.rbegin(); it != batch.rend(); ++it) {
88 (*it)->undo(type != ACTION_SELECT ? &dirty_list : nullptr);
89 }
90 // Broadcast changes!
91 queue.broadcast(dirty_list);
92}
93
94void NetworkedBatchAction::redo() {
95 commit();
96}
97
98//===================
99// Action queue
100
101NetworkedActionQueue::NetworkedActionQueue(Editor& editor) :
102 ActionQueue(editor) {
103}
104
105NetworkedActionQueue::~NetworkedActionQueue() {
106}
107
108Action* NetworkedActionQueue::createAction(ActionIdentifier ident) {
109 return newd NetworkedAction(editor, ident);
110}
111
112BatchAction* NetworkedActionQueue::createBatch(ActionIdentifier ident) {
113 return newd NetworkedBatchAction(editor, *this, ident);
114}
115
116void NetworkedActionQueue::broadcast(DirtyList& dirty_list) {
117 editor.BroadcastNodes(dirty_list);
118}
119