Skip to content

Commit

Permalink
MenuActionBar: Don't use tablist index when slot index is needed
Browse files Browse the repository at this point in the history
This fixes a crash when the number of defined slots is less than the
maximum (12).
  • Loading branch information
dorkster committed Jan 25, 2025
1 parent 5388a32 commit 4904a10
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 41 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Engine fixes:
* Added validation to player XP table parsing to ensure that XP thresholds are not less than those for previous levels.
* Errors in saved fog-of-war data no longer causes a forced exit & mod reset.
* Primary stat upgrade buttons are now hidden in the Character menu if the cooresponding label is hidden.
* Fixed crash in action bar when less than 12 slots were defined.
* Android: Fix 'Flare' directory not being automatically created.
* Android: Added a dialog to direct the player to the wiki page for installing if no game data is found.

Expand Down
28 changes: 23 additions & 5 deletions src/MenuActionBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,13 +856,13 @@ void MenuActionBar::addPower(const PowerID id, const PowerID target_id) {
}
}

Point MenuActionBar::getSlotPos(int slot) {
if (static_cast<unsigned>(slot) < slots.size()) {
Point MenuActionBar::getSlotPos(size_t slot) {
if (slot < slots.size()) {
return Point(slots[slot]->pos.x, slots[slot]->pos.y);
}
else if (static_cast<unsigned>(slot) < slots.size() + MENU_COUNT) {
int slot_size = static_cast<int>(slots.size());
return Point(menus[slot - slot_size]->pos.x, menus[slot - slot_size]->pos.y);
else if (slot < slots.size() + MENU_COUNT) {
size_t menu_index = slot - slots.size();
return Point(menus[menu_index]->pos.x, menus[menu_index]->pos.y);
}
return Point();
}
Expand All @@ -875,6 +875,24 @@ WidgetSlot* MenuActionBar::getSlotFromPosition(const Point& position) {
return NULL;
}

size_t MenuActionBar::getCurrentSlotIndexFromTablist() {
int tablist_index = static_cast<size_t>(getCurrentTabList()->getCurrent());
Widget* current_slot = getCurrentTabList()->getWidgetByIndex(tablist_index);
if (!current_slot)
return slots.size() + MENU_COUNT;

for (size_t i = 0; i < slots.size(); ++i) {
if (slots[i] == current_slot)
return i;
}
for (size_t i = 0; i < MENU_COUNT; ++i) {
if (menus[i] == current_slot)
return i + slots.size();
}

return slots.size() + MENU_COUNT;
}

MenuActionBar::~MenuActionBar() {

menu_act = NULL;
Expand Down
3 changes: 2 additions & 1 deletion src/MenuActionBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,15 @@ class MenuActionBar : public Menu {
void set(std::vector<PowerID> power_id, bool skip_empty);
void clearSlot(size_t slot);
void clear(bool skip_items);
Point getSlotPos(int slot);
Point getSlotPos(size_t slot);

void renderTooltips(const Point& position);
bool isWithinSlots(const Point& mouse);
bool isWithinMenus(const Point& mouse);
void addPower(const PowerID id, const PowerID target_id);

WidgetSlot* getSlotFromPosition(const Point& position);
size_t getCurrentSlotIndexFromTablist();

unsigned slots_count;
std::vector<PowerID> hotkeys; // refer to power_index in PowerManager
Expand Down
70 changes: 36 additions & 34 deletions src/MenuManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,8 +1059,7 @@ void MenuManager::logic() {
WidgetSlot* act_slot = act->getSlotFromPosition(inpt->mouse);
if (act_slot) {
act->tablist.setCurrent(act_slot);
int slot_index = act->tablist.getCurrent();
keydrag_pos = act->getSlotPos(slot_index);
keydrag_pos = Point(act_slot->pos.x, act_slot->pos.y);
}
showActionPicker(act, inpt->mouse);
}
Expand Down Expand Up @@ -1370,33 +1369,35 @@ void MenuManager::dragAndDropWithKeyboard() {

// actionbar
if (act->getCurrentTabList() && static_cast<unsigned>(act->getCurrentTabList()->getCurrent()) < act->slots.size()) {
int slot_index = act->getCurrentTabList()->getCurrent();
WidgetSlot::CLICK_TYPE slotClick = act->slots[slot_index]->checkClick();
Point dest_slot = act->getSlotPos(slot_index);

// pick up power
if (slotClick == WidgetSlot::DRAG && drag_stack.empty() && drag_power == 0) {
showActionPicker(act, dest_slot);
}
// drop power/item from other menu
else if (slotClick == WidgetSlot::DRAG && drag_src != DRAG_SRC_ACTIONBAR && (!drag_stack.empty() || drag_power > 0)) {
if (drag_src == DRAG_SRC_POWERS) {
act->drop(dest_slot, drag_power, !MenuActionBar::REORDER);
}
else if (drag_src == DRAG_SRC_INVENTORY) {
if (items->isValid(drag_stack.item) && items->items[drag_stack.item]->power != 0) {
act->drop(dest_slot, items->items[drag_stack.item]->power, !MenuActionBar::REORDER);
size_t slot_index = act->getCurrentSlotIndexFromTablist();
if (slot_index < act->slots.size() + MenuActionBar::MENU_COUNT && act->slots[slot_index]) {
WidgetSlot::CLICK_TYPE slotClick = act->slots[slot_index]->checkClick();
Point dest_slot = act->getSlotPos(slot_index);

// pick up power
if (slotClick == WidgetSlot::DRAG && drag_stack.empty() && drag_power == 0) {
showActionPicker(act, dest_slot);
}
// drop power/item from other menu
else if (slotClick == WidgetSlot::DRAG && drag_src != DRAG_SRC_ACTIONBAR && (!drag_stack.empty() || drag_power > 0)) {
if (drag_src == DRAG_SRC_POWERS) {
act->drop(dest_slot, drag_power, !MenuActionBar::REORDER);
}
else if (drag_src == DRAG_SRC_INVENTORY) {
if (items->isValid(drag_stack.item) && items->items[drag_stack.item]->power != 0) {
act->drop(dest_slot, items->items[drag_stack.item]->power, !MenuActionBar::REORDER);
}
}
resetDrag();
inv->applyEquipment();
}
// rearrange actionbar
else if (slotClick == WidgetSlot::DRAG && drag_src == DRAG_SRC_ACTIONBAR && drag_power > 0) {
act->drop(dest_slot, drag_power, MenuActionBar::REORDER);
drag_src = DRAG_SRC_NONE;
drag_power = 0;
keyboard_dragging = false;
}
resetDrag();
inv->applyEquipment();
}
// rearrange actionbar
else if (slotClick == WidgetSlot::DRAG && drag_src == DRAG_SRC_ACTIONBAR && drag_power > 0) {
act->drop(dest_slot, drag_power, MenuActionBar::REORDER);
drag_src = DRAG_SRC_NONE;
drag_power = 0;
keyboard_dragging = false;
}
}
}
Expand Down Expand Up @@ -1624,15 +1625,16 @@ void MenuManager::handleKeyboardTooltips() {
}

if (act->getCurrentTabList()) {
int slot_index = act->getCurrentTabList()->getCurrent();

keydrag_pos = act->getSlotPos(slot_index);
size_t slot_index = act->getCurrentSlotIndexFromTablist();
if (slot_index < act->slots.size() + MenuActionBar::MENU_COUNT) {
keydrag_pos = act->getSlotPos(slot_index);

Point tooltip_pos = keydrag_pos;
tooltip_pos.x += eset->resolutions.icon_size / 2;
tooltip_pos.y += eset->resolutions.icon_size / 2;
Point tooltip_pos = keydrag_pos;
tooltip_pos.x += eset->resolutions.icon_size / 2;
tooltip_pos.y += eset->resolutions.icon_size / 2;

act->renderTooltips(tooltip_pos);
act->renderTooltips(tooltip_pos);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ FLARE. If not, see http://www.gnu.org/licenses/

#include <SDL.h>

Version VersionInfo::ENGINE(1, 14, 97);
Version VersionInfo::ENGINE(1, 14, 98);
Version VersionInfo::MIN(0, 0, 0);
Version VersionInfo::MAX(USHRT_MAX, USHRT_MAX, USHRT_MAX);

Expand Down

0 comments on commit 4904a10

Please sign in to comment.