forked from abartlett139/Omni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIBase.cpp
152 lines (134 loc) · 3.64 KB
/
UIBase.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
#include "UIBase.h"
#define PARENT 0
void UIBase::SetFocus( UIBase * keyFocus )
{
if( !m_Focus )
{
if( m_vControl[ PARENT ] )
m_vControl[ PARENT ]->SetFocus( keyFocus );
else
{
if( GetFocus( ) )
GetFocus( )->m_Focus = false;
m_pUIFocus = keyFocus;
m_Focus = true;
}
}
}
bool UIBase::PostMessage( UINT msg, WPARAM wParam, LPARAM lParam, void * Data )
{
switch( msg )
{
case WM_LBUTTONUP:
case WM_LBUTTONDOWN:
if( CursorIntersect( LOWORD( lParam ), HIWORD( lParam ) ) )
{
UIBase* ChildControl = PostToAll( msg, wParam, lParam, Data );
if (!ChildControl)
{
OnMouseUp(msg, LOWORD(lParam), HIWORD(lParam));
OnMouseDown(msg, LOWORD(lParam), HIWORD(lParam));
}
return true;
}
else
return false;
break;
case WM_MOUSEMOVE:
if( CursorIntersect( LOWORD( lParam ), HIWORD( lParam ) ) )
{
m_Focus = true;
UIBase* ChildControl = PostToAll( msg, wParam, lParam, Data );
if( !ChildControl )
OnMouseMove( LOWORD( lParam ), HIWORD( lParam ) );
return true;
}
else
{
m_Focus = false;
OnMouseMove(LOWORD(lParam), HIWORD(lParam));
return false;
}
break;
case WM_KEYUP:
case WM_KEYDOWN:
if( GetFocus( ) )
{
if( msg == WM_KEYUP )
GetFocus( )->OnKeyUp( wParam, lParam );
if( msg == WM_KEYDOWN )
GetFocus( )->OnKeyDown( wParam, lParam );
}
break;
case WM_PAINT:
OnRender();
if (m_vControl.size() > 1)
PostToAllReverse(msg, wParam, lParam, Data);
break;
case WM_SIZING:
default:
break;
}
return false;
}
bool UIBase::CursorIntersect( FLOAT x, FLOAT y )
{
D3DXVECTOR2 l_AbsolutPosTL, l_AbsolutPosBR;
//variable for the top right corner
l_AbsolutPosTL.x = 0;
l_AbsolutPosTL.y = 0;
//variable for the bottom left corner
l_AbsolutPosBR.x = 0;
l_AbsolutPosBR.y = 0;
GetAbsolutePosition( &l_AbsolutPosTL);
GetAbsolutePosition( &l_AbsolutPosBR);
l_AbsolutPosBR.x += GetWidth( );
l_AbsolutPosBR.y += GetHeight( );
if( ((x >= l_AbsolutPosTL.x) && (x <= l_AbsolutPosBR.x)) && ((y >= l_AbsolutPosTL.y) && (y <= l_AbsolutPosBR.y)))
return true;
return false;
}
UIBase * UIBase::PostToAll( UINT msg, WPARAM wParam, LPARAM lParam, void * Data )
{
for(UINT i=1;i < m_vControl.size( );i++ )
{
if( m_vControl[i]->PostMessage( msg, wParam, lParam, Data ) )
return m_vControl[ i ];
}
return NULL;
}
UIBase * UIBase::PostToAllReverse(UINT msg, WPARAM wParam, LPARAM lParam, void * Data)
{
for (UINT i = (UINT)m_vControl.size()-1; i >0; i--)//goes backwards through the vector excluding the first element which is a pointer to the parent
{
m_vControl[i]->PostToAllReverse(msg, wParam, lParam, Data);
}
PostMessage(msg, wParam, lParam, Data);
return nullptr;
}
UIBase::UIBase( UIBase * parent, int vecPos)
{
m_ChildCount = 0;
m_vControl.clear( );
m_vControl.push_back( parent );
m_thisVecPos = vecPos;
}
UIBase * UIBase::AddChildControl(UIBase * child)
{
m_vControl.push_back(child);//add the child to this controls children
child->SetSprite(this->GetSprite());// propagate the sprite to the child
return nullptr;
}
void UIBase::SetTexture(Texture * tex)
{
m_Texture = tex;
SetHeight(m_Texture->GetHeight());
SetWidth(m_Texture->GetWidth());
}
void UIBase::GetAbsolutePosition( D3DXVECTOR2 * Position )
{
D3DXVec2Add( Position, Position, &m_Position );
}
UIBase::~UIBase( )
{
}