Skip to content

Commit 3b1f85a

Browse files
committed
- Added Arrow-Buttons Max-Click feature
1 parent 937a95b commit 3b1f85a

13 files changed

Lines changed: 543 additions & 38 deletions

src/Basescape/CraftEquipmentState.cpp

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "CraftEquipmentState.h"
2020
#include <sstream>
2121
#include <algorithm>
22+
#include "../Engine/Action.h"
2223
#include "../Engine/Game.h"
2324
#include "../Engine/Timer.h"
2425
#include "../Resource/ResourcePack.h"
@@ -115,8 +116,10 @@ CraftEquipmentState::CraftEquipmentState(Game *game, Base *base, size_t craft) :
115116
_lstEquipment->setMargin(8);
116117
_lstEquipment->onLeftArrowPress((ActionHandler)&CraftEquipmentState::lstEquipmentLeftArrowPress);
117118
_lstEquipment->onLeftArrowRelease((ActionHandler)&CraftEquipmentState::lstEquipmentLeftArrowRelease);
119+
_lstEquipment->onLeftArrowClick((ActionHandler)&CraftEquipmentState::lstEquipmentLeftArrowClick);
118120
_lstEquipment->onRightArrowPress((ActionHandler)&CraftEquipmentState::lstEquipmentRightArrowPress);
119121
_lstEquipment->onRightArrowRelease((ActionHandler)&CraftEquipmentState::lstEquipmentRightArrowRelease);
122+
_lstEquipment->onRightArrowClick((ActionHandler)&CraftEquipmentState::lstEquipmentRightArrowClick);
120123

121124
int row = 0;
122125
std::vector<std::string> items = _game->getRuleset()->getItemsList();
@@ -217,7 +220,7 @@ void CraftEquipmentState::btnOkClick(Action *action)
217220
void CraftEquipmentState::lstEquipmentLeftArrowPress(Action *action)
218221
{
219222
_sel = _lstEquipment->getSelectedRow();
220-
_timerLeft->start();
223+
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerLeft->start();
221224
}
222225

223226
/**
@@ -226,7 +229,55 @@ void CraftEquipmentState::lstEquipmentLeftArrowPress(Action *action)
226229
*/
227230
void CraftEquipmentState::lstEquipmentLeftArrowRelease(Action *action)
228231
{
229-
_timerLeft->stop();
232+
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerLeft->stop();
233+
}
234+
235+
/**
236+
* Moves all the items to the base on right-click.
237+
* @param action Pointer to an action.
238+
*/
239+
void CraftEquipmentState::lstEquipmentLeftArrowClick(Action *action)
240+
{
241+
if (action->getDetails()->button.button == SDL_BUTTON_RIGHT)
242+
{
243+
Craft *c = _base->getCrafts()->at(_craft);
244+
RuleItem *item = _game->getRuleset()->getItem(_items[_sel]);
245+
int cQty = 0;
246+
if (item->isFixed())
247+
{
248+
cQty = c->getVehicleCount(_items[_sel]);
249+
if (cQty > 0)
250+
{
251+
while (cQty > 0)
252+
{
253+
RuleItem *ammo = _game->getRuleset()->getItem(item->getCompatibleAmmo()->front());
254+
for (std::vector<Vehicle*>::iterator i = c->getVehicles()->begin(); i != c->getVehicles()->end(); ++i)
255+
{
256+
if ((*i)->getRules() == item)
257+
{
258+
_base->getItems()->addItem(ammo->getType(), (*i)->getAmmo());
259+
delete (*i);
260+
c->getVehicles()->erase(i);
261+
break;
262+
}
263+
}
264+
_base->getItems()->addItem(_items[_sel]);
265+
cQty = c->getVehicleCount(_items[_sel]);
266+
}
267+
updateQuantity();
268+
}
269+
}
270+
else
271+
{
272+
cQty = c->getItems()->getItem(_items[_sel]);
273+
if (cQty > 0)
274+
{
275+
_base->getItems()->addItem(_items[_sel], cQty);
276+
c->getItems()->removeItem(_items[_sel], cQty);
277+
updateQuantity();
278+
}
279+
}
280+
}
230281
}
231282

232283
/**
@@ -236,7 +287,7 @@ void CraftEquipmentState::lstEquipmentLeftArrowRelease(Action *action)
236287
void CraftEquipmentState::lstEquipmentRightArrowPress(Action *action)
237288
{
238289
_sel = _lstEquipment->getSelectedRow();
239-
_timerRight->start();
290+
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerRight->start();
240291
}
241292

242293
/**
@@ -245,7 +296,57 @@ void CraftEquipmentState::lstEquipmentRightArrowPress(Action *action)
245296
*/
246297
void CraftEquipmentState::lstEquipmentRightArrowRelease(Action *action)
247298
{
248-
_timerRight->stop();
299+
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerRight->stop();
300+
}
301+
302+
/**
303+
* Moves all the items (as much as possible) to the craft on right-click.
304+
* @param action Pointer to an action.
305+
*/
306+
void CraftEquipmentState::lstEquipmentRightArrowClick(Action *action)
307+
{
308+
if (action->getDetails()->button.button == SDL_BUTTON_RIGHT)
309+
{
310+
Craft *c = _base->getCrafts()->at(_craft);
311+
RuleItem *item = _game->getRuleset()->getItem(_items[_sel]);
312+
int bqty = _base->getItems()->getItem(_items[_sel]);
313+
if (bqty > 0)
314+
{
315+
// Do we need to convert item to vehicle?
316+
if (item->isFixed())
317+
{
318+
// Check if there's enough room
319+
int room = std::min(c->getRules()->getVehicles() - c->getNumVehicles(), c->getSpaceAvailable() / 4);
320+
if (room > 0)
321+
{
322+
RuleItem *ammo = _game->getRuleset()->getItem(item->getCompatibleAmmo()->front());
323+
int baqty = _base->getItems()->getItem(ammo->getType());
324+
int vehiclesCount = std::min(std::min(bqty, room), baqty);
325+
if (vehiclesCount > 0)
326+
{
327+
int newAmmoPerVehicle = std::min(baqty / vehiclesCount, ammo->getClipSize());;
328+
int remainder = baqty - (vehiclesCount * newAmmoPerVehicle);
329+
if (ammo->getClipSize() == newAmmoPerVehicle) remainder = 0;
330+
int newAmmo;
331+
for (int i=0; i < vehiclesCount; ++i)
332+
{
333+
newAmmo = newAmmoPerVehicle;
334+
if (i<remainder) ++newAmmo;
335+
c->getVehicles()->push_back(new Vehicle(item, newAmmo));
336+
_base->getItems()->removeItem(ammo->getType(), newAmmo);
337+
_base->getItems()->removeItem(_items[_sel]);
338+
}
339+
}
340+
}
341+
}
342+
else
343+
{
344+
_base->getItems()->removeItem(_items[_sel],bqty);
345+
c->getItems()->addItem(_items[_sel],bqty);
346+
}
347+
updateQuantity();
348+
}
349+
}
249350
}
250351

251352
/**

src/Basescape/CraftEquipmentState.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ class CraftEquipmentState : public State
6464
void lstEquipmentLeftArrowPress(Action *action);
6565
/// Handler for releasing a Move Left arrow in the list.
6666
void lstEquipmentLeftArrowRelease(Action *action);
67+
/// Handler for clicking a Move Left arrow in the list.
68+
void lstEquipmentLeftArrowClick(Action *action);
6769
/// Handler for pressing a Move Right arrow in the list.
6870
void lstEquipmentRightArrowPress(Action *action);
6971
/// Handler for releasing a Move Right arrow in the list.
7072
void lstEquipmentRightArrowRelease(Action *action);
73+
/// Handler for clicking a Move Right arrow in the list.
74+
void lstEquipmentRightArrowClick(Action *action);
7175
/// Moves an item to the base.
7276
void moveLeft();
7377
/// Moves an item to the craft.

src/Basescape/ManufactureInfoState.cpp

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "../Interface/Text.h"
2323
#include "../Interface/TextList.h"
2424
#include "../Interface/ArrowButton.h"
25+
#include "../Engine/Action.h"
2526
#include "../Engine/Game.h"
2627
#include "../Engine/Language.h"
2728
#include "../Engine/Palette.h"
@@ -145,18 +146,22 @@ void ManufactureInfoState::buildUi()
145146
_btnEngineerUp->setColor(Palette::blockOffset(15)+1);
146147
_btnEngineerUp->onMousePress((ActionHandler)&ManufactureInfoState::moreEngineerPress);
147148
_btnEngineerUp->onMouseRelease((ActionHandler)&ManufactureInfoState::moreEngineerRelease);
149+
_btnEngineerUp->onMouseClick((ActionHandler)&ManufactureInfoState::moreEngineerClick, 0);
148150

149151
_btnEngineerDown->setColor(Palette::blockOffset(15)+1);
150152
_btnEngineerDown->onMousePress((ActionHandler)&ManufactureInfoState::lessEngineerPress);
151153
_btnEngineerDown->onMouseRelease((ActionHandler)&ManufactureInfoState::lessEngineerRelease);
154+
_btnEngineerDown->onMouseClick((ActionHandler)&ManufactureInfoState::lessEngineerClick, 0);
152155

153156
_btnUnitUp->setColor(Palette::blockOffset(15)+1);
154157
_btnUnitUp->onMousePress((ActionHandler)&ManufactureInfoState::moreUnitPress);
155158
_btnUnitUp->onMouseRelease((ActionHandler)&ManufactureInfoState::moreUnitRelease);
159+
_btnUnitUp->onMouseClick((ActionHandler)&ManufactureInfoState::moreUnitClick, 0);
156160

157161
_btnUnitDown->setColor(Palette::blockOffset(15)+1);
158162
_btnUnitDown->onMousePress((ActionHandler)&ManufactureInfoState::lessUnitPress);
159163
_btnUnitDown->onMouseRelease((ActionHandler)&ManufactureInfoState::lessUnitRelease);
164+
_btnUnitDown->onMouseClick((ActionHandler)&ManufactureInfoState::lessUnitClick, 0);
160165

161166
_txtUnitUp->setColor(Palette::blockOffset(15)+1);
162167
_txtUnitUp->setText(_game->getLanguage()->getString("STR_INCREASE_UC"));
@@ -249,7 +254,7 @@ void ManufactureInfoState::setAssignedEngineer()
249254
*/
250255
void ManufactureInfoState::moreEngineerPress(Action * action)
251256
{
252-
_timerMoreEngineer->start();
257+
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerMoreEngineer->start();
253258
}
254259

255260
/**
@@ -258,7 +263,28 @@ void ManufactureInfoState::moreEngineerPress(Action * action)
258263
*/
259264
void ManufactureInfoState::moreEngineerRelease(Action * action)
260265
{
261-
_timerMoreEngineer->stop();
266+
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerMoreEngineer->stop();
267+
}
268+
269+
/**
270+
* Allocate all engineers
271+
* @param action a pointer to an Action
272+
*/
273+
void ManufactureInfoState::moreEngineerClick(Action * action)
274+
{
275+
if (action->getDetails()->button.button == SDL_BUTTON_RIGHT)
276+
{
277+
int assigned = _production->getAssignedEngineers();
278+
int availableEngineer = _base->getAvailableEngineers();
279+
int availableWorkSpace = _base->getFreeWorkshops();
280+
if (availableEngineer > 0 && availableWorkSpace > 0)
281+
{
282+
int change=std::min(availableEngineer, availableWorkSpace);
283+
_production->setAssignedEngineers(assigned+=change);
284+
_base->setEngineers(_base->getEngineers()-change);
285+
setAssignedEngineer();
286+
}
287+
}
262288
}
263289

264290
/**
@@ -267,7 +293,7 @@ void ManufactureInfoState::moreEngineerRelease(Action * action)
267293
*/
268294
void ManufactureInfoState::lessEngineerPress(Action * action)
269295
{
270-
_timerLessEngineer->start();
296+
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerLessEngineer->start();
271297
}
272298

273299
/**
@@ -276,7 +302,25 @@ void ManufactureInfoState::lessEngineerPress(Action * action)
276302
*/
277303
void ManufactureInfoState::lessEngineerRelease(Action * action)
278304
{
279-
_timerLessEngineer->stop();
305+
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerLessEngineer->stop();
306+
}
307+
308+
/**
309+
* Allocate 0 engineers
310+
* @param action a pointer to an Action
311+
*/
312+
void ManufactureInfoState::lessEngineerClick(Action * action)
313+
{
314+
if (action->getDetails()->button.button == SDL_BUTTON_RIGHT)
315+
{
316+
int assigned = _production->getAssignedEngineers();
317+
if (assigned > 0)
318+
{
319+
_production->setAssignedEngineers(0);
320+
_base->setEngineers(_base->getEngineers()+assigned);
321+
setAssignedEngineer();
322+
}
323+
}
280324
}
281325

282326
/**
@@ -285,7 +329,7 @@ void ManufactureInfoState::lessEngineerRelease(Action * action)
285329
*/
286330
void ManufactureInfoState::moreUnitPress(Action * action)
287331
{
288-
_timerMoreUnit->start();
332+
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerMoreUnit->start();
289333
}
290334

291335
/**
@@ -294,7 +338,36 @@ void ManufactureInfoState::moreUnitPress(Action * action)
294338
*/
295339
void ManufactureInfoState::moreUnitRelease(Action * action)
296340
{
297-
_timerMoreUnit->stop();
341+
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerMoreUnit->stop();
342+
}
343+
344+
/**
345+
* This could set a virtual infinite value for units
346+
* which could mean produce & sell automatically just like in UFOextender. :)
347+
* Perhaps someone will implement that feature later.
348+
* @param action a pointer to an Action
349+
*/
350+
void ManufactureInfoState::moreUnitClick(Action * action)
351+
{
352+
if (action->getDetails()->button.button == SDL_BUTTON_RIGHT)
353+
{
354+
// TODO: virtual infinite value for produce & sell automatically
355+
// now it just raises the value to 999 or max-available-hangars on crafts
356+
int more = _production->getAmountRemaining ();
357+
if (_production->getRules()->getCategory() == "STR_CRAFT")
358+
{
359+
if (_base->getAvailableHangars() - _base->getUsedHangars() > 0)
360+
{
361+
_production->setAmountRemaining(std::max(more,_base->getAvailableHangars() - _base->getUsedHangars()));
362+
setAssignedEngineer();
363+
}
364+
}
365+
else
366+
{
367+
_production->setAmountRemaining(std::max(more,999));
368+
setAssignedEngineer();
369+
}
370+
}
298371
}
299372

300373
/**
@@ -303,7 +376,7 @@ void ManufactureInfoState::moreUnitRelease(Action * action)
303376
*/
304377
void ManufactureInfoState::lessUnitPress(Action * action)
305378
{
306-
_timerLessUnit->start();
379+
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerLessUnit->start();
307380
}
308381

309382
/**
@@ -312,7 +385,24 @@ void ManufactureInfoState::lessUnitPress(Action * action)
312385
*/
313386
void ManufactureInfoState::lessUnitRelease(Action * action)
314387
{
315-
_timerLessUnit->stop();
388+
if (action->getDetails()->button.button == SDL_BUTTON_LEFT) _timerLessUnit->stop();
389+
}
390+
391+
/**
392+
* Build no more unit
393+
* @param action a pointer to an Action
394+
*/
395+
void ManufactureInfoState::lessUnitClick(Action * action)
396+
{
397+
if (action->getDetails()->button.button == SDL_BUTTON_RIGHT)
398+
{
399+
int less = _production->getAmountRemaining ();
400+
if (less > (_production->getAmountProduced () + 1))
401+
{
402+
_production->setAmountRemaining(_production->getAmountProduced () + 1);
403+
setAssignedEngineer();
404+
}
405+
}
316406
}
317407

318408
/**

src/Basescape/ManufactureInfoState.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,26 @@ class ManufactureInfoState : public State
5454
void moreEngineerPress(Action * action);
5555
/// Handler for releasing the more engineer button
5656
void moreEngineerRelease(Action * action);
57+
/// Handler for clicking the more engineer button
58+
void moreEngineerClick(Action * action);
5759
/// Handler for pressing the more unit button
5860
void moreUnitPress(Action * action);
5961
/// Handler for releasing the more unit button
6062
void moreUnitRelease(Action * action);
63+
/// Handler for clicking the more unit button
64+
void moreUnitClick(Action * action);
6165
/// Handler for pressing the less engineer button
6266
void lessEngineerPress(Action * action);
6367
/// Handler for releasing the less engineer button
6468
void lessEngineerRelease(Action * action);
69+
/// Handler for clicking the less engineer button
70+
void lessEngineerClick(Action * action);
6571
/// Handler for pressing the less unit button
6672
void lessUnitPress(Action * action);
6773
/// Handler for releasing the less unit button
6874
void lessUnitRelease(Action * action);
75+
/// Handler for clicking the less unit button
76+
void lessUnitClick(Action * action);
6977
/// Add one engineer to production (if possible)
7078
void onMoreEngineer();
7179
/// Remove one engineer to production (if possible)

0 commit comments

Comments
 (0)