Skip to content

Commit 389f3be

Browse files
committed
(WIP) UI: Implement Dear ImGui UI part
1 parent 344d84f commit 389f3be

File tree

5 files changed

+178
-7
lines changed

5 files changed

+178
-7
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_subdirectory (dpf)
1212

1313
include_directories (
1414
src/
15+
dpf-widgets/opengl/
1516
)
1617

1718
set (SRC_BACKEND
@@ -42,5 +43,7 @@ dpf_add_plugin (${PROJECT_NAME}
4243
src/CetoneUIHelper.cpp
4344
src/Images/CetoneArtwork.cpp
4445
src/Fonts/CetoneFonts.cpp
46+
dpf-widgets/opengl/DearImGui.cpp
47+
src/Widgets/ImGui_UI.cpp
4548
)
4649

src/CetoneUI.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ CCetoneUI::CCetoneUI()
107107
_createSwitchButton(fBtnGlideState, pPortaMode, 764, 150);
108108

109109
_createSwitchButton(fBtnLFOTrigger, pLfo1Trig, 677, 260);
110+
111+
/* ImGui instance (popup menus, subwindows, etc.) */
112+
fImGuiInstance = new ImGuiUI(getTopLevelWidget(), this);
113+
114+
/* "About" button (by clicking the plugin logo) */
115+
_createHiddenButton(fBtnAbout, BTN_ABOUT, Size<uint>(118, 25), Point<int>(0, 0));
116+
117+
/* Popup menu button on params which has constant value sets */
118+
_createHiddenButton(fBtnOsc1Waveform, pOsc1Wave, Size<uint>(45, 10 + 2), Point<int>(10 + 48 * 2, 200 + 2));
110119
}
111120

112121
void CCetoneUI::parameterChanged(uint32_t index, float value)
@@ -349,14 +358,24 @@ void CCetoneUI::parameterChanged(uint32_t index, float value)
349358

350359
void CCetoneUI::imageButtonClicked(ImageButton* button, int)
351360
{
352-
#if 0
353-
switch (button->getId()) {
354-
case BTN_PANIC: {
355-
panic();
356-
break;
357-
}
361+
switch (button->getId())
362+
{
363+
case BTN_ABOUT:
364+
{
365+
if (fImGuiInstance)
366+
fImGuiInstance->isAboutWindowOpen = !fImGuiInstance->isAboutWindowOpen;
367+
break;
368+
}
369+
case pOsc1Wave:
370+
{
371+
if(fImGuiInstance)
372+
{
373+
fImGuiInstance->menuPos = ImVec2(fBtnOsc1Waveform->getAbsolutePos().getX(), fBtnOsc1Waveform->getAbsolutePos().getY() + fBtnOsc1Waveform->getHeight());
374+
fImGuiInstance->requestMenuId = pOsc1Wave;
375+
}
376+
break;
377+
}
358378
}
359-
#endif
360379
}
361380

362381
void CCetoneUI::imageSwitchClicked(ImageSwitch* button, bool down)

src/CetoneUI.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "ImageWidgets.hpp"
55
#include "NanoVG.hpp"
66

7+
#include "Widgets/ImGui_UI.hpp"
8+
79
using DGL_NAMESPACE::ImageAboutWindow;
810
using DGL_NAMESPACE::ImageButton;
911
using DGL_NAMESPACE::ImageKnob;
@@ -55,6 +57,12 @@ class CCetoneUI : public DISTRHO::UI,
5557
NanoVG fNanoText;
5658
char fLabelBuffer[32 + 1];
5759

60+
// -------------------------------------------------------------------
61+
// Dear ImGui Instance
62+
63+
ScopedPointer<ImGuiUI> fImGuiInstance;
64+
friend class ImGuiUI;
65+
5866
// -------------------------------------------------------------------
5967
// Image resources
6068

@@ -93,6 +101,13 @@ class CCetoneUI : public DISTRHO::UI,
93101
ScopedPointer<ImageSwitch> fBtnGlideState;
94102
ScopedPointer<ImageSwitch> fBtnLFOTrigger;
95103

104+
// -------------------------------------------------------------------
105+
// Buttons
106+
107+
ScopedPointer<ImageButton> fBtnAbout;
108+
109+
ScopedPointer<ImageButton> fBtnOsc1Waveform;
110+
96111
// -------------------------------------------------------------------
97112
// Helpers
98113

@@ -127,5 +142,6 @@ class CCetoneUI : public DISTRHO::UI,
127142
// Button IDs
128143

129144
constexpr uint BTN_PANIC = d_cconst('p', 'n', 'i', 'c');
145+
constexpr uint BTN_ABOUT = d_cconst('a', 'b', 't', '.');
130146

131147
// -----------------------------------------------------------------------

src/Widgets/ImGui_UI.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "ImGui_UI.hpp"
2+
#include "DistrhoPluginInfo.h"
3+
4+
#include "Structures.h"
5+
#include "Defines.h"
6+
7+
#include "CetoneUI.hpp" // For class CCetoneUI
8+
9+
void ImGuiUI::onImGuiDisplay()
10+
{
11+
double scaleFactor = getScaleFactor() * userScaling;
12+
const double initialSize = 800 * scaleFactor;
13+
14+
//
15+
// "About" Window
16+
//
17+
{
18+
ImGui::SetNextWindowPos(ImVec2(initialSize / 4, initialSize / 16), ImGuiCond_Once);
19+
ImGui::SetNextWindowSize(ImVec2(600, 230), ImGuiCond_Once);
20+
21+
if (isAboutWindowOpen)
22+
{
23+
ImGui::Begin("About " DISTRHO_PLUGIN_NAME, &isAboutWindowOpen, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize);
24+
{
25+
ImGui::SeparatorText(DISTRHO_PLUGIN_NAME);
26+
ImGui::Text("Light-weight monophonic analogue-style synthesizer, by Neotec Software.\n");
27+
28+
ImGui::SeparatorText("Authors");
29+
ImGui::BulletText("René 'Neotec' Jeschke - Original developer");
30+
ImGui::BulletText("AnClark Liu <[email protected]> - Ported to DPF, Further developments");
31+
32+
ImGui::SeparatorText("License");
33+
ImGui::BulletText("This project is licensed under GNU General Public License, version 3.");
34+
35+
ImGui::Text("\n\n");
36+
ImGui::Dummy(ImVec2(490, 0));
37+
ImGui::SameLine();
38+
if (ImGui::Button("OK", ImVec2(80, 0)))
39+
isAboutWindowOpen = false;
40+
}
41+
ImGui::End();
42+
}
43+
}
44+
45+
46+
// Specify menu position
47+
ImGui::SetNextWindowPos(menuPos); // 指定屏幕坐标位置
48+
49+
// Here, variable `requestTestMenuOpen` acts as an "event flag" to request ImGui to show the menu.
50+
//
51+
// Dear ImGui has its own mechanism to show popup menus, which does not require a flag to control its exisitance.
52+
// This is quite different from window (ImGui::Begin()).
53+
// So just call this function once, your popup will stick on the screen unless you do some operations.
54+
//
55+
switch (requestMenuId)
56+
{
57+
case pOsc1Wave:
58+
ImGui::OpenPopup("menu_osc1_wave");
59+
60+
default:
61+
requestMenuId = 0; // Reset flag state after EACH menu popup
62+
}
63+
64+
// Create popup menu
65+
// [NOTICE] ImGui::BeginPopup() MUST be put after ImGui::OpenPopup(), otherwise popup won't show!
66+
if (ImGui::BeginPopup("menu_osc1_wave"))
67+
{
68+
ImGui::SeparatorText("Waveform");
69+
if (ImGui::MenuItem("Saw")) { _triggerParamUpdate(pOsc1Wave, ui->_pi2f(OWAVE_SAW, OWAVE_MAX)); }
70+
if (ImGui::MenuItem("Pulse")) { _triggerParamUpdate(pOsc1Wave, ui->_pi2f(OWAVE_PULSE, OWAVE_MAX)); }
71+
if (ImGui::MenuItem("Triangle")) { _triggerParamUpdate(pOsc1Wave, ui->_pi2f(OWAVE_TRI, OWAVE_MAX)); }
72+
if (ImGui::MenuItem("Sine")) { _triggerParamUpdate(pOsc1Wave, ui->_pi2f(OWAVE_SINE, OWAVE_MAX)); }
73+
if (ImGui::MenuItem("Noise")) { _triggerParamUpdate(pOsc1Wave, ui->_pi2f(OWAVE_C64NOISE, OWAVE_MAX)); }
74+
ImGui::EndPopup();
75+
}
76+
}
77+
78+
void ImGuiUI::_triggerParamUpdate(uint32_t paramId, float newValue)
79+
{
80+
ui->setParameterValue(paramId, newValue); // Tell the DSP to update parameter value
81+
ui->parameterChanged(paramId, newValue); // Request UI refresh
82+
}

src/Widgets/ImGui_UI.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Inspector Window for DPF
3+
* Copyright (C) 2022-2025 Filipe Coelho <[email protected]>
4+
*
5+
* Permission to use, copy, modify, and/or distribute this software for any purpose with
6+
* or without fee is hereby granted, provided that the above copyright notice and this
7+
* permission notice appear in all copies.
8+
*
9+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10+
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11+
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12+
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13+
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14+
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
#pragma once
18+
19+
#include "DearImGui.hpp"
20+
21+
// Forward decls.
22+
class CCetoneUI;
23+
24+
25+
// --------------------------------------------------------------------------------------------------------------------
26+
27+
class ImGuiUI : public ImGuiTopLevelWidget
28+
{
29+
30+
public:
31+
CCetoneUI *ui;
32+
33+
bool isAboutWindowOpen = false;
34+
uint16_t requestMenuId = 0;
35+
36+
ImVec2 menuPos{0, 0};
37+
38+
double userScaling = 1.0f;
39+
40+
ImGuiUI(TopLevelWidget* const tlw, CCetoneUI* const ui) :
41+
ImGuiTopLevelWidget(tlw->getWindow()),
42+
ui(ui)
43+
{
44+
}
45+
46+
protected:
47+
void onImGuiDisplay() override;
48+
49+
private:
50+
void _triggerParamUpdate(uint32_t paramId, float newValue);
51+
};

0 commit comments

Comments
 (0)