Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Pull Request #29

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions examples/Helpers/ImageUi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down Expand Up @@ -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;
Expand All @@ -129,7 +183,7 @@ struct AdvancedUi
halp_meta(height, 200)
halp_meta(font, "Inconsolata")

halp::custom_item<custom_slider, &ins::float_ctl> widget{{.x = 190, .y = 170}};
halp::custom_item<custom_slider, &ins::float_ctl> widget{{.x = 500, .y = 920}};
halp::custom_item_base<custom_anim> anim{.x = 90, .y = -50};
};
};
Expand Down
301 changes: 301 additions & 0 deletions examples/Helpers/MyMosca.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
#pragma once

/* SPDX-License-Identifier: GPL-3.0-or-later */

#include <avnd/concepts/processor.hpp>
#include <avnd/concepts/painter.hpp>
#include <avnd/wrappers/controls.hpp>
#include <halp/custom_widgets.hpp>
#include <halp/audio.hpp>
#include <halp/controls.hpp>
#include <halp/layout.hpp>
#include <halp/meta.hpp>
#include <cmath>

#include <cstdio>

//Modif
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdio.h>

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"; }

using setup = halp::setup;
using tick = halp::tick;

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<float> value)
{
this->value = avnd::map_control_to_01(control, value);
}

static auto value_to_control(auto& control, halp::xy_type<float> 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<float> 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<halp::xy_type<float>> transaction;
halp::xy_type<float> 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<void()> on_pressed = [] { };
};

struct ins
{
halp::dynamic_audio_bus<"Input", double> audio;

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;
} 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, 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<void(ui_to_processor)> send_message;

};

struct {
halp_meta(layout, halp::layouts::container)
halp::custom_actions_item<custom_button> 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<custom_mosca, &ins::level> 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::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<void(processor_to_ui)> 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<float> previous_values{};
};
}
Loading