← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/observer_building_spaces into lp:widelands

 

Martin Schmidt has proposed merging lp:~widelands-dev/widelands/observer_building_spaces into lp:widelands.

Requested reviews:
  Widelands Developers (widelands-dev)

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/observer_building_spaces/+merge/249764

move of toogle buildhelp to the base class of player and spectator
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/observer_building_spaces into lp:widelands.
=== modified file 'src/wui/interactive_gamebase.cc'
--- src/wui/interactive_gamebase.cc	2014-12-06 12:22:35 +0000
+++ src/wui/interactive_gamebase.cc	2015-02-14 22:40:35 +0000
@@ -29,6 +29,7 @@
 #include "logic/findbob.h"
 #include "logic/game.h"
 #include "logic/game_controller.h"
+#include "logic/map.h"
 #include "logic/player.h"
 #include "logic/ship.h"
 #include "profile/profile.h"
@@ -59,8 +60,18 @@
 		(global_s.get_string("building_tooltip_format",      "%r")),
 	m_chatenabled(chatenabled),
 	m_multiplayer(multiplayer),
-	m_playertype(pt)
-{}
+	m_playertype(pt),
+
+#define INIT_BTN(picture, name, tooltip)                            \
+ TOOLBAR_BUTTON_COMMON_PARAMETERS(name),                                      \
+ g_gr->images().get("pics/" picture ".png"),                      \
+ tooltip                                                                      \
+
+	m_toggle_buildhelp
+		(INIT_BTN("menu_toggle_buildhelp", "buildhelp", _("Show Building Spaces (on/off)")))
+{
+	m_toggle_buildhelp.sigclicked.connect(boost::bind(&InteractiveGameBase::toggle_buildhelp, this));
+}
 
 /// \return a pointer to the running \ref Game instance.
 Widelands::Game * InteractiveGameBase::get_game() const
@@ -119,6 +130,36 @@
 
 
 /**
+ * Called for every game after loading (from a savegame or just from a map
+ * during single/multiplayer/scenario).
+ */
+void InteractiveGameBase::postload() {
+	Widelands::Map & map = egbase().map();
+	OverlayManager & overlay_manager = map.overlay_manager();
+	overlay_manager.show_buildhelp(false);
+	overlay_manager.register_overlay_callback_function
+			(boost::bind(&InteractiveGameBase::calculate_buildcaps, this, _1));
+
+	// Connect buildhelp button to reflect build help state. Needs to be
+	// done here rather than in the constructor as the map is not present then.
+	// This code assumes that the InteractivePlayer object lives longer than
+	// the overlay_manager. Otherwise remove the hook in the deconstructor.
+	egbase().map().overlay_manager().onBuildHelpToggle =
+		boost::bind(&UI::Button::set_perm_pressed, &m_toggle_buildhelp, _1);
+	m_toggle_buildhelp.set_perm_pressed(buildhelp());
+
+	// Recalc whole map for changed owner stuff
+	map.recalc_whole_map(egbase().world());
+
+	// Close game-relevant UI windows (but keep main menu open)
+	delete m_fieldaction.window;
+	m_fieldaction.window = nullptr;
+
+	hide_minimap();
+}
+
+
+/**
  * See if we can reasonably open a ship window at the current selection position.
  * If so, do it and return true; otherwise, return false.
  */

=== modified file 'src/wui/interactive_gamebase.h'
--- src/wui/interactive_gamebase.h	2014-11-30 12:23:45 +0000
+++ src/wui/interactive_gamebase.h	2015-02-14 22:40:35 +0000
@@ -82,9 +82,11 @@
 	bool is_multiplayer() {return m_multiplayer;}
 
 	void show_game_summary();
-
+	void postload() override;
+	void start() override {};
 protected:
 	void draw_overlay(RenderTarget &) override;
+	virtual int32_t calculate_buildcaps(const Widelands::TCoords<Widelands::FCoords> c) =0;
 
 	GameMainMenuWindows m_mainm_windows;
 	ChatProvider           * m_chatProvider;
@@ -96,6 +98,8 @@
 	PlayerType m_playertype;
 	UI::UniqueWindow::Registry m_fieldaction;
 	UI::UniqueWindow::Registry m_game_summary;
+
+	UI::Button m_toggle_buildhelp;
 };
 
 #endif  // end of include guard: WL_WUI_INTERACTIVE_GAMEBASE_H

=== modified file 'src/wui/interactive_player.cc'
--- src/wui/interactive_player.cc	2014-12-08 11:08:38 +0000
+++ src/wui/interactive_player.cc	2015-02-14 22:40:35 +0000
@@ -61,18 +61,6 @@
 using Widelands::Building;
 using Widelands::Map;
 
-namespace  {
-
-// This function is the callback for recalculation of field overlays
-int32_t int_player_overlay_callback_function
-	(Widelands::TCoords<Widelands::FCoords> const c, InteractivePlayer& iap)
-{
-	assert(iap.get_player());
-	return iap.get_player()->get_buildcaps(c);
-}
-
-}  // namespace
-
 InteractivePlayer::InteractivePlayer
 	(Widelands::Game        &       _game,
 	 Section                &       global_s,
@@ -84,7 +72,6 @@
 	m_flag_to_connect(Widelands::Coords::null()),
 
 // Chat is different, as m_chatProvider needs to be checked when toggling
-// Buildhelp is different as it does not toggle a UniqueWindow
 // Minimap is different as it warps and stuff
 
 #define INIT_BTN_this(picture, name, tooltip)                       \
@@ -114,9 +101,6 @@
 m_toggle_minimap
 	(INIT_BTN_this
 	 ("menu_toggle_minimap", "minimap", _("Minimap"))),
-m_toggle_buildhelp
-	(INIT_BTN_this
-	 ("menu_toggle_buildhelp", "buildhelp", _("Show Building Spaces (on/off)"))),
 m_toggle_message_menu
 	(INIT_BTN
 	 ("menu_toggle_oldmessage_menu", "messages", _("Messages"))),
@@ -135,8 +119,6 @@
 		(boost::bind(&UI::UniqueWindow::Registry::toggle, boost::ref(m_objectives)));
 	m_toggle_minimap.sigclicked.connect
 		(boost::bind(&InteractivePlayer::toggle_minimap, this));
-	m_toggle_buildhelp.sigclicked.connect
-		(boost::bind(&InteractivePlayer::toggle_buildhelp, this));
 	m_toggle_message_menu.sigclicked.connect
 		(boost::bind(&UI::UniqueWindow::Registry::toggle, boost::ref(m_message_menu)));
 	m_toggle_help.sigclicked.connect
@@ -206,17 +188,6 @@
 	DEINIT_BTN_HOOKS(m_message_menu, m_toggle_message_menu)
 }
 
-
-/*
-===============
-Called just before the game starts, after postload, init and gfxload
-===============
-*/
-void InteractivePlayer::start()
-{
-	postload();
-}
-
 void InteractivePlayer::think()
 {
 	InteractiveBase::think();
@@ -267,37 +238,6 @@
 }
 
 
-/**
- * Called for every game after loading (from a savegame or just from a map
- * during single/multiplayer/scenario).
- */
-void InteractivePlayer::postload()
-{
-	Map & map = egbase().map();
-	OverlayManager & overlay_manager = map.overlay_manager();
-	overlay_manager.show_buildhelp(false);
-	overlay_manager.register_overlay_callback_function
-			(boost::bind(&int_player_overlay_callback_function, _1, boost::ref(*this)));
-
-	// Connect buildhelp button to reflect build help state. Needs to be
-	// done here rather than in the constructor as the map is not present then.
-	// This code assumes that the InteractivePlayer object lives longer than
-	// the overlay_manager. Otherwise remove the hook in the deconstructor.
-	egbase().map().overlay_manager().onBuildHelpToggle =
-		boost::bind(&UI::Button::set_perm_pressed, &m_toggle_buildhelp, _1);
-	m_toggle_buildhelp.set_perm_pressed(buildhelp());
-
-	// Recalc whole map for changed owner stuff
-	map.recalc_whole_map(egbase().world());
-
-	// Close game-relevant UI windows (but keep main menu open)
-	delete m_fieldaction.window;
-	m_fieldaction.window = nullptr;
-
-	hide_minimap();
-}
-
-
 void InteractivePlayer::popup_message
 	(Widelands::MessageId const id, const Widelands::Message & message)
 {
@@ -328,6 +268,11 @@
 	return m_player_number;
 }
 
+int32_t InteractivePlayer::calculate_buildcaps(const Widelands::TCoords<Widelands::FCoords> c) {
+	assert(get_player());
+	return get_player()->get_buildcaps(c);
+}
+
 
 /// Player has clicked on the given node; bring up the context menu.
 void InteractivePlayer::node_action()

=== modified file 'src/wui/interactive_player.h'
--- src/wui/interactive_player.h	2014-10-14 06:30:20 +0000
+++ src/wui/interactive_player.h	2015-02-14 22:40:35 +0000
@@ -51,8 +51,6 @@
 
 	~InteractivePlayer();
 
-	void start() override;
-
 	void toggle_chat        ();
 
 	bool can_see(Widelands::PlayerNumber) const override;
@@ -77,13 +75,13 @@
 	// For load
 	void cleanup_for_load() override;
 	void think() override;
-	void postload() override;
 
 	void set_flag_to_connect(Widelands::Coords const location) {
 		m_flag_to_connect = location;
 	}
 
 	void popup_message(Widelands::MessageId, const Widelands::Message &);
+	int32_t calculate_buildcaps(const Widelands::TCoords<Widelands::FCoords> c) override;
 
 private:
 	void cmdSwitchPlayer(const std::vector<std::string> & args);
@@ -97,7 +95,6 @@
 	UI::Button m_toggle_statistics_menu;
 	UI::Button m_toggle_objectives;
 	UI::Button m_toggle_minimap;
-	UI::Button m_toggle_buildhelp;
 	UI::Button m_toggle_message_menu;
 	UI::Button m_toggle_help;
 

=== modified file 'src/wui/interactive_spectator.cc'
--- src/wui/interactive_spectator.cc	2014-12-03 08:14:35 +0000
+++ src/wui/interactive_spectator.cc	2015-02-14 22:40:35 +0000
@@ -24,6 +24,7 @@
 #include "chat/chat.h"
 #include "graphic/graphic.h"
 #include "logic/game_controller.h"
+#include "logic/player.h"
 #include "ui_basic/editbox.h"
 #include "ui_basic/multilinetextarea.h"
 #include "ui_basic/textarea.h"
@@ -75,6 +76,7 @@
 		m_toolbar.add(&m_toggle_options_menu, UI::Box::AlignLeft);
 	m_toolbar.add(&m_toggle_statistics,      UI::Box::AlignLeft);
 	m_toolbar.add(&m_toggle_minimap,         UI::Box::AlignLeft);
+	m_toolbar.add(&m_toggle_buildhelp,       UI::Box::AlignLeft);
 	m_toolbar.add(&m_toggle_chat,            UI::Box::AlignLeft);
 
 	// TODO(unknown): instead of making unneeded buttons invisible after generation,
@@ -140,17 +142,17 @@
 }
 
 
-/**
- * Called just before the game starts, after postload, init and gfxload
- */
-void InteractiveSpectator::start()
-{
-	Widelands::Map & map = game().map();
-	OverlayManager & overlay_manager = map.overlay_manager();
-	overlay_manager.show_buildhelp(false);
+int32_t InteractiveSpectator::calculate_buildcaps(const Widelands::TCoords<Widelands::FCoords> c) {
+	const Widelands::PlayerNumber nr_players = game().map().get_nrplayers();
+	
+	iterate_players_existing(p, nr_players, game(), player) {
+		const Widelands::NodeCaps nc = player->get_buildcaps(c);
+		if( nc > Widelands::NodeCaps::CAPS_NONE )	{
+			return nc;
+		}
+	}
 
-	// Recalc whole map for changed owner stuff
-	map.recalc_whole_map(game().world());
+	return Widelands::NodeCaps::CAPS_NONE;
 }
 
 
@@ -245,6 +247,10 @@
 {
 	if (down)
 		switch (code.sym) {
+		case SDLK_SPACE:
+			toggle_buildhelp();
+			return true;
+
 		case SDLK_m:
 			toggle_minimap();
 			return true;

=== modified file 'src/wui/interactive_spectator.h'
--- src/wui/interactive_spectator.h	2014-10-14 06:30:20 +0000
+++ src/wui/interactive_spectator.h	2015-02-14 22:40:35 +0000
@@ -40,8 +40,6 @@
 
 	~InteractiveSpectator();
 
-	void start() override;
-
 	Widelands::Player * get_player() const override;
 
 	bool handle_key(bool down, SDL_Keysym) override;
@@ -52,6 +50,7 @@
 	void toggle_statistics();
 	void exit_btn();
 	void save_btn();
+	int32_t calculate_buildcaps(const Widelands::TCoords<Widelands::FCoords> ) override;
 	bool can_see(Widelands::PlayerNumber) const override;
 	bool can_act(Widelands::PlayerNumber) const override;
 	Widelands::PlayerNumber player_number() const override;


Follow ups