use layout() where appropriate and cleanup

This commit is contained in:
kepler155c@gmail.com
2020-04-09 16:08:54 -06:00
parent 8fe6e0806c
commit cd6ef0da50
14 changed files with 140 additions and 151 deletions

View File

@@ -23,11 +23,11 @@ UI.Button.defaults = {
mouse_click = 'button_activate',
}
}
function UI.Button:setParent()
function UI.Button:layout()
if not self.width and not self.ex then
self.width = self.noPadding and #self.text or #self.text + 2
end
UI.Window.setParent(self)
UI.Window.layout(self)
end
function UI.Button:draw()

View File

@@ -21,8 +21,9 @@ UI.Checkbox.defaults = {
mouse_click = 'checkbox_toggle',
}
}
function UI.Checkbox:postInit()
function UI.Checkbox:layout()
self.width = self.label and #self.label + 4 or 3
UI.Window.layout(self)
end
function UI.Checkbox:draw()

View File

@@ -20,7 +20,7 @@ UI.Chooser.defaults = {
left = 'choice_prev',
}
}
function UI.Chooser:setParent()
function UI.Chooser:layout()
if not self.width and not self.ex then
self.width = 1
for _,v in pairs(self.choices) do
@@ -30,7 +30,7 @@ function UI.Chooser:setParent()
end
self.width = self.width + 4
end
UI.Window.setParent(self)
UI.Window.layout(self)
end
function UI.Chooser:draw()

View File

@@ -41,14 +41,28 @@ end
function UI.DropMenu:enable()
local menuBar = self.parent:find(self.menuUid)
local hasActive
for _,c in pairs(self.children) do
if not c.spacer and menuBar then
c.inactive = not menuBar:getActive(c)
end
if not c.inactive then
hasActive = true
end
end
-- jump through a lot of hoops if all selections are inactive
-- there's gotta be a better way
-- lots of exception code just to handle drop menus
self.focus = not hasActive and function() end
UI.Window.enable(self)
self:focusFirst()
if self.focus then
self:setFocus(self)
else
self:focusFirst()
end
self:draw()
end
@@ -59,7 +73,7 @@ end
function UI.DropMenu:eventHandler(event)
if event.type == 'focus_lost' and self.enabled then
if not Util.contains(self.children, event.focused) then
if not (Util.contains(self.children, event.focused) or event.focused == self) then
self:disable()
end
elseif event.type == 'mouse_out' and self.enabled then

View File

@@ -15,19 +15,21 @@ UI.Embedded.defaults = {
down = 'scroll_down',
}
}
function UI.Embedded:setParent()
UI.Window.setParent(self)
function UI.Embedded:layout()
UI.Window.layout(self)
function self.render()
self:sync()
if not self.win then
function self.render()
self:sync()
end
self.win = Terminal.window(UI.term.device, self.x, self.y, self.width, self.height, false)
self.win.canvas = self
self.win.setMaxScroll(self.maxScroll)
self.win.setCursorPos(1, 1)
self.win.setBackgroundColor(self.backgroundColor)
self.win.setTextColor(self.textColor)
self.win.clear()
end
self.win = Terminal.window(UI.term.device, self.x, self.y, self.width, self.height, false)
self.win.canvas = self
self.win.setMaxScroll(self.maxScroll)
self.win.setCursorPos(1, 1)
self.win.setBackgroundColor(self.backgroundColor)
self.win.setTextColor(self.textColor)
self.win.clear()
end
function UI.Embedded:draw()

View File

@@ -83,8 +83,8 @@ UI.Grid.defaults = {
[ 'control-f' ] = 'scroll_pageDown',
},
}
function UI.Grid:setParent()
UI.Window.setParent(self)
function UI.Grid:layout()
UI.Window.layout(self)
for _,c in pairs(self.columns) do
c.cw = c.width

View File

@@ -13,8 +13,7 @@ function UI.Menu:postInit()
self.pageSize = #self.menuItems
end
function UI.Menu:setParent()
UI.Grid.setParent(self)
function UI.Menu:layout()
self.itemWidth = 1
for _,v in pairs(self.values) do
if #v.prompt > self.itemWidth then
@@ -28,6 +27,7 @@ function UI.Menu:setParent()
else
self.width = self.itemWidth + 2
end
UI.Grid.layout(self)
end
function UI.Menu:center()

View File

@@ -13,7 +13,8 @@ UI.StatusBar.defaults = {
height = 1,
ey = -1,
}
function UI.StatusBar:adjustWidth()
function UI.StatusBar:layout()
UI.Window.layout(self)
-- Can only have 1 adjustable width
if self.columns then
local w = self.width - #self.columns - 1
@@ -31,16 +32,6 @@ function UI.StatusBar:adjustWidth()
end
end
function UI.StatusBar:resize()
UI.Window.resize(self)
self:adjustWidth()
end
function UI.StatusBar:setParent()
UI.Window.setParent(self)
self:adjustWidth()
end
function UI.StatusBar:setStatus(status)
if self.values ~= status then
self.values = status

View File

@@ -8,11 +8,11 @@ UI.Text.defaults = {
value = '',
height = 1,
}
function UI.Text:setParent()
function UI.Text:layout()
if not self.width and not self.ex then
self.width = #tostring(self.value)
end
UI.Window.setParent(self)
UI.Window.layout(self)
end
function UI.Text:draw()

View File

@@ -2,61 +2,61 @@ local Tween = require('opus.ui.tween')
local Transition = { }
function Transition.slideLeft(args)
function Transition.slideLeft(canvas, args)
local ticks = args.ticks or 6
local easing = args.easing or 'inCirc'
local pos = { x = args.ex }
local tween = Tween.new(ticks, pos, { x = args.x }, easing)
local pos = { x = canvas.ex }
local tween = Tween.new(ticks, pos, { x = canvas.x }, easing)
args.canvas:move(pos.x, args.canvas.y)
canvas:move(pos.x, canvas.y)
return function()
local finished = tween:update(1)
args.canvas:move(math.floor(pos.x), args.canvas.y)
args.canvas:dirty(true)
canvas:move(math.floor(pos.x), canvas.y)
canvas:dirty(true)
return not finished
end
end
function Transition.slideRight(args)
function Transition.slideRight(canvas, args)
local ticks = args.ticks or 6
local easing = args.easing or 'inCirc'
local pos = { x = -args.canvas.width }
local pos = { x = -canvas.width }
local tween = Tween.new(ticks, pos, { x = 1 }, easing)
args.canvas:move(pos.x, args.canvas.y)
canvas:move(pos.x, canvas.y)
return function()
local finished = tween:update(1)
args.canvas:move(math.floor(pos.x), args.canvas.y)
args.canvas:dirty(true)
canvas:move(math.floor(pos.x), canvas.y)
canvas:dirty(true)
return not finished
end
end
function Transition.expandUp(args)
function Transition.expandUp(canvas, args)
local ticks = args.ticks or 3
local easing = args.easing or 'linear'
local pos = { y = args.ey + 1 }
local tween = Tween.new(ticks, pos, { y = args.y }, easing)
local pos = { y = canvas.ey + 1 }
local tween = Tween.new(ticks, pos, { y = canvas.y }, easing)
args.canvas:move(args.x, pos.y)
canvas:move(canvas.x, pos.y)
return function()
local finished = tween:update(1)
args.canvas:move(args.x, math.floor(pos.y))
args.canvas.parent:dirty(true)
canvas:move(canvas.x, math.floor(pos.y))
canvas.parent:dirty(true)
return not finished
end
end
function Transition.shake(args)
function Transition.shake(canvas, args)
local ticks = args.ticks or 8
local i = ticks
return function()
i = -i
args.canvas:move(args.canvas.x + i, args.canvas.y)
canvas:move(canvas.x + i, canvas.y)
if i > 0 then
i = i - 2
end
@@ -64,15 +64,15 @@ function Transition.shake(args)
end
end
function Transition.shuffle(args)
function Transition.shuffle(canvas, args)
local ticks = args.ticks or 4
local easing = args.easing or 'linear'
local t = { }
for _,child in pairs(args.canvas.children) do
for _,child in pairs(canvas.children) do
t[child] = Tween.new(ticks, child, { x = child.x, y = child.y }, easing)
child.x = math.random(1, args.canvas.parent.width)
child.y = math.random(1, args.canvas.parent.height)
child.x = math.random(1, canvas.parent.width)
child.y = math.random(1, canvas.parent.height)
end
return function()