Gamedev Framework (gf)  0.15.0
A C++14 framework for 2D games
UI.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 
268  class GF_API UICharBuffer {
269  public:
270  UICharBuffer(std::size_t capacity);
271  ~UICharBuffer();
272 
273  UICharBuffer(const UICharBuffer&) = delete;
274  UICharBuffer& operator=(const UICharBuffer&) = delete;
275 
276  UICharBuffer(UICharBuffer&& other) noexcept;
277  UICharBuffer& operator=(UICharBuffer&& other) noexcept;
278 
279  std::string asString() const {
280  return std::string(m_data, m_length);
281  }
282 
284  return StringRef(m_data, m_length);
285  }
286 
287  void clear();
288  void append(const UICharBuffer& other);
289  void append(const std::string& other);
290 
291  private:
292  friend class UI;
293 
294  char *m_data;
295  std::size_t m_length;
296  std::size_t m_capacity;
297  };
298 
447  class GF_API UI : public Drawable {
448  public:
452  static constexpr unsigned DefaultCharacterSize = 13;
453 
460  UI(Font& font, unsigned characterSize = DefaultCharacterSize);
461 
465  ~UI();
466 
470  UI(const UI&) = delete;
471 
475  UI& operator=(const UI&) = delete;
476 
480  UI(UI&& other) noexcept;
481 
485  UI& operator=(UI&& other) noexcept;
486 
494  void processEvent(const Event& event);
495 
501  void setCharacterSize(unsigned characterSize);
502 
520  bool begin(const std::string& title, const RectF& bounds, UIWindowFlags flags = None);
521 
529  void end();
530 
538  RectF windowGetBounds();
539 
557  void layoutRowDynamic(float height, int cols);
558 
568  void layoutRowStatic(float height, int itemWidth, int cols);
569 
579  void layoutRowBegin(UILayout format, float height, int cols);
580 
588  void layoutRowPush(float width);
589 
595  void layoutRowEnd();
596 
604  void layoutRow(UILayout format, float height, ArrayRef<float> ratio);
605 
613  void separator(float height);
614 
638  bool groupBegin(const std::string& title, UIWindowFlags flags = None);
639 
647  void groupEnd();
648 
662  bool groupScrolledBegin(UIScroll& scroll, const std::string& title, UIWindowFlags flags = None);
663 
669  void groupScrolledEnd();
670 
692  bool treePush(UITree type, const std::string& title, UICollapse& state);
693 
701  void treePop();
702 
720  void label(StringRef title, UIAlignment align = UIAlignment::Left);
721 
731  void labelColored(const Color4f& color, StringRef title, UIAlignment align = UIAlignment::Left);
732 
740  void labelWrap(StringRef title);
741 
750  void labelWrapColored(const Color4f& color, StringRef title);
751 
758  void image(const Texture& texture, const RectF& textureRect);
759 
776  void buttonSetBehavior(UIButtonBehavior behavior);
777 
785  bool buttonPushBehavior(UIButtonBehavior behavior);
786 
792  bool buttonPopBehavior();
793 
802  bool buttonLabel(StringRef title);
803 
812  bool buttonColor(const Color4f& color);
813 
822  bool buttonSymbol(UISymbol symbol);
823 
834  bool buttonSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
835 
854  bool checkbox(StringRef title, bool& active);
855 
866  bool checkboxFlags(StringRef title, unsigned& flags, unsigned value);
867 
886  bool option(StringRef title, bool active);
887 
897  bool radio(StringRef title, bool& active);
898 
918  bool selectableLabel(StringRef title, UIAlignment align, bool& value);
919 
940  bool sliderFloat(float min, float& val, float max, float step);
941 
953  bool sliderInt(int min, int& val, int max, int step);
954 
972  bool progress(std::size_t& current, std::size_t max, UIProgress modifyable = UIProgress::Modifyable);
973 
989  bool colorPicker(Color4f& color);
990 
1012  void propertyInt(const std::string& name, int min, int& val, int max, int step, float incPerPixel);
1013 
1026  void propertyFloat(const std::string& name, float min, float& val, float max, float step, float incPerPixel);
1027 
1040  void propertyDouble(const std::string& name, double min, double& val, double max, double step, float incPerPixel);
1041 
1052 
1070  bool fileSelector(UIBrowser& browser, const std::string& title, const RectF& bounds);
1071 
1092  bool popupBegin(UIPopup type, const std::string& title, UIWindowFlags flags, const RectF& bounds);
1093 
1099  void popupClose();
1100 
1106  void popupEnd();
1107 
1127  void combobox(const std::vector<std::string>& items, int& selected, int itemHeight, Vector2f size);
1128 
1140  void comboboxSeparator(const std::string& itemsSeparatedBySeparator, char separator, int& selected, int itemHeight, Vector2f size);
1141 
1151  bool comboBeginLabel(StringRef selected, Vector2f size);
1152 
1162  bool comboBeginColor(const Color4f& color, Vector2f size);
1163 
1173  bool comboBeginSymbol(UISymbol symbol, Vector2f size);
1174 
1185  bool comboBeginSymbolLabel(UISymbol symbol, StringRef selected, Vector2f size);
1186 
1194  bool comboItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1195 
1204  bool comboItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1205 
1211  void comboClose();
1212 
1218  void comboEnd();
1219 
1239  bool contextualBegin(UIWindowFlags flags, Vector2f size, const RectF& triggerBounds);
1240 
1248  bool contextualItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1249 
1258  bool contextualItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1259 
1265  void contextualClose();
1266 
1272  void contextualEnd();
1273 
1288  void tooltip(const std::string& text);
1289 
1298  bool tooltipBegin(float width);
1299 
1305  void tooltipEnd();
1306 
1321  void menubarBegin();
1322 
1328  void menubarEnd();
1329 
1340  bool menuBeginLabel(StringRef title, UIAlignment align, Vector2f size);
1341 
1352  bool menuBeginSymbol(const std::string& id, UISymbol symbol, Vector2f size);
1353 
1365  bool menuBeginSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align, Vector2f size);
1366 
1374  bool menuItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1375 
1384  bool menuItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1385 
1391  void menuClose();
1392 
1398  void menuEnd();
1399 
1414  RectF getWidgetBounds();
1415 
1423  bool isWidgetHovered();
1424 
1430  RectF getWindowBounds();
1431 
1439  bool isWindowHovered();
1440 
1446  void spacing(int cols);
1447 
1464  void setPredefinedStyle(UIPredefinedStyle style);
1465 
1470  virtual void draw(RenderTarget &target, const RenderStates& states) override;
1471 
1472  private:
1473  enum class State {
1474  Start,
1475  Input,
1476  Setup,
1477  Draw,
1478  };
1479 
1480  void setState(State state);
1481 
1482  private:
1483  struct UIImpl;
1484 
1485  std::unique_ptr<UIImpl> m_impl;
1486  };
1487 
1488 
1489 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1490 }
1491 #endif
1492 
1493 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1494 template<>
1495 struct EnableBitmaskOperators<UIWindow> {
1496  static constexpr bool value = true;
1497 };
1498 
1499 template<>
1500 struct EnableBitmaskOperators<UIEdit> {
1501  static constexpr bool value = true;
1502 };
1503 
1504 template<>
1505 struct EnableBitmaskOperators<UIEditEvent> {
1506  static constexpr bool value = true;
1507 };
1508 #endif
1509 
1510 }
1511 
1512 #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
The structure represents an internal node.
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:73
UIPopup
Type of popup.
Definition: UI.h:245
StringRef asStringRef() const
Definition: UI.h:283
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
Bitfield relying on an enumeration.
Definition: Flags.h:46
UIEditFilter
Filters for edit.
Definition: UI.h:220
UIProgress
State of the progress bar.
Definition: UI.h:132
std::string asString() const
Definition: UI.h:279
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:447
Path currentPath
The current path for searching.
Definition: UI.h:237
The window is scalable by the user.
constexpr BufferRef< T > buffer(T *data, std::size_t size)
Create a reference to a buffer.
Definition: BufferRef.h:211
Abstract base class for objects that can be drawn to a render window.
Definition: Drawable.h:57
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:301
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:1168
Data for file selector.
Definition: UI.h:236
The window can be closed with an icon in the header.
A character buffer for edition.
Definition: UI.h:268
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:43
A character font.
Definition: Font.h:109
Outline rectangle.
The progress bar can be modified.
A 4D vector.
Definition: Vector.h:838
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:97
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.