1
0
mirror of https://github.com/kepler155c/opus synced 2024-11-15 05:04:50 +00:00
opus/sys/apis/ui.lua

3499 lines
77 KiB
Lua
Raw Normal View History

local Canvas = require('ui.canvas')
local class = require('class')
local Event = require('event')
2017-10-15 23:55:05 +00:00
local Input = require('input')
2018-01-06 11:07:49 +00:00
local Peripheral = require('peripheral')
2018-11-27 01:53:28 +00:00
local Sound = require('sound')
local Transition = require('ui.transition')
local Util = require('util')
2016-12-11 19:24:52 +00:00
2017-10-11 15:37:52 +00:00
local _rep = string.rep
local _sub = string.sub
local colors = _G.colors
local device = _G.device
local fs = _G.fs
local os = _G.os
local term = _G.term
2018-01-22 03:08:30 +00:00
local window = _G.window
2016-12-11 19:24:52 +00:00
2017-10-09 04:26:19 +00:00
--[[
2018-01-24 22:39:38 +00:00
Using the shorthand window definition, elements are created from
the bottom up. Once reaching the top, setParent is called top down.
2017-10-09 04:26:19 +00:00
2018-01-24 22:39:38 +00:00
On :init(), elements do not know the parent or can calculate sizing.
2017-10-09 04:26:19 +00:00
]]
2016-12-11 19:24:52 +00:00
local function safeValue(v)
2018-01-24 22:39:38 +00:00
local t = type(v)
if t == 'string' or t == 'number' then
return v
end
return tostring(v)
2016-12-11 19:24:52 +00:00
end
-- need to add offsets to this test
local function getPosition(element)
2018-01-24 22:39:38 +00:00
local x, y = 1, 1
repeat
x = element.x + x - 1
y = element.y + y - 1
element = element.parent
until not element
return x, y
2016-12-11 19:24:52 +00:00
end
--[[-- Top Level Manager --]]--
local Manager = class()
function Manager:init()
2018-01-24 22:39:38 +00:00
local function keyFunction(event, code, held)
local ie = Input:translate(event, code, held)
if ie and self.currentPage then
local target = self.currentPage.focused or self.currentPage
self:inputEvent(target,
{ type = 'key', key = ie.code == 'char' and ie.ch or ie.code, element = target })
self.currentPage:sync()
end
end
local handlers = {
char = keyFunction,
key_up = keyFunction,
key = keyFunction,
term_resize = function(_, side)
if self.currentPage then
-- the parent doesn't have any children set...
-- that's why we have to resize both the parent and the current page
-- kinda makes sense
if self.currentPage.parent.device.side == side then
self.currentPage.parent:resize()
self.currentPage:resize()
self.currentPage:draw()
self.currentPage:sync()
end
end
end,
mouse_scroll = function(_, direction, x, y)
if self.currentPage then
local event = self.currentPage:pointToChild(x, y)
local directions = {
[ -1 ] = 'up',
[ 1 ] = 'down'
}
-- revisit - should send out scroll_up and scroll_down events
-- let the element convert them to up / down
self:inputEvent(event.element,
{ type = 'key', key = directions[direction] })
self.currentPage:sync()
end
end,
-- this should be moved to the device !
monitor_touch = function(_, side, x, y)
Input:translate('mouse_click', 1, x, y)
local ie = Input:translate('mouse_up', 1, x, y)
if self.currentPage then
if self.currentPage.parent.device.side == side then
self:click(ie.code, 1, x, y)
end
end
end,
mouse_click = function(_, button, x, y)
Input:translate('mouse_click', button, x, y)
if self.currentPage then
if not self.currentPage.parent.device.side then
local event = self.currentPage:pointToChild(x, y)
if event.element.focus and not event.element.inactive then
self.currentPage:setFocus(event.element)
self.currentPage:sync()
end
end
end
end,
mouse_up = function(_, button, x, y)
local ie = Input:translate('mouse_up', button, x, y)
if ie.code == 'control-shift-mouse_click' then -- hack
local event = self.currentPage:pointToChild(x, y)
_ENV.multishell.openTab({
path = 'sys/apps/Lua.lua',
args = { event.element },
focused = true })
elseif ie and self.currentPage then
--if not self.currentPage.parent.device.side then
self:click(ie.code, button, x, y)
--end
end
end,
mouse_drag = function(_, button, x, y)
local ie = Input:translate('mouse_drag', button, x, y)
if ie and self.currentPage then
local event = self.currentPage:pointToChild(x, y)
event.type = ie.code
self:inputEvent(event.element, event)
self.currentPage:sync()
end
end,
paste = function(_, text)
Input:translate('paste')
self:emitEvent({ type = 'paste', text = text })
self.currentPage:sync()
end,
}
-- use 1 handler to single thread all events
Event.on({
'char', 'key_up', 'key', 'term_resize',
'mouse_scroll', 'monitor_touch', 'mouse_click',
'mouse_up', 'mouse_drag', 'paste' },
function(event, ...)
handlers[event](event, ...)
end)
2016-12-11 19:24:52 +00:00
end
function Manager:configure(appName, ...)
2018-01-24 22:39:38 +00:00
local options = {
device = { arg = 'd', type = 'string',
desc = 'Device type' },
textScale = { arg = 't', type = 'number',
desc = 'Text scale' },
}
local defaults = Util.loadTable('usr/config/' .. appName) or { }
if not defaults.device then
defaults.device = { }
end
Util.getOptions(options, { ... }, true)
local optionValues = {
name = options.device.value,
textScale = options.textScale.value,
}
Util.merge(defaults.device, optionValues)
if defaults.device.name then
local dev
if defaults.device.name == 'terminal' then
dev = term.current()
else
dev = Peripheral.lookup(defaults.device.name) --- device[defaults.device.name]
end
if not dev then
error('Invalid display device')
end
self:setDefaultDevice(self.Device({
device = dev,
textScale = defaults.device.textScale,
}))
end
if defaults.theme then
for k,v in pairs(defaults.theme) do
if self[k] and self[k].defaults then
Util.merge(self[k].defaults, v)
end
end
end
2016-12-11 19:24:52 +00:00
end
2016-12-15 14:45:27 +00:00
function Manager:disableEffects()
2018-01-24 22:39:38 +00:00
self.defaultDevice.effectsEnabled = false
2016-12-15 14:45:27 +00:00
end
2016-12-11 19:24:52 +00:00
function Manager:loadTheme(filename)
2018-01-24 22:39:38 +00:00
if fs.exists(filename) then
local theme, err = Util.loadTable(filename)
if not theme then
error(err)
end
for k,v in pairs(theme) do
if self[k] and self[k].defaults then
Util.merge(self[k].defaults, v)
end
end
end
2016-12-11 19:24:52 +00:00
end
function Manager:emitEvent(event)
2018-01-24 22:39:38 +00:00
if self.currentPage and self.currentPage.focused then
return self.currentPage.focused:emit(event)
end
2016-12-11 19:24:52 +00:00
end
function Manager:inputEvent(parent, event)
2018-01-24 22:39:38 +00:00
while parent do
if parent.accelerators then
local acc = parent.accelerators[event.key]
if acc then
if parent:emit({ type = acc, element = parent }) then
return true
end
end
end
if parent.eventHandler then
if parent:eventHandler(event) then
return true
end
end
parent = parent.parent
end
2016-12-11 19:24:52 +00:00
end
2017-10-15 23:55:05 +00:00
function Manager:click(code, button, x, y)
2018-01-24 22:39:38 +00:00
if self.currentPage then
local target = self.currentPage
-- need to add offsets into this check
--[[
if x < target.x or y < target.y or
x > target.x + target.width - 1 or
y > target.y + target.height - 1 then
target:emit({ type = 'mouse_out' })
target = self.currentPage
end
--]]
local clickEvent = target:pointToChild(x, y)
if code == 'mouse_doubleclick' then
if self.doubleClickElement ~= clickEvent.element then
return
end
else
self.doubleClickElement = clickEvent.element
end
clickEvent.button = button
clickEvent.type = code
clickEvent.key = code
if clickEvent.element.focus then
self.currentPage:setFocus(clickEvent.element)
end
if not self:inputEvent(clickEvent.element, clickEvent) then
--[[
if button == 3 then
-- if the double-click was not captured
-- send through a single-click
clickEvent.button = 1
clickEvent.type = events[1]
clickEvent.key = events[1]
self:inputEvent(clickEvent.element, clickEvent)
end
]]
end
self.currentPage:sync()
end
2016-12-11 19:24:52 +00:00
end
2017-10-11 15:37:52 +00:00
function Manager:setDefaultDevice(dev)
2018-01-24 22:39:38 +00:00
self.defaultDevice = dev
self.term = dev
2016-12-11 19:24:52 +00:00
end
function Manager:addPage(name, page)
2018-10-21 22:48:08 +00:00
if not self.pages then
self.pages = { }
end
2018-01-24 22:39:38 +00:00
self.pages[name] = page
2016-12-11 19:24:52 +00:00
end
function Manager:setPages(pages)
2018-01-24 22:39:38 +00:00
self.pages = pages
2016-12-11 19:24:52 +00:00
end
function Manager:getPage(pageName)
2018-01-24 22:39:38 +00:00
local page = self.pages[pageName]
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if not page then
error('UI:getPage: Invalid page: ' .. tostring(pageName), 2)
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
return page
2016-12-11 19:24:52 +00:00
end
function Manager:setPage(pageOrName, ...)
2018-01-24 22:39:38 +00:00
local page = pageOrName
if type(pageOrName) == 'string' then
page = self.pages[pageOrName] or error('Invalid page: ' .. pageOrName)
end
if page == self.currentPage then
page:draw()
else
local needSync
if self.currentPage then
if self.currentPage.focused then
self.currentPage.focused.focused = false
self.currentPage.focused:focus()
end
self.currentPage:disable()
page.previousPage = self.currentPage
else
needSync = true
end
self.currentPage = page
self.currentPage:clear(page.backgroundColor)
page:enable(...)
page:draw()
if self.currentPage.focused then
self.currentPage.focused.focused = true
self.currentPage.focused:focus()
end
if needSync then
page:sync() -- first time a page has been set
end
end
2016-12-11 19:24:52 +00:00
end
function Manager:getCurrentPage()
2018-01-24 22:39:38 +00:00
return self.currentPage
2016-12-11 19:24:52 +00:00
end
function Manager:setPreviousPage()
2018-01-24 22:39:38 +00:00
if self.currentPage.previousPage then
local previousPage = self.currentPage.previousPage.previousPage
self:setPage(self.currentPage.previousPage)
self.currentPage.previousPage = previousPage
end
2016-12-11 19:24:52 +00:00
end
function Manager:getDefaults(element, args)
2018-01-24 22:39:38 +00:00
local defaults = Util.deepCopy(element.defaults)
if args then
Manager:mergeProperties(defaults, args)
end
return defaults
2016-12-11 19:24:52 +00:00
end
2017-10-12 21:58:35 +00:00
function Manager:mergeProperties(obj, args)
2018-01-24 22:39:38 +00:00
if args then
for k,v in pairs(args) do
if k == 'accelerators' then
if obj.accelerators then
Util.merge(obj.accelerators, args.accelerators)
else
obj[k] = v
end
else
obj[k] = v
end
end
end
2016-12-11 19:24:52 +00:00
end
2017-10-12 21:58:35 +00:00
function Manager:pullEvents(...)
2018-01-24 22:39:38 +00:00
Event.pullEvents(...)
self.term:reset()
2017-10-12 21:58:35 +00:00
end
function Manager:exitPullEvents()
2018-01-24 22:39:38 +00:00
Event.exitPullEvents()
2017-10-12 21:58:35 +00:00
end
2016-12-11 19:24:52 +00:00
local UI = Manager()
--[[-- Basic drawable area --]]--
UI.Window = class()
2017-10-11 15:37:52 +00:00
UI.Window.uid = 1
2016-12-11 19:24:52 +00:00
UI.Window.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Window',
x = 1,
y = 1,
-- z = 0, -- eventually...
offx = 0,
offy = 0,
cursorX = 1,
cursorY = 1,
2016-12-11 19:24:52 +00:00
}
function UI.Window:init(args)
2018-01-24 22:39:38 +00:00
-- merge defaults for all subclasses
local defaults = args
local m = getmetatable(self) -- get the class for this instance
repeat
defaults = UI:getDefaults(m, defaults)
m = m._base
until not m
UI:mergeProperties(self, defaults)
-- each element has a unique ID
self.uid = UI.Window.uid
UI.Window.uid = UI.Window.uid + 1
-- at this time, the object has all the properties set
-- postInit is a special constructor. the element does not need to implement
-- the method. But we need to guarantee that each subclass which has this
-- method is called.
m = self
local lpi
repeat
if m.postInit and m.postInit ~= lpi then
m.postInit(self)
lpi = m.postInit
end
m = m._base
until not m
2017-10-11 15:37:52 +00:00
end
function UI.Window:postInit()
2018-01-24 22:39:38 +00:00
if self.parent then
-- this will cascade down the whole tree of elements starting at the
-- top level window (which has a device as a parent)
self:setParent()
end
2016-12-11 19:24:52 +00:00
end
function UI.Window:initChildren()
2018-01-24 22:39:38 +00:00
local children = self.children
-- insert any UI elements created using the shorthand
-- window definition into the children array
for k,child in pairs(self) do
if k ~= 'parent' then -- reserved
if type(child) == 'table' and child.UIElement and not child.parent then
if not children then
children = { }
end
table.insert(children, child)
end
end
end
if children then
for _,child in pairs(children) do
if not child.parent then
child.parent = self
child:setParent()
-- child:reposition() -- maybe
if self.enabled then
child:enable()
end
end
end
self.children = children
end
2016-12-11 19:24:52 +00:00
end
2017-09-30 02:30:01 +00:00
local function setSize(self)
2018-01-24 22:39:38 +00:00
if self.x < 0 then
self.x = self.parent.width + self.x + 1
end
if self.y < 0 then
self.y = self.parent.height + self.y + 1
end
if self.ex then
local ex = self.ex
if self.ex <= 1 then
ex = self.parent.width + self.ex + 1
end
if self.width then
self.x = ex - self.width + 1
else
self.width = ex - self.x + 1
end
end
if self.ey then
local ey = self.ey
if self.ey <= 1 then
ey = self.parent.height + self.ey + 1
end
if self.height then
self.y = ey - self.height + 1
else
self.height = ey - self.y + 1
end
end
if not self.width then
self.width = self.parent.width - self.x + 1
end
if not self.height then
self.height = self.parent.height - self.y + 1
end
2017-09-30 02:30:01 +00:00
end
-- bad name... should be called something like postInit
-- normally used to determine sizes since the parent is
-- only known at this point
function UI.Window:setParent()
2018-01-24 22:39:38 +00:00
self.oh, self.ow = self.height, self.width
self.ox, self.oy = self.x, self.y
2017-09-30 02:30:01 +00:00
2018-01-24 22:39:38 +00:00
setSize(self)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
self:initChildren()
2016-12-11 19:24:52 +00:00
end
2017-10-01 00:35:36 +00:00
function UI.Window:resize()
2018-01-24 22:39:38 +00:00
self.height, self.width = self.oh, self.ow
self.x, self.y = self.ox, self.oy
2017-10-01 00:35:36 +00:00
2018-01-24 22:39:38 +00:00
setSize(self)
2017-10-01 00:35:36 +00:00
2018-01-24 22:39:38 +00:00
if self.children then
for _,child in ipairs(self.children) do
child:resize()
end
end
2017-10-01 00:35:36 +00:00
end
2016-12-11 19:24:52 +00:00
function UI.Window:add(children)
2018-01-24 22:39:38 +00:00
UI:mergeProperties(self, children)
self:initChildren()
2016-12-11 19:24:52 +00:00
end
function UI.Window:getCursorPos()
2018-01-24 22:39:38 +00:00
return self.cursorX, self.cursorY
2016-12-11 19:24:52 +00:00
end
function UI.Window:setCursorPos(x, y)
2018-01-24 22:39:38 +00:00
self.cursorX = x
self.cursorY = y
self.parent:setCursorPos(self.x + x - 1, self.y + y - 1)
2016-12-11 19:24:52 +00:00
end
function UI.Window:setCursorBlink(blink)
2018-01-24 22:39:38 +00:00
self.parent:setCursorBlink(blink)
2016-12-11 19:24:52 +00:00
end
function UI.Window:draw()
2018-01-24 22:39:38 +00:00
self:clear(self.backgroundColor)
if self.children then
for _,child in pairs(self.children) do
if child.enabled then
child:draw()
end
end
end
2016-12-11 19:24:52 +00:00
end
function UI.Window:sync()
2018-01-24 22:39:38 +00:00
if self.parent then
self.parent:sync()
end
2016-12-11 19:24:52 +00:00
end
function UI.Window:enable()
2018-01-24 22:39:38 +00:00
self.enabled = true
if self.children then
for _,child in pairs(self.children) do
child:enable()
end
end
2016-12-11 19:24:52 +00:00
end
function UI.Window:disable()
2018-01-24 22:39:38 +00:00
self.enabled = false
if self.children then
for _,child in pairs(self.children) do
child:disable()
end
end
2016-12-11 19:24:52 +00:00
end
function UI.Window:setTextScale(textScale)
2018-01-24 22:39:38 +00:00
self.textScale = textScale
self.parent:setTextScale(textScale)
2016-12-11 19:24:52 +00:00
end
function UI.Window:clear(bg, fg)
2018-01-24 22:39:38 +00:00
if self.canvas then
self.canvas:clear(bg or self.backgroundColor, fg or self.textColor)
else
self:clearArea(1 + self.offx, 1 + self.offy, self.width, self.height, bg)
end
2016-12-11 19:24:52 +00:00
end
function UI.Window:clearLine(y, bg)
2018-01-24 22:39:38 +00:00
self:write(1, y, _rep(' ', self.width), bg)
2016-12-11 19:24:52 +00:00
end
function UI.Window:clearArea(x, y, width, height, bg)
2018-01-24 22:39:38 +00:00
if width > 0 then
local filler = _rep(' ', width)
for i = 0, height - 1 do
self:write(x, y + i, filler, bg)
end
end
2016-12-11 19:24:52 +00:00
end
function UI.Window:write(x, y, text, bg, tc)
2018-01-24 22:39:38 +00:00
bg = bg or self.backgroundColor
tc = tc or self.textColor
x = x - self.offx
y = y - self.offy
if y <= self.height and y > 0 then
if self.canvas then
self.canvas:write(x, y, text, bg, tc)
else
self.parent:write(
self.x + x - 1, self.y + y - 1, tostring(text), bg, tc)
end
end
2016-12-11 19:24:52 +00:00
end
function UI.Window:centeredWrite(y, text, bg, fg)
2018-01-24 22:39:38 +00:00
if #text >= self.width then
self:write(1, y, text, bg, fg)
else
local space = math.floor((self.width-#text) / 2)
local filler = _rep(' ', space + 1)
local str = _sub(filler, 1, space) .. text
str = str .. _sub(filler, self.width - #str + 1)
self:write(1, y, str, bg, fg)
end
2016-12-11 19:24:52 +00:00
end
2017-10-08 03:03:18 +00:00
function UI.Window:print(text, bg, fg)
2018-01-24 22:39:38 +00:00
local marginLeft = self.marginLeft or 0
local marginRight = self.marginRight or 0
local width = self.width - marginLeft - marginRight
local function nextWord(line, cx)
local result = { line:find("(%w+)", cx) }
if #result > 1 and result[2] > cx then
return _sub(line, cx, result[2] + 1)
elseif #result > 0 and result[1] == cx then
result = { line:find("(%w+)", result[2]) }
if #result > 0 then
return _sub(line, cx, result[1] + 1)
end
end
if cx <= #line then
return _sub(line, cx, #line)
end
end
local function pieces(f, bg, fg)
local pos = 1
local t = { }
while true do
local s = string.find(f, '\027', pos, true)
if not s then
break
end
if pos < s then
table.insert(t, _sub(f, pos, s - 1))
end
local seq = _sub(f, s)
seq = seq:match("\027%[([%d;]+)m")
local e = { }
for color in string.gmatch(seq, "%d+") do
color = tonumber(color)
if color == 0 then
e.fg = fg
e.bg = bg
elseif color > 20 then
e.bg = 2 ^ (color - 21)
else
e.fg = 2 ^ (color - 1)
end
end
table.insert(t, e)
pos = s + #seq + 3
end
if pos <= #f then
table.insert(t, _sub(f, pos))
end
return t
end
local lines = Util.split(text)
for k,line in pairs(lines) do
local fragments = pieces(line, bg, fg)
for _, fragment in ipairs(fragments) do
local lx = 1
if type(fragment) == 'table' then -- ansi sequence
fg = fragment.fg
bg = fragment.bg
else
while true do
local word = nextWord(fragment, lx)
if not word then
break
end
local w = word
if self.cursorX + #word > width then
self.cursorX = marginLeft + 1
self.cursorY = self.cursorY + 1
w = word:gsub('^ ', '')
end
self:write(self.cursorX, self.cursorY, w, bg, fg)
self.cursorX = self.cursorX + #w
lx = lx + #word
end
end
end
if lines[k + 1] then
self.cursorX = marginLeft + 1
self.cursorY = self.cursorY + 1
end
end
return self.cursorX, self.cursorY
2016-12-11 19:24:52 +00:00
end
function UI.Window:setFocus(focus)
2018-01-24 22:39:38 +00:00
if self.parent then
self.parent:setFocus(focus)
end
2016-12-11 19:24:52 +00:00
end
2017-12-13 06:37:31 +00:00
function UI.Window:capture(child)
2018-01-24 22:39:38 +00:00
if self.parent then
self.parent:capture(child)
end
2017-12-13 06:37:31 +00:00
end
function UI.Window:release(child)
2018-01-24 22:39:38 +00:00
if self.parent then
self.parent:release(child)
end
2017-12-13 06:37:31 +00:00
end
function UI.Window:pointToChild(x, y)
2018-01-24 22:39:38 +00:00
x = x + self.offx - self.x + 1
y = y + self.offy - self.y + 1
if self.children then
for _,child in pairs(self.children) do
if child.enabled and not child.inactive and
x >= child.x and x < child.x + child.width and
y >= child.y and y < child.y + child.height then
local c = child:pointToChild(x, y)
if c then
return c
end
end
end
end
return {
element = self,
x = x,
y = y
}
2017-12-13 06:37:31 +00:00
end
2016-12-11 19:24:52 +00:00
function UI.Window:getFocusables()
2018-01-24 22:39:38 +00:00
local focusable = { }
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
local function focusSort(a, b)
if a.y == b.y then
return a.x < b.x
end
return a.y < b.y
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
local function getFocusable(parent, x, y)
for _,child in Util.spairs(parent.children, focusSort) do
if child.enabled and child.focus and not child.inactive then
table.insert(focusable, child)
end
if child.children then
getFocusable(child, child.x + x, child.y + y)
end
end
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if self.children then
getFocusable(self, self.x, self.y)
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
return focusable
2016-12-11 19:24:52 +00:00
end
function UI.Window:focusFirst()
2018-01-24 22:39:38 +00:00
local focusables = self:getFocusables()
local focused = focusables[1]
if focused then
self:setFocus(focused)
end
2016-12-11 19:24:52 +00:00
end
2017-10-18 23:51:55 +00:00
function UI.Window:refocus()
2018-01-24 22:39:38 +00:00
local el = self
while el do
local focusables = el:getFocusables()
if focusables[1] then
self:setFocus(focusables[1])
break
end
el = el.parent
end
2017-10-18 23:51:55 +00:00
end
2016-12-11 19:24:52 +00:00
function UI.Window:scrollIntoView()
2018-01-24 22:39:38 +00:00
local parent = self.parent
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if self.x <= parent.offx then
parent.offx = math.max(0, self.x - 1)
parent:draw()
elseif self.x + self.width > parent.width + parent.offx then
parent.offx = self.x + self.width - parent.width - 1
parent:draw()
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if self.y <= parent.offy then
parent.offy = math.max(0, self.y - 1)
parent:draw()
elseif self.y + self.height > parent.height + parent.offy then
parent.offy = self.y + self.height - parent.height - 1
parent:draw()
end
2016-12-11 19:24:52 +00:00
end
2017-10-06 17:39:47 +00:00
function UI.Window:getCanvas()
2018-01-24 22:39:38 +00:00
local el = self
repeat
if el.canvas then
return el.canvas
end
el = el.parent
until not el
2017-10-06 17:39:47 +00:00
end
function UI.Window:addLayer(bg, fg)
2018-01-24 22:39:38 +00:00
local canvas = self:getCanvas()
canvas = canvas:addLayer(self, bg, fg)
canvas:clear(bg or self.backgroundColor, fg or self.textColor)
return canvas
2017-10-06 17:39:47 +00:00
end
2017-10-03 04:50:54 +00:00
function UI.Window:addTransition(effect, args)
2018-01-24 22:39:38 +00:00
if self.parent then
args = args or { }
if not args.x then -- not good
args.x, args.y = getPosition(self)
args.width = self.width
args.height = self.height
end
2017-10-03 04:50:54 +00:00
2018-01-24 22:39:38 +00:00
args.canvas = args.canvas or self.canvas
self.parent:addTransition(effect, args)
end
2016-12-14 17:36:28 +00:00
end
2016-12-11 19:24:52 +00:00
function UI.Window:emit(event)
2018-01-24 22:39:38 +00:00
local parent = self
while parent do
if parent.eventHandler then
if parent:eventHandler(event) then
return true
end
end
parent = parent.parent
end
2016-12-11 19:24:52 +00:00
end
2017-10-18 23:51:55 +00:00
function UI.Window:find(uid)
2018-01-24 22:39:38 +00:00
if self.children then
return Util.find(self.children, 'uid', uid)
end
2017-10-18 23:51:55 +00:00
end
2016-12-11 19:24:52 +00:00
function UI.Window:eventHandler(event)
2018-01-24 22:39:38 +00:00
return false
2016-12-11 19:24:52 +00:00
end
--[[-- Terminal for computer / advanced computer / monitor --]]--
UI.Device = class(UI.Window)
UI.Device.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Device',
backgroundColor = colors.black,
textColor = colors.white,
textScale = 1,
effectsEnabled = true,
2016-12-11 19:24:52 +00:00
}
2017-10-11 15:37:52 +00:00
function UI.Device:postInit()
2018-01-24 22:39:38 +00:00
self.device = self.device or term.current()
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if self.deviceType then
self.device = device[self.deviceType]
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if not self.device.setTextScale then
self.device.setTextScale = function() end
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
self.device.setTextScale(self.textScale)
self.width, self.height = self.device.getSize()
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
self.isColor = self.device.isColor()
2016-12-23 04:22:04 +00:00
2018-01-24 22:39:38 +00:00
self.canvas = Canvas({
x = 1, y = 1, width = self.width, height = self.height,
isColor = self.isColor,
})
self.canvas:clear(self.backgroundColor, self.textColor)
2016-12-11 19:24:52 +00:00
end
function UI.Device:resize()
2018-12-05 01:55:08 +00:00
self.device.setTextScale(self.textScale)
2018-01-24 22:39:38 +00:00
self.width, self.height = self.device.getSize()
self.lines = { }
self.canvas:resize(self.width, self.height)
self.canvas:clear(self.backgroundColor, self.textColor)
2016-12-11 19:24:52 +00:00
end
function UI.Device:setCursorPos(x, y)
2018-01-24 22:39:38 +00:00
self.cursorX = x
self.cursorY = y
2016-12-11 19:24:52 +00:00
end
function UI.Device:getCursorBlink()
2018-01-24 22:39:38 +00:00
return self.cursorBlink
2016-12-11 19:24:52 +00:00
end
function UI.Device:setCursorBlink(blink)
2018-01-24 22:39:38 +00:00
self.cursorBlink = blink
self.device.setCursorBlink(blink)
2016-12-11 19:24:52 +00:00
end
function UI.Device:setTextScale(textScale)
2018-01-24 22:39:38 +00:00
self.textScale = textScale
self.device.setTextScale(self.textScale)
2016-12-11 19:24:52 +00:00
end
function UI.Device:reset()
2018-01-24 22:39:38 +00:00
self.device.setBackgroundColor(colors.black)
self.device.setTextColor(colors.white)
self.device.clear()
self.device.setCursorPos(1, 1)
2016-12-11 19:24:52 +00:00
end
2017-10-03 04:50:54 +00:00
function UI.Device:addTransition(effect, args)
2018-01-24 22:39:38 +00:00
if not self.transitions then
self.transitions = { }
end
2016-12-18 19:38:48 +00:00
2018-01-24 22:39:38 +00:00
args = args or { }
args.ex = args.x + args.width - 1
args.ey = args.y + args.height - 1
args.canvas = args.canvas or self.canvas
2017-10-03 04:50:54 +00:00
2018-01-24 22:39:38 +00:00
if type(effect) == 'string' then
effect = Transition[effect]
if not effect then
error('Invalid transition')
end
end
2017-10-03 04:50:54 +00:00
2018-01-24 22:39:38 +00:00
table.insert(self.transitions, { update = effect(args), args = args })
2016-12-14 17:36:28 +00:00
end
2016-12-23 04:22:04 +00:00
function UI.Device:runTransitions(transitions, canvas)
2018-01-24 22:39:38 +00:00
for _,t in ipairs(transitions) do
canvas:punch(t.args) -- punch out the effect areas
end
canvas:blitClipped(self.device) -- and blit the remainder
canvas:reset()
while true do
for _,k in ipairs(Util.keys(transitions)) do
local transition = transitions[k]
if not transition.update(self.device) then
transitions[k] = nil
end
end
if Util.empty(transitions) then
break
end
os.sleep(0)
end
2016-12-14 17:36:28 +00:00
end
2016-12-11 19:24:52 +00:00
function UI.Device:sync()
2018-01-24 22:39:38 +00:00
local transitions
if self.transitions and self.effectsEnabled then
transitions = self.transitions
self.transitions = nil
end
2016-12-14 17:36:28 +00:00
2018-01-24 22:39:38 +00:00
if self:getCursorBlink() then
self.device.setCursorBlink(false)
end
2016-12-14 17:36:28 +00:00
2018-01-24 22:39:38 +00:00
if transitions then
self:runTransitions(transitions, self.canvas)
else
self.canvas:render(self.device)
end
2016-12-14 17:36:28 +00:00
2018-01-24 22:39:38 +00:00
if self:getCursorBlink() then
self.device.setCursorPos(self.cursorX, self.cursorY)
self.device.setCursorBlink(true)
end
2016-12-11 19:24:52 +00:00
end
--[[-- StringBuffer --]]--
2017-10-01 00:35:36 +00:00
-- justs optimizes string concatenations
2016-12-11 19:24:52 +00:00
UI.StringBuffer = class()
function UI.StringBuffer:init(bufSize)
2018-01-24 22:39:38 +00:00
self.bufSize = bufSize
self.buffer = {}
2016-12-11 19:24:52 +00:00
end
function UI.StringBuffer:insert(s, width)
2018-01-24 22:39:38 +00:00
local len = #tostring(s or '')
if len > width then
s = _sub(s, 1, width)
end
table.insert(self.buffer, s)
if len < width then
table.insert(self.buffer, _rep(' ', width - len))
end
2016-12-11 19:24:52 +00:00
end
function UI.StringBuffer:insertRight(s, width)
local len = #tostring(s or '')
if len > width then
s = _sub(s, 1, width)
end
if len < width then
table.insert(self.buffer, _rep(' ', width - len))
end
table.insert(self.buffer, s)
end
2016-12-11 19:24:52 +00:00
function UI.StringBuffer:get(sep)
2018-01-24 22:39:38 +00:00
return Util.widthify(table.concat(self.buffer, sep or ''), self.bufSize)
2016-12-11 19:24:52 +00:00
end
function UI.StringBuffer:clear()
2018-01-24 22:39:38 +00:00
self.buffer = { }
2016-12-11 19:24:52 +00:00
end
2017-10-01 00:35:36 +00:00
-- For manipulating text in a fixed width string
2017-09-28 22:52:57 +00:00
local SB = { }
function SB:new(width)
2018-01-24 22:39:38 +00:00
return setmetatable({
width = width,
buf = _rep(' ', width)
}, { __index = SB })
2017-09-28 22:52:57 +00:00
end
function SB:insert(x, str, width)
2018-01-24 22:39:38 +00:00
if x < 1 then
x = self.width + x + 1
end
width = width or #str
if x + width - 1 > self.width then
width = self.width - x
end
if width > 0 then
self.buf = _sub(self.buf, 1, x - 1) .. _sub(str, 1, width) .. _sub(self.buf, x + width)
end
2017-09-28 22:52:57 +00:00
end
function SB:fill(x, ch, width)
2018-01-24 22:39:38 +00:00
width = width or self.width - x + 1
self:insert(x, _rep(ch, width))
2017-09-28 22:52:57 +00:00
end
function SB:center(str)
2018-01-24 22:39:38 +00:00
self:insert(math.max(1, math.ceil((self.width - #str + 1) / 2)), str)
2017-09-28 22:52:57 +00:00
end
function SB:get()
2018-01-24 22:39:38 +00:00
return self.buf
2017-09-28 22:52:57 +00:00
end
2016-12-11 19:24:52 +00:00
--[[-- Page (focus manager) --]]--
UI.Page = class(UI.Window)
UI.Page.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Page',
accelerators = {
down = 'focus_next',
enter = 'focus_next',
tab = 'focus_next',
['shift-tab' ] = 'focus_prev',
up = 'focus_prev',
},
backgroundColor = colors.cyan,
textColor = colors.white,
2016-12-11 19:24:52 +00:00
}
2017-10-11 15:37:52 +00:00
function UI.Page:postInit()
2018-01-24 22:39:38 +00:00
self.parent = self.parent or UI.defaultDevice
self.__target = self
2017-10-11 15:37:52 +00:00
end
2016-12-23 04:22:04 +00:00
2017-10-11 15:37:52 +00:00
function UI.Page:setParent()
2018-01-24 22:39:38 +00:00
UI.Window.setParent(self)
if self.z then
self.canvas = self:addLayer(self.backgroundColor, self.textColor)
self.canvas:clear(self.backgroundColor, self.textColor)
else
self.canvas = self.parent.canvas
end
2016-12-11 19:24:52 +00:00
end
function UI.Page:enable()
2018-01-24 22:39:38 +00:00
self.canvas.visible = true
UI.Window.enable(self)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if not self.focused or not self.focused.enabled then
self:focusFirst()
end
2016-12-11 19:24:52 +00:00
end
2016-12-23 04:22:04 +00:00
function UI.Page:disable()
2018-01-24 22:39:38 +00:00
if self.z then
self.canvas.visible = false
end
2016-12-23 04:22:04 +00:00
end
2017-12-13 06:37:31 +00:00
function UI.Page:capture(child)
2018-01-24 22:39:38 +00:00
self.__target = child
2017-12-13 06:37:31 +00:00
end
function UI.Page:release(child)
2018-01-24 22:39:38 +00:00
if self.__target == child then
self.__target = self
end
2017-12-13 06:37:31 +00:00
end
function UI.Page:pointToChild(x, y)
2018-01-24 22:39:38 +00:00
if self.__target == self then
return UI.Window.pointToChild(self, x, y)
end
x = x + self.offx - self.x + 1
y = y + self.offy - self.y + 1
return self.__target:pointToChild(x, y)
2017-12-13 06:37:31 +00:00
end
function UI.Page:getFocusables()
2018-01-24 22:39:38 +00:00
if self.__target == self or self.__target.pageType ~= 'modal' then
return UI.Window.getFocusables(self)
end
return self.__target:getFocusables()
2017-12-13 06:37:31 +00:00
end
2016-12-11 19:24:52 +00:00
function UI.Page:getFocused()
2018-01-24 22:39:38 +00:00
return self.focused
2016-12-11 19:24:52 +00:00
end
function UI.Page:focusPrevious()
2018-01-24 22:39:38 +00:00
local function getPreviousFocus(focused)
local focusables = self:getFocusables()
local k = Util.contains(focusables, focused)
if k then
if k > 1 then
return focusables[k - 1]
end
return focusables[#focusables]
end
end
local focused = getPreviousFocus(self.focused)
if focused then
self:setFocus(focused)
end
2016-12-11 19:24:52 +00:00
end
function UI.Page:focusNext()
2018-01-24 22:39:38 +00:00
local function getNextFocus(focused)
local focusables = self:getFocusables()
local k = Util.contains(focusables, focused)
if k then
if k < #focusables then
return focusables[k + 1]
end
return focusables[1]
end
end
local focused = getNextFocus(self.focused)
if focused then
self:setFocus(focused)
end
2016-12-11 19:24:52 +00:00
end
function UI.Page:setFocus(child)
2018-10-28 01:34:34 +00:00
if not child or not child.focus then
2018-01-24 22:39:38 +00:00
return
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if self.focused and self.focused ~= child then
self.focused.focused = false
self.focused:focus()
self.focused:emit({ type = 'focus_lost', focused = child })
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
self.focused = child
if not child.focused then
child.focused = true
2018-10-24 02:33:40 +00:00
child:emit({ type = 'focus_change', focused = child })
--self:emit({ type = 'focus_change', focused = child })
2018-01-24 22:39:38 +00:00
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
child:focus()
2016-12-11 19:24:52 +00:00
end
function UI.Page:eventHandler(event)
2018-01-24 22:39:38 +00:00
if self.focused then
if event.type == 'focus_next' then
self:focusNext()
return true
elseif event.type == 'focus_prev' then
self:focusPrevious()
return true
end
end
2016-12-11 19:24:52 +00:00
end
--[[-- Grid --]]--
UI.Grid = class(UI.Window)
UI.Grid.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Grid',
index = 1,
inverseSort = false,
disableHeader = false,
marginRight = 0,
textColor = colors.white,
textSelectedColor = colors.white,
backgroundColor = colors.black,
backgroundSelectedColor = colors.gray,
headerBackgroundColor = colors.cyan,
headerTextColor = colors.white,
2018-11-30 09:53:59 +00:00
headerSortColor = colors.yellow,
2018-01-24 22:39:38 +00:00
unfocusedTextSelectedColor = colors.white,
unfocusedBackgroundSelectedColor = colors.gray,
focusIndicator = '>',
sortIndicator = ' ',
inverseSortIndicator = '^',
values = { },
columns = { },
accelerators = {
enter = 'key_enter',
[ 'control-c' ] = 'copy',
down = 'scroll_down',
up = 'scroll_up',
home = 'scroll_top',
[ 'end' ] = 'scroll_bottom',
pageUp = 'scroll_pageUp',
[ 'control-b' ] = 'scroll_pageUp',
pageDown = 'scroll_pageDown',
[ 'control-f' ] = 'scroll_pageDown',
},
2016-12-11 19:24:52 +00:00
}
2017-10-11 15:37:52 +00:00
function UI.Grid:setParent()
2018-01-24 22:39:38 +00:00
UI.Window.setParent(self)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
for _,c in pairs(self.columns) do
c.cw = c.width
if not c.heading then
c.heading = ''
end
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
self:update()
2016-12-27 03:26:43 +00:00
2018-01-24 22:39:38 +00:00
if not self.pageSize then
if self.disableHeader then
self.pageSize = self.height
else
self.pageSize = self.height - 1
end
end
2016-12-11 19:24:52 +00:00
end
2017-04-22 06:30:41 +00:00
function UI.Grid:resize()
2018-01-24 22:39:38 +00:00
UI.Window.resize(self)
2017-04-22 06:30:41 +00:00
2018-01-24 22:39:38 +00:00
if self.disableHeader then
self.pageSize = self.height
else
self.pageSize = self.height - 1
end
self:adjustWidth()
2017-04-22 06:30:41 +00:00
end
2016-12-11 19:24:52 +00:00
function UI.Grid:adjustWidth()
2018-01-24 22:39:38 +00:00
local t = { } -- cols without width
local w = self.width - #self.columns - 1 - self.marginRight -- width remaining
for _,c in pairs(self.columns) do
if c.width then
c.cw = c.width
w = w - c.cw
else
table.insert(t, c)
end
end
if #t == 0 then
return
end
if #t == 1 then
t[1].cw = #(t[1].heading or '')
t[1].cw = math.max(t[1].cw, w)
return
end
if not self.autospace then
for k,c in ipairs(t) do
c.cw = math.floor(w / (#t - k + 1))
w = w - c.cw
end
else
for _,c in ipairs(t) do
c.cw = #(c.heading or '')
w = w - c.cw
end
-- adjust the size to the length of the value
for key,row in pairs(self.values) do
if w <= 0 then
break
end
row = self:getDisplayValues(row, key)
for _,col in pairs(t) do
local value = row[col.key]
if value then
value = tostring(value)
if #value > col.cw then
w = w + col.cw
col.cw = math.min(#value, w)
w = w - col.cw
if w <= 0 then
break
end
end
end
end
end
-- last column does not get padding (right alignment)
if not self.columns[#self.columns].width then
Util.removeByValue(t, self.columns[#self.columns])
end
-- got some extra room - add some padding
if w > 0 then
for k,c in ipairs(t) do
local padding = math.floor(w / (#t - k + 1))
c.cw = c.cw + padding
w = w - padding
end
end
end
2016-12-11 19:24:52 +00:00
end
function UI.Grid:setPageSize(pageSize)
2018-01-24 22:39:38 +00:00
self.pageSize = pageSize
2016-12-11 19:24:52 +00:00
end
function UI.Grid:getValues()
2018-01-24 22:39:38 +00:00
return self.values
2016-12-11 19:24:52 +00:00
end
function UI.Grid:setValues(t)
2018-01-24 22:39:38 +00:00
self.values = t
self:update()
2016-12-11 19:24:52 +00:00
end
function UI.Grid:setInverseSort(inverseSort)
2018-01-24 22:39:38 +00:00
self.inverseSort = inverseSort
self:update()
self:setIndex(self.index)
2016-12-11 19:24:52 +00:00
end
function UI.Grid:setSortColumn(column)
2018-01-24 22:39:38 +00:00
self.sortColumn = column
2016-12-11 19:24:52 +00:00
end
function UI.Grid:getDisplayValues(row, key)
2018-01-24 22:39:38 +00:00
return row
2016-12-11 19:24:52 +00:00
end
function UI.Grid:getSelected()
2018-01-24 22:39:38 +00:00
if self.sorted then
return self.values[self.sorted[self.index]], self.sorted[self.index]
end
2016-12-11 19:24:52 +00:00
end
2018-12-01 05:55:42 +00:00
function UI.Grid:setSelected(name, value)
if self.sorted then
for k,v in pairs(self.sorted) do
if self.values[v][name] == value then
self:setIndex(k)
return
end
end
end
self:setIndex(1)
end
2016-12-11 19:24:52 +00:00
function UI.Grid:focus()
2018-01-24 22:39:38 +00:00
self:drawRows()
2016-12-11 19:24:52 +00:00
end
function UI.Grid:draw()
2018-01-24 22:39:38 +00:00
if not self.disableHeader then
self:drawHeadings()
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if self.index <= 0 then
self:setIndex(1)
elseif self.index > #self.sorted then
self:setIndex(#self.sorted)
end
self:drawRows()
2016-12-11 19:24:52 +00:00
end
-- Something about the displayed table has changed
-- resort the table
function UI.Grid:update()
2018-01-24 22:39:38 +00:00
local function sort(a, b)
if not a[self.sortColumn] then
return false
elseif not b[self.sortColumn] then
return true
end
return self:sortCompare(a, b)
end
local function inverseSort(a, b)
return not sort(a, b)
end
local order
if self.sortColumn then
order = sort
if self.inverseSort then
order = inverseSort
end
end
2018-12-01 05:55:42 +00:00
self.sorted = Util.keys(self.values)
2018-01-24 22:39:38 +00:00
if order then
table.sort(self.sorted, function(a,b)
return order(self.values[a], self.values[b])
end)
end
self:adjustWidth()
2016-12-11 19:24:52 +00:00
end
function UI.Grid:drawHeadings()
2018-11-30 09:53:59 +00:00
local x = 1
local sb = UI.StringBuffer(self.width)
2018-01-24 22:39:38 +00:00
for _,col in ipairs(self.columns) do
local ind = ' '
if col.key == self.sortColumn then
if self.inverseSort then
ind = self.inverseSortIndicator
else
ind = self.sortIndicator
end
end
sb:insert(ind .. col.heading, col.cw + 1)
--[[
2018-11-30 09:53:59 +00:00
self:write(x,
1,
Util.widthify(ind .. col.heading, col.cw + 1),
self.headerBackgroundColor,
col.key == self.sortColumn and self.headerSortColor or self.headerTextColor)
x = x + col.cw + 1
]]
2018-01-24 22:39:38 +00:00
end
self:write(1, 1, sb:get(), self.headerBackgroundColor, self.headerTextColor)
2016-12-11 19:24:52 +00:00
end
function UI.Grid:sortCompare(a, b)
2018-01-24 22:39:38 +00:00
a = safeValue(a[self.sortColumn])
b = safeValue(b[self.sortColumn])
if type(a) == type(b) then
return a < b
end
return tostring(a) < tostring(b)
2016-12-11 19:24:52 +00:00
end
function UI.Grid:drawRows()
2018-01-24 22:39:38 +00:00
local y = 1
local startRow = math.max(1, self:getStartRow())
local sb = UI.StringBuffer(self.width)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if not self.disableHeader then
y = y + 1
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
local lastRow = math.min(startRow + self.pageSize - 1, #self.sorted)
for index = startRow, lastRow do
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
local sindex = self.sorted[index]
local rawRow = self.values[sindex]
local key = sindex
local row = self:getDisplayValues(rawRow, key)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
sb:clear()
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
local ind = ' '
if self.focused and index == self.index and not self.inactive then
ind = self.focusIndicator
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
for _,col in pairs(self.columns) do
if col.justify == 'right' then
sb:insertRight(ind .. safeValue(row[col.key] or ''), col.cw + 1)
else
sb:insert(ind .. safeValue(row[col.key] or ''), col.cw + 1)
end
2018-01-24 22:39:38 +00:00
ind = ' '
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
local selected = index == self.index and not self.inactive
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
self:write(1, y, sb:get(),
self:getRowBackgroundColor(rawRow, selected),
self:getRowTextColor(rawRow, selected))
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
y = y + 1
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if y <= self.height then
self:clearArea(1, y, self.width, self.height - y + 1)
end
2016-12-11 19:24:52 +00:00
end
function UI.Grid:getRowTextColor(row, selected)
2018-01-24 22:39:38 +00:00
if selected then
if self.focused then
return self.textSelectedColor
end
return self.unfocusedTextSelectedColor
end
return self.textColor
2016-12-11 19:24:52 +00:00
end
function UI.Grid:getRowBackgroundColor(row, selected)
2018-01-24 22:39:38 +00:00
if selected then
if self.focused then
return self.backgroundSelectedColor
end
return self.unfocusedBackgroundSelectedColor
end
return self.backgroundColor
2016-12-11 19:24:52 +00:00
end
function UI.Grid:getIndex()
2018-01-24 22:39:38 +00:00
return self.index
2016-12-11 19:24:52 +00:00
end
function UI.Grid:setIndex(index)
2018-01-24 22:39:38 +00:00
index = math.max(1, index)
self.index = math.min(index, #self.sorted)
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
local selected = self:getSelected()
if selected ~= self.selected then
self:drawRows()
self.selected = selected
if selected then
self:emit({ type = 'grid_focus_row', selected = selected, element = self })
end
end
2016-12-11 19:24:52 +00:00
end
function UI.Grid:getStartRow()
2018-01-24 22:39:38 +00:00
return math.floor((self.index - 1) / self.pageSize) * self.pageSize + 1
2016-12-11 19:24:52 +00:00
end
function UI.Grid:getPage()
2018-01-24 22:39:38 +00:00
return math.floor(self.index / self.pageSize) + 1
2016-12-11 19:24:52 +00:00
end
function UI.Grid:getPageCount()
2018-01-24 22:39:38 +00:00
local tableSize = Util.size(self.values)
local pc = math.floor(tableSize / self.pageSize)
if tableSize % self.pageSize > 0 then
pc = pc + 1
end
return pc
2016-12-11 19:24:52 +00:00
end
function UI.Grid:nextPage()
2018-01-24 22:39:38 +00:00
self:setPage(self:getPage() + 1)
2016-12-11 19:24:52 +00:00
end
function UI.Grid:previousPage()
2018-01-24 22:39:38 +00:00
self:setPage(self:getPage() - 1)
2016-12-11 19:24:52 +00:00
end
function UI.Grid:setPage(pageNo)
2018-01-24 22:39:38 +00:00
-- 1 based paging
self:setIndex((pageNo-1) * self.pageSize + 1)
2016-12-11 19:24:52 +00:00
end
function UI.Grid:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'mouse_click' or
event.type == 'mouse_rightclick' or
event.type == 'mouse_doubleclick' then
if not self.disableHeader then
if event.y == 1 then
local col = 2
for _,c in ipairs(self.columns) do
if event.x < col + c.cw then
2018-11-30 09:53:59 +00:00
self:emit({
type = 'grid_sort',
sortColumn = c.key,
inverseSort = self.sortColumn == c.key and not self.inverseSort,
element = self,
})
2018-01-24 22:39:38 +00:00
break
end
col = col + c.cw + 1
end
return true
end
end
local row = self:getStartRow() + event.y - 1
if not self.disableHeader then
row = row - 1
end
if row > 0 and row <= Util.size(self.values) then
self:setIndex(row)
if event.type == 'mouse_doubleclick' then
self:emit({ type = 'key_enter' })
elseif event.type == 'mouse_rightclick' then
self:emit({ type = 'grid_select_right', selected = self.selected, element = self })
end
return true
end
return false
2018-11-30 09:53:59 +00:00
elseif event.type == 'grid_sort' then
self.sortColumn = event.sortColumn
self:setInverseSort(event.inverseSort)
self:draw()
2018-01-24 22:39:38 +00:00
elseif event.type == 'scroll_down' then
self:setIndex(self.index + 1)
elseif event.type == 'scroll_up' then
self:setIndex(self.index - 1)
elseif event.type == 'scroll_top' then
self:setIndex(1)
elseif event.type == 'scroll_bottom' then
self:setIndex(Util.size(self.values))
elseif event.type == 'scroll_pageUp' then
self:setIndex(self.index - self.pageSize)
elseif event.type == 'scroll_pageDown' then
self:setIndex(self.index + self.pageSize)
elseif event.type == 'key_enter' then
if self.selected then
self:emit({ type = 'grid_select', selected = self.selected, element = self })
end
elseif event.type == 'copy' then
if self.selected then
os.queueEvent('clipboard_copy', self.selected)
end
else
return false
end
return true
2016-12-11 19:24:52 +00:00
end
--[[-- ScrollingGrid --]]--
UI.ScrollingGrid = class(UI.Grid)
UI.ScrollingGrid.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'ScrollingGrid',
scrollOffset = 0,
marginRight = 1,
2016-12-11 19:24:52 +00:00
}
2017-10-11 15:37:52 +00:00
function UI.ScrollingGrid:postInit()
2018-01-24 22:39:38 +00:00
self.scrollBar = UI.ScrollBar()
2016-12-11 19:24:52 +00:00
end
function UI.ScrollingGrid:drawRows()
2018-01-24 22:39:38 +00:00
UI.Grid.drawRows(self)
self.scrollBar:draw()
2016-12-11 19:24:52 +00:00
end
2017-10-08 03:03:18 +00:00
function UI.ScrollingGrid:getViewArea()
2018-01-24 22:39:38 +00:00
local y = 1
if not self.disableHeader then
y = 2
end
return {
static = true, -- the container doesn't scroll
y = y, -- scrollbar Y
height = self.pageSize, -- viewable height
totalHeight = Util.size(self.values), -- total height
offsetY = self.scrollOffset, -- scroll offset
}
2016-12-11 19:24:52 +00:00
end
function UI.ScrollingGrid:getStartRow()
2018-01-24 22:39:38 +00:00
local ts = Util.size(self.values)
if ts < self.pageSize then
self.scrollOffset = 0
end
return self.scrollOffset + 1
2016-12-11 19:24:52 +00:00
end
function UI.ScrollingGrid:setIndex(index)
2018-01-24 22:39:38 +00:00
if index < self.scrollOffset + 1 then
self.scrollOffset = index - 1
elseif index - self.scrollOffset > self.pageSize then
self.scrollOffset = index - self.pageSize
end
if self.scrollOffset < 0 then
self.scrollOffset = 0
else
local ts = Util.size(self.values)
if self.pageSize + self.scrollOffset + 1 > ts then
self.scrollOffset = math.max(0, ts - self.pageSize)
end
end
UI.Grid.setIndex(self, index)
2016-12-11 19:24:52 +00:00
end
--[[-- Menu --]]--
UI.Menu = class(UI.Grid)
UI.Menu.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Menu',
disableHeader = true,
columns = { { heading = 'Prompt', key = 'prompt', width = 20 } },
2016-12-11 19:24:52 +00:00
}
2017-10-11 15:37:52 +00:00
function UI.Menu:postInit()
2018-01-24 22:39:38 +00:00
self.values = self.menuItems
self.pageSize = #self.menuItems
2016-12-11 19:24:52 +00:00
end
function UI.Menu:setParent()
2018-01-24 22:39:38 +00:00
UI.Grid.setParent(self)
self.itemWidth = 1
for _,v in pairs(self.values) do
if #v.prompt > self.itemWidth then
self.itemWidth = #v.prompt
end
end
self.columns[1].width = self.itemWidth
if self.centered then
self:center()
else
self.width = self.itemWidth + 2
end
2016-12-11 19:24:52 +00:00
end
function UI.Menu:center()
2018-01-24 22:39:38 +00:00
self.x = (self.width - self.itemWidth + 2) / 2
self.width = self.itemWidth + 2
2016-12-11 19:24:52 +00:00
end
function UI.Menu:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'key' then
if event.key == 'enter' then
local selected = self.menuItems[self.index]
self:emit({
type = selected.event or 'menu_select',
selected = selected
})
return true
end
elseif event.type == 'mouse_click' then
if event.y <= #self.menuItems then
UI.Grid.setIndex(self, event.y)
local selected = self.menuItems[self.index]
self:emit({
type = selected.event or 'menu_select',
selected = selected
})
return true
end
end
return UI.Grid.eventHandler(self, event)
2016-12-11 19:24:52 +00:00
end
2017-10-08 03:03:18 +00:00
--[[-- Viewport --]]--
UI.Viewport = class(UI.Window)
UI.Viewport.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Viewport',
backgroundColor = colors.cyan,
accelerators = {
down = 'scroll_down',
up = 'scroll_up',
home = 'scroll_top',
[ 'end' ] = 'scroll_bottom',
pageUp = 'scroll_pageUp',
[ 'control-b' ] = 'scroll_pageUp',
pageDown = 'scroll_pageDown',
[ 'control-f' ] = 'scroll_pageDown',
},
2016-12-11 19:24:52 +00:00
}
2017-10-08 03:03:18 +00:00
function UI.Viewport:setScrollPosition(offset)
2018-01-24 22:39:38 +00:00
local oldOffset = self.offy
self.offy = math.max(offset, 0)
local max = self.ymax or self.height
if self.children then
for _, child in ipairs(self.children) do
if child ~= self.scrollBar then -- hack !
max = math.max(child.y + child.height - 1, max)
end
end
end
self.offy = math.min(self.offy, math.max(max, self.height) - self.height)
if self.offy ~= oldOffset then
self:draw()
end
2016-12-11 19:24:52 +00:00
end
2017-10-08 03:03:18 +00:00
function UI.Viewport:reset()
2018-01-24 22:39:38 +00:00
self.offy = 0
2016-12-14 17:36:28 +00:00
end
2017-10-08 03:03:18 +00:00
function UI.Viewport:getViewArea()
2018-01-24 22:39:38 +00:00
return {
y = (self.offy or 0) + 1,
height = self.height,
totalHeight = self.ymax,
offsetY = self.offy or 0,
}
2017-10-08 03:03:18 +00:00
end
function UI.Viewport:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'scroll_down' then
self:setScrollPosition(self.offy + 1)
elseif event.type == 'scroll_up' then
self:setScrollPosition(self.offy - 1)
elseif event.type == 'scroll_top' then
self:setScrollPosition(0)
elseif event.type == 'scroll_bottom' then
self:setScrollPosition(10000000)
elseif event.type == 'scroll_pageUp' then
self:setScrollPosition(self.offy - self.height)
elseif event.type == 'scroll_pageDown' then
self:setScrollPosition(self.offy + self.height)
else
return false
end
return true
2016-12-11 19:24:52 +00:00
end
2016-12-11 19:24:52 +00:00
--[[-- TitleBar --]]--
UI.TitleBar = class(UI.Window)
UI.TitleBar.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'TitleBar',
height = 1,
textColor = colors.white,
backgroundColor = colors.cyan,
title = '',
frameChar = '-',
closeInd = '*',
2016-12-11 19:24:52 +00:00
}
function UI.TitleBar:draw()
2018-01-24 22:39:38 +00:00
local sb = SB:new(self.width)
sb:fill(2, self.frameChar, sb.width - 3)
sb:center(string.format(' %s ', self.title))
if self.previousPage or self.event then
sb:insert(-1, self.closeInd)
2018-11-14 02:31:55 +00:00
else
sb:insert(-2, self.frameChar)
2018-01-24 22:39:38 +00:00
end
self:write(1, 1, sb:get())
2016-12-11 19:24:52 +00:00
end
function UI.TitleBar:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'mouse_click' then
if (self.previousPage or self.event) and event.x == self.width then
if self.event then
self:emit({ type = self.event, element = self })
elseif type(self.previousPage) == 'string' or
type(self.previousPage) == 'table' then
UI:setPage(self.previousPage)
else
UI:setPreviousPage()
end
return true
end
end
2016-12-11 19:24:52 +00:00
end
2017-10-06 17:39:47 +00:00
--[[-- Button --]]--
UI.Button = class(UI.Window)
UI.Button.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Button',
text = 'button',
backgroundColor = colors.lightGray,
backgroundFocusColor = colors.gray,
textFocusColor = colors.white,
textInactiveColor = colors.gray,
textColor = colors.black,
centered = true,
height = 1,
focusIndicator = ' ',
event = 'button_press',
accelerators = {
space = 'button_activate',
enter = 'button_activate',
mouse_click = 'button_activate',
}
2017-10-06 17:39:47 +00:00
}
function UI.Button:setParent()
2018-01-24 22:39:38 +00:00
if not self.width and not self.ex then
self.width = #self.text + 2
end
UI.Window.setParent(self)
2017-10-06 17:39:47 +00:00
end
function UI.Button:draw()
2018-01-24 22:39:38 +00:00
local fg = self.textColor
local bg = self.backgroundColor
local ind = ' '
if self.focused then
bg = self.backgroundFocusColor
fg = self.textFocusColor
ind = self.focusIndicator
elseif self.inactive then
fg = self.textInactiveColor
end
local text = ind .. self.text .. ' '
if self.centered then
self:clear(bg)
self:centeredWrite(1 + math.floor(self.height / 2), text, bg, fg)
else
self:write(1, 1, Util.widthify(text, self.width), bg, fg)
end
2017-10-06 17:39:47 +00:00
end
function UI.Button:focus()
2018-01-24 22:39:38 +00:00
if self.focused then
self:scrollIntoView()
end
self:draw()
2017-10-06 17:39:47 +00:00
end
function UI.Button:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'button_activate' then
self:emit({ type = self.event, button = self })
return true
end
return false
2017-10-06 17:39:47 +00:00
end
--[[-- MenuItem --]]--
UI.MenuItem = class(UI.Button)
UI.MenuItem.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'MenuItem',
textColor = colors.black,
backgroundColor = colors.lightGray,
textFocusColor = colors.white,
backgroundFocusColor = colors.lightGray,
2017-10-06 17:39:47 +00:00
}
2016-12-11 19:24:52 +00:00
--[[-- MenuBar --]]--
UI.MenuBar = class(UI.Window)
UI.MenuBar.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'MenuBar',
buttons = { },
height = 1,
backgroundColor = colors.lightGray,
textColor = colors.black,
spacing = 2,
lastx = 1,
showBackButton = false,
buttonClass = 'MenuItem',
2016-12-11 19:24:52 +00:00
}
2017-10-06 17:39:47 +00:00
UI.MenuBar.spacer = { spacer = true, text = 'spacer', inactive = true }
2016-12-11 19:24:52 +00:00
2017-10-11 15:37:52 +00:00
function UI.MenuBar:postInit()
2018-01-24 22:39:38 +00:00
self:addButtons(self.buttons)
2017-10-09 04:26:19 +00:00
end
2016-12-11 19:24:52 +00:00
2017-10-09 04:26:19 +00:00
function UI.MenuBar:addButtons(buttons)
2018-01-24 22:39:38 +00:00
if not self.children then
self.children = { }
end
for _,button in pairs(buttons) do
if button.UIElement then
table.insert(self.children, button)
else
local buttonProperties = {
x = self.lastx,
width = #button.text + self.spacing,
centered = false,
}
self.lastx = self.lastx + buttonProperties.width
UI:mergeProperties(buttonProperties, button)
button = UI[self.buttonClass](buttonProperties)
if button.name then
self[button.name] = button
else
table.insert(self.children, button)
end
if button.dropdown then
button.dropmenu = UI.DropMenu { buttons = button.dropdown }
end
end
end
if self.parent then
self:initChildren()
end
2016-12-11 19:24:52 +00:00
end
2017-10-06 17:39:47 +00:00
function UI.MenuBar:getActive(menuItem)
2018-01-24 22:39:38 +00:00
return not menuItem.inactive
2017-10-06 17:39:47 +00:00
end
2016-12-11 19:24:52 +00:00
function UI.MenuBar:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'button_press' and event.button.dropmenu then
if event.button.dropmenu.enabled then
event.button.dropmenu:hide()
return true
else
local x, y = getPosition(event.button)
if x + event.button.dropmenu.width > self.width then
x = self.width - event.button.dropmenu.width + 1
end
for _,c in pairs(event.button.dropmenu.children) do
if not c.spacer then
c.inactive = not self:getActive(c)
end
end
event.button.dropmenu:show(x, y + 1)
end
return true
end
2016-12-11 19:24:52 +00:00
end
2017-10-06 17:39:47 +00:00
--[[-- DropMenuItem --]]--
UI.DropMenuItem = class(UI.Button)
UI.DropMenuItem.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'DropMenuItem',
textColor = colors.black,
backgroundColor = colors.white,
textFocusColor = colors.white,
textInactiveColor = colors.lightGray,
backgroundFocusColor = colors.lightGray,
2017-10-06 17:39:47 +00:00
}
2017-10-09 17:08:38 +00:00
function UI.DropMenuItem:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'button_activate' then
self.parent:hide()
end
return UI.Button.eventHandler(self, event)
2017-10-09 17:08:38 +00:00
end
2016-12-11 19:24:52 +00:00
--[[-- DropMenu --]]--
UI.DropMenu = class(UI.MenuBar)
UI.DropMenu.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'DropMenu',
backgroundColor = colors.white,
buttonClass = 'DropMenuItem',
2016-12-11 19:24:52 +00:00
}
function UI.DropMenu:setParent()
2018-01-24 22:39:38 +00:00
UI.MenuBar.setParent(self)
local maxWidth = 1
for y,child in ipairs(self.children) do
child.x = 1
child.y = y
if #(child.text or '') > maxWidth then
maxWidth = #child.text
end
end
for _,child in ipairs(self.children) do
child.width = maxWidth + 2
if child.spacer then
child.text = string.rep('-', child.width - 2)
end
end
self.height = #self.children + 1
self.width = maxWidth + 2
self.ow = self.width
self.canvas = self:addLayer()
2016-12-11 19:24:52 +00:00
end
function UI.DropMenu:enable()
2018-01-24 22:39:38 +00:00
self.enabled = false
2016-12-11 19:24:52 +00:00
end
2017-10-06 17:39:47 +00:00
function UI.DropMenu:show(x, y)
2018-01-24 22:39:38 +00:00
self.x, self.y = x, y
self.canvas:move(x, y)
self.canvas:setVisible(true)
2017-10-06 17:39:47 +00:00
2018-01-24 22:39:38 +00:00
self.enabled = true
for _,child in pairs(self.children) do
child:enable()
end
2017-10-06 17:39:47 +00:00
2018-01-24 22:39:38 +00:00
self:draw()
self:capture(self)
self:focusFirst()
2016-12-11 19:24:52 +00:00
end
function UI.DropMenu:hide()
2018-01-24 22:39:38 +00:00
self:disable()
self.canvas:setVisible(false)
self:release(self)
2016-12-11 19:24:52 +00:00
end
function UI.DropMenu:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'focus_lost' and self.enabled then
if not Util.contains(self.children, event.focused) then
self:hide()
end
elseif event.type == 'mouse_out' and self.enabled then
self:hide()
self:refocus()
else
return UI.MenuBar.eventHandler(self, event)
end
return true
2016-12-11 19:24:52 +00:00
end
2017-10-18 23:51:55 +00:00
--[[-- TabBarMenuItem --]]--
UI.TabBarMenuItem = class(UI.Button)
UI.TabBarMenuItem.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'TabBarMenuItem',
event = 'tab_select',
textColor = colors.black,
selectedBackgroundColor = colors.cyan,
unselectedBackgroundColor = colors.lightGray,
backgroundColor = colors.lightGray,
2017-10-18 23:51:55 +00:00
}
function UI.TabBarMenuItem:draw()
2018-01-24 22:39:38 +00:00
if self.selected then
self.backgroundColor = self.selectedBackgroundColor
self.backgroundFocusColor = self.selectedBackgroundColor
else
self.backgroundColor = self.unselectedBackgroundColor
self.backgroundFocusColor = self.unselectedBackgroundColor
end
UI.Button.draw(self)
2017-10-18 23:51:55 +00:00
end
2016-12-11 19:24:52 +00:00
--[[-- TabBar --]]--
UI.TabBar = class(UI.MenuBar)
UI.TabBar.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'TabBar',
buttonClass = 'TabBarMenuItem',
selectedBackgroundColor = colors.cyan,
2016-12-11 19:24:52 +00:00
}
2017-10-18 23:51:55 +00:00
function UI.TabBar:enable()
2018-01-24 22:39:38 +00:00
UI.MenuBar.enable(self)
if not Util.find(self.children, 'selected', true) then
local menuItem = self:getFocusables()[1]
if menuItem then
menuItem.selected = true
end
end
2017-10-18 23:51:55 +00:00
end
function UI.TabBar:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'tab_select' then
local selected, si = Util.find(self:getFocusables(), 'uid', event.button.uid)
local previous, pi = Util.find(self:getFocusables(), 'selected', true)
2017-10-18 23:51:55 +00:00
2018-01-24 22:39:38 +00:00
if si ~= pi then
selected.selected = true
previous.selected = false
self:emit({ type = 'tab_change', current = si, last = pi, tab = selected })
end
UI.MenuBar.draw(self)
end
return UI.MenuBar.eventHandler(self, event)
2017-10-18 23:51:55 +00:00
end
function UI.TabBar:selectTab(text)
2018-01-24 22:39:38 +00:00
local menuItem = Util.find(self.children, 'text', text)
if menuItem then
menuItem.selected = true
end
2016-12-11 19:24:52 +00:00
end
--[[-- Tabs --]]--
UI.Tabs = class(UI.Window)
UI.Tabs.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Tabs',
2016-12-11 19:24:52 +00:00
}
2017-10-11 15:37:52 +00:00
function UI.Tabs:postInit()
2018-01-24 22:39:38 +00:00
self:add(self)
2017-10-09 04:26:19 +00:00
end
function UI.Tabs:add(children)
2018-01-24 22:39:38 +00:00
local buttons = { }
for _,child in pairs(children) do
if type(child) == 'table' and child.UIElement and child.tabTitle then
child.y = 2
table.insert(buttons, {
text = child.tabTitle,
event = 'tab_select',
tabUid = child.uid,
})
end
end
if not self.tabBar then
self.tabBar = UI.TabBar({
buttons = buttons,
})
else
self.tabBar:addButtons(buttons)
end
if self.parent then
return UI.Window.add(self, children)
end
2016-12-11 19:24:52 +00:00
end
2018-12-23 03:11:16 +00:00
function UI.Tabs:selectTab(tab)
local menuItem = Util.find(self.tabBar:getFocusables(), 'tabUid', tab.uid)
if menuItem then
self.tabBar:emit({ type = 'tab_select', button = { uid = menuItem.uid } })
end
end
2018-12-31 16:35:13 +00:00
function UI.Tabs:setActive(tab, active)
local menuItem = Util.find(self.tabBar.children, 'tabUid', tab.uid)
if menuItem then
menuItem.inactive = not active
end
end
2016-12-11 19:24:52 +00:00
function UI.Tabs:enable()
2018-01-24 22:39:38 +00:00
self.enabled = true
self.tabBar:enable()
2017-10-09 04:26:19 +00:00
2018-01-24 22:39:38 +00:00
local menuItem = Util.find(self.tabBar.children, 'selected', true)
2017-10-18 23:51:55 +00:00
2018-01-24 22:39:38 +00:00
for _,child in pairs(self.children) do
if child.uid == menuItem.tabUid then
child:enable()
self:emit({ type = 'tab_activate', activated = child })
elseif child.tabTitle then
child:disable()
end
end
2016-12-11 19:24:52 +00:00
end
2017-10-18 23:51:55 +00:00
function UI.Tabs:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'tab_change' then
local tab = self:find(event.tab.tabUid)
if event.current > event.last then
tab:addTransition('slideLeft')
else
tab:addTransition('slideRight')
end
for _,child in pairs(self.children) do
if child.uid == event.tab.tabUid then
child:enable()
elseif child.tabTitle then
child:disable()
end
end
self:emit({ type = 'tab_activate', activated = tab })
tab:draw()
end
2016-12-11 19:24:52 +00:00
end
2017-10-18 23:51:55 +00:00
--[[-- Wizard --]]--
UI.Wizard = class(UI.Window)
UI.Wizard.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Wizard',
pages = { },
2017-10-18 23:51:55 +00:00
}
function UI.Wizard:postInit()
2018-01-24 22:39:38 +00:00
self.cancelButton = UI.Button {
x = 2, y = -1,
text = 'Cancel',
event = 'cancel',
}
self.previousButton = UI.Button {
x = -18, y = -1,
text = '< Back',
event = 'previousView',
}
self.nextButton = UI.Button {
x = -9, y = -1,
text = 'Next >',
event = 'nextView',
}
Util.merge(self, self.pages)
for _, child in pairs(self.pages) do
child.ey = -2
end
2016-12-14 17:36:28 +00:00
end
2017-10-20 08:23:17 +00:00
function UI.Wizard:add(pages)
2018-01-24 22:39:38 +00:00
Util.merge(self.pages, pages)
Util.merge(self, pages)
2017-10-20 08:23:17 +00:00
2018-01-24 22:39:38 +00:00
for _, child in pairs(self.pages) do
child.ey = child.ey or -2
end
2017-10-20 08:23:17 +00:00
2018-01-24 22:39:38 +00:00
if self.parent then
self:initChildren()
end
2017-10-20 08:23:17 +00:00
end
2018-12-05 01:55:08 +00:00
function UI.Wizard:getPage(index)
return Util.find(self.pages, 'index', index)
end
2018-10-24 02:33:40 +00:00
function UI.Wizard:enable(...)
2018-01-24 22:39:38 +00:00
self.enabled = true
2018-12-05 01:55:08 +00:00
self.index = 1
local initial = self:getPage(1)
for _,child in pairs(self.children) do
if child == initial or not child.index then
2018-10-24 02:33:40 +00:00
child:enable(...)
2018-01-24 22:39:38 +00:00
else
child:disable()
end
end
2018-12-05 01:55:08 +00:00
self:emit({ type = 'enable_view', next = initial })
2017-10-18 23:51:55 +00:00
end
function UI.Wizard:isViewValid()
2018-12-05 01:55:08 +00:00
local currentView = self:getPage(self.index)
return not currentView.validate and true or currentView:validate()
end
2017-10-18 23:51:55 +00:00
function UI.Wizard:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'nextView' then
2018-12-05 01:55:08 +00:00
local currentView = self:getPage(self.index)
if self:isViewValid() then
2018-12-05 01:55:08 +00:00
self.index = self.index + 1
local nextView = self:getPage(self.index)
currentView:emit({ type = 'enable_view', next = nextView, current = currentView })
end
2018-01-24 22:39:38 +00:00
elseif event.type == 'previousView' then
2018-12-05 01:55:08 +00:00
local currentView = self:getPage(self.index)
local nextView = self:getPage(self.index - 1)
if nextView then
self.index = self.index - 1
currentView:emit({ type = 'enable_view', prev = nextView, current = currentView })
end
2018-01-24 22:39:38 +00:00
return true
elseif event.type == 'wizard_complete' then
if self:isViewValid() then
self:emit({ type = 'accept' })
end
2018-01-24 22:39:38 +00:00
elseif event.type == 'enable_view' then
if event.current then
if event.next then
self:addTransition('slideLeft')
elseif event.prev then
self:addTransition('slideRight')
end
event.current:disable()
end
local current = event.next or event.prev
if not current then error('property "index" is required on wizard pages') end
2018-12-05 01:55:08 +00:00
if self:getPage(self.index - 1) then
2018-01-24 22:39:38 +00:00
self.previousButton:enable()
else
self.previousButton:disable()
end
2018-12-05 01:55:08 +00:00
if self:getPage(self.index + 1) then
2018-01-24 22:39:38 +00:00
self.nextButton.text = 'Next >'
self.nextButton.event = 'nextView'
else
self.nextButton.text = 'Accept'
self.nextButton.event = 'wizard_complete'
2018-01-24 22:39:38 +00:00
end
2018-01-24 23:45:47 +00:00
-- a new current view
current:enable()
2018-01-24 22:39:38 +00:00
self:draw()
end
2016-12-11 19:24:52 +00:00
end
2017-12-11 16:31:41 +00:00
--[[-- SlideOut --]]--
UI.SlideOut = class(UI.Window)
UI.SlideOut.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'SlideOut',
pageType = 'modal',
2017-12-11 16:31:41 +00:00
}
function UI.SlideOut:setParent()
2018-01-24 22:39:38 +00:00
UI.Window.setParent(self)
self.canvas = self:addLayer()
2017-12-11 16:31:41 +00:00
end
function UI.SlideOut:enable()
2018-01-24 22:39:38 +00:00
self.enabled = false
2017-12-11 16:31:41 +00:00
end
2018-10-24 02:33:40 +00:00
function UI.SlideOut:show(...)
2018-01-24 22:39:38 +00:00
self:addTransition('expandUp')
self.canvas:setVisible(true)
self.enabled = true
for _,child in pairs(self.children) do
2018-10-24 02:33:40 +00:00
child:enable(...)
2018-01-24 22:39:38 +00:00
end
self:draw()
self:capture(self)
self:focusFirst()
2017-12-11 16:31:41 +00:00
end
2017-12-23 06:47:54 +00:00
function UI.SlideOut:disable()
2018-01-24 22:39:38 +00:00
self.canvas:setVisible(false)
self.enabled = false
if self.children then
for _,child in pairs(self.children) do
child:disable()
end
end
2017-12-23 06:47:54 +00:00
end
2017-12-11 16:31:41 +00:00
function UI.SlideOut:hide()
2018-01-24 22:39:38 +00:00
self:disable()
self:release(self)
self:refocus()
2017-12-11 16:31:41 +00:00
end
2017-12-23 06:47:54 +00:00
function UI.SlideOut:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'slide_show' then
self:show()
return true
2017-12-23 06:47:54 +00:00
2018-01-24 22:39:38 +00:00
elseif event.type == 'slide_hide' then
self:hide()
return true
end
2017-12-23 06:47:54 +00:00
end
2018-01-22 03:08:30 +00:00
--[[-- Embedded --]]--
UI.Embedded = class(UI.Window)
UI.Embedded.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Embedded',
backgroundColor = colors.black,
textColor = colors.white,
accelerators = {
up = 'scroll_up',
down = 'scroll_down',
}
2018-01-22 03:08:30 +00:00
}
function UI.Embedded:setParent()
2018-01-24 22:39:38 +00:00
UI.Window.setParent(self)
self.win = window.create(UI.term.device, 1, 1, self.width, self.height, false)
Canvas.scrollingWindow(self.win, self.x, self.y)
self.win.setParent(UI.term.device)
self.win.setMaxScroll(100)
2018-01-22 03:08:30 +00:00
2018-01-24 22:39:38 +00:00
local canvas = self:getCanvas()
self.win.canvas.parent = canvas
table.insert(canvas.layers, self.win.canvas)
self.canvas = self.win.canvas
2018-01-22 03:08:30 +00:00
2018-01-24 22:39:38 +00:00
self.win.setCursorPos(1, 1)
self.win.setBackgroundColor(self.backgroundColor)
self.win.setTextColor(self.textColor)
self.win.clear()
2018-01-22 03:08:30 +00:00
end
function UI.Embedded:draw()
2018-01-24 22:39:38 +00:00
self.canvas:dirty()
2018-01-22 03:08:30 +00:00
end
function UI.Embedded:enable()
2018-01-24 22:39:38 +00:00
self.canvas:setVisible(true)
UI.Window.enable(self)
2018-01-22 03:08:30 +00:00
end
function UI.Embedded:disable()
2018-01-24 22:39:38 +00:00
self.canvas:setVisible(false)
UI.Window.disable(self)
2018-01-22 03:08:30 +00:00
end
function UI.Embedded:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'scroll_up' then
self.win.scrollUp()
return true
elseif event.type == 'scroll_down' then
self.win.scrollDown()
return true
end
2018-01-22 03:08:30 +00:00
end
function UI.Embedded:focus()
2018-01-24 22:39:38 +00:00
-- allow scrolling
2018-01-22 03:08:30 +00:00
end
2016-12-11 19:24:52 +00:00
--[[-- Notification --]]--
UI.Notification = class(UI.Window)
UI.Notification.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Notification',
backgroundColor = colors.gray,
height = 3,
2016-12-11 19:24:52 +00:00
}
function UI.Notification:draw()
end
function UI.Notification:enable()
2018-01-24 22:39:38 +00:00
self.enabled = false
2016-12-11 19:24:52 +00:00
end
function UI.Notification:error(value, timeout)
2018-01-24 22:39:38 +00:00
self.backgroundColor = colors.red
2018-11-27 01:53:28 +00:00
Sound.play('entity.villager.no', .5)
2018-01-24 22:39:38 +00:00
self:display(value, timeout)
2016-12-11 19:24:52 +00:00
end
function UI.Notification:info(value, timeout)
2018-01-24 22:39:38 +00:00
self.backgroundColor = colors.gray
self:display(value, timeout)
2016-12-11 19:24:52 +00:00
end
function UI.Notification:success(value, timeout)
2018-01-24 22:39:38 +00:00
self.backgroundColor = colors.green
self:display(value, timeout)
2016-12-11 19:24:52 +00:00
end
2016-12-27 03:26:43 +00:00
function UI.Notification:cancel()
2018-01-24 22:39:38 +00:00
if self.canvas then
Event.cancelNamedTimer('notificationTimer')
self.enabled = false
self.canvas:removeLayer()
self.canvas = nil
end
2016-12-27 03:26:43 +00:00
end
2016-12-11 19:24:52 +00:00
function UI.Notification:display(value, timeout)
2018-01-24 22:39:38 +00:00
self.enabled = true
local lines = Util.wordWrap(value, self.width - 2)
self.height = #lines + 1
self.y = self.parent.height - self.height + 1
if self.canvas then
self.canvas:removeLayer()
end
self.canvas = self:addLayer(self.backgroundColor, self.textColor)
self:addTransition('expandUp', { ticks = self.height })
self.canvas:setVisible(true)
self:clear()
for k,v in pairs(lines) do
self:write(2, k, v)
end
Event.addNamedTimer('notificationTimer', timeout or 3, false, function()
self:cancel()
self:sync()
end)
2016-12-11 19:24:52 +00:00
end
2017-06-23 06:04:56 +00:00
--[[-- Throttle --]]--
UI.Throttle = class(UI.Window)
UI.Throttle.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Throttle',
backgroundColor = colors.gray,
2018-11-22 03:06:00 +00:00
bordercolor = colors.cyan,
height = 4,
2018-01-24 22:39:38 +00:00
width = 10,
2018-11-22 03:06:00 +00:00
timeout = .075,
2018-01-24 22:39:38 +00:00
ctr = 0,
image = {
' //) (O )~@ &~&-( ?Q ',
' //) (O )- @ \\-( ?) && ',
' //) (O ), @ \\-(?) && ',
' //) (O ). @ \\-d ) (@ '
}
2017-06-23 06:04:56 +00:00
}
function UI.Throttle:setParent()
2018-01-24 22:39:38 +00:00
self.x = math.ceil((self.parent.width - self.width) / 2)
self.y = math.ceil((self.parent.height - self.height) / 2)
UI.Window.setParent(self)
2017-06-23 06:04:56 +00:00
end
function UI.Throttle:enable()
2018-11-22 03:06:00 +00:00
self.c = os.clock()
2018-01-24 22:39:38 +00:00
self.enabled = false
2017-06-23 06:04:56 +00:00
end
function UI.Throttle:disable()
2018-01-24 22:39:38 +00:00
if self.canvas then
self.enabled = false
self.canvas:removeLayer()
self.canvas = nil
2018-11-22 03:06:00 +00:00
self.ctr = 0
2018-01-24 22:39:38 +00:00
end
2017-06-23 06:04:56 +00:00
end
function UI.Throttle:update()
2018-01-24 22:39:38 +00:00
local cc = os.clock()
2018-11-22 03:06:00 +00:00
if cc > self.c + self.timeout then
2018-01-24 22:39:38 +00:00
os.sleep(0)
self.c = os.clock()
self.enabled = true
if not self.canvas then
2018-11-22 03:06:00 +00:00
self.canvas = self:addLayer(self.backgroundColor, self.borderColor)
2018-01-24 22:39:38 +00:00
self.canvas:setVisible(true)
2018-11-22 03:06:00 +00:00
self:clear(self.borderColor)
2018-01-24 22:39:38 +00:00
end
local image = self.image[self.ctr + 1]
local width = self.width - 2
for i = 0, #self.image do
2018-11-22 03:06:00 +00:00
self:write(2, i + 1, image:sub(width * i + 1, width * i + width),
self.backgroundColor, self.textColor)
2018-01-24 22:39:38 +00:00
end
self.ctr = (self.ctr + 1) % #self.image
self:sync()
end
2017-06-23 06:04:56 +00:00
end
2017-09-30 02:30:01 +00:00
--[[-- StatusBar --]]--
UI.StatusBar = class(UI.Window)
UI.StatusBar.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'StatusBar',
backgroundColor = colors.lightGray,
textColor = colors.gray,
height = 1,
ey = -1,
2016-12-11 19:24:52 +00:00
}
2017-09-30 02:30:01 +00:00
function UI.StatusBar:adjustWidth()
2018-01-24 22:39:38 +00:00
-- Can only have 1 adjustable width
if self.columns then
local w = self.width - #self.columns - 1
for _,c in pairs(self.columns) do
if c.width then
c.cw = c.width -- computed width
w = w - c.width
end
end
for _,c in pairs(self.columns) do
if not c.width then
c.cw = w
end
end
end
2016-12-11 19:24:52 +00:00
end
2017-09-30 02:30:01 +00:00
function UI.StatusBar:resize()
2018-01-24 22:39:38 +00:00
UI.Window.resize(self)
self:adjustWidth()
2016-12-11 19:24:52 +00:00
end
function UI.StatusBar:setParent()
2018-01-24 22:39:38 +00:00
UI.Window.setParent(self)
self:adjustWidth()
2016-12-11 19:24:52 +00:00
end
2017-09-30 02:30:01 +00:00
function UI.StatusBar:setStatus(status)
2018-01-24 22:39:38 +00:00
if self.values ~= status then
self.values = status
self:draw()
end
2016-12-11 19:24:52 +00:00
end
function UI.StatusBar:setValue(name, value)
2018-01-24 22:39:38 +00:00
if not self.values then
self.values = { }
end
self.values[name] = value
2016-12-11 19:24:52 +00:00
end
function UI.StatusBar:getValue(name)
2018-01-24 22:39:38 +00:00
if self.values then
return self.values[name]
end
2016-12-11 19:24:52 +00:00
end
function UI.StatusBar:timedStatus(status, timeout)
2018-01-24 22:39:38 +00:00
timeout = timeout or 3
self:write(2, 1, Util.widthify(status, self.width-2), self.backgroundColor)
Event.addNamedTimer('statusTimer', timeout, false, function()
if self.parent.enabled then
self:draw()
self:sync()
end
end)
2016-12-11 19:24:52 +00:00
end
function UI.StatusBar:getColumnWidth(name)
2018-01-24 22:39:38 +00:00
local c = Util.find(self.columns, 'key', name)
return c and c.cw
2016-12-11 19:24:52 +00:00
end
function UI.StatusBar:setColumnWidth(name, width)
2018-01-24 22:39:38 +00:00
local c = Util.find(self.columns, 'key', name)
if c then
c.cw = width
end
2016-12-11 19:24:52 +00:00
end
2017-09-30 02:30:01 +00:00
function UI.StatusBar:draw()
2018-01-24 22:39:38 +00:00
if not self.values then
self:clear()
elseif type(self.values) == 'string' then
self:write(1, 1, Util.widthify(' ' .. self.values, self.width))
else
local s = ''
for _,c in ipairs(self.columns) do
s = s .. ' ' .. Util.widthify(tostring(self.values[c.key] or ''), c.cw)
end
self:write(1, 1, Util.widthify(s, self.width))
end
2017-09-30 02:30:01 +00:00
end
2016-12-11 19:24:52 +00:00
--[[-- ProgressBar --]]--
UI.ProgressBar = class(UI.Window)
UI.ProgressBar.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'ProgressBar',
progressColor = colors.lime,
backgroundColor = colors.gray,
height = 1,
value = 0,
2016-12-11 19:24:52 +00:00
}
function UI.ProgressBar:draw()
2018-01-24 22:39:38 +00:00
self:clear()
local width = math.ceil(self.value / 100 * self.width)
self:clearArea(1, 1, width, self.height, self.progressColor)
2016-12-11 19:24:52 +00:00
end
--[[-- VerticalMeter --]]--
UI.VerticalMeter = class(UI.Window)
UI.VerticalMeter.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'VerticalMeter',
backgroundColor = colors.gray,
meterColor = colors.lime,
width = 1,
value = 0,
2016-12-11 19:24:52 +00:00
}
function UI.VerticalMeter:draw()
2018-01-24 22:39:38 +00:00
local height = self.height - math.ceil(self.value / 100 * self.height)
self:clear()
self:clearArea(1, height + 1, self.width, self.height, self.meterColor)
2016-12-11 19:24:52 +00:00
end
--[[-- TextEntry --]]--
UI.TextEntry = class(UI.Window)
UI.TextEntry.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'TextEntry',
value = '',
shadowText = '',
focused = false,
textColor = colors.white,
shadowTextColor = colors.gray,
backgroundColor = colors.black, -- colors.lightGray,
backgroundFocusColor = colors.black, --lightGray,
height = 1,
limit = 6,
pos = 0,
accelerators = {
[ 'control-c' ] = 'copy',
}
2016-12-11 19:24:52 +00:00
}
2017-10-11 15:37:52 +00:00
function UI.TextEntry:postInit()
2018-01-24 22:39:38 +00:00
self.value = tostring(self.value)
2016-12-11 19:24:52 +00:00
end
function UI.TextEntry:setValue(value)
2018-01-24 22:39:38 +00:00
self.value = value
2016-12-11 19:24:52 +00:00
end
function UI.TextEntry:setPosition(pos)
2018-01-24 22:39:38 +00:00
self.pos = pos
2016-12-11 19:24:52 +00:00
end
function UI.TextEntry:updateScroll()
2018-01-24 22:39:38 +00:00
if not self.scroll then
self.scroll = 0
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if not self.pos then
self.pos = #tostring(self.value)
self.scroll = 0
elseif self.pos > #tostring(self.value) then
self.pos = #tostring(self.value)
self.scroll = 0
end
2016-12-11 19:24:52 +00:00
2018-01-24 22:39:38 +00:00
if self.pos - self.scroll > self.width - 2 then
self.scroll = self.pos - (self.width - 2)
elseif self.pos < self.scroll then
self.scroll = self.pos
end
2016-12-11 19:24:52 +00:00
end
function UI.TextEntry:draw()
2018-01-24 22:39:38 +00:00
local bg = self.backgroundColor
local tc = self.textColor
if self.focused then
bg = self.backgroundFocusColor
end
self:updateScroll()
local text = tostring(self.value)
if #text > 0 then
if self.scroll and self.scroll > 0 then
text = text:sub(1 + self.scroll)
end
if self.mask then
text = _rep('*', #text)
end
else
tc = self.shadowTextColor
text = self.shadowText
end
self:write(1, 1, ' ' .. Util.widthify(text, self.width - 2) .. ' ', bg, tc)
if self.focused then
self:setCursorPos(self.pos-self.scroll+2, 1)
end
2016-12-11 19:24:52 +00:00
end
function UI.TextEntry:reset()
2018-01-24 22:39:38 +00:00
self.pos = 0
self.value = ''
self:draw()
self:updateCursor()
2016-12-11 19:24:52 +00:00
end
function UI.TextEntry:updateCursor()
2018-01-24 22:39:38 +00:00
self:updateScroll()
self:setCursorPos(self.pos-self.scroll+2, 1)
2016-12-11 19:24:52 +00:00
end
function UI.TextEntry:focus()
2018-01-24 22:39:38 +00:00
self:draw()
if self.focused then
self:setCursorBlink(true)
else
self:setCursorBlink(false)
end
2016-12-11 19:24:52 +00:00
end
--[[
2018-01-24 22:39:38 +00:00
A few lines below from theoriginalbit
http://www.computercraft.info/forums2/index.php?/topic/16070-read-and-limit-length-of-the-input-field/
2016-12-11 19:24:52 +00:00
--]]
function UI.TextEntry:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'key' then
local ch = event.key
if ch == 'left' then
if self.pos > 0 then
self.pos = math.max(self.pos-1, 0)
self:draw()
end
elseif ch == 'right' then
local input = tostring(self.value)
if self.pos < #input then
self.pos = math.min(self.pos+1, #input)
self:draw()
end
elseif ch == 'home' then
self.pos = 0
self:draw()
elseif ch == 'end' then
self.pos = #tostring(self.value)
self:draw()
elseif ch == 'backspace' then
if self.pos > 0 then
local input = tostring(self.value)
self.value = input:sub(1, self.pos-1) .. input:sub(self.pos+1)
self.pos = self.pos - 1
self:draw()
self:emit({ type = 'text_change', text = self.value, element = self })
end
elseif ch == 'delete' then
local input = tostring(self.value)
if self.pos < #input then
self.value = input:sub(1, self.pos) .. input:sub(self.pos+2)
self:draw()
self:emit({ type = 'text_change', text = self.value, element = self })
end
elseif #ch == 1 then
local input = tostring(self.value)
if #input < self.limit then
self.value = input:sub(1, self.pos) .. ch .. input:sub(self.pos+1)
self.pos = self.pos + 1
self:draw()
self:emit({ type = 'text_change', text = self.value, element = self })
end
else
return false
end
return true
elseif event.type == 'copy' then
os.queueEvent('clipboard_copy', self.value)
elseif event.type == 'paste' then
local input = tostring(self.value)
local text = event.text
if #input + #text > self.limit then
text = text:sub(1, self.limit-#input)
end
self.value = input:sub(1, self.pos) .. text .. input:sub(self.pos+1)
self.pos = self.pos + #text
self:draw()
self:updateCursor()
self:emit({ type = 'text_change', text = self.value, element = self })
return true
elseif event.type == 'mouse_click' then
if self.focused and event.x > 1 then
self.pos = event.x + self.scroll - 2
self:updateCursor()
return true
end
elseif event.type == 'mouse_rightclick' then
local input = tostring(self.value)
if #input > 0 then
self:reset()
self:emit({ type = 'text_change', text = self.value, element = self })
end
end
return false
2016-12-11 19:24:52 +00:00
end
--[[-- Chooser --]]--
UI.Chooser = class(UI.Window)
UI.Chooser.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Chooser',
choices = { },
nochoice = 'Select',
backgroundFocusColor = colors.lightGray,
2018-10-31 23:38:09 +00:00
textInactiveColor = colors.gray,
2018-10-26 22:45:22 +00:00
leftIndicator = '<',
rightIndicator = '>',
2018-01-24 22:39:38 +00:00
height = 1,
2016-12-11 19:24:52 +00:00
}
function UI.Chooser:setParent()
2018-01-24 22:39:38 +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
UI.Window.setParent(self)
2016-12-11 19:24:52 +00:00
end
function UI.Chooser:draw()
2018-01-24 22:39:38 +00:00
local bg = self.backgroundColor
if self.focused then
bg = self.backgroundFocusColor
end
2018-10-31 23:38:09 +00:00
local fg = self.inactive and self.textInactiveColor or self.textColor
2018-01-24 22:39:38 +00:00
local choice = Util.find(self.choices, 'value', self.value)
local value = self.nochoice
if choice then
value = choice.name
end
2018-10-26 22:45:22 +00:00
self:write(1, 1, self.leftIndicator, self.backgroundColor, colors.black)
2018-10-31 23:38:09 +00:00
self:write(2, 1, ' ' .. Util.widthify(tostring(value), self.width-4) .. ' ', bg, fg)
2018-10-26 22:45:22 +00:00
self:write(self.width, 1, self.rightIndicator, self.backgroundColor, colors.black)
2016-12-11 19:24:52 +00:00
end
function UI.Chooser:focus()
2018-01-24 22:39:38 +00:00
self:draw()
2016-12-11 19:24:52 +00:00
end
function UI.Chooser:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'key' then
if event.key == 'right' or event.key == 'space' then
local _,k = Util.find(self.choices, 'value', self.value)
local choice
2018-12-23 03:11:16 +00:00
if not k then k = 1 end
2018-01-24 22:39:38 +00:00
if k and k < #self.choices then
choice = self.choices[k+1]
2018-01-24 22:39:38 +00:00
else
choice = self.choices[1]
2018-01-24 22:39:38 +00:00
end
self.value = choice.value
self:emit({ type = 'choice_change', value = self.value, element = self, choice = choice })
2018-01-24 22:39:38 +00:00
self:draw()
return true
elseif event.key == 'left' then
local _,k = Util.find(self.choices, 'value', self.value)
local choice
2018-01-24 22:39:38 +00:00
if k and k > 1 then
choice = self.choices[k-1]
2018-01-24 22:39:38 +00:00
else
choice = self.choices[#self.choices]
2018-01-24 22:39:38 +00:00
end
self.value = choice.value
self:emit({ type = 'choice_change', value = self.value, element = self, choice = choice })
2018-01-24 22:39:38 +00:00
self:draw()
return true
end
2018-12-23 03:11:16 +00:00
elseif event.type == 'mouse_click' or event.type == 'mouse_doubleclick' then
2018-01-24 22:39:38 +00:00
if event.x == 1 then
self:emit({ type = 'key', key = 'left' })
return true
elseif event.x == self.width then
self:emit({ type = 'key', key = 'right' })
return true
end
end
2016-12-11 19:24:52 +00:00
end
2018-10-26 22:45:22 +00:00
--[[-- Chooser --]]--
UI.Checkbox = class(UI.Window)
UI.Checkbox.defaults = {
UIElement = 'Checkbox',
nochoice = 'Select',
checkedIndicator = 'X',
leftMarker = '[',
rightMarker = ']',
value = false,
textColor = colors.white,
backgroundColor = colors.black,
backgroundFocusColor = colors.lightGray,
height = 1,
width = 3,
accelerators = {
space = 'checkbox_toggle',
mouse_click = 'checkbox_toggle',
}
}
function UI.Checkbox:draw()
local bg = self.backgroundColor
if self.focused then
bg = self.backgroundFocusColor
end
if type(self.value) == 'string' then
self.value = nil -- TODO: fix form
end
local text = string.format('[%s]', not self.value and ' ' or self.checkedIndicator)
self:write(1, 1, text, bg)
self:write(1, 1, self.leftMarker, self.backgroundColor, self.textColor)
self:write(2, 1, not self.value and ' ' or self.checkedIndicator, bg)
self:write(3, 1, self.rightMarker, self.backgroundColor, self.textColor)
end
function UI.Checkbox:focus()
self:draw()
end
function UI.Checkbox:reset()
self.value = false
end
function UI.Checkbox:eventHandler(event)
if event.type == 'checkbox_toggle' then
self.value = not self.value
self:emit({ type = 'checkbox_change', checked = self.value, element = self })
self:draw()
return true
end
end
2016-12-11 19:24:52 +00:00
--[[-- Text --]]--
UI.Text = class(UI.Window)
UI.Text.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Text',
value = '',
height = 1,
2016-12-11 19:24:52 +00:00
}
function UI.Text:setParent()
2018-01-24 22:39:38 +00:00
if not self.width and not self.ex then
self.width = #tostring(self.value)
end
UI.Window.setParent(self)
2016-12-11 19:24:52 +00:00
end
function UI.Text:draw()
2018-01-24 22:39:38 +00:00
self:write(1, 1, Util.widthify(self.value or '', self.width), self.backgroundColor)
2017-10-08 03:03:18 +00:00
end
--[[-- ScrollBar --]]--
UI.ScrollBar = class(UI.Window)
UI.ScrollBar.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'ScrollBar',
lineChar = '|',
sliderChar = '#',
upArrowChar = '^',
downArrowChar = 'v',
scrollbarColor = colors.lightGray,
value = '',
width = 1,
x = -1,
ey = -1,
2017-10-08 03:03:18 +00:00
}
function UI.ScrollBar:draw()
2018-01-24 22:39:38 +00:00
local parent = self.parent
local view = parent:getViewArea()
if view.totalHeight > view.height then
local maxScroll = view.totalHeight - view.height
local percent = view.offsetY / maxScroll
local sliderSize = math.max(1, Util.round(view.height / view.totalHeight * (view.height - 2)))
local x = self.width
local row = view.y
if not view.static then -- does the container scroll ?
self.y = row -- if so, move the scrollbar onscreen
row = 1
end
for i = 1, view.height - 2 do
self:write(x, row + i, self.lineChar, nil, self.scrollbarColor)
end
local y = Util.round((view.height - 2 - sliderSize) * percent)
for i = 1, sliderSize do
self:write(x, row + y + i, self.sliderChar, nil, self.scrollbarColor)
end
local color = self.scrollbarColor
if view.offsetY > 0 then
color = colors.white
end
self:write(x, row, self.upArrowChar, nil, color)
color = self.scrollbarColor
if view.offsetY + view.height < view.totalHeight then
color = colors.white
end
self:write(x, row + view.height - 1, self.downArrowChar, nil, color)
end
2017-10-08 03:03:18 +00:00
end
function UI.ScrollBar:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'mouse_click' or event.type == 'mouse_doubleclick' then
if event.x == 1 then
local view = self.parent:getViewArea()
if view.totalHeight > view.height then
if event.y == view.y then
self:emit({ type = 'scroll_up'})
elseif event.y == self.height then
self:emit({ type = 'scroll_down'})
-- else
-- ... percentage ...
end
end
return true
end
end
2016-12-11 19:24:52 +00:00
end
2017-10-05 17:07:48 +00:00
--[[-- TextArea --]]--
2017-10-08 03:03:18 +00:00
UI.TextArea = class(UI.Viewport)
2017-10-03 04:50:54 +00:00
UI.TextArea.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'TextArea',
marginRight = 2,
value = '',
2017-10-03 04:50:54 +00:00
}
2017-10-11 15:37:52 +00:00
function UI.TextArea:postInit()
2018-01-24 22:39:38 +00:00
self.scrollBar = UI.ScrollBar()
2017-10-03 04:50:54 +00:00
end
function UI.TextArea:setText(text)
2018-01-24 22:39:38 +00:00
self.offy = 0
self.ymax = nil
self.value = text
self:draw()
2017-10-03 04:50:54 +00:00
end
2017-10-09 17:08:38 +00:00
function UI.TextArea:focus()
2018-01-24 22:39:38 +00:00
-- allow keyboard scrolling
2017-10-09 17:08:38 +00:00
end
2017-10-03 04:50:54 +00:00
function UI.TextArea:draw()
2018-01-24 22:39:38 +00:00
self:clear()
2017-10-18 23:51:55 +00:00
-- self:setCursorPos(1, 1)
2018-01-24 22:39:38 +00:00
self.cursorX, self.cursorY = 1, 1
self:print(self.value)
2018-12-30 11:15:16 +00:00
self.ymax = self.cursorY
2017-10-08 03:03:18 +00:00
2018-01-24 22:39:38 +00:00
for _,child in pairs(self.children) do
if child.enabled then
child:draw()
end
end
2017-10-03 04:50:54 +00:00
end
2016-12-11 19:24:52 +00:00
--[[-- Form --]]--
UI.Form = class(UI.Window)
UI.Form.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Form',
values = { },
margin = 2,
event = 'form_complete',
2016-12-11 19:24:52 +00:00
}
2017-10-11 15:37:52 +00:00
function UI.Form:postInit()
2018-01-24 22:39:38 +00:00
self:createForm()
2016-12-27 03:26:43 +00:00
end
2016-12-11 19:24:52 +00:00
2016-12-27 03:26:43 +00:00
function UI.Form:reset()
2018-01-24 22:39:38 +00:00
for _,child in pairs(self.children) do
if child.reset then
child:reset()
end
end
2016-12-11 19:24:52 +00:00
end
function UI.Form:setValues(values)
2018-01-24 22:39:38 +00:00
self:reset()
self.values = values
for _,child in pairs(self.children) do
if child.formKey then
-- this should be child:setValue(self.values[child.formKey])
-- so chooser can set default choice if null
-- null should be valid as well
child.value = self.values[child.formKey] or ''
end
end
2016-12-11 19:24:52 +00:00
end
2016-12-27 03:26:43 +00:00
function UI.Form:createForm()
2018-01-24 22:39:38 +00:00
self.children = self.children or { }
if not self.labelWidth then
self.labelWidth = 1
for _, child in pairs(self) do
if type(child) == 'table' and child.UIElement then
if child.formLabel then
self.labelWidth = math.max(self.labelWidth, #child.formLabel + 2)
end
end
end
end
local y = self.margin
for _, child in pairs(self) do
if type(child) == 'table' and child.UIElement then
if child.formKey then
child.value = self.values[child.formKey] or ''
end
if child.formLabel then
child.x = self.labelWidth + self.margin - 1
child.y = y
if not child.width and not child.ex then
child.ex = -self.margin
end
table.insert(self.children, UI.Text {
x = self.margin,
y = y,
textColor = colors.black,
width = #child.formLabel,
value = child.formLabel,
})
end
if child.formKey or child.formLabel then
y = y + 1
end
end
end
if not self.manualControls then
table.insert(self.children, UI.Button {
y = -self.margin, x = -12 - self.margin,
text = 'Ok',
event = 'form_ok',
})
table.insert(self.children, UI.Button {
y = -self.margin, x = -7 - self.margin,
text = 'Cancel',
event = 'form_cancel',
})
end
2016-12-27 03:26:43 +00:00
end
function UI.Form:validateField(field)
2018-01-24 22:39:38 +00:00
if field.required then
if not field.value or #tostring(field.value) == 0 then
return false, 'Field is required'
end
end
if field.validate == 'numeric' then
if #tostring(field.value) > 0 then
if not tonumber(field.value) then
return false, 'Invalid number'
end
end
end
2018-01-24 22:39:38 +00:00
return true
2016-12-11 19:24:52 +00:00
end
function UI.Form:save()
for _,child in pairs(self.children) do
if child.formKey then
local s, m = self:validateField(child)
if not s then
self:setFocus(child)
self:emit({ type = 'form_invalid', message = m, field = child })
return false
2018-01-24 22:39:38 +00:00
end
end
end
for _,child in pairs(self.children) do
if child.formKey then
2018-10-24 02:33:40 +00:00
if (child.pruneEmpty and type(child.value) == 'string' and #child.value == 0) or
(child.pruneEmpty and type(child.value) == 'boolean' and not child.value) then
self.values[child.formKey] = nil
elseif child.validate == 'numeric' then
self.values[child.formKey] = tonumber(child.value)
else
2018-01-24 22:39:38 +00:00
self.values[child.formKey] = child.value
end
end
end
return true
end
function UI.Form:eventHandler(event)
if event.type == 'form_ok' then
if not self:save() then
return false
end
2018-01-24 22:39:38 +00:00
self:emit({ type = self.event, UIElement = self })
else
return UI.Window.eventHandler(self, event)
end
return true
2016-12-11 19:24:52 +00:00
end
--[[-- Dialog --]]--
UI.Dialog = class(UI.Page)
UI.Dialog.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Dialog',
x = 7,
y = 4,
z = 2,
height = 7,
textColor = colors.black,
backgroundColor = colors.white,
2016-12-11 19:24:52 +00:00
}
2017-10-11 15:37:52 +00:00
function UI.Dialog:postInit()
2018-01-24 22:39:38 +00:00
self.titleBar = UI.TitleBar({ previousPage = true, title = self.title })
2016-12-11 19:24:52 +00:00
end
2016-12-27 03:26:43 +00:00
function UI.Dialog:setParent()
2018-01-24 22:39:38 +00:00
if not self.width then
self.width = self.parent.width - 11
end
if self.width > self.parent.width then
self.width = self.parent.width
end
self.x = math.floor((self.parent.width - self.width) / 2) + 1
self.y = math.floor((self.parent.height - self.height) / 2) + 1
UI.Page.setParent(self)
2016-12-27 03:26:43 +00:00
end
2017-10-06 07:07:24 +00:00
function UI.Dialog:disable()
2018-01-24 22:39:38 +00:00
self.previousPage.canvas.palette = self.oldPalette
UI.Page.disable(self)
2017-10-06 07:07:24 +00:00
end
2017-10-03 04:50:54 +00:00
function UI.Dialog:enable(...)
2018-01-24 22:39:38 +00:00
self.oldPalette = self.previousPage.canvas.palette
self.previousPage.canvas:applyPalette(Canvas.darkPalette)
self:addTransition('grow')
UI.Page.enable(self, ...)
2017-10-03 04:50:54 +00:00
end
2016-12-11 19:24:52 +00:00
function UI.Dialog:eventHandler(event)
2018-01-24 22:39:38 +00:00
if event.type == 'cancel' then
UI:setPreviousPage()
end
return UI.Page.eventHandler(self, event)
2016-12-11 19:24:52 +00:00
end
--[[-- Image --]]--
UI.Image = class(UI.Window)
UI.Image.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'Image',
event = 'button_press',
2016-12-11 19:24:52 +00:00
}
function UI.Image:setParent()
2018-01-24 22:39:38 +00:00
if self.image then
self.height = #self.image
end
if self.image and not self.width then
self.width = #self.image[1]
end
UI.Window.setParent(self)
2016-12-11 19:24:52 +00:00
end
function UI.Image:draw()
2018-01-24 22:39:38 +00:00
self:clear()
if self.image then
for y = 1, #self.image do
local line = self.image[y]
for x = 1, #line do
local ch = line[x]
if type(ch) == 'number' then
if ch > 0 then
self:write(x, y, ' ', ch)
end
else
self:write(x, y, ch)
end
end
end
end
2016-12-11 19:24:52 +00:00
end
function UI.Image:setImage(image)
2018-01-24 22:39:38 +00:00
self.image = image
2016-12-11 19:24:52 +00:00
end
--[[-- NftImage --]]--
UI.NftImage = class(UI.Window)
UI.NftImage.defaults = {
2018-01-24 22:39:38 +00:00
UIElement = 'NftImage',
event = 'button_press',
2016-12-11 19:24:52 +00:00
}
function UI.NftImage:setParent()
2018-01-24 22:39:38 +00:00
if self.image then
self.height = self.image.height
end
if self.image and not self.width then
self.width = self.image.width
end
UI.Window.setParent(self)
2016-12-11 19:24:52 +00:00
end
function UI.NftImage:draw()
2018-01-24 22:39:38 +00:00
if self.image then
for y = 1, self.image.height do
for x = 1, #self.image.text[y] do
self:write(x, y, self.image.text[y][x], self.image.bg[y][x], self.image.fg[y][x])
end
end
else
self:clear()
end
2016-12-11 19:24:52 +00:00
end
function UI.NftImage:setImage(image)
2018-01-24 22:39:38 +00:00
self.image = image
2016-12-11 19:24:52 +00:00
end
2017-05-20 22:27:26 +00:00
UI:loadTheme('usr/config/ui.theme')
2017-10-12 21:58:35 +00:00
if Util.getVersion() >= 1.76 then
2018-01-24 22:39:38 +00:00
UI:loadTheme('sys/etc/ext.theme')
2016-12-11 19:24:52 +00:00
end
2016-12-15 14:45:27 +00:00
UI:setDefaultDevice(UI.Device({ device = term.current() }))
2016-12-11 19:24:52 +00:00
return UI