Gamedev Framework (gf)  0.7.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 
476  bool begin(const std::string& title, const RectF& bounds, UIWindowFlags flags = None);
477 
485  void end();
486 
494  RectF windowGetBounds();
495 
513  void layoutRowDynamic(float height, int cols);
514 
524  void layoutRowStatic(float height, int itemWidth, int cols);
525 
535  void layoutRowBegin(UILayout format, float height, int cols);
536 
544  void layoutRowPush(float width);
545 
551  void layoutRowEnd();
552 
560  void layoutRow(UILayout format, float height, ArrayRef<float> ratio);
561 
569  void separator(float height);
570 
594  bool groupBegin(const std::string& title, UIWindowFlags flags = None);
595 
603  void groupEnd();
604 
618  bool groupScrolledBegin(UIScroll& scroll, const std::string& title, UIWindowFlags flags = None);
619 
625  void groupScrolledEnd();
626 
648  bool treePush(UITree type, const std::string& title, UICollapse& state);
649 
657  void treePop();
658 
676  void label(StringRef title, UIAlignment align = UIAlignment::Left);
677 
687  void labelColored(const Color4f& color, StringRef title, UIAlignment align = UIAlignment::Left);
688 
696  void labelWrap(StringRef title);
697 
706  void labelWrapColored(const Color4f& color, StringRef title);
707 
714  void image(const Texture& texture, const RectF& textureRect);
715 
732  void buttonSetBehavior(UIButtonBehavior behavior);
733 
741  bool buttonPushBehavior(UIButtonBehavior behavior);
742 
748  bool buttonPopBehavior();
749 
758  bool buttonLabel(StringRef title);
759 
768  bool buttonColor(const Color4f& color);
769 
778  bool buttonSymbol(UISymbol symbol);
779 
790  bool buttonSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
791 
810  bool checkbox(StringRef title, bool& active);
811 
822  bool checkboxFlags(StringRef title, unsigned& flags, unsigned value);
823 
842  bool option(StringRef title, bool active);
843 
853  bool radio(StringRef title, bool& active);
854 
874  bool selectableLabel(StringRef title, UIAlignment align, bool& value);
875 
896  bool sliderFloat(float min, float& val, float max, float step);
897 
909  bool sliderInt(int min, int& val, int max, int step);
910 
928  bool progress(std::size_t& current, std::size_t max, UIProgress modifyable = UIProgress::Modifyable);
929 
945  bool colorPicker(Color4f& color);
946 
968  void propertyInt(const std::string& name, int min, int& val, int max, int step, float incPerPixel);
969 
982  void propertyFloat(const std::string& name, float min, float& val, float max, float step, float incPerPixel);
983 
996  void propertyDouble(const std::string& name, double min, double& val, double max, double step, float incPerPixel);
997 
1007  UIEditEventFlags edit(UIEditFlags flags, BufferRef<char> buffer, std::size_t& length, UIEditFilter filter = UIEditFilter::Default);
1008 
1026  bool fileSelector(UIBrowser& browser, const std::string& title, const RectF& bounds);
1027 
1048  bool popupBegin(UIPopup type, const std::string& title, UIWindowFlags flags, const RectF& bounds);
1049 
1055  void popupClose();
1056 
1062  void popupEnd();
1063 
1083  void combobox(const std::vector<std::string>& items, int& selected, int itemHeight, Vector2f size);
1084 
1096  void comboboxSeparator(const std::string& itemsSeparatedBySeparator, char separator, int& selected, int itemHeight, Vector2f size);
1097 
1107  bool comboBeginLabel(StringRef selected, Vector2f size);
1108 
1118  bool comboBeginColor(const Color4f& color, Vector2f size);
1119 
1129  bool comboBeginSymbol(UISymbol symbol, Vector2f size);
1130 
1141  bool comboBeginSymbolLabel(UISymbol symbol, StringRef selected, Vector2f size);
1142 
1150  bool comboItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1151 
1160  bool comboItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1161 
1167  void comboClose();
1168 
1174  void comboEnd();
1175 
1195  bool contextualBegin(UIWindowFlags flags, Vector2f size, const RectF& triggerBounds);
1196 
1204  bool contextualItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1205 
1214  bool contextualItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1215 
1221  void contextualClose();
1222 
1228  void contextualEnd();
1229 
1244  void tooltip(const std::string& text);
1245 
1254  bool tooltipBegin(float width);
1255 
1261  void tooltipEnd();
1262 
1277  void menubarBegin();
1278 
1284  void menubarEnd();
1285 
1296  bool menuBeginLabel(StringRef title, UIAlignment align, Vector2f size);
1297 
1308  bool menuBeginSymbol(const std::string& id, UISymbol symbol, Vector2f size);
1309 
1321  bool menuBeginSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align, Vector2f size);
1322 
1330  bool menuItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1331 
1340  bool menuItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1341 
1347  void menuClose();
1348 
1354  void menuEnd();
1355 
1370  RectF getWidgetBounds();
1371 
1379  bool isWidgetHovered();
1380 
1386  void spacing(int cols);
1387 
1404  void setPredefinedStyle(UIPredefinedStyle style);
1405 
1410  virtual void draw(RenderTarget &target, RenderStates states) override;
1411 
1412  private:
1413  enum class State {
1414  Start,
1415  Input,
1416  Setup,
1417  Draw,
1418  };
1419 
1420  void setState(State state);
1421 
1422  private:
1423  struct UIImpl;
1424 
1425  std::unique_ptr<UIImpl> m_impl;
1426  };
1427 
1428 
1429 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1430 }
1431 #endif
1432 
1433 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1434 template<>
1435 struct EnableBitmaskOperators<UIWindow> {
1436  static constexpr bool value = true;
1437 };
1438 
1439 template<>
1440 struct EnableBitmaskOperators<UIEdit> {
1441  static constexpr bool value = true;
1442 };
1443 
1444 template<>
1445 struct EnableBitmaskOperators<UIEditEvent> {
1446  static constexpr bool value = true;
1447 };
1448 #endif
1449 
1450 }
1451 
1452 #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 tree is a node, generally an internal node of the tree.
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:34
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:41
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.