Skip to content

Commit

Permalink
feat(radar): show target selector if multiple targets on a pip
Browse files Browse the repository at this point in the history
Display a popup to allow the player to select a target if there are
multiple targets over a "pip".

Also fix issue if the player clicks outside a popup which could get
mouse handling confused.
  • Loading branch information
mwerle committed Nov 25, 2024
1 parent c089f9b commit e215579
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions data/pigui/modules/radar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ local DEFAULT_RADAR_SIZE = 10000
local shouldDisplay2DRadar = false
local blobSize = 6.0

-- These variable needs to be outside the draw function in order to capture the
-- current state between frames. We are trying to ensure that a mouse
-- click started and finished inside the radar area in order to trigger
-- the popups and actions.
local click_on_radar = false
local radar_popup_displayed = false
local popup_targets = {}

-- Current set of targets hovered over by the mouse
local mouseTargets = {}

Expand Down Expand Up @@ -346,11 +354,10 @@ radar3d.draw = function(self, center)
end
end

-- This variable needs to be outside the function in order to capture state
-- between frames. We are trying to ensure that a mouse right-click started and
-- finished inside the radar area in order to trigger the popup.
local click_on_radar = false
local radar_popup_displayed = false
local function onTargetClicked(target)
-- TODO: Should ships be set as nav or combat targets?
Game.player:SetNavTarget(target.body)
end

-- display either the 3D or the 2D radar, show a popup on right click to select
local function displayRadar()
Expand Down Expand Up @@ -394,39 +401,56 @@ local function displayRadar()
Vector2(center.x + ui.reticuleCircleRadius * 0.9,
center.y + ui.reticuleCircleRadius * 0.7))
end
if isMouseOverRadar() or radar_popup_displayed then
if radar_popup_displayed or isMouseOverRadar() then
ui.popup("radarselector", function()
if ui.selectable(lui.HUD_2D_RADAR, shouldDisplay2DRadar, {}) then
if not shouldDisplay2DRadar then
toggle_radar = true
end
radar_popup_displayed = false
end
if ui.selectable(lui.HUD_3D_RADAR, not shouldDisplay2DRadar, {}) then
if shouldDisplay2DRadar then
toggle_radar = true
end
radar_popup_displayed = false
end
end)
ui.popup("radartargetselector", function()
for k,v in pairs(popup_targets) do
if ui.selectable(v.label) then
onTargetClicked(v)
radar_popup_displayed = false
end
end
end)

if ui.isMouseClicked(0) or ui.isMouseClicked(1) then
if not click_on_radar and not radar_popup_displayed and ui.isMouseClicked(0) or ui.isMouseClicked(1) then
click_on_radar = true
end
if click_on_radar and ui.isMouseReleased(0) then
-- check if we are on a pip and if so, set it as the target
if #mouseTargets > 0 then
-- TODO: pop up a selector, for now just set the first one
local target = mouseTargets[1].body
-- TODO: Should ships be set as nav or combat targets?
player:SetNavTarget(target)
end
if radar_popup_displayed and (ui.isMouseReleased(0) or ui.isMouseReleased(1)) then
radar_popup_displayed = false
click_on_radar = false
end
if not toggle_radar and click_on_radar and ui.isMouseReleased(1) then
ui.openPopup("radarselector")
radar_popup_displayed = true
if click_on_radar then
if ui.isMouseReleased(0) then
popup_targets = mouseTargets
-- check if we are on a pip and if so, set it as the target
if #popup_targets > 1 then
ui.openPopup("radartargetselector")
radar_popup_displayed = true
elseif #popup_targets > 0 then
onTargetClicked(popup_targets[1])
end
click_on_radar = false
elseif ui.isMouseReleased(1) then
if not toggle_radar then
ui.openPopup("radarselector")
radar_popup_displayed = true
end
click_on_radar = false
end
end
-- TODO: figure out how to "capture" the mouse wheel to prevent
-- the game engine from using it to also zoom the viewport
if zoom == 0 then
zoom = ui.getMouseWheel()
end
Expand Down

0 comments on commit e215579

Please sign in to comment.