add adv. preference for number of menu items displayed at once

See earlier commit: f24f6adee1

Forum discussion: http://forums.wesnoth.org/viewtopic.php?f=6&t=40668
This commit is contained in:
Chris Beck 2014-06-28 22:08:22 -04:00
parent 9e2563a5cf
commit fe3b5f4576
6 changed files with 143 additions and 14 deletions

View file

@ -158,6 +158,17 @@
step=1
[/advanced_preference]
[advanced_preference]
field=max_wml_menu_items
name=_ "Max WML menu items"
description= _ "Maximum number of WML menu items displayed at once"
type=int
default=7
min=3
max=32
step=1
[/advanced_preference]
[advanced_preference]
field=use_twelve_hour_clock_format
name= _ "Use 12-hour clock format"

View file

@ -0,0 +1,98 @@
{GENERIC_UNIT_TEST "test_max_menu_items" (
[event]
name=start
[set_menu_item]
id=bar1
description=foo1
[/set_menu_item]
[set_menu_item]
id=bar2
description=foo2
[/set_menu_item]
[set_menu_item]
id=bar3
description=foo3
[command]
[chat]
message="ASDFSAASDF"
[/chat]
[/command]
[/set_menu_item]
[set_menu_item]
id=bar4
description=foo4
[/set_menu_item]
[set_menu_item]
id=bar5
description=foo5
[/set_menu_item]
[set_menu_item]
id=bar6
description=foo6
[/set_menu_item]
[set_menu_item]
id=bar7
description=foo7
[/set_menu_item]
[set_menu_item]
id=bar8
description=foo8
[/set_menu_item]
[clear_menu_item]
id=bar5
[/clear_menu_item]
[set_menu_item]
id=bar9
description=foo9
[/set_menu_item]
[set_menu_item]
id=bar10
description=foo10
[/set_menu_item]
[set_menu_item]
id=bar12
description=foo12
[/set_menu_item]
[set_menu_item]
id=bar13
description=foo13
[command]
[chat]
message="ASDFSAASDF"
[/chat]
[/command]
[/set_menu_item]
[set_menu_item]
id=bar14
description=foo14
[/set_menu_item]
[set_menu_item]
id=bar15
description=foo15
[/set_menu_item]
[set_menu_item]
id=bar16
description=foo16
[/set_menu_item]
[set_menu_item]
id=bar17
description=foo17
[/set_menu_item]
[set_menu_item]
id=bar18
description=foo18
[/set_menu_item]
[set_menu_item]
id=bar19
description=foo19
[/set_menu_item]
[set_menu_item]
id=bar20
description=foo20
[/set_menu_item]
[set_menu_item]
id=bar21
description=foo21
[/set_menu_item]
[/event]
)}

View file

@ -987,6 +987,16 @@ int chat_message_aging()
return lexical_cast_default<int>(preferences::get("chat_message_aging"), 20);
}
void set_max_wml_menu_items(int max)
{
preferences::set("max_wml_menu_items", max);
}
int max_wml_menu_items()
{
return lexical_cast_default<int>(preferences::get("max_wml_menu_items"), 7);
}
bool show_all_units_in_help() {
return preferences::get("show_all_units_in_help", false);
}

View file

@ -233,6 +233,9 @@ class acquaintance;
int chat_message_aging();
void set_chat_message_aging(const int aging);
int max_wml_menu_items();
void set_max_wml_menu_items(int max);
bool show_all_units_in_help();
void set_show_all_units_in_help(bool value);

View file

@ -19,10 +19,12 @@
#include "config.hpp"
#include "game_events/menu_item.hpp"
#include "game_events/wmi_container.hpp"
#include "game_preferences.hpp"
#include "gettext.hpp"
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <cassert>
#include <iterator> //std::advance
#include <string>
#include <vector>
@ -74,9 +76,15 @@ void wmi_pager::get_items(const map_location& hex,
return;
}
assert(page_size_ > 2u); //if we dont have at least 3 items, we can't display anything...
int page_size_int = preferences::max_wml_menu_items();
if (foo_->size() <= page_size_) { //In this case the first page is sufficient and we don't have to do anything.
assert(page_size_int >= 0 && "max wml menu items cannot be negative, this indicates preferences corruption");
size_t page_size = page_size_int;
assert(page_size > 2u && "if we dont have at least 3 items, we can't display anything on a middle page...");
if (foo_->size() <= page_size) { //In this case the first page is sufficient and we don't have to do anything.
foo_->get_items(hex, items, descriptions);
page_num_ = 0; //reset page num in case there are more items later.
return;
@ -87,9 +95,9 @@ void wmi_pager::get_items(const map_location& hex,
page_num_ = 0;
}
if (page_num_ == 0) { //we are on the first page, so show page_size_-1 items and a next button
if (page_num_ == 0) { //we are on the first page, so show page_size-1 items and a next button
wmi_it end_first_page = foo_->begin();
std::advance(end_first_page, page_size_ - 1);
std::advance(end_first_page, page_size - 1);
foo_->get_items(hex, items, descriptions, foo_->begin(), end_first_page);
add_next_page_item(items, descriptions);
@ -98,31 +106,31 @@ void wmi_pager::get_items(const map_location& hex,
add_prev_page_item(items, descriptions); //this will be necessary since we aren't on the first page
// first page has page_size_ - 1.
// last page has page_size_ - 1.
// all other pages have page_size_ - 2;
// first page has page_size - 1.
// last page has page_size - 1.
// all other pages have page_size - 2;
size_t first_displayed_index = (page_size_ - 2) * page_num_ + 1; //this is the 0-based index of the first item displayed on this page.
size_t first_displayed_index = (page_size - 2) * page_num_ + 1; //this is the 0-based index of the first item displayed on this page.
//alternatively, the number of items displayed on earlier pages
while (first_displayed_index >= foo_->size())
{
page_num_--; //The list must have gotten shorter and our page counter is now off the end, so decrement
first_displayed_index = (page_size_ - 2) * page_num_ + 1; //recalculate
first_displayed_index = (page_size - 2) * page_num_ + 1; //recalculate
}
// ^ This loop terminates with first_displayed_index > 0, because foo_->size() > page_size_ or else we exited earlier, and we only decrease by (page_size_-2) each time.
// ^ This loop terminates with first_displayed_index > 0, because foo_->size() > page_size or else we exited earlier, and we only decrease by (page_size-2) each time.
wmi_it start_range = foo_->begin();
std::advance(start_range, first_displayed_index); // <-- get an iterator to the start of our range. begin() + n doesn't work because map is not random access
//^ = foo_->begin() + first_displayed_index
if (first_displayed_index + page_size_-1 >= foo_->size()) //if this can be the last page, then we won't put next page at the bottom.
if (first_displayed_index + page_size-1 >= foo_->size()) //if this can be the last page, then we won't put next page at the bottom.
{
foo_->get_items(hex, items, descriptions, start_range, foo_->end()); // display all of the remaining items
return;
} else { //we are in a middle page
wmi_it end_range = start_range;
std::advance(end_range, page_size_-2);
std::advance(end_range, page_size-2);
foo_->get_items(hex, items, descriptions, start_range, end_range);
add_next_page_item(items, descriptions);

View file

@ -32,11 +32,10 @@ namespace game_events { class wmi_container; }
class wmi_pager {
private:
int page_num_; //!< Current page number
size_t page_size_; //!< Current size of a page
const game_events::wmi_container * foo_; //!< Internal pointer to the collection of wml menu items
public:
wmi_pager() : page_num_(0), page_size_(7), foo_(NULL) {}
wmi_pager() : page_num_(0), foo_(NULL) {}
void update_ref(game_events::wmi_container * ptr) { foo_ = ptr; } //!< Updates the internal wmi_container pointer