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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/* Copyright (C) 2013 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/>.
*/
#include "precompiled.h"
#include "ScenarioEditor/ScenarioEditor.h"
#include "Common/Tools.h"
#include "Common/Brushes.h"
#include "Common/MiscState.h"
#include "Common/ObjectSettings.h"
#include "GameInterface/Messages.h"
using AtlasMessage::Position;
static float g_DefaultAngle = (float)(M_PI*3.0/4.0);
class PlaceObject : public StateDrivenTool<PlaceObject>
{
DECLARE_DYNAMIC_CLASS(PlaceObject);
Position m_ScreenPos, m_ObjPos, m_Target;
wxString m_ObjectID;
unsigned int m_ActorSeed;
public:
PlaceObject()
{
SetState(&Waiting);
}
void SendObjectMsg(bool preview)
{
int dragDistSq =
(m_ScreenPos.type1.x-m_Target.type1.x)*(m_ScreenPos.type1.x-m_Target.type1.x)
+ (m_ScreenPos.type1.y-m_Target.type1.y)*(m_ScreenPos.type1.y-m_Target.type1.y);
bool useTarget = (dragDistSq >= 16*16);
if (preview)
POST_MESSAGE(ObjectPreview, ((std::wstring)m_ObjectID.wc_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), m_ObjPos, useTarget, m_Target, g_DefaultAngle, m_ActorSeed, true));
else
{
POST_COMMAND(CreateObject, ((std::wstring)m_ObjectID.wc_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), m_ObjPos, useTarget, m_Target, g_DefaultAngle, m_ActorSeed));
RandomizeActorSeed();
}
}
virtual void Init(void* initData, ScenarioEditor* scenarioEditor)
{
StateDrivenTool<PlaceObject>::Init(initData, scenarioEditor);
wxASSERT(initData);
wxString& id = *static_cast<wxString*>(initData);
m_ObjectID = id;
SendObjectMsg(true);
}
void OnEnable()
{
RandomizeActorSeed();
}
void OnDisable()
{
m_ObjectID = _T("");
SendObjectMsg(true);
}
/*
Object placement:
* Select unit from list
* Move mouse around screen; preview of unit follows mouse
* Left mouse down -> remember position, fix preview to point
* Mouse move -> if moved > [limit], rotate unit to face mouse; else default orientation
* Left mouse release -> finalise placement of object on map
* Page up/down -> rotate default orientation
* Escape -> cancel placement tool
TOOD: what happens if somebody saves while the preview is active?
*/
bool OnMouseOverride(wxMouseEvent& WXUNUSED(evt))
{
// This used to let the scroll-wheel rotate units, but that overrides
// the camera zoom and makes navigation very awkward, so it doesn't
// any more.
return false;
}
bool OnKeyOverride(wxKeyEvent& evt, KeyEventType type)
{
if (type == KEY_CHAR && evt.GetKeyCode() == WXK_ESCAPE)
{
SetState(&Disabled);
return true;
}
else
return false;
}
void RotateTick(float dt)
{
int dir = 0;
if (wxGetKeyState(WXK_PAGEDOWN)) ++dir; // page-down key
if (wxGetKeyState(WXK_PAGEUP)) --dir; // page-up key
if (dir)
{
float speed = M_PI/2.f * ScenarioEditor::GetSpeedModifier(); // radians per second
g_DefaultAngle += (dir * dt * speed);
SendObjectMsg(true);
}
}
void RandomizeActorSeed()
{
m_ActorSeed = (unsigned int)floor((rand() / (float)RAND_MAX) * 65535.f);
}
struct sWaiting : public State
{
bool OnMouse(PlaceObject* obj, wxMouseEvent& evt)
{
if (obj->OnMouseOverride(evt))
return true;
else if (evt.LeftDown())
{
obj->m_ObjPos = obj->m_ScreenPos = obj->m_Target = Position(evt.GetPosition());
obj->SendObjectMsg(true);
obj->m_ObjPos = Position::Unchanged(); // make sure object is stationary even if the camera moves
SET_STATE(Placing);
return true;
}
else if (evt.Moving())
{
obj->m_ObjPos = obj->m_ScreenPos = obj->m_Target = Position(evt.GetPosition());
obj->SendObjectMsg(true);
return true;
}
else
return false;
}
bool OnKey(PlaceObject* obj, wxKeyEvent& evt, KeyEventType type)
{
if (type == KEY_CHAR && (evt.GetKeyCode() >= '0' && evt.GetKeyCode() <= '9'))
{
int playerID = evt.GetKeyCode() - '0';
obj->GetScenarioEditor().GetObjectSettings().SetPlayerID(playerID);
obj->GetScenarioEditor().GetObjectSettings().NotifyObservers();
obj->SendObjectMsg(true);
return true;
}
else
return obj->OnKeyOverride(evt, type);
}
void OnTick(PlaceObject* obj, float dt)
{
obj->RotateTick(dt);
}
}
Waiting;
struct sPlacing : public State
{
bool OnMouse(PlaceObject* obj, wxMouseEvent& evt)
{
if (obj->OnMouseOverride(evt))
return true;
else if (evt.LeftUp())
{
obj->m_Target = Position(evt.GetPosition());
// Create the actual object
obj->SendObjectMsg(false);
// Go back to preview mode
SET_STATE(Waiting);
obj->m_ObjPos = obj->m_ScreenPos = obj->m_Target;
obj->SendObjectMsg(true);
return true;
}
else if (evt.Dragging())
{
obj->m_Target = Position(evt.GetPosition());
obj->SendObjectMsg(true);
return true;
}
else
return false;
}
bool OnKey(PlaceObject* obj, wxKeyEvent& evt, KeyEventType type)
{
return obj->OnKeyOverride(evt, type);
}
void OnTick(PlaceObject* obj, float dt)
{
obj->RotateTick(dt);
}
}
Placing;
};
IMPLEMENT_DYNAMIC_CLASS(PlaceObject, StateDrivenTool<PlaceObject>);
|