From 64f848c9d0efcde77814d7637dcf4e01d8e0f2d6 Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt Date: Sat, 5 Nov 2016 19:16:18 +0100 Subject: [PATCH 1/4] removed local variable colors parameter "colors" can be used like a local variable. --- theme.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/theme.lua b/theme.lua index fe8b416..c052348 100644 --- a/theme.lua +++ b/theme.lua @@ -19,7 +19,7 @@ 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) if h < cornerRadius/2 then From 6286203075e9212b5f5ed2d2d0eda26a18e25665 Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt Date: Sat, 5 Nov 2016 20:08:01 +0100 Subject: [PATCH 2/4] added a progressbar as a immutable widget progressbar is a stripped down version of slider.lua. --- core.lua | 1 + progressbar.lua | 26 ++++++++++++++++++++++++++ theme.lua | 16 ++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 progressbar.lua diff --git a/core.lua b/core.lua index 6c32fbd..0395afa 100644 --- a/core.lua +++ b/core.lua @@ -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) diff --git a/progressbar.lua b/progressbar.lua new file mode 100644 index 0000000..c849646 --- /dev/null +++ b/progressbar.lua @@ -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 diff --git a/theme.lua b/theme.lua index c052348..5f6d3b6 100644 --- a/theme.lua +++ b/theme.lua @@ -105,6 +105,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 = math.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) From aad95de809a58c3ebb0f69b3104bc7ad19b50cee Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt Date: Sun, 6 Nov 2016 09:10:10 +0100 Subject: [PATCH 3/4] cached library functions in local vars --- imagebutton.lua | 3 ++- input.lua | 15 ++++++++------- layout.lua | 9 +++++---- slider.lua | 13 +++++++------ theme.lua | 7 ++++--- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/imagebutton.lua b/imagebutton.lua index 0cf544a..e280744 100644 --- a/imagebutton.lua +++ b/imagebutton.lua @@ -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 diff --git a/input.lua b/input.lua index 5262c2b..d60b1c1 100644 --- a/input.lua +++ b/input.lua @@ -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 @@ -57,26 +58,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 diff --git a/layout.lua b/layout.lua index 58c7034..ffd6258 100644 --- a/layout.lua +++ b/layout.lua @@ -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) @@ -73,7 +74,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 @@ -85,7 +86,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 @@ -108,7 +109,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 @@ -120,7 +121,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 diff --git a/slider.lua b/slider.lua index d540348..fdacc66 100644 --- a/slider.lua +++ b/slider.lua @@ -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 diff --git a/theme.lua b/theme.lua index 5f6d3b6..446fb42 100644 --- a/theme.lua +++ b/theme.lua @@ -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 @@ -21,7 +22,7 @@ end function theme.drawBox(x,y,w,h, colors, cornerRadius) 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 @@ -107,7 +108,7 @@ end function theme.ProgressBar(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 From b369f47e6960f490af624925250d24dad18b1d9e Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt Date: Sun, 6 Nov 2016 20:09:03 +0100 Subject: [PATCH 4/4] bugfix: no function parameter "opt" removed wrong code line --- theme.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/theme.lua b/theme.lua index 446fb42..433f146 100644 --- a/theme.lua +++ b/theme.lua @@ -20,7 +20,7 @@ function theme.getColorForState(opt) end function theme.drawBox(x,y,w,h, colors, cornerRadius) - colors = colors or theme.getColorForState(opt) + --colors = colors or theme.getColorForState(opt) cornerRadius = cornerRadius or theme.cornerRadius w = max(cornerRadius/2, w) if h < cornerRadius/2 then