Repository navigation
Expand file tree
/
Copy pathwidgets.cpp
More file actions
2753 lines (2514 loc) · 123 KB
/
Copy pathwidgets.cpp
File metadata and controls
2753 lines (2514 loc) · 123 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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "hudfix.h"
#include <MinHook.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
#include <cmath>
#include <cstring>
#include <memory>
#include <numbers>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cassert>
namespace hudfix
{
namespace
{
using GetDisplayInfoFn = bool(__thiscall*)(void* iface, void* data, fb::GFxDisplayInfo* out);
using SetDisplayInfoFn = bool(__thiscall*)(void* iface, void* data, const fb::GFxDisplayInfo* info);
using GetCxformFn = bool(__thiscall*)(void* iface, void* data, fb::GFxCxform* out);
using SetCxformFn = bool(__thiscall*)(void* iface, void* data, const fb::GFxCxform* cxform);
using ObjectReleaseFn = void(__thiscall*)(void* iface, fb::GFxValue* value, void* data);
using ResolveCharacterFn = fb::GFxASCharacter*(__thiscall*)(void* handle, void* movieRoot);
using InvokeFn = bool(__thiscall*)(void* iface, void* data, fb::GFxValue* result, const char* name, const fb::GFxValue* args, unsigned int count, bool isDisplayObject);
using GetMemberFn = bool(__thiscall*)(void* iface, void* data, const char* name, fb::GFxValue* out, bool isDisplayObject);
using SetMemberFn = bool(__thiscall*)(void* iface, void* data, const char* name, const fb::GFxValue* value, bool isDisplayObject);
struct MemberVisitor;
using VisitMembersFn = void(__thiscall*)(void* iface, void* data, MemberVisitor* visitor, bool isDisplayObject);
using DisplayFn = void(__thiscall*)(fb::GFxMovieView* movie);
using AdvanceFn = float(__thiscall*)(fb::GFxMovieView* movie, float dt, unsigned int catchUp);
using InitializeScreenFn = void(__thiscall*)(fb::UIScreenManager* manager, const char* screen);
using SetScreenVisibleFn = bool(__thiscall*)(fb::UIScreenManager* manager, fb::UIScreenData* screen, bool visible);
using OnMessageFn = void(__thiscall*)(void* component, const fb::Message* message);
DisplayFn oDisplay = nullptr;
AdvanceFn oAdvance = nullptr;
InitializeScreenFn oInitializeScreen = nullptr;
SetScreenVisibleFn oSetScreenVisible = nullptr;
OnMessageFn oWeaponMessage = nullptr;
OnMessageFn oHudTextMessage = nullptr;
OnMessageFn oBulletHit = nullptr;
constexpr int MESSAGE_DAMAGE_GIVEN = 93974635; // UIDamageGivenToEnemyMessage
constexpr int MESSAGE_KILLED_OTHER = -1963112840; // UINetworkKilledOtherPlayerMessage
constexpr int MESSAGE_BULLET_HIT = 0x47F6C6F0; // BulletToSoldierComponentsHitMessage
constexpr unsigned int HEAD_BONE = 1;
// ghetto
constexpr long long KILL_WINDOW_MS = 300;
constexpr long long HEADSHOT_WINDOW_MS = 1000;
std::atomic<Element> g_hitMarker{ HitMarkers };
std::atomic<long long> g_lastKillTime{ -KILL_WINDOW_MS };
std::atomic<long long> g_lastHeadshotTime{ -HEADSHOT_WINDOW_MS };
// the commo rose picks its options by mouse distance in screen pixels, so ActionScript sees its screen at the unscaled size
constexpr unsigned int COMMO_ROSE_SCREEN[4] = { 0xC9639154, 0x4E99D107, 0x9569DEB4, 0x66673E52 };
constexpr unsigned int WIDGET_MANAGED = 0x40;
constexpr unsigned short VARS_TRANSFORM = 0x1 | 0x2 | 0x8 | 0x10;
constexpr unsigned short VARS_ALPHA = 0x20; // SetDisplayInfo sub_12F0240: m_alpha in percent
constexpr unsigned short VARS_VISIBLE = 0x40;
// for chase veh crosshair
constexpr int THICKEN_PASSES = 8;
constexpr double THICKEN_PX = 1.5;
constexpr unsigned int MAX_STYLES = 256;
// a color transform per channel: mult, then add (0..1)
struct Recolor
{
float mult[3];
float add[3];
};
constexpr int MAX_SHAPES = 256;
constexpr int SHAPE_LEVELS = 6;
struct ShapeMatrix
{
float* matrix;
float saved[6];
};
ShapeMatrix g_shapes[MAX_SHAPES];
int g_shapeCount = 0;
struct ShapeCxform
{
fb::GFxCxform* cxform;
fb::GFxCxform saved;
};
// every shape of every vehicle screen in the list, shown or not, about 35 a widget
constexpr int MAX_SHAPE_CXFORMS = 8192;
ShapeCxform g_shapeCxforms[MAX_SHAPE_CXFORMS];
int g_shapeCxformCount = 0;
// Flash blend modes (GRenderer::BlendType)
constexpr unsigned char BLEND_NORMAL = 1;
constexpr unsigned char BLEND_ADD = 8;
struct BlendSave
{
unsigned char* blend;
unsigned char saved;
};
BlendSave g_blendSaves[MAX_SHAPES];
int g_blendSaveCount = 0;
// GFxASCharacter m_asFlags (GetAcceptAnimMoves sub_1316D40)
constexpr unsigned int ACCEPT_ANIM_MOVES = 0x1000;
enum WidgetType
{
NUMBER = 3,
STRING = 4
};
// GFxCharacter m_flags
constexpr unsigned char CHARACTER_SCRIPTED = 0x80;
// GFxASCharacter m_asFlags
constexpr unsigned int CHARACTER_VISIBLE = 0x1;
constexpr unsigned int CHARACTER_USED_AS_MASK = 0x8000;
constexpr float TWIPS = 20.0f;
constexpr int OUTLINE_LEVELS = 8;
constexpr int OBJECT_SPRITE = 2;
constexpr int MAX_CHILDREN = 64;
constexpr int CHILD_LEVELS = 4;
constexpr double MOVED = 0.5;
constexpr ULONGLONG RESCAN_MS = 1000;
// parent -> stage: stage = offset + scale * p
struct Frame
{
double x = 0.0, y = 0.0, sx = 1.0, sy = 1.0;
};
class Clip
{
public:
Clip() = default;
Clip(const Clip&) = delete;
Clip& operator=(const Clip&) = delete;
~Clip()
{
if (m_value.m_type & WIDGET_MANAGED)
reinterpret_cast<ObjectReleaseFn>(OFF_GFxValue_objectRelease)(m_value.m_objectInterface, &m_value, m_value.m_data);
}
bool resolve(fb::GFxMovieView* movie, const char* path)
{
return movie->getVariable(&m_value, path) && m_value.m_objectInterface;
}
bool read(fb::GFxDisplayInfo* info)
{
return reinterpret_cast<GetDisplayInfoFn>(OFF_GFxValue_getDisplayInfo)(m_value.m_objectInterface, m_value.m_data, info);
}
void write(const fb::GFxDisplayInfo* info)
{
reinterpret_cast<SetDisplayInfoFn>(OFF_GFxValue_setDisplayInfo)(m_value.m_objectInterface, m_value.m_data, info);
}
bool readCxform(fb::GFxCxform* cxform)
{
return reinterpret_cast<GetCxformFn>(OFF_GFxValue_getCxform)(m_value.m_objectInterface, m_value.m_data, cxform);
}
void writeCxform(const fb::GFxCxform* cxform)
{
reinterpret_cast<SetCxformFn>(OFF_GFxValue_setCxform)(m_value.m_objectInterface, m_value.m_data, cxform);
}
// ObjectInterface: movie root +0x0, the value's data is the character handle
fb::GFxASCharacter* character()
{
void* movieRoot = m_value.m_objectInterface ? *static_cast<void**>(m_value.m_objectInterface) : nullptr;
return movieRoot ? reinterpret_cast<ResolveCharacterFn>(OFF_GFxCharacterHandle_resolve)(m_value.m_data, movieRoot) : nullptr;
}
unsigned int* characterFlags()
{
fb::GFxASCharacter* owner = character();
return owner ? &owner->m_asFlags : nullptr;
}
// replace keeps only the alpha (add is in 0..255, Cxform::Transform
// else the clip's own colors are multiplied
void tint(const Color& color, bool replace)
{
fb::GFxCxform tinted{ };
if (!readCxform(&tinted))
return;
const float rgb[3] = { color.r, color.g, color.b };
for (int i = 0; i < 3; ++i)
{
if (replace)
{
tinted.m_rows[i][0] = 0.0f;
tinted.m_rows[i][1] = rgb[i] * 255.0f;
}
else
{
tinted.m_rows[i][0] *= rgb[i];
tinted.m_rows[i][1] *= rgb[i];
}
}
writeCxform(&tinted);
}
// per channel: the clip's colors times mult plus add (0..1)
void recolor(const Recolor& r)
{
fb::GFxCxform cxform{ };
if (!readCxform(&cxform))
return;
for (int i = 0; i < 3; ++i)
{
cxform.m_rows[i][1] = cxform.m_rows[i][1] * r.mult[i] + r.add[i] * 255.0f;
cxform.m_rows[i][0] *= r.mult[i];
}
writeCxform(&cxform);
}
bool set(const char* member, const fb::GFxValue& value)
{
return reinterpret_cast<SetMemberFn>(OFF_GFxValue_setMember)(m_value.m_objectInterface, m_value.m_data, member, &value, true);
}
// a method's result kept in out, like TextField.getTextFormat
bool invokeInto(const char* method, const fb::GFxValue* args, unsigned int count, Clip& out)
{
return reinterpret_cast<InvokeFn>(OFF_GFxValue_invoke)(m_value.m_objectInterface, m_value.m_data, &out.m_value, method, args, count, true)
&& out.m_value.m_objectInterface;
}
// the value itself, to pass as an argument
const fb::GFxValue* value() const
{
return &m_value;
}
// a member of a plain object (not a display object), like a TextFormat's size
bool setField(const char* member, const fb::GFxValue& value)
{
return reinterpret_cast<SetMemberFn>(OFF_GFxValue_setMember)(m_value.m_objectInterface, m_value.m_data, member, &value, false);
}
double fieldNumber(const char* member, double fallback)
{
fb::GFxValue out{ };
const bool ok = reinterpret_cast<GetMemberFn>(OFF_GFxValue_getMember)(m_value.m_objectInterface, m_value.m_data, member, &out, false);
const double result = ok && out.m_type == NUMBER ? out.m_number : fallback;
if (out.m_type & WIDGET_MANAGED)
reinterpret_cast<ObjectReleaseFn>(OFF_GFxValue_objectRelease)(out.m_objectInterface, &out, out.m_data);
return result;
}
// a string member, empty if it is none
std::string string(const char* member)
{
fb::GFxValue out{ };
const bool ok = reinterpret_cast<GetMemberFn>(OFF_GFxValue_getMember)(m_value.m_objectInterface, m_value.m_data, member, &out, true);
// a string of the movie is managed: its data points at the text's pointer
const char* chars = nullptr;
if (ok && (out.m_type & 0x3F) == STRING && out.m_data)
chars = out.m_type & WIDGET_MANAGED ? *static_cast<const char* const*>(out.m_data) : static_cast<const char*>(out.m_data);
std::string result = chars ? chars : "";
if (out.m_type & WIDGET_MANAGED)
reinterpret_cast<ObjectReleaseFn>(OFF_GFxValue_objectRelease)(out.m_objectInterface, &out, out.m_data);
return result;
}
// the names of its display object members (child clips, text fields)
std::vector<std::string> children();
double number(const char* member, double fallback)
{
fb::GFxValue out{ };
const bool ok = reinterpret_cast<GetMemberFn>(OFF_GFxValue_getMember)(m_value.m_objectInterface, m_value.m_data, member, &out, true);
const double result = ok && out.m_type == NUMBER ? out.m_number : fallback;
if (out.m_type & WIDGET_MANAGED)
reinterpret_cast<ObjectReleaseFn>(OFF_GFxValue_objectRelease)(out.m_objectInterface, &out, out.m_data);
return result;
}
// ActionScript method call
bool invoke(const char* method, const fb::GFxValue* args, unsigned int count, double* number = nullptr)
{
fb::GFxValue result{ };
const bool ok = reinterpret_cast<InvokeFn>(OFF_GFxValue_invoke)(m_value.m_objectInterface, m_value.m_data, &result, method, args, count, true);
if (number)
*number = result.m_type == NUMBER ? result.m_number : 0.0;
if (result.m_type & WIDGET_MANAGED)
reinterpret_cast<ObjectReleaseFn>(OFF_GFxValue_objectRelease)(result.m_objectInterface, &result, result.m_data);
return ok;
}
// MovieClip.getBounds(), in the space of target if given
bool localBounds(double out[4], const Clip* target = nullptr)
{
fb::GFxValue result{ };
const bool invoked = reinterpret_cast<InvokeFn>(OFF_GFxValue_invoke)(m_value.m_objectInterface, m_value.m_data, &result, "getBounds",
target ? target->value() : nullptr, target ? 1 : 0, true);
assert(invoked && result.m_objectInterface);
constexpr const char* names[4] = { "xMin", "yMin", "xMax", "yMax" };
bool ok = result.m_objectInterface != nullptr;
for (int i = 0; ok && i < 4; ++i)
{
fb::GFxValue member{ };
ok = reinterpret_cast<GetMemberFn>(OFF_GFxValue_getMember)(result.m_objectInterface, result.m_data, names[i], &member, false) && member.m_type == NUMBER;
out[i] = member.m_number;
}
if (result.m_type & WIDGET_MANAGED)
reinterpret_cast<ObjectReleaseFn>(OFF_GFxValue_objectRelease)(result.m_objectInterface, &result, result.m_data);
return ok;
}
private:
fb::GFxValue m_value{ };
};
struct Scaled
{
Clip clip;
fb::GFxDisplayInfo base{ };
fb::GFxCxform baseCxform{ };
bool tinted = false;
unsigned int* flags = nullptr;
unsigned int acceptAnimMoves = 0;
// before any write
void keepTimeline()
{
flags = clip.characterFlags();
acceptAnimMoves = flags ? *flags & ACCEPT_ANIM_MOVES : 0;
}
};
std::vector<std::unique_ptr<Scaled>> g_scaled;
// measured after Advance (getBounds runs ActionScript, which hangs inside Display)
std::unordered_map<std::string, std::pair<double, double>> g_corners;
// screen -> the minimap and compass bounds { x0, y0, x1, y1 } in the widgets' parent space, measured unscaled
std::unordered_map<std::string, std::array<double, 4>> g_minimapOutline;
// the minimap background's path -> its bounds { x0, y0, x1, y1 } in the parent space, measured unscaled
std::unordered_map<std::string, std::array<double, 4>> g_backgroundBounds;
// a clip inside a vehicle widget
struct Child
{
std::string path;
double restX = 0.0, restY = 0.0;
bool moving = false;
};
struct ChildSet
{
std::vector<Child> clips;
ULONGLONG scanned = 0;
};
// vehicle widget path -> its clips, looked for again every RESCAN_MS
std::unordered_map<std::string, ChildSet> g_children;
// a part of the minimap or its compass with its bounds in the widget's space, while they hold still
struct OutlinePart
{
std::array<double, 4> bounds{ };
bool varied = false;
};
// character -> what is known of it, the parts drawn this frame kept
std::unordered_map<const fb::GFxCharacter*, OutlinePart> g_outlineParts;
Frame clipFrame(fb::GFxMovieView* movie, const std::string& path)
{
Frame frame;
Clip clip;
fb::GFxDisplayInfo info{ };
if (clip.resolve(movie, path.c_str()) && clip.read(&info))
frame = Frame{ info.m_x, info.m_y, info.m_xScale / 100.0, info.m_yScale / 100.0 };
return frame;
}
// "<screen>" under _root, then its "instance1": the widgets' parent
Frame screenFrame(fb::GFxMovieView* movie, const char* screen)
{
const Frame a = clipFrame(movie, screen);
const Frame b = clipFrame(movie, std::string(screen) + ".instance1");
return Frame{ a.x + a.sx * b.x, a.y + a.sy * b.y, a.sx * b.sx, a.sy * b.sy };
}
// the point of the visible stage the widget is aligned to, in its parent's space
void alignAnchor(const fb::WidgetNode* widget, const fb::GRectF& visible, const Frame& parent, double& ax, double& ay)
{
double x = (visible.m_left + visible.m_right) * 0.5;
if (widget->m_HorisontalAlign == fb::WHA_Left)
x = visible.m_left;
else if (widget->m_HorisontalAlign == fb::WHA_Right)
x = visible.m_right;
double y = (visible.m_top + visible.m_bottom) * 0.5;
if (widget->m_VerticalAlign == fb::WVA_Top)
y = visible.m_top;
else if (widget->m_VerticalAlign == fb::WVA_Bottom)
y = visible.m_bottom;
ax = (x - parent.x) / parent.sx;
ay = (y - parent.y) / parent.sy;
}
bool hasWidget(const fb::UIScreenData* screen, const char* instanceName)
{
for (fb::WidgetNode** w = screen->m_widgetsBegin; w != screen->m_widgetsEnd; ++w)
{
if (*w && (*w)->m_InstanceName && std::strcmp((*w)->m_InstanceName, instanceName) == 0)
return true;
}
return false;
}
// a widget whose clip could not be made, so initializeScreen does not look for it
void removeWidget(fb::UIScreenData* screen, const char* instanceName)
{
fb::WidgetNode** end = std::remove_if(screen->m_widgetsBegin, screen->m_widgetsEnd, [instanceName](const fb::WidgetNode* w)
{
return w && w->m_InstanceName && std::strcmp(w->m_InstanceName, instanceName) == 0;
});
screen->m_widgetsEnd = end;
}
constexpr double TABS_GAP = 8.0;
constexpr int TABS_FIT_FRAMES = 120; // how long the tab bar may take to lay its tabs out
// list's height has to be known before its onLoad, which turns its clip scale into its mask size
constexpr double TABS_BOUNDS[4] = { -1.0, -8.0, 1091.0, 64.0 };
struct HudTabsArea
{
fb::GFxMovieView* movie = nullptr;
std::string parent;
double left = 0.0, top = 0.0, width = 0.0;
int fitFrames = 0; // left to try, 0 when placed or without tabs
};
HudTabsArea g_tabsArea;
double tabsScale(double width, const double bounds[4])
{
return (std::min)(1.0, width / (bounds[2] - bounds[0]));
}
// scaled into the list's width over its top, by the bounds the tabs really got
void fitHudTabs(fb::GFxMovieView* movie)
{
HudTabsArea& a = g_tabsArea;
if (!a.fitFrames || movie != a.movie)
return;
--a.fitFrames;
Clip tabs;
fb::GFxDisplayInfo info{ };
double b[4];
if (!tabs.resolve(movie, (a.parent + "." + HUD_TABS_NAME).c_str()) || !tabs.localBounds(b) || !tabs.read(&info))
return;
const bool placeholder = b[0] == 0.0 && b[1] == 0.0 && b[2] == 100.0 && b[3] == 100.0;
if (placeholder || b[2] <= b[0] || b[3] <= b[1])
return;
const double scale = tabsScale(a.width, b);
info.m_x = a.left - b[0] * scale;
info.m_y = a.top - b[1] * scale;
info.m_xScale = info.m_yScale = scale * 100.0;
info.m_varsSet = 0x1 | 0x2 | 0x8 | 0x10;
tabs.write(&info);
a.fitFrames = 0;
log("{}: tabs {:.0f},{:.0f} .. {:.0f},{:.0f} at {:.0f}%", HUD_TABS_NAME, b[0], b[1], b[2], b[3], scale * 100.0);
}
// TabBar_01 duplicated over the list's top; initializeScreen then gives it HUD_TABS_NAME's tabs, fitHudTabs places it
double createHudTabsClip(fb::GFxMovieView* movie, fb::UIScreenData* screen, Clip& holder, const std::string& parent, double left, double top, double width)
{
if (!hasWidget(screen, HUD_TABS_NAME))
return 0.0;
Clip main, created;
fb::GFxDisplayInfo info{ };
double depth = 0.0;
fb::GFxValue args[2]{ };
args[0].m_type = STRING;
args[0].m_data = const_cast<char*>(HUD_TABS_NAME);
args[1].m_type = NUMBER;
bool made = holder.invoke("getNextHighestDepth", nullptr, 0, &depth)
&& main.resolve(movie, (parent + ".TabBar_01").c_str())
&& (args[1].m_number = depth, main.invoke("duplicateMovieClip", args, 2))
&& created.resolve(movie, (parent + "." + HUD_TABS_NAME).c_str()) && created.read(&info);
if (made)
{
info.m_x = left;
info.m_y = top;
info.m_varsSet = 0x1 | 0x2;
created.write(&info);
made = created.invoke("onLoad", nullptr, 0);
log("{}: clip on {} at ({:.0f}, {:.0f})", HUD_TABS_NAME, screen->m_instanceName, info.m_x, info.m_y);
}
if (made)
{
fb::GFxValue none{ };
none.m_type = 1U;
created.set("onLoad", none);
g_tabsArea = { movie, parent, left, top, width, TABS_FIT_FRAMES };
return (TABS_BOUNDS[3] - TABS_BOUNDS[1]) * tabsScale(width, TABS_BOUNDS) + TABS_GAP;
}
log("{}: clip NOT created on {}", HUD_TABS_NAME, screen->m_instanceName);
removeWidget(screen, HUD_TABS_NAME);
return 0.0;
}
// GFxValue::ObjectInterface::ObjVisitor
struct MemberVisitor
{
std::vector<std::string> found;
virtual ~MemberVisitor() = default;
virtual void visit(const char* name, const fb::GFxValue* value)
{
if (name && (value->m_type & 0x3F) == 8)
found.emplace_back(name);
}
};
std::vector<std::string> Clip::children()
{
MemberVisitor visitor;
reinterpret_cast<VisitMembersFn>(OFF_GFxValue_visitMembers)(m_value.m_objectInterface, m_value.m_data, &visitor, true);
return visitor.found;
}
// all paddings are not automated by UI framework, because it was designed to work in flash
constexpr const char* PICKER_NAME = "HudFixPicker";
constexpr double ROW_HEIGHT = 28.0; // TextSelectorRow_28px
constexpr double ROW_PITCH = 30.0;
constexpr double ROW_BOUNDS_BOTTOM = 33.0; // a row clip's local bounds: -1,-1 .. 461,33
constexpr double PANEL_GAP = 8.0; // under List_01's last row
constexpr double PANEL_PAD = 10.0;
constexpr double SQUARE = 124.0;
constexpr double HUE_GAP = 8.0;
constexpr double HUE_WIDTH = 16.0;
constexpr double SWATCH_WIDTH = 150.0;
constexpr double SWATCH_HEIGHT = 36.0;
constexpr double CAPTION_HEIGHT = 20.0;
constexpr double BODY_Y = ROW_PITCH; // the body under the header row, a row spacing apart
constexpr double BODY_HEIGHT = PANEL_PAD + SQUARE + PANEL_PAD;
constexpr double SQUARE_X = PANEL_PAD;
constexpr double SQUARE_Y = BODY_Y + PANEL_PAD;
constexpr double HUE_X = SQUARE_X + SQUARE + HUE_GAP;
constexpr double ALPHA_X = HUE_X + HUE_WIDTH + HUE_GAP; // the opacity strip
constexpr double SWATCH_X = ALPHA_X + HUE_WIDTH + 3.0 * PANEL_PAD;
constexpr double OPACITY_LOW = 0.1; // the options' lowest opacity
constexpr int ALPHA_STEPS = 24;
constexpr double CHECKER = 8.0; // behind what has opacity
constexpr int CHECKER_DARK = 0x2A2A2A;
constexpr int CHECKER_LIGHT = 0x6A6A6A;
constexpr ULONGLONG CLOSE_DELAY = 400; // ms another row stays highlighted before the picker closes
constexpr double HEX_X = SWATCH_X + SWATCH_WIDTH + 2.0 * PANEL_PAD; // the hex input, right of the swatches
constexpr double HEX_WIDTH = 120.0;
constexpr int HEX_BOX = 0x0E141A;
constexpr int ROW_TOP = 0x273640;
constexpr int ROW_BOTTOM = 0x202D37;
constexpr int ROW_LINE = 0x040810;
constexpr double ROW_ALPHA = 92.0;
constexpr int SHADES = 8; // steps of the row's shading
constexpr int PICKER_STEPS = 40; // steps of the square's fades (beginGradientFill stays black)
constexpr int HUE_SLICES = 72;
enum class Drag
{
None,
Square,
Hue,
Alpha
};
struct Picker
{
fb::GFxMovieView* movie = nullptr;
std::string path; // the picker clip
std::string listPath; // the shown HUD list
std::string mainListPath; // List_01, the panel goes under its rows
double x = 0.0, width = 0.0, listY = 0.0;
int slot = -1; // color slot shown, -1 hidden
int original = 0xFFFFFF; // the slot's color when it was shown
double originalAlpha = 1.0;
int element = ElementCount; // whose opacity it sets, ElementCount for none
bool custom = true; // its row is on CUSTOM, else only the opacity is set
bool hexEditing = false; // the hex box is typed into: the game gets no keyboard meanwhile
std::string hexTyped;
bool keyWas[256] = { }; // key states of the last frame, to see presses
double h = 0.0, s = 0.0, v = 1.0;
double alpha = 1.0;
Drag drag = Drag::None;
bool wasDown = false;
ULONGLONG otherRowSince = 0; // another row is highlighted since, 0 if not
};
Picker g_picker;
fb::GFxValue number(double value)
{
fb::GFxValue v{ };
v.m_type = NUMBER;
v.m_number = value;
return v;
}
fb::GFxValue text(const char* value)
{
fb::GFxValue v{ };
v.m_type = STRING;
v.m_data = const_cast<char*>(value);
return v;
}
fb::GFxValue boolean(bool value)
{
fb::GFxValue v{ };
v.m_type = 2;
*reinterpret_cast<bool*>(&v.m_data) = value;
return v;
}
int hsvToRgb(double h, double s, double v)
{
const double c = v * s;
const double x = c * (1.0 - std::fabs(std::fmod(h * 6.0, 2.0) - 1.0));
const double m = v - c;
double r = 0.0, g = 0.0, b = 0.0;
switch (static_cast<int>(h * 6.0) % 6)
{
case 0: r = c; g = x; break;
case 1: r = x; g = c; break;
case 2: g = c; b = x; break;
case 3: g = x; b = c; break;
case 4: r = x; b = c; break;
default: r = c; b = x; break;
}
const auto byte = [m](double channel) { return static_cast<int>(std::lround((channel + m) * 255.0)); };
return byte(r) << 16 | byte(g) << 8 | byte(b);
}
void rgbToHsv(int rgb, double& h, double& s, double& v)
{
const double r = ((rgb >> 16) & 0xFF) / 255.0, g = ((rgb >> 8) & 0xFF) / 255.0, b = (rgb & 0xFF) / 255.0;
const double high = (std::max)({ r, g, b }), low = (std::min)({ r, g, b }), d = high - low;
v = high;
s = high > 0.0 ? d / high : 0.0;
if (d <= 0.0)
return; // grey keeps the hue it had
if (high == r)
h = std::fmod((g - b) / d + 6.0, 6.0) / 6.0;
else if (high == g)
h = ((b - r) / d + 2.0) / 6.0;
else
h = ((r - g) / d + 4.0) / 6.0;
}
int mixRgb(int a, int b, double t)
{
int out = 0;
for (int shift = 16; shift >= 0; shift -= 8)
{
const double channel = ((a >> shift) & 0xFF) * (1.0 - t) + ((b >> shift) & 0xFF) * t;
out |= static_cast<int>(std::lround(channel)) << shift;
}
return out;
}
void drawRect(Clip& clip, double x, double y, double w, double h)
{
const fb::GFxValue start[2] = { number(x), number(y) };
clip.invoke("moveTo", start, 2);
const double corners[4][2] = { { x + w, y }, { x + w, y + h }, { x, y + h }, { x, y } };
for (const auto& corner : corners)
{
const fb::GFxValue to[2] = { number(corner[0]), number(corner[1]) };
clip.invoke("lineTo", to, 2);
}
}
void fillRect(Clip& clip, double x, double y, double w, double h, int rgb, double alpha)
{
const fb::GFxValue fill[2] = { number(rgb), number(alpha) };
clip.invoke("beginFill", fill, 2);
drawRect(clip, x, y, w, h);
clip.invoke("endFill", nullptr, 0);
}
// a frame just outside the rect
void frameRect(Clip& clip, double x, double y, double w, double h, int rgb)
{
fillRect(clip, x - 1.0, y - 1.0, w + 2.0, 1.0, rgb, 100.0);
fillRect(clip, x - 1.0, y + h, w + 2.0, 1.0, rgb, 100.0);
fillRect(clip, x - 1.0, y, 1.0, h, rgb, 100.0);
fillRect(clip, x + w, y, 1.0, h, rgb, 100.0);
}
// a color fading over the rect in strips, down when vertical
void fadeRect(Clip& clip, double x, double y, double w, double h, int rgb, double fromAlpha, double toAlpha, bool vertical)
{
for (int i = 0; i < PICKER_STEPS; ++i)
{
const double alpha = fromAlpha + (toAlpha - fromAlpha) * (i + 0.5) / PICKER_STEPS;
if (vertical)
fillRect(clip, x, y + h * i / PICKER_STEPS, w, h / PICKER_STEPS, rgb, alpha);
else
fillRect(clip, x + w * i / PICKER_STEPS, y, w / PICKER_STEPS, h, rgb, alpha);
}
}
// one of the menu's rows: shaded down, its dark line under it
void rowRect(Clip& clip, double x, double y, double w, double h)
{
for (int i = 0; i < SHADES; ++i)
fillRect(clip, x, y + h * i / SHADES, w, h / SHADES, mixRgb(ROW_TOP, ROW_BOTTOM, (i + 0.5) / SHADES), ROW_ALPHA);
fillRect(clip, x, y + h, w, 1.0, ROW_LINE, 100.0);
}
void checker(Clip& clip, double x, double y, double w, double h)
{
fillRect(clip, x, y, w, h, CHECKER_DARK, 100.0);
for (double cy = 0.0; cy < h; cy += CHECKER)
{
for (double cx = std::fmod(cy / CHECKER, 2.0) * CHECKER; cx < w; cx += 2.0 * CHECKER)
fillRect(clip, x + cx, y + cy, (std::min)(CHECKER, w - cx), (std::min)(CHECKER, h - cy), CHECKER_LIGHT, 100.0);
}
}
// a color at an opacity over the checker
void swatchRect(Clip& clip, double x, double y, double w, double h, int rgb, double alpha)
{
if (alpha < 1.0)
checker(clip, x, y, w, h);
fillRect(clip, x, y, w, h, rgb, alpha * 100.0);
}
bool pickerPart(const char* part, Clip& clip)
{
return clip.resolve(g_picker.movie, (g_picker.path + "." + part).c_str());
}
void moveClip(Clip& clip, double x, double y)
{
fb::GFxDisplayInfo info{ };
info.m_x = x;
info.m_y = y;
info.m_varsSet = 0x1 | 0x2;
clip.write(&info);
}
// List_01's row label format: a text field of one of its rows
bool listTextFormat(Clip& format)
{
static bool logged = false;
Clip holder;
if (!holder.resolve(g_picker.movie, (g_picker.mainListPath + ".m_ListHolder").c_str()))
return false;
for (const std::string& row : holder.children())
{
if (row.rfind("row_", 0) != 0)
continue;
// the row's label (TextSelectorRow_28px), not its dummyTextField!!!
std::vector<std::pair<std::string, std::string>> texts;
const std::string rowPath = g_picker.mainListPath + ".m_ListHolder." + row;
Clip label;
if (label.resolve(g_picker.movie, (rowPath + ".Label.txtField").c_str()) && !label.string("text").empty())
texts.emplace_back(rowPath + ".Label.txtField", label.string("text"));
for (const auto& [path, value] : texts)
{
Clip field;
if (!field.resolve(g_picker.movie, path.c_str()) || !field.invokeInto("getTextFormat", nullptr, 0, format))
continue;
if (!logged)
{
logged = true;
log("{}: font of {} ('{}'), size {:.0f}", PICKER_NAME, path, value, format.fieldNumber("size", 0.0));
}
return true;
}
}
if (!logged)
{
logged = true;
log("{}: no text field in {} rows", PICKER_NAME, g_picker.mainListPath);
}
return false;
}
// a text field in the list's font, at size percent of its rows' labels, aligned right when right
void createLabel(Clip& picker, const char* name, int depth, double x, double y, double w, double h, double size, bool right)
{
const fb::GFxValue args[6] = { text(name), number(depth), number(x), number(y), number(w), number(h) };
picker.invoke("createTextField", args, 6);
Clip field, format;
if (!pickerPart(name, field))
return;
field.set("selectable", boolean(false));
if (listTextFormat(format))
{
field.set("embedFonts", boolean(true));
const double base = format.fieldNumber("size", 0.0);
if (base > 0.0)
format.setField("size", number(std::round(base * size)));
if (right)
format.setField("align", text("right"));
const fb::GFxValue formatArg[1] = { *format.value() };
field.invoke("setNewTextFormat", formatArg, 1);
}
}
void setLabel(const char* name, const char* value)
{
Clip field;
if (pickerPart(name, field))
field.set("text", text(value));
}
// the opacity strip's x: next to the hue strip, or in the square's place on DEFAULT
double alphaX()
{
return g_picker.custom ? ALPHA_X : SQUARE_X;
}
void setVisible(Clip& clip, bool shown)
{
fb::GFxDisplayInfo info{ };
info.m_visible = shown;
info.m_varsSet = VARS_VISIBLE;
clip.write(&info);
}
// the square for the hue, the opacity strip for the color, the new color's swatch, the value and the marks
void drawPicker(bool square, bool strip)
{
Picker& p = g_picker;
const int rgb = hsvToRgb(p.h, p.s, p.v);
const bool hasAlpha = p.element != ElementCount;
Clip part;
if (square && pickerPart("square", part))
{
// the hue, white over it fading out to the right, black over both fading in downwards
part.invoke("clear", nullptr, 0);
fillRect(part, SQUARE_X, SQUARE_Y, SQUARE, SQUARE, hsvToRgb(p.h, 1.0, 1.0), 100.0);
fadeRect(part, SQUARE_X, SQUARE_Y, SQUARE, SQUARE, 0xFFFFFF, 100.0, 0.0, false);
fadeRect(part, SQUARE_X, SQUARE_Y, SQUARE, SQUARE, 0x000000, 0.0, 100.0, true);
frameRect(part, SQUARE_X, SQUARE_Y, SQUARE, SQUARE, ROW_LINE);
}
Clip alphaStrip;
if (strip && pickerPart("alpha", alphaStrip))
{
// the color from opaque at the top to the lowest opacity at the bottom
alphaStrip.invoke("clear", nullptr, 0);
if (hasAlpha)
{
const double x = alphaX();
const int shown = p.custom ? rgb : 0xFFFFFF;
checker(alphaStrip, x, SQUARE_Y, HUE_WIDTH, SQUARE);
for (int i = 0; i < ALPHA_STEPS; ++i)
{
const double alpha = 1.0 - (1.0 - OPACITY_LOW) * (i + 0.5) / ALPHA_STEPS;
fillRect(alphaStrip, x, SQUARE_Y + SQUARE * i / ALPHA_STEPS, HUE_WIDTH, SQUARE / ALPHA_STEPS, shown, alpha * 100.0);
}
frameRect(alphaStrip, x, SQUARE_Y, HUE_WIDTH, SQUARE, ROW_LINE);
}
}
Clip swatch;
if (pickerPart("swatch", swatch))
{
swatch.invoke("clear", nullptr, 0);
swatchRect(swatch, SWATCH_X, SQUARE_Y + SQUARE - SWATCH_HEIGHT, SWATCH_WIDTH, SWATCH_HEIGHT, rgb, p.alpha);
frameRect(swatch, SWATCH_X, SQUARE_Y + SQUARE - SWATCH_HEIGHT, SWATCH_WIDTH, SWATCH_HEIGHT, ROW_LINE);
}
char value[32];
const int percent = static_cast<int>(std::lround(p.alpha * 100.0));
if (!p.custom)
std::snprintf(value, sizeof(value), "DEFAULT %d%%", percent);
else if (hasAlpha)
std::snprintf(value, sizeof(value), "#%06X %d%%", rgb, percent);
else
std::snprintf(value, sizeof(value), "#%06X", rgb);
setLabel("value", value);
if (!p.hexEditing)
{
char hex[8];
std::snprintf(hex, sizeof(hex), "#%06X", rgb);
setLabel("hex", hex);
}
Clip mark;
if (pickerPart("squareMark", mark))
moveClip(mark, SQUARE_X + p.s * SQUARE, SQUARE_Y + (1.0 - p.v) * SQUARE);
Clip hueMark;
if (pickerPart("hueMark", hueMark))
moveClip(hueMark, HUE_X, SQUARE_Y + p.h * SQUARE);
Clip alphaMark;
if (pickerPart("alphaMark", alphaMark))
{
moveClip(alphaMark, alphaX(), SQUARE_Y + (1.0 - p.alpha) / (1.0 - OPACITY_LOW) * SQUARE);
setVisible(alphaMark, hasAlpha);
}
}
void showPicker(bool shown)
{
Clip picker;
if (!picker.resolve(g_picker.movie, g_picker.path.c_str()))
return;
fb::GFxDisplayInfo info{ };
info.m_visible = shown;
info.m_varsSet = VARS_VISIBLE;
picker.write(&info);
}
// under List_01's rows, which are only laid out once the page is set up
void placePicker()
{
Picker& p = g_picker;
Clip holder, picker;
double b[4];
if (!picker.resolve(p.movie, p.path.c_str()))
return;
// its last row starts ROW_BOUNDS_BOTTOM above the rows' bounds end
const double rows = holder.resolve(p.movie, (p.mainListPath + ".m_ListHolder").c_str()) && holder.localBounds(b) && b[3] > b[1]
? std::round((b[3] - ROW_BOUNDS_BOTTOM) / ROW_PITCH) * ROW_PITCH + ROW_PITCH : 0.0;
moveClip(picker, p.x, p.listY + rows + PANEL_GAP);
}
// the static parts: the header row, the body behind the square, the hue strip, the old color, the marks
void buildPicker(Clip& picker)
{
const Picker& p = g_picker;
constexpr const char* PARTS[9] = { "background", "square", "hue", "alpha", "original", "swatch", "squareMark", "hueMark", "alphaMark" };
for (int i = 0; i < 9; ++i)
{
const fb::GFxValue part[2] = { text(PARTS[i]), number(i + 1) };
picker.invoke("createEmptyMovieClip", part, 2);
}
Clip part;
if (pickerPart("background", part))
{
rowRect(part, 0.0, 0.0, p.width, ROW_HEIGHT);
rowRect(part, 0.0, BODY_Y, p.width, BODY_HEIGHT);
}
Clip hue;
if (pickerPart("hue", hue))
{
for (int i = 0; i < HUE_SLICES; ++i)
fillRect(hue, HUE_X, SQUARE_Y + SQUARE * i / HUE_SLICES, HUE_WIDTH, SQUARE / HUE_SLICES + 0.5, hsvToRgb((i + 0.5) / HUE_SLICES, 1.0, 1.0), 100.0);
frameRect(hue, HUE_X, SQUARE_Y, HUE_WIDTH, SQUARE, ROW_LINE);
}
Clip mark;
if (pickerPart("squareMark", mark))
{
frameRect(mark, -4.0, -4.0, 8.0, 8.0, 0x000000);
frameRect(mark, -3.0, -3.0, 6.0, 6.0, 0xFFFFFF);
}
for (const char* stripMark : { "hueMark", "alphaMark" })
{
Clip markClip;
if (pickerPart(stripMark, markClip))
{
frameRect(markClip, -2.0, -2.0, HUE_WIDTH + 4.0, 4.0, 0x000000);
frameRect(markClip, -1.0, -1.0, HUE_WIDTH + 2.0, 2.0, 0xFFFFFF);
}
}
createLabel(picker, "title", 10, PANEL_PAD, 0.0, p.width / 2.0, ROW_HEIGHT, 1.0, false);
createLabel(picker, "value", 11, p.width / 2.0, 0.0, p.width / 2.0 - PANEL_PAD, ROW_HEIGHT, 1.0, true);
createLabel(picker, "before", 12, SWATCH_X, SQUARE_Y - 2.0, SWATCH_WIDTH, CAPTION_HEIGHT, 0.7, false);
createLabel(picker, "after", 13, SWATCH_X, SQUARE_Y + SQUARE - SWATCH_HEIGHT - CAPTION_HEIGHT - 2.0, SWATCH_WIDTH, CAPTION_HEIGHT, 0.7, false);
createLabel(picker, "note", 14, SQUARE_X + HUE_WIDTH + 2.0 * PANEL_PAD, SQUARE_Y + SQUARE / 2.0 - ROW_HEIGHT / 2.0, p.width / 2.0, ROW_HEIGHT, 1.0, false);
// the hex box: the menu gives Flash input fields no typing, so its keys are read here (typeHex only)
const fb::GFxValue hexBoxPart[2] = { text("hexBox"), number(15) };
picker.invoke("createEmptyMovieClip", hexBoxPart, 2);
Clip hexBox;
if (pickerPart("hexBox", hexBox))