| 1 |
/* Copyright (C) 2025 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 "tools/atlas/AtlasUI/General/Observable.h" |
| 21 |
#include "tools/atlas/AtlasUI/ScenarioEditor/ScenarioEditor.h" |
| 22 |
#include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/MiscState.h" |
| 23 |
#include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/ObjectSettings.h" |
| 24 |
#include "tools/atlas/AtlasUI/ScenarioEditor/Tools/Common/Tools.h" |
| 25 |
#include "tools/atlas/GameInterface/MessagePasser.h" |
| 26 |
#include "tools/atlas/GameInterface/Messages.h" |
| 27 |
#include "tools/atlas/GameInterface/SharedTypes.h" |
| 28 |
|
| 29 |
#include <algorithm> |
| 30 |
#include <cmath> |
| 31 |
#include <list> |
| 32 |
#include <vector> |
| 33 |
#include <wx/defs.h> |
| 34 |
#include <wx/event.h> |
| 35 |
#include <wx/mousestate.h> |
| 36 |
#include <wx/object.h> |
| 37 |
|
| 38 |
using AtlasMessage::Position; |
| 39 |
|
| 40 |
// ObjectSidebar handles the display and animation etc of the actor; |
| 41 |
// this tool mainly handles the user input and toggling the rendering. |
| 42 |
// (There isn't a hugely clear separation of responsibilities.) |
| 43 |
|
| 44 |
class ActorViewerTool : public StateDrivenTool<ActorViewerTool> |
| 45 |
{ |
| 46 |
DECLARE_DYNAMIC_CLASS(ActorViewerTool); |
| 47 |
|
| 48 |
// Camera settings |
| 49 |
float m_Distance; |
| 50 |
float m_Angle; |
| 51 |
float m_Elevation; |
| 52 |
|
| 53 |
// Mouse input state |
| 54 |
int m_LastX, m_LastY; |
| 55 |
bool m_LastIsValid; |
| 56 |
|
| 57 |
public: |
| 58 |
ActorViewerTool() : |
| 59 |
m_Distance(20.f), m_Angle(0.f), m_Elevation((float)M_PI / 6.f), |
| 60 |
m_LastIsValid(false) |
| 61 |
{ |
| 62 |
} |
| 63 |
|
| 64 |
void PostLookAt() |
| 65 |
{ |
| 66 |
float offset = 0.3f; // slight fudge so we turn nicely when going over the top of the unit |
| 67 |
POST_MESSAGE(LookAt, (AtlasMessage::eRenderView::ACTOR, |
| 68 |
Position( |
| 69 |
m_Distance*cos(m_Elevation)*sin(m_Angle) + offset*cos(m_Angle), |
| 70 |
m_Distance*sin(m_Elevation), |
| 71 |
m_Distance*cos(m_Elevation)*cos(m_Angle) - offset*sin(m_Angle)), |
| 72 |
Position(0, 0, 0))); |
| 73 |
} |
| 74 |
|
| 75 |
virtual void Init(void* initData, ScenarioEditor* scenarioEditor) |
| 76 |
{ |
| 77 |
StateDrivenTool<ActorViewerTool>::Init(initData, scenarioEditor); |
| 78 |
|
| 79 |
SetState(&Viewing); |
| 80 |
} |
| 81 |
|
| 82 |
void OnEnable() |
| 83 |
{ |
| 84 |
GetScenarioEditor().GetObjectSettings().SetView(AtlasMessage::eRenderView::ACTOR); |
| 85 |
|
| 86 |
std::vector<AtlasMessage::ObjectID> selected; |
| 87 |
selected.push_back(0); |
| 88 |
g_SelectedObjects = selected; |
| 89 |
|
| 90 |
PostLookAt(); |
| 91 |
|
| 92 |
POST_MESSAGE(RenderEnable, (AtlasMessage::eRenderView::ACTOR)); |
| 93 |
} |
| 94 |
|
| 95 |
void OnDisable() |
| 96 |
{ |
| 97 |
GetScenarioEditor().GetObjectSettings().SetView(AtlasMessage::eRenderView::GAME); |
| 98 |
|
| 99 |
std::vector<AtlasMessage::ObjectID> selected; |
| 100 |
g_SelectedObjects = selected; |
| 101 |
|
| 102 |
POST_MESSAGE(RenderEnable, (AtlasMessage::eRenderView::GAME)); |
| 103 |
} |
| 104 |
|
| 105 |
struct sViewing : public State |
| 106 |
{ |
| 107 |
bool OnMouse(ActorViewerTool* obj, wxMouseEvent& evt) |
| 108 |
{ |
| 109 |
bool camera_changed = false; |
| 110 |
|
| 111 |
if (evt.GetWheelRotation()) |
| 112 |
{ |
| 113 |
float speed = -1.f * ScenarioEditor::GetSpeedModifier(); |
| 114 |
|
| 115 |
obj->m_Distance += evt.GetWheelRotation() * speed / evt.GetWheelDelta(); |
| 116 |
|
| 117 |
camera_changed = true; |
| 118 |
} |
| 119 |
|
| 120 |
if (evt.ButtonDown(wxMOUSE_BTN_LEFT) || evt.ButtonDown(wxMOUSE_BTN_RIGHT)) |
| 121 |
{ |
| 122 |
obj->m_LastX = evt.GetX(); |
| 123 |
obj->m_LastY = evt.GetY(); |
| 124 |
obj->m_LastIsValid = true; |
| 125 |
} |
| 126 |
else if (evt.Dragging() |
| 127 |
&& (evt.ButtonIsDown(wxMOUSE_BTN_LEFT) || evt.ButtonIsDown(wxMOUSE_BTN_RIGHT)) |
| 128 |
&& obj->m_LastIsValid) |
| 129 |
{ |
| 130 |
int dx = evt.GetX() - obj->m_LastX; |
| 131 |
int dy = evt.GetY() - obj->m_LastY; |
| 132 |
obj->m_LastX = evt.GetX(); |
| 133 |
obj->m_LastY = evt.GetY(); |
| 134 |
|
| 135 |
obj->m_Angle += dx * M_PI/256.f * ScenarioEditor::GetSpeedModifier(); |
| 136 |
|
| 137 |
if (evt.ButtonIsDown(wxMOUSE_BTN_LEFT)) |
| 138 |
obj->m_Distance += dy / 8.f * ScenarioEditor::GetSpeedModifier(); |
| 139 |
else // evt.ButtonIsDown(wxMOUSE_BTN_RIGHT)) |
| 140 |
obj->m_Elevation += dy * M_PI/256.f * ScenarioEditor::GetSpeedModifier(); |
| 141 |
|
| 142 |
camera_changed = true; |
| 143 |
} |
| 144 |
else if ((evt.ButtonUp(wxMOUSE_BTN_LEFT) || evt.ButtonUp(wxMOUSE_BTN_RIGHT)) |
| 145 |
&& !(evt.ButtonIsDown(wxMOUSE_BTN_LEFT) || evt.ButtonIsDown(wxMOUSE_BTN_RIGHT)) |
| 146 |
) |
| 147 |
{ |
| 148 |
// In some situations (e.g. double-clicking the title bar to |
| 149 |
// maximise the window) we get a dragging event without the matching |
| 150 |
// buttondown; so disallow dragging when all buttons were released since |
| 151 |
// the last buttondown. |
| 152 |
// (TODO: does this problem affect the scenario editor too?) |
| 153 |
obj->m_LastIsValid = false; |
| 154 |
} |
| 155 |
|
| 156 |
obj->m_Distance = std::max(obj->m_Distance, 1/64.f); // don't let people fly through the origin |
| 157 |
|
| 158 |
if (camera_changed) |
| 159 |
obj->PostLookAt(); |
| 160 |
|
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
bool OnKey(ActorViewerTool* obj, wxKeyEvent& evt, KeyEventType type) |
| 165 |
{ |
| 166 |
if (type == KEY_DOWN && (evt.GetKeyCode() >= '0' && evt.GetKeyCode() <= '9')) |
| 167 |
{ |
| 168 |
// (TODO: this should probably be 'char' not 'down'; but we don't get |
| 169 |
// 'char' unless we return false from this function, in which case the |
| 170 |
// scenario editor intercepts some other keys for itself) |
| 171 |
int playerID = evt.GetKeyCode() - '0'; |
| 172 |
obj->GetScenarioEditor().GetObjectSettings().SetPlayerID(playerID); |
| 173 |
obj->GetScenarioEditor().GetObjectSettings().NotifyObservers(); |
| 174 |
} |
| 175 |
|
| 176 |
// Prevent keys from passing through to the scenario editor |
| 177 |
return true; |
| 178 |
} |
| 179 |
} |
| 180 |
Viewing; |
| 181 |
}; |
| 182 |
|
| 183 |
IMPLEMENT_DYNAMIC_CLASS(ActorViewerTool, StateDrivenTool<ActorViewerTool>); |