-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathUI_System.cpp
1477 lines (1271 loc) · 48.2 KB
/
UI_System.cpp
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 "UI_System.h"
#include "XL_Console.h"
#include "../render/IDriver3D.h"
#include "../render/TextureCache.h"
#include "../render/FontManager.h"
#include "../fileformats/ArchiveManager.h"
#include "../fileformats/Archive.h"
#include "../fileformats/LFD_Anim.h"
#include "../fileformats/TextureTypes.h"
#include "../math/Math.h"
#include "../EngineSettings.h"
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <memory.h>
/****************************
***** Static Variables *****
****************************/
#define MAX_RENDERFRAMEPOOL_SIZE 16384
#define MAX_LFD_ANIM 16
bool UI_System::m_bScriptsLoaded;
bool UI_System::m_bScriptExeSucceeded;
UI_Screen *UI_System::m_Top; //the current top-level screen.
UI_Screen *UI_System::m_Leaf; //the current bottom level screen (this absorbs the input).
UI_Screen *UI_System::m_Context; //the current UI execute context, basically which UI_Screen the script is currently operating on.
UI_Screen *UI_System::m_PendingScreenChange;
UI_Screen *UI_System::m_PushScreen;
UI_Screen *UI_System::m_PopScreen;
//float UI_System::m_fVirt_Scr_W;
//float UI_System::m_fVirt_Scr_H;
bool UI_System::m_bFlipX;
bool UI_System::m_bFlipY;
std::map<std::string, UI_Screen *> UI_System::m_ScreenMap;
std::vector<UI_Screen *> UI_System::m_ScreenList;
std::vector<UI_Window *> UI_System::m_WindowList;
IDriver3D *UI_System::m_pDriver;
Engine *UI_System::m_pEngine;
Vector2 UI_System::m_uvTop(0,0);
Vector2 UI_System::m_uvBot(1,1);
UI_RenderFrame *m_pUI_Atlas=nullptr;
UI_RenderFrame *UI_System::m_pRenderFramePool=nullptr;
LFD_Anim *UI_System::m_pLFD_Anim_List[MAX_LFD_ANIM];
int Editor_UI_Atlas;
uint32_t UI_System::m_auImage_TexType[]=
{
TEXTURETYPE_ART,
TEXTURETYPE_PCX,
TEXTURETYPE_IMG, //Daggerfall format.
};
uint32_t UI_System::m_auImage_ArchiveType[]=
{
ARCHIVETYPE_ART,
ARCHIVETYPE_LAB,
ARCHIVETYPE_NONE, //Daggerfall image files are loose files.
};
/**************************
***** Implementation *****
**************************/
bool UI_System::Init(IDriver3D *pDriver, Engine *pEngine)
{
m_bScriptsLoaded = false;
m_bScriptExeSucceeded = false;
m_Top = nullptr;
m_Leaf = nullptr;
m_Context = nullptr;
m_PendingScreenChange = nullptr;
m_PushScreen = nullptr;
m_PopScreen = nullptr;
//m_fVirt_Scr_W = 320.0f;
//m_fVirt_Scr_H = 200.0f;
m_bFlipX = false;
m_bFlipY = false;
m_pDriver = pDriver;
m_pEngine = pEngine;
AllocRenderFramePool();
memset(m_pLFD_Anim_List, 0, sizeof(LFD_Anim *)*MAX_LFD_ANIM);
Input::AddKeyDownCallback( KeyDownCallback );
return true;
}
void UI_System::Destroy()
{
if ( m_pUI_Atlas )
{
UI_FreeImage( Editor_UI_Atlas );
}
for (UI_Screen *screen : m_ScreenList)
{
xlDelete screen;
}
m_ScreenList.clear();
m_ScreenMap.clear();
for (UI_Window *window : m_WindowList)
{
xlDelete window;
}
m_WindowList.clear();
FreeRenderFramePool();
}
void UI_System::StartScript(const char *pszFile)
{
XL_Console::PrintF("Start UI Script: %s", pszFile);
if ( m_bScriptsLoaded == false )
{
//UI functions
ScriptSystem::RegisterFunc("void UI_StartScreen(string &in)", asFUNCTION(UI_StartScreen));
ScriptSystem::RegisterFunc("void UI_CreateWindow(string &in, string &in, int, int, int, int, int, int)", asFUNCTION(UI_CreateWindow));
ScriptSystem::RegisterFunc("void UI_CreateWindow_FromLFDFrame(string &in, int, int, int, int)", asFUNCTION(UI_CreateWindow_FromLFDFrame));
ScriptSystem::RegisterFunc("void UI_PushWindow(string &in, string &in, int, int, int, int, int, int)", asFUNCTION(UI_PushWindow));
ScriptSystem::RegisterFunc("void UI_PopWindow()", asFUNCTION(UI_PopWindow));
ScriptSystem::RegisterFunc("void UI_EnableWindow(string& in, int)", asFUNCTION(UI_EnableWindow));
ScriptSystem::RegisterFunc("int UI_AddImage(string &in, int, int)", asFUNCTION(UI_AddImage));
ScriptSystem::RegisterFunc("int UI_AddGameImage(int, string &in, string &in)", asFUNCTION(UI_AddGameImage));
ScriptSystem::RegisterFunc("void UI_FreeImage(int)", asFUNCTION(UI_FreeImage));
ScriptSystem::RegisterFunc("void UI_EnableImageFilter(int)", asFUNCTION(UI_EnableImageFilter));
ScriptSystem::RegisterFunc("void UI_EnableImageBlending(int)", asFUNCTION(UI_EnableImageBlending));
ScriptSystem::RegisterFunc("void UI_SetImageRenderProp(int, int)", asFUNCTION(UI_SetImageRenderProp));
ScriptSystem::RegisterFunc("void UI_RenderImage(int, int, int, float, int, int)", asFUNCTION(UI_RenderImage));
ScriptSystem::RegisterFunc("void UI_GetImageSize(int, int &out, int &out)", asFUNCTION(UI_GetImageSize));
ScriptSystem::RegisterFunc("void UI_RenderRect(int, int, int, int, float, float, float, float, int, int)", asFUNCTION(UI_RenderRect));
ScriptSystem::RegisterFunc("void UI_RenderImageRect(int, int, int, int, int, float, int, int)", asFUNCTION(UI_RenderImageRect));
ScriptSystem::RegisterFunc("void UI_SetImageUV_Range(float, float, float, float)", asFUNCTION(UI_SetImageUV_Range));
ScriptSystem::RegisterFunc("void UI_SetImageUV_RangeI(int, int, int, int, int)", asFUNCTION(UI_SetImageUV_RangeI));
ScriptSystem::RegisterFunc("void UI_RenderString(string &in, int, int, int, float, float, float, float)", asFUNCTION(UI_RenderString));
ScriptSystem::RegisterFunc("void UI_RenderPolygon(int, int[] &in, int[] &in, float, float, float, float, int, int)", asFUNCTION(UI_RenderPolygon));
ScriptSystem::RegisterFunc("void UI_PushScreen(string &in, int, int)", asFUNCTION(UI_PushScreen));
ScriptSystem::RegisterFunc("void UI_PopScreen()", asFUNCTION(UI_PopScreen));
ScriptSystem::RegisterFunc("int UI_IsKeyDown(int)", asFUNCTION(UI_GetVirtualKey));
ScriptSystem::RegisterFunc("void UI_SetVirtualScreenSize(int, int)", asFUNCTION(UI_SetVirtualScreenSize));
ScriptSystem::RegisterFunc("void UI_GetVirtualScreenSize(int &out, int &out)", asFUNCTION(UI_GetVirtualScreenSize));
ScriptSystem::RegisterFunc("void UI_GetScreenSize(int &out, int &out)", asFUNCTION(UI_GetScreenSize));
ScriptSystem::RegisterFunc("void UI_SetPalette(int, int)", asFUNCTION(UI_SetPalette));
//LFD Anim
ScriptSystem::RegisterFunc("int UI_CreateLFD_Anim(string &in, string &in, string &in)", asFUNCTION(UI_CreateLFD_Anim));
ScriptSystem::RegisterFunc("void UI_DestroyLFD_Anim(int)", asFUNCTION(UI_DestroyLFD_Anim));
ScriptSystem::RegisterFunc("void UI_RenderLFD_Anim(int, int, int, int)", asFUNCTION(UI_RenderLFD_Anim));
//Mouse
ScriptSystem::RegisterFunc("void UI_GetMousePos(int &out, int &out)", asFUNCTION(UI_GetMousePos));
//Debug
ScriptSystem::RegisterFunc("void UI_PrintMousePos(int, int, int)", asFUNCTION(UI_PrintMousePos));
//
ScriptSystem::RegisterFunc("void UI_GetStartMap(string &out)", asFUNCTION(EngineSettings::GetStartMap_StrOutCB));
//Misc.
ScriptSystem::RegisterFunc("float UI_GetBrightness()", asFUNCTION(UI_GetCurrentBrightness));
ScriptSystem::RegisterFunc("float UI_GetSpeed()", asFUNCTION(UI_GetSpeed));
//UI scripts.
ScriptSystem::LoadScript( ScriptSystem::SCR_MODULE_UI, ScriptSystem::SCR_SECTION_CORE, pszFile );
m_bScriptsLoaded = true;
}
else
{
//UI scripts.
ScriptSystem::ReloadScript( ScriptSystem::SCR_MODULE_UI, ScriptSystem::SCR_SECTION_CORE, pszFile );
//Now we have to "get back to where we were" as best we can.
//we can go through all the active screens and reset the function handles.
//Then we'll have to call OnEnter as appropriate to setup resources again.
return;
}
SHANDLE uiMain = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, "UI_Main" );
if ( uiMain )
{
ScriptSystem::ExecuteFunc( uiMain );
m_bScriptExeSucceeded = true;
}
else
{
m_bScriptExeSucceeded = false;
}
m_pUI_Atlas = AllocRenderFrame();
//Load Frame texture.
m_pUI_Atlas->hFrame = TextureCache::LoadTexture( (std::string)"XLEngineUI.png", false );
//Then get the texture size and relative scale from the Texture Cache.
int32_t nOffsX, nOffsY;
uint32_t uTexWidth, uTexHeight;
float fRelSizeX, fRelSizeY;
TextureCache::GetTextureSize( nOffsX, nOffsY, uTexWidth, uTexHeight, fRelSizeX, fRelSizeY );
m_pUI_Atlas->width = (int16_t)uTexWidth;
m_pUI_Atlas->height = (int16_t)uTexHeight;
m_pUI_Atlas->fRelWidth = fRelSizeX;
m_pUI_Atlas->fRelHeight = fRelSizeY;
//Finally setup the image handle used by the script.
Editor_UI_Atlas = (int)m_pUI_Atlas->ID;
}
void UI_System::Update()
{
if ( m_PendingScreenChange )
{
//Remove the current screen.
if ( m_Top )
{
m_Context = m_Top;
ScriptSystem::ExecuteFunc( m_Top->m_hOnExit );
}
//Enter the next screen.
m_Top = m_PendingScreenChange;
m_PendingScreenChange = nullptr;
assert( m_Top->m_hOnEnter );
m_Context = m_Top;
ScriptSystem::ExecuteFunc( m_Top->m_hOnEnter );
m_Leaf = m_Top;
}
else if ( m_PushScreen )
{
UI_Screen *pScreen = m_Top;
m_Leaf->m_child = m_PushScreen;
m_PushScreen->m_parent = m_Leaf;
m_PushScreen->m_child = nullptr;
assert( m_PushScreen->m_hOnEnter >= 0 );
m_Context = m_PushScreen;
ScriptSystem::ExecuteFunc( m_PushScreen->m_hOnEnter );
m_Leaf = m_PushScreen;
m_PushScreen = nullptr;
}
else if ( m_PopScreen && m_PopScreen->m_parent )
{
m_Context = m_PopScreen;
ScriptSystem::ExecuteFunc( m_PopScreen->m_hOnExit );
m_Leaf = m_PopScreen->m_parent;
m_Leaf->m_child = nullptr;
m_PopScreen = nullptr;
}
m_Context = m_Leaf;
//only the leaf gets key strokes and updates.
if ( m_Context )
{
ScriptSystem::ExecuteFunc( m_Context->m_hOnUpdate );
int32_t nMouseX = (uint32_t)Input::GetMouseX();
int32_t nMouseY = (uint32_t)Input::GetMouseY();
int32_t nScrWidth, nScrHeight;
m_pDriver->GetWindowSize( nScrWidth, nScrHeight );
float fScrToUI_X = m_Context->m_fVirt_Scr_W/(float)nScrWidth;
float fScrToUI_Y = m_Context->m_fVirt_Scr_H/(float)nScrHeight;
nMouseX = (int32_t)( (float)nMouseX * fScrToUI_X );
nMouseY = (int32_t)( (float)nMouseY * fScrToUI_Y );
//now update the child windows.
UI_Window *pWin = m_Context->m_childWindow;
UpdateChildWindows(pWin, 0, 0, nMouseX, nMouseY);
}
}
void UI_System::UpdateChildWindows(UI_Window *pWin, int x, int y, int nMouseX, int nMouseY)
{
while (pWin)
{
if ( pWin->m_bEnabled )
{
bool bMouseOver = false;
if ( nMouseX >= pWin->m_x+x && nMouseX < pWin->m_x+x+pWin->m_w &&
nMouseY >= pWin->m_y+y && nMouseY < pWin->m_y+y+pWin->m_h )
{
bMouseOver = true;
}
pWin->Update(bMouseOver, nMouseX, nMouseY, x, y);
if ( pWin->m_child )
{
UpdateChildWindows(pWin->m_child, x+pWin->m_x, y+pWin->m_y, nMouseX, nMouseY);
}
}
pWin = pWin->m_sibling;
};
}
void UI_System::Render()
{
if ( !m_Leaf )
return;
m_pDriver->SetBlendMode();
m_pDriver->EnableAlphaTest(false);
m_pDriver->EnableCulling(false);
m_pDriver->EnableDepthRead(false);
m_pDriver->EnableDepthWrite(false);
ScriptArgument arg;
arg.uType = SC_ARG_uint32_t;
arg.arguint32_t = 0;
m_Context = m_Leaf;
if ( (m_Context->m_uFlags&UIFLAG_OVERLAY) && m_Context->m_parent )
{
//first render the parent...
m_Context = m_Context->m_parent;
ScriptSystem::ExecuteFunc( m_Context->m_hOnRender, 1, &arg );
//Render child windows.
UI_Window *pWin = m_Context->m_childWindow;
RenderChildWindows(pWin, 0, 0);
}
//now render the current screen.
m_Context = m_Leaf;
ScriptSystem::ExecuteFunc( m_Context->m_hOnRender, 1, &arg );
//Render child windows.
UI_Window *pWin = m_Context->m_childWindow;
RenderChildWindows(pWin, 0, 0);
//Post Render.
if ( m_Context->m_hOnPostRender )
{
ScriptSystem::ExecuteFunc( m_Context->m_hOnPostRender, 1, &arg );
}
m_pDriver->SetBlendMode();
m_pDriver->EnableAlphaTest(false);
m_pDriver->EnableCulling(true);
m_pDriver->EnableDepthRead(true);
m_pDriver->EnableDepthWrite(true);
}
void UI_System::RenderChildWindows(UI_Window *pWin, int x, int y)
{
while (pWin)
{
if ( pWin->m_bEnabled )
{
pWin->Draw(x, y);
if ( pWin->m_child )
{
RenderChildWindows(pWin->m_child, x+pWin->m_x, y+pWin->m_y);
}
}
pWin = pWin->m_sibling;
};
}
UI_Screen *UI_System::AddScreen(const std::string& sName)
{
UI_Screen *pScreen = nullptr;
std::map<std::string, UI_Screen *>::iterator iScreen = m_ScreenMap.find(sName);
if ( iScreen != m_ScreenMap.end() )
{
pScreen = iScreen->second;
}
else
{
//time to create it.
pScreen = xlNew UI_Screen;
pScreen->m_sName = sName;
m_ScreenMap[sName] = pScreen;
m_ScreenList.push_back( pScreen );
}
return pScreen;
}
UI_Window *UI_System::AddWindow(const std::string& sName, const std::string& sText, uint32_t uType, int32_t x, int32_t y, int32_t w, int32_t h, uint32_t flags, UI_Window *parent)
{
//m_Context is the parent screen.
UI_Window *pWindow = xlNew UI_Window();
pWindow->m_x = x;
pWindow->m_y = y;
pWindow->m_w = w;
pWindow->m_h = h;
pWindow->m_parentScreen = m_Context;
pWindow->m_uFlags = flags;
pWindow->m_uType = uType;
pWindow->m_text = sText;
pWindow->m_name = sName;
pWindow->m_bEnabled = true;
char szFuncName[128];
sprintf(szFuncName, "%s_OnRender", sName.c_str());
pWindow->m_hOnRender = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
sprintf(szFuncName, "%s_OnUpdate", sName.c_str());
pWindow->m_hOnUpdate = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
sprintf(szFuncName, "%s_OnKey", sName.c_str());
pWindow->m_hOnKey = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
sprintf(szFuncName, "%s_OnRelease", sName.c_str());
pWindow->m_hOnRelease = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
if ( parent == nullptr )
{
pWindow->m_parent = nullptr;
if ( m_Context->m_childWindow == nullptr )
{
m_Context->m_childWindow = pWindow;
}
else
{
UI_Window *curWin = m_Context->m_childWindow;
while (1)
{
if ( curWin->m_sibling == nullptr )
{
curWin->m_sibling = pWindow;
break;
}
else
{
curWin = curWin->m_sibling;
}
};
}
}
else
{
pWindow->m_parent = parent;
if ( parent->m_child == nullptr )
{
parent->m_child = pWindow;
}
else
{
UI_Window *curWin = parent->m_child;
while (1)
{
if ( curWin->m_sibling == nullptr )
{
curWin->m_sibling = pWindow;
break;
}
else
{
curWin = curWin->m_sibling;
}
};
}
}
m_WindowList.push_back( pWindow );
return pWindow;
}
/********************************************************
***** UI Script Functions (called from UI scripts) *****
********************************************************/
UI_Window *_pCurWindow = nullptr;
UI_Window *_apWindowStack[16];
int _stackPtr = 0;
void UI_System::UI_CreateWindow(std::string& sName, std::string& sText, int type, int x, int y, int w, int h, int flags)
{
if ( m_Context )
{
UI_Window *pWin = AddWindow(sName, sText, (uint32_t)type, x, y, w, h, (uint32_t)flags, _pCurWindow);
assert(pWin);
}
}
void UI_System::UI_PushWindow(std::string& sName, std::string& sText, int type, int x, int y, int w, int h, int flags)
{
if ( m_Context && _stackPtr < 16 )
{
UI_Window *pWin = AddWindow(sName, sText, (uint32_t)type, x, y, w, h, (uint32_t)flags, _pCurWindow);
_apWindowStack[_stackPtr] = _pCurWindow;
_stackPtr++;
_pCurWindow = pWin;
assert(pWin);
}
}
void UI_System::UI_PopWindow()
{
if ( m_Context )
{
if ( _stackPtr > 0 )
{
_stackPtr--;
_pCurWindow = _apWindowStack[_stackPtr];
}
else
{
_stackPtr = 0;
_pCurWindow = nullptr;
}
}
}
void UI_System::UI_EnableWindow(std::string& sName, int enable)
{
for (UI_Window *pWindow : m_WindowList)
{
if ( pWindow->m_name == sName )
{
pWindow->m_bEnabled = enable ? true : false;
break;
}
}
}
void UI_System::UI_CreateWindow_FromLFDFrame(std::string& sName, int LFDAnim_ID, int frame, int x0, int y0)
{
if ( LFDAnim_ID >= 0 && LFDAnim_ID < MAX_LFD_ANIM && m_pLFD_Anim_List[LFDAnim_ID] )
{
int32_t frameX0, frameY0, frameWidth, frameHeight;
m_pLFD_Anim_List[LFDAnim_ID]->GetFrameExtents(frame, (float)x0/320.0f, (float)y0/200.0f, frameX0, frameY0, frameWidth, frameHeight);
UI_CreateWindow(sName, sName, 0, frameX0, frameY0, frameWidth, frameHeight, 0);
}
}
void UI_System::UI_StartScreen(std::string& sUI_Start)
{
char szFuncName[128];
sprintf(szFuncName, "%s_OnEnter", sUI_Start.c_str());
SHANDLE hOnEnter = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
if ( hOnEnter )
{
UI_Screen *pScreen = AddScreen( sUI_Start );
pScreen->m_sName = sUI_Start;
pScreen->m_hOnEnter = hOnEnter;
pScreen->m_uFlags = UIFLAG_NONE;
pScreen->m_parent = nullptr;
pScreen->m_child = nullptr;
sprintf(szFuncName, "%s_OnExit", sUI_Start.c_str());
pScreen->m_hOnExit = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
sprintf(szFuncName, "%s_OnRender", sUI_Start.c_str());
pScreen->m_hOnRender = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
sprintf(szFuncName, "%s_OnPostRender", sUI_Start.c_str());
pScreen->m_hOnPostRender = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
sprintf(szFuncName, "%s_OnUpdate", sUI_Start.c_str());
pScreen->m_hOnUpdate = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
sprintf(szFuncName, "%s_OnKey", sUI_Start.c_str());
pScreen->m_hOnKey = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
m_PendingScreenChange = pScreen;
}
}
void UI_System::UI_PushScreen(const std::string& uiName, int flags, int backgrndFX)
{
char szFuncName[128];
sprintf(szFuncName, "%s_OnEnter", uiName.c_str());
SHANDLE hOnEnter = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
if ( hOnEnter )
{
UI_Screen *pScreen = AddScreen(uiName);
pScreen->m_sName = uiName;
pScreen->m_hOnEnter = hOnEnter;
pScreen->m_uFlags = (uint32_t)flags;
pScreen->m_parent = nullptr;
pScreen->m_child = nullptr;
sprintf(szFuncName, "%s_OnExit", uiName.c_str());
pScreen->m_hOnExit = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
sprintf(szFuncName, "%s_OnRender", uiName.c_str());
pScreen->m_hOnRender = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
sprintf(szFuncName, "%s_OnPostRender", uiName.c_str());
pScreen->m_hOnPostRender = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
sprintf(szFuncName, "%s_OnUpdate", uiName.c_str());
pScreen->m_hOnUpdate = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
sprintf(szFuncName, "%s_OnKey", uiName.c_str());
pScreen->m_hOnKey = ScriptSystem::GetFunc( ScriptSystem::SCR_MODULE_UI, szFuncName );
m_PushScreen = pScreen;
}
}
//pops the current screen off the stack, returning control to the previous. This works for both Overlays and screens using PushScreen
void UI_System::UI_PopScreen()
{
m_PopScreen = m_Leaf;
}
/***************************************
****** RenderFrame Managerment ********
***************************************/
//Create a RenderFrame pool.
void UI_System::AllocRenderFramePool()
{
m_pRenderFramePool = xlNew UI_RenderFrame[MAX_RENDERFRAMEPOOL_SIZE];
memset(m_pRenderFramePool, 0, sizeof(UI_RenderFrame)*MAX_RENDERFRAMEPOOL_SIZE);
for (uint32_t i=0; i<MAX_RENDERFRAMEPOOL_SIZE; i++)
{
m_pRenderFramePool[i].ID = i;
}
}
void UI_System::FreeRenderFramePool()
{
if ( m_pRenderFramePool )
{
xlDelete [] m_pRenderFramePool;
}
m_pRenderFramePool = nullptr;
}
UI_RenderFrame *UI_System::AllocRenderFrame()
{
if ( m_pRenderFramePool == nullptr )
return nullptr;
for (uint32_t i=0; i<MAX_RENDERFRAMEPOOL_SIZE; i++)
{
if ( m_pRenderFramePool[i].bInUse == false )
{
m_pRenderFramePool[i].bInUse = true;
return &m_pRenderFramePool[i];
}
}
return nullptr;
}
void UI_System::FreeRenderFrame(uint32_t uFrameID)
{
m_pRenderFramePool[ uFrameID ].bInUse = false;
}
UI_RenderFrame *UI_System::GetRenderFrame(uint32_t uFrameID)
{
return &m_pRenderFramePool[ uFrameID ];
}
/**************************************
****** General UI Functions **********
*** A valid m_Context is required ****
**************************************/
void LoadLFD_Pal(LFD_Anim *pAnim, Archive *pArchive, std::string& sFile)
{
if ( ArchiveManager::GameFile_Open(pArchive, sFile.c_str()) )
{
uint32_t len = ArchiveManager::GameFile_GetLength();
char *data = xlNew char[len+1];
if ( data )
{
ArchiveManager::GameFile_Read(data, len);
ArchiveManager::GameFile_Close();
pAnim->SetPLTT( (PLTT_File *)data );
xlDelete [] data;
}
}
}
void LoadLFD_File(LFD_Anim *pAnim, Archive *pArchive, std::string& sFile)
{
if ( ArchiveManager::GameFile_Open(pArchive, sFile.c_str()) )
{
uint32_t len = ArchiveManager::GameFile_GetLength();
char *data = xlNew char[len+1];
if ( data )
{
ArchiveManager::GameFile_Read(data, len);
ArchiveManager::GameFile_Close();
pAnim->Load(data, len);
xlDelete [] data;
}
}
}
int UI_System::UI_CreateLFD_Anim(std::string& sArchive, std::string& sAnim, std::string& sPal)
{
//find a free slot.
int32_t ID = -1;
for (int32_t i=0; i<MAX_LFD_ANIM; i++)
{
if ( m_pLFD_Anim_List[i] == nullptr )
{
ID = i;
break;
}
}
if ( ID > -1 )
{
m_pLFD_Anim_List[ID] = xlNew LFD_Anim( m_pDriver );
Archive *pArchive = ArchiveManager::OpenArchive(ARCHIVETYPE_LFD, sArchive.c_str());
if ( pArchive )
{
LoadLFD_Pal( m_pLFD_Anim_List[ID], pArchive, sPal );
LoadLFD_File( m_pLFD_Anim_List[ID], pArchive, sAnim );
ArchiveManager::CloseArchive( pArchive );
}
}
return ID;
}
void UI_System::UI_DestroyLFD_Anim(int ID)
{
if ( ID >= 0 && ID < MAX_LFD_ANIM && m_pLFD_Anim_List[ID] )
{
xlDelete m_pLFD_Anim_List[ID];
}
m_pLFD_Anim_List[ID] = nullptr;
}
void UI_System::UI_RenderLFD_Anim(int ID, int frame, int x, int y)
{
if ( ID >= 0 && ID < MAX_LFD_ANIM && m_pLFD_Anim_List[ID] )
{
m_pDriver->SetBlendMode();
m_pDriver->EnableAlphaTest(true);
m_pLFD_Anim_List[ID]->Render(frame, (float)x/320.0f, (float)y/200.0f);
}
}
int UI_System::UI_AddImage(std::string& sImage, int cutoutMinIdx, int cutoutMaxIdx)
{
int hImageHandle = -1;
if ( m_Context )
{
UI_RenderFrame *frame = AllocRenderFrame();
//Load Frame texture.
frame->hFrame = TextureCache::LoadTexture( sImage, false );
//Then get the texture size and relative scale from the Texture Cache.
int32_t nOffsX, nOffsY;
uint32_t uTexWidth, uTexHeight;
float fRelSizeX, fRelSizeY;
TextureCache::GetTextureSize( nOffsX, nOffsY, uTexWidth, uTexHeight, fRelSizeX, fRelSizeY );
frame->width = (int16_t)uTexWidth;
frame->height = (int16_t)uTexHeight;
frame->fRelWidth = fRelSizeX;
frame->fRelHeight = fRelSizeY;
//Finally setup the image handle used by the script.
hImageHandle = (int)frame->ID;
}
return hImageHandle;
}
int UI_System::UI_AddGameImage(uint32_t uImageType, std::string &sArchive, std::string &sImage)
{
int hImageHandle = -1;
if ( m_Context )
{
UI_RenderFrame *frame = AllocRenderFrame();
//Load Frame texture.
frame->hFrame = TextureCache::GameFile_LoadTexture( m_auImage_TexType[uImageType], 0, m_auImage_ArchiveType[uImageType], sArchive, sImage, false );
//Then get the texture size and relative scale from the Texture Cache.
int32_t nOffsX, nOffsY;
uint32_t uTexWidth, uTexHeight;
float fRelSizeX, fRelSizeY;
TextureCache::GetTextureSize( nOffsX, nOffsY, uTexWidth, uTexHeight, fRelSizeX, fRelSizeY );
frame->width = (int16_t)uTexWidth;
frame->height = (int16_t)uTexHeight;
frame->fRelWidth = fRelSizeX;
frame->fRelHeight = fRelSizeY;
//Finally setup the image handle used by the script.
hImageHandle = (int)frame->ID;
}
return hImageHandle;
}
void UI_System::UI_FreeImage(int hImageHandle)
{
if ( hImageHandle > -1 )
{
FreeRenderFrame( (uint32_t)hImageHandle );
}
}
void UI_System::UI_SetVirtualScreenSize(int w, int h)
{
m_Context->m_fVirt_Scr_W = (float)w;
m_Context->m_fVirt_Scr_H = (float)h;
}
void UI_System::UI_GetVirtualScreenSize(int& w, int& h)
{
w = (int)m_Context->m_fVirt_Scr_W;
h = (int)m_Context->m_fVirt_Scr_H;
}
void UI_System::UI_GetScreenSize(int& w, int& h)
{
m_pDriver->GetWindowSize(w, h);
}
void UI_System::UI_SetPalette(int pal, int colMap)
{
m_pDriver->SetCurrentPalette(pal);
m_pDriver->SetCurrentColormap(colMap);
}
//Solid color quad.
void UI_System::UI_RenderRect(int x, int y, int w, int h, float r, float g, float b, float a, int alignHoriz, int alignVert)
{
if ( m_Context )
{
int32_t nScrWidth, nScrHeight;
m_pDriver->SetTexture(0, XL_INVALID_TEXTURE);
m_pDriver->SetBlendMode( IDriver3D::BLEND_ALPHA );
m_pDriver->GetWindowSize( nScrWidth, nScrHeight );
float virt_scr_w = m_Context->m_fVirt_Scr_W;
float virt_scr_h = m_Context->m_fVirt_Scr_H;
float fUItoScrX = (float)nScrWidth /virt_scr_w;
float fUItoScrY = (float)nScrHeight/virt_scr_h;
//render rect...
float fdX = (float)w * fUItoScrX;
float fdY = (float)h * fUItoScrY;
float fX0, fY0;
//horizontal alignment.
if ( alignHoriz == UI_Align_Left )
fX0 = (float)x * fUItoScrX;
else if ( alignHoriz == UI_Align_Right )
fX0 = ( virt_scr_w + (float)(x - w) ) * fUItoScrX;
else //center
fX0 = (float)( virt_scr_w*0.5f + (float)(x - (w>>1)) ) * fUItoScrX;
//vertical alignment
if ( alignVert == UI_Align_Bottom )
fY0 = (float)y * fUItoScrY;
else if (alignVert == UI_Align_Top )
fY0 = (virt_scr_h + (float)(y - h)) * fUItoScrY;
else //center
fY0 = ( virt_scr_h + (float)(y - (h>>1)) ) * fUItoScrY;
Vector4 color(r, g, b, a);
Vector4 posScale(fX0, fY0, fdX, fdY);
Vector2 uv(0,0);
m_pDriver->SetColor( &color );
m_pDriver->RenderScreenQuad(posScale, uv, uv, color, color);
}
}
//Solid color polygon.
void UI_System::UI_RenderPolygon(int npt, asIScriptArray *x, asIScriptArray *y, float r, float g, float b, float a, int alignHoriz, int alignVert)
{
if ( m_Context )
{
#if 0
Vector3 polygon[16];
float u[16], v[16];
Driver3D_DX9::SetShaders(Driver3D_DX9::VS_SHADER_SCREEN, Driver3D_DX9::PS_SHADER_SCREEN_NOTEX);
Driver3D_DX9::EnableAlphaBlend(TRUE);
float aspectScale = Driver3D_DX9::GetAspectScale();
if ( Driver3D_DX9::Iint32_t0x200Enabled() )
{
aspectScale = 1.0f;
}
float virt_scr_w = m_Context->m_fVirt_Scr_W / aspectScale;
float virt_scr_h = m_Context->m_fVirt_Scr_H;
float fUItoScrX = 1.0f/virt_scr_w;
float fUItoScrY = 1.0f/virt_scr_h;
int min_x=100000, min_y=100000;
int max_x = -min_x, max_y = -min_y;
for (int i=0; i<npt; i++)
{
float fX, fY;
int vx = *((int *)x->GetElementPointer(i));
int vy = *((int *)y->GetElementPointer(i));
if ( vx < min_x ) min_x = vx;
if ( vy < min_y ) min_y = vy;
if ( vx > max_x ) max_x = vx;
if ( vy > max_y ) max_y = vy;
}
int w = max_x - min_x;
int h = max_y - min_y;
for (int i=0; i<npt; i++)
{
float fX, fY;
int vx = *((int *)x->GetElementPointer(i));
int vy = *((int *)y->GetElementPointer(i));
//horizontal alignment.
if ( alignHoriz == UI_Align_Left )
fX = (float)vx * fUItoScrX;
else if ( alignHoriz == UI_Align_Right )
fX = ( virt_scr_w + (float)(vx - w) ) * fUItoScrX;
else //center
fX = (float)( virt_scr_w*0.5f + (float)(vx - (w>>1)) ) * fUItoScrX;
//vertical alignment
if ( alignVert == UI_Align_Bottom )
fY = (float)vy * fUItoScrY;
else if (alignVert == UI_Align_Top )
fY = (virt_scr_h + (float)(vy - h)) * fUItoScrY;
else //center
fY = ( virt_scr_h + (float)(vy - (h>>1)) ) * fUItoScrY;
polygon[i].Set(fX, fY, 0.9f);
}
Driver3D_DX9::SetAmbientColor(r, g, b, a);
Driver3D_DX9::RenderPolygon(npt, polygon, u, v);
#endif
}
}
void UI_System::UI_GetImageSize(int hImage, int& w, int& h)
{
UI_RenderFrame *frame = GetRenderFrame(hImage);
w = (int)frame->width;
h = (int)frame->height;
}
static bool m_bEnableFiltering = true;
static bool m_bEnableBlending = true;
void UI_System::UI_EnableImageFilter(int enable)
{
m_bEnableFiltering = (enable) ? true : false;
}
void UI_System::UI_EnableImageBlending(int enable)
{
m_bEnableBlending = (enable) ? true : false;
}
void UI_System::UI_SetImageRenderProp(int flipX, int flipY)
{
m_bFlipX = flipX ? true : false;
m_bFlipY = flipY ? true : false;
}
//Image with alpha blending.
void UI_System::UI_RenderImage(int hImage, int x, int y, float intensity, int alignHoriz, int alignVert)
{
if ( m_Context && hImage >= 0 )
{
UI_RenderFrame *frame = GetRenderFrame(hImage);
int32_t nScrWidth, nScrHeight;
m_pDriver->SetTexture(0, frame->hFrame, m_bEnableFiltering ? IDriver3D::FILTER_NORMAL_NO_MIP : IDriver3D::FILTER_POINT);
m_pDriver->SetBlendMode( m_bEnableBlending ? IDriver3D::BLEND_ALPHA : IDriver3D::BLEND_NONE );
m_pDriver->GetWindowSize( nScrWidth, nScrHeight );
float virt_scr_w = m_Context->m_fVirt_Scr_W;
float virt_scr_h = m_Context->m_fVirt_Scr_H;
float fUItoScrX = (float)nScrWidth /virt_scr_w;
float fUItoScrY = (float)nScrHeight/virt_scr_h;
int32_t w = frame->width;
int32_t h = frame->height;
//render rect...
float fdX = (float)w * fUItoScrX;
float fdY = (float)h * fUItoScrY;
float fX0, fY0;
//horizontal alignment.
if ( alignHoriz == UI_Align_Left )
fX0 = (float)x * fUItoScrX;
else if ( alignHoriz == UI_Align_Right )
fX0 = ( virt_scr_w + (float)(x - w) ) * fUItoScrX;
else //center
fX0 = (float)( virt_scr_w*0.5f + (float)(x - (w>>1)) ) * fUItoScrX;
//vertical alignment
if ( alignVert == UI_Align_Bottom )
fY0 = (float)y * fUItoScrY;
else if (alignVert == UI_Align_Top )
fY0 = (virt_scr_h + (float)(y - h)) * fUItoScrY;
else //center
fY0 = ( virt_scr_h + (float)(y - (h>>1)) ) * fUItoScrY;
Vector4 color(intensity, intensity, intensity, 1.0f);
Vector4 posScale(fX0, fY0, fdX, fdY);
Vector2 uvTop(0,0), uvBot(1,1);