1 /* Copyright (C) 2010 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 
26 using AtlasMessage::Position;
27 
28 class SmoothElevation : public StateDrivenTool<SmoothElevation>
29 {
30 	DECLARE_DYNAMIC_CLASS(SmoothElevation);
31 
32 	int m_Direction; // +1 = smoother, -1 = rougher
33 	Position m_Pos;
34 
35 public:
SmoothElevation()36 	SmoothElevation()
37 	{
38 		SetState(&Waiting);
39 	}
40 
41 
OnEnable()42 	void OnEnable()
43 	{
44 		g_Brush_Elevation.MakeActive();
45 	}
46 
OnDisable()47 	void OnDisable()
48 	{
49 		POST_MESSAGE(BrushPreview, (false, Position()));
50 	}
51 
52 
53 	struct sWaiting : public State
54 	{
OnMouseSmoothElevation::sWaiting55 		bool OnMouse(SmoothElevation* obj, wxMouseEvent& evt)
56 		{
57 			if (evt.LeftDown())
58 			{
59 				obj->m_Pos = Position(evt.GetPosition());
60 				SET_STATE(Smoothing);
61 				return true;
62 			}
63 			else if (evt.RightDown())
64 			{
65 				obj->m_Pos = Position(evt.GetPosition());
66 				SET_STATE(Roughing);
67 				return true;
68 			}
69 			else if (evt.Moving())
70 			{
71 				POST_MESSAGE(BrushPreview, (true, Position(evt.GetPosition())));
72 				return true;
73 			}
74 			else
75 			{
76 				return false;
77 			}
78 		}
79 	}
80 	Waiting;
81 
82 
83 	struct sSmoothing_common : public State
84 	{
OnEnterSmoothElevation::sSmoothing_common85 		void OnEnter(SmoothElevation* obj)
86 		{
87 			POST_MESSAGE(BrushPreview, (true, obj->m_Pos));
88 		}
89 
OnLeaveSmoothElevation::sSmoothing_common90 		void OnLeave(SmoothElevation*)
91 		{
92 			ScenarioEditor::GetCommandProc().FinaliseLastCommand();
93 		}
94 
OnMouseSmoothElevation::sSmoothing_common95 		bool OnMouse(SmoothElevation* obj, wxMouseEvent& evt)
96 		{
97 			if (IsMouseUp(evt))
98 			{
99 				SET_STATE(Waiting);
100 				return true;
101 			}
102 			else if (evt.Dragging())
103 			{
104 				wxPoint pos = evt.GetPosition();
105 				obj->m_Pos = Position(pos);
106 				POST_MESSAGE(BrushPreview, (true, obj->m_Pos));
107 				return true;
108 			}
109 			else
110 			{
111 				return false;
112 			}
113 		}
114 
OnTickSmoothElevation::sSmoothing_common115 		void OnTick(SmoothElevation* obj, float dt)
116 		{
117 			POST_COMMAND(SmoothElevation, (obj->m_Pos, dt*g_Brush_Elevation.STRENGTH_MULTIPLIER*GetDirection()*g_Brush_Elevation.GetStrength()));
118 			obj->m_Pos = Position::Unchanged();
119 		}
120 
121 		virtual bool IsMouseUp(wxMouseEvent& evt) = 0;
122 		virtual int GetDirection() = 0;
123 	};
124 
125 	struct sSmoothing : public sSmoothing_common
126 	{
IsMouseUpSmoothElevation::sSmoothing127 		bool IsMouseUp(wxMouseEvent& evt) { return evt.LeftUp(); }
GetDirectionSmoothElevation::sSmoothing128 		int GetDirection() { return +1; }
129 	}
130 	Smoothing;
131 
132 	struct sRoughing : public sSmoothing_common
133 	{
IsMouseUpSmoothElevation::sRoughing134 		bool IsMouseUp(wxMouseEvent& evt) { return evt.RightUp(); }
GetDirectionSmoothElevation::sRoughing135 		int GetDirection() { return -1; }
136 	}
137 	Roughing;
138 };
139 
140 IMPLEMENT_DYNAMIC_CLASS(SmoothElevation, StateDrivenTool<SmoothElevation>);
141