Externalize widget hit test to style definition.
This commit is contained in:
parent
2e5927e963
commit
6de65888dc
7 changed files with 18 additions and 17 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
8
core.lua
8
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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue