-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdraw_sdl.rs
More file actions
214 lines (174 loc) · 6.67 KB
/
draw_sdl.rs
File metadata and controls
214 lines (174 loc) · 6.67 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#![allow(dead_code)]
#![allow(unused_variables)]
use sdl2_mt;
use sdl2_mt::event::Event as SdlEvent;
use sdl2_mt::pixels;
use sdl2_mt::Sdl2Mt;
use draw::*;
/// Provides an SDL2-based interactive plotting backend
pub struct DrawSDL {
sdlh: Sdl2Mt,
window_id: u32,
screenspace: Range2d,
realspace: Range2d,
color: pixels::Color,
}
impl DrawSDL {
pub fn new(sdlh: Sdl2Mt) -> Box<DrawSDL> {
let window_id = sdlh.create_simple_window("2D plot", 720, 720).unwrap();
let default_s = Range { min: 0.0, max: 0.0 };
let default_r = Range {
min: 0.0,
max: 720.0,
};
Box::new(DrawSDL {
sdlh,
window_id,
screenspace: Range2d(default_s, default_s),
realspace: Range2d(default_r, default_r),
color: pixels::Color::RGBA(0, 0, 0, 255),
})
}
}
impl Drawable for DrawSDL {
/// Sets the visible range of worldspace
fn set_view(&mut self, view: Range2d) {
self.screenspace = view;
}
/// Gets the visible range of worldspace
fn get_view(&self) -> Range2d {
self.screenspace
}
/// Set color for various drawing actions
fn set_color(&mut self, color: [u8; 4]) {
self.color = pixels::Color::RGBA(color[0], color[1], color[2], color[3]);
}
/// Clears the output surface
fn clear(&mut self) {
let window_id = self.window_id;
let color = self.color;
self.sdlh
.run_on_ui_thread(Box::new(move |_sdl, windows| {
let canvas = windows.get_mut(&window_id).unwrap();
canvas.set_draw_color(color);
canvas.clear();
}))
.unwrap();
}
/// Draws a line from (x, y) -> (x, y) in worldspace
fn line(&mut self, p1: (f64, f64), p2: (f64, f64)) {
self.thick_line(p1, p2, 1);
}
/// Draws a line from (x, y) -> (x, y) in worldspace
fn thick_line(&mut self, (x1, y1): (f64, f64), (x2, y2): (f64, f64), thickness: u16) {
let x1 = point2window(x1, self.screenspace.0, self.realspace.0, false);
let y1 = point2window(y1, self.screenspace.1, self.realspace.1, true);
let x2 = point2window(x2, self.screenspace.0, self.realspace.0, false);
let y2 = point2window(y2, self.screenspace.1, self.realspace.1, true);
let window_id = self.window_id;
let color = self.color;
self.sdlh
.run_on_ui_thread(Box::new(move |_sdl, windows| {
let canvas = windows.get_mut(&window_id).unwrap();
canvas.set_draw_color(color);
canvas
.draw_line((x1 as i32, y1 as i32), (x2 as i32, y2 as i32))
.unwrap();
}))
.unwrap();
}
/// Draws a rectangle bounded by two corners
fn rectangle(&mut self, (x1, y1): (f64, f64), (x2, y2): (f64, f64)) {
let x1 = point2window(x1, self.screenspace.0, self.realspace.0, false);
let y1 = point2window(y1, self.screenspace.1, self.realspace.1, false);
let x2 = point2window(x2, self.screenspace.0, self.realspace.0, false);
let y2 = point2window(y2, self.screenspace.1, self.realspace.1, false);
let x1 = x1 as i32;
let y1 = y1 as i32;
let w = (x2 as i32 - x1) as u32;
let h = (y2 as i32 - y1) as u32;
use sdl2_mt::rect::Rect;
let window_id = self.window_id;
let color = self.color;
self.sdlh
.run_on_ui_thread(Box::new(move |_sdl, windows| {
let canvas = windows.get_mut(&window_id).unwrap();
canvas.set_draw_color(color);
canvas.fill_rect(Rect::new(x1, y1, w, h)).unwrap();
}))
.unwrap();
}
/// Draws a rectangle bounded by two corners
fn unfilled_rectangle(&mut self, (x1, y1): (f64, f64), (x2, y2): (f64, f64)) {
let x1 = point2window(x1, self.screenspace.0, self.realspace.0, false);
let y1 = point2window(y1, self.screenspace.1, self.realspace.1, false);
let x2 = point2window(x2, self.screenspace.0, self.realspace.0, false);
let y2 = point2window(y2, self.screenspace.1, self.realspace.1, false);
let x1 = x1 as i32;
let y1 = y1 as i32;
let w = (x2 as i32 - x1) as u32;
let h = (y2 as i32 - y1) as u32;
use sdl2_mt::rect::Rect;
let window_id = self.window_id;
let color = self.color;
self.sdlh
.run_on_ui_thread(Box::new(move |_sdl, windows| {
let canvas = windows.get_mut(&window_id).unwrap();
canvas.set_draw_color(color);
canvas.draw_rect(Rect::new(x1, y1, w, h)).unwrap();
}))
.unwrap();
}
fn present(&mut self) {
let window_id = self.window_id;
self.sdlh
.run_on_ui_thread(Box::new(move |_sdl, windows| {
let canvas = windows.get_mut(&window_id).unwrap();
canvas.present();
}))
.unwrap();
}
/// Returns the next pending event
fn get_events(&mut self) -> Vec<Event> {
use std::sync::mpsc::channel;
let (tx, rx) = channel();
let window_id = self.window_id;
self.sdlh
.handle_ui_events(Box::new(move |_sdl, _windows, event| {
match event {
&SdlEvent::Quit { .. } => tx.send(Event::Quit).unwrap(),
&SdlEvent::MouseWheel { x, y, .. } => {
tx.send(Event::MouseScroll(x, y)).unwrap();
}
&SdlEvent::KeyDown {
window_id,
keycode: Some(keycode),
..
} => {
tx.send(Event::KeyDown(keycode as i32)).unwrap();
}
&SdlEvent::Window {
window_id,
win_event: sdl2_mt::event::WindowEvent::Resized(new_w, new_h),
..
} => {
tx.send(Event::Resize(new_w as f64, new_h as f64)).unwrap();
}
_ => return false,
}
true
}))
.unwrap();
let events: Vec<_> = rx.iter().collect();
// let's find the last window resize event and save its values
if let Some(&Event::Resize(w, h)) =
events.iter().rev().find(|&event| match event {
&Event::Resize(_, _) => true,
_ => false,
})
{
self.realspace = Range2d(Range { min: 0.0, max: w }, Range { min: 0.0, max: h });
}
events
}
}