2025-08-09 12:17:38 -07:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2009-06-20 09:13:29 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-06-20 09:13:29 -07:00
|
|
|
* 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.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-06-20 09:13:29 -07:00
|
|
|
* 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
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-06-20 09:13:29 -07:00
|
|
|
*/
|
|
|
|
|
|
2007-06-05 11:35:05 -07:00
|
|
|
#include "precompiled.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2025-08-09 12:17:38 -07:00
|
|
|
#include "tools/atlas/AtlasUI/General/Observable.h"
|
|
|
|
|
#include "tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.h"
|
|
|
|
|
#include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/ObjectSettings.h"
|
|
|
|
|
#include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.h"
|
|
|
|
|
#include "tools/atlas/GameInterface/MessagePasser.h"
|
|
|
|
|
#include "tools/atlas/GameInterface/Messages.h"
|
|
|
|
|
#include "tools/atlas/GameInterface/SharedTypes.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2025-08-09 12:17:38 -07:00
|
|
|
#include <cmath>
|
2022-01-02 14:35:17 -08:00
|
|
|
#include <ctime>
|
2025-08-09 12:17:38 -07:00
|
|
|
#include <list>
|
2022-01-02 14:35:17 -08:00
|
|
|
#include <random>
|
2025-08-09 12:17:38 -07:00
|
|
|
#include <string>
|
|
|
|
|
#include <wx/chartype.h>
|
|
|
|
|
#include <wx/debug.h>
|
|
|
|
|
#include <wx/defs.h>
|
|
|
|
|
#include <wx/event.h>
|
|
|
|
|
#include <wx/object.h>
|
|
|
|
|
#include <wx/string.h>
|
2022-01-02 14:35:17 -08:00
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
using AtlasMessage::Position;
|
|
|
|
|
|
2007-05-28 07:25:07 -07:00
|
|
|
static float g_DefaultAngle = (float)(M_PI*3.0/4.0);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
class PlaceObject : public StateDrivenTool<PlaceObject>
|
|
|
|
|
{
|
|
|
|
|
DECLARE_DYNAMIC_CLASS(PlaceObject);
|
|
|
|
|
|
|
|
|
|
Position m_ScreenPos, m_ObjPos, m_Target;
|
|
|
|
|
wxString m_ObjectID;
|
2013-02-02 18:16:52 -08:00
|
|
|
unsigned int m_ActorSeed;
|
2019-03-17 09:04:06 -07:00
|
|
|
int m_RotationDirection;
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
public:
|
2019-03-17 09:04:06 -07:00
|
|
|
PlaceObject(): m_RotationDirection(0)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
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)
|
2014-06-17 06:14:25 -07:00
|
|
|
POST_MESSAGE(ObjectPreview, ((std::wstring)m_ObjectID.wc_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), m_ObjPos, useTarget, m_Target, g_DefaultAngle, m_ActorSeed, true));
|
2006-04-23 16:14:18 -07:00
|
|
|
else
|
2013-02-02 18:16:52 -08:00
|
|
|
{
|
|
|
|
|
POST_COMMAND(CreateObject, ((std::wstring)m_ObjectID.wc_str(), GetScenarioEditor().GetObjectSettings().GetSettings(), m_ObjPos, useTarget, m_Target, g_DefaultAngle, m_ActorSeed));
|
|
|
|
|
RandomizeActorSeed();
|
|
|
|
|
}
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2007-09-02 16:38:58 -07:00
|
|
|
virtual void Init(void* initData, ScenarioEditor* scenarioEditor)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2007-09-02 16:38:58 -07:00
|
|
|
StateDrivenTool<PlaceObject>::Init(initData, scenarioEditor);
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
wxASSERT(initData);
|
|
|
|
|
wxString& id = *static_cast<wxString*>(initData);
|
|
|
|
|
m_ObjectID = id;
|
|
|
|
|
SendObjectMsg(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnEnable()
|
|
|
|
|
{
|
2013-02-02 18:16:52 -08:00
|
|
|
RandomizeActorSeed();
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-26 18:36:33 -08:00
|
|
|
bool OnKeyOverride(wxKeyEvent& evt, KeyEventType type)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2013-01-03 21:46:35 -08:00
|
|
|
if (type == KEY_CHAR && evt.GetKeyCode() == WXK_ESCAPE)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2013-01-03 21:46:35 -08:00
|
|
|
SetState(&Disabled);
|
|
|
|
|
return true;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
2019-03-17 09:04:06 -07:00
|
|
|
else if (evt.GetKeyCode() == WXK_PAGEDOWN)
|
|
|
|
|
{
|
|
|
|
|
if (type == KEY_DOWN)
|
|
|
|
|
m_RotationDirection = 1;
|
|
|
|
|
else if (type == KEY_UP)
|
|
|
|
|
m_RotationDirection = 0;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (evt.GetKeyCode() == WXK_PAGEUP)
|
|
|
|
|
{
|
|
|
|
|
if (type == KEY_DOWN)
|
|
|
|
|
m_RotationDirection = -1;
|
|
|
|
|
else if (type == KEY_UP)
|
|
|
|
|
m_RotationDirection = 0;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-01-03 21:46:35 -08:00
|
|
|
else
|
|
|
|
|
return false;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RotateTick(float dt)
|
|
|
|
|
{
|
2019-03-17 09:04:06 -07:00
|
|
|
if (m_RotationDirection)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 10:36:42 -07:00
|
|
|
float speed = M_PI/2.f * ScenarioEditor::GetSpeedModifier(); // radians per second
|
2019-03-17 09:04:06 -07:00
|
|
|
g_DefaultAngle += (m_RotationDirection * dt * speed);
|
2006-04-23 16:14:18 -07:00
|
|
|
SendObjectMsg(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-02 18:16:52 -08:00
|
|
|
void RandomizeActorSeed()
|
|
|
|
|
{
|
2022-01-02 14:35:17 -08:00
|
|
|
std::mt19937 engine(std::time(nullptr));
|
|
|
|
|
std::uniform_int_distribution<unsigned int> distribution(0, 65535);
|
|
|
|
|
m_ActorSeed = distribution(engine);
|
2013-02-02 18:16:52 -08:00
|
|
|
}
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2007-01-26 18:36:33 -08:00
|
|
|
bool OnKey(PlaceObject* obj, wxKeyEvent& evt, KeyEventType type)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2007-01-26 18:36:33 -08:00
|
|
|
if (type == KEY_CHAR && (evt.GetKeyCode() >= '0' && evt.GetKeyCode() <= '9'))
|
|
|
|
|
{
|
|
|
|
|
int playerID = evt.GetKeyCode() - '0';
|
2007-09-02 16:38:58 -07:00
|
|
|
obj->GetScenarioEditor().GetObjectSettings().SetPlayerID(playerID);
|
|
|
|
|
obj->GetScenarioEditor().GetObjectSettings().NotifyObservers();
|
2007-01-26 18:36:33 -08:00
|
|
|
obj->SendObjectMsg(true);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return obj->OnKeyOverride(evt, type);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
2007-01-26 18:36:33 -08:00
|
|
|
bool OnKey(PlaceObject* obj, wxKeyEvent& evt, KeyEventType type)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2007-01-26 18:36:33 -08:00
|
|
|
return obj->OnKeyOverride(evt, type);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
void OnTick(PlaceObject* obj, float dt)
|
|
|
|
|
{
|
|
|
|
|
obj->RotateTick(dt);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Placing;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_DYNAMIC_CLASS(PlaceObject, StateDrivenTool<PlaceObject>);
|