Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
ImGuiLayer Class Reference

#include <ImGuiLayer.h>

Inheritance diagram for ImGuiLayer:
sk_app::Window::Layer

Public Types

typedef std::function< void(SkCanvas *)> SkiaWidgetFunc
 

Public Member Functions

 ImGuiLayer ()
 
 ~ImGuiLayer () override
 
void setScaleFactor (float scaleFactor)
 
void skiaWidget (const ImVec2 &size, SkiaWidgetFunc func)
 
void onAttach (sk_app::Window *window) override
 
void onPrePaint () override
 
void onPaint (SkSurface *) override
 
bool onMouse (int x, int y, skui::InputState state, skui::ModifierKey modifiers) override
 
bool onMouseWheel (float delta, int x, int y, skui::ModifierKey modifiers) override
 
bool onKey (skui::Key key, skui::InputState state, skui::ModifierKey modifiers) override
 
bool onChar (SkUnichar c, skui::ModifierKey modifiers) override
 
- Public Member Functions inherited from sk_app::Window::Layer
 Layer ()
 
virtual ~Layer ()=default
 
bool getActive ()
 
void setActive (bool active)
 
virtual void onBackendCreated ()
 
virtual bool onTouch (intptr_t owner, skui::InputState, float x, float y)
 
virtual bool onFling (skui::InputState state)
 
virtual bool onPinch (skui::InputState state, float scale, float x, float y)
 
virtual void onUIStateChanged (const SkString &stateName, const SkString &stateValue)
 
virtual void onResize (int width, int height)
 

Detailed Description

Definition at line 133 of file ImGuiLayer.h.

Member Typedef Documentation

◆ SkiaWidgetFunc

typedef std::function<void(SkCanvas*)> ImGuiLayer::SkiaWidgetFunc

Definition at line 140 of file ImGuiLayer.h.

Constructor & Destructor Documentation

◆ ImGuiLayer()

ImGuiLayer::ImGuiLayer ( )

Definition at line 46 of file ImGuiLayer.cpp.

46 {
47 // ImGui initialization:
48 ImGui::CreateContext();
49 ImGuiIO& io = ImGui::GetIO();
50
51 // Keymap...
52 io.KeyMap[ImGuiKey_Tab] = (int)skui::Key::kTab;
53 io.KeyMap[ImGuiKey_LeftArrow] = (int)skui::Key::kLeft;
54 io.KeyMap[ImGuiKey_RightArrow] = (int)skui::Key::kRight;
55 io.KeyMap[ImGuiKey_UpArrow] = (int)skui::Key::kUp;
56 io.KeyMap[ImGuiKey_DownArrow] = (int)skui::Key::kDown;
57 io.KeyMap[ImGuiKey_PageUp] = (int)skui::Key::kPageUp;
58 io.KeyMap[ImGuiKey_PageDown] = (int)skui::Key::kPageDown;
59 io.KeyMap[ImGuiKey_Home] = (int)skui::Key::kHome;
60 io.KeyMap[ImGuiKey_End] = (int)skui::Key::kEnd;
61 io.KeyMap[ImGuiKey_Delete] = (int)skui::Key::kDelete;
62 io.KeyMap[ImGuiKey_Backspace] = (int)skui::Key::kBack;
63 io.KeyMap[ImGuiKey_Enter] = (int)skui::Key::kOK;
64 io.KeyMap[ImGuiKey_Escape] = (int)skui::Key::kEscape;
65 io.KeyMap[ImGuiKey_A] = (int)skui::Key::kA;
66 io.KeyMap[ImGuiKey_C] = (int)skui::Key::kC;
67 io.KeyMap[ImGuiKey_V] = (int)skui::Key::kV;
68 io.KeyMap[ImGuiKey_X] = (int)skui::Key::kX;
69 io.KeyMap[ImGuiKey_Y] = (int)skui::Key::kY;
70 io.KeyMap[ImGuiKey_Z] = (int)skui::Key::kZ;
71
72 build_ImFontAtlas(*io.Fonts, fFontPaint);
73}
static void build_ImFontAtlas(ImFontAtlas &atlas, SkPaint &fontPaint)
Type::kYUV Type::kRGBA() int(0.7 *637)

◆ ~ImGuiLayer()

ImGuiLayer::~ImGuiLayer ( )
override

Definition at line 75 of file ImGuiLayer.cpp.

75 {
76 ImGui::DestroyContext();
77}

Member Function Documentation

◆ onAttach()

void ImGuiLayer::onAttach ( sk_app::Window window)
overridevirtual

Reimplemented from sk_app::Window::Layer.

Definition at line 102 of file ImGuiLayer.cpp.

102 {
103 fWindow = window;
104
105#if defined(SK_BUILD_FOR_UNIX)
106 ImGuiIO& io = ImGui::GetIO();
107 io.ClipboardUserData = fWindow;
108 io.GetClipboardTextFn = get_clipboard_text;
109 io.SetClipboardTextFn = set_clipboard_text;
110#endif
111}
GLFWwindow * window
Definition main.cc:45

◆ onChar()

bool ImGuiLayer::onChar ( SkUnichar  c,
skui::ModifierKey  modifiers 
)
overridevirtual

Reimplemented from sk_app::Window::Layer.

Definition at line 234 of file ImGuiLayer.cpp.

234 {
235 ImGuiIO& io = ImGui::GetIO();
236 if (io.WantTextInput) {
237 if (c > 0 && c < 0x10000) {
238 io.AddInputCharacter(c);
239 }
240 return true;
241 }
242 return false;
243}

◆ onKey()

bool ImGuiLayer::onKey ( skui::Key  key,
skui::InputState  state,
skui::ModifierKey  modifiers 
)
overridevirtual

Reimplemented from sk_app::Window::Layer.

Definition at line 228 of file ImGuiLayer.cpp.

228 {
229 ImGuiIO& io = ImGui::GetIO();
230 io.KeysDown[static_cast<int>(key)] = (skui::InputState::kDown == state);
231 return io.WantCaptureKeyboard;
232}
AtkStateType state

◆ onMouse()

bool ImGuiLayer::onMouse ( int  x,
int  y,
skui::InputState  state,
skui::ModifierKey  modifiers 
)
overridevirtual

Reimplemented from sk_app::Window::Layer.

Definition at line 113 of file ImGuiLayer.cpp.

113 {
114 ImGuiIO& io = ImGui::GetIO();
115 io.MousePos.x = static_cast<float>(x);
116 io.MousePos.y = static_cast<float>(y);
118 io.MouseDown[0] = true;
119 } else if (skui::InputState::kUp == state) {
120 io.MouseDown[0] = false;
121 }
122 return io.WantCaptureMouse;
123}
double y
double x

◆ onMouseWheel()

bool ImGuiLayer::onMouseWheel ( float  delta,
int  x,
int  y,
skui::ModifierKey  modifiers 
)
overridevirtual

Reimplemented from sk_app::Window::Layer.

Definition at line 125 of file ImGuiLayer.cpp.

125 {
126 ImGuiIO& io = ImGui::GetIO();
127 io.MouseWheel += delta;
128 return io.WantCaptureMouse;
129}

◆ onPaint()

void ImGuiLayer::onPaint ( SkSurface surface)
overridevirtual

Reimplemented from sk_app::Window::Layer.

Definition at line 157 of file ImGuiLayer.cpp.

157 {
158 // This causes ImGui to rebuild vertex/index data based on all immediate-mode commands
159 // (widgets, etc...) that have been issued
160 ImGui::Render();
161
162 // Then we fetch the most recent data, and convert it so we can render with Skia
163 const ImDrawData* drawData = ImGui::GetDrawData();
167
168 auto canvas = surface->getCanvas();
169
170 for (int i = 0; i < drawData->CmdListsCount; ++i) {
171 const ImDrawList* drawList = drawData->CmdLists[i];
172
173 // De-interleave all vertex data (sigh), convert to Skia types
174 pos.clear(); uv.clear(); color.clear();
175 for (int j = 0; j < drawList->VtxBuffer.size(); ++j) {
176 const ImDrawVert& vert = drawList->VtxBuffer[j];
177 pos.push_back(SkPoint::Make(vert.pos.x, vert.pos.y));
178 uv.push_back(SkPoint::Make(vert.uv.x, vert.uv.y));
179 color.push_back(vert.col);
180 }
181 // ImGui colors are RGBA
182 SkSwapRB(color.begin(), color.begin(), color.size());
183
184 int indexOffset = 0;
185
186 // Draw everything with canvas.drawVertices...
187 for (int j = 0; j < drawList->CmdBuffer.size(); ++j) {
188 const ImDrawCmd* drawCmd = &drawList->CmdBuffer[j];
189
190 SkAutoCanvasRestore acr(canvas, true);
191
192 // TODO: Find min/max index for each draw, so we know how many vertices (sigh)
193 if (drawCmd->UserCallback) {
194 drawCmd->UserCallback(drawList, drawCmd);
195 } else {
196 intptr_t idIndex = (intptr_t)drawCmd->TextureId;
197 if (idIndex < fSkiaWidgetFuncs.size()) {
198 // Small image IDs are actually indices into a list of callbacks. We directly
199 // examing the vertex data to deduce the image rectangle, then reconfigure the
200 // canvas to be clipped and translated so that the callback code gets to use
201 // Skia to render a widget in the middle of an ImGui panel.
202 ImDrawIdx rectIndex = drawList->IdxBuffer[indexOffset];
203 SkPoint tl = pos[rectIndex], br = pos[rectIndex + 2];
204 canvas->clipRect(SkRect::MakeLTRB(tl.fX, tl.fY, br.fX, br.fY));
205 canvas->translate(tl.fX, tl.fY);
206 fSkiaWidgetFuncs[idIndex](canvas);
207 } else {
208 SkPaint* paint = static_cast<SkPaint*>(drawCmd->TextureId);
210
211 canvas->clipRect(SkRect::MakeLTRB(drawCmd->ClipRect.x, drawCmd->ClipRect.y,
212 drawCmd->ClipRect.z, drawCmd->ClipRect.w));
214 drawList->VtxBuffer.size(),
215 pos.begin(), uv.begin(), color.begin(),
216 drawCmd->ElemCount,
217 drawList->IdxBuffer.begin() + indexOffset);
218 canvas->drawVertices(vertices, SkBlendMode::kModulate, *paint);
219 }
220 indexOffset += drawCmd->ElemCount;
221 }
222 }
223 }
224
225 fSkiaWidgetFuncs.clear();
226}
SkPoint pos
SkColor4f color
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kModulate
r = s*d
SK_API void SkSwapRB(uint32_t *dest, const uint32_t *src, int count)
Definition SkSwizzle.cpp:12
void push_back(const T &v)
Definition SkTDArray.h:219
T * begin()
Definition SkTDArray.h:150
void clear()
Definition SkTDArray.h:175
static sk_sp< SkVertices > MakeCopy(VertexMode mode, int vertexCount, const SkPoint positions[], const SkPoint texs[], const SkColor colors[], int indexCount, const uint16_t indices[])
@ kTriangles_VertexMode
Definition SkVertices.h:31
int size() const
Definition SkTArray.h:416
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
float fX
x-axis value
static constexpr SkPoint Make(float x, float y)
float fY
y-axis value
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646

◆ onPrePaint()

void ImGuiLayer::onPrePaint ( )
overridevirtual

Reimplemented from sk_app::Window::Layer.

Definition at line 137 of file ImGuiLayer.cpp.

137 {
138 // Update ImGui input
139 ImGuiIO& io = ImGui::GetIO();
140
141 static double previousTime = 0.0;
142 double currentTime = SkTime::GetSecs();
143 io.DeltaTime = static_cast<float>(currentTime - previousTime);
144 previousTime = currentTime;
145
146 io.DisplaySize.x = static_cast<float>(fWindow->width());
147 io.DisplaySize.y = static_cast<float>(fWindow->height());
148
149 io.KeyAlt = io.KeysDown[static_cast<int>(skui::Key::kOption)];
150 io.KeyCtrl = io.KeysDown[static_cast<int>(skui::Key::kCtrl)];
151 io.KeyShift = io.KeysDown[static_cast<int>(skui::Key::kShift)];
152 io.KeySuper = io.KeysDown[static_cast<int>(skui::Key::kSuper)];
153
154 ImGui::NewFrame();
155}
int height() const
Definition Window.cpp:130
int width() const
Definition Window.cpp:123
double GetSecs()
Definition SkTime.h:16

◆ setScaleFactor()

void ImGuiLayer::setScaleFactor ( float  scaleFactor)

Definition at line 79 of file ImGuiLayer.cpp.

79 {
80 ImGui::GetStyle().ScaleAllSizes(scaleFactor);
81
82 ImFontAtlas& atlas = *ImGui::GetIO().Fonts;
83 atlas.Clear();
84 ImFontConfig cfg;
85 cfg.SizePixels = 13 * scaleFactor;
86 atlas.AddFontDefault(&cfg);
87 build_ImFontAtlas(atlas, fFontPaint);
88}
sk_sp< const SkImage > atlas
Definition SkRecords.h:331

◆ skiaWidget()

void ImGuiLayer::skiaWidget ( const ImVec2 &  size,
SkiaWidgetFunc  func 
)

Definition at line 131 of file ImGuiLayer.cpp.

131 {
132 intptr_t funcIndex = fSkiaWidgetFuncs.size();
133 fSkiaWidgetFuncs.push_back(func);
134 ImGui::Image((ImTextureID)funcIndex, size);
135}

The documentation for this class was generated from the following files: