2019-06-28 17:50:02 +00:00
|
|
|
local class = require('opus.class')
|
|
|
|
local UI = require('opus.ui')
|
|
|
|
local Util = require('opus.util')
|
2019-02-06 04:03:57 +00:00
|
|
|
|
|
|
|
local colors = _G.colors
|
|
|
|
|
|
|
|
UI.Chooser = class(UI.Window)
|
|
|
|
UI.Chooser.defaults = {
|
|
|
|
UIElement = 'Chooser',
|
|
|
|
choices = { },
|
|
|
|
nochoice = 'Select',
|
|
|
|
backgroundFocusColor = colors.lightGray,
|
|
|
|
textInactiveColor = colors.gray,
|
2020-04-22 04:40:59 +00:00
|
|
|
leftIndicator = UI.extChars and '\171' or '<',
|
|
|
|
rightIndicator = UI.extChars and '\187' or '>',
|
2019-02-06 04:03:57 +00:00
|
|
|
height = 1,
|
2019-11-18 21:32:10 +00:00
|
|
|
accelerators = {
|
|
|
|
space = 'choice_next',
|
|
|
|
right = 'choice_next',
|
|
|
|
left = 'choice_prev',
|
|
|
|
}
|
2019-02-06 04:03:57 +00:00
|
|
|
}
|
2020-04-22 04:40:59 +00:00
|
|
|
function UI.Chooser:layout()
|
2019-02-06 04:03:57 +00:00
|
|
|
if not self.width and not self.ex then
|
|
|
|
self.width = 1
|
|
|
|
for _,v in pairs(self.choices) do
|
|
|
|
if #v.name > self.width then
|
|
|
|
self.width = #v.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.width = self.width + 4
|
|
|
|
end
|
2020-04-22 04:40:59 +00:00
|
|
|
UI.Window.layout(self)
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function UI.Chooser:draw()
|
2019-11-18 21:32:10 +00:00
|
|
|
local bg = self.focused and self.backgroundFocusColor or self.backgroundColor
|
2019-02-06 04:03:57 +00:00
|
|
|
local fg = self.inactive and self.textInactiveColor or self.textColor
|
|
|
|
local choice = Util.find(self.choices, 'value', self.value)
|
2019-11-18 21:32:10 +00:00
|
|
|
local value = choice and choice.name or self.nochoice
|
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
self:write(1, 1, self.leftIndicator, self.backgroundColor, colors.black)
|
|
|
|
self:write(2, 1, ' ' .. Util.widthify(tostring(value), self.width-4) .. ' ', bg, fg)
|
|
|
|
self:write(self.width, 1, self.rightIndicator, self.backgroundColor, colors.black)
|
|
|
|
end
|
|
|
|
|
|
|
|
function UI.Chooser:focus()
|
|
|
|
self:draw()
|
|
|
|
end
|
|
|
|
|
|
|
|
function UI.Chooser:eventHandler(event)
|
2019-11-18 21:32:10 +00:00
|
|
|
if event.type == 'choice_next' then
|
|
|
|
local _,k = Util.find(self.choices, 'value', self.value)
|
|
|
|
local choice
|
|
|
|
if not k then k = 0 end
|
|
|
|
if k and k < #self.choices then
|
|
|
|
choice = self.choices[k+1]
|
|
|
|
else
|
|
|
|
choice = self.choices[1]
|
|
|
|
end
|
|
|
|
self.value = choice.value
|
|
|
|
self:emit({ type = 'choice_change', value = self.value, element = self, choice = choice })
|
|
|
|
self:draw()
|
|
|
|
return true
|
|
|
|
elseif event.type == 'choice_prev' then
|
|
|
|
local _,k = Util.find(self.choices, 'value', self.value)
|
|
|
|
local choice
|
|
|
|
if k and k > 1 then
|
|
|
|
choice = self.choices[k-1]
|
|
|
|
else
|
|
|
|
choice = self.choices[#self.choices]
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
2019-11-18 21:32:10 +00:00
|
|
|
self.value = choice.value
|
|
|
|
self:emit({ type = 'choice_change', value = self.value, element = self, choice = choice })
|
|
|
|
self:draw()
|
|
|
|
return true
|
2019-02-06 04:03:57 +00:00
|
|
|
elseif event.type == 'mouse_click' or event.type == 'mouse_doubleclick' then
|
|
|
|
if event.x == 1 then
|
2019-11-18 21:32:10 +00:00
|
|
|
self:emit({ type = 'choice_prev' })
|
2019-02-06 04:03:57 +00:00
|
|
|
return true
|
|
|
|
elseif event.x == self.width then
|
2019-11-18 21:32:10 +00:00
|
|
|
self:emit({ type = 'choice_next' })
|
2019-02-06 04:03:57 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-11-14 04:50:00 +00:00
|
|
|
|
|
|
|
function UI.Chooser.example()
|
|
|
|
return UI.Window {
|
|
|
|
a = UI.Chooser {
|
|
|
|
x = 2, y = 2,
|
|
|
|
choices = {
|
|
|
|
{ name = 'choice1', value = 'value1' },
|
|
|
|
{ name = 'choice2', value = 'value2' },
|
|
|
|
{ name = 'choice3', value = 'value3' },
|
|
|
|
},
|
|
|
|
value = 'value2',
|
|
|
|
},
|
|
|
|
b = UI.Chooser {
|
|
|
|
x = 2, y = 4,
|
|
|
|
choices = {
|
|
|
|
{ name = 'choice1', value = 'value1' },
|
|
|
|
{ name = 'choice2', value = 'value2' },
|
|
|
|
{ name = 'choice3', value = 'value3' },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|