use nalgebra_glm::{Vec2, Vec4};
use crate::ecs::ui::components::*;
impl crate::ecs::world::World {
pub fn ui_canvas_hit_test(&self, entity: freecs::Entity, position: Vec2) -> Option<u32> {
if let Some(UiWidgetState::Canvas(data)) = self.ui.get_ui_widget_state(entity) {
for &(command_id, ref rect) in data.command_bounds.iter().rev() {
if rect.contains(position) {
return Some(command_id);
}
}
}
None
}
pub fn ui_canvas_clear(&mut self, entity: freecs::Entity) {
if let Some(UiWidgetState::Canvas(data)) = self.ui.get_ui_widget_state_mut(entity) {
data.commands.clear();
}
}
pub fn ui_canvas_rect(
&mut self,
entity: freecs::Entity,
position: Vec2,
size: Vec2,
color: Vec4,
corner_radius: f32,
) {
if let Some(UiWidgetState::Canvas(data)) = self.ui.get_ui_widget_state_mut(entity) {
data.commands.push(CanvasCommand::Rect {
position,
size,
color,
corner_radius,
id: None,
});
}
}
pub fn ui_canvas_circle(
&mut self,
entity: freecs::Entity,
center: Vec2,
radius: f32,
color: Vec4,
) {
if let Some(UiWidgetState::Canvas(data)) = self.ui.get_ui_widget_state_mut(entity) {
data.commands.push(CanvasCommand::Circle {
center,
radius,
color,
id: None,
});
}
}
pub fn ui_canvas_line(
&mut self,
entity: freecs::Entity,
from: Vec2,
to: Vec2,
thickness: f32,
color: Vec4,
) {
if let Some(UiWidgetState::Canvas(data)) = self.ui.get_ui_widget_state_mut(entity) {
data.commands.push(CanvasCommand::Line {
from,
to,
thickness,
color,
id: None,
});
}
}
pub fn ui_canvas_text(
&mut self,
entity: freecs::Entity,
text: &str,
position: Vec2,
font_size: f32,
color: Vec4,
) {
if let Some(UiWidgetState::Canvas(data)) = self.ui.get_ui_widget_state_mut(entity) {
data.commands.push(CanvasCommand::Text {
text: text.to_string(),
position,
font_size,
color,
id: None,
});
}
}
pub fn ui_canvas_polyline(
&mut self,
entity: freecs::Entity,
points: &[Vec2],
thickness: f32,
color: Vec4,
closed: bool,
) {
if let Some(UiWidgetState::Canvas(data)) = self.ui.get_ui_widget_state_mut(entity) {
data.commands.push(CanvasCommand::Polyline {
points: points.to_vec(),
thickness,
color,
closed,
id: None,
});
}
}
pub fn ui_canvas_rect_stroke(
&mut self,
entity: freecs::Entity,
position: Vec2,
size: Vec2,
color: Vec4,
thickness: f32,
corner_radius: f32,
) {
if let Some(UiWidgetState::Canvas(data)) = self.ui.get_ui_widget_state_mut(entity) {
data.commands.push(CanvasCommand::RectStroke {
position,
size,
color,
thickness,
corner_radius,
id: None,
});
}
}
pub fn ui_canvas_circle_stroke(
&mut self,
entity: freecs::Entity,
center: Vec2,
radius: f32,
color: Vec4,
thickness: f32,
) {
if let Some(UiWidgetState::Canvas(data)) = self.ui.get_ui_widget_state_mut(entity) {
data.commands.push(CanvasCommand::CircleStroke {
center,
radius,
color,
thickness,
id: None,
});
}
}
pub fn ui_canvas_arc(
&mut self,
entity: freecs::Entity,
center: Vec2,
radius: f32,
angle_range: (f32, f32),
thickness: f32,
color: Vec4,
) {
if let Some(UiWidgetState::Canvas(data)) = self.ui.get_ui_widget_state_mut(entity) {
data.commands.push(CanvasCommand::Arc {
center,
radius,
start_angle: angle_range.0,
end_angle: angle_range.1,
thickness,
color,
id: None,
});
}
}
pub fn ui_canvas_quadratic_bezier(
&mut self,
entity: freecs::Entity,
start: Vec2,
control: Vec2,
end: Vec2,
thickness: f32,
color: Vec4,
) {
if let Some(UiWidgetState::Canvas(data)) = self.ui.get_ui_widget_state_mut(entity) {
data.commands.push(CanvasCommand::QuadraticBezier {
start,
control,
end,
thickness,
color,
id: None,
});
}
}
pub fn ui_canvas_cubic_bezier(
&mut self,
entity: freecs::Entity,
start: Vec2,
controls: (Vec2, Vec2),
end: Vec2,
thickness: f32,
color: Vec4,
) {
if let Some(UiWidgetState::Canvas(data)) = self.ui.get_ui_widget_state_mut(entity) {
data.commands.push(CanvasCommand::CubicBezier {
start,
control1: controls.0,
control2: controls.1,
end,
thickness,
color,
id: None,
});
}
}
}