forked from bodzio528/FS22_CropRotation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CropRotationSettings.lua
172 lines (135 loc) · 5.78 KB
/
CropRotationSettings.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
CropRotationSettings = {
MOD_NAME = g_currentModName
}
local CropRotationSettings_mt = Class(CropRotationSettings)
function CropRotationSettings:new(cropRotation, customMt)
local self = setmetatable({}, customMt or CropRotationSettings_mt)
self.cropRotation = cropRotation
self.elementsCreated = false
self.settingsHeadline = 'Nailed to death' -- g_i18n:getText("ui_header")
self.settings = {}
return self
end
function CropRotationSettings:addSetting(name, title, description, callback, callbackTarget, default, isCheckbox, optionTexts)
log(string.format('CropRotationSettings:addSetting(): name: %s, title: %s, description: %s, default: %s, checkbox: %s',
tostring(name), tostring(title), tostring(description), tostring(default), tostring(isCheckbox)))
local setting = {
name = name,
title = title,
description = description
}
if default ~= nil then
setting.state = default
elseif isCheckbox then
setting.state = false
else
setting.state = 0
end
setting.callback = callback
setting.callbackTarget = callbackTarget
setting.isCheckbox = isCheckbox
setting.optionTexts = optionTexts
setting.element = nil
table.insert(self.settings, setting)
self:loadSettings()
self:onSettingChanged(setting)
end
function CropRotationSettings:onSettingChanged(setting)
if setting.callback ~= nil and setting.callbackTarget == nil then
setting.callback(setting.state)
elseif setting.callback ~= nil and setting.callbackTarget ~= nil then
setting.callback(setting.callbackTarget, setting.state)
end
self:saveSettings()
end
function CropRotationSettings:saveSettings()
if g_savegameXML ~= nil then
for i = 1, #self.settings do
local setting = self.settings[i]
if setting.isCheckbox then
setXMLBool(g_savegameXML, string.format("gameSettings.cropRotation.settings.%s#state", setting.name), setting.state)
else
setXMLInt(g_savegameXML, string.format("gameSettings.cropRotation.settings.%s#state", setting.name), setting.state)
end
end
end
g_gameSettings:saveToXMLFile(g_savegameXML)
end
function CropRotationSettings:loadSettings()
if g_savegameXML ~= nil then
for i = 1, #self.settings do
local setting = self.settings[i]
if setting.isCheckbox then
setting.state = Utils.getNoNil(getXMLBool(g_savegameXML, string.format("gameSettings.cropRotation.settings.%s#state", setting.name)), setting.state)
else
setting.state = Utils.getNoNil(getXMLInt(g_savegameXML, string.format("gameSettings.cropRotation.settings.%s#state", setting.name)), setting.state)
end
end
end
end
function CropRotationSettings:onClickCheckbox(state, checkboxElement)
for i = 1, #self.settings do
local setting = self.settings[i]
if setting.element == checkboxElement then
setting.state = state == CheckedOptionElement.STATE_CHECKED
self:onSettingChanged(setting)
end
end
end
function CropRotationSettings:onClickMultiOption(state, optionElement)
for i = 1, #self.settings do
local setting = self.settings[i]
if setting.element == optionElement then
setting.state = state
self:onSettingChanged(setting)
end
end
end
function CropRotationSettings:overwriteGameFunctions(crModule)
crModule:overwriteGameFunction(InGameMenuGeneralSettingsFrame, "onFrameOpen", function (superFunc, frame, element)
superFunc(frame, element)
-- general algorithm, probably not needed for CR (only global disable switch is needed I guess...)
if not self.elementsCreated then
for i = 1, #frame.boxLayout.elements do
local elem = frame.boxLayout.elements[i]
if elem:isa(TextElement) then
local header = elem:clone(frame.boxLayout)
header:setText(self.settingsHeadline)
header:reloadFocusHandling(true)
break
end
end
for i = 1, #self.settings do
local setting = self.settings[i]
if setting.isCheckbox then
setting.element = frame.checkUseEasyArmControl:clone(frame.boxLayout)
function setting.element.onClickCallback(_, ...)
self:onClickCheckbox(...)
end
setting.element:reloadFocusHandling(true)
setting.element:setIsChecked(setting.state)
else
setting.element = frame.multiRealBeaconLightBrightness:clone(frame.boxLayout)
setting.element:setTexts(setting.optionTexts)
function setting.element.onClickCallback(_, ...)
self:onClickMultiOption(...)
end
setting.element:reloadFocusHandling(true)
setting.element:setState(setting.state)
end
setting.element.elements[4]:setText(setting.title)
setting.element.elements[6]:setText(setting.description)
end
frame.boxLayout:invalidateLayout()
self.elementsCreated = true
end
end)
-- crModule:overwriteGameFunction(InGameMenuGameSettingsFrame, "onFrameOpen", function (superFunc, frame, element)
-- alter specific check, PF-specific, not sure if CR needs one
-- if frame.checkLimeRequired ~= nil then
-- frame.checkLimeRequired:setVisible(false)
-- frame.checkLimeRequired.parent:invalidateLayout()
-- end
-- superFunc(frame, element)
-- end)
end