| 1 |
// Copyright (C) 2022 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include "qrhiimgui.h" |
| 5 |
#include <QtCore/qfile.h> |
| 6 |
#include <QtGui/qguiapplication.h> |
| 7 |
#include <QtGui/qevent.h> |
| 8 |
#include <QtGui/qclipboard.h> |
| 9 |
#include <QtGui/qimage.h> |
| 10 |
|
| 11 |
#include "imgui.h" |
| 12 |
|
| 13 |
// the imgui default |
| 14 |
static_assert(sizeof(ImDrawVert) == 20); |
| 15 |
// switched to uint in imconfig.h to avoid trouble with 4 byte offset alignment reqs |
| 16 |
static_assert(sizeof(ImDrawIdx) == 4); |
| 17 |
|
| 18 |
QT_BEGIN_NAMESPACE |
| 19 |
|
| 20 |
static QShader getShader(const QString &name) |
| 21 |
{ |
| 22 |
QFile f(name); |
| 23 |
if (f.open(QIODevice::ReadOnly)) |
| 24 |
return QShader::fromSerialized(f.readAll()); |
| 25 |
|
| 26 |
return QShader(); |
| 27 |
} |
| 28 |
|
| 29 |
QRhiImguiRenderer::~QRhiImguiRenderer() |
| 30 |
{ |
| 31 |
releaseResources(); |
| 32 |
} |
| 33 |
|
| 34 |
void QRhiImguiRenderer::releaseResources() |
| 35 |
{ |
| 36 |
for (Texture &t : m_textures) { |
| 37 |
if (t.ownTex) |
| 38 |
delete t.tex; |
| 39 |
delete t.srb; |
| 40 |
} |
| 41 |
m_textures.clear(); |
| 42 |
|
| 43 |
m_vbuf.reset(); |
| 44 |
m_ibuf.reset(); |
| 45 |
m_ubuf.reset(); |
| 46 |
m_ps.reset(); |
| 47 |
m_linearSampler.reset(); |
| 48 |
m_nearestSampler.reset(); |
| 49 |
|
| 50 |
m_rhi = nullptr; |
| 51 |
} |
| 52 |
|
| 53 |
void QRhiImguiRenderer::prepare(QRhi *rhi, |
| 54 |
QRhiRenderTarget *rt, |
| 55 |
QRhiCommandBuffer *cb, |
| 56 |
const QMatrix4x4 &mvp, |
| 57 |
float opacity, |
| 58 |
float hdrWhiteLevelMultiplierOrZeroForSDRsRGB) |
| 59 |
{ |
| 60 |
if (!m_rhi) { |
| 61 |
m_rhi = rhi; |
| 62 |
} else if (m_rhi != rhi) { |
| 63 |
releaseResources(); |
| 64 |
m_rhi = rhi; |
| 65 |
} |
| 66 |
|
| 67 |
if (!m_rhi || f.draw.isEmpty()) |
| 68 |
return; |
| 69 |
|
| 70 |
m_rt = rt; |
| 71 |
m_cb = cb; |
| 72 |
|
| 73 |
if (!m_vbuf) { |
| 74 |
m_vbuf.reset(m_rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::VertexBuffer, f.totalVbufSize)); |
| 75 |
m_vbuf->setName(QByteArrayLiteral("imgui vertex buffer")); |
| 76 |
if (!m_vbuf->create()) |
| 77 |
return; |
| 78 |
} else { |
| 79 |
if (f.totalVbufSize > m_vbuf->size()) { |
| 80 |
m_vbuf->setSize(f.totalVbufSize); |
| 81 |
if (!m_vbuf->create()) |
| 82 |
return; |
| 83 |
} |
| 84 |
} |
| 85 |
if (!m_ibuf) { |
| 86 |
m_ibuf.reset(m_rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::IndexBuffer, f.totalIbufSize)); |
| 87 |
m_ibuf->setName(QByteArrayLiteral("imgui index buffer")); |
| 88 |
if (!m_ibuf->create()) |
| 89 |
return; |
| 90 |
} else { |
| 91 |
if (f.totalIbufSize > m_ibuf->size()) { |
| 92 |
m_ibuf->setSize(f.totalIbufSize); |
| 93 |
if (!m_ibuf->create()) |
| 94 |
return; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if (!m_ubuf) { |
| 99 |
m_ubuf.reset(m_rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, 64 + 4 + 4)); |
| 100 |
m_ubuf->setName(QByteArrayLiteral("imgui uniform buffer")); |
| 101 |
if (!m_ubuf->create()) |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
if (!m_linearSampler) { |
| 106 |
m_linearSampler.reset(m_rhi->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None, |
| 107 |
QRhiSampler::Repeat, QRhiSampler::Repeat)); |
| 108 |
m_linearSampler->setName(QByteArrayLiteral("imgui linear sampler")); |
| 109 |
if (!m_linearSampler->create()) |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
if (!m_nearestSampler) { |
| 114 |
m_nearestSampler.reset(m_rhi->newSampler(QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None, |
| 115 |
QRhiSampler::Repeat, QRhiSampler::Repeat)); |
| 116 |
m_nearestSampler->setName(QByteArrayLiteral("imgui nearest sampler")); |
| 117 |
if (!m_nearestSampler->create()) |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
if (m_textures.isEmpty()) { |
| 122 |
Texture fontTex; |
| 123 |
fontTex.image = sf.fontTextureData; |
| 124 |
m_textures.insert(nullptr, fontTex); |
| 125 |
sf.reset(); |
| 126 |
} else if (sf.isValid()) { |
| 127 |
Texture fontTex; |
| 128 |
fontTex.image = sf.fontTextureData; |
| 129 |
Texture &fontTexEntry(m_textures[nullptr]); |
| 130 |
delete fontTexEntry.tex; |
| 131 |
delete fontTexEntry.srb; |
| 132 |
fontTexEntry = fontTex; |
| 133 |
sf.reset(); |
| 134 |
} |
| 135 |
|
| 136 |
QVarLengthArray<void *, 8> texturesNeedUpdate; |
| 137 |
for (auto it = m_textures.begin(), end = m_textures.end(); it != end; ++it) { |
| 138 |
Texture &t(*it); |
| 139 |
if (!t.tex) { |
| 140 |
t.tex = m_rhi->newTexture(QRhiTexture::RGBA8, t.image.size()); |
| 141 |
t.tex->setName(QByteArrayLiteral("imgui texture ") + QByteArray::number(qintptr(it.key()))); |
| 142 |
if (!t.tex->create()) |
| 143 |
return; |
| 144 |
texturesNeedUpdate.append(it.key()); |
| 145 |
} |
| 146 |
if (!t.srb) { |
| 147 |
QRhiSampler *sampler = t.filter == QRhiSampler::Nearest ? m_nearestSampler.get() : m_linearSampler.get(); |
| 148 |
t.srb = m_rhi->newShaderResourceBindings(); |
| 149 |
t.srb->setBindings({ |
| 150 |
QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, m_ubuf.get()), |
| 151 |
QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, t.tex, sampler) |
| 152 |
}); |
| 153 |
if (!t.srb->create()) |
| 154 |
return; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
// If layer.enabled is toggled on the item or an ancestor, the render |
| 159 |
// target is then suddenly different and may not be compatible. |
| 160 |
if (m_ps && m_rt->renderPassDescriptor()->serializedFormat() != m_renderPassFormat) |
| 161 |
m_ps.reset(); |
| 162 |
|
| 163 |
if (m_ps && m_rt->sampleCount() != m_ps->sampleCount()) |
| 164 |
m_ps.reset(); |
| 165 |
|
| 166 |
if (!m_ps) { |
| 167 |
QShader vs = getShader(QLatin1String(":/imgui.vert.qsb")); |
| 168 |
QShader fs = getShader(QLatin1String(":/imgui.frag.qsb")); |
| 169 |
if (!vs.isValid() || !fs.isValid()) { |
| 170 |
qWarning("Failed to load imgui shaders"); |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
m_ps.reset(m_rhi->newGraphicsPipeline()); |
| 175 |
QRhiGraphicsPipeline::TargetBlend blend; |
| 176 |
blend.enable = true; |
| 177 |
// Premultiplied alpha (matches imgui.frag). Would not be needed if we |
| 178 |
// only cared about outputting to the window (the common case), but |
| 179 |
// once going through a texture (Item layer, ShaderEffect) which is |
| 180 |
// then sampled by Quick, the result wouldn't be correct otherwise. |
| 181 |
blend.srcColor = QRhiGraphicsPipeline::One; |
| 182 |
blend.dstColor = QRhiGraphicsPipeline::OneMinusSrcAlpha; |
| 183 |
blend.srcAlpha = QRhiGraphicsPipeline::One; |
| 184 |
blend.dstAlpha = QRhiGraphicsPipeline::OneMinusSrcAlpha; |
| 185 |
m_ps->setTargetBlends({ blend }); |
| 186 |
m_ps->setCullMode(QRhiGraphicsPipeline::None); |
| 187 |
m_ps->setDepthTest(true); |
| 188 |
m_ps->setDepthOp(QRhiGraphicsPipeline::LessOrEqual); |
| 189 |
m_ps->setDepthWrite(false); |
| 190 |
m_ps->setFlags(QRhiGraphicsPipeline::UsesScissor | QRhiGraphicsPipeline::UsesShadingRate); |
| 191 |
|
| 192 |
m_ps->setShaderStages({ |
| 193 |
{ QRhiShaderStage::Vertex, vs }, |
| 194 |
{ QRhiShaderStage::Fragment, fs } |
| 195 |
}); |
| 196 |
|
| 197 |
QRhiVertexInputLayout inputLayout; |
| 198 |
inputLayout.setBindings({ |
| 199 |
{ 4 * sizeof(float) + sizeof(quint32) } |
| 200 |
}); |
| 201 |
inputLayout.setAttributes({ |
| 202 |
{ 0, 0, QRhiVertexInputAttribute::Float2, 0 }, |
| 203 |
{ 0, 1, QRhiVertexInputAttribute::Float2, 2 * sizeof(float) }, |
| 204 |
{ 0, 2, QRhiVertexInputAttribute::UNormByte4, 4 * sizeof(float) } |
| 205 |
}); |
| 206 |
m_ps->setVertexInputLayout(inputLayout); |
| 207 |
m_ps->setSampleCount(rt->sampleCount()); |
| 208 |
m_ps->setShaderResourceBindings(m_textures[0].srb); |
| 209 |
m_ps->setRenderPassDescriptor(m_rt->renderPassDescriptor()); |
| 210 |
m_renderPassFormat = m_rt->renderPassDescriptor()->serializedFormat(); |
| 211 |
|
| 212 |
if (!m_ps->create()) |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
QRhiResourceUpdateBatch *u = m_rhi->nextResourceUpdateBatch(); |
| 217 |
|
| 218 |
for (const CmdListBuffer &b : f.vbuf) |
| 219 |
u->updateDynamicBuffer(m_vbuf.get(), b.offset, b.data.size(), b.data.constData()); |
| 220 |
|
| 221 |
for (const CmdListBuffer &b : f.ibuf) |
| 222 |
u->updateDynamicBuffer(m_ibuf.get(), b.offset, b.data.size(), b.data.constData()); |
| 223 |
|
| 224 |
u->updateDynamicBuffer(m_ubuf.get(), 0, 64, mvp.constData()); |
| 225 |
u->updateDynamicBuffer(m_ubuf.get(), 64, 4, &opacity); |
| 226 |
u->updateDynamicBuffer(m_ubuf.get(), 68, 4, &hdrWhiteLevelMultiplierOrZeroForSDRsRGB); |
| 227 |
|
| 228 |
for (int i = 0; i < texturesNeedUpdate.count(); ++i) { |
| 229 |
Texture &t(m_textures[texturesNeedUpdate[i]]); |
| 230 |
if (!t.image.isNull()) { |
| 231 |
u->uploadTexture(t.tex, t.image); |
| 232 |
t.image = QImage(); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
m_cb->resourceUpdate(u); |
| 237 |
} |
| 238 |
|
| 239 |
void QRhiImguiRenderer::render() |
| 240 |
{ |
| 241 |
if (!m_rhi || f.draw.isEmpty() || !m_ps) |
| 242 |
return; |
| 243 |
|
| 244 |
m_cb->setGraphicsPipeline(m_ps.get()); |
| 245 |
|
| 246 |
const QSize viewportSize = m_rt->pixelSize(); |
| 247 |
bool needsViewport = true; |
| 248 |
|
| 249 |
for (const DrawCmd &c : f.draw) { |
| 250 |
QRhiCommandBuffer::VertexInput vbufBinding(m_vbuf.get(), f.vbuf[c.cmdListBufferIdx].offset); |
| 251 |
if (needsViewport) { |
| 252 |
needsViewport = false; |
| 253 |
m_cb->setViewport({ 0, 0, float(viewportSize.width()), float(viewportSize.height()) }); |
| 254 |
} |
| 255 |
const float sx1 = c.clipRect.x() + c.itemPixelOffset.x(); |
| 256 |
const float sy1 = c.clipRect.y() + c.itemPixelOffset.y(); |
| 257 |
const float sx2 = c.clipRect.z() + c.itemPixelOffset.x(); |
| 258 |
const float sy2 = c.clipRect.w() + c.itemPixelOffset.y(); |
| 259 |
QPoint scissorPos = QPointF(sx1, viewportSize.height() - sy2).toPoint(); |
| 260 |
QSize scissorSize = QSizeF(sx2 - sx1, sy2 - sy1).toSize(); |
| 261 |
scissorPos.setX(qMax(0, scissorPos.x())); |
| 262 |
scissorPos.setY(qMax(0, scissorPos.y())); |
| 263 |
scissorSize.setWidth(qMin(viewportSize.width(), scissorSize.width())); |
| 264 |
scissorSize.setHeight(qMin(viewportSize.height(), scissorSize.height())); |
| 265 |
m_cb->setScissor({ scissorPos.x(), scissorPos.y(), scissorSize.width(), scissorSize.height() }); |
| 266 |
m_cb->setShaderResources(m_textures[c.textureId].srb); |
| 267 |
m_cb->setVertexInput(0, 1, &vbufBinding, m_ibuf.get(), c.indexOffset, QRhiCommandBuffer::IndexUInt32); |
| 268 |
m_cb->drawIndexed(c.elemCount); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
void QRhiImguiRenderer::registerCustomTexture(void *id, |
| 273 |
QRhiTexture *texture, |
| 274 |
QRhiSampler::Filter filter, |
| 275 |
CustomTextureOwnership ownership) |
| 276 |
{ |
| 277 |
Q_ASSERT(id); |
| 278 |
auto it = m_textures.constFind(id); |
| 279 |
if (it != m_textures.cend()) { |
| 280 |
if (it->ownTex) |
| 281 |
delete it->tex; |
| 282 |
delete it->srb; |
| 283 |
} |
| 284 |
Texture t; |
| 285 |
t.tex = texture; |
| 286 |
t.filter = filter; |
| 287 |
t.ownTex = ownership == TakeCustomTextureOwnership; |
| 288 |
m_textures[id] = t; |
| 289 |
} |
| 290 |
|
| 291 |
static const char *getClipboardText(void *) |
| 292 |
{ |
| 293 |
static QByteArray contents; |
| 294 |
contents = QGuiApplication::clipboard()->text().toUtf8(); |
| 295 |
return contents.constData(); |
| 296 |
} |
| 297 |
|
| 298 |
static void setClipboardText(void *, const char *text) |
| 299 |
{ |
| 300 |
QGuiApplication::clipboard()->setText(QString::fromUtf8(text)); |
| 301 |
} |
| 302 |
|
| 303 |
QRhiImgui::QRhiImgui() |
| 304 |
{ |
| 305 |
context = ImGui::CreateContext(); |
| 306 |
ImGui::SetCurrentContext(static_cast<ImGuiContext *>(context)); |
| 307 |
rebuildFontAtlas(); |
| 308 |
ImGuiIO &io(ImGui::GetIO()); |
| 309 |
io.GetClipboardTextFn = getClipboardText; |
| 310 |
io.SetClipboardTextFn = setClipboardText; |
| 311 |
} |
| 312 |
|
| 313 |
QRhiImgui::~QRhiImgui() |
| 314 |
{ |
| 315 |
ImGui::DestroyContext(static_cast<ImGuiContext *>(context)); |
| 316 |
} |
| 317 |
|
| 318 |
void QRhiImgui::rebuildFontAtlas() |
| 319 |
{ |
| 320 |
ImGui::SetCurrentContext(static_cast<ImGuiContext *>(context)); |
| 321 |
ImGuiIO &io(ImGui::GetIO()); |
| 322 |
unsigned char *pixels; |
| 323 |
int w, h; |
| 324 |
io.Fonts->GetTexDataAsRGBA32(&pixels, &w, &h); |
| 325 |
const QImage wrapperImg(const_cast<const uchar *>(pixels), w, h, QImage::Format_RGBA8888); |
| 326 |
sf.fontTextureData = wrapperImg.copy(); |
| 327 |
io.Fonts->SetTexID(nullptr); |
| 328 |
} |
| 329 |
|
| 330 |
void QRhiImgui::rebuildFontAtlasWithFont(const QString &filename) |
| 331 |
{ |
| 332 |
QFile f(filename); |
| 333 |
if (!f.open(QIODevice::ReadOnly)) { |
| 334 |
qWarning("Failed to open %s", qPrintable(filename)); |
| 335 |
return; |
| 336 |
} |
| 337 |
QByteArray font = f.readAll(); |
| 338 |
ImGui::SetCurrentContext(static_cast<ImGuiContext *>(context)); |
| 339 |
ImFontConfig fontCfg; |
| 340 |
fontCfg.FontDataOwnedByAtlas = false; |
| 341 |
ImGui::GetIO().Fonts->Clear(); |
| 342 |
ImGui::GetIO().Fonts->AddFontFromMemoryTTF(font.data(), font.size(), 20.0f, &fontCfg); |
| 343 |
rebuildFontAtlas(); |
| 344 |
} |
| 345 |
|
| 346 |
void QRhiImgui::nextFrame(const QSizeF &logicalOutputSize, float dpr, const QPointF &logicalOffset, FrameFunc frameFunc) |
| 347 |
{ |
| 348 |
ImGui::SetCurrentContext(static_cast<ImGuiContext *>(context)); |
| 349 |
ImGuiIO &io(ImGui::GetIO()); |
| 350 |
|
| 351 |
const QPointF itemPixelOffset = logicalOffset * dpr; |
| 352 |
f.outputPixelSize = (logicalOutputSize * dpr).toSize(); |
| 353 |
io.DisplaySize.x = logicalOutputSize.width(); |
| 354 |
io.DisplaySize.y = logicalOutputSize.height(); |
| 355 |
io.DisplayFramebufferScale = ImVec2(dpr, dpr); |
| 356 |
|
| 357 |
ImGui::NewFrame(); |
| 358 |
if (frameFunc) |
| 359 |
frameFunc(); |
| 360 |
ImGui::Render(); |
| 361 |
|
| 362 |
ImDrawData *draw = ImGui::GetDrawData(); |
| 363 |
draw->ScaleClipRects(ImVec2(dpr, dpr)); |
| 364 |
|
| 365 |
f.vbuf.resize(draw->CmdListsCount); |
| 366 |
f.ibuf.resize(draw->CmdListsCount); |
| 367 |
f.totalVbufSize = 0; |
| 368 |
f.totalIbufSize = 0; |
| 369 |
for (int n = 0; n < draw->CmdListsCount; ++n) { |
| 370 |
const ImDrawList *cmdList = draw->CmdLists[n]; |
| 371 |
const int vbufSize = cmdList->VtxBuffer.Size * sizeof(ImDrawVert); |
| 372 |
f.vbuf[n].offset = f.totalVbufSize; |
| 373 |
f.totalVbufSize += vbufSize; |
| 374 |
const int ibufSize = cmdList->IdxBuffer.Size * sizeof(ImDrawIdx); |
| 375 |
f.ibuf[n].offset = f.totalIbufSize; |
| 376 |
f.totalIbufSize += ibufSize; |
| 377 |
} |
| 378 |
f.draw.clear(); |
| 379 |
for (int n = 0; n < draw->CmdListsCount; ++n) { |
| 380 |
const ImDrawList *cmdList = draw->CmdLists[n]; |
| 381 |
f.vbuf[n].data = QByteArray(reinterpret_cast<const char *>(cmdList->VtxBuffer.Data), |
| 382 |
cmdList->VtxBuffer.Size * sizeof(ImDrawVert)); |
| 383 |
f.ibuf[n].data = QByteArray(reinterpret_cast<const char *>(cmdList->IdxBuffer.Data), |
| 384 |
cmdList->IdxBuffer.Size * sizeof(ImDrawIdx)); |
| 385 |
const ImDrawIdx *indexBufOffset = nullptr; |
| 386 |
for (int i = 0; i < cmdList->CmdBuffer.Size; ++i) { |
| 387 |
const ImDrawCmd *cmd = &cmdList->CmdBuffer[i]; |
| 388 |
const quint32 indexOffset = f.ibuf[n].offset + quintptr(indexBufOffset); |
| 389 |
if (!cmd->UserCallback) { |
| 390 |
QRhiImguiRenderer::DrawCmd dc; |
| 391 |
dc.cmdListBufferIdx = n; |
| 392 |
dc.textureId = cmd->TextureId; |
| 393 |
dc.indexOffset = indexOffset; |
| 394 |
dc.elemCount = cmd->ElemCount; |
| 395 |
dc.itemPixelOffset = itemPixelOffset; |
| 396 |
dc.clipRect = QVector4D(cmd->ClipRect.x, cmd->ClipRect.y, cmd->ClipRect.z, cmd->ClipRect.w); |
| 397 |
f.draw.append(dc); |
| 398 |
} else { |
| 399 |
cmd->UserCallback(cmdList, cmd); |
| 400 |
} |
| 401 |
indexBufOffset += cmd->ElemCount; |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
void QRhiImgui::syncRenderer(QRhiImguiRenderer *renderer) |
| 407 |
{ |
| 408 |
if (sf.isValid()) { |
| 409 |
renderer->sf = sf; |
| 410 |
sf.reset(); |
| 411 |
} |
| 412 |
renderer->f = std::move(f); |
| 413 |
} |
| 414 |
|
| 415 |
static void updateKeyboardModifiers(Qt::KeyboardModifiers modifiers) |
| 416 |
{ |
| 417 |
ImGuiIO &io(ImGui::GetIO()); |
| 418 |
io.AddKeyEvent(ImGuiKey_ModCtrl, modifiers.testFlag(Qt::ControlModifier)); |
| 419 |
io.AddKeyEvent(ImGuiKey_ModShift, modifiers.testFlag(Qt::ShiftModifier)); |
| 420 |
io.AddKeyEvent(ImGuiKey_ModAlt, modifiers.testFlag(Qt::AltModifier)); |
| 421 |
io.AddKeyEvent(ImGuiKey_ModSuper, modifiers.testFlag(Qt::MetaModifier)); |
| 422 |
} |
| 423 |
|
| 424 |
static ImGuiKey mapKey(int k) |
| 425 |
{ |
| 426 |
switch (k) { |
| 427 |
case Qt::Key_Space: |
| 428 |
return ImGuiKey_Space; |
| 429 |
case Qt::Key_Apostrophe: |
| 430 |
return ImGuiKey_Apostrophe; |
| 431 |
case Qt::Key_Comma: |
| 432 |
return ImGuiKey_Comma; |
| 433 |
case Qt::Key_Minus: |
| 434 |
return ImGuiKey_Minus; |
| 435 |
case Qt::Key_Period: |
| 436 |
return ImGuiKey_Period; |
| 437 |
case Qt::Key_Slash: |
| 438 |
return ImGuiKey_Slash; |
| 439 |
case Qt::Key_0: |
| 440 |
return ImGuiKey_0; |
| 441 |
case Qt::Key_1: |
| 442 |
return ImGuiKey_1; |
| 443 |
case Qt::Key_2: |
| 444 |
return ImGuiKey_2; |
| 445 |
case Qt::Key_3: |
| 446 |
return ImGuiKey_3; |
| 447 |
case Qt::Key_4: |
| 448 |
return ImGuiKey_4; |
| 449 |
case Qt::Key_5: |
| 450 |
return ImGuiKey_5; |
| 451 |
case Qt::Key_6: |
| 452 |
return ImGuiKey_6; |
| 453 |
case Qt::Key_7: |
| 454 |
return ImGuiKey_8; |
| 455 |
case Qt::Key_8: |
| 456 |
return ImGuiKey_8; |
| 457 |
case Qt::Key_9: |
| 458 |
return ImGuiKey_9; |
| 459 |
case Qt::Key_Semicolon: |
| 460 |
return ImGuiKey_Semicolon; |
| 461 |
case Qt::Key_Equal: |
| 462 |
return ImGuiKey_Equal; |
| 463 |
case Qt::Key_A: |
| 464 |
return ImGuiKey_A; |
| 465 |
case Qt::Key_B: |
| 466 |
return ImGuiKey_B; |
| 467 |
case Qt::Key_C: |
| 468 |
return ImGuiKey_C; |
| 469 |
case Qt::Key_D: |
| 470 |
return ImGuiKey_D; |
| 471 |
case Qt::Key_E: |
| 472 |
return ImGuiKey_E; |
| 473 |
case Qt::Key_F: |
| 474 |
return ImGuiKey_F; |
| 475 |
case Qt::Key_G: |
| 476 |
return ImGuiKey_G; |
| 477 |
case Qt::Key_H: |
| 478 |
return ImGuiKey_H; |
| 479 |
case Qt::Key_I: |
| 480 |
return ImGuiKey_I; |
| 481 |
case Qt::Key_J: |
| 482 |
return ImGuiKey_J; |
| 483 |
case Qt::Key_K: |
| 484 |
return ImGuiKey_K; |
| 485 |
case Qt::Key_L: |
| 486 |
return ImGuiKey_L; |
| 487 |
case Qt::Key_M: |
| 488 |
return ImGuiKey_M; |
| 489 |
case Qt::Key_N: |
| 490 |
return ImGuiKey_N; |
| 491 |
case Qt::Key_O: |
| 492 |
return ImGuiKey_O; |
| 493 |
case Qt::Key_P: |
| 494 |
return ImGuiKey_P; |
| 495 |
case Qt::Key_Q: |
| 496 |
return ImGuiKey_Q; |
| 497 |
case Qt::Key_R: |
| 498 |
return ImGuiKey_R; |
| 499 |
case Qt::Key_S: |
| 500 |
return ImGuiKey_S; |
| 501 |
case Qt::Key_T: |
| 502 |
return ImGuiKey_T; |
| 503 |
case Qt::Key_U: |
| 504 |
return ImGuiKey_U; |
| 505 |
case Qt::Key_V: |
| 506 |
return ImGuiKey_V; |
| 507 |
case Qt::Key_W: |
| 508 |
return ImGuiKey_W; |
| 509 |
case Qt::Key_X: |
| 510 |
return ImGuiKey_X; |
| 511 |
case Qt::Key_Y: |
| 512 |
return ImGuiKey_Y; |
| 513 |
case Qt::Key_Z: |
| 514 |
return ImGuiKey_Z; |
| 515 |
case Qt::Key_BracketLeft: |
| 516 |
return ImGuiKey_LeftBracket; |
| 517 |
case Qt::Key_Backslash: |
| 518 |
return ImGuiKey_Backslash; |
| 519 |
case Qt::Key_BracketRight: |
| 520 |
return ImGuiKey_RightBracket; |
| 521 |
case Qt::Key_QuoteLeft: |
| 522 |
return ImGuiKey_GraveAccent; |
| 523 |
case Qt::Key_Escape: |
| 524 |
return ImGuiKey_Escape; |
| 525 |
case Qt::Key_Tab: |
| 526 |
return ImGuiKey_Tab; |
| 527 |
case Qt::Key_Backspace: |
| 528 |
return ImGuiKey_Backspace; |
| 529 |
case Qt::Key_Return: |
| 530 |
case Qt::Key_Enter: |
| 531 |
return ImGuiKey_Enter; |
| 532 |
case Qt::Key_Insert: |
| 533 |
return ImGuiKey_Insert; |
| 534 |
case Qt::Key_Delete: |
| 535 |
return ImGuiKey_Delete; |
| 536 |
case Qt::Key_Pause: |
| 537 |
return ImGuiKey_Pause; |
| 538 |
case Qt::Key_Print: |
| 539 |
return ImGuiKey_PrintScreen; |
| 540 |
case Qt::Key_Home: |
| 541 |
return ImGuiKey_Home; |
| 542 |
case Qt::Key_End: |
| 543 |
return ImGuiKey_End; |
| 544 |
case Qt::Key_Left: |
| 545 |
return ImGuiKey_LeftArrow; |
| 546 |
case Qt::Key_Up: |
| 547 |
return ImGuiKey_UpArrow; |
| 548 |
case Qt::Key_Right: |
| 549 |
return ImGuiKey_RightArrow; |
| 550 |
case Qt::Key_Down: |
| 551 |
return ImGuiKey_DownArrow; |
| 552 |
case Qt::Key_PageUp: |
| 553 |
return ImGuiKey_PageUp; |
| 554 |
case Qt::Key_PageDown: |
| 555 |
return ImGuiKey_PageDown; |
| 556 |
case Qt::Key_Shift: |
| 557 |
return ImGuiKey_LeftShift; |
| 558 |
case Qt::Key_Control: |
| 559 |
return ImGuiKey_LeftCtrl; |
| 560 |
case Qt::Key_Meta: |
| 561 |
return ImGuiKey_LeftSuper; |
| 562 |
case Qt::Key_Alt: |
| 563 |
return ImGuiKey_LeftAlt; |
| 564 |
case Qt::Key_CapsLock: |
| 565 |
return ImGuiKey_CapsLock; |
| 566 |
case Qt::Key_NumLock: |
| 567 |
return ImGuiKey_NumLock; |
| 568 |
case Qt::Key_ScrollLock: |
| 569 |
return ImGuiKey_ScrollLock; |
| 570 |
case Qt::Key_F1: |
| 571 |
return ImGuiKey_F1; |
| 572 |
case Qt::Key_F2: |
| 573 |
return ImGuiKey_F2; |
| 574 |
case Qt::Key_F3: |
| 575 |
return ImGuiKey_F3; |
| 576 |
case Qt::Key_F4: |
| 577 |
return ImGuiKey_F4; |
| 578 |
case Qt::Key_F5: |
| 579 |
return ImGuiKey_F5; |
| 580 |
case Qt::Key_F6: |
| 581 |
return ImGuiKey_F6; |
| 582 |
case Qt::Key_F7: |
| 583 |
return ImGuiKey_F7; |
| 584 |
case Qt::Key_F8: |
| 585 |
return ImGuiKey_F8; |
| 586 |
case Qt::Key_F9: |
| 587 |
return ImGuiKey_F9; |
| 588 |
case Qt::Key_F10: |
| 589 |
return ImGuiKey_F10; |
| 590 |
case Qt::Key_F11: |
| 591 |
return ImGuiKey_F11; |
| 592 |
case Qt::Key_F12: |
| 593 |
return ImGuiKey_F12; |
| 594 |
default: |
| 595 |
break; |
| 596 |
} |
| 597 |
return ImGuiKey_None; |
| 598 |
} |
| 599 |
|
| 600 |
bool QRhiImgui::processEvent(QEvent *event) |
| 601 |
{ |
| 602 |
ImGui::SetCurrentContext(static_cast<ImGuiContext *>(context)); |
| 603 |
ImGuiIO &io(ImGui::GetIO()); |
| 604 |
|
| 605 |
switch (event->type()) { |
| 606 |
case QEvent::MouseButtonPress: |
| 607 |
{ |
| 608 |
QMouseEvent *me = static_cast<QMouseEvent *>(event); |
| 609 |
updateKeyboardModifiers(me->modifiers()); |
| 610 |
Qt::MouseButtons buttons = me->buttons(); |
| 611 |
if (buttons.testFlag(Qt::LeftButton) && !pressedMouseButtons.testFlag(Qt::LeftButton)) |
| 612 |
io.AddMouseButtonEvent(0, true); |
| 613 |
if (buttons.testFlag(Qt::RightButton) && !pressedMouseButtons.testFlag(Qt::RightButton)) |
| 614 |
io.AddMouseButtonEvent(1, true); |
| 615 |
if (buttons.testFlag(Qt::MiddleButton) && !pressedMouseButtons.testFlag(Qt::MiddleButton)) |
| 616 |
io.AddMouseButtonEvent(2, true); |
| 617 |
pressedMouseButtons = buttons; |
| 618 |
} |
| 619 |
return true; |
| 620 |
|
| 621 |
case QEvent::MouseButtonRelease: |
| 622 |
{ |
| 623 |
QMouseEvent *me = static_cast<QMouseEvent *>(event); |
| 624 |
Qt::MouseButtons buttons = me->buttons(); |
| 625 |
if (!buttons.testFlag(Qt::LeftButton) && pressedMouseButtons.testFlag(Qt::LeftButton)) |
| 626 |
io.AddMouseButtonEvent(0, false); |
| 627 |
if (!buttons.testFlag(Qt::RightButton) && pressedMouseButtons.testFlag(Qt::RightButton)) |
| 628 |
io.AddMouseButtonEvent(1, false); |
| 629 |
if (!buttons.testFlag(Qt::MiddleButton) && pressedMouseButtons.testFlag(Qt::MiddleButton)) |
| 630 |
io.AddMouseButtonEvent(2, false); |
| 631 |
pressedMouseButtons = buttons; |
| 632 |
} |
| 633 |
return true; |
| 634 |
|
| 635 |
case QEvent::MouseMove: |
| 636 |
{ |
| 637 |
QMouseEvent *me = static_cast<QMouseEvent *>(event); |
| 638 |
const QPointF pos = me->position(); |
| 639 |
io.AddMousePosEvent(pos.x(), pos.y()); |
| 640 |
} |
| 641 |
return true; |
| 642 |
|
| 643 |
case QEvent::Wheel: |
| 644 |
{ |
| 645 |
QWheelEvent *we = static_cast<QWheelEvent *>(event); |
| 646 |
QPointF wheel(we->angleDelta().x() / 120.0f, we->angleDelta().y() / 120.0f); |
| 647 |
io.AddMouseWheelEvent(wheel.x(), wheel.y()); |
| 648 |
} |
| 649 |
return true; |
| 650 |
|
| 651 |
case QEvent::KeyPress: |
| 652 |
case QEvent::KeyRelease: |
| 653 |
{ |
| 654 |
QKeyEvent *ke = static_cast<QKeyEvent *>(event); |
| 655 |
const bool down = event->type() == QEvent::KeyPress; |
| 656 |
updateKeyboardModifiers(ke->modifiers()); |
| 657 |
io.AddKeyEvent(mapKey(ke->key()), down); |
| 658 |
if (down && !ke->text().isEmpty()) { |
| 659 |
const QByteArray text = ke->text().toUtf8(); |
| 660 |
io.AddInputCharactersUTF8(text.constData()); |
| 661 |
} |
| 662 |
} |
| 663 |
return true; |
| 664 |
|
| 665 |
default: |
| 666 |
break; |
| 667 |
} |
| 668 |
|
| 669 |
return false; |
| 670 |
} |
| 671 |
|
| 672 |
QT_END_NAMESPACE |