summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwarmist2012-09-09 02:28:07 +0300
committerwarmist2012-09-09 02:28:07 +0300
commit8e0f3e3bce25484ecd37280e764302737acb8443 (patch)
tree291d92ba9cca8fe159b38a00a370e524943acd09
parentfb88aad51d9a49f489ed99fb1c33f9212b38c068 (diff)
downloaddfhack-8e0f3e3bce25484ecd37280e764302737acb8443.tar.gz
dfhack-8e0f3e3bce25484ecd37280e764302737acb8443.tar.bz2
dfhack-8e0f3e3bce25484ecd37280e764302737acb8443.tar.xz
Added ListBox to gui.dialogs
A listbox class. Can be either filled with table of strings, or string+callback tables. Needs some code revision :)
-rw-r--r--library/lua/gui/dialogs.lua95
1 files changed, 95 insertions, 0 deletions
diff --git a/library/lua/gui/dialogs.lua b/library/lua/gui/dialogs.lua
index c4f15c9a..dc435811 100644
--- a/library/lua/gui/dialogs.lua
+++ b/library/lua/gui/dialogs.lua
@@ -172,5 +172,100 @@ function showInputPrompt(title, text, tcolor, input, on_input, on_cancel, min_wi
}:show()
end
+ListBox = defclass(ListBox, MessageBox)
+
+ListBox.focus_path = 'ListBox'
+
+function ListBox:init(info)
+ info = info or {}
+ self:init_fields{
+ selection = info.selection or 0,
+ choices = info.choices or {},
+ select_pen = info.select_pen,
+ on_input = info.on_input,
+ page_top = 0
+ }
+ MessageBox.init(self, info)
+ self.on_accept = nil
+ return self
+end
+
+function ListBox:getWantedFrameSize()
+ local mw, mh = MessageBox.getWantedFrameSize(self)
+ return mw, mh+#self.choices
+end
+
+function ListBox:onRenderBody(dc)
+ MessageBox.onRenderBody(self, dc)
+
+ dc:newline(1)
+
+ if self.selection>dc.height-3 then
+ self.page_top=self.selection-(dc.height-3)
+ elseif self.selection<self.page_top and self.selection >0 then
+ self.page_top=self.selection-1
+ end
+ for i,entry in ipairs(self.choices) do
+ if type(entry)=="table" then
+ entry=entry[1]
+ end
+ if i>self.page_top then
+ if i == self.selection then
+ dc:pen(self.select_pen or COLOR_LIGHTCYAN)
+ else
+ dc:pen(self.text_pen or COLOR_GREY)
+ end
+ dc:string(entry)
+ dc:newline(1)
+ end
+ end
+end
+function ListBox:moveCursor(delta)
+ local newsel=self.selection+delta
+ if #self.choices ~=0 then
+ if newsel<1 or newsel>#self.choices then
+ newsel=newsel % #self.choices
+ end
+ end
+ self.selection=newsel
+end
+function ListBox:onInput(keys)
+ if keys.SELECT then
+ self:dismiss()
+ local choice=self.choices[self.selection]
+ if self.on_input then
+ self.on_input(self.selection,choice)
+ end
+
+ if choice and choice[2] then
+ choice[2](choice,self.selection) -- maybe reverse the arguments?
+ end
+ elseif keys.LEAVESCREEN then
+ self:dismiss()
+ if self.on_cancel then
+ self.on_cancel()
+ end
+ elseif keys.CURSOR_UP then
+ self:moveCursor(-1)
+ elseif keys.CURSOR_DOWN then
+ self:moveCursor(1)
+ elseif keys.CURSOR_UP_FAST then
+ self:moveCursor(-10)
+ elseif keys.CURSOR_DOWN_FAST then
+ self:moveCursor(10)
+ end
+end
+
+function showListPrompt(title, text, tcolor, choices, on_input, on_cancel, min_width)
+ mkinstance(ListBox):init{
+ title = title,
+ text = text,
+ text_pen = tcolor,
+ choices = choices,
+ on_input = on_input,
+ on_cancel = on_cancel,
+ frame_width = min_width,
+ }:show()
+end
return _ENV