Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add num_box widget for numerical input #117

Closed
wants to merge 4 commits into from

1 participant

@aquileia
Collaborator

No description provided.

aquileia added some commits
@aquileia aquileia update changelog 43e4da8
@aquileia aquileia Register num_box for compilation, syntax & wiki
add a missing wiki entry for password_box as well
21634af
@aquileia aquileia delete an unnecessary comment in password_box.hpp 458762e
@aquileia aquileia new GUI2 widget: num_box
This widget implements the integer_selector interface and can
therefore
directly replace sliders.

In text.hpp, the private variable
'text_' had to be changed to protected
status to access it in num_box.
54e96de
@aquileia
Collaborator

f3bfa19 accepted by lipkab
bb6bfdc accepted by Ivanovic

@aquileia
Collaborator

The first four commits were already merged thanks to Soliton, I will update the pull request as soon as I fix some issues with 54e96de.

@aquileia aquileia closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 15, 2014
  1. @aquileia

    update changelog

    aquileia authored
  2. @aquileia

    Register num_box for compilation, syntax & wiki

    aquileia authored
    add a missing wiki entry for password_box as well
  3. @aquileia
  4. @aquileia

    new GUI2 widget: num_box

    aquileia authored
    This widget implements the integer_selector interface and can
    therefore
    directly replace sliders.
    
    In text.hpp, the private variable
    'text_' had to be changed to protected
    status to access it in num_box.
This page is out of date. Refresh to see the latest.
View
3  changelog
@@ -16,6 +16,8 @@ Version 1.13.0-dev:
* Units:
* Increased the experience requirement for the Rami from 32 to 39
* Increased the experience requirement for the Saree from 56 to 64
+ * GUI
+ * add a number box widget for numerical input
* Miscellaneous and bug fixes:
* Fix Fisher-Yates implemenation of Lua helper.shuffle (bug #21706)
* Fixed issues with the ~BG() image path function not always working with
@@ -23,6 +25,7 @@ Version 1.13.0-dev:
* Idle controlled sides now serialize as human controlled.
* Fixed bug #20876: A segfault in cut_surface.
* Changed: Dropped support for AmigaOS, BeOS, and OS/2.
+ * update syntax highlighting of GUI tags for Vim
Version 1.11.11:
* Add-ons server:
View
26 data/gui/schema.cfg
@@ -1628,6 +1628,32 @@
[/key]
[/tag]
[tag]
+ name="num_box"
+ min="0"
+ max="-1"
+ super="generic/widget_instance"
+ [key]
+ name="maximum_value"
+ type="int"
+ default=32767
+ [/key]
+ [key]
+ name="minimum_value"
+ type="int"
+ default=0
+ [/key]
+ [key]
+ name="value"
+ type="int"
+ default=0
+ [/key]
+ [key]
+ name="history"
+ type="string"
+ default=""
+ [/key]
+ [/tag]
+ [tag]
name="progress_bar"
min="0"
max="-1"
View
4 data/tools/emacs_mode/wesnoth-wml-data.el
@@ -22,8 +22,8 @@
("resolution")
nil)
("column"
- ("drawing" "matrix" "window" "tree_view" "slider" "minimap" "password_box" "progress_bar" "stacked_widget" "multi_page" "listbox" "toggle_button" "text_box" "scroll_label" "panel" "image" "toggle_panel" "viewport" "label" "button" "grid" "scrollbar_panel" "spacer" "horizontal_scrollbar" "vertical_scrollbar" "repeating_button")
- ("_grid" "_vertical_scrollbar" "_tree_view" "_toggle_panel" "_toggle_button" "_password_box" "_text_box" "_stacked_widget" "_spacer" "_slider" "_scrollbar_panel" "_scroll_label" "_repeating_button" "_panel" "_multi_page" "_minimap" "_menubar" "_listbox" "_label" "_image" "_horizontal_scrollbar" "_horizontal_listbox" "_button" "vertical_grow" "horizontal_grow" "border_size" "horizontal_alignment" "border" "grow_factor" "vertical_alignment"))
+ ("drawing" "matrix" "window" "tree_view" "slider" "minimap" "password_box" "num_box" "progress_bar" "stacked_widget" "multi_page" "listbox" "toggle_button" "text_box" "scroll_label" "panel" "image" "toggle_panel" "viewport" "label" "button" "grid" "scrollbar_panel" "spacer" "horizontal_scrollbar" "vertical_scrollbar" "repeating_button")
+ ("_grid" "_vertical_scrollbar" "_tree_view" "_toggle_panel" "_toggle_button" "_password_box" "_num_box" "_text_box" "_stacked_widget" "_spacer" "_slider" "_scrollbar_panel" "_scroll_label" "_repeating_button" "_panel" "_multi_page" "_minimap" "_menubar" "_listbox" "_label" "_image" "_horizontal_scrollbar" "_horizontal_listbox" "_button" "vertical_grow" "horizontal_grow" "border_size" "horizontal_alignment" "border" "grow_factor" "vertical_alignment"))
("row"
("grid" "toggle_button" "text_box" "row" "column")
("_column" "grow_factor"))
View
112 projectfiles/VC9/wesnoth.vcproj
@@ -5767,6 +5767,62 @@
>
</File>
<File
+ RelativePath="..\..\src\gui\widgets\num_box.cpp"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\Gui\Widgets\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\Gui\Widgets\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Debug_with_VLD|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\Gui\Widgets\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Test_Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\Gui\Widgets\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Test_Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\Gui\Widgets\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="ReleaseDEBUG|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\Gui\Widgets\"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath="..\..\src\gui\widgets\num_box.hpp"
+ >
+ </File>
+ <File
RelativePath="..\..\src\gui\widgets\pane.cpp"
>
<FileConfiguration
@@ -11322,6 +11378,62 @@
>
</File>
<File
+ RelativePath="..\..\src\gui\auxiliary\window_builder\num_box.cpp"
+ >
+ <FileConfiguration
+ Name="Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\Gui\Auxiliary\Window_Builder\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\Gui\Auxiliary\Window_Builder\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Debug_with_VLD|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\Gui\Auxiliary\Window_Builder\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Test_Debug|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\Gui\Auxiliary\Window_Builder\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="Test_Release|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\Gui\Auxiliary\Window_Builder\"
+ />
+ </FileConfiguration>
+ <FileConfiguration
+ Name="ReleaseDEBUG|Win32"
+ >
+ <Tool
+ Name="VCCLCompilerTool"
+ ObjectFile="$(IntDir)\Gui\Auxiliary\Window_Builder\"
+ />
+ </FileConfiguration>
+ </File>
+ <File
+ RelativePath="..\..\src\gui\auxiliary\window_builder\num_box.hpp"
+ >
+ </File>
+ <File
RelativePath="..\..\src\gui\auxiliary\window_builder\pane.cpp"
>
<FileConfiguration
View
2  src/CMakeLists.txt
@@ -481,6 +481,7 @@ set(wesnoth-gui_widget_SRC
gui/widgets/matrix.cpp
gui/widgets/minimap.cpp
gui/widgets/multi_page.cpp
+ gui/widgets/num_box.cpp
gui/widgets/pane.cpp
gui/widgets/panel.cpp
gui/widgets/password_box.cpp
@@ -518,6 +519,7 @@ set(wesnoth-gui_widget_SRC
gui/auxiliary/window_builder/matrix.cpp
gui/auxiliary/window_builder/minimap.cpp
gui/auxiliary/window_builder/multi_page.cpp
+ gui/auxiliary/window_builder/num_box.cpp
gui/auxiliary/window_builder/pane.cpp
gui/auxiliary/window_builder/panel.cpp
gui/auxiliary/window_builder/password_box.cpp
View
2  src/SConscript
@@ -328,6 +328,7 @@ wesnoth_sources = Split("""
gui/auxiliary/window_builder/matrix.cpp
gui/auxiliary/window_builder/minimap.cpp
gui/auxiliary/window_builder/multi_page.cpp
+ gui/auxiliary/window_builder/num_box.cpp
gui/auxiliary/window_builder/pane.cpp
gui/auxiliary/window_builder/panel.cpp
gui/auxiliary/window_builder/password_box.cpp
@@ -416,6 +417,7 @@ wesnoth_sources = Split("""
gui/widgets/matrix.cpp
gui/widgets/minimap.cpp
gui/widgets/multi_page.cpp
+ gui/widgets/num_box.cpp
gui/widgets/pane.cpp
gui/widgets/panel.cpp
gui/widgets/password_box.cpp
View
2  src/gui/auxiliary/window_builder.cpp
@@ -37,6 +37,7 @@
#include "gui/auxiliary/window_builder/drawing.hpp"
#include "gui/auxiliary/window_builder/pane.hpp"
#include "gui/auxiliary/window_builder/password_box.hpp"
+#include "gui/auxiliary/window_builder/num_box.hpp"
#include "gui/auxiliary/window_builder/viewport.hpp"
#endif
#include "gui/auxiliary/window_builder/instance.hpp"
@@ -219,6 +220,7 @@ tbuilder_widget_ptr create_builder_widget(const config& cfg)
TRY(button);
TRY(drawing);
TRY(password_box);
+ TRY(num_box);
#undef TRY
#endif
View
80 src/gui/auxiliary/window_builder/num_box.cpp
@@ -0,0 +1,80 @@
+/*
+ Copyright (C) 2014 by
+ Part of the Battle for Wesnoth Project http://www.wesnoth.org/
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY.
+
+ See the COPYING file for more details.
+*/
+
+#define GETTEXT_DOMAIN "wesnoth-lib"
+
+#include "gui/auxiliary/window_builder/num_box.hpp"
+
+#include "config.hpp"
+#include "gui/auxiliary/log.hpp"
+#include "gui/widgets/num_box.hpp"
+
+
+namespace gui2
+{
+
+namespace implementation
+{
+
+tbuilder_num_box::tbuilder_num_box(const config& cfg)
+ : tbuilder_control(cfg)
+ , history_(cfg["history"])
+ , minimum_value_(cfg["minimum_value"])
+ , maximum_value_(cfg["maximum_value"])
+ , value_(cfg["value"])
+{
+}
+
+twidget* tbuilder_num_box::build() const
+{
+ tnum_box* widget = new tnum_box();
+
+ init_control(widget);
+
+ // set keys for numerical interface
+ widget->set_maximum_value(maximum_value_);
+ widget->set_minimum_value(minimum_value_);
+ widget->set_value(value_);
+
+ if(!history_.empty()) {
+ widget->set_history(history_);
+ }
+
+ DBG_GUI_G << "Window builder: placed number box '" << id
+ << "' with definition '" << definition << "'.\n";
+
+ return widget;
+}
+
+} // namespace implementation
+
+} // namespace gui2
+
+/*WIKI
+ * @page = GUIWidgetInstanceWML
+ * @order = 2_num_box
+ *
+ * == Number box ==
+ * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
+ * @begin{tag}{name="num_box"}{min="0"}{max="-1"}{super="generic/widget_instance"}
+ * @begin{table}{config}
+ * history & string & "" & See the description of the text box. $
+ *
+ * minimum_value & int & 0 & The minimum value the number box can hold. $
+ * maximum_value & int & 0 & The maximum value the number box can hold. $
+ * value & int & 0 & The value of the number box. $
+ * @end{table}
+ * @end{tag}{name="num_box"}
+ * @end{parent}{name="gui/window/resolution/grid/row/column/"}
+ */
View
46 src/gui/auxiliary/window_builder/num_box.hpp
@@ -0,0 +1,46 @@
+/*
+ Copyright (C) 2008 - 2014 by Mark de Wever <koraq@xs4all.nl>
+ Part of the Battle for Wesnoth Project http://www.wesnoth.org/
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY.
+
+ See the COPYING file for more details.
+*/
+
+#ifndef GUI_AUXILIARY_WINDOW_BUILDER_NUM_BOX_HPP_INCLUDED
+#define GUI_AUXILIARY_WINDOW_BUILDER_NUM_BOX_HPP_INCLUDED
+
+#include "gui/auxiliary/window_builder/control.hpp"
+
+namespace gui2
+{
+
+namespace implementation
+{
+
+struct tbuilder_num_box : public tbuilder_control
+{
+public:
+ explicit tbuilder_num_box(const config& cfg);
+
+ using tbuilder_control::build;
+
+ twidget* build() const;
+
+private:
+ std::string history_;
+ int minimum_value_;
+ int maximum_value_;
+ int value_;
+};
+
+} // namespace implementation
+
+} // namespace gui2
+
+#endif
View
134 src/gui/widgets/num_box.cpp
@@ -0,0 +1,134 @@
+/*
+ Copyright (C) 2014 by
+ Part of the Battle for Wesnoth Project http://www.wesnoth.org/
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY.
+
+ See the COPYING file for more details.
+*/
+
+#define GETTEXT_DOMAIN "wesnoth-lib"
+
+#include "gui/widgets/num_box.hpp"
+
+#include "gui/auxiliary/widget_definition/text_box.hpp"
+#include "gui/auxiliary/window_builder/num_box.hpp"
+#include "gui/widgets/detail/register.tpp"
+#include "gui/widgets/settings.hpp"
+#include "clipboard.hpp"
+#include "serialization/string_utils.hpp"
+
+#include <boost/bind.hpp>
+#include <boost/algorithm/string/trim.hpp>
+#include <boost/lexical_cast.hpp>
+
+namespace gui2 {
+
+REGISTER_WIDGET3(ttext_box_definition, num_box, "text_box_definition")
+
+void tnum_box::validate(const int value, const int cursor_shift)
+{
+ if (value > get_maximum_value()) {
+ if (set_validated(get_maximum_value() , cursor_shift)) {
+ return;
+ }
+ } else if (value < get_minimum_value()) {
+ if (set_validated(get_minimum_value() , cursor_shift)) {
+ return;
+ }
+ } else {
+ if (set_validated(value, cursor_shift)) {
+ return;
+ }
+ }
+ if (cursor_shift) {
+ set_cursor(get_selection_start() + cursor_shift, false);
+ }
+ ttext_box::set_value(boost::lexical_cast<std::string>(get_value()));
+}
+
+void tnum_box::set_value(const std::string& text)
+{
+ try {
+ validate(boost::lexical_cast<int>(text));
+ }
+ catch (boost::bad_lexical_cast) { }
+}
+
+void tnum_box::insert_char(const Uint16 unicode)
+{
+ std::string restore = get_text();
+ delete_selection();
+ if (!text_.insert_unicode(get_selection_start(), unicode)) {
+ return;
+ }
+ try {
+ validate(boost::lexical_cast<int>(get_text()) , 1);
+ }
+ catch (boost::bad_lexical_cast) {
+ ttext_::set_value(restore);
+ }
+}
+
+void tnum_box::paste_selection(const bool mouse)
+{
+ std::string paste = copy_from_clipboard(mouse);
+
+ // trim leading and trailing whitespaces
+ boost::algorithm::trim(paste);
+ // is something left?
+ if(paste.empty()) return;
+ std::string restore = get_text();
+ delete_selection();
+ unsigned len = text_.insert_text(get_selection_start(), paste);
+ try {
+ validate(boost::lexical_cast<int>(get_text()), len);
+ }
+ catch (boost::bad_lexical_cast) {
+ ttext_::set_value(restore);
+ }
+}
+
+
+void tnum_box::delete_char(const bool before_cursor)
+{
+ ttext_box::delete_char(before_cursor);
+ validate(boost::lexical_cast<int>(get_text()));
+}
+
+void tnum_box::handle_key_backspace(SDLMod /*modifier*/, bool& handled)
+{
+ handled = true;
+ if (get_selection_length() != 0) {
+ delete_selection();
+ }
+ else if (get_selection_start()) {
+ delete_char(true);
+ }
+ validate(boost::lexical_cast<int>(get_text()));
+}
+
+void tnum_box::handle_key_delete(SDLMod /*modifier*/, bool& handled)
+{
+ handled = true;
+ if (get_selection_length() != 0) {
+ delete_selection();
+ }
+ else if (get_selection_start() < utils::string_to_wstring(get_text()).size()) {
+ delete_char(false);
+ }
+ validate(boost::lexical_cast<int>(get_text()));
+}
+
+const std::string& tnum_box::get_control_type() const
+{
+ static const std::string type = "num_box";
+ return type;
+}
+
+} // namespace gui2
View
120 src/gui/widgets/num_box.hpp
@@ -0,0 +1,120 @@
+/*
+ Copyright (C) 2014 by
+ Part of the Battle for Wesnoth Project http://www.wesnoth.org/
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY.
+
+ See the COPYING file for more details.
+*/
+
+#ifndef GUI_WIDGETS_NUM_BOX_HPP_INCLUDED
+#define GUI_WIDGETS_NUM_BOX_HPP_INCLUDED
+
+#include "gui/widgets/integer_selector.hpp"
+#include "gui/widgets/text_box.hpp"
+
+
+/**
+ * A class inherited from ttext_box that only takes numerical values
+ */
+namespace gui2
+{
+
+class tnum_box : public ttext_box, tinteger_selector_
+{
+public:
+ tnum_box() : ttext_box(), value_(0), minimum_value_(0), maximum_value_(32767)
+ {
+ }
+
+ /** Inherited from ttext_. */
+ void set_value(const std::string& text);
+
+ void set_value(const int value)
+ {
+ validate(value);
+ }
+
+ int get_value() const
+ {
+ return value_;
+ }
+
+ std::string get_text() const
+ {
+ return ttext_box::get_value();
+ }
+
+ void set_minimum_value(const int minimum_value)
+ {
+ minimum_value_ = minimum_value;
+ if (minimum_value < get_value()) {
+ validate(minimum_value);
+ }
+ }
+
+ int get_minimum_value() const
+ {
+ return minimum_value_;
+ }
+
+ void set_maximum_value(const int maximum_value)
+ {
+ maximum_value_ = maximum_value;
+ if (maximum_value > get_value()) {
+ validate(maximum_value);
+ }
+ }
+
+ int get_maximum_value() const
+ {
+ return maximum_value_;
+ }
+
+
+protected:
+ void insert_char(const Uint16 unicode);
+ void delete_char(const bool before_cursor);
+ void paste_selection(const bool mouse);
+
+ // optimization, especially for increment/decrement at bonds
+ bool set_validated(const int value, const int cursor_shift)
+ {
+ if (value_ == value && !cursor_shift) {
+ return false;
+ }
+ value_ = value;
+ return true;
+ }
+
+private:
+ void handle_key_backspace(SDLMod modifier, bool& handled);
+ void handle_key_delete(SDLMod modifier, bool& handled);
+
+ void handle_key_up_arrow(SDLMod /*modifier*/, bool& handled) {
+ handled = true;
+ validate(get_value()+1);
+ }
+ void handle_key_down_arrow(SDLMod /*modifier*/, bool& handled) {
+ handled = true;
+ validate(get_value()-1);
+ }
+
+ int value_;
+ int minimum_value_;
+ int maximum_value_;
+
+ void validate(const int value, const int cursor_shift = 0);
+
+ /** See @ref tcontrol::get_control_type. */
+ virtual const std::string& get_control_type() const OVERRIDE;
+};
+
+} // namespace gui2
+
+#endif
View
1  src/gui/widgets/password_box.hpp
@@ -65,7 +65,6 @@ class tpassword_box : public ttext_box
protected:
- // Overwritten functions must of course be virtual!
void insert_char(const Uint16 unicode);
void delete_char(const bool before_cursor);
View
2  src/gui/widgets/settings.cpp
@@ -207,7 +207,9 @@ struct tgui_definition
* Listbox & @macro = listbox_description $
* Minimap & @macro = minimap_description $
* Multi_page & @macro = multi_page_description $
+ * Num_box & A text box that converts the input to an integer. $
* Panel & @macro = panel_description $
+ * Password_box & A text box masking it's content by asterisks. $
* Repeating_button & @macro = repeating_button_description $
* Scroll_label & @macro = scroll_label_description $
* Slider & @macro = slider_description $
View
2  src/gui/widgets/text.cpp
@@ -30,8 +30,8 @@ namespace gui2
ttext_::ttext_()
: tcontrol(COUNT)
- , state_(ENABLED)
, text_()
+ , state_(ENABLED)
, selection_start_(0)
, selection_length_(0)
, text_changed_callback_()
View
6 src/gui/widgets/text.hpp
@@ -91,6 +91,9 @@ class ttext_ : public tcontrol
}
protected:
+ /** The text entered in the widget. */
+ font::ttext text_;
+
/**
* Moves the cursor to the end of the line.
*
@@ -248,9 +251,6 @@ class ttext_ : public tcontrol
*/
tstate state_;
- /** The text entered in the widget. */
- font::ttext text_;
-
/** Start of the selected text. */
size_t selection_start_;
Something went wrong with that request. Please try again.