Smolgui
Immediate gui library based on SFML
Loading...
Searching...
No Matches
Gui.h
Go to the documentation of this file.
1#pragma once
2
3#include <stack>
4#include <string>
5
8#include "sgui/Internals/Counters.h"
9#include "sgui/Internals/GuiGroup.h"
10#include "sgui/Internals/GuiStates.h"
11#include "sgui/Internals/ScrollerInformation.h"
12
15#include "sgui/Render/Plotter.h"
19
20namespace sgui
21{
26class Gui : public sf::Drawable, public sf::Transformable
27{
28public:
32 Gui ();
36 Gui (
37 sf::Font& font,
38 sf::Texture& widgetTexture,
39 const TextureAtlas& widgetAtlas,
40 const sf::RenderWindow& window);
45 void initialize (
46 sf::Font& font,
47 sf::Texture& widgetTexture,
48 const TextureAtlas& widgetAtlas,
49 const sf::RenderWindow& window);
50
52
55 void setResources (
56 sf::Font& font,
57 sf::Texture& widgetTexture);
61 void setTextureAtlas (const TextureAtlas& widgetAtlas);
65 void setSounds (SoundHolder& sounds);
69 void setStyle (
70 const Style& newStyle,
71 const bool defaultPadding = true);
75 void setPadding (const sf::Vector2f& padding = {0.5f, 0.125f});
79 void setPixelsPerScroll (const float amount);
80
82
85 void setView (const sf::View& view);
89 void beginFrame ();
94 void endFrame (const float tooltipDelay = 0.5f);
100 void update (
101 const sf::RenderWindow& window,
102 const std::optional <sf::Event>& event);
107 void setScreenSize (const sf::Vector2f& size);
112 void updateTimer ();
113
115
118 Style& style ();
122 const Style& style () const;
126 sf::Vector2f textSize (
127 const std::string& text,
128 const TextType type = TextType::Normal) const;
132 float textHeight (const TextType type = TextType::Normal) const;
136 sf::Vector2f activePanelSize () const;
141 sf::Vector2f normalizeSize (const sf::Vector2f& panelSize) const;
146 sf::Vector2f denormalizeSize (const sf::Vector2f& panelSize) const;
151 sf::Vector2f lastSpacing () const;
156 void addSpacing (const sf::Vector2f& amount);
160 void addLastSpacing (const float amount = 1.f);
164 void setAnchor (const std::string& key = "");
168 void backToAnchor (const std::string& key = "");
172 sf::Vector2f cursorPosition () const;
176 void sameLine ();
181 bool isActive () const;
185 sf::Vector2f parentGroupSize ();
189 sf::Vector2f textureSize (const std::string& texture) const;
190
192
199 bool beginWindow (
200 Panel& settings,
201 const Constraints& constraint = {},
202 const WidgetOptions& options = {});
208 bool beginWindow (
209 Window& window,
210 const TextContainer& texts = {});
214 void endWindow ();
215
222 void beginPanel (
223 Panel& settings,
224 const Constraints& constraint = {},
225 const WidgetOptions& options = {});
230 void beginPanel (Window& window);
234 void endPanel ();
235
237
241 void beginMenu ();
245 void endMenu ();
252 bool menuItem (
253 const std::string& text,
254 const WidgetOptions& options = {});
255
257 // as a general note for all widgets, displacement in options (if different from zero)
258 // will override automatic placement and are relative to group
262 void separation (const float thickness = 0.75f);
266 void image (
267 const std::string& textureId,
268 const sf::Vector2f& size = {},
269 const WidgetOptions& options = {});
273 bool clickable (
274 const sf::Vector2f& size,
275 const WidgetOptions& options = {});
279 bool button (
280 const std::string& text,
281 const WidgetOptions& options = {});
285 bool icon (
286 const std::string& iconName,
287 const WidgetOptions& options = {});
291 bool checkBox (
292 bool& checked,
293 const WidgetOptions& options = {});
294
296
299 void text (
300 const std::string& text,
301 const TextOptions& textOptions = {},
302 const WidgetOptions& options = {});
306 void fontawesome (
307 const std::string& iconName,
308 const TextType type = TextType::Normal);
309
311
318 template <typename Type>
320 Type& number,
321 const WidgetOptions& options = {},
322 const Type min = 0,
323 const Type max = 0,
324 const std::string& label = "",
325 const bool fixedWidth = false);
329 template <typename Type>
331 sf::Vector2<Type>& vector,
332 const WidgetOptions& options = {},
333 const sf::Vector2<Type>& min = {},
334 const sf::Vector2<Type>& max = {});
338 template <typename Type>
340 sf::Vector3<Type>& vector,
341 const WidgetOptions& options = {},
342 const sf::Vector3<Type>& min = {},
343 const sf::Vector3<Type>& max = {});
347 void inputColor (
348 sf::Color& color,
349 const WidgetOptions& options = {});
353 void inputText (
354 std::string& text,
355 const TextOptions& textOptions = {},
356 const WidgetOptions& options = {});
360 void inputKey (
361 char& key,
362 const WidgetOptions& options = {});
363
365
368 void progressBar (
369 const float progress,
370 const WidgetOptions& options = {});
371
373
376 std::string comboBox (
377 const std::vector <std::string>& list,
378 const WidgetOptions& options = {});
379
381
384 template <typename Type>
385 void slider (
386 Type& value,
387 const Type min,
388 const Type max,
389 const WidgetOptions& options = {});
390
392
395 void setPlotRange (
396 const PlotRange xRange,
397 const PlotRange yRange);
398 void setSample (const uint32_t sample);
404 void setPlotBound (const sf::Vector2f& bound);
408 void unsetPlotBound ();
413 void plot (
414 const std::function<float (float)>& slope,
415 const float thickness = 1.f,
416 const sf::Color& lineColor = sf::Color::White);
421 void plot (
422 const std::function<sf::Vector2f (float)>& slope,
423 const float thickness = 1.f,
424 const sf::Color& lineColor = sf::Color::White);
428 void plot (
429 const std::vector<sf::Vector2f>& points,
430 const float thickness = 1.f,
431 const sf::Color& lineColor = sf::Color::White);
435 void forcePlotUpdate ();
436private:
437 // draw gui
438 void draw (sf::RenderTarget& target, sf::RenderStates states) const override;
439 // to remove or add last vertical or horizontal spacing
440 void addLastVerticalSpacing (const float amount = 1.f);
441 void addLastHorizontalSpacing (const float amount = 1.f);
442 // begin/end a group (subjacent struct of window/box/etc.)
443 void beginGroup (const bool horizontal, const sf::FloatRect& box);
444 void endGroup ();
445 // window or panel utility functions
446 sf::Vector2f computePosition (const Panel& settings, const Constraints& constraint);
447 // for nested clipping layer, this need to be called before beginGroup.
448 sf::FloatRect handleParentClipBox (const sf::FloatRect& box);
449 // this need to be called after endGroup to clean clipping layer.
450 void removeClipping ();
451 // move a group with the mouse if its possible
452 ItemState interactWithMouse (Panel& settings, const sf::FloatRect& box, const std::string& name, const Tooltip& info);
453 // cache plot data
454 void cachePlotData (const std::function<float (float)>& slope);
455 void cachePlotData (const std::function<sf::Vector2f (float)>& slope);
456 // set plot bound depending of the current context
457 void handlePlotBound ();
458 // for scrollable panel or window
459 bool isPanelScrollable (const Impl::GroupData& panel);
460 bool scrollThroughPanel (Impl::GroupData& panel, const sf::FloatRect& panelBox, const ItemState panelState, const bool horizontal);
461 sf::Vector2f scroller (float& scrollPercent, const sf::FloatRect& panelBox, const sf::Vector2f& scrollSize, const ItemState panelState, const bool horizontal);
462 // to handle scroll bar in a slider
463 float sliderBar (const sf::FloatRect& parentBox, const ItemState state, const float scrollPercent, const bool horizontal);
464 // to handle scroll bar in a scroller
465 float scrollerBar (const sf::FloatRect& parentBox, const ItemState state, const float scrollPercent, const float extraSize, const bool horizontal);
466 // to handle shifting value of scroller/slider
467 template <typename Type>
468 Type sliderValue (const sf::FloatRect& box, const Type min, const Type max, const bool horizontal);
469 // draw tooltip of the hovered item
470 void tooltip (const float apparitionDelay);
471 bool tooltipNeedReset ();
472 // to manage each item in a dropList
473 bool dropListItem (float& clock, const std::string& selectedName, const std::string& itemName, const sf::Vector2f& itemSize);
474 // Utility function that return status of widget, it is not clickable by default
475 ItemState itemStatus (const sf::FloatRect& boundingbox, const std::string& item, const bool condition, const Tooltip& tooltip = {}, const bool forceActive = false);
476 // handle all edge cases and special keys
477 void handleKeyInput (std::string& text, size_t& textCursorIndex);
478 void drawTextCursor (sf::Vector2f position, const std::string& inputTextId, const std::string& text, const TextOptions& options);
479 size_t utf8Length (const std::string& text) const;
480 // handle key for inputNumber
481 template <typename Type>
482 void handleNumberKeyInput (Type& number, size_t& cursorIndex, const bool focused, const Type min, const Type max);
483 template <typename Type>
484 Type convertKeyIntoNumber (std::string& key, const Type min, const Type max) const;
485 template <typename Type>
486 std::string formatNumberToString (const Type& number) const;
487 // format text to fit in a box
488 std::vector<std::string> formatText (const std::string& text, const sf::Vector2f& boxSize, const TextType type = TextType::Normal) const;
489 // get font size
490 uint32_t getFontSize (const TextType type) const;
491 // fit text in a constrained line
492 std::string truncateText (const std::string& text, const float width) const;
493 // format text for fontawesome use
494 void handleTextDrawing (const sf::Vector2f& position, const std::string& text, const TextType fontSize = TextType::Normal);
495 // draw a fontawesome icon
496 void fontawesomeIcon (const sf::Vector2f& position, const std::string& icon, const uint32_t fontSize);
497 // to compute widget name and relative position to the cursor/group
498 std::string initializeActivable (const std::string& key);
499 sf::Vector2f computeRelativePosition (const sf::Vector2f& displacement = {}) const;
500 // to handle appearance options
501 WidgetDrawOptions drawOptions (const WidgetAspect& standard, const WidgetAspect& custom = {}, const bool horizontal = true, const float progress = 1.f) const;
502 bool isValid (const Widget widget) const;
503 bool isValid (const Slices slices) const;
504 bool isValid (const ItemState state) const;
505 bool isValid (const std::string& image) const;
506 // to draw text description of widgets
507 sf::Vector2f widgetDescription (const sf::Vector2f& position, const std::string& description);
508 // to compute widget spacing and scrolling
509 void updateSpacing (const sf::Vector2f& size);
510 void updateScrolling ();
511 Impl::GroupData getParentGroup ();
512 // to process sfml events and store it in the internal state
513 void handleMouseInputs (const sf::RenderWindow& window, const std::optional <sf::Event>& event);
514 void handleKeyboardInputs (const std::optional <sf::Event>& event);
515 // to play sounds
516 void playSound (const ItemState state);
517private:
518 // plot parameters
519 bool mPlotIsBounded = false;
520 uint32_t mPlotSample = 50u;
521 // Tooltip clock
522 sf::Clock mInternalClock = {};
523 float mTipAppearClock = 0.f;
524 float mTipDisappearClock = 100.f;
525 float mTextCursorClock = 0.f;
526 // Scroll intensity
527 float mPixelsPerScroll = 40.f;
528 // data to keep track of same line call
529 sf::Vector2f mResetCursorPosition = {};
530 std::stack <sf::Vector2f> mSameLinePosition;
531 // counters to keep track of gui objects
532 Impl::Counters mCounters = {};
533 Impl::GroupChecker mChecker = {};
534 std::string mWidgetChain = "";
535 // alignement data
536 sf::Vector2f mCursorPosition = {};
537 sf::Vector2f mPadding = {6.f, 1.5f};
538 sf::Vector2f mLastSpacing = {};
539 sf::Vector2f mWindowSize;
540 sf::Vector2f mPlotBound;
541 std::string mActiveInputNumberStr;
542 // to play sound
543 bool mSoundIsOn = false;
544 std::string mPreviousWidgetSoundId = "";
545 std::string mActiveWidgetSoundId = "";
546 SoundPlayer mSoundPlayer;
547 // render for gui, plot and primitive shape
548 Style mStyle;
549 sf::Font* mFont;
550 sf::Font mFontawesome;
551 Plotter mPlotter;
552 GuiRender mRender;
553 PrimitiveShapeRender mColorRender;
554 // inputs and gui state
555 Impl::InputState mInputState;
556 Impl::InternalItemState mGuiState;
557 // gui internal data
558 std::stack <sf::Vector2f> mAnchors;
559 std::unordered_map <std::string, sf::Vector2f> mAnchorsWithKeys;
560 std::stack <uint32_t> mMenuClippingLayer;
561 std::stack <Impl::GroupData> mGroups;
562 ObjectPool <Impl::GroupHoverBox> mGroupsHoverBoxes;
563 ObjectPool <uint32_t> mGroupsActiveItem;
564 ObjectPool <std::string> mComboBoxActiveItem;
565 ObjectPool <size_t, std::string> mTextCursorPositions;
566 ObjectPool <uint8_t, std::string> mTextHasCursor;
567 ObjectPool <float> mComboBoxClocks;
568 ObjectPool <Panel, std::string> mInputTextPanels;
569 ObjectPool <std::vector <sf::Vector2f>> mPlotsData;
570 ObjectPool <Impl::ScrollerInformation> mGroupsScrollerData;
571};
572
573} // namespace sgui
574
575#include "sgui/Gui.tpp"
Definition Interpolation.h:11
Definition Interpolation.h:12
sf::Vector2f textureSize(const std::string &texture) const
to get texture size as stored in the uv mapper
Definition Gui.cpp:271
bool clickable(const sf::Vector2f &size, const WidgetOptions &options={})
display a clickable button that return true if pressed
Definition Gui.cpp:858
sf::Vector2f textSize(const std::string &text, const TextType type=TextType::Normal) const
get normal size of text in gui
Definition Gui.cpp:117
bool button(const std::string &text, const WidgetOptions &options={})
button with a text displayed over it
Definition Gui.cpp:881
void updateTimer()
to allow apparition of tooltip and animation to occur.
Definition Gui.cpp:425
sf::Vector2f lastSpacing() const
get spacing generated by the last widget Return last spacing
Definition Gui.cpp:156
void inputText(std::string &text, const TextOptions &textOptions={}, const WidgetOptions &options={})
modify text on one or multiple line
Definition Gui.cpp:1008
void addSpacing(const sf::Vector2f &amount)
add spacing
Definition Gui.cpp:162
void initialize(sf::Font &font, sf::Texture &widgetTexture, const TextureAtlas &widgetAtlas, const sf::RenderWindow &window)
initialize window size and all required resources this need to be called once before beginFrame/endFr...
Definition Gui.cpp:33
void slider(Type &value, const Type min, const Type max, const WidgetOptions &options={})
slider to vary value between min and max
void endMenu()
endMenu need to be called to clean state after beginMenu.
Definition Gui.cpp:702
void addLastSpacing(const float amount=1.f)
to add (amount > 0) or remove (amount < 0) last widget spacing
Definition Gui.cpp:170
void inputColor(sf::Color &color, const WidgetOptions &options={})
modify a color value through text
Definition Gui.cpp:1172
void inputKey(char &key, const WidgetOptions &options={})
modify a specific character
Definition Gui.cpp:1133
sf::Vector2f activePanelSize() const
get active panel or window size
Definition Gui.cpp:131
void setPadding(const sf::Vector2f &padding={0.5f, 0.125f})
set padding of widgets
Definition Gui.cpp:82
void beginMenu()
menu bar in which menu item can be arranged. Note that it need to be called in a window or box with h...
Definition Gui.cpp:669
std::string comboBox(const std::vector< std::string > &list, const WidgetOptions &options={})
display selected item and all others selectables items when clicked
Definition Gui.cpp:1349
void update(const sf::RenderWindow &window, const std::optional< sf::Event > &event)
set-up inputs for gui interaction.
Definition Gui.cpp:408
sf::Vector2f normalizeSize(const sf::Vector2f &panelSize) const
normalize size of a panel Return panel size in fraction
Definition Gui.cpp:141
void sameLine()
go back to previous line/column, next to the last widget
Definition Gui.cpp:197
void endWindow()
enWindow need to be called to clean state after beginWindow.
Definition Gui.cpp:581
bool isActive() const
to know if some part of the gui are active or hovered. It must be called before endFrame.
Definition Gui.cpp:254
Gui()
load fontawesome and initialize window size.
Definition Gui.cpp:17
void inputVector3(sf::Vector3< Type > &vector, const WidgetOptions &options={}, const sf::Vector3< Type > &min={}, const sf::Vector3< Type > &max={})
modify a vector3 value through text, using three inputNumber call
bool checkBox(bool &checked, const WidgetOptions &options={})
display textured box that can be checked
Definition Gui.cpp:917
sf::Vector2f parentGroupSize()
to get size of the active parent group
Definition Gui.cpp:262
void setSample(const uint32_t sample)
Definition Gui.cpp:1238
void inputNumber(Type &number, const WidgetOptions &options={}, const Type min=0, const Type max=0, const std::string &label="", const bool fixedWidth=false)
modify a number value through text
sf::Vector2f cursorPosition() const
get current cursor position of the gui
Definition Gui.cpp:245
void setPixelsPerScroll(const float amount)
set scroll wheel strength (should be ~ 20.f)
Definition Gui.cpp:88
void setTextureAtlas(const TextureAtlas &widgetAtlas)
set texture atlas
Definition Gui.cpp:56
void setPlotRange(const PlotRange xRange, const PlotRange yRange)
set plot range and number of points sampled
Definition Gui.cpp:1229
float textHeight(const TextType type=TextType::Normal) const
to have standard height size across gui code
Definition Gui.cpp:125
void endFrame(const float tooltipDelay=0.5f)
this function must be called at the end of every loop.
Definition Gui.cpp:292
void setScreenSize(const sf::Vector2f &size)
set screen size, its required for panel and window to call it if no Gui::update was called
Definition Gui.cpp:419
void setStyle(const Style &newStyle, const bool defaultPadding=true)
to change font size, color and item padding.
Definition Gui.cpp:69
void unsetPlotBound()
remove plot bound.
Definition Gui.cpp:1252
void beginFrame()
this function must be called at the start of every loop.
Definition Gui.cpp:280
void setPlotBound(const sf::Vector2f &bound)
set plot size. This depend on the context of use. In a window/panel, plot bound will be limited to th...
Definition Gui.cpp:1245
void progressBar(const float progress, const WidgetOptions &options={})
display advancement of a value between 0 and 1
Definition Gui.cpp:1200
void setResources(sf::Font &font, sf::Texture &widgetTexture)
set resources
Definition Gui.cpp:47
sf::Vector2f denormalizeSize(const sf::Vector2f &panelSize) const
denormalize size of a panel Return panel size in pixels
Definition Gui.cpp:147
void setView(const sf::View &view)
to set gui view
Definition Gui.cpp:106
bool icon(const std::string &iconName, const WidgetOptions &options={})
clickable icon that work like a button
Definition Gui.cpp:899
void setAnchor(const std::string &key="")
register a position at which you can go back with backToAnchor.
Definition Gui.cpp:206
void fontawesome(const std::string &iconName, const TextType type=TextType::Normal)
display a fontawesome icon
Definition Gui.cpp:993
void text(const std::string &text, const TextOptions &textOptions={}, const WidgetOptions &options={})
display text, if a box size is given it will be formatted to fit in
Definition Gui.cpp:951
void backToAnchor(const std::string &key="")
go back to the last setAnchor position set.
Definition Gui.cpp:217
Style & style()
get current style of the gui
Definition Gui.cpp:94
void setSounds(SoundHolder &sounds)
this must be called once if user want to have sound effects
Definition Gui.cpp:62
void forcePlotUpdate()
force cache update
Definition Gui.cpp:1298
void separation(const float thickness=0.75f)
draw a simple line that span the current group width
Definition Gui.cpp:814
void beginPanel(Panel &settings, const Constraints &constraint={}, const WidgetOptions &options={})
static box in which widget will be arranged.
Definition Gui.cpp:601
bool menuItem(const std::string &text, const WidgetOptions &options={})
menu item are clickable button aligned along menu bar. It need to be enclosed in a beginMenu; { menuI...
Definition Gui.cpp:722
void endPanel()
endPanel need to be called to clean state after beginPanel.
Definition Gui.cpp:645
bool beginWindow(Panel &settings, const Constraints &constraint={}, const WidgetOptions &options={})
windows in which widgets will be arranged, their position are automatically computed....
Definition Gui.cpp:456
void inputVector2(sf::Vector2< Type > &vector, const WidgetOptions &options={}, const sf::Vector2< Type > &min={}, const sf::Vector2< Type > &max={})
modify a vector2 value through text, using two inputNumber call
void plot(const std::function< float(float)> &slope, const float thickness=1.f, const sf::Color &lineColor=sf::Color::White)
plot a function R -> R. The data is cached and not recomputed until user request an update with force...
Definition Gui.cpp:1258
void image(const std::string &textureId, const sf::Vector2f &size={}, const WidgetOptions &options={})
draw an image
Definition Gui.cpp:837
store text accessible with a string key, with option to load and store text for a given tongue,...
Definition TextContainer.h:15
Contains texture position and sub-box of all sprites for a given sprite sheet. Animations ca be handl...
Definition TextureAtlas.h:28
Definition Interpolation.h:16
Slices
Defines number of slices for stretchable widgets.
Definition Widgets.h:39
ItemState
List gui elements states.
Definition ItemStates.h:11
TextType
Text type.
Definition Options.h:13
@ Normal
Definition Options.h:16
ResourcesHolder< sf::SoundBuffer > SoundHolder
Definition ResourcesHolder.h:77
Widget
List widget textures available in gui.
Definition Widgets.h:11
Store constraints on position for gui panel. Alignment always precede relative position.
Definition Constraints.h:36
store panel posiiton, size and some parameters
Definition Panel.h:16
plot range for an axis
Definition Plotter.h:14
define gui font style and padding
Definition Style.h:27
Store text options : boxSize, centered, type, etc.
Definition Options.h:74
store tooltip in a function with its parent widget ID
Definition Options.h:25
Store widget options : tooltip, position, description.
Definition Options.h:41
small struct to ease use of beginPanel and beginWindow in Gui
Definition Panel.h:41