Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
GUI.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef GUI_H_
34#define GUI_H_
35
36#include "GUIInterface.h"
43namespace CEGUI
44{
45 class CEGUIRenderer;
46 class DVDTextureTarget;
47};
48
49struct FONScontext;
50
51namespace Divide
52{
53
54namespace GFX
55{
56 class CommandBuffer;
57 struct MemoryBarrierCommand;
58};
59
60class Scene;
61class Pipeline;
62class GUIConsole;
63class GUIElement;
64class ShaderProgram;
65class ResourceCache;
66class SceneGraphNode;
67class PlatformContext;
68class SceneGUIElements;
69
70struct ImageView;
71struct TextElementBatch;
72struct SizeChangeParams;
73
75
77class GUI final : public GUIInterface,
78 public KernelComponent,
79 public FrameListener,
81{
82 public:
84
85 public:
86 explicit GUI( Kernel& parent );
87 ~GUI() override;
88
90 [[nodiscard]] ErrorCode init( PlatformContext& context );
91 void destroy();
92
94 void preDraw( GFXDevice& context, const Rect<I32>& viewport, GFX::CommandBuffer& bufferInOut, GFX::MemoryBarrierCommand& memCmdInOut );
96 void draw( GFXDevice& context, const Rect<I32>& viewport, GFX::CommandBuffer& bufferInOut, GFX::MemoryBarrierCommand& memCmdInOut );
98 void drawText( const TextElementBatch& batch, const Rect<I32>& targetViewport, GFX::CommandBuffer& bufferInOut, GFX::MemoryBarrierCommand& memCmdInOut, bool pushCamera = true);
100 void onResolutionChange( const SizeChangeParams& params );
102 void onChangeScene( Scene* newScene );
104 void onUnloadScene( Scene* scene );
106 void update( U64 deltaTimeUS );
108 template <typename T> requires std::is_base_of_v<GUIElement, T>
109 [[nodiscard]] T* getSceneGUIElementImpl( const I64 sceneID, const U64 elementName ) { return static_cast<T*>(getSceneGUIElementImpl( sceneID, elementName, T::Type )); }
111 template <typename T> requires std::is_base_of_v<GUIElement, T>
112 [[nodiscard]] T* getSceneGUIElementImpl( const I64 sceneID, const I64 elementID ) { return static_cast<T*>(getSceneGUIElementImpl( sceneID, elementID, T::Type )); }
114 [[nodiscard]] GUIConsole& getConsole() noexcept { return *_console; }
116 [[nodiscard]] const GUIConsole& getConsole() const noexcept { return *_console; }
118 [[nodiscard]] CEGUI::Window* rootSheet() const noexcept { return _rootSheet; }
120 [[nodiscard]] GUIMessageBox* getDefaultMessageBox() const noexcept { return _defaultMsgBox.get(); }
122 void setCursorPosition( I32 x, I32 y );
124 [[nodiscard]] CEGUI::GUIContext* getCEGUIContext() noexcept;
126 void showDebugCursor( bool state );
130 PROPERTY_R( string, defaultGUIScheme, "GWEN" );
132 PROPERTY_RW( U64, textRenderInterval, Time::MillisecondsToMicroseconds( 10 ) );
133
134
135 protected:
136 bool frameStarted( const FrameEvent& evt ) override;
137
139 [[nodiscard]] bool onKeyDown( const Input::KeyEvent& key ) override;
141 [[nodiscard]] bool onKeyUp( const Input::KeyEvent& key ) override;
143 [[nodiscard]] bool joystickAxisMoved( const Input::JoystickEvent& arg ) override;
145 [[nodiscard]] bool joystickPovMoved( const Input::JoystickEvent& arg ) override;
147 [[nodiscard]] bool joystickButtonPressed( const Input::JoystickEvent& arg ) override;
149 [[nodiscard]] bool joystickButtonReleased( const Input::JoystickEvent& arg ) override;
150 [[nodiscard]] bool joystickBallMoved( const Input::JoystickEvent& arg ) override;
151 [[nodiscard]] bool joystickAddRemove( const Input::JoystickEvent& arg ) override;
152 [[nodiscard]] bool joystickRemap( const Input::JoystickEvent& arg ) override;
154 [[nodiscard]] bool mouseMoved( const Input::MouseMoveEvent& arg ) override;
156 [[nodiscard]] bool mouseButtonPressed( const Input::MouseButtonEvent& arg ) override;
158 [[nodiscard]] bool mouseButtonReleased( const Input::MouseButtonEvent& arg ) override;
160 [[nodiscard]] bool onTextEvent( const Input::TextEvent& arg ) noexcept override;
161
162 private:
164 [[nodiscard]] I32 getFont( const Str<64>& fontName );
166 [[nodiscard]] GUIElement* getSceneGUIElementImpl( I64 sceneID, U64 elementName, GUIType type ) const;
168 [[nodiscard]] GUIElement* getSceneGUIElementImpl( I64 sceneID, I64 elementID, GUIType type ) const;
171
172 protected:
173 friend class SceneGUIElements;
175 CEGUI::Window* _rootSheet{nullptr};
177 CEGUI::GUIContext* _ceguiContext{nullptr};
178
179 private:
181 bool _init{false};
189 std::unique_ptr<GUIConsole> _console;
191 std::unique_ptr<GUIMessageBox> _defaultMsgBox;
199 DVDFONSContext_uptr _fonsContext;
203 hashAlg::pair<Str<64>, I32> _fontCache = { "", -1 };
207 Handle<ShaderProgram> _textRenderShader = INVALID_HANDLE<ShaderProgram>;
208 Handle<ShaderProgram> _ceguiRenderShader = INVALID_HANDLE<ShaderProgram>;
209 };
210
211}; // namespace Divide
212#endif
#define PROPERTY_RW(...)
Convenience method to add a class member with public read access and write access.
#define PROPERTY_R(...)
Convenience method to add a class member with public read access but protected write access.
#define FWD_DECLARE_MANAGED_STRUCT(T)
This class defines AutoRepeatKey::repeatKey(...) as CEGUI key inputs.
Definition: CEGUIInput.h:46
Rough around the edges Adapter pattern abstracting the actual rendering API and access to the GPU.
Definition: GFXDevice.h:215
GUIConsole implementation, CEGUI based, as in the practical tutorial series.
Definition: GUIConsole.h:52
Graphical User Interface.
Definition: GUI.h:81
CEGUI::GUIContext * getCEGUIContext() noexcept
Provides direct access to the CEGUI context. Used by plugins (e.g. GUIConsole, GUIInput,...
Definition: GUI.cpp:862
CEGUI::Window * _rootSheet
The root window into which CEGUI anchors all of its elements.
Definition: GUI.h:175
CEGUI::Window * rootSheet() const noexcept
Get a pointer to the root sheet that CEGUI renders into.
Definition: GUI.h:118
bool joystickPovMoved(const Input::JoystickEvent &arg) override
Joystick direction change: return true if input was consumed.
Definition: GUI.cpp:786
void destroy()
Definition: GUI.cpp:662
bool onKeyUp(const Input::KeyEvent &key) override
Key released: return true if input was consumed.
Definition: GUI.cpp:756
std::unique_ptr< GUIConsole > _console
Our custom in-game console (for logs and commands. A la Quake's ~-console)
Definition: GUI.h:189
ErrorCode init(PlatformContext &context)
Create the GUI.
Definition: GUI.cpp:399
GUIConsole & getConsole() noexcept
Get a reference to our console window.
Definition: GUI.h:114
bool onTextEvent(const Input::TextEvent &arg) noexcept override
Called when text input was detected.
Definition: GUI.cpp:820
bool joystickButtonReleased(const Input::JoystickEvent &arg) override
Joystick button released: return true if input was consumed.
Definition: GUI.cpp:798
~GUI() override
Definition: GUI.cpp:135
std::unique_ptr< GUIMessageBox > _defaultMsgBox
Pointer to a default message box used for general purpose messages.
Definition: GUI.h:191
CEGUIInput _ceguiInput
Used to implement key repeat.
Definition: GUI.h:183
void recreateDefaultMessageBox()
Used to recreate and re-register the default message box if needed (usually on scene change)
Definition: GUI.cpp:647
CEGUI::GUIContext * _ceguiContext
The CEGUI context as returned by the library upon creation.
Definition: GUI.h:177
void setCursorPosition(I32 x, I32 y)
Mouse cursor forced to a certain position.
Definition: GUI.cpp:741
DVDFONSContext_uptr _fonsContext
We use Font Stash (https://github.com/memononen/fontstash) for rendering basic text on the screen....
Definition: GUI.h:199
Handle< ShaderProgram > _textRenderShader
The text rendering shaderProgram we used to draw Font Stash text.
Definition: GUI.h:207
void preDraw(GFXDevice &context, const Rect< I32 > &viewport, GFX::CommandBuffer &bufferInOut, GFX::MemoryBarrierCommand &memCmdInOut)
Render all elements that need their own internal render targets (e.g. CEGUI)
Definition: GUI.cpp:277
bool mouseButtonPressed(const Input::MouseButtonEvent &arg) override
Mouse button pressed: return true if input was consumed.
Definition: GUI.cpp:768
bool joystickAddRemove(const Input::JoystickEvent &arg) override
Definition: GUI.cpp:810
bool joystickBallMoved(const Input::JoystickEvent &arg) override
Definition: GUI.cpp:804
void draw(GFXDevice &context, const Rect< I32 > &viewport, GFX::CommandBuffer &bufferInOut, GFX::MemoryBarrierCommand &memCmdInOut)
Go through all of the registered scene gui elements and gather all of the render commands.
Definition: GUI.cpp:302
Pipeline * _textRenderPipeline
The text rendering pipeline we used to draw Font Stash text.
Definition: GUI.h:205
void showDebugCursor(bool state)
Toggle debug cursor rendering on or off.
Definition: GUI.cpp:709
void onUnloadScene(Scene *scene)
When we unload a scene, we unload all of its GUI elements. ToDo: Scene should own these and scene sho...
Definition: GUI.cpp:169
void onChangeScene(Scene *newScene)
When we change a scene, we want to toggle our current scene GUI elements off and toggle the new scene...
Definition: GUI.cpp:140
bool joystickRemap(const Input::JoystickEvent &arg) override
Definition: GUI.cpp:815
hashMap< I64, SceneGUIElements * > GUIMapPerScene
Definition: GUI.h:83
void onResolutionChange(const SizeChangeParams &params)
Mostly used by CEGUI to keep track of screen dimensions.
Definition: GUI.cpp:719
bool joystickAxisMoved(const Input::JoystickEvent &arg) override
Joystick axis change: return true if input was consumed.
Definition: GUI.cpp:780
I32 getFont(const Str< 64 > &fontName)
Try to find the requested font in the font cache. Load on cache miss.
Definition: GUI.cpp:868
bool mouseMoved(const Input::MouseMoveEvent &arg) override
Mouse moved: return true if input was consumed.
Definition: GUI.cpp:762
T * getSceneGUIElementImpl(const I64 sceneID, const I64 elementID)
Find a return a gui element by ID.
Definition: GUI.h:112
bool joystickButtonPressed(const Input::JoystickEvent &arg) override
Joystick button pressed: return true if input was consumed.
Definition: GUI.cpp:792
GUIMapPerScene _guiStack
All the GUI elements created per scene.
Definition: GUI.h:195
bool onKeyDown(const Input::KeyEvent &key) override
Key pressed: return true if input was consumed.
Definition: GUI.cpp:750
const GUIConsole & getConsole() const noexcept
Get a const reference to our console window.
Definition: GUI.h:116
bool frameStarted(const FrameEvent &evt) override
Definition: GUI.cpp:381
GUIMessageBox * getDefaultMessageBox() const noexcept
Return a pointer to the default, general purpose message box.
Definition: GUI.h:120
T * getSceneGUIElementImpl(const I64 sceneID, const U64 elementName)
Find a return a gui element by name.
Definition: GUI.h:109
bool mouseButtonReleased(const Input::MouseButtonEvent &arg) override
Mouse button released: return true if input was consumed.
Definition: GUI.cpp:774
void update(U64 deltaTimeUS)
Main update call. Used to tick gui elements (cursors, animations, etc)
Definition: GUI.cpp:358
bool _init
Set to true when the GUI has finished loading.
Definition: GUI.h:181
SharedMutex _guiStackLock
A lock to protect access to _guiStack.
Definition: GUI.h:197
CEGUI::CEGUIRenderer * _ceguiRenderer
Used to render CEGUI to a texture; We want to port this to the Divide::GFX interface.
Definition: GUI.h:185
hashMap< U64, I32 > _fonts
A cache of all font IDs used by Font Stash stored by name ID.
Definition: GUI.h:201
Scene * _activeScene
Each scene has its own gui elements! (0 = global). We keep a pointer to the scene but we really shoul...
Definition: GUI.h:193
CEGUI::DVDTextureTarget * _renderTextureTarget
Used to render CEGUI elements into. We blit this on top of our scene target.
Definition: GUI.h:187
Handle< ShaderProgram > _ceguiRenderShader
Definition: GUI.h:208
hashAlg::pair< Str< 64 >, I32 > _fontCache
A cache of the last requested font by name to avoid a lookup in the fonts map.
Definition: GUI.h:203
void drawText(const TextElementBatch &batch, const Rect< I32 > &targetViewport, GFX::CommandBuffer &bufferInOut, GFX::MemoryBarrierCommand &memCmdInOut, bool pushCamera=true)
Text rendering is handled exclusively by Mikko Mononen's FontStash library (https://github....
Definition: GUI.cpp:180
Kernel & parent() noexcept
The kernel is the main system that connects all of our various systems: windows, gfx,...
Definition: Kernel.h:81
struct FONScontext FONScontext
Definition: fontstash.h:119
constexpr Optick::Category::Type Scene
Definition: Profiler.h:66
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
int32_t I32
std::shared_mutex SharedMutex
Definition: SharedMutex.h:43
hashAlg::unordered_map< K, V, HashFun, Predicate > hashMap
Definition: HashMap.h:55
int64_t I64
uint64_t U64