Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
ImGuiLayer.h
Go to the documentation of this file.
1/*
2* Copyright 2017 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
8#ifndef ImGuiLayer_DEFINED
9#define ImGuiLayer_DEFINED
10
19#include "tools/sk_app/Window.h"
20
21#include <algorithm>
22#include <functional>
23
24#include "imgui.h"
25
26class SkCanvas;
27class SkSurface;
28
29namespace skui {
30enum class InputState;
31enum class Key;
32enum class ModifierKey;
33} // namespace skui
34
35namespace ImGui {
36
37// Helper object for drawing in a widget region, with draggable points
38struct DragCanvas {
39 DragCanvas(const void* id, SkPoint tl = { 0.0f, 0.0f }, SkPoint br = { 1.0f, 1.0f },
40 float aspect = -1.0f)
41 : fID(0), fDragging(false) {
42 ImGui::PushID(id);
43 fDrawList = ImGui::GetWindowDrawList();
44
45 // Logical size
46 SkScalar w = SkTAbs(br.fX - tl.fX),
47 h = SkTAbs(br.fY - tl.fY);
48
49 // Determine aspect ratio automatically by default
50 if (aspect < 0) {
51 aspect = h / w;
52 }
53
54 float availWidth = std::max(ImGui::GetContentRegionAvailWidth(), 1.0f);
55 fPos = ImGui::GetCursorScreenPos();
56 fSize = ImVec2(availWidth, availWidth * aspect);
57
58 SkPoint local[4] = {
59 { tl.fX, tl.fY },
60 { br.fX, tl.fY },
61 { tl.fX, br.fY },
62 { br.fX, br.fY },
63 };
64 SkPoint screen[4] = {
65 { fPos.x , fPos.y },
66 { fPos.x + fSize.x, fPos.y },
67 { fPos.x , fPos.y + fSize.y },
68 { fPos.x + fSize.x, fPos.y + fSize.y },
69 };
70 fLocalToScreen.setPolyToPoly(local, screen, 4);
71 fScreenToLocal.setPolyToPoly(screen, local, 4);
72 }
73
75 ImGui::SetCursorScreenPos(ImVec2(fPos.x, fPos.y + fSize.y));
76 ImGui::Spacing();
77 ImGui::PopID();
78 }
79
80 void fillColor(ImU32 color) {
81 fDrawList->AddRectFilled(fPos, ImVec2(fPos.x + fSize.x, fPos.y + fSize.y), color);
82 }
83
84 void dragPoint(SkPoint* p, bool tooltip = false, ImU32 color = 0xFFFFFFFF) {
85 // Transform points from logical coordinates to screen coordinates
86 SkPoint center = fLocalToScreen.mapXY(p->fX, p->fY);
87
88 // Invisible 10x10 button
89 ImGui::PushID(fID++);
90 ImGui::SetCursorScreenPos(ImVec2(center.fX - 5, center.fY - 5));
91 ImGui::InvisibleButton("", ImVec2(10, 10));
92
93 if (ImGui::IsItemActive() && ImGui::IsMouseDragging(0)) {
94 // Update screen position to track mouse, clamped to our area
95 ImGuiIO& io = ImGui::GetIO();
96 center.set(SkTPin(io.MousePos.x, fPos.x, fPos.x + fSize.x),
97 SkTPin(io.MousePos.y, fPos.y, fPos.y + fSize.y));
98
99 // Update local coordinates for the caller
100 *p = fScreenToLocal.mapXY(center.fX, center.fY);
101 fDragging = true;
102 }
103
104 if (tooltip && ImGui::IsItemHovered()) {
105 ImGui::SetTooltip("x: %.3f\ny: %.3f", p->fX, p->fY);
106 }
107
108 ImGui::PopID();
109
110 fScreenPoints.push_back(ImVec2(center.fX, center.fY));
111 fDrawList->AddCircle(fScreenPoints.back(), 5.0f, color);
112 }
113
114 ImDrawList* fDrawList;
115
116 // Location and dimensions (in screen coordinates)
117 ImVec2 fPos;
118 ImVec2 fSize;
119
120 // Screen coordinates of points (for additional user drawing)
122
123 // To simplify dragPoint
126
127 int fID;
129};
130
131} // namespace ImGui
132
134public:
135 ImGuiLayer();
136 ~ImGuiLayer() override;
137
138 void setScaleFactor(float scaleFactor);
139
140 typedef std::function<void(SkCanvas*)> SkiaWidgetFunc;
141 void skiaWidget(const ImVec2& size, SkiaWidgetFunc func);
142
143 void onAttach(sk_app::Window* window) override;
144 void onPrePaint() override;
145 void onPaint(SkSurface*) override;
146 bool onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) override;
147 bool onMouseWheel(float delta, int x, int y, skui::ModifierKey modifiers) override;
148 bool onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) override;
149 bool onChar(SkUnichar c, skui::ModifierKey modifiers) override;
150
151private:
152 sk_app::Window* fWindow;
153 SkPaint fFontPaint;
155};
156
157#endif
SkColor4f color
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition SkTPin.h:19
static T SkTAbs(T value)
Definition SkTemplates.h:43
int32_t SkUnichar
Definition SkTypes.h:175
static SkScalar center(float pos0, float pos1)
bool onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers) override
void onAttach(sk_app::Window *window) override
void onPaint(SkSurface *) override
void onPrePaint() override
bool onMouseWheel(float delta, int x, int y, skui::ModifierKey modifiers) override
~ImGuiLayer() override
std::function< void(SkCanvas *)> SkiaWidgetFunc
Definition ImGuiLayer.h:140
void setScaleFactor(float scaleFactor)
bool onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers) override
void skiaWidget(const ImVec2 &size, SkiaWidgetFunc func)
bool onChar(SkUnichar c, skui::ModifierKey modifiers) override
bool setPolyToPoly(const SkPoint src[], const SkPoint dst[], int count)
void mapXY(SkScalar x, SkScalar y, SkPoint *result) const
Definition SkMatrix.cpp:777
GLFWwindow * window
Definition main.cc:45
float SkScalar
Definition extension.cpp:12
AtkStateType state
double y
double x
InputState
Definition InputState.h:6
ModifierKey
Definition ModifierKey.h:9
Key
Definition Key.h:6
SkScalar w
SkScalar h
ImDrawList * fDrawList
Definition ImGuiLayer.h:114
skia_private::STArray< 4, ImVec2, true > fScreenPoints
Definition ImGuiLayer.h:121
void dragPoint(SkPoint *p, bool tooltip=false, ImU32 color=0xFFFFFFFF)
Definition ImGuiLayer.h:84
DragCanvas(const void *id, SkPoint tl={ 0.0f, 0.0f }, SkPoint br={ 1.0f, 1.0f }, float aspect=-1.0f)
Definition ImGuiLayer.h:39
SkMatrix fScreenToLocal
Definition ImGuiLayer.h:125
SkMatrix fLocalToScreen
Definition ImGuiLayer.h:124
void fillColor(ImU32 color)
Definition ImGuiLayer.h:80
float fX
x-axis value
float fY
y-axis value
constexpr float x() const