/
ResearchInfoState.cpp
340 lines (302 loc) · 9.65 KB
/
ResearchInfoState.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
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
/*
* Copyright 2010-2016 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom 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.
*
* OpenXcom 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 OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ResearchInfoState.h"
#include "../Engine/Action.h"
#include "../Engine/Game.h"
#include "../Mod/Mod.h"
#include "../Engine/LocalizedText.h"
#include "../Engine/Options.h"
#include "../Interface/TextButton.h"
#include "../Interface/Window.h"
#include "../Interface/Text.h"
#include "../Savegame/Base.h"
#include "../Mod/RuleResearch.h"
#include "../Savegame/ItemContainer.h"
#include "../Savegame/ResearchProject.h"
#include "../Interface/ArrowButton.h"
#include "../Engine/Timer.h"
#include "../Engine/RNG.h"
#include <climits>
namespace OpenXcom
{
/**
* Initializes all the elements in the ResearchProject screen.
* @param game Pointer to the core game.
* @param base Pointer to the base to get info from.
* @param rule A RuleResearch which will be used to create a new ResearchProject
*/
ResearchInfoState::ResearchInfoState(Base *base, RuleResearch *rule) : _base(base), _project(new ResearchProject(rule, int(rule->getCost() * OpenXcom::RNG::generate(50, 150)/100))), _rule(rule)
{
buildUi();
}
/**
* Initializes all the elements in the ResearchProject screen.
* @param game Pointer to the core game.
* @param base Pointer to the base to get info from.
* @param project A ResearchProject to modify
*/
ResearchInfoState::ResearchInfoState(Base *base, ResearchProject *project) : _base(base), _project(project), _rule(0)
{
buildUi();
}
/**
* Builds dialog.
*/
void ResearchInfoState::buildUi()
{
_screen = false;
_window = new Window(this, 230, 140, 45, 30);
_txtTitle = new Text(210, 17, 61, 40);
_txtAvailableScientist = new Text(210, 9, 61, 60);
_txtAvailableSpace = new Text(210, 9, 61, 70);
_txtAllocatedScientist = new Text(210, 17, 61, 80);
_txtMore = new Text(110, 17, 85, 100);
_txtLess = new Text(110, 17, 85, 120);
_btnCancel = new TextButton(90, 16, 61, 145);
_btnOk = new TextButton(90, 16, 169, 145);
_btnMore = new ArrowButton(ARROW_BIG_UP, 13, 14, 195, 100);
_btnLess = new ArrowButton(ARROW_BIG_DOWN, 13, 14, 195, 120);
_surfaceScientists = new InteractiveSurface(230, 140, 45, 30);
_surfaceScientists->onMouseClick((ActionHandler)&ResearchInfoState::handleWheel, 0);
// Set palette
setInterface("allocateResearch");
add(_surfaceScientists);
add(_window, "window", "allocateResearch");
add(_btnOk, "button2", "allocateResearch");
add(_btnCancel, "button2", "allocateResearch");
add(_txtTitle, "text", "allocateResearch");
add(_txtAvailableScientist, "text", "allocateResearch");
add(_txtAvailableSpace, "text", "allocateResearch");
add(_txtAllocatedScientist, "text", "allocateResearch");
add(_txtMore, "text", "allocateResearch");
add(_txtLess, "text", "allocateResearch");
add(_btnMore, "button1", "allocateResearch");
add(_btnLess, "button1", "allocateResearch");
centerAllSurfaces();
// Set up objects
_window->setBackground(_game->getMod()->getSurface("BACK05.SCR"));
_txtTitle->setBig();
_txtTitle->setText(_rule ? tr(_rule->getName()) : tr(_project->getRules()->getName()));
_txtAllocatedScientist->setBig();
_txtMore->setText(tr("STR_INCREASE"));
_txtLess->setText(tr("STR_DECREASE"));
_txtMore->setBig();
_txtLess->setBig();
if (_rule)
{
_base->addResearch(_project);
if (_rule->needItem() && _rule->destroyItem())
{
_base->getStorageItems()->removeItem(_rule->getName(), 1);
}
}
setAssignedScientist();
_btnMore->onMousePress((ActionHandler)&ResearchInfoState::morePress);
_btnMore->onMouseRelease((ActionHandler)&ResearchInfoState::moreRelease);
_btnMore->onMouseClick((ActionHandler)&ResearchInfoState::moreClick, 0);
_btnLess->onMousePress((ActionHandler)&ResearchInfoState::lessPress);
_btnLess->onMouseRelease((ActionHandler)&ResearchInfoState::lessRelease);
_btnLess->onMouseClick((ActionHandler)&ResearchInfoState::lessClick, 0);
_timerMore = new Timer(250);
_timerMore->onTimer((StateHandler)&ResearchInfoState::more);
_timerLess = new Timer(250);
_timerLess->onTimer((StateHandler)&ResearchInfoState::less);
_btnOk->onMouseClick((ActionHandler)&ResearchInfoState::btnOkClick);
_btnOk->onKeyboardPress((ActionHandler)&ResearchInfoState::btnOkClick, Options::keyOk);
if (_rule)
{
_btnOk->setText(tr("STR_START_PROJECT"));
_btnCancel->setText(tr("STR_CANCEL_UC"));
_btnCancel->onKeyboardPress((ActionHandler)&ResearchInfoState::btnCancelClick, Options::keyCancel);
}
else
{
_btnOk->setText(tr("STR_OK"));
_btnCancel->setText(tr("STR_CANCEL_PROJECT"));
_btnOk->onKeyboardPress((ActionHandler)&ResearchInfoState::btnOkClick, Options::keyCancel);
}
_btnCancel->onMouseClick((ActionHandler)&ResearchInfoState::btnCancelClick);
}
/**
* Frees up memory that's not automatically cleaned on exit
*/
ResearchInfoState::~ResearchInfoState()
{
delete _timerLess;
delete _timerMore;
}
/**
* Returns to the previous screen.
* @param action Pointer to an action.
*/
void ResearchInfoState::btnOkClick(Action *)
{
_game->popState();
}
/**
* Returns to the previous screen, removing the current project from the active
* research list.
* @param action Pointer to an action.
*/
void ResearchInfoState::btnCancelClick(Action *)
{
_base->removeResearch(_project);
_game->popState();
}
/**
* Updates count of assigned/free scientists and available lab space.
*/
void ResearchInfoState::setAssignedScientist()
{
_txtAvailableScientist->setText(tr("STR_SCIENTISTS_AVAILABLE_UC").arg(_base->getAvailableScientists()));
_txtAvailableSpace->setText(tr("STR_LABORATORY_SPACE_AVAILABLE_UC").arg(_base->getFreeLaboratories()));
_txtAllocatedScientist->setText(tr("STR_SCIENTISTS_ALLOCATED").arg(_project->getAssigned()));
}
/**
* Increases or decreases the scientists according the mouse-wheel used.
* @param action Pointer to an Action.
*/
void ResearchInfoState::handleWheel(Action *action)
{
if (action->getDetails()->button.button == SDL_BUTTON_WHEELUP) moreByValue(Options::changeValueByMouseWheel);
else if (action->getDetails()->button.button == SDL_BUTTON_WHEELDOWN) lessByValue(Options::changeValueByMouseWheel);
}
/**
* Starts the timeMore timer.
* @param action Pointer to an Action.
*/
void ResearchInfoState::morePress(Action *action)
{
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerMore->start();
}
/**
* Stops the timeMore timer.
* @param action Pointer to an Action.
*/
void ResearchInfoState::moreRelease(Action *action)
{
if (action->getDetails()->button.button == SDL_BUTTON_LEFT)
{
_timerMore->setInterval(250);
_timerMore->stop();
}
}
/**
* Allocates scientists to the current project;
* one scientist on left-click, all scientists on right-click.
* @param action Pointer to an Action.
*/
void ResearchInfoState::moreClick(Action *action)
{
if (action->getDetails()->button.button == SDL_BUTTON_RIGHT)
moreByValue(INT_MAX);
if (action->getDetails()->button.button == SDL_BUTTON_LEFT)
moreByValue(1);
}
/**
* Starts the timeLess timer.
* @param action Pointer to an Action.
*/
void ResearchInfoState::lessPress(Action *action)
{
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerLess->start();
}
/**
* Stops the timeLess timer.
* @param action Pointer to an Action.
*/
void ResearchInfoState::lessRelease(Action *action)
{
if (action->getDetails()->button.button == SDL_BUTTON_LEFT)
{
_timerLess->setInterval(250);
_timerLess->stop();
}
}
/**
* Removes scientists from the current project;
* one scientist on left-click, all scientists on right-click.
* @param action Pointer to an Action.
*/
void ResearchInfoState::lessClick(Action *action)
{
if (action->getDetails()->button.button == SDL_BUTTON_RIGHT)
lessByValue(INT_MAX);
if (action->getDetails()->button.button == SDL_BUTTON_LEFT)
lessByValue(1);
}
/**
* Adds one scientist to the project if possible.
*/
void ResearchInfoState::more()
{
_timerMore->setInterval(50);
moreByValue(1);
}
/**
* Adds the given number of scientists to the project if possible.
* @param change Number of scientists to add.
*/
void ResearchInfoState::moreByValue(int change)
{
if (0 >= change) return;
int freeScientist = _base->getAvailableScientists();
int freeSpaceLab = _base->getFreeLaboratories();
if (freeScientist > 0 && freeSpaceLab > 0)
{
change = std::min(std::min(freeScientist, freeSpaceLab), change);
_project->setAssigned(_project->getAssigned()+change);
_base->setScientists(_base->getScientists()-change);
setAssignedScientist();
}
}
/**
* Removes one scientist from the project if possible.
*/
void ResearchInfoState::less()
{
_timerLess->setInterval(50);
lessByValue(1);
}
/**
* Removes the given number of scientists from the project if possible.
* @param change Number of scientists to subtract.
*/
void ResearchInfoState::lessByValue(int change)
{
if (0 >= change) return;
int assigned = _project->getAssigned();
if (assigned > 0)
{
change = std::min(assigned, change);
_project->setAssigned(assigned-change);
_base->setScientists(_base->getScientists()+change);
setAssignedScientist();
}
}
/**
* Runs state functionality every cycle (used to update the timer).
*/
void ResearchInfoState::think()
{
State::think();
_timerLess->think (this, 0);
_timerMore->think (this, 0);
}
}