Skip to content

Commit

Permalink
feat(radar): convert input to new Lua input system
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mwerle committed Nov 15, 2024
1 parent fcf3e0f commit 9ec571d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 17 deletions.
4 changes: 4 additions & 0 deletions data/lang/input-core/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions data/meta/CoreObject/Game.meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 19 additions & 4 deletions data/pigui/modules/radar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Pi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LuaConsole> Pi::luaConsole;
Game *Pi::game;
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/Pi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions src/lua/LuaGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 },
Expand Down
13 changes: 0 additions & 13 deletions src/ship/PlayerShipController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() :
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 9ec571d

Please sign in to comment.