Gamedev Framework (gf)  0.14.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 
290  private:
291  friend class UI;
292 
293  char *m_data;
294  std::size_t m_length;
295  std::size_t m_capacity;
296  };
297 
446  class GF_API UI : public Drawable {
447  public:
451  static constexpr unsigned DefaultCharacterSize = 13;
452 
459  UI(Font& font, unsigned characterSize = DefaultCharacterSize);
460 
464  ~UI();
465 
469  UI(const UI&) = delete;
470 
474  UI& operator=(const UI&) = delete;
475 
479  UI(UI&& other) noexcept;
480 
484  UI& operator=(UI&& other) noexcept;
485 
493  void processEvent(const Event& event);
494 
500  void setCharacterSize(unsigned characterSize);
501 
519  bool begin(const std::string& title, const RectF& bounds, UIWindowFlags flags = None);
520 
528  void end();
529 
537  RectF windowGetBounds();
538 
556  void layoutRowDynamic(float height, int cols);
557 
567  void layoutRowStatic(float height, int itemWidth, int cols);
568 
578  void layoutRowBegin(UILayout format, float height, int cols);
579 
587  void layoutRowPush(float width);
588 
594  void layoutRowEnd();
595 
603  void layoutRow(UILayout format, float height, ArrayRef<float> ratio);
604 
612  void separator(float height);
613 
637  bool groupBegin(const std::string& title, UIWindowFlags flags = None);
638 
646  void groupEnd();
647 
661  bool groupScrolledBegin(UIScroll& scroll, const std::string& title, UIWindowFlags flags = None);
662 
668  void groupScrolledEnd();
669 
691  bool treePush(UITree type, const std::string& title, UICollapse& state);
692 
700  void treePop();
701 
719  void label(StringRef title, UIAlignment align = UIAlignment::Left);
720 
730  void labelColored(const Color4f& color, StringRef title, UIAlignment align = UIAlignment::Left);
731 
739  void labelWrap(StringRef title);
740 
749  void labelWrapColored(const Color4f& color, StringRef title);
750 
757  void image(const Texture& texture, const RectF& textureRect);
758 
775  void buttonSetBehavior(UIButtonBehavior behavior);
776 
784  bool buttonPushBehavior(UIButtonBehavior behavior);
785 
791  bool buttonPopBehavior();
792 
801  bool buttonLabel(StringRef title);
802 
811  bool buttonColor(const Color4f& color);
812 
821  bool buttonSymbol(UISymbol symbol);
822 
833  bool buttonSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
834 
853  bool checkbox(StringRef title, bool& active);
854 
865  bool checkboxFlags(StringRef title, unsigned& flags, unsigned value);
866 
885  bool option(StringRef title, bool active);
886 
896  bool radio(StringRef title, bool& active);
897 
917  bool selectableLabel(StringRef title, UIAlignment align, bool& value);
918 
939  bool sliderFloat(float min, float& val, float max, float step);
940 
952  bool sliderInt(int min, int& val, int max, int step);
953 
971  bool progress(std::size_t& current, std::size_t max, UIProgress modifyable = UIProgress::Modifyable);
972 
988  bool colorPicker(Color4f& color);
989 
1011  void propertyInt(const std::string& name, int min, int& val, int max, int step, float incPerPixel);
1012 
1025  void propertyFloat(const std::string& name, float min, float& val, float max, float step, float incPerPixel);
1026 
1039  void propertyDouble(const std::string& name, double min, double& val, double max, double step, float incPerPixel);
1040 
1051 
1069  bool fileSelector(UIBrowser& browser, const std::string& title, const RectF& bounds);
1070 
1091  bool popupBegin(UIPopup type, const std::string& title, UIWindowFlags flags, const RectF& bounds);
1092 
1098  void popupClose();
1099 
1105  void popupEnd();
1106 
1126  void combobox(const std::vector<std::string>& items, int& selected, int itemHeight, Vector2f size);
1127 
1139  void comboboxSeparator(const std::string& itemsSeparatedBySeparator, char separator, int& selected, int itemHeight, Vector2f size);
1140 
1150  bool comboBeginLabel(StringRef selected, Vector2f size);
1151 
1161  bool comboBeginColor(const Color4f& color, Vector2f size);
1162 
1172  bool comboBeginSymbol(UISymbol symbol, Vector2f size);
1173 
1184  bool comboBeginSymbolLabel(UISymbol symbol, StringRef selected, Vector2f size);
1185 
1193  bool comboItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1194 
1203  bool comboItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1204 
1210  void comboClose();
1211 
1217  void comboEnd();
1218 
1238  bool contextualBegin(UIWindowFlags flags, Vector2f size, const RectF& triggerBounds);
1239 
1247  bool contextualItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1248 
1257  bool contextualItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1258 
1264  void contextualClose();
1265 
1271  void contextualEnd();
1272 
1287  void tooltip(const std::string& text);
1288 
1297  bool tooltipBegin(float width);
1298 
1304  void tooltipEnd();
1305 
1320  void menubarBegin();
1321 
1327  void menubarEnd();
1328 
1339  bool menuBeginLabel(StringRef title, UIAlignment align, Vector2f size);
1340 
1351  bool menuBeginSymbol(const std::string& id, UISymbol symbol, Vector2f size);
1352 
1364  bool menuBeginSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align, Vector2f size);
1365 
1373  bool menuItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1374 
1383  bool menuItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1384 
1390  void menuClose();
1391 
1397  void menuEnd();
1398 
1413  RectF getWidgetBounds();
1414 
1422  bool isWidgetHovered();
1423 
1429  void spacing(int cols);
1430 
1447  void setPredefinedStyle(UIPredefinedStyle style);
1448 
1453  virtual void draw(RenderTarget &target, const RenderStates& states) override;
1454 
1455  private:
1456  enum class State {
1457  Start,
1458  Input,
1459  Setup,
1460  Draw,
1461  };
1462 
1463  void setState(State state);
1464 
1465  private:
1466  struct UIImpl;
1467 
1468  std::unique_ptr<UIImpl> m_impl;
1469  };
1470 
1471 
1472 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1473 }
1474 #endif
1475 
1476 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1477 template<>
1478 struct EnableBitmaskOperators<UIWindow> {
1479  static constexpr bool value = true;
1480 };
1481 
1482 template<>
1483 struct EnableBitmaskOperators<UIEdit> {
1484  static constexpr bool value = true;
1485 };
1486 
1487 template<>
1488 struct EnableBitmaskOperators<UIEditEvent> {
1489  static constexpr bool value = true;
1490 };
1491 #endif
1492 
1493 }
1494 
1495 #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:446
Path currentPath
The current path for searching.
Definition: UI.h:237
The window is scalable by the user.
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:42
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.