-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuicore_Button.cpp
More file actions
166 lines (136 loc) · 4.5 KB
/
uicore_Button.cpp
File metadata and controls
166 lines (136 loc) · 4.5 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
/*
Copyright (C) 2007 Benjamin Litzelmann
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "uicore_Global.h"
#if 0
int shadowoffset = 1;
if( !font )
font = uis.fontSystemSmall;
shadowoffset += ( trap_SCR_strHeight( font ) >= trap_SCR_strHeight( uis.fontSystemBig ) );
if( maxwidth > 0 )
{
//trap_SCR_DrawStringWidth( x+shadowoffset, y+shadowoffset, align, COM_RemoveColorTokens( str ), maxwidth, font, colorBlack );
//trap_SCR_DrawStringWidth( x, y, align, COM_RemoveColorTokens( str ), maxwidth, font, UI_COLOR_HIGHLIGHT );
x += UISCR_HorizontalAlignOffset( align, maxwidth );
y += UISCR_VerticalAlignOffset( align, trap_SCR_strHeight( font ) );
trap_SCR_DrawClampString( x+shadowoffset, y+shadowoffset, COM_RemoveColorTokens( str ), x+shadowoffset, y+shadowoffset, x+shadowoffset+maxwidth, y+shadowoffset+trap_SCR_strHeight( font ), font, colorBlack );
trap_SCR_DrawClampString( x, y, COM_RemoveColorTokens( str ), x, y, x+maxwidth, y+trap_SCR_strHeight( font ), font, UI_COLOR_HIGHLIGHT );
}
else
{
trap_SCR_DrawString( x+shadowoffset, y+shadowoffset, align, str, font, colorBlack );
#endif
namespace UICore
{
ALLOCATOR_DEFINITION(Button)
DELETER_DEFINITION(Button)
void Button::Initialize( void )
{
setHighlightColor( Color( 0, 0, 0, 0 ) );
setHighlightFontColor( Color( 0, 0, 0, 0 ) );
setHighlightImage( NULL );
setFocusable( true );
setAlign( ALIGN_MIDDLE_CENTER );
}
Button::Button()
: Label()
{
Initialize();
}
Button::Button( BaseObject *parent, float x, float y, float w, float h, std::string caption )
: Label( NULL, x, y, w, h, caption )
{
Initialize();
setParent( parent );
}
Button::~Button()
{
}
void Button::DrawText( const Rect &posSrc, const Rect &posDest, bool enabled )
{
if ( !font )
return;
Color backupColor;
backupColor = fontColor;
if( mouseOverObject == this || focusedObject == this )
{
Rect shadowRect;
float shadowOffset = 1;
shadowRect = posDest;
shadowOffset += ( Importer::StringHeight( font ) >= 30 ); // FIXME
shadowRect.x += shadowOffset;
shadowRect.y += shadowOffset;
fontColor = Color( 0, 0, 0, 1 );
Label::DrawText( posSrc, shadowRect, enabled );
fontColor = highlightFontColor;
}
Label::DrawText( posSrc, posDest, enabled );
fontColor = backupColor;
}
void Button::DrawBackground( const Rect &posSrc, const Rect &posDest, bool enabled )
{
Rect rect;
rect.x = posDest.x + posSrc.x;
rect.y = posDest.y + posSrc.y;
rect.w = posSrc.w;
rect.h = posSrc.h;
if ( !enabled || !this->enabled )
{
if ( disabledImage )
Importer::DrawImage( disabledImage, posSrc, posDest, disabledColor, rotation );
else
Importer::FillRect( rect, disabledColor );
}
else if ( mouseOverObject == this || focusedObject == this )
{
if ( highlightImage )
Importer::DrawImage( highlightImage, posSrc, posDest, highlightColor, rotation );
else
Importer::FillRect( rect, highlightColor );
}
else
{
if ( backgroundImage )
Importer::DrawImage( backgroundImage, posSrc, posDest, backColor, rotation );
else
Importer::FillRect( rect, backColor );
}
}
void Button::BaseKeyDown( int key, int charVal )
{
if( !isEnabled() )
return;
if ( charVal == Importer::keySpace || charVal == Importer::keyEnter || charVal == Importer::keyPadEnter )
{
if ( Click )
Click( this );
}
else
BaseObject::BaseKeyDown( key, charVal );
}
void Button::BaseMouseUp( float x, float y, int button )
{
if( !isEnabled() )
return;
BaseObject::BaseMouseUp( x, y, button );
// special for button only (not inheritable)
if ( getType() == UICORE_TYPE_BUTTON )
{
// buttons don't keep focus after being pressed
if ( LostFocus )
LostFocus( this );
focusedObject = NULL;
}
}
}