can now use named colors

This commit is contained in:
kepler155c@gmail.com
2020-04-21 22:32:12 -06:00
parent e703c7f7b6
commit 5933f8c40f
42 changed files with 308 additions and 400 deletions

View File

@@ -2,17 +2,15 @@ local class = require('opus.class')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
UI.Button = class(UI.Window)
UI.Button.defaults = {
UIElement = 'Button',
text = 'button',
backgroundColor = colors.lightGray,
backgroundFocusColor = colors.gray,
textFocusColor = colors.white,
textInactiveColor = colors.gray,
textColor = colors.black,
backgroundColor = 'lightGray',
backgroundFocusColor = 'gray',
textFocusColor = 'white',
textInactiveColor = 'gray',
textColor = 'black',
centered = true,
height = 1,
focusIndicator = ' ',
@@ -73,7 +71,7 @@ function UI.Button.example()
},
button2 = UI.Button {
x = 2, y = 4,
backgroundColor = colors.green,
backgroundColor = 'green',
event = 'custom_event',
},
button3 = UI.Button {

View File

@@ -1,8 +1,6 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.Checkbox = class(UI.Window)
UI.Checkbox.defaults = {
UIElement = 'Checkbox',
@@ -11,9 +9,9 @@ UI.Checkbox.defaults = {
leftMarker = UI.extChars and '\124' or '[',
rightMarker = UI.extChars and '\124' or ']',
value = false,
textColor = colors.white,
backgroundColor = colors.black,
backgroundFocusColor = colors.lightGray,
textColor = 'white',
backgroundColor = 'black',
backgroundFocusColor = 'lightGray',
height = 1,
width = 3,
accelerators = {

View File

@@ -1,8 +1,6 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.Dialog = class(UI.SlideOut)
UI.Dialog.defaults = {
UIElement = 'Dialog',
@@ -33,7 +31,7 @@ function UI.Dialog.example()
cancelEvent = 'slide_hide',
text = UI.Text {
x = 5, y = 1, width = 20,
textColor = colors.gray,
textColor = 'gray',
},
textEntry = UI.TextEntry {
formKey = 'level',

View File

@@ -2,12 +2,10 @@ local class = require('opus.class')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
UI.DropMenu = class(UI.MenuBar)
UI.DropMenu.defaults = {
UIElement = 'DropMenu',
backgroundColor = colors.white,
backgroundColor = 'white',
buttonClass = 'DropMenuItem',
}
function UI.DropMenu:layout()

View File

@@ -1,16 +1,14 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.DropMenuItem = class(UI.Button)
UI.DropMenuItem.defaults = {
UIElement = 'DropMenuItem',
textColor = colors.black,
backgroundColor = colors.white,
textFocusColor = colors.white,
textInactiveColor = colors.lightGray,
backgroundFocusColor = colors.lightGray,
textColor = 'black',
backgroundColor = 'white',
textFocusColor = 'white',
textInactiveColor = 'lightGray',
backgroundFocusColor = 'lightGray',
}
function UI.DropMenuItem:eventHandler(event)
if event.type == 'button_activate' then

View File

@@ -3,13 +3,11 @@ local Event = require('opus.event')
local Terminal = require('opus.terminal')
local UI = require('opus.ui')
local colors = _G.colors
UI.Embedded = class(UI.Window)
UI.Embedded.defaults = {
UIElement = 'Embedded',
backgroundColor = colors.black,
textColor = colors.white,
backgroundColor = 'black',
textColor = 'white',
maxScroll = 100,
accelerators = {
up = 'scroll_up',

View File

@@ -1,13 +1,11 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.FlatButton = class(UI.Button)
UI.FlatButton.defaults = {
UIElement = 'FlatButton',
textColor = colors.black,
textFocusColor = colors.white,
textColor = 'black',
textFocusColor = 'white',
noPadding = true,
}
function UI.FlatButton:setParent()

View File

@@ -2,8 +2,6 @@ local class = require('opus.class')
local Sound = require('opus.sound')
local UI = require('opus.ui')
local colors = _G.colors
UI.Form = class(UI.Window)
UI.Form.defaults = {
UIElement = 'Form',
@@ -68,7 +66,7 @@ function UI.Form:createForm()
table.insert(self.children, UI.Text {
x = self.margin,
y = child.y,
textColor = colors.black,
textColor = 'black',
width = #child.formLabel,
value = child.formLabel,
})

View File

@@ -2,10 +2,8 @@ local class = require('opus.class')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
local os = _G.os
local _rep = string.rep
local _sub = string.sub
local function safeValue(v)
local t = type(v)
@@ -23,18 +21,7 @@ function Writer:init(element, y)
end
function Writer:write(s, width, align, bg, fg)
local len = #tostring(s or '')
if len > width then
s = _sub(s, 1, width)
end
local padding = len < width and _rep(' ', width - len)
if padding then
if align == 'right' then
s = padding .. s
else
s = s .. padding
end
end
s = Util.widthify(s, width, align)
self.element:write(self.x, self.y, s, bg, fg)
self.x = self.x + width
end
@@ -56,16 +43,16 @@ UI.Grid.defaults = {
disableHeader = false,
headerHeight = 1,
marginRight = 0,
textColor = colors.white,
textSelectedColor = colors.white,
backgroundColor = colors.black,
backgroundSelectedColor = colors.gray,
headerBackgroundColor = UI.colors.primary,
headerTextColor = colors.white,
headerSortColor = colors.yellow,
unfocusedTextSelectedColor = colors.white,
unfocusedBackgroundSelectedColor = colors.gray,
focusIndicator = UI.extChars and '\183' or '>',
textColor = 'white',
textSelectedColor = 'white',
backgroundColor = 'black',
backgroundSelectedColor = 'gray',
headerBackgroundColor = 'primary',
headerTextColor = 'white',
headerSortColor = 'yellow',
unfocusedTextSelectedColor = 'white',
unfocusedBackgroundSelectedColor = 'gray',
focusIndicator = UI.extChars and '\26' or '>',
sortIndicator = ' ',
inverseSortIndicator = UI.extChars and '\24' or '^',
values = { },
@@ -522,7 +509,7 @@ function UI.Grid.example()
values = values,
columns = {
{ heading = 'key', key = 'key', width = 6, },
{ heading = 'value', key = 'value', textColor = colors.yellow },
{ heading = 'value', key = 'value', textColor = 'yellow' },
},
},
autospace = UI.Grid {

View File

@@ -1,15 +1,13 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.MenuBar = class(UI.Window)
UI.MenuBar.defaults = {
UIElement = 'MenuBar',
buttons = { },
height = 1,
backgroundColor = UI.colors.secondary,
textColor = colors.black,
backgroundColor = 'secondary',
textColor = 'black',
spacing = 2,
lastx = 1,
buttonClass = 'MenuItem',

View File

@@ -5,5 +5,5 @@ UI.MenuItem = class(UI.FlatButton)
UI.MenuItem.defaults = {
UIElement = 'MenuItem',
noPadding = false,
textInactiveColor = colors.gray,
textInactiveColor = 'gray',
}

View File

@@ -5,7 +5,7 @@ UI.MiniSlideOut = class(UI.SlideOut)
UI.MiniSlideOut.defaults = {
UIElement = 'MiniSlideOut',
noFill = true,
backgroundColor = UI.colors.primary,
backgroundColor = 'primary',
height = 1,
}
function UI.MiniSlideOut:postInit()

View File

@@ -4,36 +4,34 @@ local Sound = require('opus.sound')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
UI.Notification = class(UI.Window)
UI.Notification.defaults = {
UIElement = 'Notification',
backgroundColor = colors.gray,
backgroundColor = 'gray',
closeInd = UI.extChars and '\215' or '*',
height = 3,
timeout = 3,
anchor = 'bottom',
}
function UI.Notification:draw()
function UI.Notification.draw()
end
function UI.Notification:enable()
function UI.Notification.enable()
end
function UI.Notification:error(value, timeout)
self.backgroundColor = colors.red
self.backgroundColor = 'red'
Sound.play('entity.villager.no', .5)
self:display(value, timeout)
end
function UI.Notification:info(value, timeout)
self.backgroundColor = colors.lightGray
self.backgroundColor = 'lightGray'
self:display(value, timeout)
end
function UI.Notification:success(value, timeout)
self.backgroundColor = colors.green
self.backgroundColor = 'green'
self:display(value, timeout)
end

View File

@@ -2,8 +2,6 @@ local class = require('opus.class')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
UI.Page = class(UI.Window)
UI.Page.defaults = {
UIElement = 'Page',
@@ -16,8 +14,8 @@ UI.Page.defaults = {
up = 'focus_prev',
scroll_up = 'focus_prev',
},
backgroundColor = UI.colors.primary,
textColor = colors.white,
backgroundColor = 'primary',
textColor = 'white',
}
function UI.Page:postInit()
self.parent = self.parent or UI.term

View File

@@ -1,35 +1,28 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.ProgressBar = class(UI.Window)
UI.ProgressBar.defaults = {
UIElement = 'ProgressBar',
backgroundColor = colors.gray,
backgroundColor = 'gray',
height = 1,
progressColor = colors.lime,
progressColor = 'lime',
progressChar = UI.extChars and '\153' or ' ',
fillChar = ' ',
fillColor = colors.gray,
textColor = colors.green,
fillColor = 'gray',
textColor = 'green',
value = 0,
}
function UI.ProgressBar:draw()
local width = math.ceil(self.value / 100 * self.width)
local filler = string.rep(self.fillChar, self.width)
local progress = string.rep(self.progressChar, width)
for i = 1, self.height do
self:write(1, i, filler, nil, self.fillColor)
self:write(1, i, progress, self.progressColor)
end
self:fillArea(width + 1, 1, self.width - width, self.height, self.fillChar, nil, self.fillColor)
self:fillArea(1, 1, width, self.height, self.progressChar, self.progressColor)
end
function UI.ProgressBar.example()
return UI.ProgressBar {
x = 2, ex = -2, y = 2,
x = 2, ex = -2, y = 2, height = 2,
focus = function() end,
enable = function(self)
require('opus.event').onInterval(.25, function()

View File

@@ -15,13 +15,13 @@ function UI.Question:postInit()
self.yes_button = UI.Button {
x = x,
text = 'Yes',
backgroundColor = UI.colors.primary,
backgroundColor = 'primary',
event = 'question_yes',
}
self.no_button = UI.Button {
x = x + 5,
text = 'No',
backgroundColor = UI.colors.primary,
backgroundColor = 'primary',
event = 'question_no',
}
end

View File

@@ -30,7 +30,7 @@ end
function UI.SlideOut:draw()
if not self.noFill then
self:fillArea(1, 1, self.width, self.height, string.rep('\127', self.width), colors.black, colors.gray)
self:fillArea(1, 1, self.width, self.height, string.rep('\127', self.width), 'black', 'gray')
end
self:drawChildren()
end

View File

@@ -2,17 +2,15 @@ local class = require('opus.class')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
UI.Slider = class(UI.Window)
UI.Slider.defaults = {
UIElement = 'Slider',
height = 1,
barChar = UI.extChars and '\140' or '-',
barColor = colors.gray,
barColor = 'gray',
sliderChar = UI.extChars and '\143' or '\124',
sliderColor = colors.blue,
sliderFocusColor = colors.lightBlue,
sliderColor = 'blue',
sliderFocusColor = 'lightBlue',
leftBorder = UI.extChars and '\141' or '\124',
rightBorder = UI.extChars and '\142' or '\124',
value = 0,

View File

@@ -3,13 +3,11 @@ local Event = require('opus.event')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
UI.StatusBar = class(UI.Window)
UI.StatusBar.defaults = {
UIElement = 'StatusBar',
backgroundColor = colors.lightGray,
textColor = colors.gray,
backgroundColor = 'lightGray',
textColor = 'gray',
height = 1,
ey = -1,
}

View File

@@ -6,9 +6,9 @@ UI.TabBar = class(UI.MenuBar)
UI.TabBar.defaults = {
UIElement = 'TabBar',
buttonClass = 'TabBarMenuItem',
backgroundColor = colors.black,
selectedBackgroundColor = UI.colors.primary,
unselectedBackgroundColor = UI.colors.tertiary,
backgroundColor = 'black',
selectedBackgroundColor = 'primary',
unselectedBackgroundColor = 'tertiary',
}
function UI.TabBar:enable()
UI.MenuBar.enable(self)

View File

@@ -5,7 +5,7 @@ UI.TabBarMenuItem = class(UI.Button)
UI.TabBarMenuItem.defaults = {
UIElement = 'TabBarMenuItem',
event = 'tab_select',
textInactiveColor = colors.lightGray,
textInactiveColor = 'lightGray',
}
function UI.TabBarMenuItem:draw()
if self.selected then

View File

@@ -14,7 +14,7 @@ function UI.TextArea:setText(text)
self:draw()
end
function UI.TextArea:focus()
function UI.TextArea.focus()
-- allow keyboard scrolling
end
@@ -25,6 +25,7 @@ function UI.TextArea:draw()
end
function UI.TextArea.example()
local Ansi = require('opus.ansi')
return UI.Window {
backgroundColor = 2048,
t1 = UI.TextArea {
@@ -33,14 +34,16 @@ function UI.TextArea.example()
},
t2 = UI.TextArea {
y = 5,
value = [[1
backgroundColor = 'green',
value = string.format([[now %%is the %stime %sfor%s all good men to come to the aid of their country.
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
3
4
5
6
7
8]]
8]], Ansi.yellow, Ansi.onred, Ansi.reset),
}
}
end

View File

@@ -3,7 +3,6 @@ local entry = require('opus.entry')
local UI = require('opus.ui')
local Util = require('opus.util')
local colors = _G.colors
local _rep = string.rep
local function transform(directive)
@@ -21,11 +20,11 @@ UI.TextEntry.defaults = {
UIElement = 'TextEntry',
shadowText = '',
focused = false,
textColor = colors.white,
shadowTextColor = colors.gray,
markBackgroundColor = colors.gray,
backgroundColor = colors.black, -- colors.lightGray,
backgroundFocusColor = colors.black, --lightGray,
textColor = 'white',
shadowTextColor = 'gray',
markBackgroundColor = 'gray',
backgroundColor = 'black',
backgroundFocusColor = 'black',
height = 1,
limit = 6,
cursorBlink = true,

View File

@@ -1,39 +1,6 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
local _rep = string.rep
local _sub = string.sub
-- For manipulating text in a fixed width string
local SB = class()
function SB:init(width)
self.width = width
self.buf = _rep(' ', width)
end
function SB:insert(x, str, width)
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
end
function SB:fill(x, ch, width)
width = width or self.width - x + 1
self:insert(x, _rep(ch, width))
end
function SB:center(str)
self:insert(math.max(1, math.ceil((self.width - #str + 1) / 2)), str)
end
function SB:get()
return self.buf
end
UI.TitleBar = class(UI.Window)
UI.TitleBar.defaults = {
UIElement = 'TitleBar',
@@ -43,15 +10,11 @@ UI.TitleBar.defaults = {
closeInd = UI.extChars and '\215' or '*',
}
function UI.TitleBar:draw()
local sb = SB(self.width)
sb:fill(2, self.frameChar, sb.width - 3)
sb:center(string.format(' %s ', self.title))
self:fillArea(2, 1, self.width - 2, 1, self.frameChar)
self:centeredWrite(1, string.format(' %s ', self.title))
if self.previousPage or self.event then
sb:insert(-1, self.closeInd)
else
sb:insert(-2, self.frameChar)
self:write(self.width - 1, 1, ' ' .. self.closeInd)
end
self:write(1, 1, sb:get())
end
function UI.TitleBar:eventHandler(event)
@@ -78,7 +41,7 @@ function UI.TitleBar:eventHandler(event)
self.parent:reposition(self.parent.x, self.anchor.oy + event.dy, self.width, self.anchor.h - d)
end
else --if self.moveable then
elseif self.moveable then
local d = event.dy
if self.anchor.oy + d > 0 and self.anchor.oy + d <= self.parent.parent.height then
self.parent:move(self.anchor.ox + event.dx, self.anchor.oy + event.dy)
@@ -91,9 +54,9 @@ function UI.TitleBar.example()
return UI.Window {
win1 = UI.Window {
x = 9, y = 2, ex = -7, ey = -3,
backgroundColor = colors.green,
backgroundColor = 'green',
titleBar = UI.TitleBar {
title = 'test', moveable = true,
title = 'A really, really, really long title', moveable = true,
},
button1 = UI.Button {
x = 2, y = 3,
@@ -105,9 +68,10 @@ function UI.TitleBar.example()
},
win2 = UI.Window {
x = 7, y = 3, ex = -9, ey = -2,
backgroundColor = colors.orange,
backgroundColor = 'orange',
titleBar = UI.TitleBar {
title = 'test', moveable = true,
event = 'none',
},
button1 = UI.Button {
x = 2, y = 3,

View File

@@ -1,13 +1,11 @@
local class = require('opus.class')
local UI = require('opus.ui')
local colors = _G.colors
UI.VerticalMeter = class(UI.Window)
UI.VerticalMeter.defaults = {
UIElement = 'VerticalMeter',
backgroundColor = colors.gray,
meterColor = colors.lime,
backgroundColor = 'gray',
meterColor = 'lime',
width = 1,
value = 0,
}
@@ -30,4 +28,4 @@ function UI.VerticalMeter.example()
return UI.VerticalMeter.enable(self)
end
}
end
end