Skip to content
Navigation Menu
{{ message }}
forked from endless-sky/endless-sky
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialog.cpp
More file actions
332 lines (277 loc) · 8.43 KB
/
Copy pathDialog.cpp
File metadata and controls
332 lines (277 loc) · 8.43 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
/* Dialog.cpp
Copyright (c) 2014 by Michael Zahniser
Endless Sky 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 3 of the License, or (at your option) any later version.
Endless Sky 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.
*/
#include "Dialog.h"
#include "Color.h"
#include "Command.h"
#include "Conversation.h"
#include "DataNode.h"
#include "FillShader.h"
#include "Font.h"
#include "FontSet.h"
#include "GameData.h"
#include "MapDetailPanel.h"
#include "PlayerInfo.h"
#include "Point.h"
#include "shift.h"
#include "Sprite.h"
#include "SpriteSet.h"
#include "SpriteShader.h"
#include "UI.h"
#include <cmath>
#include <functional>
using namespace std;
namespace {
const int WIDTH = 250;
// Map any conceivable numeric keypad keys to their ASCII values. Most of
// these will presumably only exist on special programming keyboards.
const map<SDL_Keycode, char> KEY_MAP = {
{SDLK_KP_0, '0'},
{SDLK_KP_1, '1'},
{SDLK_KP_2, '2'},
{SDLK_KP_3, '3'},
{SDLK_KP_4, '4'},
{SDLK_KP_5, '5'},
{SDLK_KP_6, '6'},
{SDLK_KP_7, '7'},
{SDLK_KP_8, '8'},
{SDLK_KP_9, '9'},
{SDLK_KP_AMPERSAND, '&'},
{SDLK_KP_AT, '@'},
{SDLK_KP_A, 'a'},
{SDLK_KP_B, 'b'},
{SDLK_KP_C, 'c'},
{SDLK_KP_D, 'd'},
{SDLK_KP_E, 'e'},
{SDLK_KP_F, 'f'},
{SDLK_KP_COLON, ':'},
{SDLK_KP_COMMA, ','},
{SDLK_KP_DIVIDE, '/'},
{SDLK_KP_EQUALS, '='},
{SDLK_KP_EXCLAM, '!'},
{SDLK_KP_GREATER, '>'},
{SDLK_KP_HASH, '#'},
{SDLK_KP_LEFTBRACE, '{'},
{SDLK_KP_LEFTPAREN, '('},
{SDLK_KP_LESS, '<'},
{SDLK_KP_MINUS, '-'},
{SDLK_KP_MULTIPLY, '*'},
{SDLK_KP_PERCENT, '%'},
{SDLK_KP_PERIOD, '.'},
{SDLK_KP_PLUS, '+'},
{SDLK_KP_POWER, '^'},
{SDLK_KP_RIGHTBRACE, '}'},
{SDLK_KP_RIGHTPAREN, ')'},
{SDLK_KP_SPACE, ' '},
{SDLK_KP_VERTICALBAR, '|'}
};
}
// Dialog that has no callback (information only). In this form, there is
// only an "ok" button, not a "cancel" button.
Dialog::Dialog(const string &text)
{
Init(text, false);
}
// Mission accept / decline dialog.
Dialog::Dialog(const string &text, PlayerInfo &player, const System *system)
: intFun(bind(&PlayerInfo::MissionCallback, &player, placeholders::_1)),
system(system), player(&player)
{
Init(text, true, true);
}
// Draw this panel.
void Dialog::Draw()
{
DrawBackdrop();
const Sprite *top = SpriteSet::Get("ui/dialog top");
const Sprite *middle = SpriteSet::Get("ui/dialog middle");
const Sprite *bottom = SpriteSet::Get("ui/dialog bottom");
const Sprite *cancel = SpriteSet::Get("ui/dialog cancel");
// Get the position of the top of this dialog, and of the text and input.
Point pos(0., (top->Height() + height * middle->Height() + bottom->Height()) * -.5f);
Point textPos(WIDTH * -.5 + 10., pos.Y() + 20.);
Point inputPos = Point(0., -70.) - pos;
// Draw the top section of the dialog box.
pos.Y() += top->Height() * .5;
SpriteShader::Draw(top, pos);
pos.Y() += top->Height() * .5;
// The middle section is duplicated depending on how long the text is.
for(int i = 0; i < height; ++i)
{
pos.Y() += middle->Height() * .5;
SpriteShader::Draw(middle, pos);
pos.Y() += middle->Height() * .5;
}
// Draw the bottom section.
const Font &font = FontSet::Get(14);
pos.Y() += bottom->Height() * .5;
SpriteShader::Draw(bottom, pos);
pos.Y() += bottom->Height() * .5 - 25.;
// Draw the buttons, including optionally the cancel button.
const Color &bright = *GameData::Colors().Get("bright");
const Color &dim = *GameData::Colors().Get("medium");
const Color &back = *GameData::Colors().Get("faint");
if(canCancel)
{
string cancelText = isMission ? "Decline" : "Cancel";
cancelPos = pos + Point(10., 0.);
SpriteShader::Draw(cancel, cancelPos);
Point labelPos(
cancelPos.X() - .5 * font.Width(cancelText),
cancelPos.Y() - .5 * font.Height());
font.Draw(cancelText, labelPos, !okIsActive ? bright : dim);
}
string okText = isMission ? "Accept" : "OK";
okPos = pos + Point(90., 0.);
Point labelPos(
okPos.X() - .5 * font.Width(okText),
okPos.Y() - .5 * font.Height());
font.Draw(okText, labelPos, okIsActive ? bright : dim);
// Draw the text.
text.Draw(textPos, dim);
// Draw the input, if any.
if(!isMission && (intFun || stringFun))
{
FillShader::Fill(inputPos, Point(WIDTH - 20., 20.), back);
Point stringPos(
inputPos.X() - (WIDTH - 20) * .5 + 5.,
inputPos.Y() - .5 * font.Height());
string truncated = font.TruncateFront(input, WIDTH - 30);
font.Draw(truncated, stringPos, bright);
Point barPos(stringPos.X() + font.Width(truncated) + 2., inputPos.Y());
FillShader::Fill(barPos, Point(1., 16.), dim);
}
}
// Format and add the text from the given node to the given string.
void Dialog::ParseTextNode(const DataNode &node, size_t startingIndex, string &text)
{
for(int i = startingIndex; i < node.Size(); ++i)
{
if(!text.empty())
text += "\n\t";
text += node.Token(i);
}
for(const DataNode &child : node)
for(int i = 0; i < child.Size(); ++i)
{
if(!text.empty())
text += "\n\t";
text += child.Token(i);
}
}
bool Dialog::KeyDown(SDL_Keycode key, Uint16 mod, const Command &command, bool isNewPress)
{
auto it = KEY_MAP.find(key);
bool isCloseRequest = key == SDLK_ESCAPE || (key == 'w' && (mod & (KMOD_CTRL | KMOD_GUI)));
if((it != KEY_MAP.end() || (key >= ' ' && key <= '~')) && !isMission && (intFun || stringFun) && !isCloseRequest)
{
int ascii = (it != KEY_MAP.end()) ? it->second : key;
char c = ((mod & KMOD_SHIFT) ? SHIFT[ascii] : ascii);
// Caps lock should shift letters, but not any other keys.
if((mod & KMOD_CAPS) && c >= 'a' && c <= 'z')
c += 'A' - 'a';
if(stringFun)
input += c;
// Integer input should not allow leading zeros.
else if(intFun && c == '0' && !input.empty())
input += c;
else if(intFun && c >= '1' && c <= '9')
input += c;
}
else if((key == SDLK_DELETE || key == SDLK_BACKSPACE) && !input.empty())
input.erase(input.length() - 1);
else if(key == SDLK_TAB && canCancel)
okIsActive = !okIsActive;
else if(key == SDLK_LEFT)
okIsActive = !canCancel;
else if(key == SDLK_RIGHT)
okIsActive = true;
else if(key == SDLK_RETURN || key == SDLK_KP_ENTER || isCloseRequest
|| (isMission && (key == 'a' || key == 'd')))
{
// Shortcuts for "accept" and "decline."
if(key == 'a' || (!canCancel && isCloseRequest))
okIsActive = true;
if(key == 'd' || (canCancel && isCloseRequest))
okIsActive = false;
if(okIsActive || isMission)
DoCallback();
GetUI()->Pop(this);
}
else if((key == 'm' || command.Has(Command::MAP)) && system && player)
GetUI()->Push(new MapDetailPanel(*player, system));
else
return false;
return true;
}
bool Dialog::Click(int x, int y, int clicks)
{
Point clickPos(x, y);
Point ok = clickPos - okPos;
if(fabs(ok.X()) < 40. && fabs(ok.Y()) < 20.)
{
okIsActive = true;
return DoKey(SDLK_RETURN);
}
if(canCancel)
{
Point cancel = clickPos - cancelPos;
if(fabs(cancel.X()) < 40. && fabs(cancel.Y()) < 20.)
{
okIsActive = false;
return DoKey(SDLK_RETURN);
}
}
return true;
}
// Common code from all three constructors:
void Dialog::Init(const string &message, bool canCancel, bool isMission)
{
this->isMission = isMission;
this->canCancel = canCancel;
okIsActive = true;
text.SetAlignment(WrappedText::JUSTIFIED);
text.SetWrapWidth(WIDTH - 20);
text.SetFont(FontSet::Get(14));
text.Wrap(message);
// The dialog with no extenders is 80 pixels tall. 10 pixels at the top and
// bottom are "padding," but text.Height() over-reports the height by about
// 5 pixels because it includes its own padding at the bottom. If there is a
// text input, we need another 20 pixels for it and 10 pixels padding.
height = 10 + (text.Height() - 5) + 10 + 30 * (!isMission && (intFun || stringFun));
// Determine how many 40-pixel extension panels we need.
if(height <= 80)
height = 0;
else
height = (height - 40) / 40;
}
void Dialog::DoCallback() const
{
if(isMission)
{
if(intFun)
intFun(okIsActive ? Conversation::ACCEPT : Conversation::DECLINE);
return;
}
if(intFun)
{
// Only call the callback if the input can be converted to an int.
// Otherwise treat this as if the player clicked "cancel."
try {
intFun(stoi(input));
}
catch(...)
{
}
}
if(stringFun)
stringFun(input);
if(voidFun)
voidFun();
}
You can’t perform that action at this time.
