Skip to content

Commit 8d345a7

Browse files
authored
make this an overlay
1 parent 33dbe85 commit 8d345a7

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

gui/tooltips.lua

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
-- Show tooltips on units and/or mouse
22

3+
--@ module = true
4+
35
local RELOAD = false -- set to true when actively working on this script
46

57
local gui = require('gui')
68
local widgets = require('gui.widgets')
9+
local overlay = require('plugins.overlay')
710
local ResizingPanel = require('gui.widgets.containers.resizing_panel')
811

912
--------------------------------------------------------------------------------
1013

11-
local follow_units = true;
12-
local follow_mouse = true;
14+
config = config or {
15+
follow_units = true,
16+
follow_mouse = true,
17+
}
1318
local function change_follow_units(new, old)
14-
follow_units = new
19+
config.follow_units = new
1520
end
1621
local function change_follow_mouse(new, old)
17-
follow_mouse = new
22+
config.follow_mouse = new
1823
end
1924

2025
local shortenings = {
@@ -25,6 +30,22 @@ local shortenings = {
2530

2631
local TITLE = "Tooltips"
2732

33+
if RELOAD then TooltipControlScreen = nil end
34+
TooltipControlScreen = defclass(TooltipControlScreen, gui.ZScreen)
35+
TooltipControlScreen.ATTRS {
36+
focus_path = "TooltipControlScreen",
37+
pass_movement_keys = true,
38+
}
39+
40+
function TooltipControlScreen:init()
41+
local controls = TooltipControlWindow{view_id = 'controls'}
42+
self:addviews{controls}
43+
end
44+
45+
function TooltipControlScreen:onDismiss()
46+
view = nil
47+
end
48+
2849
if RELOAD then TooltipControlWindow = nil end
2950
TooltipControlWindow = defclass(TooltipControlWindow, widgets.Window)
3051
TooltipControlWindow.ATTRS {
@@ -130,7 +151,7 @@ function MouseTooltip:init()
130151
end
131152

132153
function MouseTooltip:render(dc)
133-
if not follow_mouse then return end
154+
if not config.follow_mouse then return end
134155

135156
local x, y = dfhack.screen.getMousePos()
136157
if not x then return end
@@ -149,22 +170,25 @@ function MouseTooltip:render(dc)
149170
end
150171

151172
--------------------------------------------------------------------------------
152-
153-
if RELOAD then TooltipsVizualizer = nil end
154-
TooltipsVizualizer = defclass(TooltipsVizualizer, gui.ZScreen)
155-
TooltipsVizualizer.ATTRS{
156-
focus_path='TooltipsVizualizer',
157-
pass_movement_keys=true,
173+
if RELOAD then TooltipsOverlay = nil end
174+
TooltipsOverlay = defclass(TooltipsOverlay, overlay.OverlayWidget)
175+
TooltipsOverlay.ATTRS{
176+
desc='Adds tooltips with some info to units.',
177+
default_pos={x=1,y=1},
178+
default_enabled=true,
179+
fullscreen=true, -- not player-repositionable
180+
viewscreens={
181+
'dwarfmode/Default',
182+
},
158183
}
159184

160-
function TooltipsVizualizer:init()
161-
local controls = TooltipControlWindow{view_id = 'controls'}
185+
function TooltipsOverlay:init()
162186
local tooltip = MouseTooltip{view_id = 'tooltip'}
163-
self:addviews{controls, tooltip}
187+
self:addviews{tooltip}
164188
end
165189

166190
-- map coordinates -> interface layer coordinates
167-
function GetScreenCoordinates(map_coord)
191+
local function GetScreenCoordinates(map_coord)
168192
if not map_coord then return end
169193
-- -> map viewport offset
170194
local vp = df.global.world.viewport
@@ -195,10 +219,10 @@ function GetScreenCoordinates(map_coord)
195219
end
196220
end
197221

198-
function TooltipsVizualizer:onRenderFrame(dc, rect)
199-
TooltipsVizualizer.super.onRenderFrame(self, dc, rect)
222+
function TooltipsOverlay:render(dc)
223+
TooltipsOverlay.super.render(self, dc)
200224

201-
if not follow_units then return end
225+
if not config.follow_units then return end
202226

203227
if not dfhack.screen.inGraphicsMode() and not gui.blink_visible(500) then
204228
return
@@ -282,12 +306,21 @@ function TooltipsVizualizer:onRenderFrame(dc, rect)
282306
end
283307
end
284308

285-
function TooltipsVizualizer:onDismiss()
286-
view = nil
309+
function TooltipsOverlay:preUpdateLayout(parent_rect)
310+
self.frame.w = parent_rect.width
311+
self.frame.h = parent_rect.height
287312
end
288313

289314
----------------------------------------------------------------
290315

316+
OVERLAY_WIDGETS = {
317+
tooltips=TooltipsOverlay,
318+
}
319+
320+
if dfhack_flags.module then
321+
return
322+
end
323+
291324
if not dfhack.isMapLoaded() then
292325
qerror('gui/tooltips requires a map to be loaded')
293326
end
@@ -297,4 +330,4 @@ if RELOAD and view then
297330
-- view is nil now
298331
end
299332

300-
view = view and view:raise() or TooltipsVizualizer{}:show()
333+
view = view and view:raise() or TooltipControlScreen{}:show()

0 commit comments

Comments
 (0)