-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
buttonwidget.cpp
196 lines (166 loc) · 5.19 KB
/
buttonwidget.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
/** @file buttonwidget.cpp Button widget.
*
* @authors Copyright © 2005-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2014 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "common.h"
#include "menu/widgets/buttonwidget.h"
#include "g_defs.h"
#include "hu_menu.h" // menu sounds
#include "hu_stuff.h"
#include "menu/page.h" // mnRendState
using namespace de;
namespace common {
namespace menu {
DENG2_PIMPL_NOREF(ButtonWidget)
{
String text; ///< Label text.
patchid_t patch = -1; ///< Used when drawing this instead of text, if set.
bool noAltText = false;
bool silent = false;
};
ButtonWidget::ButtonWidget(String const &text, patchid_t patch)
: Widget()
, d(new Impl)
{
setFont(MENU_FONT2);
setColor(MENU_COLOR1);
setText(text);
setPatch(patch);
}
ButtonWidget::~ButtonWidget()
{}
void ButtonWidget::draw() const
{
fontid_t const fontId = mnRendState->textFonts[font()];
Vector4f const &textColor = mnRendState->textColors[color()];
//float t = (isFocused()? 1 : 0);
const Vector4f color = selectionFlashColor(textColor);
// // Flash if focused.
// if (isFocused() && cfg.common.menuTextFlashSpeed > 0)
// {
// float const speed = cfg.common.menuTextFlashSpeed / 2.f;
// t = (1 + sin(page().timer() / (float)TICSPERSEC * speed * DD_PI)) / 2;
// }
// Vector4f const color = de::lerp(textColor, Vector4f(Vector3f(cfg.common.menuTextFlashColor), textColor.w), t);
const float fadeout = scrollingFadeout();
if (fadeout < .001f) return;
FR_SetFont(fontId);
FR_SetColorAndAlpha(color.x, color.y, color.z, color.w * fadeout);
DGL_Color4f(1, 1, 1, color.w * fadeout);
if(d->patch >= 0)
{
String replacement;
if(!d->noAltText)
{
replacement = Hu_ChoosePatchReplacement(patchreplacemode_t(cfg.common.menuPatchReplaceMode), d->patch, d->text);
}
DGL_Enable(DGL_TEXTURE_2D);
WI_DrawPatch(d->patch,
replacement,
geometry().topLeft,
ALIGN_TOPLEFT,
0,
Hu_MenuMergeEffectWithDrawTextFlags(0));
DGL_Disable(DGL_TEXTURE_2D);
return;
}
DGL_Enable(DGL_TEXTURE_2D);
FR_DrawTextXY3(d->text.toUtf8().constData(), geometry().topLeft.x, geometry().topLeft.y,
ALIGN_TOPLEFT, Hu_MenuMergeEffectWithDrawTextFlags(0));
DGL_Disable(DGL_TEXTURE_2D);
}
int ButtonWidget::handleCommand(menucommand_e cmd)
{
if (cmd == MCMD_SELECT)
{
if (!isActive())
{
setFlags(Active);
execAction(Activated);
}
// We are not going to receive a separate "up event".
if (!d->silent)
{
S_LocalSound(SFX_MENU_ACCEPT, NULL);
}
setFlags(Active, UnsetFlags);
execAction(Deactivated);
return true;
}
return false; // Not eaten.
}
void ButtonWidget::setSilent(bool silent)
{
d->silent = silent;
}
void ButtonWidget::updateGeometry()
{
String useText = d->text;
// @todo What if patch replacement is disabled?
if(d->patch >= 0)
{
if(!d->noAltText)
{
// Use the replacement string?
useText = Hu_ChoosePatchReplacement(patchreplacemode_t(cfg.common.menuPatchReplaceMode), d->patch, d->text);
}
if(useText.isEmpty())
{
// Use the original patch.
patchinfo_t info;
R_GetPatchInfo(d->patch, &info);
geometry().setSize(Vector2ui(info.geometry.size.width, info.geometry.size.height));
return;
}
}
FR_PushAttrib();
Size2Raw size;
FR_SetFont(page().predefinedFont(mn_page_fontid_t(font())));
FR_TextSize(&size, useText.toUtf8().constData());
geometry().setSize(Vector2ui(size.width, size.height));
FR_PopAttrib();
}
String ButtonWidget::text() const
{
return d->text;
}
ButtonWidget &ButtonWidget::setText(String const &newText)
{
d->text = labelText(newText);
return *this;
}
patchid_t ButtonWidget::patch() const
{
return d->patch;
}
ButtonWidget &ButtonWidget::setPatch(patchid_t newPatch)
{
d->patch = newPatch;
return *this;
}
bool ButtonWidget::noAltText() const
{
return d->noAltText;
}
ButtonWidget &ButtonWidget::setNoAltText(bool yes)
{
d->noAltText = yes;
return *this;
}
} // namespace menu
} // namespace common