This commit is contained in:
Ulrich Schmidt 2017-03-16 19:22:55 +00:00 committed by GitHub
commit e9eacee1dd
7 changed files with 69 additions and 21 deletions

View file

@ -22,6 +22,7 @@ function suit.new(theme)
Checkbox = require(BASE.."checkbox"),
Input = require(BASE.."input"),
Slider = require(BASE.."slider"),
ProgressBar = require(BASE.."progressbar"),
layout = require(BASE.."layout").new(),
}, suit)

View file

@ -1,4 +1,5 @@
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
local floor = math.floor;
local BASE = (...):match('(.-)[^%.]+$')
@ -13,7 +14,7 @@ return function(core, normal, ...)
opt.state = core:registerMouseHit(opt.id, x,y, function(u,v)
local id = opt.normal:getData()
assert(id:typeOf("ImageData"), "Can only use uncompressed images")
u, v = math.floor(u+.5), math.floor(v+.5)
u, v = floor(u+.5), floor(v+.5)
if u < 0 or u >= opt.normal:getWidth() or v < 0 or v >= opt.normal:getHeight() then
return false
end

View file

@ -1,4 +1,5 @@
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
local min, max, concat = math.min, math.max, table.concat;
local BASE = (...):match('(.-)[^%.]+$')
local utf8 = require 'utf8'
@ -18,7 +19,7 @@ return function(core, input, ...)
h = h or opt.font:getHeight() + 4
input.text = input.text or ""
input.cursor = math.max(1, math.min(utf8.len(input.text)+1, input.cursor or utf8.len(input.text)+1))
input.cursor = max(1, min(utf8.len(input.text)+1, input.cursor or utf8.len(input.text)+1))
-- cursor is position *before* the character (including EOS) i.e. in "hello":
-- position 1: |hello
-- position 2: h|ello
@ -62,26 +63,26 @@ return function(core, input, ...)
-- text input
if char and char ~= "" then
local a,b = split(input.text, input.cursor)
input.text = table.concat{a, char, b}
input.text = concat{a, char, b}
input.cursor = input.cursor + utf8.len(char)
end
-- text editing
if keycode == 'backspace' then
local a,b = split(input.text, input.cursor)
input.text = table.concat{split(a,utf8.len(a)), b}
input.cursor = math.max(1, input.cursor-1)
input.text = concat{split(a,utf8.len(a)), b}
input.cursor = max(1, input.cursor-1)
elseif keycode == 'delete' then
local a,b = split(input.text, input.cursor)
local _,b = split(b, 2)
input.text = table.concat{a, b}
input.text = concat{a, b}
end
-- cursor movement
if keycode =='left' then
input.cursor = math.max(0, input.cursor-1)
input.cursor = max(0, input.cursor-1)
elseif keycode =='right' then -- cursor movement
input.cursor = math.min(utf8.len(input.text)+1, input.cursor+1)
input.cursor = min(utf8.len(input.text)+1, input.cursor+1)
elseif keycode =='home' then -- cursor movement
input.cursor = 1
elseif keycode =='end' then -- cursor movement

View file

@ -1,4 +1,5 @@
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil;
local Layout = {}
function Layout.new(x,y,padx,pady)
@ -65,7 +66,7 @@ function Layout:pop()
self._isFirstCell = false
self._stack[#self._stack] = nil
self._w, self._h = math.max(w, self._w or 0), math.max(h, self._h or 0)
self._w, self._h = max(w, self._w or 0), max(h, self._h or 0)
return w, h
end
@ -77,7 +78,7 @@ local function insert_sorted_helper(t, i0, i1, v)
return
end
local i = i0 + math.floor((i1-i0)/2)
local i = i0 + floor((i1-i0)/2)
if t[i] < v then
return insert_sorted_helper(t, i+1, i1, v)
elseif t[i] > v then
@ -100,7 +101,7 @@ local function calc_width_height(self, w, h)
elseif w == "min" then
w = self._widths[1]
elseif w == "median" then
w = self._widths[math.ceil(#self._widths/2)] or 0
w = self._widths[ceil(#self._widths/2)] or 0
elseif type(w) ~= "number" then
error("width: invalid value (" .. tostring(w) .. ")", 3)
end
@ -112,7 +113,7 @@ local function calc_width_height(self, w, h)
elseif h == "min" then
h = self._heights[1]
elseif h == "median" then
h = self._heights[math.ceil(#self._heights/2)] or 0
h = self._heights[ceil(#self._heights/2)] or 0
elseif type(h) ~= "number" then
error("width: invalid value (" .. tostring(w) .. ")", 3)
end

26
progressbar.lua Normal file
View file

@ -0,0 +1,26 @@
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
local BASE = (...):match('(.-)[^%.]+$')
local min, max = math.min, math.max;
return function(core, info, ...)
local opt, x,y,w,h = core.getOptionsAndSize(...)
opt.id = opt.id or info
info.min = info.min or min(info.value, 0)
info.max = info.max or max(info.value, 1)
info.step = info.step or (info.max - info.min) / 10
local fraction = (info.value - info.min) / (info.max - info.min)
opt.state = core:registerHitbox(opt.id, x,y,w,h)
core:registerDraw(opt.draw or core.theme.ProgressBar, fraction, opt, x,y,w,h)
return {
id = opt.id,
hovered = core:isHovered(opt.id),
entered = core:isHovered(opt.id) and not core:wasHovered(opt.id),
left = not core:isHovered(opt.id) and core:wasHovered(opt.id)
}
end

View file

@ -1,14 +1,15 @@
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
local BASE = (...):match('(.-)[^%.]+$')
local min, max = math.min, math.max;
return function(core, info, ...)
local opt, x,y,w,h = core.getOptionsAndSize(...)
opt.id = opt.id or info
info.min = info.min or math.min(info.value, 0)
info.max = info.max or math.max(info.value, 1)
info.min = info.min or min(info.value, 0)
info.max = info.max or max(info.value, 1)
info.step = info.step or (info.max - info.min) / 10
local fraction = (info.value - info.min) / (info.max - info.min)
local value_changed = false
@ -19,9 +20,9 @@ return function(core, info, ...)
-- mouse update
local mx,my = core:getMousePosition()
if opt.vertical then
fraction = math.min(1, math.max(0, (y+h - my) / h))
fraction = min(1, max(0, (y+h - my) / h))
else
fraction = math.min(1, math.max(0, (mx - x) / w))
fraction = min(1, max(0, (mx - x) / w))
end
local v = fraction * (info.max - info.min) + info.min
if v ~= info.value then
@ -33,10 +34,10 @@ return function(core, info, ...)
local key_up = opt.vertical and 'up' or 'right'
local key_down = opt.vertical and 'down' or 'left'
if core:getPressedKey() == key_up then
info.value = math.min(info.max, info.value + info.step)
info.value = min(info.max, info.value + info.step)
value_changed = true
elseif core:getPressedKey() == key_down then
info.value = math.max(info.min, info.value - info.step)
info.value = max(info.min, info.value - info.step)
value_changed = true
end
end

View file

@ -1,6 +1,7 @@
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
local BASE = (...):match('(.-)[^%.]+$')
local min, max = math.min, math.max;
local theme = {}
theme.cornerRadius = 4
@ -19,9 +20,9 @@ function theme.getColorForState(opt)
end
function theme.drawBox(x,y,w,h, colors, cornerRadius)
local colors = colors or theme.getColorForState(opt)
--colors = colors or theme.getColorForState(opt)
cornerRadius = cornerRadius or theme.cornerRadius
w = math.max(cornerRadius/2, w)
w = max(cornerRadius/2, w)
if h < cornerRadius/2 then
y,h = y - (cornerRadius - h), cornerRadius/2
end
@ -82,7 +83,7 @@ end
function theme.Slider(fraction, opt, x,y,w,h)
local xb, yb, wb, hb -- size of the progress bar
local r = math.min(w,h) / 2.1
local r = min(w,h) / 2.1
if opt.vertical then
x, w = x + w*.25, w*.5
xb, yb, wb, hb = x, y+h*(1-fraction), w, h*fraction
@ -105,6 +106,22 @@ function theme.Slider(fraction, opt, x,y,w,h)
end
end
function theme.ProgressBar(fraction, opt, x,y,w,h)
local xb, yb, wb, hb -- size of the progress bar
local r = min(w,h) / 2.1
if opt.vertical then
x, w = x + w*.25, w*.5
xb, yb, wb, hb = x, y+h*(1-fraction), w, h*fraction
else
y, h = y + h*.25, h*.5
xb, yb, wb, hb = x,y, w*fraction, h
end
local c = opt.color and opt.color.normal or theme.color.normal
theme.drawBox(x,y,w,h, c, opt.cornerRadius)
theme.drawBox(xb,yb,wb,hb, {bg=c.fg}, opt.cornerRadius)
end
function theme.Input(input, opt, x,y,w,h)
local utf8 = require 'utf8'
theme.drawBox(x,y,w,h, (opt.color and opt.color.normal) or theme.color.normal, opt.cornerRadius)