GUI2/Text Box: refactored hint text interface

Previously, I had implemented the hint text ("Search)" and image (the magnifying glass)
as a custom text box definition. This caused some problems, though. Since the string was
hard-coded as part of a WFL formula, it couldn't be translated (see #2709 and #2732). It
also wasn't expandable to any other usecase.

Instead, I've added two new hint_text= and hint_image= keys to [text_box], wrapped the
"Search" text and magnifying image path in a helper macro, and refactored the default
definition to display the hint text and image when appropriate.

This also fixes a minor issue where selected text wouldn't remain highlighted when the
box wasn't focused when using the filter definition (it did with the default one).

This should fix the untranslatable "Search" text issue mentioned in both issues above.
The new string is in the wesnoth-lib textdomain.

(cherry-picked from commit db5abb7e63)
This commit is contained in:
Charles Dang 2018-03-26 12:11:34 +11:00
parent 31320923bf
commit e0b35791e2
12 changed files with 109 additions and 109 deletions

View file

@ -344,6 +344,11 @@
[/pre_commit]
#enddef
#define FITER_TEXT_BOX_HINT
hint_text = _ "Search"
hint_image = "icons/action/zoomdefault_25.png~FL(horiz)"
#enddef
###############################################################################
### ###
### Macros for the normal gui. ###

View file

@ -2156,6 +2156,16 @@
type="t_string"
default=""
[/key]
[key]
name="hint_text"
type="t_string"
default=""
[/key]
[key]
name="hint_image"
type="string"
default=""
[/key]
[/tag]
[tag]
name="toggle_button"

View file

@ -59,6 +59,57 @@
[/text]
#enddef
#
# The preprocessor can't process GUI__FONT_COLOR_DISABLED__DEFAULT as part of string
# concatenation for some reason. Perhaps it's the optional argument. So I'm just copying
# it here. Though the lack of quotes means I don't need to use concatenation anyway.
#
# -- vultraz, 2018-03-26
#
#define __DISABLED_COLOR_PREPROCESSOR_WORKAROUND
128, 128, 128, 255 #enddef
#define _GUI_DRAW_TEXT_OR_HINT SIZE COLOR
[rectangle]
x = "(text_x_offset + selection_offset)"
y = "(text_y_offset)"
w = "(selection_width)"
h = "(text_font_height)"
border_thickness = 0
fill_color = "21, 53, 80, 255"
[/rectangle]
[text]
x = "(text_x_offset)"
y = "(text_y_offset)"
w = "(text_width)"
h = "(text_height)"
maximum_width = "(text_maximum_width)"
font_size = "(
if(text = '' and hint_text != '', hint_size, reg_size) where
hint_size = {GUI_FONT_SIZE_SMALL},
reg_size = {SIZE}
)"
color = "(
if(text = '' and hint_text != '', hint_color, reg_color) where
hint_color = [{__DISABLED_COLOR_PREPROCESSOR_WORKAROUND}],
reg_color = [{COLOR}]
)"
text = "(
if(text = '' and hint_text != '', hint_text, text))"
[/text]
[image]
x = "(width - image_width)"
y = 0
name = "(if(text = '' and hint_image != '', hint_image, ''))"
[/image]
#enddef
#define _GUI_DRAW_CURSOR X_OFFSET
[line]
x1 = "(cursor_offset + {X_OFFSET})"
@ -98,6 +149,8 @@
text_y_offset = "(if(text_font_height <= height, (height - text_font_height) / 2, 0))"
text_extra_width = {EXTRA_WIDTH}
#functions = "(def show_hint_text() (text = '' and hint_text != '');)"
[state_enabled]
[draw]
@ -106,7 +159,7 @@
{_GUI_DRAW_BORDER ({GUI__BORDER_COLOR_DARK}) }
{_GUI_DRAW_TEXT ({FONT_SIZE}) ({GUI__FONT_COLOR_ENABLED__DEFAULT}) }
{_GUI_DRAW_TEXT_OR_HINT ({FONT_SIZE}) ({GUI__FONT_COLOR_ENABLED__DEFAULT}) }
[/draw]
@ -120,7 +173,7 @@
{_GUI_DRAW_BORDER ({GUI__FONT_COLOR_DISABLED_DARK__DEFAULT}) }
{_GUI_DRAW_TEXT ({FONT_SIZE}) ({GUI__FONT_COLOR_DISABLED__DEFAULT}) }
{_GUI_DRAW_TEXT_OR_HINT ({FONT_SIZE}) ({GUI__FONT_COLOR_DISABLED__DEFAULT}) }
[/draw]
@ -134,6 +187,7 @@
{_GUI_DRAW_BORDER ({GUI__BORDER_COLOR}) }
# We never draw the hint text or image if focused
{_GUI_DRAW_TEXT ({FONT_SIZE}) ({GUI__FONT_COLOR_ENABLED__DEFAULT}) }
{_GUI_DRAW_CURSOR ({X_OFFSET}) }
@ -162,110 +216,11 @@
[/text_box_definition]
#define _GUI_FILTER_BOX_STUFF FONT_SIZE
[image]
x = "(width - image_width)"
y = 0
name = "(if(text = '', 'icons/action/zoomdefault_25.png~FL(horiz)', ''))"
[/image]
[text]
x = "(text_x_offset)"
y = "(text_y_offset)"
w = "(text_width)"
h = "(text_height)"
maximum_width = "(text_maximum_width)"
font_size = {GUI_FONT_SIZE_SMALL}
color = {GUI__FONT_COLOR_DISABLED__DEFAULT}
text = "(if(text = '', 'Search', ''))"
[/text]
#enddef
#define _GUI_RESOLUTION_FILTER RESOLUTION MIN_WIDTH DEFAULT_WIDTH HEIGHT X_OFFSET EXTRA_WIDTH FONT_SIZE BACKGROUND_ENABLED BACKGROUND_DISABLED
[resolution]
{RESOLUTION}
min_width = {MIN_WIDTH}
min_height = {HEIGHT}
default_width = {DEFAULT_WIDTH}
default_height = {HEIGHT}
max_width = 0
max_height = {HEIGHT}
text_font_size = {FONT_SIZE}
text_x_offset = {X_OFFSET}
text_y_offset = "(if(text_font_height <= height, (height - text_font_height) / 2, 0))"
text_extra_width = {EXTRA_WIDTH}
[state_enabled]
[draw]
{BACKGROUND_ENABLED}
{_GUI_DRAW_BORDER ({GUI__BORDER_COLOR_DARK}) }
{_GUI_DRAW_TEXT ({FONT_SIZE}) ({GUI__FONT_COLOR_ENABLED__DEFAULT}) }
{_GUI_FILTER_BOX_STUFF ({FONT_SIZE})}
[/draw]
[/state_enabled]
[state_disabled]
[draw]
{BACKGROUND_DISABLED}
{_GUI_DRAW_BORDER ({GUI__FONT_COLOR_DISABLED_DARK__DEFAULT}) }
{_GUI_DRAW_TEXT ({FONT_SIZE}) ({GUI__FONT_COLOR_DISABLED__DEFAULT}) }
{_GUI_FILTER_BOX_STUFF ({FONT_SIZE})}
[/draw]
[/state_disabled]
[state_focused]
[draw]
{BACKGROUND_ENABLED}
{_GUI_DRAW_BORDER ({GUI__BORDER_COLOR}) }
{_GUI_DRAW_TEXT ({FONT_SIZE}) ({GUI__FONT_COLOR_ENABLED__DEFAULT}) }
{_GUI_DRAW_CURSOR ({X_OFFSET}) }
[/draw]
[/state_focused]
[/resolution]
#enddef
[text_box_definition]
id = "filter"
description = "Filter text box"
{_GUI_RESOLUTION_FILTER () 40 250 25 5 10 ({GUI_FONT_SIZE_DEFAULT}) ({_GUI_TEXTBOX_BACKGROUND_ENABLED}) ({_GUI_TEXTBOX_BACKGROUND_DISABLED})}
[/text_box_definition]
#undef _GUI_RESOLUTION
#undef _GUI_RESOLUTION_FILTER
#undef _GUI_FILTER_BOX_STUFF
#undef _GUI_DRAW_CURSOR
#undef _GUI_DRAW_TEXT
#undef _GUI_DRAW_BORDER
#undef _GUI_DRAW_BACKGROUND
#undef _GUI_TEXTBOX_BACKGROUND_ENABLED
#undef _GUI_TEXTBOX_BACKGROUND_DISABLED
#undef __DISABLED_COLOR_PREPROCESSOR_WORKAROUND

View file

@ -689,9 +689,10 @@
[text_box]
id = "filter"
definition = "filter"
definition = "default"
tooltip = _ "Filters on addon description, version, type or author."
{FITER_TEXT_BOX_HINT}
[/text_box]
[/column]

View file

@ -129,7 +129,8 @@
[text_box]
id = "filter"
definition = "filter"
definition = "default"
{FITER_TEXT_BOX_HINT}
[/text_box]
[/column]

View file

@ -416,7 +416,8 @@
[text_box]
id = "txtFilter"
definition = "filter"
definition = "default"
{FITER_TEXT_BOX_HINT}
[/text_box]
[/column]

View file

@ -403,8 +403,9 @@
border_size = 5
[text_box]
definition = "filter"
definition = "default"
id = "filter_text"
{FITER_TEXT_BOX_HINT}
[/text_box]
[/column]

View file

@ -651,7 +651,8 @@
horizontal_grow = true
[text_box]
id = "game_filter"
definition = "filter"
definition = "default"
{FITER_TEXT_BOX_HINT}
[/text_box]
[/column]

View file

@ -90,7 +90,8 @@
[text_box]
id = "filter_box"
definition = "filter"
definition = "default"
{FITER_TEXT_BOX_HINT}
[/text_box]
[/column]

View file

@ -337,7 +337,8 @@
[text_box]
id = "filter_box"
definition = "filter"
definition = "default"
{FITER_TEXT_BOX_HINT}
[/text_box]
[/column]

View file

@ -210,6 +210,9 @@ void text_box::update_canvas()
tmp.set_variable("composition_offset", wfl::variant(comp_start_offset));
tmp.set_variable("composition_width", wfl::variant(comp_end_offset - comp_start_offset));
tmp.set_variable("hint_text", wfl::variant(hint_text_));
tmp.set_variable("hint_image", wfl::variant(hint_image_));
}
}
@ -478,6 +481,8 @@ builder_text_box::builder_text_box(const config& cfg)
: builder_styled_widget(cfg)
, history(cfg["history"])
, max_input_length(cfg["max_input_length"])
, hint_text(cfg["hint_text"])
, hint_image(cfg["hint_image"])
{
}
@ -493,6 +498,7 @@ widget* builder_text_box::build() const
}
widget->set_max_input_length(max_input_length);
widget->set_hint_data(hint_text, hint_image);
DBG_GUI_G << "Window builder: placed text box '" << id
<< "' with definition '" << definition << "'.\n";

View file

@ -143,6 +143,14 @@ public:
max_input_length_ = length;
}
void set_hint_data(const std::string& text, const std::string& image)
{
hint_text_ = text;
hint_image_ = image;
update_canvas();
}
void clear()
{
set_value("");
@ -214,6 +222,12 @@ private:
/** Is the mouse in dragging mode, this affects selection in mouse move */
bool dragging_;
/** Helper text to display (such as "Search") if the text box is empty. */
std::string hint_text_;
/** Image (such as a magnifying glass) that accompanies the help text. */
std::string hint_image_;
/**
* Inherited from text_box_base.
*
@ -309,6 +323,9 @@ public:
std::string history;
std::size_t max_input_length;
std::string hint_text;
std::string hint_image;
};
} // namespace implementation