From bd7ee2d697529ddcfe199cd52a18434a071bdc8a Mon Sep 17 00:00:00 2001 From: SCRIME Date: Thu, 21 Apr 2022 10:17:52 +0200 Subject: [PATCH 1/3] First Push --- examples/Helpers/ImageUi.hpp | 60 +++++++++- examples/Helpers/MyMosca.hpp | 211 +++++++++++++++++++++++++++++++++++ examples/Helpers/MySpati.hpp | 63 +++++++++++ examples/Helpers/Ui.hpp | 2 +- 4 files changed, 332 insertions(+), 4 deletions(-) create mode 100644 examples/Helpers/MyMosca.hpp create mode 100644 examples/Helpers/MySpati.hpp diff --git a/examples/Helpers/ImageUi.hpp b/examples/Helpers/ImageUi.hpp index f7143cf1..a2abc365 100644 --- a/examples/Helpers/ImageUi.hpp +++ b/examples/Helpers/ImageUi.hpp @@ -73,13 +73,66 @@ struct custom_slider struct custom_anim { using item_type = custom_anim; - static constexpr double width() { return 200.; } - static constexpr double height() { return 200.; } + static constexpr double width() { return 1000.; } + static constexpr double height() { return 1000.; } void paint(avnd::painter auto ctx) { constexpr double side = 40.; + + //White Square --> ok +// ctx.set_fill_color({255, 255, 255, 255}); +// ctx.draw_rect(50, 50, 50, 50); +// ctx.fill(); + + //White Circle --> ok +// ctx.set_fill_color({255, 255, 255, 255}); +// ctx.draw_circle(50, 100, 50); +// ctx.fill(); + + //Text --> ok +// ctx.set_fill_color({255, 255, 255, 255}); +// ctx.set_font("Ubuntu"); +// ctx.set_font_size(15); +// ctx.draw_text(50,100, "Hello World !"); +// ctx.fill(); + + //Image --> ok +// ctx.begin_path(); +// ctx.draw_pixmap(50,50,"//home/scrime/Images/StageScrime/external-content.duckduckgo.com.jpeg"); + + //Triangle --> ok +// ctx.set_fill_color({0, 0, 204, 255}); +// ctx.draw_triangle(250, 500, 750, 500, 500, 250); +// ctx.fill(); + + //Gradient --> ok +// ctx.set_fill_color({255, 255, 255, 255}); +// ctx.set_linear_gradient(250, 250, 250, 750, {0, 255, 234, 255}, {239, 0, 255, 255}); +// //ctx.set_radial_gradient(500, 500, 250, {0, 255, 234, 255}, {239, 0, 255, 255}); +// //ctx.set_conical_gradient(500, 500, 90, {0, 255, 234, 255}, {239, 0, 255, 255}); +// ctx.draw_rect(250, 250, 500, 500); +// ctx.fill(); + + //Polygon --> ok +// double tab[8] = {250, 500, 750, 500, 500, 250, 0, 0}; +// ctx.set_fill_color({0, 0, 204, 255}); +// ctx.draw_polygon(tab, 4); +// ctx.fill(); + + //Star --> ok +// ctx.set_fill_color({200, 0, 0, 255}); +// ctx.set_linear_gradient(0, 0, 1000, 1000, {0, 255, 234, 255}, {239, 0, 255, 255}); +// ctx.draw_star(500, 500, 100, 400, 25); +// ctx.fill(); + + //Clock --> test + ctx.draw_analog_clock(width(), height(), {127, 0, 127, 255}, {0, 127, 127, 190}, {0, 0, 0, 255}); + + return; + + ctx.translate(100, 100); ctx.rotate(rot += 0.1); for(int i = 0; i < 10; i++) @@ -115,6 +168,7 @@ struct AdvancedUi struct ins { halp::knob_f32<"Float", halp::range{.min = -1000., .max = 1000., .init = 100.}> float_ctl; + //halp::knob_f32<"Test", halp::range{.min = 0., .max = 1., .init = 0.5}> tests; } inputs; struct { } outputs; @@ -129,7 +183,7 @@ struct AdvancedUi halp_meta(height, 200) halp_meta(font, "Inconsolata") - halp::custom_item widget{{.x = 190, .y = 170}}; + halp::custom_item widget{{.x = 500, .y = 920}}; halp::custom_item_base anim{.x = 90, .y = -50}; }; }; diff --git a/examples/Helpers/MyMosca.hpp b/examples/Helpers/MyMosca.hpp new file mode 100644 index 00000000..c7fbc40c --- /dev/null +++ b/examples/Helpers/MyMosca.hpp @@ -0,0 +1,211 @@ +#pragma once + +/* SPDX-License-Identifier: GPL-3.0-or-later */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +//Modif +#include +#include +#include +#include + +using namespace std; + +namespace Spat +{ +struct MyMosca +{ + static consteval auto name() { return "MoscaUi example"; } + static consteval auto c_name() { return "mosca_ui"; } + static consteval auto uuid() { return "f253d48c-b090-497c-a561-ec442efdcc92"; } + //static consteval auto category() { return "Test"; } + + struct custom_mosca + { + static constexpr double width() { return 300.; } // X + static constexpr double height() { return 300.; } // Y + + void set_value(const auto& control, halp::xy_type value) + { + this->value = avnd::map_control_to_01(control, value); + } + + static auto value_to_control(auto& control, halp::xy_type value) + { + return avnd::map_control_from_01(control, value); + } + + void paint(avnd::painter auto ctx) + { + double c_x = width()/2; + double c_y = height()/2; + double c_r = 150; + double c_r_bis = 4; + + float m_x = value.x * width(); + float m_y = value.y * height(); + float m_r = 15.; + + ctx.set_stroke_color({200, 200, 200, 255}); + ctx.set_stroke_width(2.); + ctx.set_fill_color({120, 120, 120, 255}); + ctx.begin_path(); + ctx.draw_rect(0., 0., width(), height()); + ctx.fill(); + ctx.stroke(); + + ctx.begin_path(); + ctx.set_stroke_color({255, 255, 255, 255}); + ctx.set_fill_color({255, 255, 255, 255}); + ctx.draw_circle(c_x, c_y, c_r); + ctx.fill(); + ctx.stroke(); + + ctx.begin_path(); + ctx.set_stroke_color({255, 255, 255, 255}); + ctx.set_fill_color({0, 0, 0, 255}); + ctx.draw_circle(c_x, c_y, c_r_bis); + ctx.fill(); + ctx.stroke(); + + float formula = sqrt(pow((m_x - c_x), 2) + pow((m_y - c_y), 2)); + if(formula + (m_r - 5) < c_r){ + ctx.begin_path(); + ctx.set_fill_color({90, 90, 90, 255}); + ctx.draw_circle(m_x, m_y, m_r); + ctx.fill(); + ctx.close_path(); + + ctx.begin_path(); + ctx.set_fill_color({255, 255, 255, 255}); + ctx.set_font("Ubuntu"); + ctx.set_font_size(15); + ctx.draw_text(m_x-6, m_y+7, "1"); + ctx.fill(); + } + + /* + ctx.begin_path(); + ctx.set_fill_color({255, 255, 255, 255}); + ctx.set_font("Ubuntu"); + ctx.set_font_size(15); + std::stringstream strs; + strs << value.x; + std::string test = strs.str(); + ctx.draw_text(50,100, test); + std::stringstream strs2; + strs2 << value.y; + std::string test2 = strs2.str(); + ctx.draw_text(50,200, test2); + + ctx.fill(); + */ + + } + + bool mouse_press(double x, double y) + { + transaction.start(); + mouse_move(x, y); + return true; + } + + void mouse_move(double x, double y) + { + halp::xy_type res; + res.x = std::clamp(x / width(), 0., 1.); + + res.y = std::clamp(y / height(), 0., 1.); + transaction.update(res); + } + + void mouse_release(double x, double y) + { + mouse_move(x, y); + transaction.commit(); + } + + halp::transaction> transaction; + halp::xy_type value{}; + }; + + struct ins + { + halp::dynamic_audio_bus<"Input", double> audio; + + halp::hslider_f32<"A", halp::range{.min = -10, .max = 10, .init = 0}> a; + halp::hslider_f32<"B", halp::range{.min = -10, .max = 10, .init = 0}> b; + halp::spinbox_i32<"Int spinbox", halp::range{0, 1000, 10}> c; + halp::hslider_f32<"A", halp::range{.min = -10, .max = 10, .init = 0}> d; + halp::hslider_f32<"B", halp::range{.min = -10, .max = 10, .init = 0}> e; + } inputs; + + struct outs + { + halp::dynamic_audio_bus<"Output", double> audio; + } outputs; + + struct ui { + halp_meta(name, "Main") + halp_meta(layout, halp::layouts::hbox) + halp_meta(background, "background.svg") + halp_meta(width, 600) + halp_meta(height, 300) + halp_meta(font, "Inconsolata") + + struct { + halp_meta(name, "Mosca") + halp_meta(layout, halp::layouts::vbox) + halp_meta(background, halp::colors::mid) + + halp::custom_item widget{{.x = 500, .y = 920}}; + } mosca; + + struct { + halp_meta(name, "Panel") + halp_meta(layout, halp::layouts::vbox) + halp_meta(width, 300) + halp_meta(height, 300) + + halp::item<&ins::a> w1; + halp::item<&ins::b> w2; + halp::item<&ins::c> w3; + halp::item<&ins::d> w4; + halp::item<&ins::e> w5; + } panel; + + }; + + void operator()(halp::tick t, halp::xy_type value) + { + // Process the input buffer + for (int i = 0; i < inputs.audio.channels; i++) + { + auto* in = inputs.audio[i]; + auto* out = outputs.audio[i]; + + float& prev = this->previous_values[i]; + + for (int j = 0; j < t.frames; j++) + { + out[j] = value.x * in[j]; + prev = out[j]; + } + } + } +private: + // Here we have some state which depends on the host configuration (number of channels, etc). + std::vector previous_values{}; +}; +} diff --git a/examples/Helpers/MySpati.hpp b/examples/Helpers/MySpati.hpp new file mode 100644 index 00000000..142077ac --- /dev/null +++ b/examples/Helpers/MySpati.hpp @@ -0,0 +1,63 @@ +#pragma once + +/* SPDX-License-Identifier: GPL-3.0-or-later */ + +#include +#include +#include + +#include + +namespace Spat +{ + +/** + * Same as the "simple" example, but with the helpers library + */ +struct Spati +{ +public: + halp_meta(name, "Spati example") + halp_meta(c_name, "avnd_example") + halp_meta(uuid, "82bdb9b5-9cf8-440e-8675-c0caf4fc59b9") + + using setup = halp::setup; + using tick = halp::tick; + + struct + { + halp::dynamic_audio_bus<"Input", double> audio; + halp::hslider_f32<"Weight", halp::range{.min = 0., .max = 1., .init = 0.5}> weight; + } inputs; + + struct + { + halp::dynamic_audio_bus<"Output", double> audio; + } outputs; + + void prepare(halp::setup info) { previous_values.resize(info.input_channels); } + + // Do our processing for N samples + void operator()(halp::tick t) + { + // Process the input buffer + for (int i = 0; i < inputs.audio.channels; i++) + { + auto* in = inputs.audio[i]; + auto* out = outputs.audio[i]; + + float& prev = this->previous_values[i]; + + for (int j = 0; j < t.frames; j++) + { + out[j] = inputs.weight * in[j];// + (1.0 - inputs.weight) * prev; + prev = out[j]; + } + } + } + +private: + // Here we have some state which depends on the host configuration (number of channels, etc). + std::vector previous_values{}; +}; +} diff --git a/examples/Helpers/Ui.hpp b/examples/Helpers/Ui.hpp index d828697b..d99ccfa7 100644 --- a/examples/Helpers/Ui.hpp +++ b/examples/Helpers/Ui.hpp @@ -55,7 +55,7 @@ struct Ui // using enum halp::layouts; halp_meta(name, "Main") - halp_meta(layout, halp::layouts::hbox) + halp_meta(layout, halp::layouts::vbox) halp_meta(background, halp::colors::mid) struct { From e5023d27a8d0dbf1c561bbb47eee606c67aba4d7 Mon Sep 17 00:00:00 2001 From: ErkEntonio Date: Thu, 21 Apr 2022 15:00:15 +0200 Subject: [PATCH 2/3] Modification MyMosca.cpp --- examples/Helpers/MyMosca.hpp | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/examples/Helpers/MyMosca.hpp b/examples/Helpers/MyMosca.hpp index c7fbc40c..95fba405 100644 --- a/examples/Helpers/MyMosca.hpp +++ b/examples/Helpers/MyMosca.hpp @@ -142,7 +142,7 @@ struct MyMosca struct ins { - halp::dynamic_audio_bus<"Input", double> audio; + //halp::dynamic_audio_bus<"Input", double> audio; halp::hslider_f32<"A", halp::range{.min = -10, .max = 10, .init = 0}> a; halp::hslider_f32<"B", halp::range{.min = -10, .max = 10, .init = 0}> b; @@ -153,7 +153,7 @@ struct MyMosca struct outs { - halp::dynamic_audio_bus<"Output", double> audio; + //halp::dynamic_audio_bus<"Output", double> audio; } outputs; struct ui { @@ -186,26 +186,5 @@ struct MyMosca } panel; }; - - void operator()(halp::tick t, halp::xy_type value) - { - // Process the input buffer - for (int i = 0; i < inputs.audio.channels; i++) - { - auto* in = inputs.audio[i]; - auto* out = outputs.audio[i]; - - float& prev = this->previous_values[i]; - - for (int j = 0; j < t.frames; j++) - { - out[j] = value.x * in[j]; - prev = out[j]; - } - } - } -private: - // Here we have some state which depends on the host configuration (number of channels, etc). - std::vector previous_values{}; }; } From 12b617637700a0a79c765be6da1f1d3ec5862559 Mon Sep 17 00:00:00 2001 From: ErkEntonio Date: Fri, 22 Apr 2022 17:11:07 +0200 Subject: [PATCH 3/3] Modification MyMosca.hpp --- examples/Helpers/MyMosca.hpp | 141 +++++++++++++++++++++++++++++++---- 1 file changed, 126 insertions(+), 15 deletions(-) diff --git a/examples/Helpers/MyMosca.hpp b/examples/Helpers/MyMosca.hpp index 95fba405..d9919e96 100644 --- a/examples/Helpers/MyMosca.hpp +++ b/examples/Helpers/MyMosca.hpp @@ -31,6 +31,9 @@ struct MyMosca static consteval auto uuid() { return "f253d48c-b090-497c-a561-ec442efdcc92"; } //static consteval auto category() { return "Test"; } + using setup = halp::setup; + using tick = halp::tick; + struct custom_mosca { static constexpr double width() { return 300.; } // X @@ -140,36 +143,113 @@ struct MyMosca halp::xy_type value{}; }; + struct custom_button + { + static constexpr double width() { return 100.; } + static constexpr double height() { return 100.; } + + void paint(avnd::painter auto ctx) + { + ctx.set_stroke_color({200, 200, 200, 255}); + ctx.set_stroke_width(2.); + ctx.set_fill_color({100, 100, 100, 255}); + ctx.begin_path(); + ctx.draw_rounded_rect(0., 0., width(), height(), 5); + ctx.fill(); + ctx.stroke(); + + ctx.set_fill_color({0, 0, 0, 255}); + ctx.begin_path(); + ctx.draw_text(20., 20., fmt::format("{}", press_count)); + ctx.fill(); + } + + bool mouse_press(double x, double y) + { + on_pressed(); + return true; + } + + void mouse_move(double x, double y) + { + } + + void mouse_release(double x, double y) + { + } + + int press_count{0}; + + std::function on_pressed = [] { }; + }; + struct ins { - //halp::dynamic_audio_bus<"Input", double> audio; + halp::dynamic_audio_bus<"Input", double> audio; - halp::hslider_f32<"A", halp::range{.min = -10, .max = 10, .init = 0}> a; - halp::hslider_f32<"B", halp::range{.min = -10, .max = 10, .init = 0}> b; - halp::spinbox_i32<"Int spinbox", halp::range{0, 1000, 10}> c; - halp::hslider_f32<"A", halp::range{.min = -10, .max = 10, .init = 0}> d; - halp::hslider_f32<"B", halp::range{.min = -10, .max = 10, .init = 0}> e; + halp::knob_f32<"Volume", halp::range{.min = 0., .max = 5., .init = 1.}> volume; + halp::hslider_f32<"Level", halp::range{.min = -10, .max = 10, .init = 0}> level; + halp::hslider_f32<"Doppler amount", halp::range{.min = -10, .max = 10, .init = 0}> dopler; + halp::hslider_f32<"Concentration", halp::range{.min = -10, .max = 10, .init = 0}> concentration; + halp::hslider_f32<"Dst. amount", halp::range{.min = -10, .max = 10, .init = 0}> dstAmount; } inputs; struct outs { - //halp::dynamic_audio_bus<"Output", double> audio; + halp::dynamic_audio_bus<"Output", double> audio; } outputs; + struct ui_to_processor + { + int test; + }; + + struct processor_to_ui + { + int test_back; + }; + struct ui { halp_meta(name, "Main") halp_meta(layout, halp::layouts::hbox) halp_meta(background, "background.svg") - halp_meta(width, 600) + halp_meta(width, 900) halp_meta(height, 300) halp_meta(font, "Inconsolata") + struct bus { + void init(ui& ui) + { + ui.test.button.on_pressed = [&]{ + fprintf(stderr, "Sending message from UI thread !\n"); + this->send_message(ui_to_processor{.test = 1}); + }; + } + + static void process_message(ui& self, processor_to_ui msg) + { + fprintf(stderr, "Got message in ui thread !\n"); + self.test.button.press_count++; + } + + std::function send_message; + + }; + + struct { + halp_meta(layout, halp::layouts::container) + halp::custom_actions_item button{ + .x = 10 + , .y = 10 + }; + } test; + struct { halp_meta(name, "Mosca") halp_meta(layout, halp::layouts::vbox) halp_meta(background, halp::colors::mid) - halp::custom_item widget{{.x = 500, .y = 920}}; + halp::custom_item widget{{.x = 500, .y = 920}}; } mosca; struct { @@ -178,13 +258,44 @@ struct MyMosca halp_meta(width, 300) halp_meta(height, 300) - halp::item<&ins::a> w1; - halp::item<&ins::b> w2; - halp::item<&ins::c> w3; - halp::item<&ins::d> w4; - halp::item<&ins::e> w5; + halp::item<&ins::volume> w0; + halp::item<&ins::level> w1; + halp::item<&ins::dopler> w2; + halp::item<&ins::concentration> w3; + halp::item<&ins::dstAmount> w4; } panel; - }; + + void prepare(halp::setup info) { previous_values.resize(info.input_channels); } + + std::function send_message; + + void process_message(const ui_to_processor& msg) + { + fprintf(stderr, "Got message in processing thread !\n"); + send_message(processor_to_ui{.test_back = 1}); + } + + void operator()(halp::tick t) + { + // Process the input buffer + for (int i = 0; i < inputs.audio.channels; i++) + { + auto* in = inputs.audio[i]; + auto* out = outputs.audio[i]; + + float& prev = this->previous_values[i]; + + for (int j = 0; j < t.frames; j++) + { + out[j] = inputs.volume * in[j]; + prev = out[j]; + } + } + } + + private: + // Here we have some state which depends on the host configuration (number of channels, etc). + std::vector previous_values{}; }; }