Skip to content

Commit 927c273

Browse files
committed
Some cleanup; working out how to make new nodes cleanly
1 parent bac8ea4 commit 927c273

File tree

3 files changed

+242
-216
lines changed

3 files changed

+242
-216
lines changed

app/demo.cpp

Lines changed: 104 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99
#include <nodegraph/widgets/layout.h>
1010
#include <nodegraph/widgets/node.h>
1111
#include <nodegraph/widgets/widget.h>
12-
#include <nodegraph/widgets/widget_knob.h>
13-
#include <nodegraph/widgets/widget_label.h>
14-
#include <nodegraph/widgets/widget_slider.h>
15-
#include <nodegraph/widgets/widget_socket.h>
16-
#include <nodegraph/widgets/widget_waveslider.h>
1712

1813
#include <zing/audio/audio.h>
1914

15+
#include "nodes/node_oscillator.h"
16+
2017
extern "C" {
2118
#include <soundpipe.h>
2219
}
@@ -27,65 +24,10 @@ using namespace NodeGraph;
2724
using namespace Zest;
2825
namespace fs = std::filesystem;
2926

30-
namespace {
3127
std::unique_ptr<CanvasImGui> spCanvas;
3228
const glm::vec2 worldCenter = glm::vec2(0.0f);
3329

34-
struct SetterWave : public ISliderCB
35-
{
36-
SliderValue myVal;
37-
virtual void UpdateSlider(Slider* pSlider, SliderOp op, SliderValue& val)
38-
{
39-
myVal.type = SliderType::Mark;
40-
myVal.step = 0.33f;
41-
if (op == SliderOp::Get)
42-
{
43-
myVal.name = pSlider->GetLabel();
44-
myVal.valueText = fmt::format("{:1.2f}", myVal.value);
45-
myVal.units = "dB";
46-
myVal.valueFlags = WidgetValueFlags::NoQuantization;
47-
val = myVal;
48-
}
49-
else
50-
{
51-
myVal = val;
52-
}
53-
}
54-
};
55-
56-
SetterWave swave;
57-
58-
struct Setter : public ISliderCB
59-
{
60-
SliderValue myVal;
61-
std::string m_units;
62-
Setter(std::string units)
63-
: m_units(units)
64-
{
65-
}
66-
67-
virtual void UpdateSlider(Slider* pSlider, SliderOp op, SliderValue& val)
68-
{
69-
myVal.type = SliderType::Mark;
70-
myVal.step = 0.2f;
71-
if (op == SliderOp::Get)
72-
{
73-
myVal.name = pSlider->GetLabel();
74-
myVal.valueText = fmt::format("{:1.2f}", myVal.value);
75-
myVal.units = m_units;
76-
val = myVal;
77-
}
78-
else
79-
{
80-
myVal = val;
81-
}
82-
}
83-
};
84-
85-
Setter s1 = Setter("dB");
86-
Setter s2 = Setter("Hz");
87-
88-
}
30+
std::shared_ptr<Oscillator> spOsc;
8931

9032
NodeGraph::Canvas* demo_get_canvas()
9133
{
@@ -102,148 +44,8 @@ void demo_resize(const glm::vec2& size, IFontTexture* pFontTexture)
10244
spCanvas->SetPixelRegionSize(size);
10345
spCanvas->SetWorldAtCenter(worldCenter);
10446

105-
// Node 1
106-
#if 0
107-
{
108-
auto spWidget = std::make_shared<Node>("Node 1");
109-
spWidget->SetRect(NRectf(0.0f, -350.0f, 400.0f, 300.0f));
110-
spCanvas->GetRootLayout()->AddChild(spWidget);
111-
112-
// Child
113-
auto spNodeChild = std::make_shared<Node>("Child");
114-
spNodeChild->SetRect(NRectf(10.0f, 40.0f, 270.0f, 190.0f));
115-
spNodeChild->SetFlags(WidgetFlags::DoNotLayout);
116-
spWidget->GetLayout()->AddChild(spNodeChild);
117-
}
118-
119-
// Node 2
120-
{
121-
auto spWidget = std::make_shared<Node>("Node 2" ICON_FA_SEARCH);
122-
spWidget->SetRect(NRectf(0.0f, 0.0f, 400.0f, 300.0f));
123-
spCanvas->GetRootLayout()->AddChild(spWidget);
124-
125-
auto spRootLayout = std::make_shared<Layout>(LayoutType::Vertical);
126-
spRootLayout->SetLabel("Vertical Node 2");
127-
spWidget->SetLayout(spRootLayout);
128-
129-
// Keep same height, expand the width
130-
auto spCustom = std::make_shared<Widget>("Custom");
131-
spCustom->SetConstraints(glm::uvec2(LayoutConstraint::Preferred, LayoutConstraint::Preferred));
132-
spCustom->SetRect(NRectf(0.0f, 0.0f, 100.0f, 75.0f));
133-
spRootLayout->AddChild(spCustom);
134-
135-
// Sliders
136-
{
137-
for (int i = 0; i < 2; i++)
138-
{
139-
auto spSliderLayout = std::make_shared<Layout>(LayoutType::Horizontal);
140-
spSliderLayout->SetContentsMargins(glm::vec4(0.0f));
141-
spSliderLayout->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Preferred));
142-
spSliderLayout->SetRect(NRectf(0.0f, 0.0f, 100.0f, 50.0f));
143-
spSliderLayout->SetLabel("Slider Horizontal Layout");
144-
spRootLayout->AddChild(spSliderLayout);
145-
146-
auto spSocket = std::make_shared<Socket>("Freq");
147-
spSocket->SetRect(NRectf(0.0f, 0.0f, 30.0f, 30.0f));
148-
spSocket->SetConstraints(glm::uvec2(LayoutConstraint::Preferred, LayoutConstraint::Expanding));
149-
spSliderLayout->AddChild(spSocket);
150-
151-
auto spSlider = std::make_shared<Slider>("Amp", &s1);
152-
spSlider->SetRect(NRectf(0.0f, 0.0f, 190.0f, 50.0f));
153-
spSliderLayout->AddChild(spSlider);
154-
155-
spSlider = std::make_shared<Slider>("Freq", &s2);
156-
spSlider->SetRect(NRectf(0.0f, 0.0f, 190.0f, 50.0f));
157-
spSliderLayout->AddChild(spSlider);
158-
159-
{
160-
auto spSubLayout = std::make_shared<Layout>(LayoutType::Horizontal);
161-
spSubLayout->SetLabel("Sub Layout");
162-
spSliderLayout->AddChild(spSubLayout);
163-
164-
spSlider = std::make_shared<Slider>("A");
165-
spSlider->SetRect(NRectf(0.0f, 0.0f, 190.0f, 30.0f));
166-
spSubLayout->AddChild(spSlider);
167-
168-
spSlider = std::make_shared<Slider>("B");
169-
spSlider->SetRect(NRectf(0.0f, 0.0f, 190.0f, 30.0f));
170-
spSlider->SetPadding(glm::vec4(4.0f));
171-
spSubLayout->AddChild(spSlider);
172-
}
173-
}
174-
}
175-
// Knobs
176-
{
177-
auto spKnobLayout = std::make_shared<Layout>(LayoutType::Horizontal);
178-
spKnobLayout->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Expanding));
179-
spKnobLayout->SetLabel("Knob Horizontal Layout");
180-
spRootLayout->AddChild(spKnobLayout);
181-
182-
auto spKnob = std::make_shared<Knob>("Attack");
183-
spKnob->SetRect(NRectf(0.0f, 0.0f, 200.0f, 120.0f));
184-
spKnob->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Expanding));
185-
spKnob->SetPadding(glm::vec4(4.0f));
186-
spKnobLayout->AddChild(spKnob);
187-
188-
spKnob = std::make_shared<Knob>("Decay");
189-
spKnob->SetRect(NRectf(0.0f, 0.0f, 200.0f, 120.0f));
190-
spKnob->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Preferred));
191-
spKnob->SetPadding(glm::vec4(4.0f));
192-
spKnobLayout->AddChild(spKnob);
193-
}
194-
}
195-
#endif
196-
auto spOsc = std::make_shared<Node>("Oscillator" ICON_FA_SEARCH);
197-
spOsc->SetRect(NRectf(0.0f, 0.0f, 400.0f, 300.0f));
198-
spCanvas->GetRootLayout()->AddChild(spOsc);
199-
200-
auto spRootLayout = std::make_shared<Layout>(LayoutType::Vertical);
201-
spOsc->SetLayout(spRootLayout);
202-
203-
auto spWaveSlider = std::make_shared<WaveSlider>("Wave", &swave);
204-
spWaveSlider->SetRect(NRectf(0.0f, 0.0f, 190.0f, 50.0f));
205-
spWaveSlider->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Preferred));
206-
207-
spRootLayout->AddChild(spWaveSlider);
208-
209-
// Keep same height, expand the width
210-
auto spCustom = std::make_shared<Widget>("Custom");
211-
spCustom->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Preferred));
212-
spCustom->SetRect(NRectf(0.0f, 0.0f, 100.0f, 50.0f));
213-
spCustom->AddPostDrawCB([=](Canvas& canvas, const NRectf& rect) {
214-
spWaveSlider->DrawGeneratedWave(canvas, rect);
215-
});
216-
spRootLayout->AddChild(spCustom);
217-
218-
// Sliders
219-
auto spHorzLayout = std::make_shared<Layout>(LayoutType::Horizontal);
220-
spHorzLayout->SetContentsMargins(glm::vec4(0.0f));
221-
spHorzLayout->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Preferred));
222-
spHorzLayout->SetRect(NRectf(0.0f, 0.0f, 100.0f, 50.0f));
223-
spRootLayout->AddChild(spHorzLayout);
224-
225-
auto spSocket = std::make_shared<Socket>("Freq", SocketType::Left);
226-
spSocket->SetRect(NRectf(0.0f, 0.0f, 30.0f, 30.0f));
227-
spSocket->SetConstraints(glm::uvec2(LayoutConstraint::Preferred, LayoutConstraint::Expanding));
228-
spHorzLayout->AddChild(spSocket);
229-
230-
auto spSlider = std::make_shared<Slider>("Amp", &s1);
231-
spSlider->SetRect(NRectf(0.0f, 0.0f, 190.0f, 50.0f));
232-
spHorzLayout->AddChild(spSlider);
233-
234-
spSlider = std::make_shared<Slider>("Freq", &s2);
235-
spSlider->SetRect(NRectf(0.0f, 0.0f, 190.0f, 50.0f));
236-
spHorzLayout->AddChild(spSlider);
237-
238-
spSocket = std::make_shared<Socket>("Amp", SocketType::Right);
239-
spSocket->SetRect(NRectf(0.0f, 0.0f, 30.0f, 30.0f));
240-
spSocket->SetConstraints(glm::uvec2(LayoutConstraint::Preferred, LayoutConstraint::Expanding));
241-
spHorzLayout->AddChild(spSocket);
242-
243-
/*
244-
auto& settings = Zest::GlobalSettingsManager::Instance();
245-
auto theme = settings.GetCurrentTheme();
246-
settings.Load(fs::path(NODEGRAPH_ROOT) / "theme.toml");*/
47+
spOsc = std::make_shared<Oscillator>("Oscillator", AudioUtils::WaveTableType::Sine);
48+
spOsc->BuildNode(*spCanvas);
24749
}
24850
spCanvas->SetPixelRegionSize(size);
24951
}
@@ -414,3 +216,102 @@ void demo_cleanup()
414216

415217
spCanvas.reset();
416218
}
219+
/*
220+
221+
// Node 1
222+
#if 0
223+
{
224+
auto spWidget = std::make_shared<Node>("Node 1");
225+
spWidget->SetRect(NRectf(0.0f, -350.0f, 400.0f, 300.0f));
226+
spCanvas->GetRootLayout()->AddChild(spWidget);
227+
228+
// Child
229+
auto spNodeChild = std::make_shared<Node>("Child");
230+
spNodeChild->SetRect(NRectf(10.0f, 40.0f, 270.0f, 190.0f));
231+
spNodeChild->SetFlags(WidgetFlags::DoNotLayout);
232+
spWidget->GetLayout()->AddChild(spNodeChild);
233+
}
234+
235+
// Node 2
236+
{
237+
auto spWidget = std::make_shared<Node>("Node 2" ICON_FA_SEARCH);
238+
spWidget->SetRect(NRectf(0.0f, 0.0f, 400.0f, 300.0f));
239+
spCanvas->GetRootLayout()->AddChild(spWidget);
240+
241+
auto spRootLayout = std::make_shared<Layout>(LayoutType::Vertical);
242+
spRootLayout->SetLabel("Vertical Node 2");
243+
spWidget->SetLayout(spRootLayout);
244+
245+
// Keep same height, expand the width
246+
auto spCustom = std::make_shared<Widget>("Custom");
247+
spCustom->SetConstraints(glm::uvec2(LayoutConstraint::Preferred, LayoutConstraint::Preferred));
248+
spCustom->SetRect(NRectf(0.0f, 0.0f, 100.0f, 75.0f));
249+
spRootLayout->AddChild(spCustom);
250+
251+
// Sliders
252+
{
253+
for (int i = 0; i < 2; i++)
254+
{
255+
auto spSliderLayout = std::make_shared<Layout>(LayoutType::Horizontal);
256+
spSliderLayout->SetContentsMargins(glm::vec4(0.0f));
257+
spSliderLayout->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Preferred));
258+
spSliderLayout->SetRect(NRectf(0.0f, 0.0f, 100.0f, 50.0f));
259+
spSliderLayout->SetLabel("Slider Horizontal Layout");
260+
spRootLayout->AddChild(spSliderLayout);
261+
262+
auto spSocket = std::make_shared<Socket>("Freq");
263+
spSocket->SetRect(NRectf(0.0f, 0.0f, 30.0f, 30.0f));
264+
spSocket->SetConstraints(glm::uvec2(LayoutConstraint::Preferred, LayoutConstraint::Expanding));
265+
spSliderLayout->AddChild(spSocket);
266+
267+
auto spSlider = std::make_shared<Slider>("Amp", &s1);
268+
spSlider->SetRect(NRectf(0.0f, 0.0f, 190.0f, 50.0f));
269+
spSliderLayout->AddChild(spSlider);
270+
271+
spSlider = std::make_shared<Slider>("Freq", &s2);
272+
spSlider->SetRect(NRectf(0.0f, 0.0f, 190.0f, 50.0f));
273+
spSliderLayout->AddChild(spSlider);
274+
275+
{
276+
auto spSubLayout = std::make_shared<Layout>(LayoutType::Horizontal);
277+
spSubLayout->SetLabel("Sub Layout");
278+
spSliderLayout->AddChild(spSubLayout);
279+
280+
spSlider = std::make_shared<Slider>("A");
281+
spSlider->SetRect(NRectf(0.0f, 0.0f, 190.0f, 30.0f));
282+
spSubLayout->AddChild(spSlider);
283+
284+
spSlider = std::make_shared<Slider>("B");
285+
spSlider->SetRect(NRectf(0.0f, 0.0f, 190.0f, 30.0f));
286+
spSlider->SetPadding(glm::vec4(4.0f));
287+
spSubLayout->AddChild(spSlider);
288+
}
289+
}
290+
}
291+
// Knobs
292+
{
293+
auto spKnobLayout = std::make_shared<Layout>(LayoutType::Horizontal);
294+
spKnobLayout->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Expanding));
295+
spKnobLayout->SetLabel("Knob Horizontal Layout");
296+
spRootLayout->AddChild(spKnobLayout);
297+
298+
auto spKnob = std::make_shared<Knob>("Attack");
299+
spKnob->SetRect(NRectf(0.0f, 0.0f, 200.0f, 120.0f));
300+
spKnob->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Expanding));
301+
spKnob->SetPadding(glm::vec4(4.0f));
302+
spKnobLayout->AddChild(spKnob);
303+
304+
spKnob = std::make_shared<Knob>("Decay");
305+
spKnob->SetRect(NRectf(0.0f, 0.0f, 200.0f, 120.0f));
306+
spKnob->SetConstraints(glm::uvec2(LayoutConstraint::Expanding, LayoutConstraint::Preferred));
307+
spKnob->SetPadding(glm::vec4(4.0f));
308+
spKnobLayout->AddChild(spKnob);
309+
}
310+
}
311+
#endif
312+
313+
/*
314+
auto& settings = Zest::GlobalSettingsManager::Instance();
315+
auto theme = settings.GetCurrentTheme();
316+
settings.Load(fs::path(NODEGRAPH_ROOT) / "theme.toml");
317+
*/

0 commit comments

Comments
 (0)