From e5c9e7e29fd34271522a4ed7dfc6b8b8ee6670cd Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Wed, 23 Jan 2013 13:18:58 +0100 Subject: Allow setting custom widget ids --- button.lua | 2 +- checkbox.lua | 2 +- core.lua | 9 +++++++-- input.lua | 2 +- label.lua | 2 +- slider.lua | 2 +- slider2d.lua | 2 +- 7 files changed, 13 insertions(+), 8 deletions(-) diff --git a/button.lua b/button.lua index fd0cff4..2e2b310 100644 --- a/button.lua +++ b/button.lua @@ -48,7 +48,7 @@ return function(w) end -- Generate unique identifier for gui state update and querying. - local id = core.generateID() + local id = w.id or core.generateID() -- group.getRect determines the position and size of the widget according -- to the currently active group. Both arguments may be omitted. diff --git a/checkbox.lua b/checkbox.lua index 14a92a5..5137bb0 100644 --- a/checkbox.lua +++ b/checkbox.lua @@ -48,7 +48,7 @@ return function(w) w.size[1] = w.size[1] + bw + 4 end - local id = core.generateID() + local id = w.id or core.generateID() local pos, size = group.getRect(w.pos, w.size) mouse.updateWidget(id, pos, size, w.widgetHit) diff --git a/core.lua b/core.lua index 67602ac..533ab96 100644 --- a/core.lua +++ b/core.lua @@ -65,10 +65,15 @@ end -- -- Widget ID -- -local maxid = 0 +local maxid, uids = 0, {} +setmetatable(uids, {__index = function(t, i) + t[i] = {} + return t[i] +end}) + local function generateID() maxid = maxid + 1 - return maxid + return uids[maxid] end -- diff --git a/input.lua b/input.lua index 64f2ced..87d980e 100644 --- a/input.lua +++ b/input.lua @@ -36,7 +36,7 @@ return function(w) w.info.text = w.info.text or "" w.info.cursor = math.min(w.info.cursor or w.info.text:len(), w.info.text:len()) - local id = core.generateID() + local id = w.id or core.generateID() local pos, size = group.getRect(w.pos, w.size) mouse.updateWidget(id, pos, size, w.widgetHit) keyboard.makeCyclable(id) diff --git a/label.lua b/label.lua index c84d08e..345516c 100644 --- a/label.lua +++ b/label.lua @@ -46,7 +46,7 @@ return function(w) end end - local id = core.generateID() + local id = w.id or core.generateID() local pos, size = group.getRect(w.pos, w.size) if keyboard.hasFocus(id) then diff --git a/slider.lua b/slider.lua index 4561d4a..1a0ef77 100644 --- a/slider.lua +++ b/slider.lua @@ -38,7 +38,7 @@ return function(w) w.info.step = w.info.step or (w.info.max - w.info.min) / 20 local fraction = (w.info.value - w.info.min) / (w.info.max - w.info.min) - local id = core.generateID() + local id = w.id or core.generateID() local pos, size = group.getRect(w.pos, w.info.size) mouse.updateWidget(id, pos, size, w.widgetHit) diff --git a/slider2d.lua b/slider2d.lua index 19abaf1..0ebfeb8 100644 --- a/slider2d.lua +++ b/slider2d.lua @@ -51,7 +51,7 @@ return function(w) (w.info.value[2] - w.info.min[2]) / (w.info.max[2] - w.info.min[2]), } - local id = core.generateID() + local id = w.id or core.generateID() local pos, size = group.getRect(w.pos, w.size) mouse.updateWidget(id, pos, size, w.widgetHit) -- cgit v1.2.3-54-g00ecf