1 /* Copyright (C) 2013 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. 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 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. 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 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
20 #include "ScenarioEditor/ScenarioEditor.h"
21 #include "Common/Tools.h"
22 #include "Common/Brushes.h"
23 #include "GameInterface/Messages.h"
24 
25 using AtlasMessage::Position;
26 
27 class PikeElevation : public StateDrivenTool<PikeElevation>
28 {
29 	DECLARE_DYNAMIC_CLASS(PikeElevation);
30 
31 	int m_Direction; // +1 = raise, -1 = lower
32 	Position m_Pos;
33 
34 public:
PikeElevation()35 	PikeElevation()
36 	{
37 		SetState(&Waiting);
38 	}
39 
40 
OnEnable()41 	void OnEnable()
42 	{
43 		g_Brush_Elevation.MakeActive();
44 	}
45 
OnDisable()46 	void OnDisable()
47 	{
48 		POST_MESSAGE(BrushPreview, (false, Position()));
49 	}
50 
51 
52 	struct sWaiting : public State
53 	{
OnMousePikeElevation::sWaiting54 		bool OnMouse(PikeElevation* obj, wxMouseEvent& evt)
55 		{
56 			if (evt.LeftDown())
57 			{
58 				obj->m_Pos = Position(evt.GetPosition());
59 				SET_STATE(PikeRaising);
60 				return true;
61 			}
62 			else if (evt.RightDown())
63 			{
64 				obj->m_Pos = Position(evt.GetPosition());
65 				SET_STATE(PikeLowering);
66 				return true;
67 			}
68 			else if (evt.Moving())
69 			{
70 				POST_MESSAGE(BrushPreview, (true, Position(evt.GetPosition())));
71 				return true;
72 			}
73 			else
74 			{
75 				return false;
76 			}
77 		}
78 	}
79 	Waiting;
80 
81 
82 	struct sPiking : public State
83 	{
OnEnterPikeElevation::sPiking84 		void OnEnter(PikeElevation* obj)
85 		{
86 			POST_MESSAGE(BrushPreview, (true, obj->m_Pos));
87 		}
88 
OnLeavePikeElevation::sPiking89 		void OnLeave(PikeElevation*)
90 		{
91 			ScenarioEditor::GetCommandProc().FinaliseLastCommand();
92 		}
93 
OnMousePikeElevation::sPiking94 		bool OnMouse(PikeElevation* obj, wxMouseEvent& evt)
95 		{
96 			if (IsMouseUp(evt))
97 			{
98 				SET_STATE(Waiting);
99 				return true;
100 			}
101 			else if (evt.Dragging())
102 			{
103 				wxPoint pos = evt.GetPosition();
104 				obj->m_Pos = Position(pos);
105 				POST_MESSAGE(BrushPreview, (true, obj->m_Pos));
106 				return true;
107 			}
108 			else
109 			{
110 				return false;
111 			}
112 		}
113 
OnTickPikeElevation::sPiking114 		void OnTick(PikeElevation* obj, float dt)
115 		{
116 			POST_COMMAND(PikeElevation, (obj->m_Pos, dt*g_Brush_Elevation.STRENGTH_MULTIPLIER*GetDirection()*g_Brush_Elevation.GetStrength()));
117 			obj->m_Pos = Position::Unchanged();
118 		}
119 
120 		virtual bool IsMouseUp(wxMouseEvent& evt) = 0;
121 		virtual int GetDirection() = 0;
122 	};
123 
124 	struct sPikeRaising : public sPiking
125 	{
IsMouseUpPikeElevation::sPikeRaising126 		bool IsMouseUp(wxMouseEvent& evt) { return evt.LeftUp(); }
GetDirectionPikeElevation::sPikeRaising127 		int GetDirection() { return +1; }
128 	}
129 	PikeRaising;
130 
131 	struct sPikeLowering : public sPiking
132 	{
IsMouseUpPikeElevation::sPikeLowering133 		bool IsMouseUp(wxMouseEvent& evt) { return evt.RightUp(); }
GetDirectionPikeElevation::sPikeLowering134 		int GetDirection() { return -1; }
135 	}
136 	PikeLowering;
137 };
138 
139 IMPLEMENT_DYNAMIC_CLASS(PikeElevation, StateDrivenTool<PikeElevation>);
140