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_111INVR = (4u) << 28,
39 GUI_MODE_TEX_CHANNELS_MASK = (7u) << 28,
40
41 GUI_MODE_MASK = GUI_MODE_TEX_TYPE_MASK | GUI_MODE_TEX_CHANNELS_MASK
42 };
43
45 COGSCORE_DLL_API extern ImDrawCallback setGuiMode;
46
47 struct GuiFont
48 {
49 static constexpr int cNoOfFontSizes = 4;
50
51 ImFontAtlas fontAtlas;
52 ImFont* font[cNoOfFontSizes] = {};
53 float initialSize = 0.0f;
54 std::string path;
55
56 COGSCORE_DLL_API void load(const Context* context, const std::string& name, float size, const ImWchar* glyphRanges);
57 COGSCORE_DLL_API void reloadWithNewGlyphs(const Context* context, const ImWchar* glyphRanges);
58 COGSCORE_DLL_API ImFont* find(float size, float& scale) const;
59 };
60
62 {
63 std::unordered_map<size_t, GuiFont> fonts;
64 GuiFont* defaultFont = nullptr;
65 };
66
67 class COGSCORE_DLL_API ImguiRenderer
68 {
69 public:
70 bool initialize(Context* context);
71 void cleanup();
72
73 ImGuiContext* createGuiContext();
74 void deleteGuiContext(ImGuiContext* guiContext);
75
80 static bool isUsingMouse();
81
86 static bool isUsingKeyboard();
87
88 static void setClipboardCallbacks(GetClipboardTextFn getter, SetClipboardTextFn setter);
89
90 void frame(ImGuiContext* guiContext, ViewContext& view, bool updateio = true);
91 void style();
92
93 void render();
94
95 void addTextToGlyphBuilder(const char* text);
96 void addRangeToGlyphBuilder(const ImWchar* ranges);
97 ImVector<ImWchar> getLoadedRanges() { return loadedRanges; }
98
99 GuiFontRegistry fontRegistry;
100
101 private:
102 bool createResources();
103 void createSampler();
104 void updateConstantBuffer(IContext* deviceContext, uint32_t mode);
105
106 static GetClipboardTextFn getClipboardTextFn;
107 static SetClipboardTextFn setClipboardTextFn;
108
109 Context* context = nullptr;
110 IGraphicsDevice* device = nullptr;
111
112 VertexBufferHandle vertexBuffer;
113 IndexBufferHandle indexBuffer;
114
119
120 Cogs::BufferHandle constantBuffer;
121
122 Cogs::EffectHandle effect;
123 InputLayoutHandle inputLayout;
124
125 VertexFormatHandle format;
126
127 SamplerStateHandle sampler;
128
129 BlendStateHandle blendState;
130 RasterizerStateHandle rasterizerState;
131 DepthStencilStateHandle depthState;
132
133 Timer timer;
134
135 ImGuiContext* imguiContext = nullptr;
136
137 ImFontGlyphRangesBuilder fontGlyphBuilder; // Cache Glyphs in use
138 ImVector<ImWchar> loadedRanges;
139 bool updateLoadedRanges = false;
140
141 uint32_t mode = GUI_MODE_DEFAULT;
142
143 int vertexBufferSize = 5000;
144 int indexBufferSize = 10000;
145
146 // Used for storing current state of touch pointer
147 bool touchPointerHeld = false;
148 // Used for keeping track where the touch pointer was last recorded
149 glm::vec2 touchPointerPosition = { 0.0f, 0.0f };
150 };
151 }
152}
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
COGSCORE_DLL_API ImDrawCallback setGuiMode
Callback for Render updates - not really called.
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
static const Handle_t NoHandle
Represents a handle to nothing.
Definition: Common.h:78
Represents a graphics device context which can receive rendering commands.
Definition: IContext.h:43