← Back to team overview

widelands-dev team mailing list archive

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

 

SirVer has proposed merging lp:~widelands-dev/widelands/toggle_resources into lp:widelands with lp:~widelands-dev/widelands/buildhelp_overlay as a prerequisite.

Commit message:
Make resources toggable in the editor.

- Changes drawing of resources to use a callback instead of a vector of objects which saves some code and might be slightly more efficient.


Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #778540 in widelands: "Editor: Make display of resources switchable"
  https://bugs.launchpad.net/widelands/+bug/778540
  Bug #1649958 in widelands: "Work on Toggle-Editor-Views with discussion on shortcuts/hotkeys"
  https://bugs.launchpad.net/widelands/+bug/1649958

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/toggle_resources/+merge/328958
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/toggle_resources into lp:widelands.
=== added file 'data/images/wui/menus/menu_toggle_resources.png'
Binary files data/images/wui/menus/menu_toggle_resources.png	1970-01-01 00:00:00 +0000 and data/images/wui/menus/menu_toggle_resources.png	2017-08-13 20:50:38 +0000 differ
=== modified file 'src/editor/editorinteractive.cc'
--- src/editor/editorinteractive.cc	2017-08-13 20:50:38 +0000
+++ src/editor/editorinteractive.cc	2017-08-13 20:50:38 +0000
@@ -115,16 +115,27 @@
 
 	toolbar_.add_space(15);
 
-	add_toolbar_button(
-	   "wui/menus/menu_toggle_minimap", "minimap", _("Minimap"), &minimap_registry(), true);
-	minimap_registry().open_window = [this] { toggle_minimap(); };
-
 	toggle_buildhelp_ = add_toolbar_button(
 	   "wui/menus/menu_toggle_buildhelp", "buildhelp", _("Show Building Spaces (on/off)"));
 	toggle_buildhelp_->sigclicked.connect(boost::bind(&EditorInteractive::toggle_buildhelp, this));
-
-	reset_zoom_ = add_toolbar_button("wui/menus/menu_reset_zoom", "reset_zoom", _("Reset zoom"));
-	reset_zoom_->sigclicked.connect([this] {
+	auto toggle_resources = add_toolbar_button(
+	   "wui/menus/menu_toggle_resources", "resources", _("Show Resources (on/off)"));
+	toggle_resources->set_perm_pressed(true);
+	toggle_resources->sigclicked.connect([this, toggle_resources]() {
+		auto* overlay_manager = mutable_field_overlay_manager();
+		const bool value = !overlay_manager->is_enabled(OverlayLevel::kResource);
+		overlay_manager->set_enabled(OverlayLevel::kResource, value);
+		toggle_resources->set_perm_pressed(value);
+	});
+
+	toolbar_.add_space(15);
+
+	add_toolbar_button(
+	   "wui/menus/menu_toggle_minimap", "minimap", _("Minimap"), &minimap_registry(), true);
+	minimap_registry().open_window = [this] { toggle_minimap(); };
+
+	auto zoom = add_toolbar_button("wui/menus/menu_reset_zoom", "reset_zoom", _("Reset zoom"));
+	zoom->sigclicked.connect([this] {
 		zoom_around(1.f, Vector2f(get_w() / 2.f, get_h() / 2.f), MapView::Transition::Smooth);
 	});
 

=== modified file 'src/editor/editorinteractive.h'
--- src/editor/editorinteractive.h	2017-06-24 08:47:46 +0000
+++ src/editor/editorinteractive.h	2017-08-13 20:50:38 +0000
@@ -178,7 +178,6 @@
 	UI::UniqueWindow::Registry helpmenu_;
 
 	UI::Button* toggle_buildhelp_;
-	UI::Button* reset_zoom_;
 	UI::Button* undo_;
 	UI::Button* redo_;
 

=== modified file 'src/graphic/game_renderer.cc'
--- src/graphic/game_renderer.cc	2017-08-13 20:50:38 +0000
+++ src/graphic/game_renderer.cc	2017-08-13 20:50:38 +0000
@@ -210,7 +210,6 @@
                   const Player* player,
                   const TextToDraw text_to_draw,
                   RenderTarget* dst) {
-	std::vector<FieldOverlayManager::OverlayInfo> overlay_info;
 	for (size_t current_index = 0; current_index < fields_to_draw.size(); ++current_index) {
 		const FieldsToDraw::Field& field = fields_to_draw.at(current_index);
 		if (!field.all_neighbors_valid()) {
@@ -248,18 +247,13 @@
 			draw_objects_for_formerly_visible_field(field, player_field, zoom, dst);
 		}
 
-		const FieldOverlayManager& overlay_manager = egbase.get_ibase()->field_overlay_manager();
-		{
-			overlay_info.clear();
-			overlay_manager.get_overlays(field.fcoords, &overlay_info);
-			for (const auto& overlay : overlay_info) {
-				dst->blitrect_scale(
-				   Rectf(field.rendertarget_pixel - overlay.hotspot.cast<float>() * zoom,
-				         overlay.pic->width() * zoom, overlay.pic->height() * zoom),
-				   overlay.pic, Recti(0, 0, overlay.pic->width(), overlay.pic->height()), 1.f,
-				   BlendMode::UseAlpha);
-			}
-		}
+		egbase.get_ibase()->field_overlay_manager().foreach_overlay(
+		   field.fcoords, [dst, &field, zoom](const Image* pic, const Vector2i& hotspot) {
+			   dst->blitrect_scale(Rectf(field.rendertarget_pixel - hotspot.cast<float>() * zoom,
+			                             pic->width() * zoom, pic->height() * zoom),
+			                       pic, Recti(0, 0, pic->width(), pic->height()), 1.f,
+			                       BlendMode::UseAlpha);
+			});
 	}
 }
 

=== modified file 'src/wui/field_overlay_manager.cc'
--- src/wui/field_overlay_manager.cc	2017-08-13 20:50:38 +0000
+++ src/wui/field_overlay_manager.cc	2017-08-13 20:50:38 +0000
@@ -26,12 +26,6 @@
 #include "graphic/graphic.h"
 #include "logic/field.h"
 
-namespace  {
-
-constexpr int kLevelForBuildHelp = 5;
-
-}  // namespace
-
 FieldOverlayManager::FieldOverlayManager() : current_overlay_id_(0) {
 	OverlayInfo* buildhelp_info = buildhelp_infos_;
 	const char* filenames[] = {"images/wui/overlays/set_flag.png", "images/wui/overlays/small.png",
@@ -64,28 +58,6 @@
 	buildhelp_ = value;
 }
 
-void FieldOverlayManager::get_overlays(const Widelands::FCoords& c,
-                                       std::vector<OverlayInfo>* result) const {
-	auto it = overlays_.lower_bound(c);
-	while (it != overlays_.end() && it->first == c &&
-	       static_cast<int>(it->second.level) <= kLevelForBuildHelp) {
-		result->emplace_back(it->second.pic, it->second.hotspot);
-		++it;
-	}
-
-	if (buildhelp_) {
-		int buildhelp_overlay_index = get_buildhelp_overlay(c);
-		if (buildhelp_overlay_index < Widelands::Field::Buildhelp_None) {
-			result->emplace_back(buildhelp_infos_[buildhelp_overlay_index]);
-		}
-	}
-
-	while (it != overlays_.end() && it->first == c) {
-		result->emplace_back(it->second.pic, it->second.hotspot);
-		++it;
-	}
-}
-
 int FieldOverlayManager::get_buildhelp_overlay(const Widelands::FCoords& fc) const {
 	Widelands::NodeCaps const caps =
 	   callback_ ? static_cast<Widelands::NodeCaps>(callback_(fc)) : fc.field->nodecaps();
@@ -196,3 +168,16 @@
 	++current_overlay_id_;
 	return current_overlay_id_;
 }
+
+bool FieldOverlayManager::is_enabled(const OverlayLevel& level) const {
+	return disabled_layers_.count(level) == 0;
+}
+
+void FieldOverlayManager::set_enabled(const OverlayLevel& level, const bool enabled) {
+	if (enabled) {
+		disabled_layers_.erase(level);
+	} else {
+		disabled_layers_.insert(level);
+	}
+}
+

=== modified file 'src/wui/field_overlay_manager.h'
--- src/wui/field_overlay_manager.h	2017-08-13 20:50:38 +0000
+++ src/wui/field_overlay_manager.h	2017-08-13 20:50:38 +0000
@@ -56,24 +56,13 @@
 	kResource = 4,
 	kSelection = 7,
 	kRoadBuildSlope = 8,
-	kPlayerStartingPosition= 9,
+	kPlayerStartingPosition = 9,
 };
 
 struct FieldOverlayManager {
 	/// A unique id identifying a registered overlay.
 	using OverlayId = uint32_t;
 
-	/// A overlay as drawn onto the screen.
-	struct OverlayInfo {
-		OverlayInfo() = default;
-		OverlayInfo(const Image* init_pic, const Vector2i& init_hotspot)
-		   : pic(init_pic), hotspot(init_hotspot) {
-		}
-
-		const Image* pic;
-		Vector2i hotspot = Vector2i::zero();
-	};
-
 	/// A function returning Field::nodecaps() for the build overlay. This can be
 	/// registered to hide or change some of the nodecaps during rendering.
 	using CallbackFn =
@@ -90,6 +79,10 @@
 	/// Register callback function.
 	void register_overlay_callback_function(CallbackFn function);
 
+	/// Like 'buildhelp', but for an individual layer.
+	bool is_enabled(const OverlayLevel& level) const;
+	void set_enabled(const OverlayLevel& level, bool value);
+
 	/// Get a unique, unused id that can be passed to register_overlay.
 	OverlayId next_overlay_id();
 
@@ -112,10 +105,44 @@
 	// TODO(sirver): It would be preferable to just delete and recreate the object.
 	void remove_all_overlays();
 
-	/// Returns the currently registered overlays and the buildhelp for a node.
-	void get_overlays(const Widelands::FCoords& c, std::vector<OverlayInfo>* result) const;
+	/// Calls 'func' for each of the the currently registered and enabled
+	/// overlays and the buildhelp.
+	template <typename T> void foreach_overlay(const Widelands::FCoords& c, T func) const {
+		auto it = overlays_.lower_bound(c);
+		while (it != overlays_.end() && it->first == c &&
+		       static_cast<int>(it->second.level) <= kLevelForBuildHelp) {
+			if (is_enabled(it->second.level)) {
+				func(it->second.pic, it->second.hotspot);
+			}
+			++it;
+		}
+
+		if (buildhelp_) {
+			int buildhelp_overlay_index = get_buildhelp_overlay(c);
+			if (buildhelp_overlay_index < Widelands::Field::Buildhelp_None) {
+				auto& overlay_info = buildhelp_infos_[buildhelp_overlay_index];
+				func(overlay_info.pic, overlay_info.hotspot);
+			}
+		}
+
+		while (it != overlays_.end() && it->first == c) {
+			if (is_enabled(it->second.level)) {
+				func(it->second.pic, it->second.hotspot);
+			}
+			++it;
+		}
+}
+
 
 private:
+	static constexpr int kLevelForBuildHelp = 5;
+
+	/// A overlay as drawn onto the screen.
+	struct OverlayInfo {
+		const Image* pic = nullptr;
+		Vector2i hotspot = Vector2i::zero();
+	};
+
 	struct RegisteredOverlays {
 		RegisteredOverlays(const OverlayId init_overlay_id,
 		                   const Image* init_pic,
@@ -138,6 +165,9 @@
 
 	OverlayInfo buildhelp_infos_[Widelands::Field::Buildhelp_None];
 	bool buildhelp_;
+	// We are inverting the logic here, since new layers should be by default
+	// enabled and we only support to toggle some of them off.
+	std::set<OverlayLevel> disabled_layers_;
 
 	// this callback is used to define where overlays are drawn.
 	CallbackFn callback_;


Follow ups