2012-09-05 09:45:45 -06:00
|
|
|
-- Some simple dialog screens
|
|
|
|
|
|
|
|
local _ENV = mkmodule('gui.dialogs')
|
|
|
|
|
|
|
|
local gui = require('gui')
|
|
|
|
local utils = require('utils')
|
|
|
|
|
|
|
|
local dscreen = dfhack.screen
|
|
|
|
|
|
|
|
MessageBox = defclass(MessageBox, gui.FramedScreen)
|
|
|
|
|
2012-09-05 11:27:42 -06:00
|
|
|
MessageBox.focus_path = 'MessageBox'
|
2012-09-18 10:30:25 -06:00
|
|
|
|
|
|
|
MessageBox.ATTRS{
|
|
|
|
frame_style = gui.GREY_LINE_FRAME,
|
|
|
|
-- new attrs
|
|
|
|
text = {},
|
|
|
|
on_accept = DEFAULT_NIL,
|
|
|
|
on_cancel = DEFAULT_NIL,
|
|
|
|
on_close = DEFAULT_NIL,
|
|
|
|
text_pen = DEFAULT_NIL,
|
|
|
|
}
|
|
|
|
|
|
|
|
function MessageBox:preinit(info)
|
|
|
|
if type(info.text) == 'string' then
|
|
|
|
info.text = utils.split_string(info.text, "\n")
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function MessageBox:getWantedFrameSize()
|
|
|
|
local text = self.text
|
2012-09-05 11:27:42 -06:00
|
|
|
local w = #(self.frame_title or '') + 4
|
2012-09-05 09:45:45 -06:00
|
|
|
w = math.max(w, 20)
|
|
|
|
w = math.max(self.frame_width or w, w)
|
|
|
|
for _, l in ipairs(text) do
|
|
|
|
w = math.max(w, #l)
|
|
|
|
end
|
|
|
|
local h = #text+1
|
|
|
|
if h > 1 then
|
|
|
|
h = h+1
|
|
|
|
end
|
2012-09-05 11:27:42 -06:00
|
|
|
return w+2, #text+2
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function MessageBox:onRenderBody(dc)
|
|
|
|
if #self.text > 0 then
|
|
|
|
dc:newline(1):pen(self.text_pen or COLOR_GREY)
|
|
|
|
for _, l in ipairs(self.text or {}) do
|
|
|
|
dc:string(l):newline(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.on_accept then
|
2012-10-02 05:25:59 -06:00
|
|
|
local fr = self.frame_rect
|
|
|
|
local dc2 = gui.Painter.new_xy(fr.x1+1,fr.y2+1,fr.x2-8,fr.y2+1)
|
|
|
|
dc2:key('LEAVESCREEN'):string('/'):key('MENU_CONFIRM')
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function MessageBox:onDestroy()
|
|
|
|
if self.on_close then
|
|
|
|
self.on_close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function MessageBox:onInput(keys)
|
|
|
|
if keys.MENU_CONFIRM then
|
|
|
|
self:dismiss()
|
|
|
|
if self.on_accept then
|
|
|
|
self.on_accept()
|
|
|
|
end
|
|
|
|
elseif keys.LEAVESCREEN or (keys.SELECT and not self.on_accept) then
|
|
|
|
self:dismiss()
|
|
|
|
if self.on_cancel then
|
|
|
|
self.on_cancel()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function showMessage(title, text, tcolor, on_close)
|
2012-09-18 10:30:25 -06:00
|
|
|
MessageBox{
|
|
|
|
frame_title = title,
|
2012-09-05 09:45:45 -06:00
|
|
|
text = text,
|
|
|
|
text_pen = tcolor,
|
|
|
|
on_close = on_close
|
|
|
|
}:show()
|
|
|
|
end
|
|
|
|
|
|
|
|
function showYesNoPrompt(title, text, tcolor, on_accept, on_cancel)
|
2012-09-18 10:30:25 -06:00
|
|
|
MessageBox{
|
|
|
|
frame_title = title,
|
2012-09-05 09:45:45 -06:00
|
|
|
text = text,
|
|
|
|
text_pen = tcolor,
|
|
|
|
on_accept = on_accept,
|
|
|
|
on_cancel = on_cancel,
|
|
|
|
}:show()
|
|
|
|
end
|
|
|
|
|
|
|
|
InputBox = defclass(InputBox, MessageBox)
|
|
|
|
|
2012-09-05 11:27:42 -06:00
|
|
|
InputBox.focus_path = 'InputBox'
|
|
|
|
|
2012-09-18 10:30:25 -06:00
|
|
|
InputBox.ATTRS{
|
|
|
|
input = '',
|
|
|
|
input_pen = DEFAULT_NIL,
|
|
|
|
on_input = DEFAULT_NIL,
|
|
|
|
}
|
|
|
|
|
|
|
|
function InputBox:preinit(info)
|
|
|
|
info.on_accept = nil
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function InputBox:getWantedFrameSize()
|
2012-09-18 10:30:25 -06:00
|
|
|
local mw, mh = InputBox.super.getWantedFrameSize(self)
|
2012-09-05 09:45:45 -06:00
|
|
|
return mw, mh+2
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputBox:onRenderBody(dc)
|
2012-09-18 10:30:25 -06:00
|
|
|
InputBox.super.onRenderBody(self, dc)
|
2012-09-05 09:45:45 -06:00
|
|
|
|
|
|
|
dc:newline(1)
|
|
|
|
dc:pen(self.input_pen or COLOR_LIGHTCYAN)
|
2012-09-06 02:37:29 -06:00
|
|
|
dc:fill(1,dc:localY(),dc.width-2,dc:localY())
|
2012-09-05 09:45:45 -06:00
|
|
|
|
|
|
|
local cursor = '_'
|
2012-09-05 11:27:42 -06:00
|
|
|
if math.floor(dfhack.getTickCount()/300) % 2 == 0 then
|
2012-09-05 09:45:45 -06:00
|
|
|
cursor = ' '
|
|
|
|
end
|
|
|
|
local txt = self.input .. cursor
|
|
|
|
if #txt > dc.width-2 then
|
2012-09-06 02:37:29 -06:00
|
|
|
txt = string.char(27)..string.sub(txt, #txt-dc.width+4)
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
dc:string(txt)
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputBox:onInput(keys)
|
|
|
|
if keys.SELECT then
|
|
|
|
self:dismiss()
|
|
|
|
if self.on_input then
|
|
|
|
self.on_input(self.input)
|
|
|
|
end
|
|
|
|
elseif keys.LEAVESCREEN then
|
|
|
|
self:dismiss()
|
|
|
|
if self.on_cancel then
|
|
|
|
self.on_cancel()
|
|
|
|
end
|
|
|
|
elseif keys._STRING then
|
|
|
|
if keys._STRING == 0 then
|
|
|
|
self.input = string.sub(self.input, 1, #self.input-1)
|
|
|
|
else
|
|
|
|
self.input = self.input .. string.char(keys._STRING)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function showInputPrompt(title, text, tcolor, input, on_input, on_cancel, min_width)
|
2012-09-18 10:30:25 -06:00
|
|
|
InputBox{
|
|
|
|
frame_title = title,
|
2012-09-05 09:45:45 -06:00
|
|
|
text = text,
|
|
|
|
text_pen = tcolor,
|
|
|
|
input = input,
|
|
|
|
on_input = on_input,
|
|
|
|
on_cancel = on_cancel,
|
|
|
|
frame_width = min_width,
|
|
|
|
}:show()
|
|
|
|
end
|
|
|
|
|
2012-09-08 17:28:07 -06:00
|
|
|
ListBox = defclass(ListBox, MessageBox)
|
|
|
|
|
|
|
|
ListBox.focus_path = 'ListBox'
|
|
|
|
|
2012-09-18 10:30:25 -06:00
|
|
|
ListBox.ATTRS{
|
2012-10-02 06:45:17 -06:00
|
|
|
selection = 1,
|
2012-09-18 10:30:25 -06:00
|
|
|
choices = {},
|
|
|
|
select_pen = DEFAULT_NIL,
|
2012-10-02 06:45:17 -06:00
|
|
|
on_select = DEFAULT_NIL
|
2012-09-18 10:30:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function InputBox:preinit(info)
|
|
|
|
info.on_accept = nil
|
|
|
|
end
|
|
|
|
|
2012-09-08 17:28:07 -06:00
|
|
|
function ListBox:init(info)
|
2012-10-02 06:45:17 -06:00
|
|
|
self.page_top = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
local function choice_text(entry)
|
|
|
|
if type(entry)=="table" then
|
|
|
|
return entry.caption or entry[1]
|
|
|
|
else
|
|
|
|
return entry
|
|
|
|
end
|
2012-09-08 17:28:07 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function ListBox:getWantedFrameSize()
|
2012-09-18 10:30:25 -06:00
|
|
|
local mw, mh = ListBox.super.getWantedFrameSize(self)
|
2012-10-02 06:45:17 -06:00
|
|
|
for _,v in ipairs(self.choices) do
|
|
|
|
local text = choice_text(v)
|
|
|
|
mw = math.max(mw, #text+2)
|
|
|
|
end
|
|
|
|
return mw, mh+#self.choices+1
|
2012-09-08 17:28:07 -06:00
|
|
|
end
|
|
|
|
|
2012-10-02 06:45:17 -06:00
|
|
|
function ListBox:postUpdateLayout()
|
|
|
|
self.page_size = self.frame_rect.height - #self.text - 3
|
|
|
|
self:moveCursor(0)
|
|
|
|
end
|
2012-09-09 00:53:08 -06:00
|
|
|
|
2012-10-02 06:45:17 -06:00
|
|
|
function ListBox:moveCursor(delta)
|
|
|
|
local page = math.max(1, self.page_size)
|
|
|
|
local cnt = #self.choices
|
|
|
|
local off = self.selection+delta-1
|
|
|
|
local ds = math.abs(delta)
|
|
|
|
|
|
|
|
if ds > 1 then
|
|
|
|
if off >= cnt+ds-1 then
|
|
|
|
off = 0
|
|
|
|
else
|
|
|
|
off = math.min(cnt-1, off)
|
2012-09-09 00:53:08 -06:00
|
|
|
end
|
2012-10-02 06:45:17 -06:00
|
|
|
if off <= -ds then
|
|
|
|
off = cnt-1
|
|
|
|
else
|
|
|
|
off = math.max(0, off)
|
2012-09-09 00:53:08 -06:00
|
|
|
end
|
|
|
|
end
|
2012-10-02 06:45:17 -06:00
|
|
|
|
|
|
|
self.selection = 1 + off % cnt
|
|
|
|
self.page_top = 1 + page * math.floor((self.selection-1) / page)
|
2012-09-08 17:28:07 -06:00
|
|
|
end
|
2012-09-18 10:30:25 -06:00
|
|
|
|
2012-10-02 06:45:17 -06:00
|
|
|
function ListBox:onRenderBody(dc)
|
|
|
|
ListBox.super.onRenderBody(self, dc)
|
|
|
|
|
|
|
|
dc:newline(1):pen(self.select_pen or COLOR_CYAN)
|
|
|
|
|
|
|
|
local choices = self.choices
|
|
|
|
local iend = math.min(#choices, self.page_top+self.page_size-1)
|
|
|
|
|
|
|
|
for i = self.page_top,iend do
|
|
|
|
local text = choice_text(choices[i])
|
|
|
|
if text then
|
|
|
|
dc.cur_pen.bold = (i == self.selection);
|
|
|
|
dc:string(text)
|
|
|
|
else
|
|
|
|
dc:string('?ERROR?', COLOR_LIGHTRED)
|
2012-09-09 00:53:08 -06:00
|
|
|
end
|
2012-10-02 06:45:17 -06:00
|
|
|
dc:newline(1)
|
2012-09-09 00:53:08 -06:00
|
|
|
end
|
2012-09-08 17:28:07 -06:00
|
|
|
end
|
2012-09-18 10:30:25 -06:00
|
|
|
|
2012-09-08 17:28:07 -06:00
|
|
|
function ListBox:onInput(keys)
|
|
|
|
if keys.SELECT then
|
|
|
|
self:dismiss()
|
2012-10-02 06:45:17 -06:00
|
|
|
|
2012-09-09 00:53:08 -06:00
|
|
|
local choice=self.choices[self.selection]
|
2012-10-02 06:45:17 -06:00
|
|
|
if self.on_select then
|
|
|
|
self.on_select(self.selection, choice)
|
2012-09-08 17:28:07 -06:00
|
|
|
end
|
2012-09-09 00:53:08 -06:00
|
|
|
|
2012-10-02 06:45:17 -06:00
|
|
|
if choice then
|
|
|
|
local callback = choice.on_select or choice[2]
|
|
|
|
if callback then
|
|
|
|
callback(choice, self.selection)
|
|
|
|
end
|
2012-09-09 00:53:08 -06:00
|
|
|
end
|
2012-09-08 17:28:07 -06:00
|
|
|
elseif keys.LEAVESCREEN then
|
|
|
|
self:dismiss()
|
|
|
|
if self.on_cancel then
|
|
|
|
self.on_cancel()
|
|
|
|
end
|
2012-10-02 06:45:17 -06:00
|
|
|
elseif keys.STANDARDSCROLL_UP then
|
2012-09-09 00:53:08 -06:00
|
|
|
self:moveCursor(-1)
|
2012-10-02 06:45:17 -06:00
|
|
|
elseif keys.STANDARDSCROLL_DOWN then
|
2012-09-09 00:53:08 -06:00
|
|
|
self:moveCursor(1)
|
2012-10-02 06:45:17 -06:00
|
|
|
elseif keys.STANDARDSCROLL_PAGEUP then
|
|
|
|
self:moveCursor(-self.page_size)
|
|
|
|
elseif keys.STANDARDSCROLL_PAGEDOWN then
|
|
|
|
self:moveCursor(self.page_size)
|
2012-09-08 17:28:07 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-02 06:45:17 -06:00
|
|
|
function showListPrompt(title, text, tcolor, choices, on_select, on_cancel, min_width)
|
2012-09-18 10:30:25 -06:00
|
|
|
ListBox{
|
|
|
|
frame_title = title,
|
2012-09-08 17:28:07 -06:00
|
|
|
text = text,
|
|
|
|
text_pen = tcolor,
|
|
|
|
choices = choices,
|
2012-10-02 06:45:17 -06:00
|
|
|
on_select = on_select,
|
2012-09-08 17:28:07 -06:00
|
|
|
on_cancel = on_cancel,
|
|
|
|
frame_width = min_width,
|
|
|
|
}:show()
|
|
|
|
end
|
2012-09-05 09:45:45 -06:00
|
|
|
|
|
|
|
return _ENV
|