-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
docker.cpp
259 lines (212 loc) · 6.28 KB
/
docker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* ReaImGui: ReaScript binding for Dear ImGui
* Copyright (C) 2021 Christian Fillion
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "docker.hpp"
#include "context.hpp"
#include "platform.hpp"
#include "window.hpp"
#include <cassert>
#include <imgui/imgui.h>
#include <imgui/imgui_internal.h>
#include <reaper_plugin_functions.h>
Docker::Docker(const ReaDockID id)
: m_id { id }
{
snprintf(m_windowTitle, sizeof(m_windowTitle), "reaimgui_docker_%X", id);
m_windowId = ImHashStr(m_windowTitle);
}
void Docker::draw()
{
constexpr ImGuiWindowFlags windowFlags {
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus |
ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoBackground |
ImGuiWindowFlags_NoSavedSettings
};
constexpr ImGuiDockNodeFlags dockSpaceFlags {
ImGuiDockNodeFlags_AutoHideTabBar | ImGuiDockNodeFlags_PassthruCentralNode
};
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 0.f, 0.f });
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.f);
const int visible { ImGui::Begin(m_windowTitle, nullptr, windowFlags) };
ImGui::PopStyleVar(3);
if(visible) // just in case, altough NoDecoration implies NoCollapse
ImGui::DockSpace(nodeId(), { 0.f, 0.f }, dockSpaceFlags);
ImGui::End();
}
bool Docker::isActive() const
{
const ImGuiDockNode *node { ImGui::DockBuilderGetNode(nodeId()) };
if(!node || node->IsEmpty())
return false;
const ImGuiWindow *window { node->VisibleWindow };
return window && (window->Active || window->WasActive) && window->DockIsActive;
}
void Docker::moveTo(Docker *target)
{
assert(target && target != this);
if(target->isActive()) {
reset(); // undock all contained windows
return;
}
// The target docker is unused: move our contents to it and take its place
// to reuse the same platform window and keep using the same docker instance.
ImVector<const char *> remap;
ImGui::DockBuilderCopyDockSpace(nodeId(), target->nodeId(), &remap);
reset(); // clear out the previous node to hide it
std::swap(m_id, target->m_id);
}
void Docker::reset()
{
// the node will be re-created next frame in draw()
ImGui::DockBuilderRemoveNode(nodeId());
}
template <ImGuiID... IDs>
constexpr std::array<Docker, sizeof...(IDs)>
makeDockers(std::integer_sequence<ReaDockID, IDs...>) { return { IDs... }; }
DockerList::DockerList()
: m_dockers { makeDockers(std::make_integer_sequence<ReaDockID, DOCKER_COUNT>{}) }
{
}
void DockerList::drawAll()
{
if(!(ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_DockingEnable))
return;
for(Docker &docker : m_dockers)
docker.draw();
}
Docker *DockerList::findById(const ReaDockID id)
{
for(Docker &docker : m_dockers) {
if(docker.id() == id)
return &docker;
}
return nullptr;
}
Docker *DockerList::findByViewport(const ImGuiViewport *viewport)
{
const auto *viewportPrivate { static_cast<const ImGuiViewportP *>(viewport) };
if(ImGuiWindow *userWindow { viewportPrivate->Window }) {
for(Docker &docker : m_dockers) {
if(docker.windowId() == userWindow->ID)
return &docker;
}
}
return nullptr;
}
DockerHost::DockerHost(Docker *docker, ImGuiViewport *viewport)
: Viewport { viewport }, m_docker { docker }
{
}
void DockerHost::activate()
{
m_window.reset(Platform::createWindow(m_viewport, this));
m_window->create();
HWND hwnd { m_window->nativeHandle() };
m_viewport->PlatformHandle = hwnd;
constexpr const char *INI_KEY { "reaimgui" };
Dock_UpdateDockID(INI_KEY, m_docker->id());
DockWindowAddEx(hwnd, m_ctx->name(), INI_KEY, true);
m_window->show();
if(!(m_viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing))
DockWindowActivate(hwnd);
}
void DockerHost::create()
{
}
HWND DockerHost::nativeHandle() const
{
return m_window ? m_window->nativeHandle() : nullptr;
}
void DockerHost::show()
{
}
void DockerHost::setPosition(ImVec2)
{
}
ImVec2 DockerHost::getPosition() const
{
return m_window ? m_window->getPosition() : ImVec2{};
}
void DockerHost::setSize(ImVec2)
{
}
ImVec2 DockerHost::getSize() const
{
return m_window ? m_window->getSize() : ImVec2{};
}
void DockerHost::setFocus()
{
if(m_window)
m_window->setFocus();
}
bool DockerHost::hasFocus() const
{
return m_window ? m_window->hasFocus() : false;
}
bool DockerHost::isMinimized() const
{
return m_window ? m_window->isMinimized() : true;
}
void DockerHost::setTitle(const char *)
{
}
void DockerHost::update()
{
if(m_window)
m_window->update();
}
void DockerHost::render(void *payload)
{
if(m_window)
m_window->render(payload);
}
float DockerHost::scaleFactor() const
{
return m_window ? m_window->scaleFactor() : 1.f;
}
void DockerHost::onChanged()
{
const bool isActive { m_docker->isActive() };
if(isActive ^ !!m_window) {
if(isActive) {
activate();
m_window->show();
}
else {
m_viewport->PlatformHandle = nullptr;
m_window.reset();
}
}
ImGuiViewportP *viewport { static_cast<ImGuiViewportP *>(m_viewport) };
if(ImGuiWindow *userWindow { viewport->Window }) {
userWindow->Pos = viewport->Pos = viewport->LastPlatformPos = getPosition();
userWindow->Size = userWindow->SizeFull = viewport->LastRendererSize =
viewport->Size = viewport->LastPlatformSize = getSize();
}
if(m_window) {
const int dockIndex { DockIsChildOfDock(m_window->nativeHandle(), nullptr) };
if(static_cast<ReaDockID>(dockIndex) != m_docker->id())
m_docker->moveTo(m_ctx->dockers().findById(dockIndex));
m_window->onChanged();
}
}
void DockerHost::setImePosition(const ImVec2 pos)
{
if(m_window)
m_window->setImePosition(pos);
}