From 40dbc7134bae5cff48c2262d92721a3dd5394f84 Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Tue, 7 Feb 2012 23:10:29 +0100 Subject: Initial commit --- slider.lua | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 slider.lua (limited to 'slider.lua') diff --git a/slider.lua b/slider.lua new file mode 100644 index 0000000..ae75c01 --- /dev/null +++ b/slider.lua @@ -0,0 +1,44 @@ +local core = require((...):match("^(.+)%.[^%.]+") .. '.core') + +return function(info, x,y,w,h, draw) + assert(type(info) == 'table' and info.value, "Incomplete slider value info") + info.min = info.min or 0 + info.max = info.max or math.max(info.value, 1) + info.step = info.step or (info.max - info.min) / 50 + local fraction = (info.value - info.min) / (info.max - info.min) + + local id = core.generateID() + core.mouse.updateState(id, x,y,w,h) + core.makeTabable(id) + core.registerDraw(id,draw or core.style.Slider, fraction, x,y,w,h, info.vertical) + + -- mouse update + if core.isActive(id) then + core.setKeyFocus(id) + if info.vertical then + fraction = math.min(1, math.max(0, (y - core.mouse.y + h) / h)) + else + fraction = math.min(1, math.max(0, (core.mouse.x - x) / w)) + end + local v = fraction * (info.max - info.min) + info.min + if v ~= info.value then + info.value = v + return true + end + end + + -- keyboard update + local changed = false + if core.hasKeyFocus(id) then + local keys = info.vertical and {'up', 'down'} or {'right', 'left'} + if core.keyboard.key == keys[1] then + info.value = math.min(info.max, info.value + info.step) + changed = true + elseif core.keyboard.key == keys[2] then + info.value = math.max(info.min, info.value - info.step) + changed = true + end + end + + return changed +end -- cgit v1.2.3-54-g00ecf