tpreferences: added support for opening preferences to specific main/tab pages

This commit is contained in:
Charles Dang 2016-03-04 00:38:39 +11:00
parent 02597f0230
commit 3d0f1364fb
6 changed files with 164 additions and 94 deletions

View file

@ -15,35 +15,57 @@
[/column]
#enddef
#define _GUI_TAB_BAR
[toggle_panel]
linked_group = "tabs"
[grid]
#define _GUI_PREFS_TAB_BAR
[horizontal_listbox]
id = "tab_selector"
[list_definition]
[row]
{_GUI_INFO_TAB_PADDING}
[column]
grow_factor = 1
border = all
border_size = 5
[toggle_panel]
linked_group = "tabs"
[label]
id = "tab_label"
wrap = true
[/label]
[grid]
[row]
{_GUI_INFO_TAB_PADDING}
[column]
grow_factor = 1
border = all
border_size = 5
[label]
id = "tab_label"
wrap = true
[/label]
[/column]
{_GUI_INFO_TAB_PADDING}
[/row]
[/grid]
[/toggle_panel]
[/column]
{_GUI_INFO_TAB_PADDING}
[/row]
[/list_definition]
[/grid]
[/horizontal_listbox]
#enddef
[/toggle_panel]
#define _GUI_PREFS_TAB_PAGER _CONTENTS
[stacked_widget]
id = "tab_pager"
definition = "default"
[stack]
{_CONTENTS}
[/stack]
[/stacked_widget]
#enddef
#define _GUI_PREFERENCES_MAIN_COMPOSITE_SLIDER _LABEL_ID _LABEL_TEXT _SLIDER_ID _SLIDER_ATTRIBUTES
@ -314,4 +336,4 @@
#undef _GUI_PREFS_PAGE
#undef _GUI_PREFERENCES_MAIN_COMPOSITE_SLIDER
#undef _GUI_INFO_TAB_PADDING
#undef _GUI_TAB_BAR
#undef _GUI_PREFS_TAB_BAR

View file

@ -403,17 +403,7 @@
border_size = 5
horizontal_alignment = "left"
[horizontal_listbox]
id = "mp_tab"
[list_definition]
[row]
[column]
{_GUI_TAB_BAR}
[/column]
[/row]
[/list_definition]
[/horizontal_listbox]
{_GUI_PREFS_TAB_BAR}
[/column]
[/row]
#enddef
@ -425,48 +415,45 @@
horizontal_grow = true
vertical_grow = true
[stacked_widget]
id = "mp_tab_pager"
definition = "default"
[stack]
[layer]
[row]
[column]
horizontal_grow = "true"
vertical_alignment = "top"
{_GUI_PREFS_TAB_PAGER (
[layer]
[row]
[column]
horizontal_grow = "true"
vertical_alignment = "top"
[grid]
{_GUI_PREFERENCES_MP_PAGE_1_GRID_1}
[/grid]
[/column]
[/row]
[grid]
{_GUI_PREFERENCES_MP_PAGE_1_GRID_1}
[/grid]
[/column]
[/row]
[row]
[column]
horizontal_alignment = "left"
vertical_alignment = "bottom"
[row]
[column]
horizontal_alignment = "left"
vertical_alignment = "bottom"
[grid]
{_GUI_PREFERENCES_MP_PAGE_1_GRID_2}
[/grid]
[/column]
[/row]
[/layer]
[grid]
{_GUI_PREFERENCES_MP_PAGE_1_GRID_2}
[/grid]
[/column]
[/row]
[/layer]
[layer]
[row]
[column]
horizontal_grow = "true"
vertical_alignment = "top"
[layer]
[row]
[column]
horizontal_grow = "true"
vertical_alignment = "top"
[grid]
{_GUI_PREFERENCES_MP_PAGE_2}
[/grid]
[/column]
[/row]
[/layer]
)}
[grid]
{_GUI_PREFERENCES_MP_PAGE_2}
[/grid]
[/column]
[/row]
[/layer]
[/stack]
[/stacked_widget]
[/column]
[/row]

View file

@ -29,9 +29,21 @@
namespace preferences {
void show_preferences_dialog(CVideo& video, const config& game_cfg)
void show_preferences_dialog(CVideo& video, const config& game_cfg, const DIALOG_OPEN_TO initial_view)
{
gui2::tpreferences::display(video, game_cfg);
gui2::tpreferences dlg(video, game_cfg);
switch (initial_view) {
case VIEW_DEFAULT:
// Default value (0,0) already set in tpreferences
break;
case VIEW_FRIENDS: {
dlg.set_selected_index(std::make_pair(4, 1));
break;
}
}
dlg.show(video);
}
bool show_theme_dialog(CVideo& video)

View file

@ -119,6 +119,7 @@ tpreferences::tpreferences(CVideo& video, const config& game_cfg)
, accl_speeds_()
, visible_hotkeys_()
, font_scaling_(font_scaling())
, index_(0,0)
{
BOOST_FOREACH(const config& adv, game_cfg.child_range("advanced_preference")) {
adv_preferences_cfg_.push_back(adv);
@ -1073,19 +1074,33 @@ void tpreferences::add_tab(tlistbox& tab_bar, const std::string& label)
tab_bar.add_row(data);
}
void tpreferences::initialize_tabs(twindow& window)
void tpreferences::initialize_tabs(twindow& /*window*/, tlistbox& selector, const int index)
{
//
// MULTIPLAYER TABS
//
tlistbox& tabs_multiplayer = find_widget<tlistbox>(&window, "mp_tab", false);
add_tab(tabs_multiplayer, _("Prefs tab^General"));
add_tab(tabs_multiplayer, _("Prefs tab^Friends"));
//add_tab(tabs_multiplayer, _("Prefs tab^Alerts"));
if(index == 4) {
add_tab(selector, _("Prefs tab^General"));
add_tab(selector, _("Prefs tab^Friends"));
}
tabs_multiplayer.set_callback_value_change(make_dialog_callback(
boost::bind(&tpreferences::on_tab_select, this, _1, "mp_tab")));
#ifdef GUI2_EXPERIMENTAL_LISTBOX
connect_signal_notify_modified(selector, boost::bind(
&tpreferences::on_tab_select,
this,
boost::ref(window)));
#else
selector.set_callback_value_change(dialog_callback
<tpreferences, &tpreferences::on_tab_select>);
#endif
}
static int index_in_pager_range(const int& first, const tstacked_widget& pager)
{
// Ensure the specified index is between 0 and one less than the max
// number of pager layers (since get_layer_count returns one-past-end).
return std::min<int>(std::max(0, first), pager.get_layer_count() - 1);
}
void tpreferences::pre_show(CVideo& /*video*/, twindow& window)
@ -1111,10 +1126,6 @@ void tpreferences::pre_show(CVideo& /*video*/, twindow& window)
add_pager_row(selector, "multiplayer.png", _("Prefs section^Multiplayer"));
add_pager_row(selector, "advanced.png", _("Prefs section^Advanced"));
// Initializes tabs for the various pages. This should be done before
// setting up the member callbacks.
initialize_tabs(window);
// Initializes initial values and sets up callbacks. This needs to be
// done before selecting the initial page, otherwise widgets from other
// pages cannot be found afterwards.
@ -1122,12 +1133,34 @@ void tpreferences::pre_show(CVideo& /*video*/, twindow& window)
assert(selector.get_item_count() == pager.get_layer_count());
// Selects initial tab for each page and the initially displayed main
// page. This should be done last after all intilization
set_visible_page(window, 0, "mp_tab_pager");
const int main_index = index_in_pager_range(index_.first, pager);
selector.select_row(0);
pager.select_layer(0);
// Loops through each pager layer and checks if it has both a tab bar
// and stack. If so, it initilizes the options for the former and
// selects the specified layer of the latter.
for(unsigned int i = 0; i < pager.get_layer_count(); ++i) {
tlistbox* tab_selector = find_widget<tlistbox>(
pager.get_layer_grid(i), "tab_selector", false, false);
tstacked_widget* tab_pager = find_widget<tstacked_widget>(
pager.get_layer_grid(i), "tab_pager", false, false);
if(tab_pager && tab_selector) {
const int ii = static_cast<int>(i);
const int tab_index = index_in_pager_range(index_.second, *tab_pager);
const int to_select = (ii == main_index ? tab_index : 0);
// Initialize tabs for this page
initialize_tabs(window, *tab_selector, ii);
tab_selector->select_row(to_select);
tab_pager->select_layer(to_select);
}
}
// Finally, select the initial main page
selector.select_row(main_index);
pager.select_layer(main_index);
}
void tpreferences::set_visible_page(twindow& window, unsigned int page, const std::string& pager_id)
@ -1230,11 +1263,11 @@ void tpreferences::on_page_select(twindow& window)
set_visible_page(window, static_cast<unsigned int>(selected_row), "pager");
}
void tpreferences::on_tab_select(twindow& window, const std::string& widget_id)
void tpreferences::on_tab_select(twindow& window)
{
const int selected_row =
std::max(0, find_widget<tlistbox>(&window, widget_id, false).get_selected_row());
set_visible_page(window, static_cast<unsigned int>(selected_row), (widget_id + "_pager"));
std::max(0, find_widget<tlistbox>(&window, "tab_selector", false).get_selected_row());
set_visible_page(window, static_cast<unsigned int>(selected_row), "tab_pager");
}
void tpreferences::post_show(twindow& /*window*/)

View file

@ -24,6 +24,7 @@
// This file is not named preferences.hpp in order -I conflicts with
// src/preferences.hpp.
namespace hotkey {
struct hotkey_command;
}
@ -51,8 +52,14 @@ public:
tpreferences(video, game_cfg).show(video);
return true;
}
typedef std::vector<const hotkey::hotkey_command*> t_visible_hotkeys;
void set_selected_index(std::pair<int, int> index)
{
index_ = index;
}
private:
/** Inherited from tdialog, implemented by REGISTER_DIALOG. */
virtual const std::string& window_id() const;
@ -63,7 +70,7 @@ private:
/** Initializers */
void initialize_members(twindow& window);
void initialize_tabs(twindow& window);
void initialize_tabs(twindow& window, tlistbox& selector, const int index);
void setup_friends_list(twindow& window);
void setup_hotkey_list(twindow& window);
@ -81,7 +88,7 @@ private:
/** Callback for selection changes */
void on_page_select(twindow& window);
void on_tab_select(twindow& window, const std::string& widget_id);
void on_tab_select(twindow& window);
void on_advanced_prefs_list_select(tlistbox& tree, twindow& window);
/** Special callback functions */
@ -214,6 +221,9 @@ private:
// Special variable to keep the value of the scaling slider,
// to be used in post_show
int font_scaling_;
// The page/tab index pairs for setting visible pages
std::pair<int, int> index_;
};
} // namespace gui2

View file

@ -22,6 +22,11 @@ class CVideo;
namespace preferences {
enum DIALOG_OPEN_TO {
VIEW_DEFAULT,
VIEW_FRIENDS
};
// FIXME: this box should be vertically centered on the screen, but is not
static const int height = 400;
static const int width = 465;
@ -45,7 +50,8 @@ namespace preferences {
void set_idle_anim_rate(int rate);
std::string show_wesnothd_server_search(CVideo&);
void show_preferences_dialog(CVideo& disp, const config& game_cfg);
void show_preferences_dialog(CVideo& disp, const config& game_cfg,
const DIALOG_OPEN_TO initial_view = VIEW_DEFAULT);
bool show_theme_dialog(CVideo& disp);
// If prefs is non-null, save the hotkeys in that config