Skip to content

Commit 6082802

Browse files
committed
Show item info by hovering cursor over item.
Deselect item only if unload actually happened. Close inventory screen by its hotkey.
1 parent 6cc6a5d commit 6082802

4 files changed

Lines changed: 99 additions & 10 deletions

File tree

‎src/Battlescape/Inventory.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,24 @@ void Inventory::setSelectedItem(BattleItem *item)
411411
drawItems();
412412
}
413413

414+
/**
415+
* Returns the item currently under mouse cursor.
416+
* @return Pointer to selected item, or 0 if none.
417+
*/
418+
BattleItem *Inventory::getMouseOverItem() const
419+
{
420+
return _mouseOverItem;
421+
}
422+
423+
/**
424+
* Changes the item currently under mouse cursor.
425+
* @param item Pointer to selected item, or NULL if none.
426+
*/
427+
void Inventory::setMouseOverItem(BattleItem *item)
428+
{
429+
_mouseOverItem = (item && !item->getRules()->isFixed()) ? item : 0;
430+
}
431+
414432
/**
415433
* Handles timers.
416434
*/
@@ -440,6 +458,26 @@ void Inventory::blit(Surface *surface)
440458
*/
441459
void Inventory::mouseOver(Action *action, State *state)
442460
{
461+
if (_selUnit == 0)
462+
return;
463+
464+
int x = (int)floor(action->getAbsoluteXMouse()) - _dx,
465+
y = (int)floor(action->getAbsoluteYMouse()) - _dy;
466+
RuleInventory *slot = getSlotInPosition(&x, &y);
467+
if (slot != 0)
468+
{
469+
if (slot->getType() == INV_GROUND)
470+
{
471+
x += _groundOffset;
472+
}
473+
BattleItem *item = _selUnit->getItem(slot, x, y);
474+
setMouseOverItem(item);
475+
}
476+
else
477+
{
478+
setMouseOverItem(0);
479+
}
480+
443481
_selection->setX((int)floor(action->getAbsoluteXMouse()) - _selection->getWidth()/2 - _dx);
444482
_selection->setY((int)floor(action->getAbsoluteYMouse()) - _selection->getHeight()/2 - _dy);
445483
InteractiveSurface::mouseOver(action, state);

‎src/Battlescape/Inventory.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Inventory : public InteractiveSurface
4545
WarningMessage *_warning;
4646
BattleUnit *_selUnit;
4747
BattleItem *_selItem;
48+
BattleItem *_mouseOverItem;
4849
bool _tu;
4950
int _groundOffset;
5051
std::map<int, std::map<int, int> > _stackLevel;
@@ -77,6 +78,10 @@ class Inventory : public InteractiveSurface
7778
BattleItem *getSelectedItem() const;
7879
/// Sets the currently selected item.
7980
void setSelectedItem(BattleItem *item);
81+
/// Gets the mouse over item.
82+
BattleItem *getMouseOverItem() const;
83+
/// Sets the mouse over item.
84+
void setMouseOverItem(BattleItem *item);
8085
/// Handles timers.
8186
void think();
8287
/// Blits the inventory onto another surface.

‎src/Battlescape/InventoryState.cpp‎

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ InventoryState::InventoryState(Game *game, bool tu, BattlescapeState *parent) :
160160

161161
_btnOk->onMouseClick((ActionHandler)&InventoryState::btnOkClick);
162162
_btnOk->onKeyboardPress((ActionHandler)&InventoryState::btnOkClick, (SDLKey)Options::getInt("keyCancel"));
163+
_btnOk->onKeyboardPress((ActionHandler)&InventoryState::btnOkClick, (SDLKey)Options::getInt("keyBattleInventory"));
163164
_btnPrev->onMouseClick((ActionHandler)&InventoryState::btnPrevClick);
164165
_btnPrev->onKeyboardPress((ActionHandler)&InventoryState::btnPrevClick, (SDLKey)Options::getInt("keyBattlePrevUnit"));
165166
_btnNext->onMouseClick((ActionHandler)&InventoryState::btnNextClick);
@@ -172,6 +173,8 @@ InventoryState::InventoryState(Game *game, bool tu, BattlescapeState *parent) :
172173
_inv->setTuMode(_tu);
173174
_inv->setSelectedUnit(_game->getSavedGame()->getSavedBattle()->getSelectedUnit());
174175
_inv->onMouseClick((ActionHandler)&InventoryState::invClick, 0);
176+
_inv->onMouseOver((ActionHandler)&InventoryState::invMouseOver);
177+
_inv->onMouseOut((ActionHandler)&InventoryState::invMouseOut);
175178
}
176179

177180
/**
@@ -433,11 +436,13 @@ void InventoryState::btnUnloadClick(Action *)
433436
{
434437
if (_inv->getSelectedItem() != 0 && _inv->getSelectedItem()->getAmmoItem() != 0 && _inv->getSelectedItem()->needsAmmo())
435438
{
436-
_inv->unload();
437-
_txtItem->setText(L"");
438-
_txtAmmo->setText(L"");
439-
_selAmmo->clear();
440-
updateStats();
439+
if (_inv->unload())
440+
{
441+
_txtItem->setText(L"");
442+
_txtAmmo->setText(L"");
443+
_selAmmo->clear();
444+
updateStats();
445+
}
441446
}
442447
}
443448

@@ -465,10 +470,21 @@ void InventoryState::btnRankClick(Action *)
465470
*/
466471
void InventoryState::invClick(Action *)
467472
{
468-
BattleItem *item = _inv->getSelectedItem();
469-
_txtItem->setText(L"");
470-
_txtAmmo->setText(L"");
471-
_selAmmo->clear();
473+
updateStats();
474+
}
475+
476+
/**
477+
* Shows item info.
478+
* @param action Pointer to an action.
479+
*/
480+
void InventoryState::invMouseOver(Action *)
481+
{
482+
if (_inv->getSelectedItem() != 0)
483+
{
484+
return;
485+
}
486+
487+
BattleItem *item = _inv->getMouseOverItem();
472488
if (item != 0)
473489
{
474490
if (item->getUnit() && item->getUnit()->getStatus() == STATUS_UNCONSCIOUS)
@@ -506,10 +522,36 @@ void InventoryState::invClick(Action *)
506522
else if (item->getAmmoQuantity() != 0 && item->needsAmmo())
507523
{
508524
s = tr("STR_AMMO_ROUNDS_LEFT").arg(item->getAmmoQuantity());
525+
_selAmmo->clear();
526+
}
527+
else
528+
{
529+
_selAmmo->clear();
509530
}
510531
_txtAmmo->setText(s);
511532
}
512-
updateStats();
533+
else
534+
{
535+
_txtItem->setText(L"");
536+
_txtAmmo->setText(L"");
537+
_selAmmo->clear();
538+
}
539+
}
540+
541+
/**
542+
* Hides item info.
543+
* @param action Pointer to an action.
544+
*/
545+
void InventoryState::invMouseOut(Action *)
546+
{
547+
if (_inv->getSelectedItem() != 0)
548+
{
549+
return;
550+
}
551+
552+
_txtItem->setText(L"");
553+
_txtAmmo->setText(L"");
554+
_selAmmo->clear();
513555
}
514556

515557
/**

‎src/Battlescape/InventoryState.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ class InventoryState : public State
7171
void btnRankClick(Action *action);
7272
/// Handler for clicking the Unload button.
7373
void invClick(Action *action);
74+
/// Handler for showing item info.
75+
void invMouseOver(Action *action);
76+
/// Handler for hiding item info.
77+
void invMouseOut(Action *action);
7478
/// Handles keypresses.
7579
void handle(Action *action);
7680
};

0 commit comments

Comments
 (0)