forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateunit.lua
More file actions
760 lines (683 loc) · 29.7 KB
/
createunit.lua
File metadata and controls
760 lines (683 loc) · 29.7 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
local ItemList = import('/lua/maui/itemlist.lua').ItemList
local LayoutHelpers = import('/lua/maui/layouthelpers.lua')
local Group = import('/lua/maui/group.lua').Group
local Text = import('/lua/maui/text.lua').Text
local Border = import('/lua/maui/border.lua').Border
local Bitmap = import('/lua/maui/bitmap.lua').Bitmap
local Checkbox = import('/lua/maui/checkbox.lua').Checkbox
local RadioGroup = import('/lua/maui/mauiutil.lua').RadioGroup
local Combo = import('/lua/ui/controls/combo.lua').Combo
local UIUtil = import('/lua/ui/uiutil.lua')
local Edit = import('/lua/maui/edit.lua').Edit
local dialog = false
local nameDialog = false
local activeFilters = {}
local activeFilterTypes = {}
local specialFilterControls = {}
local filterSet = {}
local currentArmy = GetFocusArmy()
local UnitList = {}
local CreationList = {}
local defaultEditField = false
local unselectedCheckboxFile = UIUtil.UIFile('/widgets/rad_un.dds')
local selectedCheckboxFile = UIUtil.UIFile('/widgets/rad_sel.dds')
local nameFilters = import('/lua/ui/dialogs/createunitfilters.lua').Filters
local function getItems()
local idlist = EntityCategoryGetUnitList(categories.ALLUNITS)
table.sort(idlist)
return idlist
end
local function CreateNameFilter(data)
local group = Group(dialog)
group.Width:Set(dialog.Width)
LayoutHelpers.SetHeight(group, 30)
group.check = UIUtil.CreateCheckboxStd(group, '/dialogs/check-box_btn/radio')
LayoutHelpers.AtLeftIn(group.check, group)
LayoutHelpers.AtVerticalCenterIn(group.check, group)
group.check.key = data.key
if filterSet[data.key] == nil then
filterSet[data.key] = {value = false, choices = {}}
end
if activeFilters[data.key] == nil then
activeFilters[data.key] = {}
end
group.label = UIUtil.CreateText(group, data.title, 14, UIUtil.bodyFont)
LayoutHelpers.RightOf(group.label, group.check)
LayoutHelpers.AtVerticalCenterIn(group.label, group)
if data.choices then
group.items = {}
for i, v in data.choices do
local index = i
group.items[index] = UIUtil.CreateCheckboxStd(group, '/dialogs/toggle_btn/toggle')
if index == 1 then
LayoutHelpers.AtLeftTopIn(group.items[index], group, 95)
else
LayoutHelpers.RightOf(group.items[index], group.items[index-1])
end
LayoutHelpers.AtVerticalCenterIn(group.items[index], group)
group.items[index].label = UIUtil.CreateText(group.items[index], v.title, 10, UIUtil.bodyFont)
LayoutHelpers.AtCenterIn(group.items[index].label, group.items[index])
group.items[index].label:DisableHitTest()
group.items[index].sortFunc = v.sortFunc
group.items[index].filterKey = v.key
group.items[index].key = data.key
group.items[index].OnCheck = function(self, checked)
filterSet[self.key].choices[self.filterKey] = checked
self.label:SetColor(UIUtil.fontColor)
if checked then
if not group.check:IsChecked() then
group.check:SetCheck(true)
end
activeFilters[self.key][self.filterKey] = self.sortFunc
elseif activeFilters[self.key][self.filterKey] then
local otherChecked = false
for _, control in group.items do
if control ~= self then
if control:IsChecked() then
otherChecked = true
break
end
end
end
if not otherChecked then
group.check:SetCheck(false)
end
activeFilters[self.key][self.filterKey] = nil
end
RefreshList()
end
if filterSet[data.key].choices[v.key] == nil then
filterSet[data.key].choices[v.key] = false
end
group.items[index]:SetCheck(filterSet[data.key].choices[v.key])
if activeFilters[data.key] == nil then activeFilters[data.key] = {} end
end
else
group.edit = Edit(group)
group.edit:SetForegroundColor(UIUtil.fontColor)
group.edit:SetBackgroundColor('ff333333')
group.edit:SetHighlightForegroundColor(UIUtil.highlightColor)
group.edit:SetHighlightBackgroundColor("880085EF")
LayoutHelpers.SetDimensions(group.edit, 400, 15)
group.edit:SetText(filterSet[data.key].editText or '')
group.edit:SetFont(UIUtil.bodyFont, 12)
group.edit:SetMaxChars(20)
LayoutHelpers.AtLeftIn(group.edit, group, 95)
LayoutHelpers.AtVerticalCenterIn(group.edit, group)
group.edit.filterKey = data.key
group.edit.key = data.key
group.edit.sortFunc = data.sortFunc
group.edit.OnTextChanged = function(self, new, old)
if new == '' then
activeFilters[self.key][self.filterKey] = nil
if group.check:IsChecked() then
group.check:SetCheck(false)
end
else
filterSet[self.key].editText = new
activeFilters[self.key][self.filterKey] = self.sortFunc
if not group.check:IsChecked() then
group.check:SetCheck(true)
end
end
RefreshList()
end
defaultEditField = group.edit
specialFilterControls[data.key] = group.edit
end
group.check.OnCheck = function(self, checked)
activeFilterTypes[self.key] = checked
filterSet[data.key].value = checked
local labelColor = 'ff555555'
if checked then
labelColor = UIUtil.fontColor
end
if group.items then
for i, v in group.items do
if not checked then
v:SetCheck(false, true)
end
v.label:SetColor(labelColor)
end
else
end
group.label:SetColor(labelColor)
RefreshList()
end
group.check:SetCheck(filterSet[data.key].value)
return group
end
function CreateDialog(x, y)
if dialog then
dialog:Destroy()
dialog = false
return
end
CreationList = {}
dialog = Bitmap(GetFrame(0))
dialog:SetSolidColor('CC000000')
local NoArmies = math.ceil( ( table.getn(GetArmiesTable().armiesTable) / 2 ) + 1 )
-- set window high. 400 pixel for the window + 30 pixel for every army line
LayoutHelpers.SetDimensions(dialog, 550, 450 + 30 * NoArmies)
dialog.Left:Set(function() return math.max(math.min(x, GetFrame(0).Right() - dialog.Width()), 0) end)
dialog.Top:Set(function() return math.max(math.min(y, GetFrame(0).Bottom() - dialog.Height()), 0) end)
dialog.Depth:Set(GetFrame(0):GetTopmostDepth() + 1)
local cancelBtn = UIUtil.CreateButtonStd(dialog, '/widgets/small', "Cancel", 12)
LayoutHelpers.AtBottomIn(cancelBtn, dialog)
LayoutHelpers.AtRightIn(cancelBtn, dialog)
cancelBtn.OnClick = function(button)
dialog:Destroy()
dialog = false
end
ForkThread(function()
while dialog do
if IsKeyDown('ESCAPE') then
cancelBtn.OnClick()
return
end
WaitSeconds(0.05)
end
end)
local countLabel = UIUtil.CreateText(dialog, 'Count:', 12, UIUtil.bodyFont)
LayoutHelpers.AtBottomIn(countLabel, dialog,10)
LayoutHelpers.AtLeftIn(countLabel, dialog, 5)
local count = Edit(dialog)
count:SetForegroundColor(UIUtil.fontColor)
count:SetBackgroundColor('ff333333')
count:SetHighlightForegroundColor(UIUtil.highlightColor)
count:SetHighlightBackgroundColor("880085EF")
LayoutHelpers.SetDimensions(count, 30, 15)
count:SetFont(UIUtil.bodyFont, 12)
count:SetMaxChars(4)
count:SetText('1')
LayoutHelpers.RightOf(count, countLabel, 5)
count.OnCharPressed = function(self, charcode)
if (charcode < 48) or (charcode > 57) then -- between 0 and 9
return true
end
end
count.OnNonTextKeyPressed = function(self, keycode, modifiers)
end
count.OnKeyboardFocusChange = function(self)
if self:GetText() == '' then
self:SetText('1')
end
end
local veterancyLabel = UIUtil.CreateText(count, 'Veterancy:', 12, UIUtil.bodyFont)
LayoutHelpers.RightOf(veterancyLabel, count, 5)
local veterancyLevel = Edit(dialog)
veterancyLevel:SetForegroundColor(UIUtil.fontColor)
veterancyLevel:SetBackgroundColor('ff333333')
veterancyLevel:SetHighlightForegroundColor(UIUtil.highlightColor)
veterancyLevel:SetHighlightBackgroundColor("880085EF")
LayoutHelpers.SetDimensions(veterancyLevel, 30, 15)
veterancyLevel:SetFont(UIUtil.bodyFont, 12)
veterancyLevel:SetMaxChars(1)
veterancyLevel:SetText('0')
LayoutHelpers.RightOf(veterancyLevel, veterancyLabel, 5)
veterancyLevel.OnCharPressed = function(self, charcode)
if (charcode < 48) or (charcode > 53) then -- between 0 and 5
return true
end
self:ClearText()
end
veterancyLevel.OnNonTextKeyPressed = function(self, keycode, modifiers)
end
veterancyLevel.OnKeyboardFocusChange = function(self)
if self:GetText() == '' then
self:SetText('0')
end
end
local function spawnUnits(creationList, targetArmy, fast)
if table.getsize(creationList) <= 0 then return end
local numUnits = tonumber(count:GetText())
local vetLvl = tonumber(veterancyLevel:GetText())
if fast then
SimCallback( { Func = 'SpawnAndSetVeterancyUnit',
Args = { bpId = creationList, count = numUnits,
army = targetArmy, pos = GetMouseWorldPos(), veterancy = vetLvl }, }, true)
else
WaitSeconds(0.1)
while not dialog do
if IsKeyDown('ESCAPE') then return end
if IsKeyDown(1) then -- Left mouse button
SimCallback( { Func = 'SpawnAndSetVeterancyUnit',
Args = { bpId = creationList, count = numUnits,
army = targetArmy, pos = GetMouseWorldPos(), veterancy = vetLvl }, }, true)
if not IsKeyDown('SHIFT') then return end
end
WaitSeconds(0.09)
end
end
end
local createBtn = UIUtil.CreateButtonStd(dialog, '/widgets/small', "Create", 12)
LayoutHelpers.AtBottomIn(createBtn, dialog)
LayoutHelpers.AtHorizontalCenterIn(createBtn, dialog)
createBtn.HandleEvent = function(self, event)
if event.Type ~= 'ButtonPress' then return end
ForkThread(spawnUnits, CreationList, currentArmy, event.Modifiers.Right)
cancelBtn.OnClick()
end
local function SetFilters(filterTable)
for filterGroup, groupControls in filterGroups do
local key = groupControls.check.key
if filterTable[key] ~= nil then
LOG('setting key: ', key, ' to: ', filterTable[key].value)
if groupControls.check:IsChecked() ~= filterTable[key].value then
groupControls.check:SetCheck(filterTable[key].value)
end
if groupControls.items then
for choiceIndex, choiceControl in groupControls.items do
if filterTable[key].choices[choiceControl.filterKey] ~= nil and choiceControl:IsChecked() ~= filterTable[key].choices[choiceControl.filterKey] then
choiceControl:SetCheck(filterTable[key].choices[choiceControl.filterKey])
end
end
else
groupControls.edit:SetText(filterTable[key].editText)
end
end
end
RefreshList()
end
local function CreateArmySelectionSlot(parent, index, armyData)
local group = Bitmap(parent)
LayoutHelpers.SetHeight(group, 30)
group.Width:Set(function() return parent.Width() / 2 end)
local iconBG = Bitmap(group)
LayoutHelpers.SetDimensions(iconBG, 30, 30)
iconBG:SetSolidColor(armyData.color)
LayoutHelpers.AtLeftTopIn(iconBG, group)
iconBG:DisableHitTest()
local icon = Bitmap(iconBG)
if armyData.civilian then
icon:SetSolidColor('aaaaaaaa')
else
icon:SetTexture(UIUtil.UIFile(UIUtil.GetFactionIcon(armyData.faction)))
end
LayoutHelpers.FillParent(icon, iconBG)
icon:DisableHitTest()
-- Player / Ai name
local name = UIUtil.CreateText(group, armyData.nickname, 12, UIUtil.bodyFont)
LayoutHelpers.RightOf(name, icon, 2)
LayoutHelpers.AtTopIn(name, group)
name:SetColor('ffffffff')
name:DisableHitTest()
local army = UIUtil.CreateText(group, armyData.name, 12, UIUtil.bodyFont)
LayoutHelpers.Below(army, name)
army:DisableHitTest()
group.HandleEvent = function(self, event)
if event.Type == 'MouseEnter' then
if currentArmy == index then
self:SetSolidColor('cc00cc00')
else
self:SetSolidColor('77007700')
end
elseif event.Type == 'MouseExit' then
if currentArmy == index then
self:SetSolidColor('aa00aa00')
else
self:SetSolidColor('00000000')
end
elseif event.Type == 'ButtonPress' then
currentArmy = index
for i, v in parent.armySlots do
if i == index then
v:SetSolidColor('aa00aa00')
else
v:SetSolidColor('00000000')
end
end
elseif event.Type == 'ButtonDClick' then
ConExecute('SetFocusArmy '..tostring(currentArmy-1))
end
end
if index == currentArmy then
group:SetSolidColor('aa00aa00')
end
return group
end
local armiesGroup = Group(dialog)
armiesGroup.Width:Set(dialog.Width)
LayoutHelpers.AtLeftTopIn(armiesGroup, dialog)
armiesGroup.armySlots = {}
local lowestControl = false
local NoArmies = math.ceil( ( table.getn(GetArmiesTable().armiesTable) / 2 ) + 1 )
for i, val in GetArmiesTable().armiesTable do
armiesGroup.armySlots[i] = CreateArmySelectionSlot(armiesGroup, i, val)
-- set the layout to left at the first army
if i == 1 then
LayoutHelpers.AtLeftTopIn(armiesGroup.armySlots[i],armiesGroup)
lowestControl = armiesGroup.armySlots[i]
-- Change layout to right after half army count
elseif i == NoArmies then
LayoutHelpers.RightOf(armiesGroup.armySlots[i],armiesGroup.armySlots[1])
LayoutHelpers.AtTopIn(armiesGroup.armySlots[i],armiesGroup)
else
LayoutHelpers.Below(armiesGroup.armySlots[i],armiesGroup.armySlots[i-1])
end
if armiesGroup.armySlots[i].Bottom() > lowestControl.Bottom() then
lowestControl = armiesGroup.armySlots[i]
end
end
armiesGroup.Height:Set(function() return lowestControl.Bottom() - armiesGroup.armySlots[1].Top() end)
local filterSetCombo = Combo(dialog, 14, 10, nil, nil, "UI_Tab_Click_01", "UI_Tab_Rollover_01")
LayoutHelpers.SetWidth(filterSetCombo, 340)
LayoutHelpers.Below(filterSetCombo, armiesGroup, 5)
filterSetCombo.OnClick = function(self, index, text, skipUpdate)
SetFilters(self.keyMap[index])
end
local function RefreshFilterList(defName)
filterSetCombo:ClearItems()
filterSetCombo.itemArray = {}
filterSetCombo.keyMap = {}
local CurrentFilterSets = GetPreference('CreateUnitFilters')
if CurrentFilterSets and table.getsize(CurrentFilterSets) > 0 then
local index = 1
local default = 1
for filterName, filter in sortedpairs(CurrentFilterSets) do
if filterName == defName then
default = index
end
filterSetCombo.itemArray[index] = string.format('%s', filterName)
filterSetCombo.keyMap[index] = filter
index = index + 1
end
filterSetCombo:AddItems(filterSetCombo.itemArray, default)
end
end
local saveFilterSet = UIUtil.CreateButton(dialog,
'/dialogs/toggle_btn/toggle-d_btn_up.dds',
'/dialogs/toggle_btn/toggle-d_btn_down.dds',
'/dialogs/toggle_btn/toggle-d_btn_over.dds',
'/dialogs/toggle_btn/toggle-d_btn_dis.dds',
'Save Filter', 10)
saveFilterSet.label:SetFont(UIUtil.bodyFont, 10)
LayoutHelpers.RightOf(saveFilterSet, filterSetCombo)
LayoutHelpers.AtVerticalCenterIn(saveFilterSet, filterSetCombo)
saveFilterSet.OnClick = function(self, modifiers)
NameSet(function(name)
local newFilterListing = {}
if GetPreference('CreateUnitFilters') then
newFilterListing = table.deepcopy(GetPreference('CreateUnitFilters'))
newFilterListing[name] = filterSet
else
newFilterListing[name] = filterSet
end
SetPreference('CreateUnitFilters',newFilterListing)
RefreshFilterList(name)
end)
end
local delFilterSet = UIUtil.CreateButton(dialog,
'/dialogs/toggle_btn/toggle-d_btn_up.dds',
'/dialogs/toggle_btn/toggle-d_btn_down.dds',
'/dialogs/toggle_btn/toggle-d_btn_over.dds',
'/dialogs/toggle_btn/toggle-d_btn_dis.dds',
'Delete Filter', 10)
delFilterSet.label:SetFont(UIUtil.bodyFont, 10)
LayoutHelpers.RightOf(delFilterSet, saveFilterSet)
LayoutHelpers.AtVerticalCenterIn(delFilterSet, filterSetCombo)
delFilterSet.OnClick = function(self, modifiers)
local index = filterSetCombo:GetItem()
if index >= 1 then
local delName = filterSetCombo.itemArray[index]
LOG(delName)
local oldFilterSets = GetPreference('CreateUnitFilters')
if oldFilterSets[delName] then
oldFilterSets[delName] = nil
end
SetPreference('CreateUnitFilters',oldFilterSets)
RefreshFilterList()
end
end
RefreshFilterList()
filterGroups = {}
for filtIndex, filter in nameFilters do
local index = filtIndex
filterGroups[index] = CreateNameFilter(filter)
if filtIndex == 1 then
LayoutHelpers.Below(filterGroups[index], filterSetCombo)
LayoutHelpers.AtLeftIn(filterGroups[index], dialog)
else
LayoutHelpers.Below(filterGroups[index], filterGroups[index-1])
end
end
dialog.unitList = Group(dialog)
dialog.unitList.Height:Set(function() return createBtn.Top() - filterGroups[table.getn(filterGroups)].Bottom() - LayoutHelpers.ScaleNumber(5) end)
dialog.unitList.Width:Set(function() return dialog.Width() - LayoutHelpers.ScaleNumber(40) end)
LayoutHelpers.Below(dialog.unitList, filterGroups[table.getn(filterGroups)])
dialog.unitList.top = 0
dialog.unitEntries = {}
UIUtil.CreateVertScrollbarFor(dialog.unitList)
local LineColors = {
Up = '00000000', Sel_Up = 'ff447744',
Over = 'ff444444', Sel_Over = 'ff669966',
}
local mouseover = false
local function CreateElementMouseover(unitData,x,y)
if mouseover then mouseover:Destroy() end
mouseover = Bitmap(dialog)
mouseover:SetSolidColor('dd115511')
mouseover.img = Bitmap(mouseover)
LayoutHelpers.SetDimensions(mouseover.img, 40, 40)
LayoutHelpers.AtLeftTopIn(mouseover.img, mouseover, 2,2)
if DiskGetFileInfo(UIUtil.UIFile('/icons/units/'..unitData..'_icon.dds', true)) then
mouseover.img:SetTexture(UIUtil.UIFile('/icons/units/'..unitData..'_icon.dds', true))
else
mouseover.img:SetTexture(UIUtil.UIFile('/icons/units/default_icon.dds'))
end
mouseover.name = UIUtil.CreateText(mouseover, __blueprints[unitData].Description, 14, UIUtil.bodyFont)
LayoutHelpers.RightOf(mouseover.name, mouseover.img, 2)
mouseover.desc = UIUtil.CreateText(mouseover, __blueprints[unitData].General.UnitName or unitData, 14, UIUtil.bodyFont)
LayoutHelpers.AtLeftIn(mouseover.desc, mouseover, 44)
LayoutHelpers.AtBottomIn(mouseover.desc, mouseover, 5)
mouseover.Left:Set(x+20)
mouseover.Top:Set(y+20)
mouseover.Height:Set(function() return mouseover.img.Height() + 4 end)
mouseover.Width:Set(function() return mouseover.img.Width() + math.max(mouseover.name.Width(), mouseover.desc.Width()) + 8 end)
mouseover.Depth:Set(GetFrame(0):GetTopmostDepth() + 1)
end
local function MoveMouseover(x,y)
if mouseover then
mouseover.Left:Set(x+20)
mouseover.Top:Set(y+20)
end
end
local function DestroyMouseover()
if mouseover then
mouseover:Destroy()
mouseover = false
end
end
local function CreateUnitElements()
if dialog.unitEntries then
for i, v in dialog.unitEntries do
if v.bg then v.bg:Destroy() end
end
dialog.unitEntries = {}
end
local function CreateElement(index)
dialog.unitEntries[index] = Bitmap(dialog.unitList)
dialog.unitEntries[index].Left:Set(dialog.unitList.Left)
dialog.unitEntries[index].Right:Set(dialog.unitList.Right)
LayoutHelpers.SetHeight(dialog.unitEntries[index], 16)
dialog.unitEntries[index].Checked = false
dialog.unitEntries[index].HandleEvent = function(self, event)
if event.Type == 'MouseEnter' then
CreateElementMouseover(self.unitID,event.MouseX,event.MouseY)
if self.Checked then
self:SetSolidColor(LineColors.Sel_Over)
else
self:SetSolidColor(LineColors.Over)
end
elseif event.Type == 'MouseExit' then
DestroyMouseover()
if self.Checked then
self:SetSolidColor(LineColors.Sel_Up)
else
self:SetSolidColor(LineColors.Up)
end
elseif event.Type == 'ButtonPress' and event.Modifiers.Left then
self.Checked = not self.Checked
if CreationList[self.unitID] then
CreationList[self.unitID] = nil
else
CreationList[self.unitID] = true
self:SetSolidColor(LineColors.Sel_Up)
end
elseif event.Type == 'ButtonPress' and event.Modifiers.Right then
CreationList[self.unitID] = true
ForkThread(spawnUnits, CreationList, currentArmy, true)
cancelBtn:OnClick()
elseif event.Type == 'ButtonDClick' and event.Modifiers.Left then
ForkThread(spawnUnits, {[self.unitID] = true}, currentArmy, false)
cancelBtn:OnClick()
elseif event.Type == 'MouseMotion' then
MoveMouseover(event.MouseX,event.MouseY)
end
end
dialog.unitEntries[index].id = UIUtil.CreateText(dialog.unitEntries[index], '', 12, UIUtil.bodyFont)
LayoutHelpers.AtLeftTopIn(dialog.unitEntries[index].id, dialog.unitEntries[index])
end
CreateElement(1)
LayoutHelpers.AtTopIn(dialog.unitEntries[1], dialog.unitList)
local index = 2
while dialog.unitEntries[table.getsize(dialog.unitEntries)].Top() + (2 * dialog.unitEntries[1].Height()) < dialog.unitList.Bottom() do
CreateElement(index)
LayoutHelpers.Below(dialog.unitEntries[index], dialog.unitEntries[index-1])
index = index + 1
end
end
CreateUnitElements()
local numLines = function() return table.getsize(dialog.unitEntries) end
local function DataSize()
return table.getn(UnitList)
end
-- called when the scrollbar for the control requires data to size itself
-- GetScrollValues must return 4 values in this order:
-- rangeMin, rangeMax, visibleMin, visibleMax
-- aixs can be "Vert" or "Horz"
dialog.unitList.GetScrollValues = function(self, axis)
local size = DataSize()
--LOG(size, ":", self.top, ":", math.min(self.top + numLines, size))
return 0, size, self.top, math.min(self.top + numLines(), size)
end
-- called when the scrollbar wants to scroll a specific number of lines (negative indicates scroll up)
dialog.unitList.ScrollLines = function(self, axis, delta)
self:ScrollSetTop(axis, self.top + math.floor(delta))
end
-- called when the scrollbar wants to scroll a specific number of pages (negative indicates scroll up)
dialog.unitList.ScrollPages = function(self, axis, delta)
self:ScrollSetTop(axis, self.top + math.floor(delta) * numLines())
end
-- called when the scrollbar wants to set a new visible top line
dialog.unitList.ScrollSetTop = function(self, axis, top)
top = math.floor(top)
if top == self.top then return end
local size = DataSize()
self.top = math.max(math.min(size - numLines() , top), 0)
self:CalcVisible()
end
-- called to determine if the control is scrollable on a particular access. Must return true or false.
dialog.unitList.IsScrollable = function(self, axis)
return true
end
-- determines what controls should be visible or not
dialog.unitList.CalcVisible = function(self)
local function SetTextLine(line, data, lineID)
line:Show()
if CreationList[data.id] then
line.Checked = true
line:SetSolidColor(LineColors.Sel_Up)
else
line.Checked = false
line:SetSolidColor(LineColors.Up)
end
line.unitID = data.id
line.id:SetText(string.format('%s %5s %s', data.id, ' ', data.desc))
end
for i, v in dialog.unitEntries do
if UnitList[i + self.top] then
SetTextLine(v, UnitList[i + self.top], i + self.top)
else
v:Hide()
end
end
--LOG(repr(ObjectiveLogData))
end
dialog.unitList.HandleEvent = function(control, event)
if event.Type == 'WheelRotation' then
local lines = 3
if event.WheelRotation > 0 then
lines = -3
end
control:ScrollLines(nil, lines)
end
end
defaultEditField:AcquireFocus()
RefreshList()
end
function RefreshList()
if not dialog.unitList then return end
UnitList = {}
local totalList = getItems()
for i, v in totalList do
local allValid = true
for filterType, filters in activeFilters do
if activeFilterTypes[filterType] then
local valid = false
for filterIndex, filter in filters do
local specialText = ''
if specialFilterControls[filterIndex] then
specialText = specialFilterControls[filterIndex]:GetText()
end
if filter(v, specialText) then
valid = true
break
end
end
allValid = valid and allValid
end
end
if allValid then
table.insert(UnitList, {id = v, name = LOC(__blueprints[v].General.UnitName) or '', desc = LOC(__blueprints[v].Description) or ''})
end
end
dialog.unitList.top = 0
dialog.unitList:CalcVisible()
end
function NameSet(callback)
-- Dialog already showing? Don't show another one
if nameDialog then return end
nameDialog = Bitmap(dialog, UIUtil.SkinnableFile('/dialogs/dialog_02/panel_bmp.dds'), "Marker Name Dialog")
LayoutHelpers.AtCenterIn(nameDialog, GetFrame(0))
nameDialog.Depth:Set(GetFrame(0):GetTopmostDepth() + 10)
local label = UIUtil.CreateText(nameDialog, "Name your filter set:", 16, UIUtil.buttonFont)
LayoutHelpers.AtLeftTopIn(label, nameDialog, 35, 30)
local cancelButton = UIUtil.CreateButtonStd(nameDialog, '/widgets02/small', "<LOC _CANCEL>", 12)
LayoutHelpers.AtTopIn(cancelButton, nameDialog, 112)
cancelButton.Left:Set(function() return nameDialog.Left() + (((nameDialog.Width() / 4) * 1) - (cancelButton.Width() / 2)) end)
cancelButton.OnClick = function(self, modifiers)
nameDialog:Destroy()
nameDialog = false
end
--TODO this should be in layout
local nameEdit = Edit(nameDialog)
LayoutHelpers.AtLeftTopIn(nameEdit, nameDialog, 35, 60)
LayoutHelpers.SetWidth(nameEdit, 283)
nameEdit.Height:Set(nameEdit:GetFontHeight())
nameEdit:ShowBackground(false)
nameEdit:AcquireFocus()
UIUtil.SetupEditStd(nameEdit, UIUtil.fontColor, nil, nil, nil, UIUtil.bodyFont, 16, 30)
local okButton = UIUtil.CreateButtonStd(nameDialog, '/widgets02/small', "<LOC _OK>", 12)
okButton.Top:Set(function() return nameDialog.Top() + 112 end)
okButton.Left:Set(function() return nameDialog.Left() + (((nameDialog.Width() / 4) * 3) - (okButton.Width() / 2)) end)
okButton.OnClick = function(self, modifiers)
local newName = nameEdit:GetText()
callback(newName)
nameDialog:Destroy()
nameDialog = false
end
nameEdit.OnEnterPressed = function(self, text)
okButton.OnClick()
end
end