-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathDXUTgui.cpp
More file actions
8263 lines (6738 loc) · 283 KB
/
Copy pathDXUTgui.cpp
File metadata and controls
8263 lines (6738 loc) · 283 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
//--------------------------------------------------------------------------------------
// File: DXUTgui.cpp
//
// Desc:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "dxstdafx.h"
#include "DXUTgui.h"
#include "DXUTsettingsDlg.h"
#undef min // use __min instead
#undef max // use __max instead
#ifndef WM_XBUTTONDOWN
#define WM_XBUTTONDOWN 0x020B // (not always defined)
#endif
#ifndef WM_XBUTTONUP
#define WM_XBUTTONUP 0x020C // (not always defined)
#endif
#ifndef WM_MOUSEWHEEL
#define WM_MOUSEWHEEL 0x020A // (not always defined)
#endif
#ifndef WHEEL_DELTA
#define WHEEL_DELTA 120 // (not always defined)
#endif
// Minimum scroll bar thumb size
#define SCROLLBAR_MINTHUMBSIZE 8
// Delay and repeat period when clicking on the scroll bar arrows
#define SCROLLBAR_ARROWCLICK_DELAY 0.33
#define SCROLLBAR_ARROWCLICK_REPEAT 0.05
#define UNISCRIBE_DLLNAME L"\\usp10.dll"
#define GETPROCADDRESS( Module, APIName, Temp ) \
Temp = GetProcAddress( Module, #APIName ); \
if( Temp ) \
*(FARPROC*)&_##APIName = Temp
#define PLACEHOLDERPROC( APIName ) \
_##APIName = Dummy_##APIName
#define IMM32_DLLNAME L"\\imm32.dll"
#define VER_DLLNAME L"\\version.dll"
// DXUT_MAX_EDITBOXLENGTH is the maximum string length allowed in edit boxes,
// including the NULL terminator.
//
// Uniscribe does not support strings having bigger-than-16-bits length.
// This means that the string must be less than 65536 characters long,
// including the NULL terminator.
#define DXUT_MAX_EDITBOXLENGTH 0xFFFF
double CDXUTDialog::s_fTimeRefresh = 0.0f;
CDXUTControl* CDXUTDialog::s_pControlFocus = NULL; // The control which has focus
CDXUTControl* CDXUTDialog::s_pControlPressed = NULL; // The control currently pressed
struct DXUT_SCREEN_VERTEX
{
float x, y, z, h;
D3DCOLOR color;
float tu, tv;
static DWORD FVF;
};
DWORD DXUT_SCREEN_VERTEX::FVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1;
struct DXUT_SCREEN_VERTEX_UNTEX
{
float x, y, z, h;
D3DCOLOR color;
static DWORD FVF;
};
DWORD DXUT_SCREEN_VERTEX_UNTEX::FVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;
inline int RectWidth( RECT &rc ) { return ( (rc).right - (rc).left ); }
inline int RectHeight( RECT &rc ) { return ( (rc).bottom - (rc).top ); }
//--------------------------------------------------------------------------------------
// CDXUTDialog class
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
CDXUTDialog::CDXUTDialog()
{
m_x = 0;
m_y = 0;
m_width = 0;
m_height = 0;
m_pManager = NULL;
m_bVisible = true;
m_bCaption = false;
m_bMinimized = false;
m_bDrag = false;
m_wszCaption[0] = L'\0';
m_nCaptionHeight = 18;
m_colorTopLeft = 0;
m_colorTopRight = 0;
m_colorBottomLeft = 0;
m_colorBottomRight = 0;
m_pCallbackEvent = NULL;
m_pCallbackEventUserContext = NULL;
m_fTimeLastRefresh = 0;
m_pControlMouseOver = NULL;
m_pNextDialog = this;
m_pPrevDialog = this;
m_nDefaultControlID = 0xffff;
m_bNonUserEvents = false;
m_bKeyboardInput = false;
m_bMouseInput = true;
}
//--------------------------------------------------------------------------------------
CDXUTDialog::~CDXUTDialog()
{
int i=0;
RemoveAllControls();
m_Fonts.RemoveAll();
m_Textures.RemoveAll();
for( i=0; i < m_DefaultElements.GetSize(); i++ )
{
DXUTElementHolder* pElementHolder = m_DefaultElements.GetAt( i );
SAFE_DELETE( pElementHolder );
}
m_DefaultElements.RemoveAll();
}
//--------------------------------------------------------------------------------------
void CDXUTDialog::Init( CDXUTDialogResourceManager* pManager, bool bRegisterDialog )
{
m_pManager = pManager;
if( bRegisterDialog )
pManager->RegisterDialog( this );
SetTexture( 0, MAKEINTRESOURCE(0xFFFF), (HMODULE)0xFFFF );
InitDefaultElements();
}
//--------------------------------------------------------------------------------------
void CDXUTDialog::Init( CDXUTDialogResourceManager* pManager, bool bRegisterDialog, LPCWSTR pszControlTextureFilename )
{
m_pManager = pManager;
if( bRegisterDialog )
pManager->RegisterDialog( this );
SetTexture( 0, pszControlTextureFilename );
InitDefaultElements();
}
//--------------------------------------------------------------------------------------
void CDXUTDialog::Init( CDXUTDialogResourceManager* pManager, bool bRegisterDialog, LPCWSTR szControlTextureResourceName, HMODULE hControlTextureResourceModule )
{
m_pManager = pManager;
if( bRegisterDialog )
pManager->RegisterDialog( this );
SetTexture( 0, szControlTextureResourceName, hControlTextureResourceModule );
InitDefaultElements();
}
//--------------------------------------------------------------------------------------
void CDXUTDialog::SetCallback( PCALLBACKDXUTGUIEVENT pCallback, void* pUserContext )
{
// If this assert triggers, you need to call CDXUTDialog::Init() first. This change
// was made so that the DXUT's GUI could become seperate and optional from DXUT's core. The
// creation and interfacing with CDXUTDialogResourceManager is now the responsibility
// of the application if it wishes to use DXUT's GUI.
assert( m_pManager != NULL && L"To fix call CDXUTDialog::Init() first. See comments for details." );
m_pCallbackEvent = pCallback;
m_pCallbackEventUserContext = pUserContext;
}
//--------------------------------------------------------------------------------------
void CDXUTDialog::RemoveControl( int ID )
{
for( int i=0; i < m_Controls.GetSize(); i++ )
{
CDXUTControl* pControl = m_Controls.GetAt( i );
if( pControl->GetID() == ID )
{
// Clean focus first
ClearFocus();
// Clear references to this control
if( s_pControlFocus == pControl )
s_pControlFocus = NULL;
if( s_pControlPressed == pControl )
s_pControlPressed = NULL;
if( m_pControlMouseOver == pControl )
m_pControlMouseOver = NULL;
SAFE_DELETE( pControl );
m_Controls.Remove( i );
return;
}
}
}
//--------------------------------------------------------------------------------------
void CDXUTDialog::RemoveAllControls()
{
if( s_pControlFocus && s_pControlFocus->m_pDialog == this )
s_pControlFocus = NULL;
if( s_pControlPressed && s_pControlPressed->m_pDialog == this )
s_pControlPressed = NULL;
m_pControlMouseOver = NULL;
for( int i=0; i < m_Controls.GetSize(); i++ )
{
CDXUTControl* pControl = m_Controls.GetAt( i );
SAFE_DELETE( pControl );
}
m_Controls.RemoveAll();
}
//--------------------------------------------------------------------------------------
CDXUTDialogResourceManager::CDXUTDialogResourceManager()
{
m_pd3dDevice = NULL;
m_pStateBlock = NULL;
m_pSprite = NULL;
}
//--------------------------------------------------------------------------------------
CDXUTDialogResourceManager::~CDXUTDialogResourceManager()
{
int i;
for( i=0; i < m_FontCache.GetSize(); i++ )
{
DXUTFontNode* pFontNode = m_FontCache.GetAt( i );
SAFE_DELETE( pFontNode );
}
m_FontCache.RemoveAll();
for( i=0; i < m_TextureCache.GetSize(); i++ )
{
DXUTTextureNode* pTextureNode = m_TextureCache.GetAt( i );
SAFE_DELETE( pTextureNode );
}
m_TextureCache.RemoveAll();
CUniBuffer::Uninitialize();
CDXUTIMEEditBox::Uninitialize();
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDialogResourceManager::OnCreateDevice( LPDIRECT3DDEVICE9 pd3dDevice )
{
HRESULT hr = S_OK;
int i=0;
m_pd3dDevice = pd3dDevice;
for( i=0; i < m_FontCache.GetSize(); i++ )
{
hr = CreateFont( i );
if( FAILED(hr) )
return hr;
}
for( i=0; i < m_TextureCache.GetSize(); i++ )
{
hr = CreateTexture( i );
if( FAILED(hr) )
return hr;
}
hr = D3DXCreateSprite( pd3dDevice, &m_pSprite );
if( FAILED(hr) )
return DXUT_ERR( L"D3DXCreateSprite", hr );
// Call CDXUTIMEEditBox's StaticOnCreateDevice()
// to initialize certain window-dependent data.
CDXUTIMEEditBox::StaticOnCreateDevice();
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDialogResourceManager::OnResetDevice()
{
HRESULT hr = S_OK;
for( int i=0; i < m_FontCache.GetSize(); i++ )
{
DXUTFontNode* pFontNode = m_FontCache.GetAt( i );
if( pFontNode->pFont )
pFontNode->pFont->OnResetDevice();
}
if( m_pSprite )
m_pSprite->OnResetDevice();
IDirect3DDevice9* pd3dDevice = DXUTGetD3DDevice();
V_RETURN( pd3dDevice->CreateStateBlock( D3DSBT_ALL, &m_pStateBlock ) );
return S_OK;
}
//--------------------------------------------------------------------------------------
bool CDXUTDialogResourceManager::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
// Let the CDXUTIMEEditBox's static message proc handle the msg.
// This is because some IME messages must be handled to ensure
// proper functionalities and the static msg proc ensures that
// this happens even if no control has the input focus.
if( CDXUTIMEEditBox::StaticMsgProc( uMsg, wParam, lParam ) )
return true;
return false;
}
//--------------------------------------------------------------------------------------
void CDXUTDialogResourceManager::OnLostDevice()
{
for( int i=0; i < m_FontCache.GetSize(); i++ )
{
DXUTFontNode* pFontNode = m_FontCache.GetAt( i );
if( pFontNode->pFont )
pFontNode->pFont->OnLostDevice();
}
if( m_pSprite )
m_pSprite->OnLostDevice();
SAFE_RELEASE( m_pStateBlock );
}
//--------------------------------------------------------------------------------------
void CDXUTDialogResourceManager::OnDestroyDevice()
{
int i=0;
m_pd3dDevice = NULL;
// Release the resources but don't clear the cache, as these will need to be
// recreated if the device is recreated
for( i=0; i < m_FontCache.GetSize(); i++ )
{
DXUTFontNode* pFontNode = m_FontCache.GetAt( i );
SAFE_RELEASE( pFontNode->pFont );
}
for( i=0; i < m_TextureCache.GetSize(); i++ )
{
DXUTTextureNode* pTextureNode = m_TextureCache.GetAt( i );
SAFE_RELEASE( pTextureNode->pTexture );
}
SAFE_RELEASE( m_pSprite );
}
//--------------------------------------------------------------------------------------
bool CDXUTDialogResourceManager::RegisterDialog( CDXUTDialog *pDialog )
{
// Check that the dialog isn't already registered.
for( int i = 0; i < m_Dialogs.GetSize(); ++i )
if( m_Dialogs.GetAt( i ) == pDialog )
return true;
// Add to the list.
if( FAILED( m_Dialogs.Add( pDialog ) ) )
return false;
// Set up next and prev pointers.
if( m_Dialogs.GetSize() > 1 )
m_Dialogs[m_Dialogs.GetSize() - 2]->SetNextDialog( pDialog );
m_Dialogs[m_Dialogs.GetSize() - 1]->SetNextDialog( m_Dialogs[0] );
return true;
}
//--------------------------------------------------------------------------------------
void CDXUTDialogResourceManager::UnregisterDialog( CDXUTDialog *pDialog )
{
// Search for the dialog in the list.
for( int i = 0; i < m_Dialogs.GetSize(); ++i )
if( m_Dialogs.GetAt( i ) == pDialog )
{
m_Dialogs.Remove( i );
if( m_Dialogs.GetSize() > 0 )
{
int l, r;
if( 0 == i )
l = m_Dialogs.GetSize() - 1;
else
l = i - 1;
if( m_Dialogs.GetSize() == i )
r = 0;
else
r = i;
m_Dialogs[l]->SetNextDialog( m_Dialogs[r] );
}
return;
}
}
//--------------------------------------------------------------------------------------
void CDXUTDialogResourceManager::EnableKeyboardInputForAllDialogs()
{
// Enable keyboard input for all registered dialogs
for( int i = 0; i < m_Dialogs.GetSize(); ++i )
m_Dialogs[i]->EnableKeyboardInput( true );
}
//--------------------------------------------------------------------------------------
void CDXUTDialog::Refresh()
{
if( s_pControlFocus )
s_pControlFocus->OnFocusOut();
if( m_pControlMouseOver )
m_pControlMouseOver->OnMouseLeave();
s_pControlFocus = NULL;
s_pControlPressed = NULL;
m_pControlMouseOver = NULL;
for( int i=0; i < m_Controls.GetSize(); i++ )
{
CDXUTControl* pControl = m_Controls.GetAt(i);
pControl->Refresh();
}
if( m_bKeyboardInput )
FocusDefaultControl();
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDialog::OnRender( float fElapsedTime )
{
// If this assert triggers, you need to call CDXUTDialogResourceManager::On*Device() from inside
// the application's device callbacks. See the SDK samples for an example of how to do this.
assert( m_pManager->GetD3DDevice() && m_pManager->m_pStateBlock && L"To fix hook up CDXUTDialogResourceManager to device callbacks. See comments for details" );
// See if the dialog needs to be refreshed
if( m_fTimeLastRefresh < s_fTimeRefresh )
{
m_fTimeLastRefresh = DXUTGetTime();
Refresh();
}
// For invisible dialog, out now.
if( !m_bVisible ||
( m_bMinimized && !m_bCaption ) )
return S_OK;
IDirect3DDevice9* pd3dDevice = m_pManager->GetD3DDevice();
// Set up a state block here and restore it when finished drawing all the controls
m_pManager->m_pStateBlock->Capture();
pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, FALSE );
pd3dDevice->SetRenderState( D3DRS_SEPARATEALPHABLENDENABLE, FALSE );
pd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD );
pd3dDevice->SetRenderState( D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA|D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_RED );
pd3dDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_GOURAUD );
pd3dDevice->SetRenderState( D3DRS_FOGENABLE, FALSE );
pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2 );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_RESULTARG, D3DTA_CURRENT );
pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
BOOL bBackgroundIsVisible = ( m_colorTopLeft | m_colorTopRight | m_colorBottomRight | m_colorBottomLeft ) & 0xff000000;
if( !m_bMinimized && bBackgroundIsVisible )
{
DXUT_SCREEN_VERTEX_UNTEX vertices[4] =
{
(float)m_x, (float)m_y, 0.5f, 1.0f, m_colorTopLeft,
(float)m_x + m_width, (float)m_y, 0.5f, 1.0f, m_colorTopRight,
(float)m_x + m_width, (float)m_y + m_height, 0.5f, 1.0f, m_colorBottomRight,
(float)m_x, (float)m_y + m_height, 0.5f, 1.0f, m_colorBottomLeft,
};
pd3dDevice->SetVertexShader( NULL );
pd3dDevice->SetPixelShader( NULL );
pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
pd3dDevice->SetFVF( DXUT_SCREEN_VERTEX_UNTEX::FVF );
pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, vertices, sizeof(DXUT_SCREEN_VERTEX_UNTEX) );
}
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
DXUTTextureNode* pTextureNode = GetTexture( 0 );
pd3dDevice->SetTexture( 0, pTextureNode->pTexture );
m_pManager->m_pSprite->Begin( D3DXSPRITE_DONOTSAVESTATE );
// Render the caption if it's enabled.
if( m_bCaption )
{
// DrawSprite will offset the rect down by
// m_nCaptionHeight, so adjust the rect higher
// here to negate the effect.
RECT rc = { 0, -m_nCaptionHeight, m_width, 0 };
DrawSprite( &m_CapElement, &rc );
rc.left += 5; // Make a left margin
WCHAR wszOutput[256];
StringCchCopy( wszOutput, 256, m_wszCaption );
if( m_bMinimized )
StringCchCat( wszOutput, 256, L" (Minimized)" );
DrawText( wszOutput, &m_CapElement, &rc, true );
}
// If the dialog is minimized, skip rendering
// its controls.
if( !m_bMinimized )
{
for( int i=0; i < m_Controls.GetSize(); i++ )
{
CDXUTControl* pControl = m_Controls.GetAt(i);
// Focused control is drawn last
if( pControl == s_pControlFocus )
continue;
pControl->Render( pd3dDevice, fElapsedTime );
}
if( s_pControlFocus != NULL && s_pControlFocus->m_pDialog == this )
s_pControlFocus->Render( pd3dDevice, fElapsedTime );
}
m_pManager->m_pSprite->End();
m_pManager->m_pStateBlock->Apply();
return S_OK;
}
//--------------------------------------------------------------------------------------
VOID CDXUTDialog::SendEvent( UINT nEvent, bool bTriggeredByUser, CDXUTControl* pControl )
{
// If no callback has been registered there's nowhere to send the event to
if( m_pCallbackEvent == NULL )
return;
// Discard events triggered programatically if these types of events haven't been
// enabled
if( !bTriggeredByUser && !m_bNonUserEvents )
return;
m_pCallbackEvent( nEvent, pControl->GetID(), pControl, m_pCallbackEventUserContext );
}
//--------------------------------------------------------------------------------------
int CDXUTDialogResourceManager::AddFont( LPCWSTR strFaceName, LONG height, LONG weight )
{
// See if this font already exists
for( int i=0; i < m_FontCache.GetSize(); i++ )
{
DXUTFontNode* pFontNode = m_FontCache.GetAt(i);
size_t nLen = 0;
StringCchLength( strFaceName, MAX_PATH, &nLen );
if( 0 == _wcsnicmp( pFontNode->strFace, strFaceName, nLen ) &&
pFontNode->nHeight == height &&
pFontNode->nWeight == weight )
{
return i;
}
}
// Add a new font and try to create it
DXUTFontNode* pNewFontNode = new DXUTFontNode();
if( pNewFontNode == NULL )
return -1;
ZeroMemory( pNewFontNode, sizeof(DXUTFontNode) );
StringCchCopy( pNewFontNode->strFace, MAX_PATH, strFaceName );
pNewFontNode->nHeight = height;
pNewFontNode->nWeight = weight;
m_FontCache.Add( pNewFontNode );
int iFont = m_FontCache.GetSize()-1;
// If a device is available, try to create immediately
if( m_pd3dDevice )
CreateFont( iFont );
return iFont;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDialog::SetFont( UINT index, LPCWSTR strFaceName, LONG height, LONG weight )
{
// If this assert triggers, you need to call CDXUTDialog::Init() first. This change
// was made so that the DXUT's GUI could become seperate and optional from DXUT's core. The
// creation and interfacing with CDXUTDialogResourceManager is now the responsibility
// of the application if it wishes to use DXUT's GUI.
assert( m_pManager != NULL && L"To fix call CDXUTDialog::Init() first. See comments for details." );
// Make sure the list is at least as large as the index being set
UINT i;
for( i=m_Fonts.GetSize(); i <= index; i++ )
{
m_Fonts.Add( -1 );
}
int iFont = m_pManager->AddFont( strFaceName, height, weight );
m_Fonts.SetAt( index, iFont );
return S_OK;
}
//--------------------------------------------------------------------------------------
DXUTFontNode* CDXUTDialog::GetFont( UINT index )
{
if( NULL == m_pManager )
return NULL;
return m_pManager->GetFontNode( m_Fonts.GetAt( index ) );
}
//--------------------------------------------------------------------------------------
int CDXUTDialogResourceManager::AddTexture( LPCWSTR strFilename )
{
// See if this texture already exists
for( int i=0; i < m_TextureCache.GetSize(); i++ )
{
DXUTTextureNode* pTextureNode = m_TextureCache.GetAt(i);
size_t nLen = 0;
StringCchLength( strFilename, MAX_PATH, &nLen );
if( pTextureNode->bFileSource && // Sources must match
0 == _wcsnicmp( pTextureNode->strFilename, strFilename, nLen ) )
return i;
}
// Add a new texture and try to create it
DXUTTextureNode* pNewTextureNode = new DXUTTextureNode();
if( pNewTextureNode == NULL )
return -1;
ZeroMemory( pNewTextureNode, sizeof(DXUTTextureNode) );
pNewTextureNode->bFileSource = true;
StringCchCopy( pNewTextureNode->strFilename, MAX_PATH, strFilename );
m_TextureCache.Add( pNewTextureNode );
int iTexture = m_TextureCache.GetSize()-1;
// If a device is available, try to create immediately
if( m_pd3dDevice )
CreateTexture( iTexture );
return iTexture;
}
//--------------------------------------------------------------------------------------
int CDXUTDialogResourceManager::AddTexture( LPCWSTR strResourceName, HMODULE hResourceModule )
{
// See if this texture already exists
for( int i=0; i < m_TextureCache.GetSize(); i++ )
{
DXUTTextureNode* pTextureNode = m_TextureCache.GetAt(i);
if( !pTextureNode->bFileSource && // Sources must match
pTextureNode->hResourceModule == hResourceModule ) // Module handles must match
{
if( IS_INTRESOURCE( strResourceName ) )
{
// Integer-based ID
if( (INT_PTR)strResourceName == pTextureNode->nResourceID )
return i;
}
else
{
// String-based ID
size_t nLen = 0;
StringCchLength( strResourceName, MAX_PATH, &nLen );
if( 0 == _wcsnicmp( pTextureNode->strFilename, strResourceName, nLen ) )
return i;
}
}
}
// Add a new texture and try to create it
DXUTTextureNode* pNewTextureNode = new DXUTTextureNode();
if( pNewTextureNode == NULL )
return -1;
ZeroMemory( pNewTextureNode, sizeof(DXUTTextureNode) );
pNewTextureNode->hResourceModule = hResourceModule;
if( IS_INTRESOURCE( strResourceName ) )
{
pNewTextureNode->nResourceID = (int)(size_t)strResourceName;
}
else
{
pNewTextureNode->nResourceID = 0;
StringCchCopy( pNewTextureNode->strFilename, MAX_PATH, strResourceName );
}
m_TextureCache.Add( pNewTextureNode );
int iTexture = m_TextureCache.GetSize()-1;
// If a device is available, try to create immediately
if( m_pd3dDevice )
CreateTexture( iTexture );
return iTexture;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDialog::SetTexture( UINT index, LPCWSTR strFilename )
{
// If this assert triggers, you need to call CDXUTDialog::Init() first. This change
// was made so that the DXUT's GUI could become seperate and optional from DXUT's core. The
// creation and interfacing with CDXUTDialogResourceManager is now the responsibility
// of the application if it wishes to use DXUT's GUI.
assert( m_pManager != NULL && L"To fix this, call CDXUTDialog::Init() first. See comments for details." );
// Make sure the list is at least as large as the index being set
for( UINT i=m_Textures.GetSize(); i <= index; i++ )
{
m_Textures.Add( -1 );
}
int iTexture = m_pManager->AddTexture( strFilename );
m_Textures.SetAt( index, iTexture );
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT CDXUTDialog::SetTexture( UINT index, LPCWSTR strResourceName, HMODULE hResourceModule )
{
// If this assert triggers, you need to call CDXUTDialog::Init() first. This change
// was made so that the DXUT's GUI could become seperate and optional from DXUT's core. The
// creation and interfacing with CDXUTDialogResourceManager is now the responsibility
// of the application if it wishes to use DXUT's GUI.
assert( m_pManager != NULL && L"To fix this, call CDXUTDialog::Init() first. See comments for details." );
// Make sure the list is at least as large as the index being set
for( UINT i=m_Textures.GetSize(); i <= index; i++ )
{
m_Textures.Add( -1 );
}
int iTexture = m_pManager->AddTexture( strResourceName, hResourceModule );
m_Textures.SetAt( index, iTexture );
return S_OK;
}
//--------------------------------------------------------------------------------------
DXUTTextureNode* CDXUTDialog::GetTexture( UINT index )
{
if( NULL == m_pManager )
return NULL;
return m_pManager->GetTextureNode( m_Textures.GetAt( index ) );
}
//--------------------------------------------------------------------------------------
bool CDXUTDialog::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
bool bHandled = false;
// For invisible dialog, do not handle anything.
if( !m_bVisible )
return false;
// If automation command-line switch is on, enable this dialog's keyboard input
// upon any key press or mouse click.
if( DXUTGetAutomation() &&
( WM_LBUTTONDOWN == uMsg || WM_LBUTTONDBLCLK == uMsg || WM_KEYDOWN == uMsg ) )
{
m_pManager->EnableKeyboardInputForAllDialogs();
}
// If caption is enable, check for clicks in the caption area.
if( m_bCaption )
{
if( uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONDBLCLK )
{
POINT mousePoint = { short(LOWORD(lParam)), short(HIWORD(lParam)) };
if( mousePoint.x >= m_x && mousePoint.x < m_x + m_width &&
mousePoint.y >= m_y && mousePoint.y < m_y + m_nCaptionHeight )
{
m_bDrag = true;
SetCapture( DXUTGetHWND() );
return true;
}
} else
if( uMsg == WM_LBUTTONUP && m_bDrag )
{
POINT mousePoint = { short(LOWORD(lParam)), short(HIWORD(lParam)) };
if( mousePoint.x >= m_x && mousePoint.x < m_x + m_width &&
mousePoint.y >= m_y && mousePoint.y < m_y + m_nCaptionHeight )
{
ReleaseCapture();
m_bDrag = false;
m_bMinimized = !m_bMinimized;
return true;
}
}
}
// If the dialog is minimized, don't send any messages to controls.
if( m_bMinimized )
return false;
// If a control is in focus, it belongs to this dialog, and it's enabled, then give
// it the first chance at handling the message.
if( s_pControlFocus &&
s_pControlFocus->m_pDialog == this &&
s_pControlFocus->GetEnabled() )
{
// If the control MsgProc handles it, then we don't.
if( s_pControlFocus->MsgProc( uMsg, wParam, lParam ) )
return true;
}
switch( uMsg )
{
case WM_SIZE:
case WM_MOVE:
{
// Handle sizing and moving messages so that in case the mouse cursor is moved out
// of an UI control because of the window adjustment, we can properly
// unhighlight the highlighted control.
POINT pt = { -1, -1 };
OnMouseMove( pt );
break;
}
case WM_ACTIVATEAPP:
// Call OnFocusIn()/OnFocusOut() of the control that currently has the focus
// as the application is activated/deactivated. This matches the Windows
// behavior.
if( s_pControlFocus &&
s_pControlFocus->m_pDialog == this &&
s_pControlFocus->GetEnabled() )
{
if( wParam )
s_pControlFocus->OnFocusIn();
else
s_pControlFocus->OnFocusOut();
}
break;
// Keyboard messages
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
{
// If a control is in focus, it belongs to this dialog, and it's enabled, then give
// it the first chance at handling the message.
if( s_pControlFocus &&
s_pControlFocus->m_pDialog == this &&
s_pControlFocus->GetEnabled() )
{
if( s_pControlFocus->HandleKeyboard( uMsg, wParam, lParam ) )
return true;
}
// Not yet handled, see if this matches a control's hotkey
// Activate the hotkey if the focus doesn't belong to an
// edit box.
if( uMsg == WM_KEYDOWN && ( !s_pControlFocus ||
( s_pControlFocus->GetType() != DXUT_CONTROL_EDITBOX
&& s_pControlFocus->GetType() != DXUT_CONTROL_IMEEDITBOX ) ) )
{
for( int i=0; i < m_Controls.GetSize(); i++ )
{
CDXUTControl* pControl = m_Controls.GetAt( i );
if( pControl->GetHotkey() == wParam )
{
pControl->OnHotkey();
return true;
}
}
}
// Not yet handled, check for focus messages
if( uMsg == WM_KEYDOWN )
{
// If keyboard input is not enabled, this message should be ignored
if( !m_bKeyboardInput )
return false;
switch( wParam )
{
case VK_RIGHT:
case VK_DOWN:
if( s_pControlFocus != NULL )
{
return OnCycleFocus( true );
}
break;
case VK_LEFT:
case VK_UP:
if( s_pControlFocus != NULL )
{
return OnCycleFocus( false );
}
break;
case VK_TAB:
{
bool bShiftDown = ((GetKeyState( VK_SHIFT ) & 0x8000) != 0);
return OnCycleFocus( !bShiftDown );
}
}
}
break;
}
// Mouse messages
case WM_MOUSEMOVE:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_XBUTTONDOWN:
case WM_XBUTTONUP:
case WM_LBUTTONDBLCLK: