From adc78875875a64d40b4cf65a8618fb49a77e8080 Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Wed, 9 May 2012 21:27:45 +0200 Subject: Mega update: Auto layout, code cleanup, api change. Basically half a rewrite. --- checkbox.lua | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) (limited to 'checkbox.lua') diff --git a/checkbox.lua b/checkbox.lua index 22c325a..e04f6a6 100644 --- a/checkbox.lua +++ b/checkbox.lua @@ -23,22 +23,46 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]]-- -local core = require((...):match("(.-)[^%.]+$") .. 'core') +local BASE = (...):match("(.-)[^%.]+$") +local core = require(BASE .. 'core') +local group = require(BASE .. 'group') +local mouse = require(BASE .. 'mouse') +local keyboard = require(BASE .. 'keyboard') + +-- {info = {checked = status, label = "", algin = "left"}, pos = {x, y}, size={w, h}, widgetHit=widgetHit, draw=draw} +return function(w) + assert(type(w) == "table") + w.info.label = w.info.label or "" + + local tight = w.size and (w.size[1] == 'tight' or w.size[2] == 'tight') + if tight then + local f = assert(love.graphics.getFont()) + if w.size[1] == 'tight' then + w.size[1] = f:getWidth(w.info.label) + end + if w.size[2] == 'tight' then + w.size[2] = f:getHeight(w.info.label) + end + -- account for the checkbox + local bw = math.min(w.size[1] or group.size[1], w.size[2] or group.size[2]) + w.size[1] = w.size[1] + bw + 4 + end -return function(info, x,y, w,h, widgetHit, draw) local id = core.generateID() + local pos, size = group.getRect(w.pos, w.size) - core.mouse.updateState(id, widgetHit or core.style.widgetHit, x,y,w,h) - core.makeCyclable(id) - core.registerDraw(id, draw or core.style.Checkbox, info.checked,x,y,w,h) + mouse.updateWidget(id, pos, size, w.widgetHit) + keyboard.makeCyclable(id) - local checked = info.checked - local key = core.keyboard.key - if core.mouse.releasedOn(id) or - (core.hasKeyFocus(id) and key == 'return' or key == ' ') then - info.checked = not info.checked + local checked = w.info.checked + local key = keyboard.key + if mouse.releasedOn(id) or ((key == 'return' or key == ' ') and keyboard.hasFocus(id)) then + w.info.checked = not w.info.checked end - return info.checked ~= checked + core.registerDraw(id, w.draw or core.style.Checkbox, + w.info.checked, w.info.label, w.info.align or 'left', pos[1], pos[2], size[1], size[2]) + + return w.info.checked ~= checked end -- cgit v1.2.3-54-g00ecf