Finally have inventory not crashing for most edge cases. This solves many bugs but mostly closes #58.

This commit is contained in:
Zed A. Shaw 2025-07-07 13:25:17 -04:00
parent 601f3331ed
commit f64b202ee7
9 changed files with 90 additions and 43 deletions

View file

@ -61,11 +61,13 @@ namespace gui {
case LOOT_OPEN:
END(CLOSE);
break;
case LOOT_SELECT:
if(commit_move($loot_ui.$gui, $grab_source, data)) {
state(DNDState::LOOTING);
}
break;
case LOOT_SELECT: {
auto drop_id = std::any_cast<guecs::Entity>(data);
if(move_or_swap($loot_ui, drop_id)) {
state(DNDState::LOOTING);
}
} break;
case INV_SELECT:
if(commit_drop($loot_ui.$gui,
$status_ui.$gui, $grab_source, data))
@ -92,11 +94,12 @@ namespace gui {
state(DNDState::LOOTING);
}
break;
case INV_SELECT:
if(commit_move($status_ui.$gui, $grab_source, data)) {
state(DNDState::LOOTING);
}
break;
case INV_SELECT: {
auto drop_id = std::any_cast<guecs::Entity>(data);
if(move_or_swap($status_ui, drop_id)) {
state(DNDState::LOOTING);
}
} break;
default:
handle_mouse(ev, $status_ui.$gui);
}
@ -114,15 +117,10 @@ namespace gui {
} break;
case INV_SELECT: {
auto drop_id = std::any_cast<guecs::Entity>(data);
if($status_ui.occupied(drop_id)) {
$status_ui.swap(*$grab_source, drop_id);
END(CLOSE);
} else if(commit_move($status_ui.$gui, $grab_source, data)) {
if(move_or_swap($status_ui, drop_id)) {
END(CLOSE);
}
} break;
break;
} break;
default:
handle_mouse(ev, $status_ui.$gui);
}
@ -252,13 +250,12 @@ namespace gui {
}
}
bool DNDLoot::commit_move(guecs::UI& gui, std::optional<guecs::Entity> source_id, std::any data) {
bool DNDLoot::commit_move(guecs::UI& gui, std::optional<guecs::Entity> source_id, guecs::Entity drop_id) {
dbc::check(source_id != std::nullopt, "source_id must exist");
auto& grab = gui.get<guecs::GrabSource>(*source_id);
grab.commit();
auto drop_id = std::any_cast<guecs::Entity>(data);
auto& drop = gui.get<guecs::DropTarget>(drop_id);
if(drop.commit(grab.world_entity)) {
@ -307,4 +304,30 @@ namespace gui {
clear_grab();
return result;
}
/*
* If I refactored everything to use a levelmanager module then
* this and many other things could go away. Access to $level is
* making this too complicated. Do this for now, but fix bug #59.
*/
bool DNDLoot::move_or_swap(StatusUI& ui, guecs::Entity drop_id) {
if(ui.occupied(drop_id)) {
ui.swap(*$grab_source, drop_id);
clear_grab();
return true;
} else {
return commit_move(ui.$gui, $grab_source, drop_id);
}
}
bool DNDLoot::move_or_swap(LootUI& ui, guecs::Entity drop_id) {
if(ui.occupied(drop_id)) {
ui.swap(*$grab_source, drop_id);
clear_grab();
return true;
} else {
return commit_move(ui.$gui, $grab_source, drop_id);
}
}
}