Cogs.Core
ImguiRenderer.h
1#pragma once
2
3#include "Base.h"
4#include "../../Bridge/BridgeApi.h"
5
6#include "Rendering/IGraphicsDevice.h"
7#include "Rendering/VertexFormat.h"
8
9#include "Foundation/StringView.h"
10#include "Foundation/Platform/Timer.h"
11
12#include "imgui.h"
13
14#include <unordered_map>
15
16namespace Cogs
17{
18 namespace Core
19 {
20 class Context;
21 class ViewContext;
22
23 enum : uint32_t {
24 GUI_MODE_DEFAULT = 0,
25
26 GUI_MODE_TEX_OFFSET_MASK = 255,
27
28 GUI_MODE_TEX_TYPE_2D = (0u) << 26,
29 GUI_MODE_TEX_TYPE_ARRAY = (1u) << 26,
30 GUI_MODE_TEX_TYPE_CUBE = (2u) << 26,
31 GUI_MODE_TEX_TYPE_2DMS = (3u) << 26,
32 GUI_MODE_TEX_TYPE_MASK = (3u) << 26,
33
34 GUI_MODE_TEX_CHANNELS_RGB = (0u) << 28,
35 GUI_MODE_TEX_CHANNELS_ALPHA = (1u) << 28,
36 GUI_MODE_TEX_CHANNELS_RED = (2u) << 28,
37 GUI_MODE_TEX_CHANNELS_111R = (3u) << 28,
38 GUI_MODE_TEX_CHANNELS_MASK = (7u) << 28,
39
40 GUI_MODE_MASK = GUI_MODE_TEX_TYPE_MASK | GUI_MODE_TEX_CHANNELS_MASK
41 };
42
44 extern ImDrawCallback setGuiMode;
45
46 struct GuiFont
47 {
48 static constexpr int cNoOfFontSizes = 4;
49
50 ImFontAtlas fontAtlas;
51 ImFont* font[cNoOfFontSizes] = {};
52 float initialSize = 0.0f;
53 std::string path;
54
55 COGSCORE_DLL_API void load(const Context* context, const std::string& name, float size, const ImWchar* glyphRanges);
56 COGSCORE_DLL_API void reloadWithNewGlyphs(const Context* context, const ImWchar* glyphRanges);
57 COGSCORE_DLL_API ImFont* find(float size, float& scale) const;
58 };
59
61 {
62 std::unordered_map<size_t, GuiFont> fonts;
63 GuiFont* defaultFont = nullptr;
64 };
65
66 class COGSCORE_DLL_API ImguiRenderer
67 {
68 public:
69 bool initialize(Context* context);
70 void cleanup();
71
72 ImGuiContext* createGuiContext();
73 void deleteGuiContext(ImGuiContext* guiContext);
74
79 static bool isUsingMouse();
80
85 static bool isUsingKeyboard();
86
87 static void setClipboardCallbacks(GetClipboardTextFn getter, SetClipboardTextFn setter);
88
89 void frame(ImGuiContext* guiContext, ViewContext& view, bool updateio = true);
90 void style();
91
92 void render();
93
94 void addTextToGlyphBuilder(const char* text);
95 void addRangeToGlyphBuilder(const ImWchar* ranges);
96 ImVector<ImWchar> getLoadedRanges() { return loadedRanges; }
97
98 GuiFontRegistry fontRegistry;
99
100 private:
101 bool createResources();
102 void createSampler();
103 void updateConstantBuffer(IContext* deviceContext, uint32_t mode);
104
105 static GetClipboardTextFn getClipboardTextFn;
106 static SetClipboardTextFn setClipboardTextFn;
107
108 Context* context = nullptr;
109 IGraphicsDevice* device = nullptr;
110
111 VertexBufferHandle vertexBuffer;
112 IndexBufferHandle indexBuffer;
113
114 Cogs::BufferHandle constantBuffer;
115
116 Cogs::EffectHandle effect;
117 InputLayoutHandle inputLayout;
118
119 VertexFormatHandle format;
120
121 SamplerStateHandle sampler;
122
123 BlendStateHandle blendState;
124 RasterizerStateHandle rasterizerState;
125 DepthStencilStateHandle depthState;
126
127 Timer timer;
128
129 ImGuiContext* imguiContext = nullptr;
130
131 ImFontGlyphRangesBuilder fontGlyphBuilder; // Cache Glyphs in use
132 ImVector<ImWchar> loadedRanges;
133 bool updateLoadedRanges = false;
134
135 uint32_t mode = GUI_MODE_DEFAULT;
136
137 int vertexBufferSize = 5000;
138 int indexBufferSize = 10000;
139
140 // Used for storing current state of touch pointer
141 bool touchPointerHeld = false;
142 // Used for keeping track where the touch pointer was last recorded
143 glm::uvec2 touchPointerPosition{ 0,0 };
144 };
145 }
146}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Represents a graphics device used to manage graphics resources and issue drawing commands.
Old timer class.
Definition: Timer.h:37
ImDrawCallback setGuiMode
Callback for Render updates - not really called.
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Represents a graphics device context which can receive rendering commands.
Definition: IContext.h:43