|
| 1 | +use image::{self,ImageBuffer}; |
| 2 | +use draw::*; |
| 3 | + |
| 4 | + |
| 5 | +pubstructDrawImage{ |
| 6 | +context:ImageBuffer, |
| 7 | +screenspace:Range2d, |
| 8 | +realspace:Range2d, |
| 9 | +color:[u8;4], |
| 10 | +} |
| 11 | +#![allow(dead_code)] |
| 12 | +#![allow(unused_variables)] |
| 13 | + |
| 14 | +implDrawImage{ |
| 15 | +pubfnnew(sdlh:Sdl2Mt) ->Box<DrawSDL>{ |
| 16 | +let window_id = sdlh.create_simple_window("2D plot",720,720).unwrap(); |
| 17 | + |
| 18 | +let default_s =Range{min:0.0,max:0.0}; |
| 19 | + |
| 20 | +let default_r =Range{ |
| 21 | +min:0.0, |
| 22 | +max:720.0, |
| 23 | +}; |
| 24 | + |
| 25 | +Box::new(DrawSDL{ |
| 26 | + sdlh, |
| 27 | + window_id, |
| 28 | +screenspace:Range2d(default_s, default_s), |
| 29 | +realspace:Range2d(default_r, default_r), |
| 30 | +color: pixels::Color::RGBA(0,0,0,255), |
| 31 | +}) |
| 32 | +} |
| 33 | +} |
| 34 | + |
| 35 | +implDrawableforDrawImage{ |
| 36 | +/// Sets the visible range of worldspace |
| 37 | +fnset_view(&mutself,view:Range2d){ |
| 38 | +self.screenspace = view; |
| 39 | +} |
| 40 | + |
| 41 | +/// Gets the visible range of worldspace |
| 42 | +fnget_view(&self) ->Range2d{ |
| 43 | +self.screenspace |
| 44 | +} |
| 45 | + |
| 46 | +/// Set color for various drawing actions |
| 47 | +fnset_color(&mutself,color:[u8;4]){ |
| 48 | +self.color = pixels::Color::RGBA(color[0], color[1], color[2], color[3]); |
| 49 | +} |
| 50 | + |
| 51 | +/// Clears the output surface |
| 52 | +fnclear(&mutself){ |
| 53 | +let color =self.color; |
| 54 | +// self.sdlh |
| 55 | +// .run_on_ui_thread(Box::new(move |_sdl, windows| { |
| 56 | +// let canvas = windows.get_mut(&window_id).unwrap(); |
| 57 | +// canvas.set_draw_color(color); |
| 58 | +// canvas.clear(); |
| 59 | +// })) |
| 60 | +// .unwrap(); |
| 61 | +} |
| 62 | + |
| 63 | +/// Draws a line from (x, y) -> (x, y) in worldspace |
| 64 | +fnline(&mutself,p1:(f64,f64),p2:(f64,f64)){ |
| 65 | +self.thick_line(p1, p2,1); |
| 66 | +} |
| 67 | + |
| 68 | +/// Draws a line from (x, y) -> (x, y) in worldspace |
| 69 | +fnthick_line(&mutself,(x1, y1):(f64,f64),(x2, y2):(f64,f64),thickness:u16){ |
| 70 | + |
| 71 | +let x1 =point2window(x1,self.screenspace.0,self.realspace.0,false); |
| 72 | +let y1 =point2window(y1,self.screenspace.1,self.realspace.1,true); |
| 73 | + |
| 74 | +let x2 =point2window(x2,self.screenspace.0,self.realspace.0,false); |
| 75 | +let y2 =point2window(y2,self.screenspace.1,self.realspace.1,true); |
| 76 | + |
| 77 | +let color =self.color; |
| 78 | +// self.sdlh |
| 79 | +// .run_on_ui_thread(Box::new(move |_sdl, windows| { |
| 80 | +// let canvas = windows.get_mut(&window_id).unwrap(); |
| 81 | +// canvas.set_draw_color(color); |
| 82 | +// canvas |
| 83 | +// .draw_line((x1 as i32, y1 as i32), (x2 as i32, y2 as i32)) |
| 84 | +// .unwrap(); |
| 85 | +// })) |
| 86 | +// .unwrap(); |
| 87 | +} |
| 88 | + |
| 89 | +/// Draws a rectangle bounded by two corners |
| 90 | +fnrectangle(&mutself,(x1, y1):(f64,f64),(x2, y2):(f64,f64)){ |
| 91 | + |
| 92 | +let x1 =point2window(x1,self.screenspace.0,self.realspace.0,false); |
| 93 | +let y1 =point2window(y1,self.screenspace.1,self.realspace.1,false); |
| 94 | + |
| 95 | +let x2 =point2window(x2,self.screenspace.0,self.realspace.0,false); |
| 96 | +let y2 =point2window(y2,self.screenspace.1,self.realspace.1,false); |
| 97 | + |
| 98 | +let x1 = x1asi32; |
| 99 | +let y1 = y1asi32; |
| 100 | +let w =(x2asi32 - x1)asu32; |
| 101 | +let h =(y2asi32 - y1)asu32; |
| 102 | + |
| 103 | +let color =self.color; |
| 104 | +// self.sdlh |
| 105 | +// .run_on_ui_thread(Box::new(move |_sdl, windows| { |
| 106 | +// let canvas = windows.get_mut(&window_id).unwrap(); |
| 107 | +// canvas.set_draw_color(color); |
| 108 | +// canvas.fill_rect(Rect::new(x1, y1, w, h)).unwrap(); |
| 109 | +// })) |
| 110 | +// .unwrap(); |
| 111 | +} |
| 112 | + |
| 113 | +/// Draws a rectangle bounded by two corners |
| 114 | +fnunfilled_rectangle(&mutself,(x1, y1):(f64,f64),(x2, y2):(f64,f64)){ |
| 115 | + |
| 116 | +let x1 =point2window(x1,self.screenspace.0,self.realspace.0,false); |
| 117 | +let y1 =point2window(y1,self.screenspace.1,self.realspace.1,false); |
| 118 | + |
| 119 | +let x2 =point2window(x2,self.screenspace.0,self.realspace.0,false); |
| 120 | +let y2 =point2window(y2,self.screenspace.1,self.realspace.1,false); |
| 121 | + |
| 122 | +let x1 = x1asi32; |
| 123 | +let y1 = y1asi32; |
| 124 | +let w =(x2asi32 - x1)asu32; |
| 125 | +let h =(y2asi32 - y1)asu32; |
| 126 | + |
| 127 | +let color =self.color; |
| 128 | +// self.sdlh |
| 129 | +// .run_on_ui_thread(Box::new(move |_sdl, windows| { |
| 130 | +// let canvas = windows.get_mut(&window_id).unwrap(); |
| 131 | +// canvas.set_draw_color(color); |
| 132 | +// canvas.draw_rect(Rect::new(x1, y1, w, h)).unwrap(); |
| 133 | +// })) |
| 134 | +// .unwrap(); |
| 135 | +} |
| 136 | + |
| 137 | +fnpresent(&mutself){ |
| 138 | +// self.sdlh |
| 139 | +// .run_on_ui_thread(Box::new(move |_sdl, windows| { |
| 140 | +// let canvas = windows.get_mut(&window_id).unwrap(); |
| 141 | +// canvas.present(); |
| 142 | +// })) |
| 143 | +// .unwrap(); |
| 144 | +} |
| 145 | + |
| 146 | +/// Returns the next pending event |
| 147 | +fnget_events(&mutself) ->Vec<Event>{ |
| 148 | +vec![Event::Quit] |
| 149 | +} |
| 150 | +} |