Externalize widget hit test to style definition.

This commit is contained in:
Matthias Richter 2012-03-14 16:19:46 +01:00
parent 2e5927e963
commit 6de65888dc
7 changed files with 18 additions and 17 deletions

View file

@ -1,7 +1,7 @@
local core = require((...):match("(.-)[^%.]+$") .. 'core') local core = require((...):match("(.-)[^%.]+$") .. 'core')
-- the widget -- 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. -- Generate unique identifier for gui state update and querying.
local id = core.generateID() local id = core.generateID()
@ -10,8 +10,8 @@ return function(title, x,y, w,h, draw)
-- active (mouse pressed on widget) or -- active (mouse pressed on widget) or
-- normal (mouse not on widget and not pressed on widget). -- 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, widgetHit, x,y,w,h) updates the state for this widget.
core.mouse.updateState(id, x,y,w,h) 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 -- core.makeCyclable makes the item focus on tab or whatever binding is
-- in place (see core.keyboard.cycle). Cycle order is determied by the -- in place (see core.keyboard.cycle). Cycle order is determied by the

View file

@ -1,9 +1,9 @@
local core = require((...):match("(.-)[^%.]+$") .. 'core') 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() 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.makeCyclable(id)
core.registerDraw(id, draw or core.style.Checkbox, info.checked,x,y,w,h) core.registerDraw(id, draw or core.style.Checkbox, info.checked,x,y,w,h)

View file

@ -29,12 +29,8 @@ keyboard.cycle = {
next = {key = 'tab'}, next = {key = 'tab'},
} }
function mouse.inRect(x,y,w,h) function mouse.updateState(id, widgetHit, ...)
return mouse.x >= x and mouse.x <= x+w and mouse.y >= y and mouse.y <= y+h if widgetHit(mouse.x, mouse.y, ...) then
end
function mouse.updateState(id, x,y,w,h)
if mouse.inRect(x,y,w,h) then
setHot(id) setHot(id)
if not context.active and mouse.down then if not context.active and mouse.down then
setActive(id) setActive(id)

View file

@ -1,11 +1,11 @@
local core = require((...):match("(.-)[^%.]+$") .. 'core') 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.text = info.text or ""
info.cursor = math.min(info.cursor or info.text:len(), info.text:len()) info.cursor = math.min(info.cursor or info.text:len(), info.text:len())
local id = core.generateID() 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.makeCyclable(id)
if core.isActive(id) then core.setKeyFocus(id) end if core.isActive(id) then core.setKeyFocus(id) end

View file

@ -1,6 +1,6 @@
local core = require((...):match("(.-)[^%.]+$") .. 'core') 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") assert(type(info) == 'table' and info.value, "Incomplete slider value info")
info.min = info.min or 0 info.min = info.min or 0
info.max = info.max or math.max(info.value, 1) 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 fraction = (info.value - info.min) / (info.max - info.min)
local id = core.generateID() 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.makeCyclable(id)
core.registerDraw(id,draw or core.style.Slider, fraction, x,y,w,h, info.vertical) core.registerDraw(id,draw or core.style.Slider, fraction, x,y,w,h, info.vertical)

View file

@ -1,6 +1,6 @@
local core = require((...):match("(.-)[^%.]+$") .. 'core') 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") assert(type(info) == 'table' and type(info.value) == "table", "Incomplete slider value info")
info.min = info.min or {x = 0, y = 0} 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)} 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() 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.makeCyclable(id)
core.registerDraw(id,draw or core.style.Slider2D, fraction, x,y,w,h) core.registerDraw(id,draw or core.style.Slider2D, fraction, x,y,w,h)

View file

@ -10,6 +10,10 @@ if not love.graphics.getFont() then
love.graphics.setFont(love.graphics.newFont(12)) love.graphics.setFont(love.graphics.newFont(12))
end 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 function Button(state, title, x,y,w,h)
local c = color[state] local c = color[state]
love.graphics.setColor(c.bg) love.graphics.setColor(c.bg)
@ -104,6 +108,7 @@ end
-- the style -- the style
return { return {
widgetHit = widgetHit,
color = color, color = color,
Button = Button, Button = Button,
Label = Label, Label = Label,