From 6de65888dc6575a00d4e16defe92bcb0ccf08423 Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Wed, 14 Mar 2012 16:19:46 +0100 Subject: Externalize widget hit test to style definition. --- button.lua | 6 +++--- checkbox.lua | 4 ++-- core.lua | 8 ++------ input.lua | 4 ++-- slider.lua | 4 ++-- slider2d.lua | 4 ++-- style-default.lua | 5 +++++ 7 files changed, 18 insertions(+), 17 deletions(-) diff --git a/button.lua b/button.lua index b601458..8d76b1f 100644 --- a/button.lua +++ b/button.lua @@ -1,7 +1,7 @@ local core = require((...):match("(.-)[^%.]+$") .. 'core') -- the widget -return function(title, x,y, w,h, draw) +return function(title, x,y, w,h, widgetHit, draw) -- Generate unique identifier for gui state update and querying. local id = core.generateID() @@ -10,8 +10,8 @@ return function(title, x,y, w,h, draw) -- active (mouse pressed on widget) or -- normal (mouse not on widget and not pressed on widget). -- - -- core.mouse.updateState(id, x,y,w,h) updates the state for this widget. - core.mouse.updateState(id, x,y,w,h) + -- core.mouse.updateState(id, widgetHit, x,y,w,h) updates the state for this widget. + core.mouse.updateState(id, widgetHit or core.style.widgetHit, x,y,w,h) -- core.makeCyclable makes the item focus on tab or whatever binding is -- in place (see core.keyboard.cycle). Cycle order is determied by the diff --git a/checkbox.lua b/checkbox.lua index b51cbb3..db282a7 100644 --- a/checkbox.lua +++ b/checkbox.lua @@ -1,9 +1,9 @@ local core = require((...):match("(.-)[^%.]+$") .. 'core') -return function(info, x,y, w,h, draw) +return function(info, x,y, w,h, widgetHit, draw) local id = core.generateID() - core.mouse.updateState(id, x,y,w,h) + 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) diff --git a/core.lua b/core.lua index a52f7cd..d97e83a 100644 --- a/core.lua +++ b/core.lua @@ -29,12 +29,8 @@ keyboard.cycle = { next = {key = 'tab'}, } -function mouse.inRect(x,y,w,h) - return mouse.x >= x and mouse.x <= x+w and mouse.y >= y and mouse.y <= y+h -end - -function mouse.updateState(id, x,y,w,h) - if mouse.inRect(x,y,w,h) then +function mouse.updateState(id, widgetHit, ...) + if widgetHit(mouse.x, mouse.y, ...) then setHot(id) if not context.active and mouse.down then setActive(id) diff --git a/input.lua b/input.lua index d514348..688ce5f 100644 --- a/input.lua +++ b/input.lua @@ -1,11 +1,11 @@ local core = require((...):match("(.-)[^%.]+$") .. 'core') -return function(info, x,y,w,h, draw) +return function(info, x,y,w,h, widgetHit, draw) info.text = info.text or "" info.cursor = math.min(info.cursor or info.text:len(), info.text:len()) local id = core.generateID() - core.mouse.updateState(id, x,y,w,h) + core.mouse.updateState(id, widgetHit or core.style.widgetHit, x,y,w,h) core.makeCyclable(id) if core.isActive(id) then core.setKeyFocus(id) end diff --git a/slider.lua b/slider.lua index 7e4a6d9..749049a 100644 --- a/slider.lua +++ b/slider.lua @@ -1,6 +1,6 @@ local core = require((...):match("(.-)[^%.]+$") .. 'core') -return function(info, x,y,w,h, draw) +return function(info, x,y,w,h, widgetHit, 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) @@ -8,7 +8,7 @@ return function(info, x,y,w,h, draw) local fraction = (info.value - info.min) / (info.max - info.min) local id = core.generateID() - core.mouse.updateState(id, x,y,w,h) + core.mouse.updateState(id, widgetHit or core.style.widgetHit, x,y,w,h) core.makeCyclable(id) core.registerDraw(id,draw or core.style.Slider, fraction, x,y,w,h, info.vertical) diff --git a/slider2d.lua b/slider2d.lua index 8614113..3a97ad9 100644 --- a/slider2d.lua +++ b/slider2d.lua @@ -1,6 +1,6 @@ local core = require((...):match("(.-)[^%.]+$") .. 'core') -return function(info, x,y,w,h, draw) +return function(info, x,y,w,h, widgetHit, draw) assert(type(info) == 'table' and type(info.value) == "table", "Incomplete slider value info") info.min = info.min or {x = 0, y = 0} info.max = info.max or {x = math.max(info.value.x or 0, 1), y = math.max(info.value.y or 0, 1)} @@ -11,7 +11,7 @@ return function(info, x,y,w,h, draw) } local id = core.generateID() - core.mouse.updateState(id, x,y,w,h) + core.mouse.updateState(id, widgetHit or core.style.widgetHit, x,y,w,h) core.makeCyclable(id) core.registerDraw(id,draw or core.style.Slider2D, fraction, x,y,w,h) diff --git a/style-default.lua b/style-default.lua index 5ae9dda..4a10f92 100644 --- a/style-default.lua +++ b/style-default.lua @@ -10,6 +10,10 @@ if not love.graphics.getFont() then love.graphics.setFont(love.graphics.newFont(12)) end +local function widgetHit(xx,yy, x,y,w,h) + return xx >= x and xx <= x+w and yy >= y and yy <= y+h +end + local function Button(state, title, x,y,w,h) local c = color[state] love.graphics.setColor(c.bg) @@ -104,6 +108,7 @@ end -- the style return { + widgetHit = widgetHit, color = color, Button = Button, Label = Label, -- cgit v1.2.3-54-g00ecf