Gamedev Framework (gf)  0.17.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 
50  enum class UIWindow : uint32_t {
51  Border = 0x0001,
52  Movable = 0x0002,
53  Scalable = 0x0004,
54  Closable = 0x0008,
55  Minimizable = 0x0010,
56  NoScrollbar = 0x0020,
57  Title = 0x0040,
58  ScrollAutoHide = 0x0080,
59  Background = 0x0100,
60  ScaleLeft = 0x0200,
61  NoInput = 0x0400,
62  };
63 
68  enum class UICollapse : bool {
69  Minimized = false,
70  Maximized = true,
71  };
72 
77  enum class UILayout {
78  Dynamic = 0,
79  Static = 1,
80  };
81 
86  enum class UITree {
87  Node = 0,
88  Tab = 1,
89  };
90 
97  enum class UIAlignment {
98  Left = 0x10 | 0x01,
99  Center = 0x10 | 0x02,
100  Right = 0x10 | 0x04,
101  };
102 
108 
113  enum class UIButtonBehavior {
114  Default = 0,
115  Repeater = 1,
116  };
117 
122  enum class UIProgress : bool {
123  Fixed = false,
124  Modifyable = true,
125  };
126 
131  enum class UISymbol {
132  None,
133  X,
134  Underscore,
135  CircleSolid,
136  CircleOutline,
137  RectSolid,
138  RectOutline,
139  TriangleUp,
140  TriangleDown,
141  TriangleLeft,
142  TriangleRight,
143  Plus,
144  Minus,
145  };
146 
151  enum class UIEdit : uint32_t {
152  Default = 0x0000,
153  ReadOnly = 0x0001,
154  AutoSelect = 0x0002,
155  SigEnter = 0x0004,
156  AllowTab = 0x0008,
157  NoCursor = 0x0010,
158  Selectable = 0x0020,
159  Clipboard = 0x0040,
160  CtrlEnterNewline = 0x0080,
161  NoHorizontalScroll = 0x0100,
162  AlwaysInsertMode = 0x0200,
163  Multiline = 0x0400,
164  GotoEndOnActivate = 0x0800,
165  };
166 
171  struct GF_API UIEditType {
172  static const Flags<UIEdit> Simple;
173  static const Flags<UIEdit> Field;
174  static const Flags<UIEdit> Box;
175  static const Flags<UIEdit> Editor;
176  };
177 
182  enum class UIEditEvent : uint32_t {
183  Active = 0x0001,
184  Inactive = 0x0002,
185  Activated = 0x0004,
186  Deactivated = 0x0008,
187  Commited = 0x0010,
188  };
189 
194  enum class UIEditFilter {
195  Default,
196  Ascii,
197  Float,
198  Decimal,
199  Hex,
200  Oct,
201  Binary,
202  };
203 
210  struct UIBrowser {
213  };
214 
219  enum class UIPopup {
220  Static = 0,
221  Dynamic = 1,
222  };
223 
228  enum class UIPredefinedStyle {
229  Default,
230  White,
231  Red,
232  Blue,
233  Dark,
234  };
235 
242  class GF_API UICharBuffer {
243  public:
244  UICharBuffer(std::size_t capacity);
245  ~UICharBuffer();
246 
247  UICharBuffer(const UICharBuffer&) = delete;
248  UICharBuffer& operator=(const UICharBuffer&) = delete;
249 
250  UICharBuffer(UICharBuffer&& other) noexcept;
251  UICharBuffer& operator=(UICharBuffer&& other) noexcept;
252 
253  std::string asString() const {
254  return std::string(m_data, m_length);
255  }
256 
258  return StringRef(m_data, m_length);
259  }
260 
261  void clear();
262  void append(const UICharBuffer& other);
263  void append(const std::string& other);
264 
265  private:
266  friend class UI;
267 
268  char *m_data;
269  std::size_t m_length;
270  std::size_t m_capacity;
271  };
272 
421  class GF_API UI : public Drawable {
422  public:
426  static constexpr unsigned DefaultCharacterSize = 13;
427 
434  UI(Font& font, unsigned characterSize = DefaultCharacterSize);
435 
439  ~UI();
440 
444  UI(const UI&) = delete;
445 
449  UI& operator=(const UI&) = delete;
450 
454  UI(UI&& other) noexcept;
455 
459  UI& operator=(UI&& other) noexcept;
460 
468  void processEvent(const Event& event);
469 
475  void setCharacterSize(unsigned characterSize);
476 
494  bool begin(const std::string& title, const RectF& bounds, Flags<UIWindow> flags = None);
495 
503  void end();
504 
512  RectF windowGetBounds();
513 
531  void layoutRowDynamic(float height, int cols);
532 
542  void layoutRowStatic(float height, int itemWidth, int cols);
543 
553  void layoutRowBegin(UILayout format, float height, int cols);
554 
562  void layoutRowPush(float width);
563 
569  void layoutRowEnd();
570 
578  void layoutRow(UILayout format, float height, ArrayRef<float> ratio);
579 
587  void separator(float height);
588 
612  bool groupBegin(const std::string& title, Flags<UIWindow> flags = None);
613 
621  void groupEnd();
622 
636  bool groupScrolledBegin(UIScroll& scroll, const std::string& title, Flags<UIWindow> flags = None);
637 
643  void groupScrolledEnd();
644 
666  bool treePush(UITree type, const std::string& title, UICollapse& state);
667 
675  void treePop();
676 
694  void label(StringRef title, UIAlignment align = UIAlignment::Left);
695 
705  void labelColored(const Color4f& color, StringRef title, UIAlignment align = UIAlignment::Left);
706 
714  void labelWrap(StringRef title);
715 
724  void labelWrapColored(const Color4f& color, StringRef title);
725 
732  void image(const Texture& texture, const RectF& textureRect);
733 
750  void buttonSetBehavior(UIButtonBehavior behavior);
751 
759  bool buttonPushBehavior(UIButtonBehavior behavior);
760 
766  bool buttonPopBehavior();
767 
776  bool buttonLabel(StringRef title);
777 
786  bool buttonColor(const Color4f& color);
787 
796  bool buttonSymbol(UISymbol symbol);
797 
808  bool buttonSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
809 
828  bool checkbox(StringRef title, bool& active);
829 
839  bool checkboxValue(StringRef title, bool active);
840 
851  bool checkboxFlags(StringRef title, unsigned& flags, unsigned value);
852 
863  unsigned checkboxValueFlags(StringRef title, unsigned flags, unsigned value);
864 
883  bool option(StringRef title, bool active);
884 
894  bool radio(StringRef title, bool& active);
895 
915  bool selectableLabel(StringRef title, UIAlignment align, bool& value);
916 
927  bool selectableValueLabel(StringRef title, UIAlignment align, bool value);
928 
949  bool sliderFloat(float min, float& val, float max, float step);
950 
962  bool sliderInt(int min, int& val, int max, int step);
963 
981  bool progress(std::size_t& current, std::size_t max, UIProgress modifyable = UIProgress::Modifyable);
982 
998  bool colorPicker(Color4f& color);
999 
1021  void propertyInt(const std::string& name, int min, int& val, int max, int step, float incPerPixel);
1022 
1035  void propertyFloat(const std::string& name, float min, float& val, float max, float step, float incPerPixel);
1036 
1049  void propertyDouble(const std::string& name, double min, double& val, double max, double step, float incPerPixel);
1050 
1061 
1079  bool fileSelector(UIBrowser& browser, const std::string& title, const RectF& bounds);
1080 
1101  bool popupBegin(UIPopup type, const std::string& title, Flags<UIWindow> flags, const RectF& bounds);
1102 
1108  void popupClose();
1109 
1115  void popupEnd();
1116 
1136  void combobox(const std::vector<std::string>& items, int& selected, int itemHeight, Vector2f size);
1137 
1149  void comboboxSeparator(const std::string& itemsSeparatedBySeparator, char separator, int& selected, int itemHeight, Vector2f size);
1150 
1160  bool comboBeginLabel(StringRef selected, Vector2f size);
1161 
1171  bool comboBeginColor(const Color4f& color, Vector2f size);
1172 
1182  bool comboBeginSymbol(UISymbol symbol, Vector2f size);
1183 
1194  bool comboBeginSymbolLabel(UISymbol symbol, StringRef selected, Vector2f size);
1195 
1203  bool comboItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1204 
1213  bool comboItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1214 
1220  void comboClose();
1221 
1227  void comboEnd();
1228 
1248  bool contextualBegin(Flags<UIWindow> flags, Vector2f size, const RectF& triggerBounds);
1249 
1257  bool contextualItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1258 
1267  bool contextualItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1268 
1274  void contextualClose();
1275 
1281  void contextualEnd();
1282 
1297  void tooltip(const std::string& text);
1298 
1307  bool tooltipBegin(float width);
1308 
1314  void tooltipEnd();
1315 
1330  void menubarBegin();
1331 
1337  void menubarEnd();
1338 
1349  bool menuBeginLabel(StringRef title, UIAlignment align, Vector2f size);
1350 
1361  bool menuBeginSymbol(const std::string& id, UISymbol symbol, Vector2f size);
1362 
1374  bool menuBeginSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align, Vector2f size);
1375 
1383  bool menuItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1384 
1393  bool menuItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1394 
1400  void menuClose();
1401 
1407  void menuEnd();
1408 
1423  RectF getWidgetBounds();
1424 
1432  bool isWidgetHovered();
1433 
1439  RectF getWindowBounds();
1440 
1448  bool isWindowHovered();
1449 
1455  void spacing(int cols);
1456 
1473  void setPredefinedStyle(UIPredefinedStyle style);
1474 
1479  virtual void draw(RenderTarget &target, const RenderStates& states) override;
1480 
1481  private:
1482  enum class State {
1483  Start,
1484  Input,
1485  Setup,
1486  Draw,
1487  };
1488 
1489  void setState(State state);
1490 
1491  private:
1492  struct UIImpl;
1493 
1494  std::unique_ptr<UIImpl> m_impl;
1495  };
1496 
1497 
1498 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1499 }
1500 #endif
1501 
1502 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1503 template<>
1504 struct EnableBitmaskOperators<UIWindow> {
1505  static constexpr bool value = true;
1506 };
1507 
1508 template<>
1509 struct EnableBitmaskOperators<UIEdit> {
1510  static constexpr bool value = true;
1511 };
1512 
1513 template<>
1514 struct EnableBitmaskOperators<UIEditEvent> {
1515  static constexpr bool value = true;
1516 };
1517 #endif
1518 
1519 }
1520 
1521 #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:77
The structure represents an internal node.
Edit widget is not active and is not being modified.
The button is active once.
static const Flags< UIEdit > Editor
Definition: UI.h:175
Edit widget is currently being modified.
UIEditEvent
Properties for edit events.
Definition: UI.h:182
No alignement.
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:90
UIPopup
Type of popup.
Definition: UI.h:219
StringRef asStringRef() const
Definition: UI.h:257
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
UIEditFilter
Filters for edit.
Definition: UI.h:194
UIProgress
State of the progress bar.
Definition: UI.h:122
std::string asString() const
Definition: UI.h:253
The button is active as long as it is pressed.
The tree is maximized.
UIPredefinedStyle
A predefined style.
Definition: UI.h:228
Context for an immediate mode graphical interface.
Definition: UI.h:421
Path currentPath
The current path for searching.
Definition: UI.h:211
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
static const Flags< UIEdit > Simple
Definition: UI.h:172
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:97
A texture for colored images.
Definition: Texture.h:309
The Start button.
UIButtonBehavior
Behavior for buttons.
Definition: UI.h:113
Vector< unsigned, 2 > Vector2u
A unsigned vector with 2 components.
Definition: Vector.h:1189
Data for file selector.
Definition: UI.h:210
The window can be closed with an icon in the header.
A character buffer for edition.
Definition: UI.h:242
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
static const Flags< UIEdit > Field
Definition: UI.h:173
Outline rectangle.
The progress bar can be modified.
A 4D vector.
Definition: Vector.h:852
The X button.
The tree is minimized.
UICollapse
Collapse property of a tree.
Definition: UI.h:68
A light gray style.
UIEdit
Properties for edit widgets.
Definition: UI.h:151
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:171
A constant reference to a string and its size.
Definition: StringRef.h:41
UISymbol
A representative symbol.
Definition: UI.h:131
Left alignment.
Left alignement.
The window is in the background.
static const Flags< UIEdit > Box
Definition: UI.h:174
The window has no scrollbar.
UITree
The type of tree.
Definition: UI.h:86
Right alignement.
The window&#39;s scaler is on the left.
Defines a system event and its parameters.
Definition: Event.h:101
UIWindow
Properties for windows and window-like elements.
Definition: UI.h:50
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:212
The window has a title in the header.