From 6286203075e9212b5f5ed2d2d0eda26a18e25665 Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt Date: Sat, 5 Nov 2016 20:08:01 +0100 Subject: [PATCH] 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)