LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

ToggleButtonElement

Description
TODO: Refactor child display element retrieval Used layers: "image" for the background.
Parent
BitmapElement
XML Configuration Parameters
GuiElement#isCheckedbool [optional] If true, the button is initialized in checked state.
GuiElement#onClickcallback [optional] onClick(element, isChecked) Called when the element is clicked. Receives this element and the current toggle state as a boolean (true for checked, false for unchecked).

Functions

addElement

Description
Definition
addElement()
Code
70function ToggleButtonElement:addElement(element)
71 ToggleButtonElement:superClass().addElement(self, element);
72 if table.getn(self.elements) <= 2 then
73 element.target = self;
74 element.onClickCallback = ToggleButtonElement.onButtonClicked;
75 self:setIsChecked(self.isChecked);
76 self:setDisabled(self.disabled);
77 end;
78end

canReceiveFocus

Description
Focus methods
Definition
canReceiveFocus()
Code
112function ToggleButtonElement:canReceiveFocus()
113 return not (self.disabled or not self:getIsVisible());
114end

copyAttributes

Description
Definition
copyAttributes()
Code
50function ToggleButtonElement:copyAttributes(src)
51 ToggleButtonElement:superClass().copyAttributes(self, src);
52 self.isChecked = src.isChecked;
53 self.onClickCallback = src.onClickCallback;
54end

getFocusTarget

Description
Get the actual focus target of this element.
Definition
getFocusTarget()
Code
119function ToggleButtonElement:getFocusTarget()
120 -- shadow parent behavior, always focus self
121 return self
122end

loadFromXML

Description
Definition
loadFromXML()
Code
33function ToggleButtonElement:loadFromXML(xmlFile, key)
34 ToggleButtonElement:superClass().loadFromXML(self, xmlFile, key);
35
36 self:addCallback(xmlFile, key.."#onClick", "onClickCallback");
37 self:setIsChecked(Utils.getNoNil(getXMLBool(xmlFile, key.."#isChecked"), self.isChecked));
38end

loadProfile

Description
Definition
loadProfile()
Code
42function ToggleButtonElement:loadProfile(profile, applyProfile)
43 ToggleButtonElement:superClass().loadProfile(self, profile, applyProfile);
44
45 self:setIsChecked(profile:getBool("isChecked", self.isChecked));
46end

mouseEvent

Description
Definition
mouseEvent()
Code
82function ToggleButtonElement:mouseEvent(posX, posY, isDown, isUp, button, eventUsed)
83 if self:getIsActive() then
84 -- check if button is highlighted
85 if not eventUsed and GuiUtils.checkOverlayOverlap(posX, posY, self.absPosition[1], self.absPosition[2], self.size[1], self.size[2]) then
86 FocusManager:setHighlight(self);
87 else
88 FocusManager:unsetHighlight(self);
89 end;
90 -- check for other mouse actions
91 return ToggleButtonElement:superClass().mouseEvent(self, posX, posY, isDown, isUp, button, eventUsed);
92 end;
93 return false;
94end

new

Description
Definition
new()
Code
20function ToggleButtonElement:new(target, custom_mt)
21 if custom_mt == nil then
22 custom_mt = ToggleButtonElement_mt;
23 end;
24 local self = BitmapElement:new(target, custom_mt);
25
26 self.isChecked = false;
27
28 return self;
29end

onButtonClicked

Description
Definition
onButtonClicked()
Code
98function ToggleButtonElement:onButtonClicked()
99 self:setIsChecked(not self.isChecked);
100 if self.onClickCallback ~= nil then
101 if self.target ~= nil then
102 self.onClickCallback(self.target, self, self.isChecked);
103 else
104 self.onClickCallback(self, self.isChecked);
105 end;
106 end;
107end

onFocusActivate

Description
Definition
onFocusActivate()
Code
148function ToggleButtonElement:onFocusActivate()
149 self:onButtonClicked();
150end

onFocusEnter

Description
Definition
onFocusEnter()
Code
137function ToggleButtonElement:onFocusEnter()
138 if self.elements[1] ~= nil then
139 self.elements[1]:onFocusEnter();
140 end;
141 if self.elements[2] ~= nil then
142 self.elements[2]:onFocusEnter();
143 end;
144end

onFocusLeave

Description
Definition
onFocusLeave()
Code
126function ToggleButtonElement:onFocusLeave()
127 if self.elements[1] ~= nil then
128 self.elements[1]:onFocusLeave();
129 end;
130 if self.elements[2] ~= nil then
131 self.elements[2]:onFocusLeave();
132 end;
133end

setIsChecked

Description
Definition
setIsChecked()
Code
58function ToggleButtonElement:setIsChecked(isChecked)
59 self.isChecked = isChecked;
60 if self.elements[1] ~= nil then
61 self.elements[1]:setVisible(self.isChecked);
62 end;
63 if self.elements[2] ~= nil then
64 self.elements[2]:setVisible(not self.isChecked);
65 end;
66end