adapt lua library widgets to new keyboard keybindings

develop
Myk Taylor 2022-12-30 01:13:13 -08:00
parent bf9db8e223
commit 38b525bdb5
No known key found for this signature in database
6 changed files with 54 additions and 120 deletions

@ -4309,7 +4309,6 @@ Attributes:
If it returns false, the character is ignored. If it returns false, the character is ignored.
:on_change: Change notification callback; used as ``on_change(new_text,old_text)``. :on_change: Change notification callback; used as ``on_change(new_text,old_text)``.
:on_submit: Enter key callback; if set the field will handle the key and call ``on_submit(text)``. :on_submit: Enter key callback; if set the field will handle the key and call ``on_submit(text)``.
:on_submit2: Shift-Enter key callback; if set the field will handle the key and call ``on_submit2(text)``.
:key: If specified, the field is disabled until this key is pressed. Must be given as a string. :key: If specified, the field is disabled until this key is pressed. Must be given as a string.
:key_sep: If specified, will be used to customize how the activation key is :key_sep: If specified, will be used to customize how the activation key is
displayed. See ``token.key_sep`` in the ``Label`` documentation below. displayed. See ``token.key_sep`` in the ``Label`` documentation below.
@ -4339,8 +4338,8 @@ You can click where you want the cursor to move or you can use any of the
following keyboard hotkeys: following keyboard hotkeys:
- Left/Right arrow: move the cursor one character to the left or right. - Left/Right arrow: move the cursor one character to the left or right.
- Ctrl-Left/Right arrow: move the cursor one word to the left or right. - Ctrl-B/Ctrl-F: move the cursor one word back or forward.
- Alt-Left/Right arrow: move the cursor to the beginning/end of the text. - Ctrl-A/Ctrl-E: move the cursor to the beginning/end of the text.
Scrollbar class Scrollbar class
--------------- ---------------
@ -4622,8 +4621,8 @@ It has the following attributes:
with an empty list. with an empty list.
:on_submit: Enter key or mouse click callback; if specified, the list reacts to the :on_submit: Enter key or mouse click callback; if specified, the list reacts to the
key/click and calls the callback as ``on_submit(index,choice)``. key/click and calls the callback as ``on_submit(index,choice)``.
:on_submit2: Shift-Enter key or shift-mouse click callback; if specified, the list :on_submit2: Shift-click callback; if specified, the list reacts to the click and
reacts to the key/click and calls it as ``on_submit2(index,choice)``. calls the callback as ``on_submit2(index,choice)``.
:row_height: Height of every row in text lines. :row_height: Height of every row in text lines.
:icon_width: If not *nil*, the specified number of character columns :icon_width: If not *nil*, the specified number of character columns
are reserved to the left of the list item for the icons. are reserved to the left of the list item for the icons.

@ -258,8 +258,8 @@ function BuildingDialog:onSubmitItem(idx, item)
end end
function BuildingDialog:onInput(keys) function BuildingDialog:onInput(keys)
if keys.LEAVESCREEN or keys.LEAVESCREEN_ALL then if keys.LEAVESCREEN then
if self.subviews.back.visible and not keys.LEAVESCREEN_ALL then if self.subviews.back.visible then
self:onGoBack() self:onGoBack()
else else
self:dismiss() self:dismiss()
@ -267,9 +267,9 @@ function BuildingDialog:onInput(keys)
self.on_cancel() self.on_cancel()
end end
end end
else return true
self:inputToSubviews(keys)
end end
self:inputToSubviews(keys)
end end
function showBuildingPrompt(title, prompt, on_select, on_cancel, build_filter) function showBuildingPrompt(title, prompt, on_select, on_cancel, build_filter)

@ -59,19 +59,16 @@ function MessageBox:onDestroy()
end end
function MessageBox:onInput(keys) function MessageBox:onInput(keys)
if keys.MENU_CONFIRM then if keys.SELECT or keys.LEAVESCREEN then
self:dismiss() self:dismiss()
if self.on_accept then if keys.SELECT and self.on_accept then
self.on_accept() self.on_accept()
end elseif keys.LEAVESCREEN and self.on_cancel then
elseif keys.LEAVESCREEN or (keys.SELECT and not self.on_accept) then
self:dismiss()
if self.on_cancel then
self.on_cancel() self.on_cancel()
end end
else return true
self:inputToSubviews(keys)
end end
return self:inputToSubviews(keys)
end end
function showMessage(title, text, tcolor, on_close) function showMessage(title, text, tcolor, on_close)
@ -132,14 +129,15 @@ function InputBox:onInput(keys)
if self.on_input then if self.on_input then
self.on_input(self.subviews.edit.text) self.on_input(self.subviews.edit.text)
end end
return true
elseif keys.LEAVESCREEN then elseif keys.LEAVESCREEN then
self:dismiss() self:dismiss()
if self.on_cancel then if self.on_cancel then
self.on_cancel() self.on_cancel()
end end
else return true
self:inputToSubviews(keys)
end end
return self:inputToSubviews(keys)
end end
function showInputPrompt(title, text, tcolor, input, on_input, on_cancel, min_width) function showInputPrompt(title, text, tcolor, input, on_input, on_cancel, min_width)
@ -238,9 +236,9 @@ function ListBox:onInput(keys)
if self.on_cancel then if self.on_cancel then
self.on_cancel() self.on_cancel()
end end
else return true
self:inputToSubviews(keys)
end end
return self:inputToSubviews(keys)
end end
function showListPrompt(title, text, tcolor, choices, on_select, on_cancel, min_width, filter) function showListPrompt(title, text, tcolor, choices, on_select, on_cancel, min_width, filter)

@ -553,66 +553,4 @@ function MenuOverlay:renderMapOverlay(get_overlay_char_fn, bounds_rect)
end end
end end
--fakes a "real" workshop sidebar menu, but on exactly selected workshop
WorkshopOverlay = defclass(WorkshopOverlay, MenuOverlay)
WorkshopOverlay.focus_path="WorkshopOverlay"
WorkshopOverlay.ATTRS={
workshop=DEFAULT_NIL,
}
function WorkshopOverlay:onAboutToShow(below)
WorkshopOverlay.super.onAboutToShow(self,below)
if df.global.world.selected_building ~= self.workshop then
error("The workshop overlay tried to show up for incorrect workshop")
end
end
function WorkshopOverlay:onInput(keys)
local allowedKeys={ --TODO add options: job management, profile, etc...
"CURSOR_RIGHT","CURSOR_RIGHT_FAST","CURSOR_LEFT","CURSOR_LEFT_FAST","CURSOR_UP","CURSOR_UP_FAST","CURSOR_DOWN","CURSOR_DOWN_FAST",
"CURSOR_UPRIGHT","CURSOR_UPRIGHT_FAST","CURSOR_UPLEFT","CURSOR_UPLEFT_FAST","CURSOR_DOWNRIGHT","CURSOR_DOWNRIGHT_FAST","CURSOR_DOWNLEFT","CURSOR_DOWNLEFT_FAST",
"CURSOR_UP_Z","CURSOR_DOWN_Z","DESTROYBUILDING","CHANGETAB","SUSPENDBUILDING"}
if keys.LEAVESCREEN then
self:dismiss()
self:sendInputToParent('LEAVESCREEN')
elseif keys.CHANGETAB then
self:sendInputToParent("CHANGETAB")
self:inputToSubviews(keys)
self:updateLayout()
else
for _,name in ipairs(allowedKeys) do
if keys[name] then
self:sendInputToParent(name)
break
end
end
self:inputToSubviews(keys)
end
if df.global.world.selected_building ~= self.workshop then
self:dismiss()
return
end
end
function WorkshopOverlay:onGetSelectedBuilding()
return self.workshop
end
local function is_slated_for_remove( bld )
for i,v in ipairs(bld.jobs) do
if v.job_type==df.job_type.DestroyBuilding then
return true
end
end
return false
end
function WorkshopOverlay:render(dc)
self:renderParent()
if df.global.world.selected_building ~= self.workshop then
return
end
if is_slated_for_remove(self.workshop) then
return
end
WorkshopOverlay.super.render(self, dc)
end
return _ENV return _ENV

@ -255,8 +255,8 @@ function MaterialDialog:onSubmitItem(idx, item)
end end
function MaterialDialog:onInput(keys) function MaterialDialog:onInput(keys)
if keys.LEAVESCREEN or keys.LEAVESCREEN_ALL then if keys.LEAVESCREEN then
if self.subviews.back.visible and not keys.LEAVESCREEN_ALL then if self.subviews.back.visible then
self:onGoBack() self:onGoBack()
else else
self:dismiss() self:dismiss()
@ -264,9 +264,9 @@ function MaterialDialog:onInput(keys)
self.on_cancel() self.on_cancel()
end end
end end
else return true
self:inputToSubviews(keys)
end end
return self:inputToSubviews(keys)
end end
function showMaterialPrompt(title, prompt, on_select, on_cancel, mat_filter) function showMaterialPrompt(title, prompt, on_select, on_cancel, mat_filter)

@ -26,16 +26,17 @@ end
STANDARDSCROLL = { STANDARDSCROLL = {
STANDARDSCROLL_UP = -1, STANDARDSCROLL_UP = -1,
KEYBOARD_CURSOR_UP = -1,
CONTEXT_SCROLL_UP = -1,
STANDARDSCROLL_DOWN = 1, STANDARDSCROLL_DOWN = 1,
KEYBOARD_CURSOR_DOWN = 1,
CONTEXT_SCROLL_DOWN = 1,
STANDARDSCROLL_PAGEUP = '-page', STANDARDSCROLL_PAGEUP = '-page',
KEYBOARD_CURSOR_UP_FAST = '-page',
CONTEXT_SCROLL_PAGEUP = '-page',
STANDARDSCROLL_PAGEDOWN = '+page', STANDARDSCROLL_PAGEDOWN = '+page',
} KEYBOARD_CURSOR_DOWN_FAST = '+page',
CONTEXT_SCROLL_PAGEDOWN = '+page',
SECONDSCROLL = {
SECONDSCROLL_UP = -1,
SECONDSCROLL_DOWN = 1,
SECONDSCROLL_PAGEUP = '-page',
SECONDSCROLL_PAGEDOWN = '+page',
} }
------------ ------------
@ -677,22 +678,20 @@ function EditField:onInput(keys)
return true return true
end end
if keys.SELECT then if keys.SELECT or keys.CUSTOM_SHIFT_ENTER then
if self.key then
self:setFocus(false)
end
if self.on_submit then
self.on_submit(self.text)
return true
end
return not not self.key
elseif keys.SEC_SELECT then
if self.key then if self.key then
self:setFocus(false) self:setFocus(false)
end end
if self.on_submit2 then if keys.CUSTOM_SHIFT_ENTER then
self.on_submit2(self.text) if self.on_submit2 then
return true self.on_submit2(self.text)
return true
end
else
if self.on_submit then
self.on_submit(self.text)
return true
end
end end
return not not self.key return not not self.key
elseif keys._MOUSE_L then elseif keys._MOUSE_L then
@ -723,25 +722,25 @@ function EditField:onInput(keys)
self.on_change(self.text, old) self.on_change(self.text, old)
end end
return true return true
elseif keys.CURSOR_LEFT then elseif keys.KEYBOARD_CURSOR_LEFT then
self:setCursor(self.cursor - 1) self:setCursor(self.cursor - 1)
return true return true
elseif keys.A_MOVE_W_DOWN then -- Ctrl-Left (end of prev word) elseif keys.CUSTOM_CTRL_B then -- back one word
local _, prev_word_end = self.text:sub(1, self.cursor-1): local _, prev_word_end = self.text:sub(1, self.cursor-1):
find('.*[%w_%-][^%w_%-]') find('.*[%w_%-][^%w_%-]')
self:setCursor(prev_word_end or 1) self:setCursor(prev_word_end or 1)
return true return true
elseif keys.A_CARE_MOVE_W then -- Alt-Left (home) elseif keys.CUSTOM_CTRL_A then -- home
self:setCursor(1) self:setCursor(1)
return true return true
elseif keys.CURSOR_RIGHT then elseif keys.KEYBOARD_CURSOR_RIGHT then
self:setCursor(self.cursor + 1) self:setCursor(self.cursor + 1)
return true return true
elseif keys.A_MOVE_E_DOWN then -- Ctrl-Right (beginning of next word) elseif keys.CUSTOM_CTRL_F then -- forward one word
local _,next_word_start = self.text:find('[^%w_%-][%w_%-]', self.cursor) local _,next_word_start = self.text:find('[^%w_%-][%w_%-]', self.cursor)
self:setCursor(next_word_start) self:setCursor(next_word_start)
return true return true
elseif keys.A_CARE_MOVE_E then -- Alt-Right (end) elseif keys.CUSTOM_CTRL_E then -- end
self:setCursor() self:setCursor()
return true return true
end end
@ -1680,12 +1679,14 @@ end
function List:submit() function List:submit()
if self.on_submit and #self.choices > 0 then if self.on_submit and #self.choices > 0 then
self.on_submit(self:getSelected()) self.on_submit(self:getSelected())
return true
end end
end end
function List:submit2() function List:submit2()
if self.on_submit2 and #self.choices > 0 then if self.on_submit2 and #self.choices > 0 then
self.on_submit2(self:getSelected()) self.on_submit2(self:getSelected())
return true
end end
end end
@ -1693,12 +1694,10 @@ function List:onInput(keys)
if self:inputToSubviews(keys) then if self:inputToSubviews(keys) then
return true return true
end end
if self.on_submit and keys.SELECT then if keys.SELECT then
self:submit() return self:submit()
return true elseif keys.CUSTOM_SHIFT_ENTER then
elseif self.on_submit2 and keys.SEC_SELECT then return self:submit2()
self:submit2()
return true
elseif keys._MOUSE_L_DOWN then elseif keys._MOUSE_L_DOWN then
local idx = self:getIdxUnderMouse() local idx = self:getIdxUnderMouse()
if idx then if idx then