Add shape helpers
This commit is contained in:
parent
144a441e2e
commit
118c73a030
|
@ -7,6 +7,7 @@ edition = "2021"
|
|||
crate-type = ["cdylib", "rlib"]
|
||||
|
||||
[dependencies]
|
||||
glam = "^0.21"
|
||||
wee_alloc = "^0.4"
|
||||
|
||||
[profile.release]
|
||||
|
|
80
src/lib.rs
80
src/lib.rs
|
@ -1,3 +1,5 @@
|
|||
use glam::Vec2;
|
||||
|
||||
#[global_allocator]
|
||||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
||||
|
||||
|
@ -30,8 +32,8 @@ macro_rules! handle_event_at {
|
|||
pub extern "C" fn $event(id: u32, x: f32, y: f32) {
|
||||
unsafe {
|
||||
let panel = &mut PANEL_IMPLS[id as usize];
|
||||
let point = Point { x, y };
|
||||
(&mut *panel).$event(point);
|
||||
let at = Vec2::new(x, y);
|
||||
(&mut *panel).$event(at);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -45,10 +47,10 @@ handle_event_at!(on_deselect);
|
|||
#[allow(unused)]
|
||||
pub trait PanelImpl {
|
||||
fn update(&mut self, dt: f32) {}
|
||||
fn on_hover(&mut self, at: Point) {}
|
||||
fn on_select(&mut self, at: Point) {}
|
||||
fn on_drag(&mut self, at: Point) {}
|
||||
fn on_deselect(&mut self, at: Point) {}
|
||||
fn on_hover(&mut self, at: Vec2) {}
|
||||
fn on_select(&mut self, at: Vec2) {}
|
||||
fn on_drag(&mut self, at: Vec2) {}
|
||||
fn on_deselect(&mut self, at: Vec2) {}
|
||||
}
|
||||
|
||||
pub struct DummyPanel {
|
||||
|
@ -63,10 +65,9 @@ impl DummyPanel {
|
|||
|
||||
impl PanelImpl for DummyPanel {
|
||||
fn update(&mut self, _dt: f32) {
|
||||
self.panel.draw_triangle(
|
||||
&Point { x: 0.0, y: 0.0 },
|
||||
&Point { x: 0.1, y: 0.0 },
|
||||
&Point { x: 0.0, y: 0.1 },
|
||||
self.panel.draw_rect(
|
||||
&Vec2 { x: 0.0, y: 0.0 },
|
||||
&Vec2 { x: 0.5, y: 0.5 },
|
||||
&Color {
|
||||
r: 1.0,
|
||||
g: 0.0,
|
||||
|
@ -99,13 +100,6 @@ extern "C" {
|
|||
);
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct Point {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct Color {
|
||||
|
@ -139,11 +133,61 @@ impl UiPanel {
|
|||
unsafe { UiPanel_setColor(self.0, color.r, color.g, color.b, color.a) }
|
||||
}
|
||||
|
||||
pub fn draw_triangle(&mut self, v1: &Point, v2: &Point, v3: &Point, color: &Color) {
|
||||
pub fn draw_triangle(&mut self, v1: &Vec2, v2: &Vec2, v3: &Vec2, color: &Color) {
|
||||
unsafe {
|
||||
UiPanel_drawTriangle(
|
||||
self.0, v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, color.r, color.g, color.b, color.a,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_circle(&mut self, center: &Vec2, radius: f32, color: &Color) {
|
||||
use std::f32::consts::PI;
|
||||
|
||||
let delta = PI / 16.0;
|
||||
let limit = PI * 2.0 + delta;
|
||||
|
||||
let mut last_spoke = Vec2::new(radius + center.x, center.y);
|
||||
let mut theta = delta;
|
||||
while theta < limit {
|
||||
let new_spoke = Vec2::from_angle(theta) * radius + *center;
|
||||
self.draw_triangle(¢er, &last_spoke, &new_spoke, &color);
|
||||
last_spoke = new_spoke;
|
||||
theta += delta;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_ring(&mut self, center: &Vec2, radius: f32, thickness: f32, color: &Color) {
|
||||
use std::f32::consts::PI;
|
||||
|
||||
let delta = PI / 64.0;
|
||||
let limit = PI * 2.0 + delta;
|
||||
|
||||
let mut last_spoke = glam::Vec2::new(radius + center.x, center.y);
|
||||
let mut last_theta = 0.0;
|
||||
let mut theta = delta;
|
||||
while theta < limit {
|
||||
let angle = Vec2::from_angle(theta);
|
||||
let new_spoke = angle * radius + *center;
|
||||
let new_spoke2 = angle * (radius + thickness) + *center;
|
||||
let last_spoke2 = Vec2::from_angle(last_theta) * (radius + thickness) + *center;
|
||||
|
||||
self.draw_triangle(&new_spoke2, &last_spoke, &new_spoke, &color);
|
||||
self.draw_triangle(&new_spoke2, &last_spoke2, &last_spoke, &color);
|
||||
|
||||
last_spoke = new_spoke;
|
||||
last_theta = theta;
|
||||
theta += delta;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_rect(&mut self, xy: &Vec2, size: &Vec2, color: &Color) {
|
||||
let v1 = *xy;
|
||||
let v2 = v1 + Vec2::new(size.x, 0.0);
|
||||
let v3 = v1 + Vec2::new(0.0, size.y);
|
||||
let v4 = v1 + *size;
|
||||
|
||||
self.draw_triangle(&v1, &v2, &v3, &color);
|
||||
self.draw_triangle(&v2, &v3, &v4, &color);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue