canvas.rs
3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// …
//
// Georg Hopp <georg@steffers.org>
//
// Copyright © 2019 Georg Hopp
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
use std::fmt::{Debug, Display, Formatter, Result};
use std::ops::{Add, Div, Sub};
use std::sync::mpsc;
use super::drawable::Drawable;
use super::line_iterator::LineIterator;
// A 2D drawing surface.
pub trait Canvas<T> {
fn init_events(&self);
fn start_events(&self, tx :mpsc::Sender<i32>);
fn width(&self) -> u16;
fn height(&self) -> u16;
fn clear(&mut self);
fn draw(&mut self, c :&dyn Drawable<T>, color :u32);
fn set_pixel(&mut self, c :Vertex<T>, color :u32);
fn put_text(&self, ofs :Vertex<T>, s :&str);
fn show(&self);
}
// A Vertex is a position on a 2D drawing surface along other stuff
// that needs to iterate between those coordinates.
#[derive(Debug, Clone, Copy)]
pub struct Vertex<T>{ x :i32 // canvas x coordinate
, y :i32 // canvas y coordinate
, zr :T } // z reciprocal from 3D projection.
pub type Vertices<T> = Vec<Vertex<T>>;
impl<T> Vertex<T>
where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
+ Debug + Clone + Copy + From<i32> {
pub fn new(x :i32, y :i32, zr :T) -> Self {
Vertex{x, y, zr}
}
#[inline]
pub fn as_tuple(&self) -> (i32, i32, T) {
(self.x, self.y, self.zr)
}
#[inline]
pub fn same_x(&self, b :&Self) -> bool {
self.x == b.x
}
#[inline]
pub fn same_y(&self, b :&Self) -> bool {
self.y == b.y
}
#[inline]
pub fn same_position(&self, b :&Self) -> bool {
self.same_x(b) && self.same_y(b)
}
fn iter(self, b :Self, only_edges :bool) -> LineIterator<T> {
LineIterator::new(self, b, only_edges)
}
pub fn line_iter(self, b :Self) -> LineIterator<T> {
self.iter(b, false)
}
pub fn line(self, b :Self) -> Vec<Self> {
self.line_iter(b).collect()
}
pub fn edge_iter(self, b :Self) -> LineIterator<T> {
self.iter(b, true)
}
fn edge(self, b :Self) -> Vec<Self> {
self.edge_iter(b).collect()
}
fn face(edges :&[Self]) -> Vec<Self> {
edges.to_vec()
}
}
impl<T> Display for Vertex<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "<{},{}>", self.x, self.y)
}
}
impl<T> Add for Vertex<T> where T: Add<Output=T> {
type Output = Self;
fn add(self, other :Self) -> Vertex<T> {
Vertex{ x: self.x + other.x
, y: self.y + other.y
, zr: self.zr + other.zr }
}
}