Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
UI.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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);
444 
448  UI& operator=(UI&& other);
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 
487  void windowSetBounds(const RectF& bounds);
488  RectF windowGetBounds();
489 
507  void layoutRowDynamic(float height, int cols);
508 
518  void layoutRowStatic(float height, int itemWidth, int cols);
519 
529  void layoutRowBegin(UILayout format, float height, int cols);
530 
538  void layoutRowPush(float width);
539 
545  void layoutRowEnd();
546 
554  void layoutRow(UILayout format, float height, ArrayRef<float> ratio);
555 
563  void separator(float height);
564 
588  bool groupBegin(const std::string& title, UIWindowFlags flags = None);
589 
597  void groupEnd();
598 
612  bool groupScrolledBegin(UIScroll& scroll, const std::string& title, UIWindowFlags flags = None);
613 
619  void groupScrolledEnd();
620 
642  bool treePush(UITree type, const std::string& title, UICollapse& state);
643 
651  void treePop();
652 
670  void label(StringRef title, UIAlignment align = UIAlignment::Left);
671 
681  void labelColored(const Color4f& color, StringRef title, UIAlignment align = UIAlignment::Left);
682 
690  void labelWrap(StringRef title);
691 
700  void labelWrapColored(const Color4f& color, StringRef title);
701 
708  void image(const Texture& texture, const RectF& textureRect);
709 
726  void buttonSetBehavior(UIButtonBehavior behavior);
727 
735  bool buttonPushBehavior(UIButtonBehavior behavior);
736 
742  bool buttonPopBehavior();
743 
752  bool buttonLabel(StringRef title);
753 
762  bool buttonColor(const Color4f& color);
763 
772  bool buttonSymbol(UISymbol symbol);
773 
784  bool buttonSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
785 
804  bool checkbox(StringRef title, bool& active);
805 
816  bool checkboxFlags(StringRef title, unsigned& flags, unsigned value);
817 
836  bool option(StringRef title, bool active);
837 
847  bool radio(StringRef title, bool& active);
848 
868  bool selectableLabel(StringRef title, UIAlignment align, bool& value);
869 
890  bool sliderFloat(float min, float& val, float max, float step);
891 
903  bool sliderInt(int min, int& val, int max, int step);
904 
922  bool progress(std::size_t& current, std::size_t max, UIProgress modifyable = UIProgress::Modifyable);
923 
939  bool colorPicker(Color4f& color);
940 
962  void propertyInt(const std::string& name, int min, int& val, int max, int step, float incPerPixel);
963 
976  void propertyFloat(const std::string& name, float min, float& val, float max, float step, float incPerPixel);
977 
990  void propertyDouble(const std::string& name, double min, double& val, double max, double step, float incPerPixel);
991 
1001  UIEditEventFlags edit(UIEditFlags flags, BufferRef<char> buffer, std::size_t& length, UIEditFilter filter = UIEditFilter::Default);
1002 
1020  bool fileSelector(UIBrowser& browser, const std::string& title, const RectF& bounds);
1021 
1042  bool popupBegin(UIPopup type, const std::string& title, UIWindowFlags flags, const RectF& bounds);
1043 
1049  void popupClose();
1050 
1056  void popupEnd();
1057 
1077  void combobox(const std::vector<std::string>& items, int& selected, int itemHeight, Vector2f size);
1078 
1090  void comboboxSeparator(const std::string& itemsSeparatedBySeparator, char separator, int& selected, int itemHeight, Vector2f size);
1091 
1101  bool comboBeginLabel(StringRef selected, Vector2f size);
1102 
1112  bool comboBeginColor(const Color4f& color, Vector2f size);
1113 
1123  bool comboBeginSymbol(UISymbol symbol, Vector2f size);
1124 
1135  bool comboBeginSymbolLabel(UISymbol symbol, StringRef selected, Vector2f size);
1136 
1144  bool comboItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1145 
1154  bool comboItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1155 
1161  void comboClose();
1162 
1168  void comboEnd();
1169 
1189  bool contextualBegin(UIWindowFlags flags, Vector2f size, const RectF& triggerBounds);
1190 
1198  bool contextualItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1199 
1208  bool contextualItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1209 
1215  void contextualClose();
1216 
1222  void contextualEnd();
1223 
1238  void tooltip(const std::string& text);
1239 
1248  bool tooltipBegin(float width);
1249 
1255  void tooltipEnd();
1256 
1271  void menubarBegin();
1272 
1278  void menubarEnd();
1279 
1290  bool menuBeginLabel(StringRef title, UIAlignment align, Vector2f size);
1291 
1302  bool menuBeginSymbol(const std::string& id, UISymbol symbol, Vector2f size);
1303 
1315  bool menuBeginSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align, Vector2f size);
1316 
1324  bool menuItemLabel(StringRef title, UIAlignment align = UIAlignment::Left);
1325 
1334  bool menuItemSymbolLabel(UISymbol symbol, StringRef title, UIAlignment align = UIAlignment::Left);
1335 
1341  void menuClose();
1342 
1348  void menuEnd();
1349 
1364  RectF getWidgetBounds();
1365 
1373  bool isWidgetHovered();
1374 
1380  void spacing(int cols);
1381 
1398  void setPredefinedStyle(UIPredefinedStyle style);
1399 
1404  virtual void draw(RenderTarget &target, RenderStates states) override;
1405 
1406  private:
1407  enum class State {
1408  Start,
1409  Input,
1410  Setup,
1411  Draw,
1412  };
1413 
1414  void setState(State state);
1415 
1416  private:
1417  struct UIImpl;
1418 
1419  std::unique_ptr<UIImpl> m_impl;
1420  };
1421 
1422 
1423 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1424 }
1425 #endif
1426 
1427 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1428 template<>
1429 struct EnableBitmaskOperators<UIWindow> {
1430  static constexpr bool value = true;
1431 };
1432 
1433 template<>
1434 struct EnableBitmaskOperators<UIEdit> {
1435  static constexpr bool value = true;
1436 };
1437 
1438 template<>
1439 struct EnableBitmaskOperators<UIEditEvent> {
1440  static constexpr bool value = true;
1441 };
1442 #endif
1443 
1444 }
1445 
1446 #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:1040
Data for file selector.
Definition: UI.h:236
The window can be closed with an icon in the header.
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 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.