From 9ec571d5e7acd18b8329513c3bfce4866d7da571 Mon Sep 17 00:00:00 2001 From: Micha WERLE Date: Fri, 15 Nov 2024 11:39:35 +0900 Subject: [PATCH] feat(radar): convert input to new Lua input system Move the input control registration from C++ to Lua. From a user-perspective, the move also includes moving the configuration into a new "Ship HUD" controls page, as well as changing the default key bindings away from the arrow-keys to avoid clashing with the radial menu popups. TODO: Add an axis to control the radar zoom level. --- data/lang/input-core/en.json | 4 +++ data/meta/CoreObject/Game.meta.lua | 3 +++ data/pigui/modules/radar.lua | 23 ++++++++++++++--- src/Pi.cpp | 3 +++ src/Pi.h | 2 ++ src/lua/LuaGame.cpp | 41 ++++++++++++++++++++++++++++++ src/ship/PlayerShipController.cpp | 13 ---------- 7 files changed, 72 insertions(+), 17 deletions(-) diff --git a/data/lang/input-core/en.json b/data/lang/input-core/en.json index bcc20e4ba0..128f107365 100644 --- a/data/lang/input-core/en.json +++ b/data/lang/input-core/en.json @@ -338,6 +338,10 @@ "description": "Header for the ShipControls input page.", "message": "Ship - Controls" }, + "PAGE_SHIP_HUD": { + "description": "Header for the ShipHUD input page.", + "message": "Ship - HUD" + }, "PAGE_SHIP_VIEW": { "description": "Header for the ShipView input page.", "message": "Ship - View" diff --git a/data/meta/CoreObject/Game.meta.lua b/data/meta/CoreObject/Game.meta.lua index 65fe3fca13..b2b1afaf72 100644 --- a/data/meta/CoreObject/Game.meta.lua +++ b/data/meta/CoreObject/Game.meta.lua @@ -63,6 +63,9 @@ function Game.SwitchView() end ---@return string function Game.CurrentView() end +---@return string +function Game.PreviousView() end + ---@param view string function Game.SetView(view) end diff --git a/data/pigui/modules/radar.lua b/data/pigui/modules/radar.lua index a6f9ef965b..dee7d77395 100644 --- a/data/pigui/modules/radar.lua +++ b/data/pigui/modules/radar.lua @@ -24,13 +24,19 @@ local DEFAULT_RADAR_SIZE = 10000 local shouldDisplay2DRadar = false local blobSize = 6.0 +local input_group = 'ShipHUD.RadarControl' local keys = { - radar_toggle_mode = Input.GetActionBinding('BindRadarToggleMode'), - radar_reset = Input.GetActionBinding('BindRadarZoomReset'), + radar_reset = Input.RegisterActionBinding('BindRadarZoomReset', input_group, { activator = { key = Input.keys.slash } } ), + radar_toggle_mode = Input.RegisterActionBinding('BindRadarToggleMode', input_group, { activator = { key = Input.keys.semicolon } } ), -- TODO: Convert to Axis? - radar_zoom_in = Input.GetActionBinding('BindRadarZoomIn'), - radar_zoom_out = Input.GetActionBinding('BindRadarZoomOut'), + radar_zoom_in = Input.RegisterActionBinding('BindRadarZoomIn', input_group, { activator = { key = Input.keys.comma } } ), + radar_zoom_out = Input.RegisterActionBinding('BindRadarZoomOut', input_group, { activator = { key = Input.keys.period } } ), } +local input_frame = Input.CreateInputFrame("ShipHudRadar", false) +input_frame:AddAction(keys.radar_reset) +input_frame:AddAction(keys.radar_toggle_mode) +input_frame:AddAction(keys.radar_zoom_in) +input_frame:AddAction(keys.radar_zoom_out) local function getColorFor(item) local body = item.body @@ -370,6 +376,15 @@ local function displayRadar() end -- function displayRadar() +-- view has changed, update input frame +Event.Register("onViewChanged", function() + if Game.CurrentView() == "world" then + input_frame:AddToStack() + elseif Game.PreviousView() == "world" then + input_frame:RemoveFromStack() + end +end) + -- reset radar to default at game end Event.Register("onGameEnd", function() shouldDisplay2DRadar = false diff --git a/src/Pi.cpp b/src/Pi.cpp index a44c0d37e0..bc718b4333 100644 --- a/src/Pi.cpp +++ b/src/Pi.cpp @@ -108,6 +108,7 @@ ServerAgent *Pi::serverAgent; Input::Manager *Pi::input; Player *Pi::player; View *Pi::currentView; +View *Pi::previousView; TransferPlanner *Pi::planner; std::unique_ptr Pi::luaConsole; Game *Pi::game; @@ -1216,6 +1217,8 @@ void Pi::RequestQuit() void Pi::SetView(View *v) { + // TODO: Should it be an error or warning to switch the view to itself? + previousView = currentView; if (currentView) currentView->Detach(); currentView = v; if (currentView) currentView->Attach(); diff --git a/src/Pi.h b/src/Pi.h index e623070c60..6c12288866 100644 --- a/src/Pi.h +++ b/src/Pi.h @@ -169,6 +169,7 @@ class Pi { static void SetView(View *v); static View *GetView() { return currentView; } + static View *GetPreviousView() { return previousView; } static void SetAmountBackgroundStars(const float pc) { @@ -220,6 +221,7 @@ class Pi { static bool menuDone; static View *currentView; + static View *previousView; /** So, the game physics rate (50Hz) can run slower * than the frame rate. gameTickAlpha is the interpolation diff --git a/src/lua/LuaGame.cpp b/src/lua/LuaGame.cpp index be4a6d88f4..46f4685794 100644 --- a/src/lua/LuaGame.cpp +++ b/src/lua/LuaGame.cpp @@ -602,6 +602,46 @@ static int l_game_current_view(lua_State *l) return 1; } +/* + * Function: PreviousView + * + * Return the previously active game view + * + * > previous_view = Game.PreviousView() + * + * Return: + * + * view - a string describing the game view: "world", "space_station", "info", "sector", "system", "death", "settings" + * + * Availability: + * + * 2024-11 + * + * Status: + * + * stable + */ + +static int l_game_previous_view(lua_State *l) +{ + const View *view = Pi::GetPreviousView(); + if (view == Pi::game->GetWorldView()) + LuaPush(l, "world"); + else if (view == Pi::game->GetSpaceStationView()) + LuaPush(l, "space_station"); + else if (view == Pi::game->GetInfoView()) + LuaPush(l, "info"); + else if (view == Pi::game->GetSectorView()) + LuaPush(l, "sector"); + else if (view == Pi::game->GetSystemView()) + LuaPush(l, "system"); + else if (view == Pi::game->GetDeathView()) + LuaPush(l, "death"); + else + lua_pushnil(l); + return 1; +} + // XXX temporary to support StationView "Launch" button // remove once WorldView has been converted to the new UI static int l_game_switch_view(lua_State *l) @@ -776,6 +816,7 @@ void LuaGame::Register() { "SwitchView", l_game_switch_view }, { "CurrentView", l_game_current_view }, + { "PreviousView", l_game_previous_view }, { "SetView", l_game_set_view }, { "GetDateTime", l_game_get_date_time }, { "GetPartsFromDateTime", l_game_get_parts_from_date_time }, diff --git a/src/ship/PlayerShipController.cpp b/src/ship/PlayerShipController.cpp index ca5d684a02..d35fa3abc5 100644 --- a/src/ship/PlayerShipController.cpp +++ b/src/ship/PlayerShipController.cpp @@ -166,13 +166,6 @@ REGISTER_INPUT_BINDING(PlayerShipController) auto landingGroup = controlsPage->GetBindingGroup("LandingControl"); input->AddActionBinding("BindToggleLandingGear", landingGroup, Action({ SDLK_n })); input->AddAxisBinding("BindControlLandingGear", landingGroup, Axis()); - - auto radarGroup = controlsPage->GetBindingGroup("RadarControl"); - input->AddActionBinding("BindRadarZoomReset", radarGroup, Action({ SDLK_DOWN })); - input->AddActionBinding("BindRadarToggleMode", radarGroup, Action({ SDLK_UP })); - input->AddActionBinding("BindRadarZoomIn", radarGroup, Action({ SDLK_LEFT })); - input->AddActionBinding("BindRadarZoomOut", radarGroup, Action({ SDLK_RIGHT })); - input->AddAxisBinding("BindRadarZoom", radarGroup, Axis()); } PlayerShipController::PlayerShipController() : @@ -250,12 +243,6 @@ void PlayerShipController::InputBinding::RegisterBindings() toggleLandingGear = AddAction("BindToggleLandingGear"); controlLandingGear = AddAxis("BindControlLandingGear"); - - AddAction("BindRadarToggleMode"); - AddAction("BindRadarZoomReset"); - // TODO: Convert to axis? - AddAction("BindRadarZoomIn"); - AddAction("BindRadarZoomOut"); } PlayerShipController::~PlayerShipController()