File size: 5,635 Bytes
b6a38d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
local max_o = 100
local function oname(n)
	return string.format("o%d", n)
end

DefineClass.DevDockPinsButton = {
	__parents = { "XTextButton" },
	IdNode = true,
	ChildrenHandleMouse = true,
	BorderWidth = 1,
	BorderColor = const.clrBlack,
	RolloverBorderColor = const.clrBlack,
	RolloverTemplate = "GedToolbarRollover",
	Translate = false,
	AltPress = true,
	
	plugin = false,
	selected = false,
}

function DevDockPinsButton:Init(parent, context)
	local o_label = XLabel:new({
		Id = "idOLabel",
		HAlign = "right",
		VAlign = "bottom",
		ScaleModifier = point(500, 500),
		Translate = false,
	}, self, self.context)
	function o_label:OnContextUpdate(context, update)
		local main_btn = self.parent
		local idx = main_btn:GetPinIndex(context)
		if idx then
			self:SetVisible(true)
			self:SetText(oname(idx))
		else
			self:SetVisible(false)
		end
	end
	
	local name = self.plugin:GetDisplayName(self.context)
	--find other buttons before me
	local n = 0
	for i,btn in ipairs(self.parent) do
		if btn == self then break end
		if btn.context.class == self.context.class then
			n = n + 1
		end
	end
	if n > 0 then
		name = string.format("%s %d", name, n + 1)
	end
	self:SetText(name)
end

function DevDockPinsButton:OnPress(gamepad)
	SelectObj(self.context)
end

function DevDockPinsButton:OnMouseButtonDoubleClick(button)
	ViewObject(self.context)
	return "break"
end

function DevDockPinsButton:OnAltPress(gamepad)
	self.plugin:SetPinned(self.context, not self:IsPinned())
end

function DevDockPinsButton:OnContextUpdate(context, update)
	if not IsValid(context) or (update ~= "open" and not self.selected and not self:IsPinned()) then
		self:Close()
	end
end

function DevDockPinsButton:HasOLabel()
	return self:ResolveId("idOLabel"):GetVisible()
end

function DevDockPinsButton:GetPinIndex()
	return self.plugin:GetPinIndex(self.context)
end

function DevDockPinsButton:IsPinned()
	return not not self:GetPinIndex()
end

function DevDockPinsButton:SetSelected(selected)
	self.selected = selected
	if not selected and not self:IsPinned() then
		self:Close()
	else
		self:SetHighlighted(selected)
	end
end

function DevDockPinsButton:SetHighlighted(highlighted)
	local color = highlighted and RGB(120,120,255) or const.clrBlack
	self:SetBorderColor(color)
	self:SetRolloverBorderColor(color)
end

----

DefineClass.DevDockPinsPlugin = {
	__parents = { "DevDockPlugin" },
	LayoutMethod = "HList",
	my_pins = false, --list of indices pinned by the dlg
}

function DevDockPinsPlugin.IsValid()
	return GetInGameInterface()
end

function DevDockPinsPlugin:Init()
	self.my_pins = {}
    self:CreateThread("o_thread", self.OThreadProc, self)
end

function DevDockPinsPlugin:OnDelete(result, ...)
	for idx in pairs(self.my_pins) do
		local varname = oname(idx)
		rawset(_G, varname, nil)
	end
end

function DevDockPinsPlugin:GetDisplayName(obj)
	if IsKindOf(obj, "Human") then
		return _InternalTranslate(obj.FirstName)
	else
		return obj.class
	end
end

function DevDockPinsPlugin:GetPinIndex(obj)
	return OPinsGetIndex(obj)
end

function DevDockPinsPlugin:GetNextPinIndex()
	return OPinsGetNextIndex()
end

function DevDockPinsPlugin:SetPinned(obj, pinned)
	local idx = OPinsSet(obj, pinned)
	if pinned then
		self.my_pins[idx] = true
	else
		self.my_pins[idx] = nil
	end
end

function DevDockPinsPlugin:TogglePinned(obj)
	local idx = self:GetPinIndex(obj)
	local is_pinned = not not idx
	self:SetPinned(obj, not is_pinned)
end

function DevDockPinsPlugin:AddButton(obj)
    local btn = self:FindButton(obj)
    if not btn then
        btn = DevDockPinsButton:new({ plugin = self }, self, obj)
		btn:Open()
    end
    return btn
end

function DevDockPinsPlugin:RemoveButton(obj)
	local btn = self:FindButton(obj)
	if btn then
		btn:Close()
	end
end

function DevDockPinsPlugin:FindButton(obj)
    for i,btn in ipairs(self) do
        if btn.context == obj then
            return btn
        end
    end
end

function DevDockPinsPlugin:SelectionAdded(obj)
    local btn = self:FindButton(obj)
    if not btn then
        btn = self:AddButton(obj)
    end
    btn:SetSelected(true)
end

function DevDockPinsPlugin:SelectionRemoved(obj)
    local btn = self:FindButton(obj)
    if not btn then return end
    btn:SetSelected(false)
end

function DevDockPinsPlugin:OThreadProc()
	while self.window_state ~= "destroying" do
		for i,btn in ipairs(self) do
			if btn:HasOLabel() then
				ObjModified(btn.context)
			end
		end
		for idx=1,max_o do
			local obj = rawget(_G, oname(idx))
			if IsValid(obj) then
				self:AddButton(obj)
				ObjModified(obj)
			end
		end
		Sleep(1000)
	end
end

function OPinsGetIndex(obj)
	for idx=1,max_o do
		local value = rawget(_G, oname(idx))
		if value == obj then return idx end
	end
end

function OPinsGetNextIndex()
	for idx=1,max_o do
		local value = rawget(_G, oname(idx))
		if value == nil then return idx end
	end
end

function OPinsSet(obj, pinned)
	local idx = OPinsGetIndex(obj)
	if pinned then
		if not idx then
			idx = OPinsGetNextIndex()
			rawset(_G, oname(idx), obj)
			ObjModified(obj)
		end
	else
		if idx then
			rawset(_G, oname(idx), nil)
			ObjModified(obj)
		end
	end
	
	return idx
end

function OPinsClear()
	for idx=1,max_o do
		rawset(_G, oname(idx), nil)
	end
end

function OnMsg.LoadGame(metadata, version)
	OPinsClear()
end

function OnMsg.NewGame()
	OPinsClear()
end

function OnMsg.SelectionAdded(obj)
	local plugin = GetDevDockPlugin("DevDockPinsPlugin")
	if not plugin then return end
	plugin:SelectionAdded(obj)
end

function OnMsg.SelectionRemoved(obj)
	local plugin = GetDevDockPlugin("DevDockPinsPlugin")
	if not plugin then return end
	plugin:SelectionRemoved(obj)
end