-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
radiobuttongroup.lua
91 lines (78 loc) · 2.22 KB
/
radiobuttongroup.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
--=========== Copyright © 2019, Planimeter, All rights reserved. ===========--
--
-- Purpose: Radio Button Group class
--
--==========================================================================--
class "gui.radiobuttongroup" ( "gui.box" )
local radiobuttongroup = gui.radiobuttongroup
function radiobuttongroup:radiobuttongroup( parent, name )
gui.box.box( self, parent, name )
self:setDisplay( "block" )
self:setPosition( "absolute" )
self.selectedId = 0
self.disabled = false
end
function radiobuttongroup:addItem( item )
self.items = self.items or {}
item.id = #self.items + 1
table.insert( self.items, item )
item.group = self
end
function radiobuttongroup:removeItem( item )
local items = self:getItems()
for i, v in ipairs( items ) do
if ( v == item ) then
table.remove( items, i )
if ( self.selectedId == i ) then
self.selectedId = 0
end
self:invalidateLayout()
end
end
if ( #items == 0 ) then
self.items = nil
end
end
accessor( radiobuttongroup, "items" )
accessor( radiobuttongroup, "selectedId" )
function radiobuttongroup:getSelectedItem()
local items = self:getItems()
if ( items ) then
return items[ self:getSelectedId() ]
end
end
function radiobuttongroup:getValue()
local item = self:getSelectedItem()
if ( item ) then
return item:getValue()
end
end
accessor( radiobuttongroup, "disabled", "is" )
function radiobuttongroup:setSelectedId( selectedId, default )
local oldSelectedId = self:getSelectedId()
local items = self:getItems()
local oldSelection = items[ oldSelectedId ]
local newSelection = items[ selectedId ]
if ( oldSelection and oldSelectedId ~= selectedId ) then
oldSelection:setSelected( false )
newSelection:setSelected( true )
self.selectedId = selectedId
self:onValueChanged( oldSelection:getValue(), newSelection:getValue() )
else
newSelection:setSelected( true )
self.selectedId = selectedId
if ( not default ) then
self:onValueChanged( nil, newSelection:getValue() )
end
end
end
function radiobuttongroup:setValue( value )
local items = self:getItems()
for i, v in ipairs( items ) do
if ( v:getValue() == value ) then
self:setSelectedId( i )
end
end
end
function radiobuttongroup:onValueChanged( oldValue, newValue )
end