2025-05-21 03:29:47 -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
|
|
|
*/
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "MessageHandler.h"
|
2019-09-18 13:51:45 -07:00
|
|
|
#include "gui/CGUI.h"
|
2025-08-08 12:04:43 -07:00
|
|
|
#include "gui/GUIManager.h"
|
2012-01-12 15:32:27 -08:00
|
|
|
#include "lib/external_libraries/libsdl.h"
|
2025-08-08 12:04:43 -07:00
|
|
|
#include "lib/input.h"
|
|
|
|
|
#include "lib/path.h"
|
|
|
|
|
#include "lib/types.h"
|
2010-06-01 17:36:21 -07:00
|
|
|
#include "maths/MathUtil.h"
|
|
|
|
|
#include "ps/Game.h"
|
|
|
|
|
#include "ps/GameSetup/Config.h"
|
2022-01-04 10:13:45 -08:00
|
|
|
#include "renderer/Renderer.h"
|
2021-05-10 04:51:32 -07:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2014-09-12 14:38:10 -07:00
|
|
|
#include "simulation2/components/ICmpSoundManager.h"
|
2025-08-02 12:24:35 -07:00
|
|
|
#include "simulation2/system/Component.h"
|
2025-08-08 12:04:43 -07:00
|
|
|
#include "simulation2/system/Entity.h"
|
|
|
|
|
#include "tools/atlas/GameInterface/MessagePasser.h"
|
|
|
|
|
#include "tools/atlas/GameInterface/MessagePasserImpl.h"
|
|
|
|
|
#include "tools/atlas/GameInterface/Messages.h"
|
|
|
|
|
#include "tools/atlas/GameInterface/Shareable.h"
|
|
|
|
|
#include "tools/atlas/GameInterface/SharedTypes.h"
|
|
|
|
|
#include "tools/atlas/GameInterface/View.h"
|
|
|
|
|
|
|
|
|
|
#include <SDL_events.h>
|
|
|
|
|
#include <SDL_keyboard.h>
|
|
|
|
|
#include <SDL_keycode.h>
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
#include <js/CallArgs.h>
|
|
|
|
|
#include <js/RootingAPI.h>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
2006-12-04 18:35:00 -08:00
|
|
|
|
|
|
|
|
extern void (*Atlas_GLSwapBuffers)(void* context);
|
|
|
|
|
|
2021-12-31 02:35:56 -08:00
|
|
|
namespace AtlasMessage
|
|
|
|
|
{
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
MESSAGEHANDLER(MessageTrace)
|
|
|
|
|
{
|
|
|
|
|
((MessagePasserImpl*)g_MessagePasser)->SetTrace(msg->enable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(Screenshot)
|
|
|
|
|
{
|
2012-03-05 16:15:48 -08:00
|
|
|
if (msg->big)
|
2022-01-04 10:13:45 -08:00
|
|
|
g_Renderer.MakeScreenShotOnNextFrame(CRenderer::ScreenShotType::BIG);
|
2012-03-05 16:15:48 -08:00
|
|
|
else
|
2022-01-04 10:13:45 -08:00
|
|
|
g_Renderer.MakeScreenShotOnNextFrame(CRenderer::ScreenShotType::DEFAULT);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2006-07-12 07:49:10 -07:00
|
|
|
QUERYHANDLER(Ping)
|
|
|
|
|
{
|
|
|
|
|
}
|
2016-11-23 05:02:58 -08:00
|
|
|
|
2014-12-29 13:01:54 -08:00
|
|
|
MESSAGEHANDLER(SimStopMusic)
|
|
|
|
|
{
|
2014-12-30 09:27:18 -08:00
|
|
|
CmpPtr<ICmpSoundManager> cmpSoundManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
|
|
|
|
|
if (cmpSoundManager)
|
|
|
|
|
cmpSoundManager->StopMusic();
|
2014-12-29 13:01:54 -08:00
|
|
|
}
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2007-01-16 19:25:20 -08:00
|
|
|
MESSAGEHANDLER(SimStateSave)
|
|
|
|
|
{
|
2012-03-03 15:48:04 -08:00
|
|
|
AtlasView::GetView_Game()->SaveState(*msg->label);
|
2007-01-16 19:25:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(SimStateRestore)
|
|
|
|
|
{
|
2012-03-03 15:48:04 -08:00
|
|
|
AtlasView::GetView_Game()->RestoreState(*msg->label);
|
2007-01-16 19:25:20 -08:00
|
|
|
}
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
QUERYHANDLER(SimStateDebugDump)
|
|
|
|
|
{
|
2012-03-03 15:48:04 -08:00
|
|
|
msg->dump = AtlasView::GetView_Game()->DumpState(msg->binary);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2007-01-16 19:25:20 -08:00
|
|
|
MESSAGEHANDLER(SimPlay)
|
|
|
|
|
{
|
2012-03-03 15:48:04 -08:00
|
|
|
AtlasView::GetView_Game()->SetSpeedMultiplier(msg->speed);
|
2013-02-22 15:20:59 -08:00
|
|
|
AtlasView::GetView_Game()->SetTesting(msg->simTest);
|
2007-01-16 19:25:20 -08:00
|
|
|
}
|
|
|
|
|
|
2007-02-01 18:17:15 -08:00
|
|
|
MESSAGEHANDLER(JavaScript)
|
|
|
|
|
{
|
2014-01-04 02:14:53 -08:00
|
|
|
g_GUI->GetActiveGUI()->GetScriptInterface()->LoadGlobalScript(L"Atlas", *msg->command);
|
2007-02-01 18:17:15 -08:00
|
|
|
}
|
|
|
|
|
|
2010-06-01 17:36:21 -07:00
|
|
|
MESSAGEHANDLER(GuiSwitchPage)
|
|
|
|
|
{
|
2014-08-04 13:14:17 -07:00
|
|
|
g_GUI->SwitchPage(*msg->page, NULL, JS::UndefinedHandleValue);
|
2010-06-01 17:36:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(GuiMouseButtonEvent)
|
|
|
|
|
{
|
|
|
|
|
SDL_Event_ ev = { { 0 } };
|
|
|
|
|
ev.ev.type = msg->pressed ? SDL_MOUSEBUTTONDOWN : SDL_MOUSEBUTTONUP;
|
|
|
|
|
ev.ev.button.button = msg->button;
|
|
|
|
|
ev.ev.button.state = msg->pressed ? SDL_PRESSED : SDL_RELEASED;
|
2017-05-15 17:09:16 -07:00
|
|
|
ev.ev.button.clicks = msg->clicks;
|
2010-06-01 17:36:21 -07:00
|
|
|
float x, y;
|
|
|
|
|
msg->pos->GetScreenSpace(x, y);
|
2019-09-18 08:02:36 -07:00
|
|
|
ev.ev.button.x = static_cast<u16>(Clamp<int>(x, 0, g_xres));
|
|
|
|
|
ev.ev.button.y = static_cast<u16>(Clamp<int>(y, 0, g_yres));
|
2010-06-01 17:36:21 -07:00
|
|
|
in_dispatch_event(&ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(GuiMouseMotionEvent)
|
|
|
|
|
{
|
|
|
|
|
SDL_Event_ ev = { { 0 } };
|
|
|
|
|
ev.ev.type = SDL_MOUSEMOTION;
|
|
|
|
|
float x, y;
|
|
|
|
|
msg->pos->GetScreenSpace(x, y);
|
2019-09-18 08:02:36 -07:00
|
|
|
ev.ev.motion.x = static_cast<u16>(Clamp<int>(x, 0, g_xres));
|
|
|
|
|
ev.ev.motion.y = static_cast<u16>(Clamp<int>(y, 0, g_yres));
|
2010-06-01 17:36:21 -07:00
|
|
|
in_dispatch_event(&ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(GuiKeyEvent)
|
|
|
|
|
{
|
|
|
|
|
SDL_Event_ ev = { { 0 } };
|
|
|
|
|
ev.ev.type = msg->pressed ? SDL_KEYDOWN : SDL_KEYUP;
|
2012-02-06 14:47:35 -08:00
|
|
|
ev.ev.key.keysym.sym = (SDL_Keycode)(int)msg->sdlkey;
|
Let players remap hotkeys in-game, fix default hotkeys being qwerty-specific.
- Provide a "Hotkey" screen to let players remap hotkeys in-game using a
convenient setup.
- Make all .cfg hotkeys refer to scancodes (i.e. position on the
keyboard), so that default hotkeys now translate correctly for AZERTY,
QWERTZ and other layouts.
- 'BackSpace' is now an alias for 'Delete', and works for killing units.
This fixes #1917, as macs don't have a proper delete key and would need
to use Fn+Del otherwise. This shifts "timewarp" to Shift+BackSpace.
Functionally, this switches hotkeys to scancodes, as that makes more
sense (they are combinations of key positions, not actual text output).
SDL includes are cleaned and key names are reused.
Fixes #2850, Fixes #2604, Refs #1810, Fixes #1917.
Follows work in 3d7784d2af.
Various diffs tested by: Angen, Stan, Freagarach
Comments by: Nescio
Differential Revision: https://code.wildfiregames.com/D2814
This was SVN commit r24215.
2020-11-19 01:27:26 -08:00
|
|
|
ev.ev.key.keysym.scancode = SDL_GetScancodeFromKey((SDL_Keycode)(int)msg->sdlkey);
|
2010-06-01 17:36:21 -07:00
|
|
|
in_dispatch_event(&ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MESSAGEHANDLER(GuiCharEvent)
|
|
|
|
|
{
|
2020-08-03 05:23:16 -07:00
|
|
|
// Simulate special 'text input' events in the SDL
|
|
|
|
|
// This isn't quite compatible with WXWidget's handling,
|
|
|
|
|
// so to avoid trouble we only send 'letter-like' ASCII input.
|
2010-06-01 17:36:21 -07:00
|
|
|
SDL_Event_ ev = { { 0 } };
|
2020-08-03 05:23:16 -07:00
|
|
|
ev.ev.type = SDL_TEXTEDITING;
|
|
|
|
|
ev.ev.text.type = SDL_TEXTEDITING;
|
|
|
|
|
ev.ev.text.text[0] = (char)msg->sdlkey;
|
|
|
|
|
ev.ev.text.text[1] = (char)0;
|
2010-06-01 17:36:21 -07:00
|
|
|
in_dispatch_event(&ev);
|
|
|
|
|
|
2020-08-03 05:23:16 -07:00
|
|
|
ev.ev.type = SDL_TEXTINPUT;
|
|
|
|
|
ev.ev.text.type = SDL_TEXTINPUT;
|
|
|
|
|
ev.ev.text.text[0] = (char)msg->sdlkey;
|
|
|
|
|
ev.ev.text.text[1] = (char)0;
|
2010-06-01 17:36:21 -07:00
|
|
|
in_dispatch_event(&ev);
|
|
|
|
|
}
|
2007-01-16 19:25:20 -08:00
|
|
|
|
2021-12-31 02:35:56 -08:00
|
|
|
} // namespace AtlasMessage
|