Vectorize MsgItems - #7687
Conversation
|
There is an existing behavior where if you input a "comms menu move up" command in the same frame as opening the comms menu, you end up with a |
|
I vote fixing the bug would be good, though you may also want to look at #7688. Possibly they could all be combined into one PR here? |
|
I'd recommend, instead, fixing the bug in #7688 and keeping this PR focused on the vectorization. Then this PR can be rebased on 7688, after merge, and inherit or accommodate the fix. |
|
Ah yeah good point! Though I think merging this one first might work better, and then I can fully test all the cleanup and fixes in 7688 once this is merged :) |
|
@Goober5000 is this one good to merge now or is more review needed? Note, once this is merged I'll address various other fixes in 7688 |
No, there are several things that need to be addressed, but I haven't had time to respond. Will try to follow up ASAP. |
|
Thanks, sounds good! |
Goober5000
left a comment
There was a problem hiding this comment.
It will be necessary to handle config mode, because MsgItems is only valid during a mission. So, in hudsquadmsg.cpp:
- Remove the
active = -1assignment, and enclosing if(), inHudGaugeSquadMessage::render - Then, 22 lines later, add this before the if() and change the if() to use
item_visible:
bool item_visible = config
? !(Hide_main_rearm_items_in_comms_gauge && (i == TYPE_REPAIR_REARM_ITEM || i == TYPE_REPAIR_REARM_ABORT_ITEM))
: (MsgItems[First_menu_item + i].active >= 0);
Goober5000
left a comment
There was a problem hiding this comment.
Additional comments. With these changes, every ternary MsgItems ? sz2i(MsgItems.size()) : -1 can become just sz2i(MsgItems.size()).
| renderString(x + Header_offsets[0], y + fl2i(Header_offsets[1] * scale), title, scale, config); | ||
| } | ||
|
|
||
| int msg_items_size = !Rebuild_MsgItems ? sz2i(MsgItems.size()) : -1; |
There was a problem hiding this comment.
This is still a ternary; it can directly use the size now
| void hud_squadmsg_page_down() | ||
| { | ||
| if ( (First_menu_item + MAX_MENU_DISPLAY) < Num_menu_items ) { | ||
| if ( !Rebuild_MsgItems && (First_menu_item + MAX_MENU_DISPLAY) < sz2i(MsgItems.size()) ) { |
There was a problem hiding this comment.
The Rebuild_MsgItems check is no longer needed here
| "Number of comm orders in the mission. 0 if comm menu is closed") | ||
| { | ||
| return ade_set_args(L, "i", Num_menu_items); | ||
| return ade_set_args(L, "i", (!Rebuild_MsgItems ? sz2i(MsgItems.size()) : -1)); |
There was a problem hiding this comment.
This still uses the ternary
|
|
||
| return ade_set_args(L, "s", MsgItems[current].text.c_str()); | ||
| SCP_string message = "Invalid comm item!"; | ||
| if (!Rebuild_MsgItems && sz2i(MsgItems.size()) > current) { |
There was a problem hiding this comment.
Rebuild_MsgItems is no longer needed, and you can use in_bounds here
| } | ||
|
|
||
| if (MsgItems[current].active > 0) { | ||
| if (!Rebuild_MsgItems && sz2i(MsgItems.size()) > current && MsgItems[current].active > 0) { |
| idx--; | ||
|
|
||
| if ((idx < 0) || idx >= MAX_MENU_ITEMS) | ||
| if ((idx < 0)) |
There was a problem hiding this comment.
double parentheses here
| SCP_string message = "Invalid comm item!"; | ||
| if (!Rebuild_MsgItems && sz2i(MsgItems.size()) > current) { | ||
| message = MsgItems[current].text; | ||
| }; |
|
|
||
| Num_menu_items++; | ||
|
|
||
| } |
There was a problem hiding this comment.
this has a trailing tab
| } | ||
| MsgItems[i].active = 1; // assume active | ||
| MsgItems.push_back({0, 1, lua_cat_list[i - NUM_COMM_ORDER_TYPES]}); // assume active | ||
| } |
|
|
||
| //Fuctions that allow selection of specific comms menu items with simple up/down/select buttons | ||
| void hud_squadmsg_selection_move_down() { | ||
| void hud_squadmsg_selection_move( bool up ) { |
There was a problem hiding this comment.
This function has a bug and a few infelicities, but it turns out you can replace the whole thing with this:
void hud_squadmsg_selection_move( bool up )
{
if (Rebuild_MsgItems || MsgItems.empty())
return;
//Check if comms menu is up
if (Player->flags & PLAYER_FLAGS_MSG_MODE)
{
Display_selector = true;
//play scrolling sound and reset the comms window timeout timer, so the window doesn't disappear while we select our item
gamesnd_play_iface(InterfaceSounds::SCROLL);
Msg_mode_timestamp = _timestamp(DEFAULT_MSG_TIMEOUT);
//The selection moves +/-1 through the whole menu, wrapping around at either end.
//First_menu_item and Selected_menu_item are just the page and the offset within the page.
int num_items = sz2i(MsgItems.size());
int selected_index = First_menu_item + Selected_menu_item;
selected_index = (selected_index + (up ? -1 : 1) + num_items) % num_items;
First_menu_item = (selected_index / MAX_MENU_DISPLAY) * MAX_MENU_DISPLAY;
Selected_menu_item = selected_index % MAX_MENU_DISPLAY;
}
}
Convert MsgItems from an array to a vector, and remove the associated limit value and counter.
MsgItems is dynamically rewritten at runtime, and previously used a counter value of -1 to indicate that the array was in a state suitable for rewriting in some circumstances. To replicate this behavior, MsgItems is now actually an
std::optional<SCP_vector<mmode_item>>, and its nullopt state corresponds to -1.