diff --git a/data/pigui/modules/radar.lua b/data/pigui/modules/radar.lua index a85f50e376..f82efd6ac2 100644 --- a/data/pigui/modules/radar.lua +++ b/data/pigui/modules/radar.lua @@ -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 = {} @@ -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() @@ -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