Ejemplo n.º 1
0
void title_screen::pre_show(window& win)
{
	win.set_click_dismiss(false);
	win.set_enter_disabled(true);
	win.set_escape_disabled(true);

	// Each time the dialog shows, we set this to false
	redraw_background_ = false;

#ifdef DEBUG_TOOLTIP
	win.connect_signal<event::SDL_MOUSE_MOTION>(
			std::bind(debug_tooltip, std::ref(win), _3, _5),
			event::dispatcher::front_child);
#endif

	win.connect_signal<event::SDL_VIDEO_RESIZE>(std::bind(&title_screen::on_resize, this, std::ref(win)));

	//
	// General hotkeys
	//
	win.register_hotkey(hotkey::TITLE_SCREEN__RELOAD_WML, [](event::dispatcher& w, hotkey::HOTKEY_COMMAND) {
		dynamic_cast<window&>(w).set_retval(RELOAD_GAME_DATA);
		return true;
	});

	win.register_hotkey(hotkey::HOTKEY_FULLSCREEN, std::bind(fullscreen, std::ref(win.video())));
	win.register_hotkey(hotkey::LUA_CONSOLE, std::bind(&launch_lua_console, std::ref(win)));

	//
	// Background and logo images
	//
	if(game_config::images::game_title.empty()) {
		ERR_CF << "No title image defined" << std::endl;
	} 

	win.get_canvas()[0].set_variable("title_image", variant(game_config::images::game_title));

	if(game_config::images::game_title_background.empty()) {
		ERR_CF << "No title background image defined" << std::endl;
	}

	win.get_canvas()[0].set_variable("background_image", variant(game_config::images::game_title_background));

	find_widget<image>(&win, "logo-bg", false).set_image(game_config::images::game_logo_background);
	find_widget<image>(&win, "logo", false).set_image(game_config::images::game_logo);

	//
	// Version string
	//
	const std::string version_string = formatter() << ("Version") << " " << game_config::revision;

	if(label* version_label = find_widget<label>(&win, "revision_number", false, false)) {
		version_label->set_label(version_string);
	}

	win.get_canvas()[0].set_variable("revision_number", variant(version_string));

	//
	// Tip-of-the-day browser
	//
	multi_page& tip_pages = find_widget<multi_page>(&win, "tips", false);

	std::vector<game_tip> tips(settings::get_tips());
	if(tips.empty()) {
		WRN_CF << "There are no tips of day available." << std::endl;
	}

	for(const auto& tip : tips)	{
		string_map widget;
		std::map<std::string, string_map> page;

		widget["use_markup"] = "true";

		widget["label"] = tip.text();
		page.emplace("tip", widget);

		widget["label"] = tip.source();
		page.emplace("source", widget);

		tip_pages.add_page(page);
	}

	update_tip(win, true);

	register_button(win, "next_tip", hotkey::TITLE_SCREEN__NEXT_TIP,
		std::bind(&title_screen::update_tip, this, std::ref(win), true));
	register_button(win, "previous_tip", hotkey::TITLE_SCREEN__PREVIOUS_TIP,
		std::bind(&title_screen::update_tip, this, std::ref(win), false));

	//
	// Help
	//
	register_button(win, "help", hotkey::HOTKEY_HELP, [this](window&) {
		help::help_manager help_manager(&game_config_manager::get()->game_config());
		help::show_help(game_.video());
	});

	//
	// About
	//
	register_button(win, "about", hotkey::HOTKEY_NULL, std::bind(&game_version::display, std::ref(win.video())));

	//
	// Tutorial
	//
	register_button(win, "tutorial", hotkey::TITLE_SCREEN__TUTORIAL, [this](window& w) {
		game_.set_tutorial();
		w.set_retval(LAUNCH_GAME);
	});

	//
	// Campaign
	//
	register_button(win, "campaign", hotkey::TITLE_SCREEN__CAMPAIGN, [this](window& w) {
		try{
			if(game_.new_campaign()) {
				w.set_retval(LAUNCH_GAME);
			}
		} catch (const config::error& e) {
			gui2::show_error_message(game_.video(), e.what());
		}
	});

	//
	// Multiplayer
	//
	register_button(win, "multiplayer", hotkey::TITLE_SCREEN__MULTIPLAYER, [this](window& w) {
		while(true) {
			gui2::dialogs::mp_method_selection dlg;
			dlg.show(game_.video());

			if(dlg.get_retval() != gui2::window::OK) {
				return;
			}

			const int res = dlg.get_choice();

			if(res == 2 && preferences::mp_server_warning_disabled() < 2) {
				if(!gui2::dialogs::mp_host_game_prompt::execute(game_.video())) {
					continue;
				}
			}

			switch(res) {
				case 0:
					game_.select_mp_server(preferences::server_list().front().address);
					w.set_retval(MP_CONNECT);
					break;
				case 1:
					game_.select_mp_server("");
					w.set_retval(MP_CONNECT);
					break;
				case 2:
					game_.select_mp_server("localhost");
					w.set_retval(MP_HOST);
					break;
				case 3:
					w.set_retval(MP_LOCAL);
					break;
			}

			return;
		}
	});

	//
	// Load game
	//
	register_button(win, "load", hotkey::HOTKEY_LOAD_GAME, [this](window& w) {
		if(game_.load_game()) {
			w.set_retval(LAUNCH_GAME);
		} else {
			game_.clear_loaded_game();
		}
	});

	//
	// Addons
	//
	register_button(win, "addons", hotkey::TITLE_SCREEN__ADDONS, [this](window&) {
		// NOTE: we need the help_manager to get access to the Add-ons section in the game help!
		help::help_manager help_manager(&game_config_manager::get()->game_config());

		if(manage_addons(game_.video())) {
			game_config_manager::get()->reload_changed_game_config();
		}
	});

	//
	// Editor
	//
	register_button(win, "editor", hotkey::TITLE_SCREEN__EDITOR, [&](window& w) { w.set_retval(MAP_EDITOR); });

	//
	// Cores
	//
	register_button(win, "cores", hotkey::TITLE_SCREEN__CORES, [this](window&) {
		int current = 0;
		std::vector<config> cores;
		for(const config& core : game_config_manager::get()->game_config().child_range("core")) {
			cores.push_back(core);

			if(core["id"] == preferences::core_id()) {
				current = cores.size() - 1;
			}
		}

		gui2::dialogs::core_selection core_dlg(cores, current);
		if(core_dlg.show(game_.video())) {
			const std::string& core_id = cores[core_dlg.get_choice()]["id"];

			preferences::set_core_id(core_id);
			game_config_manager::get()->reload_changed_game_config();
		}
	});

	if(game_config_manager::get()->game_config().child_range("core").size() <= 1) {
		find_widget<button>(&win, "cores", false).set_visible(window::visibility::invisible);
	}

	//
	// Language
	//
	register_button(win, "language", hotkey::HOTKEY_LANGUAGE, [this](window& w) {
		try {
			if(game_.change_language()) {
				t_string::reset_translations();
				::image::flush_cache();
				on_resize(w);
			}
		} catch(std::runtime_error& e) {
			gui2::show_error_message(game_.video(), e.what());
		}
	});

	//
	// Preferences
	//
	register_button(win, "preferences", hotkey::HOTKEY_PREFERENCES, [this](window&) { game_.show_preferences(); });

	//
	// Credits
	//
	register_button(win, "credits", hotkey::TITLE_SCREEN__CREDITS, [&](window& w) { w.set_retval(SHOW_ABOUT); });

	//
	// Quit
	//
	register_button(win, "quit", hotkey::HOTKEY_QUIT_TO_DESKTOP, [&](window& w) { w.set_retval(QUIT_GAME); });

	//
	// Debug clock
	//
	register_button(win, "clock", hotkey::HOTKEY_NULL,
		std::bind(&title_screen::show_debug_clock_window, this, std::ref(win.video())));

	find_widget<button>(&win, "clock", false).set_visible(show_debug_clock_button
		? widget::visibility::visible
		: widget::visibility::invisible);
}
Ejemplo n.º 2
0
void lobby_main::pre_show(window& window)
{
	SCOPE_LB;

	gamelistbox_ = find_widget<listbox>(&window, "game_list", false, true);
#ifdef GUI2_EXPERIMENTAL_LISTBOX
	connect_signal_notify_modified(
			*gamelistbox_,
			std::bind(&lobby_main::gamelist_change_callback, *this, std::ref(window)));
#else
	gamelistbox_->set_callback_value_change(
			dialog_callback<lobby_main, &lobby_main::gamelist_change_callback>);
#endif

	window.keyboard_capture(gamelistbox_);

	player_list_.init(window);

	player_list_.sort_by_name->set_value(preferences::playerlist_sort_name());
	player_list_.sort_by_relation->set_value(preferences::playerlist_sort_relation());
	player_list_.update_sort_icons();

	player_list_.sort_by_name->set_callback_state_change(
			std::bind(&lobby_main::player_filter_callback, this, _1));
	player_list_.sort_by_relation->set_callback_state_change(
			std::bind(&lobby_main::player_filter_callback, this, _1));

	window.set_enter_disabled(true);
	window.set_escape_disabled(true);

	// A new key handler to deal with escape in a different manner.
	window.connect_signal<event::SDL_KEY_DOWN>(
		std::bind(&lobby_main::signal_handler_key_down, this, _5, _3, _4),
		event::dispatcher::front_pre_child);

	window_ = &window;

	chatbox_ = find_widget<chatbox>(&window, "chat", false, true);
	chatbox_->set_lobby_info(lobby_info_);
	chatbox_->set_wesnothd_connection(wesnothd_connection_);
	chatbox_->set_active_window_changed_callback([this]() { player_list_dirty_ = true; });

	find_widget<button>(&window, "create", false).set_retval(CREATE);

	connect_signal_mouse_left_click(
		find_widget<button>(&window, "refresh", false),
		std::bind(&lobby_main::refresh_button_callback, this, std::ref(window)));

	connect_signal_mouse_left_click(
		find_widget<button>(&window, "show_preferences", false),
		std::bind(&lobby_main::show_preferences_button_callback, this, std::ref(window)));

	connect_signal_mouse_left_click(
		find_widget<button>(&window, "join_global", false),
		std::bind(&lobby_main::join_global_button_callback, this, std::ref(window)));

	find_widget<button>(&window, "join_global", false).set_active(false);

	connect_signal_mouse_left_click(
		find_widget<button>(&window, "observe_global", false),
		std::bind(&lobby_main::observe_global_button_callback, this, std::ref(window)));

	find_widget<button>(&window, "observe_global", false).set_active(false);

	menu_button& replay_options = find_widget<menu_button>(&window, "replay_options", false);

	if(preferences::skip_mp_replay()) {
		replay_options.set_selected(1);
	}

	if(preferences::blindfold_replay()) {
		replay_options.set_selected(2);
	}

	replay_options.connect_click_handler(
			std::bind(&lobby_main::skip_replay_changed_callback, this, std::ref(window)));

	filter_friends_ = find_widget<toggle_button>(&window, "filter_with_friends", false, true);
	filter_ignored_ = find_widget<toggle_button>(&window, "filter_without_ignored", false, true);
	filter_slots_   = find_widget<toggle_button>(&window, "filter_vacant_slots", false, true);
	filter_invert_  = find_widget<toggle_button>(&window, "filter_invert", false, true);
	filter_text_    = find_widget<text_box>(&window, "filter_text", false, true);

	filter_friends_->set_callback_state_change(
			std::bind(&lobby_main::game_filter_change_callback, this, _1));
	filter_ignored_->set_callback_state_change(
			std::bind(&lobby_main::game_filter_change_callback, this, _1));
	filter_slots_->set_callback_state_change(
			std::bind(&lobby_main::game_filter_change_callback, this, _1));
	filter_invert_->set_callback_state_change(
			std::bind(&lobby_main::game_filter_change_callback, this, _1));
	connect_signal_pre_key_press(
			*filter_text_,
			std::bind(&lobby_main::game_filter_keypress_callback, this, _5));

	chatbox_->room_window_open("lobby", true, false);
	chatbox_->active_window_changed();
	game_filter_reload();

	// Force first update to be directly.
	lobby_main::network_handler();
	lobby_update_timer_ = add_timer(
		game_config::lobby_network_timer, std::bind(&lobby_main::network_handler, this), true);

	// Set up Lua plugin context
	plugins_context_.reset(new plugins_context("Multiplayer Lobby"));

	plugins_context_->set_callback("join",    [&, this](const config&) {
		if(do_game_join(get_game_index_from_id(selected_game_id_), false)) {
			window.set_retval(JOIN);
		}
	}, true);

	plugins_context_->set_callback("observe", [&, this](const config&) {
		if(do_game_join(get_game_index_from_id(selected_game_id_), true)) {
			window.set_retval(OBSERVE);
		}
	}, true);

	plugins_context_->set_callback("create", [this, &window](const config&) { window.set_retval(CREATE); }, true);
	plugins_context_->set_callback("quit", [&window](const config&) { window.set_retval(window::CANCEL); }, false);

	plugins_context_->set_callback("chat", [this](const config& cfg) { chatbox_->send_chat_message(cfg["message"], false); }, true);
	plugins_context_->set_callback("select_game", [this](const config& cfg) {
		selected_game_id_ = cfg.has_attribute("id") ? cfg["id"].to_int() : lobby_info_.games()[cfg["index"].to_int()]->id;
	}, true);

	plugins_context_->set_accessor("game_list",   [this](const config&) { return lobby_info_.gamelist(); });
	plugins_context_->set_accessor("game_config", [this](const config&) { return game_config_; });
}