File: Tools.h

package info (click to toggle)
0ad 0.0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,292 kB
  • sloc: cpp: 245,166; ansic: 200,249; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (177 lines) | stat: -rw-r--r-- 5,046 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Copyright (C) 2014 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_TOOLS
#define INCLUDED_TOOLS

#include "General/AtlasWindowCommand.h"
#include "General/Observable.h"

class wxMouseEvent;
class wxKeyEvent;
class ScenarioEditor;

class ITool : public wxObject
{
public:
	enum KeyEventType { KEY_DOWN, KEY_UP, KEY_CHAR };

	virtual void Init(void* initData, ScenarioEditor* scenarioEditor) = 0;
	virtual void Shutdown() = 0;
	virtual bool OnMouse(wxMouseEvent& evt) = 0; // return true if handled
	virtual bool OnKey(wxKeyEvent& evt, KeyEventType dir) = 0; // return true if handled
	virtual void OnTick(float dt) = 0; // dt in seconds
    virtual void OnCommand(const wxString& command, void* userData) = 0;

	virtual ~ITool() {};
};

struct ToolManagerImpl;
class ToolManager
{
private: // NONCOPYABLE
	ToolManager(const ToolManager&);
	const ToolManager& operator=(const ToolManager&);

public:
	ToolManager(ScenarioEditor* scenarioEditor);
	~ToolManager();
	ObservablePtr<ITool>& GetCurrentTool();
	wxString GetCurrentToolName();
	void SetCurrentTool(const wxString& name, void* initData = NULL);
private:
	ToolManagerImpl* m;
	ScenarioEditor* m_ScenarioEditor;
};

class ToolButton;
extern void RegisterToolButton(ToolButton* button, const wxString& toolName);
extern void RegisterToolBarButton(wxToolBar* toolbar, int buttonId, const wxString& toolName);

//////////////////////////////////////////////////////////////////////////


namespace AtlasMessage { struct mWorldCommand; }
class WorldCommand : public AtlasWindowCommand
{
	DECLARE_CLASS(WorldCommand);

	bool m_AlreadyDone;
public:
	WorldCommand(AtlasMessage::mWorldCommand* command);
	~WorldCommand();
	bool Do();
	bool Undo();
	bool Merge(AtlasWindowCommand* previousCommand);

private:
	AtlasMessage::mWorldCommand* m_Command;
};

#define POST_COMMAND(type, data) ScenarioEditor::GetCommandProc().Submit(new WorldCommand(new AtlasMessage::m##type(AtlasMessage::d##type data)))

//////////////////////////////////////////////////////////////////////////

#define SET_STATE(s) obj->SetState(&obj->s)

template <typename T>
class StateDrivenTool : public ITool
{
public:
	StateDrivenTool()
		: m_CurrentState(&Disabled), m_ScenarioEditor(NULL)
	{
	}

	virtual void Init(void* WXUNUSED(initData), ScenarioEditor* scenarioEditor)
	{
		m_ScenarioEditor = scenarioEditor;
	}

	virtual void Shutdown()
	{
		// This can't be done in the destructor, because ~StateDrivenTool
		// is not called until after the subclass has been destroyed and its
		// vtable (containing OnDisable) has been removed.
		SetState(&Disabled);
	}

protected:
	// Called when the tool is enabled/disabled; always called in zero or
	// more enable-->disable pairs per object instance.
	virtual void OnEnable() {}
	virtual void OnDisable() {}

	struct State
	{
		virtual ~State() {}
		virtual void OnEnter(T* WXUNUSED(obj)) {}
		virtual void OnLeave(T* WXUNUSED(obj)) {}
		virtual void OnTick (T* WXUNUSED(obj), float WXUNUSED(dt)) {}

		// Should return true if the event has been handled (else the event will
		// be passed to a lower-priority level)
		virtual bool OnMouse(T* WXUNUSED(obj), wxMouseEvent& WXUNUSED(evt)) { return false; }
		virtual bool OnKey(T* WXUNUSED(obj), wxKeyEvent& WXUNUSED(evt), KeyEventType WXUNUSED(type)) { return false; }
	};


	struct sDisabled : public State
	{
		void OnEnter(T* obj) { obj->OnDisable(); }
		void OnLeave(T* obj) { obj->OnEnable(); }
	}
	Disabled;

	void SetState(State* state)
	{
		m_CurrentState->OnLeave(static_cast<T*>(this));
		// this cast is safe as long as the class is used as in
		// "class Something : public StateDrivenTool<Something> { ... }"
		m_CurrentState = state;
		m_CurrentState->OnEnter(static_cast<T*>(this));
	}

	ScenarioEditor& GetScenarioEditor() { wxASSERT(m_ScenarioEditor); return *m_ScenarioEditor; }

private:
	State* m_CurrentState;

	ScenarioEditor* m_ScenarioEditor; // not NULL, except before Init has been called

	virtual bool OnMouse(wxMouseEvent& evt)
	{
		return m_CurrentState->OnMouse(static_cast<T*>(this), evt);
	}

	virtual bool OnKey(wxKeyEvent& evt, KeyEventType dir)
	{
		return m_CurrentState->OnKey(static_cast<T*>(this), evt, dir);
	}

	virtual void OnTick(float dt)
	{
		m_CurrentState->OnTick(static_cast<T*>(this), dt);
	}

    virtual void OnCommand(const wxString& WXUNUSED(command), void* WXUNUSED(userData))
    {
    }
};


#endif // INCLUDED_TOOLS