cached library functions in local vars

This commit is contained in:
Ulrich Schmidt 2016-11-06 09:10:10 +01:00
parent 6286203075
commit aad95de809
5 changed files with 26 additions and 21 deletions

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
@ -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

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)
@ -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

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
@ -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