copper_rs/gfx/
surface.rs

1
2use wasm_bindgen::prelude::*;
3use crate::gfx::TextAlign;
4
5#[wasm_bindgen]
6extern {
7    fn copperCreateCanvasSurface(element_id: &str) -> usize;
8    fn copperCreateSizedSurface(width: usize, height: usize) -> usize;
9    fn copperCreateImageSurface(image_url: &str) -> usize;
10
11    fn copperSurfaceLoaded(pointer: usize) -> bool;
12    fn copperSurfaceWidth(pointer: usize) -> usize;
13    fn copperSurfaceHeight(pointer: usize) -> usize;
14    fn copperSurfaceGetPixelR(pointer: usize, x: usize, y: usize) -> u8;
15    fn copperSurfaceGetPixelG(pointer: usize, x: usize, y: usize) -> u8;
16    fn copperSurfaceGetPixelB(pointer: usize, x: usize, y: usize) -> u8;
17    fn copperSurfaceGetPixelA(pointer: usize, x: usize, y: usize) -> u8;
18    fn copperSurfaceResize(pointer: usize, width: usize, height: usize) -> usize;
19
20    fn copperSurfaceClearMode(point: usize);
21    fn copperSurfaceDrawMode(point: usize);
22    fn copperSurfaceDrawSubSurfaceRotated(pointer: usize, src_pointer: usize, a: u8, sx: usize, sy: usize, sw: usize, sh: usize, x: f32, y: f32, w: f32, h: f32, rx: f32, ry: f32, ra: f32);
23    fn copperSurfaceDrawRectRotated(pointer: usize, r: u8, g: u8, b: u8, a: u8, x: f32, y: f32, w: f32, h: f32, rx: f32, ry: f32, ra: f32);
24    fn copperSurfaceDrawOvalRotated(pointer: usize, r: u8, g: u8, b: u8, a: u8, x: f32, y: f32, w: f32, h: f32, rx: f32, ry: f32, ra: f32);
25    fn copperSurfaceDrawLine(pointer: usize, r: u8, g: u8, b: u8, a: u8, x1: f32, y1: f32, x2: f32, y2: f32, w: f32);
26    fn copperSurfaceDrawQuadraticBezier(pointer: usize, r: u8, g: u8, b: u8, a: u8, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, w: f32);
27    fn copperSurfaceDrawCubicBezier(pointer: usize, r: u8, g: u8, b: u8, a: u8, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, x4: f32, y4: f32, w: f32);
28    fn copperSurfaceDrawTextRotated(pointer: usize, text: &str, font: &str, r: u8, g: u8, b: u8, a: u8, x: f32, y: f32, align: &str, h: f32, rx: f32, ry: f32, ra: f32);
29
30    fn copperDropSurface(pointer: usize);
31}
32
33fn get_text_align_js(align: TextAlign) -> &'static str {
34    match align {
35        TextAlign::Left => "right",
36        TextAlign::Center => "center",
37        TextAlign::Right => "left",
38    }
39}
40
41pub struct Surface {
42    pointer: usize
43}
44
45impl Surface {
46    pub fn from_canvas(element_id: &str) -> Surface { Surface { pointer: copperCreateCanvasSurface(element_id) } }
47    pub fn from_size(width: usize, height: usize) -> Surface { Surface { pointer: copperCreateSizedSurface(width, height) } }
48    pub fn from_image(image_url: &str) -> Surface { Surface { pointer: copperCreateImageSurface(image_url) } }
49
50    pub fn loaded(&self) -> bool { copperSurfaceLoaded(self.pointer) }
51    pub fn width(&self) -> usize { copperSurfaceWidth(self.pointer) }
52    pub fn height(&self) -> usize { copperSurfaceHeight(self.pointer) }
53    pub fn get_pixel(&self, x: usize, y: usize) -> [u8; 4] {
54        [
55            copperSurfaceGetPixelR(self.pointer, x, y),
56            copperSurfaceGetPixelG(self.pointer, x, y),
57            copperSurfaceGetPixelB(self.pointer, x, y),
58            copperSurfaceGetPixelA(self.pointer, x, y)
59        ]
60    }
61    pub fn resize(&self, width: usize, height: usize) -> Surface { Surface { pointer: copperSurfaceResize(self.pointer, width, height) } }
62
63    pub fn draw_surface(&self, src: &Surface, alpha: u8, x: f32, y: f32, w: f32, h: f32) {
64        copperSurfaceDrawSubSurfaceRotated(self.pointer, src.pointer, alpha, 0, 0, src.width(), src.height(), x, y, w, h, 0f32, 0f32, 0f32);
65    }
66    pub fn draw_surface_rotated(&self, src: &Surface, alpha: u8, x: f32, y: f32, w: f32, h: f32, rot_x: f32, rot_y: f32, rot_angle: f32) {
67        copperSurfaceDrawSubSurfaceRotated(self.pointer, src.pointer, alpha, 0, 0, src.width(), src.height(), x, y, w, h, rot_x, rot_y, rot_angle);
68    }
69    pub fn draw_subsurface(&self, src: &Surface, alpha: u8, src_x: usize, src_y: usize, src_w: usize, src_h: usize, x: f32, y: f32, w: f32, h: f32) {
70        copperSurfaceDrawSubSurfaceRotated(self.pointer, src.pointer, alpha, src_x, src_y, src_w, src_h, x, y, w, h, 0f32, 0f32, 0f32);
71    }
72    pub fn draw_subsurface_rotated(&self, src: &Surface, alpha: u8, src_x: usize, src_y: usize, src_w: usize, src_h: usize, x: f32, y: f32, w: f32, h: f32, rot_x: f32, rot_y: f32, rot_angle: f32) {
73        copperSurfaceDrawSubSurfaceRotated(self.pointer, src.pointer, alpha, src_x, src_y, src_w, src_h, x, y, w, h, rot_x, rot_y, rot_angle);
74    }
75
76    pub fn draw_rect(&self, red: u8, green: u8, blue: u8, alpha: u8, x: f32, y: f32, w: f32, h: f32) {
77        copperSurfaceDrawRectRotated(self.pointer, red, green, blue, alpha, x, y, w, h, 0f32, 0f32, 0f32);
78    }
79    pub fn draw_rect_rotated(&self, red: u8, green: u8, blue: u8, alpha: u8, x: f32, y: f32, w: f32, h: f32, rot_x: f32, rot_y: f32, rot_angle: f32) {
80        copperSurfaceDrawRectRotated(self.pointer, red, green, blue, alpha, x, y, w, h, rot_x, rot_y, rot_angle);
81    }
82    pub fn clear_rect(&self, x: f32, y: f32, w: f32, h: f32) {
83        copperSurfaceClearMode(self.pointer);
84        self.draw_rect(0, 0, 0, 255, x, y, w, h);
85        copperSurfaceDrawMode(self.pointer);
86    }
87    pub fn clear_rect_rotated(&self, x: f32, y: f32, w: f32, h: f32, rot_x: f32, rot_y: f32, rot_angle: f32) {
88        copperSurfaceClearMode(self.pointer);
89        self.draw_rect_rotated(0, 0, 0, 255, x, y, w, h, rot_x, rot_y, rot_angle);
90        copperSurfaceDrawMode(self.pointer);
91    }
92
93    pub fn draw_oval(&self, red: u8, green: u8, blue: u8, alpha: u8, x: f32, y: f32, w: f32, h: f32) {
94        copperSurfaceDrawOvalRotated(self.pointer, red, green, blue, alpha, x, y, w, h, 0f32, 0f32, 0f32);
95    }
96    pub fn draw_oval_rotated(&self, red: u8, green: u8, blue: u8, alpha: u8, x: f32, y: f32, w: f32, h: f32, rot_x: f32, rot_y: f32, rot_angle: f32) {
97        copperSurfaceDrawOvalRotated(self.pointer, red, green, blue, alpha, x, y, w, h, rot_x, rot_y, rot_angle);
98    }
99    pub fn clear_oval(&self, x: f32, y: f32, w: f32, h: f32) {
100        copperSurfaceClearMode(self.pointer);
101        self.draw_oval(0, 0, 0, 255, x, y, w, h);
102        copperSurfaceDrawMode(self.pointer);
103    }
104    pub fn clear_oval_rotated(&self, x: f32, y: f32, w: f32, h: f32, rot_x: f32, rot_y: f32, rot_angle: f32) {
105        copperSurfaceClearMode(self.pointer);
106        self.draw_oval_rotated(0, 0, 0, 255, x, y, w, h, rot_x, rot_y, rot_angle);
107        copperSurfaceDrawMode(self.pointer);
108    }
109
110    pub fn draw_line(&self, red: u8, green: u8, blue: u8, alpha: u8, x1: f32, y1: f32, x2: f32, y2: f32, w: f32) {
111        copperSurfaceDrawLine(self.pointer, red, green, blue, alpha, x1, y1, x2, y2, w);
112    }
113    pub fn clear_line(&self, x1: f32, y1: f32, x2: f32, y2: f32, w: f32) {
114        copperSurfaceClearMode(self.pointer);
115        self.draw_line(0, 0, 0, 255, x1, y1, x2, y2, w);
116        copperSurfaceDrawMode(self.pointer);
117    }
118
119    pub fn draw_quadratic_bezier(&self, red: u8, green: u8, blue: u8, alpha: u8, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, w: f32) {
120        copperSurfaceDrawQuadraticBezier(self.pointer, red, green, blue, alpha, x1, y1, x2, y2, x3, y3, w);
121    }
122    pub fn clear_quadratic_bezier(&self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, w: f32) {
123        copperSurfaceClearMode(self.pointer);
124        self.draw_quadratic_bezier(0, 0, 0, 255, x1, y1, x2, y2, x3, y3, w);
125        copperSurfaceDrawMode(self.pointer);
126    }
127
128    pub fn draw_cubic_bezier(&self, red: u8, green: u8, blue: u8, alpha: u8, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, x4: f32, y4: f32, w: f32) {
129        copperSurfaceDrawCubicBezier(self.pointer, red, green, blue, alpha, x1, y1, x2, y2, x3, y3, x4, y4, w);
130    }
131    pub fn clear_cubic_bezier(&self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, x4: f32, y4: f32, w: f32) {
132        copperSurfaceClearMode(self.pointer);
133        self.draw_cubic_bezier(0, 0, 0, 255, x1, y1, x2, y2, x3, y3, x4, y4, w);
134        copperSurfaceDrawMode(self.pointer);
135    }
136
137    pub fn draw_text(&self, text: &str, font: &str, red: u8, green: u8, blue: u8, alpha: u8, x: f32, y: f32, align: TextAlign, h: f32) {
138        copperSurfaceDrawTextRotated(self.pointer, text, font, red, green, blue, alpha, x, y, get_text_align_js(align), h, 0.0, 0.0, 0.0);
139    }
140    pub fn clear_text(&self, text: &str, font: &str, x: f32, y: f32, align: TextAlign, h: f32) {
141        copperSurfaceClearMode(self.pointer);
142        self.draw_text(text, font, 0, 0, 0, 255, x, y, align, h);
143        copperSurfaceDrawMode(self.pointer);
144    }
145    pub fn draw_text_rotated(&self, text: &str, font: &str, red: u8, green: u8, blue: u8, alpha: u8, x: f32, y: f32, align: TextAlign, h: f32, rot_x: f32, rot_y: f32, rot_angle: f32) {
146        copperSurfaceDrawTextRotated(self.pointer, text, font, red, green, blue, alpha, x, y, get_text_align_js(align), h, rot_x, rot_y, rot_angle);
147    }
148    pub fn clear_text_rotated(&self, text: &str, font: &str, x: f32, y: f32, align: TextAlign, h: f32, rot_x: f32, rot_y: f32, rot_angle: f32) {
149        copperSurfaceClearMode(self.pointer);
150        self.draw_text_rotated(text, font, 0, 0, 0, 255, x, y, align, h, rot_x, rot_y, rot_angle);
151        copperSurfaceDrawMode(self.pointer);
152    }
153
154    pub fn fill(&self, red: u8, green: u8, blue: u8, alpha: u8) {
155        self.draw_rect(red, green, blue, alpha, 0.0, 0.0, self.width() as f32, self.height() as f32);
156    }
157}
158
159impl Drop for Surface {
160    fn drop(&mut self) {
161        copperDropSurface(self.pointer);
162    }
163}