Merge b369f47e69
into 727c925f1b
This commit is contained in:
commit
e9eacee1dd
7 changed files with 69 additions and 21 deletions
1
core.lua
1
core.lua
|
@ -22,6 +22,7 @@ function suit.new(theme)
|
||||||
Checkbox = require(BASE.."checkbox"),
|
Checkbox = require(BASE.."checkbox"),
|
||||||
Input = require(BASE.."input"),
|
Input = require(BASE.."input"),
|
||||||
Slider = require(BASE.."slider"),
|
Slider = require(BASE.."slider"),
|
||||||
|
ProgressBar = require(BASE.."progressbar"),
|
||||||
|
|
||||||
layout = require(BASE.."layout").new(),
|
layout = require(BASE.."layout").new(),
|
||||||
}, suit)
|
}, suit)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
|
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
|
||||||
|
local floor = math.floor;
|
||||||
|
|
||||||
local BASE = (...):match('(.-)[^%.]+$')
|
local BASE = (...):match('(.-)[^%.]+$')
|
||||||
|
|
||||||
|
@ -13,7 +14,7 @@ return function(core, normal, ...)
|
||||||
opt.state = core:registerMouseHit(opt.id, x,y, function(u,v)
|
opt.state = core:registerMouseHit(opt.id, x,y, function(u,v)
|
||||||
local id = opt.normal:getData()
|
local id = opt.normal:getData()
|
||||||
assert(id:typeOf("ImageData"), "Can only use uncompressed images")
|
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
|
if u < 0 or u >= opt.normal:getWidth() or v < 0 or v >= opt.normal:getHeight() then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
15
input.lua
15
input.lua
|
@ -1,4 +1,5 @@
|
||||||
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
|
-- 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 BASE = (...):match('(.-)[^%.]+$')
|
||||||
local utf8 = require 'utf8'
|
local utf8 = require 'utf8'
|
||||||
|
@ -18,7 +19,7 @@ return function(core, input, ...)
|
||||||
h = h or opt.font:getHeight() + 4
|
h = h or opt.font:getHeight() + 4
|
||||||
|
|
||||||
input.text = input.text or ""
|
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":
|
-- cursor is position *before* the character (including EOS) i.e. in "hello":
|
||||||
-- position 1: |hello
|
-- position 1: |hello
|
||||||
-- position 2: h|ello
|
-- position 2: h|ello
|
||||||
|
@ -62,26 +63,26 @@ return function(core, input, ...)
|
||||||
-- text input
|
-- text input
|
||||||
if char and char ~= "" then
|
if char and char ~= "" then
|
||||||
local a,b = split(input.text, input.cursor)
|
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)
|
input.cursor = input.cursor + utf8.len(char)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- text editing
|
-- text editing
|
||||||
if keycode == 'backspace' then
|
if keycode == 'backspace' then
|
||||||
local a,b = split(input.text, input.cursor)
|
local a,b = split(input.text, input.cursor)
|
||||||
input.text = table.concat{split(a,utf8.len(a)), b}
|
input.text = concat{split(a,utf8.len(a)), b}
|
||||||
input.cursor = math.max(1, input.cursor-1)
|
input.cursor = max(1, input.cursor-1)
|
||||||
elseif keycode == 'delete' then
|
elseif keycode == 'delete' then
|
||||||
local a,b = split(input.text, input.cursor)
|
local a,b = split(input.text, input.cursor)
|
||||||
local _,b = split(b, 2)
|
local _,b = split(b, 2)
|
||||||
input.text = table.concat{a, b}
|
input.text = concat{a, b}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- cursor movement
|
-- cursor movement
|
||||||
if keycode =='left' then
|
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
|
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
|
elseif keycode =='home' then -- cursor movement
|
||||||
input.cursor = 1
|
input.cursor = 1
|
||||||
elseif keycode =='end' then -- cursor movement
|
elseif keycode =='end' then -- cursor movement
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
|
-- 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 = {}
|
local Layout = {}
|
||||||
function Layout.new(x,y,padx,pady)
|
function Layout.new(x,y,padx,pady)
|
||||||
|
@ -65,7 +66,7 @@ function Layout:pop()
|
||||||
self._isFirstCell = false
|
self._isFirstCell = false
|
||||||
self._stack[#self._stack] = nil
|
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
|
return w, h
|
||||||
end
|
end
|
||||||
|
@ -77,7 +78,7 @@ local function insert_sorted_helper(t, i0, i1, v)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local i = i0 + math.floor((i1-i0)/2)
|
local i = i0 + floor((i1-i0)/2)
|
||||||
if t[i] < v then
|
if t[i] < v then
|
||||||
return insert_sorted_helper(t, i+1, i1, v)
|
return insert_sorted_helper(t, i+1, i1, v)
|
||||||
elseif t[i] > v then
|
elseif t[i] > v then
|
||||||
|
@ -100,7 +101,7 @@ local function calc_width_height(self, w, h)
|
||||||
elseif w == "min" then
|
elseif w == "min" then
|
||||||
w = self._widths[1]
|
w = self._widths[1]
|
||||||
elseif w == "median" then
|
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
|
elseif type(w) ~= "number" then
|
||||||
error("width: invalid value (" .. tostring(w) .. ")", 3)
|
error("width: invalid value (" .. tostring(w) .. ")", 3)
|
||||||
end
|
end
|
||||||
|
@ -112,7 +113,7 @@ local function calc_width_height(self, w, h)
|
||||||
elseif h == "min" then
|
elseif h == "min" then
|
||||||
h = self._heights[1]
|
h = self._heights[1]
|
||||||
elseif h == "median" then
|
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
|
elseif type(h) ~= "number" then
|
||||||
error("width: invalid value (" .. tostring(w) .. ")", 3)
|
error("width: invalid value (" .. tostring(w) .. ")", 3)
|
||||||
end
|
end
|
||||||
|
|
26
progressbar.lua
Normal file
26
progressbar.lua
Normal 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
|
13
slider.lua
13
slider.lua
|
@ -1,14 +1,15 @@
|
||||||
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
|
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
|
||||||
|
|
||||||
local BASE = (...):match('(.-)[^%.]+$')
|
local BASE = (...):match('(.-)[^%.]+$')
|
||||||
|
local min, max = math.min, math.max;
|
||||||
|
|
||||||
return function(core, info, ...)
|
return function(core, info, ...)
|
||||||
local opt, x,y,w,h = core.getOptionsAndSize(...)
|
local opt, x,y,w,h = core.getOptionsAndSize(...)
|
||||||
|
|
||||||
opt.id = opt.id or info
|
opt.id = opt.id or info
|
||||||
|
|
||||||
info.min = info.min or math.min(info.value, 0)
|
info.min = info.min or min(info.value, 0)
|
||||||
info.max = info.max or math.max(info.value, 1)
|
info.max = info.max or max(info.value, 1)
|
||||||
info.step = info.step or (info.max - info.min) / 10
|
info.step = info.step or (info.max - info.min) / 10
|
||||||
local fraction = (info.value - info.min) / (info.max - info.min)
|
local fraction = (info.value - info.min) / (info.max - info.min)
|
||||||
local value_changed = false
|
local value_changed = false
|
||||||
|
@ -19,9 +20,9 @@ return function(core, info, ...)
|
||||||
-- mouse update
|
-- mouse update
|
||||||
local mx,my = core:getMousePosition()
|
local mx,my = core:getMousePosition()
|
||||||
if opt.vertical then
|
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
|
else
|
||||||
fraction = math.min(1, math.max(0, (mx - x) / w))
|
fraction = min(1, max(0, (mx - x) / w))
|
||||||
end
|
end
|
||||||
local v = fraction * (info.max - info.min) + info.min
|
local v = fraction * (info.max - info.min) + info.min
|
||||||
if v ~= info.value then
|
if v ~= info.value then
|
||||||
|
@ -33,10 +34,10 @@ return function(core, info, ...)
|
||||||
local key_up = opt.vertical and 'up' or 'right'
|
local key_up = opt.vertical and 'up' or 'right'
|
||||||
local key_down = opt.vertical and 'down' or 'left'
|
local key_down = opt.vertical and 'down' or 'left'
|
||||||
if core:getPressedKey() == key_up then
|
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
|
value_changed = true
|
||||||
elseif core:getPressedKey() == key_down then
|
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
|
value_changed = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
23
theme.lua
23
theme.lua
|
@ -1,6 +1,7 @@
|
||||||
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
|
-- This file is part of SUIT, copyright (c) 2016 Matthias Richter
|
||||||
|
|
||||||
local BASE = (...):match('(.-)[^%.]+$')
|
local BASE = (...):match('(.-)[^%.]+$')
|
||||||
|
local min, max = math.min, math.max;
|
||||||
|
|
||||||
local theme = {}
|
local theme = {}
|
||||||
theme.cornerRadius = 4
|
theme.cornerRadius = 4
|
||||||
|
@ -19,9 +20,9 @@ function theme.getColorForState(opt)
|
||||||
end
|
end
|
||||||
|
|
||||||
function theme.drawBox(x,y,w,h, colors, cornerRadius)
|
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
|
cornerRadius = cornerRadius or theme.cornerRadius
|
||||||
w = math.max(cornerRadius/2, w)
|
w = max(cornerRadius/2, w)
|
||||||
if h < cornerRadius/2 then
|
if h < cornerRadius/2 then
|
||||||
y,h = y - (cornerRadius - h), cornerRadius/2
|
y,h = y - (cornerRadius - h), cornerRadius/2
|
||||||
end
|
end
|
||||||
|
@ -82,7 +83,7 @@ end
|
||||||
|
|
||||||
function theme.Slider(fraction, opt, x,y,w,h)
|
function theme.Slider(fraction, opt, x,y,w,h)
|
||||||
local xb, yb, wb, hb -- size of the progress bar
|
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
|
if opt.vertical then
|
||||||
x, w = x + w*.25, w*.5
|
x, w = x + w*.25, w*.5
|
||||||
xb, yb, wb, hb = x, y+h*(1-fraction), w, h*fraction
|
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
|
||||||
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)
|
function theme.Input(input, opt, x,y,w,h)
|
||||||
local utf8 = require 'utf8'
|
local utf8 = require 'utf8'
|
||||||
theme.drawBox(x,y,w,h, (opt.color and opt.color.normal) or theme.color.normal, opt.cornerRadius)
|
theme.drawBox(x,y,w,h, (opt.color and opt.color.normal) or theme.color.normal, opt.cornerRadius)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue