Gamedev Framework (gf)  0.11.0
A C++14 framework for 2D games
UI.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_UI_H
22 #define GF_UI_H
23 
24 #include <cstdint>
25 #include <memory>
26 
27 #include "ArrayRef.h"
28 #include "BufferRef.h"
29 #include "Drawable.h"
30 #include "Event.h"
31 #include "Flags.h"
32 #include "Font.h"
33 #include "Path.h"
34 #include "Portability.h"
35 #include "StringRef.h"
36 #include "Types.h"
37 #include "Vector.h"
38 
39 namespace gf {
40 #ifndef DOXYGEN_SHOULD_SKIP_THIS
41 inline namespace v1 {
42 #endif
43 
44  class Texture;
45 
52  enum class UIWindow : uint32_t {
53  Border = 0x0001,
54  Movable = 0x0002,
55  Scalable = 0x0004,
56  Closable = 0x0008,
57  Minimizable = 0x0010,
58  NoScrollbar = 0x0020,
59  Title = 0x0040,
60  ScrollAutoHide = 0x0080,
61  Background = 0x0100,
62  ScaleLeft = 0x0200,
63  NoInput = 0x0400,
64  };
65 
73 
78  enum class UICollapse : bool {
79  Minimized = false,
80  Maximized = true,
81  };
82 
87  enum class UILayout {
88  Dynamic = 0,
89  Static = 1,
90  };
91 
96  enum class UITree {
97  Node = 0,
98  Tab = 1,
99  };
100 
107  enum class UIAlignment {
108  Left = 0x10 | 0x01,
109  Center = 0x10 | 0x02,
110  Right = 0x10 | 0x04,
111  };
112 
118 
123  enum class UIButtonBehavior {
124  Default = 0,
125  Repeater = 1,
126  };
127 
132  enum class UIProgress : bool {
133  Fixed = false,
134  Modifyable = true,
135  };
136 
141  enum class UISymbol {
142  None,
143  X,
144  Underscore,
145  CircleSolid,
146  CircleOutline,
147  RectSolid,
148  RectOutline,
149  TriangleUp,
150  TriangleDown,
151  TriangleLeft,
152  TriangleRight,
153  Plus,
154  Minus,
155  };
156 
161  enum class UIEdit : uint32_t {
162  Default = 0x0000,
163  ReadOnly = 0x0001,
164  AutoSelect = 0x0002,
165  SigEnter = 0x0004,
166  AllowTab = 0x0008,
167  NoCursor = 0x0010,
168  Selectable = 0x0020,
169  Clipboard = 0x0040,
170  CtrlEnterNewline = 0x0080,
171  NoHorizontalScroll = 0x0100,
172  AlwaysInsertMode = 0x0200,
173  Multiline = 0x0400,
174  GotoEndOnActivate = 0x0800,
175  };
176 
184 
189  struct GF_API UIEditType {
190  static const UIEditFlags Simple;
191  static const UIEditFlags Field;
192  static const UIEditFlags Box;
193  static const UIEditFlags Editor;
194  };
195 
200  enum class UIEditEvent : uint32_t {
201  Active = 0x0001,
202  Inactive = 0x0002,
203  Activated = 0x0004,
204  Deactivated = 0x0008,
205  Commited = 0x0010,
206  };
207 
215 
220  enum class UIEditFilter {
221  Default,
222  Ascii,
223  Float,
224  Decimal,
225  Hex,
226  Oct,
227  Binary,
228  };
229 
236  struct UIBrowser {
239  };
240 
245  enum class UIPopup {
246  Static = 0,
247  Dynamic = 1,
248  };
249 
254  enum class UIPredefinedStyle {
255  Default,
256  White,
257  Red,
258  Blue,
259  Dark,
260  };
261 
410  class GF_API UI : public Drawable {
411  public:
415  static constexpr unsigned DefaultCharacterSize = 13;
416 
423  UI(Font& font, unsigned characterSize = DefaultCharacterSize);
424 
428  ~UI();
429 
433  UI(const UI&) = delete;
434 
438  UI& operator=(const UI&) = delete;
439 
443  UI(UI&& other) noexcept;
444 
448  UI& operator=(UI&& other) noexcept;
449 
457  void processEvent(const Event& event);
458 
464  void setCharacterSize(unsigned characterSize);
465 
483  bool begin(const std::string& title, const RectF& bounds, UIWindowFlags flags = None);
484 
492  void end();
493 
501  RectF windowGetBounds();
502 
520  void layoutRowDynamic(float height, int cols);
521 
531  void layoutRowStatic(float height, int itemWidth, int cols);
532 
542  void layoutRowBegin(UILayout format, float height, int cols);
543 
551  void layoutRowPush(float width);
552 
558  void layoutRowEnd();
559 
567  void layoutRow(UILayout format, float height, ArrayRef<float> ratio);
568 
576  void separator(float height);
577 
601  bool groupBegin(const std::string& title, UIWindowFlags flags = None);
602 
610  void groupEnd();
611 
625  bool groupScrolledBegin(UIScroll& scroll, const std::string& title, UIWindowFlags flags = None);
626 
632  void groupScrolledEnd();
633 
655  bool treePush(UITree type, const std::string& title, UICollapse& state);
656 
664  void treePop();
665 
683  void label(StringRef title, UIAlignment align = UIAlignment::Left);
684 
694  void labelColored(const Color4f& color, StringRef title, UIAlignment align = UIAlignment::Left);
695 
703  void labelWrap(StringRef title);
704 
713  void labelWrapColored(const Color4f& color, StringRef title);
714 
721  void image(const Texture& texture, const RectF& textureRect);
722 
739  void buttonSetBehavior(UIButtonBehavior behavior);
740 
748  bool buttonPushBehavior(UIButtonBehavior behavior);
749 
755  bool buttonPopBehavior();
756 
765  bool buttonLabel(StringRef title);
766 
775  bool buttonColor(const Color4f& color);
776 
785  bool buttonSymbol(UISymbol symbol);
786 
797  bool buttonSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
798 
817  bool checkbox(StringRef title, bool& active);
818 
829  bool checkboxFlags(StringRef title, unsigned& flags, unsigned value);
830 
849  bool option(StringRef title, bool active);
850 
860  bool radio(StringRef title, bool& active);
861 
881  bool selectableLabel(StringRef title, UIAlignment align, bool& value);
882 
903  bool sliderFloat(float min, float& val, float max, float step);
904 
916  bool sliderInt(int min, int& val, int max, int step);
917 
935  bool progress(std::size_t& current, std::size_t max, UIProgress modifyable = UIProgress::Modifyable);
936 
952  bool colorPicker(Color4f& color);
953 
975  void propertyInt(const std::string& name, int min, int& val, int max, int step, float incPerPixel);
976 
989  void propertyFloat(const std::string& name, float min, float& val, float max, float step, float incPerPixel);
990 
1003  void propertyDouble(const std::string& name, double min, double& val, double max, double step, float incPerPixel);
1004 
1014  UIEditEventFlags edit(UIEditFlags flags, BufferRef<char> buffer, std::size_t& length, UIEditFilter filter = UIEditFilter::Default);
1015 
1033  bool fileSelector(UIBrowser& browser, const std::string& title, const RectF& bounds);
1034 
1055  bool popupBegin(UIPopup type, const std::string& title, UIWindowFlags flags, const RectF& bounds);
1056 
1062  void popupClose();
1063 
1069  void popupEnd();
1070 
1090  void combobox(const std::vector<std::string>& items, int& selected, int itemHeight, Vector2f size);
1091 
1103  void comboboxSeparator(const std::string& itemsSeparatedBySeparator, char separator, int& selected, int itemHeight, Vector2f size);
1104 
1114  bool comboBeginLabel(StringRef selected, Vector2f size);
1115 
1125  bool comboBeginColor(const Color4f& color, Vector2f size);
1126 
1136  bool comboBeginSymbol(UISymbol symbol, Vector2f size);
1137 
1148  bool comboBeginSymbolLabel(UISymbol symbol, StringRef selected, Vector2f size);
1149 
1157  bool comboItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1158 
1167  bool comboItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1168 
1174  void comboClose();
1175 
1181  void comboEnd();
1182 
1202  bool contextualBegin(UIWindowFlags flags, Vector2f size, const RectF& triggerBounds);
1203 
1211  bool contextualItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1212 
1221  bool contextualItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1222 
1228  void contextualClose();
1229 
1235  void contextualEnd();
1236 
1251  void tooltip(const std::string& text);
1252 
1261  bool tooltipBegin(float width);
1262 
1268  void tooltipEnd();
1269 
1284  void menubarBegin();
1285 
1291  void menubarEnd();
1292 
1303  bool menuBeginLabel(StringRef title, UIAlignment align, Vector2f size);
1304 
1315  bool menuBeginSymbol(const std::string& id, UISymbol symbol, Vector2f size);
1316 
1328  bool menuBeginSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align, Vector2f size);
1329 
1337  bool menuItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1338 
1347  bool menuItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1348 
1354  void menuClose();
1355 
1361  void menuEnd();
1362 
1377  RectF getWidgetBounds();
1378 
1386  bool isWidgetHovered();
1387 
1393  void spacing(int cols);
1394 
1411  void setPredefinedStyle(UIPredefinedStyle style);
1412 
1417  virtual void draw(RenderTarget &target, const RenderStates& states) override;
1418 
1419  private:
1420  enum class State {
1421  Start,
1422  Input,
1423  Setup,
1424  Draw,
1425  };
1426 
1427  void setState(State state);
1428 
1429  private:
1430  struct UIImpl;
1431 
1432  std::unique_ptr<UIImpl> m_impl;
1433  };
1434 
1435 
1436 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1437 }
1438 #endif
1439 
1440 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1441 template<>
1442 struct EnableBitmaskOperators<UIWindow> {
1443  static constexpr bool value = true;
1444 };
1445 
1446 template<>
1447 struct EnableBitmaskOperators<UIEdit> {
1448  static constexpr bool value = true;
1449 };
1450 
1451 template<>
1452 struct EnableBitmaskOperators<UIEditEvent> {
1453  static constexpr bool value = true;
1454 };
1455 #endif
1456 
1457 }
1458 
1459 #endif // GF_UI_H
The window&#39;s scrollbar can hide automatically.
The window can be moved by the user.
The row has a dynamic layout.
The row has a static layout.
UILayout
Layout property for rows.
Definition: UI.h:87
Edit widget is not active and is not being modified.
The button is active once.
Edit widget is currently being modified.
UIEditEvent
Properties for edit events.
Definition: UI.h:200
No alignement.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
UIPopup
Type of popup.
Definition: UI.h:245
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
Bitfield relying on an enumeration.
Definition: Flags.h:68
UIEditFilter
Filters for edit.
Definition: UI.h:220
UIProgress
State of the progress bar.
Definition: UI.h:132
static const UIEditFlags Field
Definition: UI.h:191
The button is active as long as it is pressed.
The tree is maximized.
UIPredefinedStyle
A predefined style.
Definition: UI.h:254
Context for an immediate mode graphical interface.
Definition: UI.h:410
Path currentPath
The current path for searching.
Definition: UI.h:237
The window is scalable by the user.
A reference to a modifiable buffer and its size.
Definition: BufferRef.h:43
Abstract base class for objects that can be drawn to a render window.
Definition: Drawable.h:79
The window has a border.
A red and gray style.
Edit widget has received an enter and lost focus.
Solid rectangle.
UIAlignment
The alignment of the text.
Definition: UI.h:107
A texture for colored images.
Definition: Texture.h:339
The Start button.
static const UIEditFlags Box
Definition: UI.h:192
UIButtonBehavior
Behavior for buttons.
Definition: UI.h:123
Vector< unsigned, 2 > Vector2u
A unsigned vector with 2 components.
Definition: Vector.h:1179
Data for file selector.
Definition: UI.h:236
The window can be closed with an icon in the header.
gf::Clipboard provides an interface for getting and setting the contents of the system clipboard...
Definition: Clipboard.h:67
The window can not scale, move or get focus.
The namespace for gf classes.
Definition: Action.h:35
A blue and light gray style.
A constant reference to an array and its size.
Definition: ArrayRef.h:42
A character font.
Definition: Font.h:130
Outline rectangle.
The progress bar can be modified.
static const UIEditFlags Simple
Definition: UI.h:190
static const UIEditFlags Editor
Definition: UI.h:193
The X button.
The tree is minimized.
UICollapse
Collapse property of a tree.
Definition: UI.h:78
A light gray style.
UIEdit
Properties for edit widgets.
Definition: UI.h:161
Edit widget went from state inactive to state active.
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:44
The progress bar is fixed.
Predefined flags for edit.
Definition: UI.h:189
A constant reference to a string and its size.
Definition: StringRef.h:41
UISymbol
A representative symbol.
Definition: UI.h:141
Left alignment.
Left alignement.
The window is in the background.
The window has no scrollbar.
UITree
The type of tree.
Definition: UI.h:96
Right alignement.
The window&#39;s scaler is on the left.
Defines a system event and its parameters.
Definition: Event.h:118
UIWindow
Properties for windows and window-like elements.
Definition: UI.h:52
A dark gray and dark blue style.
The window can be minimized with an icon in the header.
Centered alignment.
Edit widget went from state active to state inactive.
Path selectedPath
The selected path.
Definition: UI.h:238
The window has a title in the header.