fixed bug where blanks misaligned the item selection process

This commit is contained in:
David Anderson 2006-10-21 21:13:39 +00:00
parent 553a950b98
commit a60e9d205b

View File

@ -37,12 +37,16 @@ CStack<int> g_MenuFreeStack;
void ClearMenus() void ClearMenus()
{ {
for (size_t i = 0; i < g_NewMenus.size(); i++) for (size_t i = 0; i < g_NewMenus.size(); i++)
{
delete g_NewMenus[i]; delete g_NewMenus[i];
}
g_NewMenus.clear(); g_NewMenus.clear();
while (!g_MenuFreeStack.empty()) while (!g_MenuFreeStack.empty())
{
g_MenuFreeStack.pop(); g_MenuFreeStack.pop();
} }
}
void validate_menu_text(char *str) void validate_menu_text(char *str)
{ {
@ -64,13 +68,17 @@ void validate_menu_text(char *str)
} }
} }
if (offs) if (offs)
{
*(str-offs) = *str; *(str-offs) = *str;
}
str++; str++;
} }
if (offs) if (offs)
{
*(str-offs) = '\0'; *(str-offs) = '\0';
} }
} }
}
Menu::Menu(const char *title, int mid, int tid) Menu::Menu(const char *title, int mid, int tid)
{ {
@ -99,7 +107,9 @@ Menu::Menu(const char *title, int mid, int tid)
Menu::~Menu() Menu::~Menu()
{ {
for (size_t i = 0; i < m_Items.size(); i++) for (size_t i = 0; i < m_Items.size(); i++)
{
delete m_Items[i]; delete m_Items[i];
}
unregisterSPForward(this->func); unregisterSPForward(this->func);
@ -139,7 +149,9 @@ size_t Menu::GetPageCount()
{ {
size_t items = GetItemCount(); size_t items = GetItemCount();
if (items_per_page == 0) if (items_per_page == 0)
{
return 1; return 1;
}
return ((items/items_per_page) + ((items % items_per_page) ? 1 : 0)); return ((items/items_per_page) + ((items % items_per_page) ? 1 : 0));
} }
@ -159,15 +171,49 @@ int Menu::PagekeyToItem(page_t page, item_t key)
//first page //first page
if (page == 0) if (page == 0)
{ {
/* The algorithm for spaces here is same as a middle page. */
item_t new_key = key;
for (size_t i=start; i<(start+key-1) && i<m_Items.size(); i++)
{
for (size_t j=0; j<m_Items[i]->blanks.size(); j++)
{
if (m_Items[i]->blanks[j] == 1)
{
if (!new_key)
{
break;
}
new_key--;
}
if (!new_key)
{
break;
}
}
}
key = new_key;
if (key == items_per_page + 1) if (key == items_per_page + 1)
{
return MENU_MORE; return MENU_MORE;
else if (key == items_per_page + 2) } else if (key == items_per_page + 2) {
return MENU_EXIT; return MENU_EXIT;
else } else {
return (start + key - 1); return (start + key - 1);
}
} else if (page == num_pages - 1) { } else if (page == num_pages - 1) {
//last page //last page
size_t remaining = m_Items.size() - start; size_t remaining = m_Items.size() - start;
/* We have to add one remaining for each "bumping" space */
for (size_t i=m_Items.size() - remaining; i<m_Items.size(); i++)
{
for (size_t j=0; j<m_Items[i]->blanks.size(); j++)
{
if (m_Items[i]->blanks[j] == 1)
{
remaining++;
}
}
}
if (key == remaining + 1) if (key == remaining + 1)
{ {
return MENU_BACK; return MENU_BACK;
@ -177,6 +223,29 @@ int Menu::PagekeyToItem(page_t page, item_t key)
return (start + key - 1); return (start + key - 1);
} }
} else { } else {
/* The algorithm for spaces here is a bit harder. We have to subtract
* one from the key for each space we find along the way.
*/
item_t new_key = key;
for (size_t i=start; i<(start+key-1) && i<m_Items.size(); i++)
{
for (size_t j=0; j<m_Items[i]->blanks.size(); j++)
{
if (m_Items[i]->blanks[j] == 1)
{
if (!new_key)
{
break;
}
new_key--;
}
if (!new_key)
{
break;
}
}
}
key = new_key;
if (key > items_per_page && (key-items_per_page<=3)) if (key > items_per_page && (key-items_per_page<=3))
{ {
return m_OptOrders[key-items_per_page-1]; return m_OptOrders[key-items_per_page-1];
@ -286,27 +355,33 @@ const char *Menu::GetTextString(int player, page_t page, int &keys)
{ {
ret = executeForwards(pItem->handler, static_cast<cell>(player), static_cast<cell>(thisId), static_cast<cell>(i)); ret = executeForwards(pItem->handler, static_cast<cell>(player), static_cast<cell>(thisId), static_cast<cell>(i));
if (ret == ITEM_ENABLED) if (ret == ITEM_ENABLED)
{
enabled = true; enabled = true;
else if (ret == ITEM_DISABLED) } else if (ret == ITEM_DISABLED) {
enabled = false; enabled = false;
} }
}
if (pItem->pfn) if (pItem->pfn)
{ {
ret = (pItem->pfn)(player, thisId, i); ret = (pItem->pfn)(player, thisId, i);
if (ret == ITEM_ENABLED) if (ret == ITEM_ENABLED)
{
enabled = true; enabled = true;
else if (ret == ITEM_DISABLED) } else if (ret == ITEM_DISABLED) {
enabled = false; enabled = false;
} }
}
if (enabled) if (enabled)
{ {
keys |= (1<<option); keys |= (1<<option);
if (m_AutoColors) if (m_AutoColors)
{
_snprintf(buffer, sizeof(buffer)-1, "\\r%d.\\w %s\n", ++option, pItem->name.c_str()); _snprintf(buffer, sizeof(buffer)-1, "\\r%d.\\w %s\n", ++option, pItem->name.c_str());
else } else {
_snprintf(buffer, sizeof(buffer)-1, "%d. %s\n", ++option, pItem->name.c_str()); _snprintf(buffer, sizeof(buffer)-1, "%d. %s\n", ++option, pItem->name.c_str());
}
} else { } else {
if (m_AutoColors) if (m_AutoColors)
{ {
@ -326,7 +401,9 @@ const char *Menu::GetTextString(int player, page_t page, int &keys)
for (size_t j=0; j<pItem->blanks.size(); j++) for (size_t j=0; j<pItem->blanks.size(); j++)
{ {
if (pItem->blanks[j] == 1) if (pItem->blanks[j] == 1)
{
option++; option++;
}
m_Text.append("\n"); m_Text.append("\n");
slots++; slots++;
} }
@ -337,11 +414,17 @@ const char *Menu::GetTextString(int player, page_t page, int &keys)
{ {
int pad = items_per_page; int pad = items_per_page;
if (flags & Display_Back) if (flags & Display_Back)
{
pad--; pad--;
}
if (flags & Display_Next) if (flags & Display_Next)
{
pad--; pad--;
}
if (flags & Display_Exit) if (flags & Display_Exit)
{
pad--; pad--;
}
for (int i=slots+1; i<=pad; i++) for (int i=slots+1; i<=pad; i++)
{ {
m_Text.append("\n"); m_Text.append("\n");