Skip to main content

box2d_rs/
b2_draw.rs

1use crate::b2_math::{B2Transform, B2vec2};
2
3use std::cell::RefCell;
4use std::rc::Rc;
5use bitflags::bitflags;
6
7/// Color for debug drawing. Each value has the range [0,1].
8#[derive(Copy, Clone, Debug)]
9pub struct B2color {
10    pub r: f32,
11    pub g: f32,
12    pub b: f32,
13    pub a: f32,
14}
15impl B2color {
16    pub fn new(r_in: f32, g_in: f32, b_in: f32) -> B2color {
17        return B2color {
18            r: r_in,
19            g: g_in,
20            b: b_in,
21            a: 1.0,
22        };
23    }
24
25    pub fn new_with_alpha(r_in: f32, g_in: f32, b_in: f32, a_in: f32) -> B2color {
26        return B2color {
27            r: r_in,
28            g: g_in,
29            b: b_in,
30            a: a_in,
31        };
32    }
33
34    pub fn set(&mut self, r_in: f32, g_in: f32, b_in: f32) {
35        self.r = r_in;
36        self.g = g_in;
37        self.b = b_in;
38    }
39
40    pub fn set_with_alpha(&mut self, r_in: f32, g_in: f32, b_in: f32, a_in: f32) {
41        self.r = r_in;
42        self.g = g_in;
43        self.b = b_in;
44        self.a = a_in;
45    }
46}
47
48#[derive(Debug, Clone, Copy, Default)]
49pub struct B2draw {
50    m_draw_flags: B2drawShapeFlags,
51}
52
53bitflags! {
54    #[derive(Debug, Clone, Copy, Default)]
55    pub struct B2drawShapeFlags: u16 {
56    /// draw shapes
57    const SHAPE_BIT = 0x0001;
58    /// draw joint connections
59    const JOINT_BIT = 0x0002;
60    /// draw axis aligned bounding boxes
61    const AABB_BIT = 0x0004;
62    /// draw broad-phase pairs
63    const PAIR_BIT = 0x0008;
64    /// draw center of mass frame
65    const CENTER_OF_MASS_BIT = 0x0010;
66}
67}
68
69impl B2draw {
70    /// Set the drawing flags.
71    pub fn set_flags(&mut self, flags: B2drawShapeFlags) {
72        self.m_draw_flags = flags;
73    }
74
75    /// Get the drawing flags.
76    pub fn get_flags(self) -> B2drawShapeFlags {
77        return self.m_draw_flags;
78    }
79
80    /// Append flags to the current flags.
81    pub fn append_flags(&mut self, flags: B2drawShapeFlags) {
82        self.m_draw_flags.insert(flags);
83    }
84
85    /// clear flags from the current flags.
86    pub fn clear_flags(&mut self, flags: B2drawShapeFlags) {
87        self.m_draw_flags.remove(flags);
88    }
89}
90
91pub type B2drawTraitPtr = Rc<RefCell<dyn B2drawTrait>>;
92
93pub trait B2drawTrait {
94    fn get_base(&self) -> &B2draw;
95    fn get_base_mut(&mut self) -> &mut B2draw;
96    /// draw a closed polygon provided in CCW order.
97    fn draw_polygon(&mut self, vertices: &[B2vec2], color: B2color);
98
99    /// draw a solid closed polygon provided in CCW order.
100    fn draw_solid_polygon(&mut self, vertices: &[B2vec2], color: B2color);
101
102    /// draw a circle.
103    fn draw_circle(&mut self, center: B2vec2, radius: f32, color: B2color);
104
105    /// draw a solid circle.
106    fn draw_solid_circle(&mut self, center: B2vec2, radius: f32, axis: B2vec2, color: B2color);
107
108    /// draw a line segment.
109    fn draw_segment(&mut self, p1: B2vec2, p2: B2vec2, color: B2color);
110
111    /// draw a transform. Choose your own length scale.
112    /// * `xf` - a transform.
113    fn draw_transform(&mut self, xf: B2Transform);
114
115    /// draw a point.
116    fn draw_point(&mut self, p: B2vec2, size: f32, color: B2color);
117}