2022-11-04 18:42:38 -06:00
|
|
|
local _ENV = mkmodule('plugins.hotkeys')
|
|
|
|
|
|
|
|
local gui = require('gui')
|
|
|
|
local helpdb = require('helpdb')
|
|
|
|
local overlay = require('plugins.overlay')
|
|
|
|
local widgets = require('gui.widgets')
|
|
|
|
|
|
|
|
-- ----------------- --
|
|
|
|
-- HotspotMenuWidget --
|
|
|
|
-- ----------------- --
|
|
|
|
|
|
|
|
HotspotMenuWidget = defclass(HotspotMenuWidget, overlay.OverlayWidget)
|
|
|
|
HotspotMenuWidget.ATTRS{
|
2023-01-03 13:07:40 -07:00
|
|
|
default_pos={x=2,y=2},
|
2022-11-04 18:42:38 -06:00
|
|
|
hotspot=true,
|
2023-01-01 18:21:35 -07:00
|
|
|
viewscreens='all',
|
2022-11-04 18:42:38 -06:00
|
|
|
overlay_onupdate_max_freq_seconds=0,
|
2022-12-27 18:01:39 -07:00
|
|
|
frame={w=4, h=3}
|
2022-11-04 18:42:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function HotspotMenuWidget:init()
|
|
|
|
self.mouseover = false
|
|
|
|
end
|
|
|
|
|
|
|
|
function HotspotMenuWidget:overlay_onupdate()
|
|
|
|
local hasMouse = self:getMousePos()
|
|
|
|
if hasMouse and not self.mouseover then
|
|
|
|
self.mouseover = true
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
self.mouseover = hasMouse
|
|
|
|
end
|
|
|
|
|
|
|
|
function HotspotMenuWidget:overlay_trigger()
|
2023-01-22 01:43:10 -07:00
|
|
|
return MenuScreen{hotspot=self}:show()
|
2022-11-04 18:42:38 -06:00
|
|
|
end
|
|
|
|
|
2023-01-03 02:34:58 -07:00
|
|
|
local dscreen = dfhack.screen
|
|
|
|
|
|
|
|
function HotspotMenuWidget:onRenderBody(dc)
|
|
|
|
local tpos = dfhack.textures.getDfhackLogoTexposStart()
|
|
|
|
local x, y = dc.x, dc.y
|
|
|
|
|
|
|
|
if tpos == -1 then
|
|
|
|
dscreen.paintString(COLOR_WHITE, x, y+0, '!DF!')
|
|
|
|
dscreen.paintString(COLOR_WHITE, x, y+1, '!Ha!')
|
|
|
|
dscreen.paintString(COLOR_WHITE, x, y+2, '!ck!')
|
|
|
|
else
|
|
|
|
dscreen.paintTile(COLOR_WHITE, x+0, y+0, '!', tpos+0)
|
|
|
|
dscreen.paintTile(COLOR_WHITE, x+1, y+0, 'D', tpos+1)
|
|
|
|
dscreen.paintTile(COLOR_WHITE, x+2, y+0, 'F', tpos+2)
|
|
|
|
dscreen.paintTile(COLOR_WHITE, x+3, y+0, '!', tpos+3)
|
|
|
|
|
|
|
|
dscreen.paintTile(COLOR_WHITE, x+0, y+1, '!', tpos+4)
|
|
|
|
dscreen.paintTile(COLOR_WHITE, x+1, y+1, 'H', tpos+5)
|
|
|
|
dscreen.paintTile(COLOR_WHITE, x+2, y+1, 'a', tpos+6)
|
|
|
|
dscreen.paintTile(COLOR_WHITE, x+3, y+1, '!', tpos+7)
|
|
|
|
|
|
|
|
dscreen.paintTile(COLOR_WHITE, x+0, y+2, '!', tpos+8)
|
|
|
|
dscreen.paintTile(COLOR_WHITE, x+1, y+2, 'c', tpos+9)
|
|
|
|
dscreen.paintTile(COLOR_WHITE, x+2, y+2, 'k', tpos+10)
|
|
|
|
dscreen.paintTile(COLOR_WHITE, x+3, y+2, '!', tpos+11)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-04 18:42:38 -06:00
|
|
|
-- register the menu hotspot with the overlay
|
|
|
|
OVERLAY_WIDGETS = {menu=HotspotMenuWidget}
|
|
|
|
|
2023-01-13 13:18:18 -07:00
|
|
|
-- ---- --
|
|
|
|
-- Menu --
|
|
|
|
-- ---- --
|
2022-11-04 18:42:38 -06:00
|
|
|
|
|
|
|
local ARROW = string.char(26)
|
2022-11-07 14:59:10 -07:00
|
|
|
local MAX_LIST_WIDTH = 45
|
|
|
|
local MAX_LIST_HEIGHT = 15
|
2022-11-04 18:42:38 -06:00
|
|
|
|
2023-01-22 01:43:10 -07:00
|
|
|
Menu = defclass(Menu, widgets.Panel)
|
2023-01-13 13:18:18 -07:00
|
|
|
Menu.ATTRS{
|
2023-01-22 01:43:10 -07:00
|
|
|
hotspot=DEFAULT_NIL,
|
2022-11-04 18:42:38 -06:00
|
|
|
}
|
|
|
|
|
2022-11-07 14:59:10 -07:00
|
|
|
-- get a map from the binding string to a list of hotkey strings that all
|
|
|
|
-- point to that binding
|
|
|
|
local function get_bindings_to_hotkeys(hotkeys, bindings)
|
|
|
|
local bindings_to_hotkeys = {}
|
|
|
|
for _,hotkey in ipairs(hotkeys) do
|
|
|
|
local binding = bindings[hotkey]
|
|
|
|
table.insert(ensure_key(bindings_to_hotkeys, binding), hotkey)
|
|
|
|
end
|
|
|
|
return bindings_to_hotkeys
|
|
|
|
end
|
|
|
|
|
|
|
|
-- number of non-text tiles: icon, space, space between cmd and hk, scrollbar
|
|
|
|
local LIST_BUFFER = 2 + 1 + 1
|
|
|
|
|
|
|
|
local function get_choices(hotkeys, bindings, is_inverted)
|
|
|
|
local choices, max_width, seen = {}, 0, {}
|
|
|
|
local bindings_to_hotkeys = get_bindings_to_hotkeys(hotkeys, bindings)
|
|
|
|
|
|
|
|
-- build list choices
|
|
|
|
for _,hotkey in ipairs(hotkeys) do
|
|
|
|
local command = bindings[hotkey]
|
|
|
|
if seen[command] then goto continue end
|
|
|
|
seen[command] = true
|
|
|
|
local hk_width, tokens = 0, {}
|
|
|
|
for _,hk in ipairs(bindings_to_hotkeys[command]) do
|
|
|
|
if hk_width ~= 0 then
|
|
|
|
table.insert(tokens, ', ')
|
|
|
|
hk_width = hk_width + 2
|
|
|
|
end
|
|
|
|
table.insert(tokens, {text=hk, pen=COLOR_LIGHTGREEN})
|
|
|
|
hk_width = hk_width + #hk
|
|
|
|
end
|
|
|
|
local command_str = command
|
|
|
|
if hk_width + #command + LIST_BUFFER > MAX_LIST_WIDTH then
|
|
|
|
local max_command_len = MAX_LIST_WIDTH - hk_width - LIST_BUFFER
|
|
|
|
command_str = command:sub(1, max_command_len - 3) .. '...'
|
|
|
|
end
|
|
|
|
table.insert(tokens, 1, {text=command_str})
|
|
|
|
local choice = {icon=ARROW, command=command, text=tokens,
|
|
|
|
hk_width=hk_width}
|
|
|
|
max_width = math.max(max_width, hk_width + #command_str + LIST_BUFFER)
|
|
|
|
table.insert(choices, is_inverted and 1 or #choices + 1, choice)
|
|
|
|
::continue::
|
|
|
|
end
|
|
|
|
|
|
|
|
-- adjust width of command fields so the hotkey tokens are right justified
|
|
|
|
for _,choice in ipairs(choices) do
|
|
|
|
local command_token = choice.text[1]
|
|
|
|
command_token.width = max_width - choice.hk_width - 3
|
|
|
|
end
|
|
|
|
|
|
|
|
return choices, max_width
|
|
|
|
end
|
|
|
|
|
2023-01-13 13:18:18 -07:00
|
|
|
function Menu:init()
|
|
|
|
local hotkeys, bindings = getHotkeys()
|
|
|
|
|
2023-01-22 01:43:10 -07:00
|
|
|
local is_inverted = not not self.hotspot.frame.b
|
2023-01-13 13:18:18 -07:00
|
|
|
local choices,list_width = get_choices(hotkeys, bindings, is_inverted)
|
2022-11-04 18:42:38 -06:00
|
|
|
|
2023-01-22 01:43:10 -07:00
|
|
|
local list_frame = copyall(self.hotspot.frame)
|
2023-01-15 21:17:23 -07:00
|
|
|
local list_widget_frame = {h=math.min(#choices, MAX_LIST_HEIGHT)}
|
|
|
|
local quickstart_frame = {}
|
2022-11-07 14:59:10 -07:00
|
|
|
list_frame.w = list_width + 2
|
2023-01-15 21:17:23 -07:00
|
|
|
list_frame.h = list_widget_frame.h + 4
|
2022-11-04 18:42:38 -06:00
|
|
|
if list_frame.t then
|
2022-11-07 14:59:10 -07:00
|
|
|
list_frame.t = math.max(0, list_frame.t - 1)
|
2023-01-15 21:17:23 -07:00
|
|
|
list_widget_frame.t = 0
|
|
|
|
quickstart_frame.b = 0
|
2022-11-04 18:42:38 -06:00
|
|
|
else
|
2022-11-07 14:59:10 -07:00
|
|
|
list_frame.b = math.max(0, list_frame.b - 1)
|
2023-01-15 21:17:23 -07:00
|
|
|
list_widget_frame.b = 0
|
|
|
|
quickstart_frame.t = 0
|
2022-11-07 14:59:10 -07:00
|
|
|
end
|
|
|
|
if list_frame.l then
|
2023-01-13 13:18:18 -07:00
|
|
|
list_frame.l = math.max(0, list_frame.l + 5)
|
2022-11-07 14:59:10 -07:00
|
|
|
else
|
2023-01-13 13:18:18 -07:00
|
|
|
list_frame.r = math.max(0, list_frame.r + 5)
|
2022-11-04 18:42:38 -06:00
|
|
|
end
|
|
|
|
|
2022-11-07 14:59:10 -07:00
|
|
|
local help_frame = {w=list_frame.w, l=list_frame.l, r=list_frame.r}
|
|
|
|
if list_frame.t then
|
2022-12-27 18:01:39 -07:00
|
|
|
help_frame.t = list_frame.t + list_frame.h
|
2022-11-07 14:59:10 -07:00
|
|
|
else
|
2022-12-27 18:01:39 -07:00
|
|
|
help_frame.b = list_frame.b + list_frame.h
|
2022-11-04 18:42:38 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
self:addviews{
|
2023-01-15 21:17:23 -07:00
|
|
|
widgets.Panel{
|
2022-11-07 14:59:10 -07:00
|
|
|
view_id='list_panel',
|
2022-11-04 18:42:38 -06:00
|
|
|
frame=list_frame,
|
2023-01-23 05:03:41 -07:00
|
|
|
frame_style=gui.PANEL_FRAME,
|
2022-11-06 17:42:13 -07:00
|
|
|
frame_background=gui.CLEAR_PEN,
|
|
|
|
subviews={
|
|
|
|
widgets.List{
|
|
|
|
view_id='list',
|
2023-01-15 21:17:23 -07:00
|
|
|
frame=list_widget_frame,
|
2022-11-06 17:42:13 -07:00
|
|
|
choices=choices,
|
|
|
|
icon_width=2,
|
|
|
|
on_select=self:callback('onSelect'),
|
|
|
|
on_submit=self:callback('onSubmit'),
|
|
|
|
on_submit2=self:callback('onSubmit2'),
|
|
|
|
},
|
2023-01-15 21:17:23 -07:00
|
|
|
widgets.Panel{frame={h=1}},
|
|
|
|
widgets.HotkeyLabel{
|
|
|
|
frame=quickstart_frame,
|
|
|
|
label='Quickstart guide',
|
|
|
|
key='STRING_A063',
|
|
|
|
on_activate=function()
|
|
|
|
self:onSubmit(nil, {command='quickstart-guide'})
|
|
|
|
end,
|
|
|
|
},
|
2022-11-06 17:42:13 -07:00
|
|
|
},
|
2022-11-04 18:42:38 -06:00
|
|
|
},
|
2022-11-06 17:42:13 -07:00
|
|
|
widgets.ResizingPanel{
|
|
|
|
view_id='help_panel',
|
|
|
|
autoarrange_subviews=true,
|
2022-11-04 18:42:38 -06:00
|
|
|
frame=help_frame,
|
2023-01-23 05:03:41 -07:00
|
|
|
frame_style=gui.PANEL_FRAME,
|
2022-11-06 17:42:13 -07:00
|
|
|
frame_background=gui.CLEAR_PEN,
|
|
|
|
subviews={
|
|
|
|
widgets.WrappedLabel{
|
|
|
|
view_id='help',
|
|
|
|
text_to_wrap='',
|
|
|
|
scroll_keys={},
|
|
|
|
},
|
|
|
|
},
|
2022-11-04 18:42:38 -06:00
|
|
|
},
|
|
|
|
}
|
2022-11-30 19:11:02 -07:00
|
|
|
|
|
|
|
self.initialize = function()
|
|
|
|
self.subviews.list:setSelected(is_inverted and #choices or 1)
|
|
|
|
end
|
2022-11-04 18:42:38 -06:00
|
|
|
end
|
|
|
|
|
2023-01-13 13:18:18 -07:00
|
|
|
function Menu:onSelect(_, choice)
|
2022-11-06 17:42:13 -07:00
|
|
|
if not choice or #self.subviews == 0 then return end
|
2022-11-04 18:42:38 -06:00
|
|
|
local first_word = choice.command:trim():split(' +')[1]
|
2022-11-12 10:57:32 -07:00
|
|
|
if first_word:startswith(':') then first_word = first_word:sub(2) end
|
2022-11-06 17:42:13 -07:00
|
|
|
self.subviews.help.text_to_wrap = helpdb.is_entry(first_word) and
|
|
|
|
helpdb.get_entry_short_help(first_word) or 'Command not found'
|
|
|
|
self.subviews.help_panel:updateLayout()
|
2022-11-04 18:42:38 -06:00
|
|
|
end
|
|
|
|
|
2023-01-13 13:18:18 -07:00
|
|
|
function Menu:onSubmit(_, choice)
|
2022-11-06 17:47:45 -07:00
|
|
|
if not choice then return end
|
2023-01-13 13:18:18 -07:00
|
|
|
dfhack.screen.hideGuard(self.parent_view, dfhack.run_command, choice.command)
|
|
|
|
self.parent_view:dismiss()
|
2022-11-04 18:42:38 -06:00
|
|
|
end
|
|
|
|
|
2023-01-13 13:18:18 -07:00
|
|
|
function Menu:onSubmit2(_, choice)
|
2022-11-06 17:47:45 -07:00
|
|
|
if not choice then return end
|
2023-01-13 13:18:18 -07:00
|
|
|
self.parent_view:dismiss()
|
2022-11-04 18:42:38 -06:00
|
|
|
dfhack.run_script('gui/launcher', choice.command)
|
|
|
|
end
|
|
|
|
|
2023-01-13 13:18:18 -07:00
|
|
|
function Menu:onInput(keys)
|
|
|
|
if keys.LEAVESCREEN or keys._MOUSE_R_DOWN then
|
|
|
|
return false
|
2022-11-06 17:47:45 -07:00
|
|
|
elseif keys.STANDARDSCROLL_RIGHT then
|
|
|
|
self:onSubmit2(self.subviews.list:getSelected())
|
|
|
|
return true
|
2022-11-21 18:36:46 -07:00
|
|
|
elseif keys._MOUSE_L_DOWN then
|
2022-11-06 17:58:44 -07:00
|
|
|
local list = self.subviews.list
|
|
|
|
local x = list:getMousePos()
|
|
|
|
if x == 0 then -- clicked on icon
|
|
|
|
self:onSubmit2(list:getSelected())
|
|
|
|
return true
|
|
|
|
end
|
2023-01-22 01:43:10 -07:00
|
|
|
if not self:getMouseFramePos() and not self.hotspot:getMousePos() then
|
2023-01-13 13:18:18 -07:00
|
|
|
self.parent_view:dismiss()
|
|
|
|
return true
|
|
|
|
end
|
2022-11-04 18:42:38 -06:00
|
|
|
end
|
2023-01-13 13:18:18 -07:00
|
|
|
self:inputToSubviews(keys)
|
|
|
|
return true -- we're modal
|
2022-11-04 18:42:38 -06:00
|
|
|
end
|
|
|
|
|
2023-01-13 13:18:18 -07:00
|
|
|
function Menu:onRenderFrame(dc, rect)
|
2022-11-30 19:11:02 -07:00
|
|
|
if self.initialize then
|
|
|
|
self.initialize()
|
|
|
|
self.initialize = nil
|
|
|
|
end
|
2023-01-15 21:17:23 -07:00
|
|
|
Menu.super.onRenderFrame(dc, rect)
|
2022-11-06 17:42:13 -07:00
|
|
|
end
|
|
|
|
|
2023-01-13 13:18:18 -07:00
|
|
|
function Menu:getMouseFramePos()
|
|
|
|
return self.subviews.list_panel:getMouseFramePos() or
|
|
|
|
self.subviews.help_panel:getMouseFramePos()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Menu:onRenderBody(dc)
|
2022-11-07 14:59:10 -07:00
|
|
|
local panel = self.subviews.list_panel
|
2022-11-04 18:42:38 -06:00
|
|
|
local list = self.subviews.list
|
|
|
|
local idx = list:getIdxUnderMouse()
|
|
|
|
if idx and idx ~= self.last_mouse_idx then
|
|
|
|
-- focus follows mouse, but if cursor keys were used to change the
|
|
|
|
-- selection, don't override the selection until the mouse moves to
|
|
|
|
-- another item
|
|
|
|
list:setSelected(idx)
|
|
|
|
self.last_mouse_idx = idx
|
2023-01-13 13:18:18 -07:00
|
|
|
end
|
|
|
|
if self:getMouseFramePos() then
|
|
|
|
self.mouseover = true
|
|
|
|
elseif self.mouseover then
|
2022-11-07 14:59:10 -07:00
|
|
|
-- once the mouse has entered the list area, leaving the frame should
|
2022-11-04 18:42:38 -06:00
|
|
|
-- close the menu screen
|
2023-01-13 13:18:18 -07:00
|
|
|
self.parent_view:dismiss()
|
2022-11-04 18:42:38 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-13 13:18:18 -07:00
|
|
|
-- ---------- --
|
|
|
|
-- MenuScreen --
|
|
|
|
-- ---------- --
|
|
|
|
|
|
|
|
MenuScreen = defclass(MenuScreen, gui.ZScreen)
|
|
|
|
MenuScreen.ATTRS {
|
|
|
|
focus_path='hotkeys/menu',
|
2023-01-22 01:43:10 -07:00
|
|
|
hotspot=DEFAULT_NIL,
|
2023-01-13 13:18:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function MenuScreen:init()
|
|
|
|
self:addviews{
|
2023-01-22 01:43:10 -07:00
|
|
|
Menu{hotspot=self.hotspot},
|
2023-01-13 13:18:18 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function MenuScreen:onDismiss()
|
|
|
|
cleanupHotkeys()
|
|
|
|
end
|
|
|
|
|
2022-11-04 18:42:38 -06:00
|
|
|
return _ENV
|