Browse Source

TextEditor: Pass-through scroll and click events to Rack as needed

Signed-off-by: falkTX <falktx@falktx.com>
tags/22.02
falkTX 3 years ago
parent
commit
1a4534bf22
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
5 changed files with 95 additions and 65 deletions
  1. +21
    -0
      plugins/Cardinal/src/ImGuiTextEditor.cpp
  2. +2
    -0
      plugins/Cardinal/src/ImGuiTextEditor.hpp
  3. +59
    -54
      plugins/Cardinal/src/ImGuiWidget.cpp
  4. +11
    -10
      plugins/Cardinal/src/ImGuiWidget.hpp
  5. +2
    -1
      plugins/Cardinal/src/TextEditor.cpp

+ 21
- 0
plugins/Cardinal/src/ImGuiTextEditor.cpp View File

@@ -158,6 +158,27 @@ void ImGuiTextEditor::drawImGui()
ImGui::End(); ImGui::End();
} }


void ImGuiTextEditor::onButton(const ButtonEvent& e)
{
setAsCurrentContext();

// if mouse press is over the top status bar, do nothing so editor can be moved in the Rack
if (e.action == GLFW_PRESS && e.pos.y < 25)
return;

ImGuiWidget::onButton(e);
}

void ImGuiTextEditor::onHoverScroll(const HoverScrollEvent& e)
{
// use Rack view scrolling if there is no scrollbar
if (pData->editor.GetTotalLines() < 27)
return;

// if there is a scrollbar, handle the event
ImGuiWidget::onHoverScroll(e);
}

/* /*
void ImGuiTextEditor::onSelectKey(const SelectKeyEvent& e) void ImGuiTextEditor::onSelectKey(const SelectKeyEvent& e)
{ {


+ 2
- 0
plugins/Cardinal/src/ImGuiTextEditor.hpp View File

@@ -59,6 +59,8 @@ struct ImGuiTextEditor : ImGuiWidget
protected: protected:
/** @internal */ /** @internal */
void drawImGui() override; void drawImGui() override;
void onButton(const ButtonEvent& e) override;
void onHoverScroll(const HoverScrollEvent& e) override;
/* /*
void onSelectKey(const SelectKeyEvent& e) override; void onSelectKey(const SelectKeyEvent& e) override;
*/ */


+ 59
- 54
plugins/Cardinal/src/ImGuiWidget.cpp View File

@@ -165,62 +165,9 @@ void ImGuiWidget::onContextDestroy(const ContextDestroyEvent& e)
OpenGlWidget::onContextDestroy(e); OpenGlWidget::onContextDestroy(e);
} }


void ImGuiWidget::drawFramebuffer()
void ImGuiWidget::setAsCurrentContext()
{ {
ImGui::SetCurrentContext(imData->context); ImGui::SetCurrentContext(imData->context);
ImGuiIO& io(ImGui::GetIO());

const math::Vec fbSize = getFramebufferSize();
const float scaleFactor = APP->window->pixelRatio;

if (d_isNotEqual(imData->scaleFactor, scaleFactor))
{
imData->scaleFactor = scaleFactor;

ImGuiStyle& style(ImGui::GetStyle());
new(&style)ImGuiStyle();
imData->resetStyle();

if (! imData->fontGenerated)
{
imData->originalScaleFactor = scaleFactor;
imData->generateFontIfNeeded();
}
else
{
io.FontGlobalScale = scaleFactor / imData->originalScaleFactor;
}
}

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, box.size.x * scaleFactor, box.size.y * scaleFactor, 0.0, -1.0, 1.0);
glViewport(0.0, 0.0, fbSize.x, fbSize.y);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

io.DisplaySize = ImVec2(box.size.x * scaleFactor, box.size.y * scaleFactor);
io.DisplayFramebufferScale = ImVec2(fbSize.x / (box.size.x * scaleFactor), fbSize.y / (box.size.y * scaleFactor));

if (!imData->created)
{
ImGui_ImplOpenGL2_Init();
imData->created = true;
}

// TODO io.DeltaTime

ImGui_ImplOpenGL2_NewFrame();
ImGui::NewFrame();

drawImGui();

ImGui::Render();

if (ImDrawData* const data = ImGui::GetDrawData())
ImGui_ImplOpenGL2_RenderDrawData(data);
} }


void ImGuiWidget::onHover(const HoverEvent& e) void ImGuiWidget::onHover(const HoverEvent& e)
@@ -358,3 +305,61 @@ void ImGuiWidget::onSelectText(const SelectTextEvent& e)
if (io.WantCaptureKeyboard) if (io.WantCaptureKeyboard)
e.consume(this); e.consume(this);
} }

void ImGuiWidget::drawFramebuffer()
{
ImGui::SetCurrentContext(imData->context);
ImGuiIO& io(ImGui::GetIO());

const math::Vec fbSize = getFramebufferSize();
const float scaleFactor = APP->window->pixelRatio;

if (d_isNotEqual(imData->scaleFactor, scaleFactor))
{
imData->scaleFactor = scaleFactor;

ImGuiStyle& style(ImGui::GetStyle());
new(&style)ImGuiStyle();
imData->resetStyle();

if (! imData->fontGenerated)
{
imData->originalScaleFactor = scaleFactor;
imData->generateFontIfNeeded();
}
else
{
io.FontGlobalScale = scaleFactor / imData->originalScaleFactor;
}
}

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, box.size.x * scaleFactor, box.size.y * scaleFactor, 0.0, -1.0, 1.0);
glViewport(0.0, 0.0, fbSize.x, fbSize.y);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

io.DisplaySize = ImVec2(box.size.x * scaleFactor, box.size.y * scaleFactor);
io.DisplayFramebufferScale = ImVec2(fbSize.x / (box.size.x * scaleFactor), fbSize.y / (box.size.y * scaleFactor));

if (!imData->created)
{
ImGui_ImplOpenGL2_Init();
imData->created = true;
}

// TODO io.DeltaTime

ImGui_ImplOpenGL2_NewFrame();
ImGui::NewFrame();

drawImGui();

ImGui::Render();

if (ImDrawData* const data = ImGui::GetDrawData())
ImGui_ImplOpenGL2_RenderDrawData(data);
}

+ 11
- 10
plugins/Cardinal/src/ImGuiWidget.hpp View File

@@ -27,21 +27,11 @@ struct ImGuiWidget : OpenGlWidget {
ImGuiWidget(); ImGuiWidget();
~ImGuiWidget() override; ~ImGuiWidget() override;


virtual void drawImGui()
{
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize(ImVec2(box.size.x, box.size.y));
ImGui::ShowDemoWindow();
}

float getScaleFactor() const noexcept; float getScaleFactor() const noexcept;


protected: protected:
void onContextCreate(const ContextCreateEvent& e) override; void onContextCreate(const ContextCreateEvent& e) override;
void onContextDestroy(const ContextDestroyEvent& e) override; void onContextDestroy(const ContextDestroyEvent& e) override;

private:
void drawFramebuffer() override;
void onHover(const HoverEvent& e) override; void onHover(const HoverEvent& e) override;
void onDragHover(const DragHoverEvent& e) override; void onDragHover(const DragHoverEvent& e) override;
void onDragLeave(const DragLeaveEvent& e) override; void onDragLeave(const DragLeaveEvent& e) override;
@@ -49,4 +39,15 @@ private:
void onButton(const ButtonEvent& e) override; void onButton(const ButtonEvent& e) override;
void onSelectKey(const SelectKeyEvent& e) override; void onSelectKey(const SelectKeyEvent& e) override;
void onSelectText(const SelectTextEvent& e) override; void onSelectText(const SelectTextEvent& e) override;
void setAsCurrentContext();

virtual void drawImGui()
{
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize(ImVec2(box.size.x, box.size.y));
ImGui::ShowDemoWindow();
}

private:
void drawFramebuffer() override;
}; };

+ 2
- 1
plugins/Cardinal/src/TextEditor.cpp View File

@@ -71,6 +71,7 @@ struct TextEditorModule : Module {
widget->setText(text); widget->setText(text);
} }
#endif #endif
return;
} }


if (json_t* const widthJ = json_object_get(rootJ, "width")) if (json_t* const widthJ = json_object_get(rootJ, "width"))
@@ -202,7 +203,7 @@ struct TextEditorLoadFileItem : MenuItem {
text = "Load File"; text = "Load File";
} }


void onAction(const event::Action &e) override
void onAction(const event::Action&) override
{ {
TextEditorModule* const module = this->module;; TextEditorModule* const module = this->module;;
WeakPtr<ImGuiTextEditor> widget = this->widget; WeakPtr<ImGuiTextEditor> widget = this->widget;


Loading…
Cancel
Save