← Back to team overview

widelands-dev team mailing list archive

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

 

GunChleoc has proposed merging lp:~widelands-dev/widelands/fit_button_text into lp:widelands.

Commit message:
Buttons will now automatically scale down their text if it is too wide. Removed unused function set_fontface from Textarea.

Requested reviews:
  Widelands Developers (widelands-dev)

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/fit_button_text/+merge/285528
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/fit_button_text into lp:widelands.
=== modified file 'src/graphic/text_layout.cc'
--- src/graphic/text_layout.cc	2016-02-08 00:18:02 +0000
+++ src/graphic/text_layout.cc	2016-02-09 20:55:39 +0000
@@ -136,6 +136,18 @@
 	return f.str();
 }
 
+const Image* autofit_ui_text(const std::string& text, int width, RGBColor color, int fontsize) {
+
+	const Image* result = UI::g_fh1->render(as_uifont(text, fontsize, color, UI::FontSet::Face::kSans));
+
+	if (width > 0) { // Autofit
+		for (; result->width() > width && fontsize >= kMinimumFontSize; --fontsize) {
+			result = UI::g_fh1->render(as_uifont(text, fontsize, color, UI::FontSet::Face::kCondensed));
+		}
+	}
+	return result;
+}
+
 
 namespace UI {
 

=== modified file 'src/graphic/text_layout.h'
--- src/graphic/text_layout.h	2016-02-08 00:18:02 +0000
+++ src/graphic/text_layout.h	2016-02-09 20:55:39 +0000
@@ -25,7 +25,9 @@
 
 #include "graphic/align.h"
 #include "graphic/font.h"
+#include "graphic/font_handler1.h"
 #include "graphic/color.h"
+#include "graphic/image.h"
 #include "graphic/text_constants.h"
 #include "graphic/text/font_set.h"
 
@@ -80,6 +82,16 @@
 std::string as_window_title(const std::string&);
 std::string as_game_tip(const std::string&);
 
+/**
+  * This will render the text as ui_font. If width > 0 and the rendered image is too wide,
+  * it will first use the condensed font face and then make the text smaller until it fits the specified
+  * width. The resulting font size will not go below kMinimumFontSize.
+  */
+const Image* autofit_ui_text(const std::string& text,
+									  int width = 0,
+									  RGBColor color = UI_FONT_CLR_FG,
+									  int fontsize = UI_FONT_SIZE_SMALL);
+
 namespace UI {
 
 /**

=== modified file 'src/ui_basic/button.cc'
--- src/ui_basic/button.cc	2016-02-03 18:09:15 +0000
+++ src/ui_basic/button.cc	2016-02-09 20:55:39 +0000
@@ -211,10 +211,11 @@
 
 	} else if (title_.length()) {
 		//  Otherwise draw title string centered
-		const Image* entry_text_im = UI::g_fh1->render(
-												  as_uifont(title_,
-																UI_FONT_SIZE_SMALL,
-																enabled_ ? UI_FONT_CLR_FG : UI_FONT_CLR_DISABLED));
+		const Image* entry_text_im =
+				autofit_ui_text(title_,
+									 get_inner_w() - 2 * kButtonImageMargin,
+									 enabled_ ? UI_FONT_CLR_FG : UI_FONT_CLR_DISABLED);
+
 		dst.blit(Point((get_w() - entry_text_im->width()) / 2, (get_h() - entry_text_im->height()) / 2),
 					entry_text_im);
 	}

=== modified file 'src/ui_basic/textarea.cc'
--- src/ui_basic/textarea.cc	2016-02-09 08:04:32 +0000
+++ src/ui_basic/textarea.cc	2016-02-09 20:55:39 +0000
@@ -85,7 +85,6 @@
 	set_thinks(false);
 	color_ = UI_FONT_CLR_FG;
 	fontsize_ = UI_FONT_SIZE_SMALL;
-	fontface_ = UI::FontSet::Face::kSans;
 	update();
 }
 
@@ -103,20 +102,13 @@
 	}
 }
 
-void Textarea::set_fontface(UI::FontSet::Face face) {
-	if (fontface_ != face) {
-		fontface_ = face;
-		update();
-	}
-}
-
 void Textarea::update()
 {
 	if (layoutmode_ == AutoMove) {
 		collapse(); // collapse() implicitly updates the size and position
 	}
 
-	render_text();
+	rendered_text_ = autofit_ui_text(text_, fixed_width_, color_, fontsize_);
 
 	if (layoutmode_ == AutoMove) {
 		expand();
@@ -243,15 +235,4 @@
 	set_desired_size(w, h);
 }
 
-
-void Textarea::render_text() {
-	rendered_text_ = UI::g_fh1->render(as_uifont(text_, fontsize_, color_, fontface_));
-
-	if (fixed_width_ > 0) { // Autofit
-		for (int size = fontsize_; rendered_text_->width() > fixed_width_ && size > kMinimumFontSize; --size) {
-			rendered_text_ = UI::g_fh1->render(as_uifont(text_, size, color_, UI::FontSet::Face::kCondensed));
-		}
-	}
-}
-
 }

=== modified file 'src/ui_basic/textarea.h'
--- src/ui_basic/textarea.h	2016-02-08 20:53:35 +0000
+++ src/ui_basic/textarea.h	2016-02-09 20:55:39 +0000
@@ -83,7 +83,6 @@
 
 	void set_color(RGBColor color);
 	void set_fontsize(int fontsize);
-	void set_fontface(UI::FontSet::Face face);
 
 protected:
 	void update_desired_size() override;
@@ -100,19 +99,12 @@
 	void expand();
 	void update();
 
-	/**
-	 * This rerenders the text. If fixed_width_ is set, it will also use the condensed
-	 * Fontset if needed and then make the text smaller until it fits.
-	 */
-	void render_text();
-
 	LayoutMode layoutmode_;
 	std::string text_;
 	const Image* rendered_text_;
 	Align align_;
 	RGBColor color_;
 	int fontsize_;
-	UI::FontSet::Face fontface_;
 
 	int fixed_width_;
 };


Follow ups