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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
use gfx; use glutin; use gfx_device_gl; use gfx_window_glutin; use gfx_glyph; use gfx::Device; use glutin::{GlContext, Event}; use gfx_glyph::GlyphCruncher; use prototty_grid::*; use prototty::*; use cell::*; use formats::*; use background::*; use input::*; type Resources = gfx_device_gl::Resources; const UNDERLINE_WIDTH_RATIO: u32 = 15; const UNDERLINE_POSITION_RATIO: u32 = 15; const FONT_SCALE: gfx_glyph::Scale = gfx_glyph::Scale { x: 16.0, y: 16.0 }; const FONT_ID: gfx_glyph::FontId = gfx_glyph::FontId(0); const BOLD_FONT_ID: gfx_glyph::FontId = gfx_glyph::FontId(1); pub const MAX_WIDTH_IN_CELLS: u32 = 256; pub const MAX_HEIGHT_IN_CELLS: u32 = 256; #[derive(Debug)] pub enum Error { GfxGlyph(String), GlutinContextError(glutin::ContextError), FailedToMeasureFont, } pub type Result<T> = ::std::result::Result<T, Error>; pub struct ContextBuilder<'a> { font: &'a [u8], bold_font: Option<&'a [u8]>, font_scale: gfx_glyph::Scale, bold_font_scale: Option<gfx_glyph::Scale>, window_builder: glutin::WindowBuilder, context_builder: glutin::ContextBuilder<'a>, cell_dimensions: Option<Size>, underline_width: Option<u32>, underline_position: Option<u32>, max_grid_size: Option<Size>, } impl<'a> ContextBuilder<'a> { pub fn new_with_font(font: &'a [u8]) -> Self { Self { font, bold_font: None, font_scale: FONT_SCALE, bold_font_scale: None, window_builder: glutin::WindowBuilder::new(), context_builder: glutin::ContextBuilder::new(), cell_dimensions: None, underline_width: None, underline_position: None, max_grid_size: None, } } pub fn with_cell_dimensions(self, width: u32, height: u32) -> Self { Self { cell_dimensions: Some(Size::new(width, height)), ..self } } pub fn with_window_dimensions(self, width: u32, height: u32) -> Self { Self { window_builder: self.window_builder.with_dimensions(width, height), ..self } } pub fn with_vsync(self, vsync: bool) -> Self { Self { context_builder: self.context_builder.with_vsync(vsync), ..self } } pub fn with_underline_width(self, underline_width: u32) -> Self { Self { underline_width: Some(underline_width), ..self } } pub fn with_underline_position(self, underline_position: u32) -> Self { Self { underline_position: Some(underline_position), ..self } } pub fn with_bold_font(self, bold_font: &'a [u8]) -> Self { Self { bold_font: Some(bold_font), ..self } } pub fn with_font_scale(self, x: f32, y: f32) -> Self { Self { font_scale: gfx_glyph::Scale { x, y }, ..self } } pub fn with_bold_font_scale(self, x: f32, y: f32) -> Self { Self { bold_font_scale: Some(gfx_glyph::Scale { x, y }), ..self } } pub fn with_max_grid_size(self, width: u32, height: u32) -> Self { Self { max_grid_size: Some(Size::new(width, height)), ..self } } pub fn build(self) -> Result<Context<'a>> { let events_loop = glutin::EventsLoop::new(); let (window, device, mut factory, rtv, dsv) = gfx_window_glutin::init::<ColourFormat, DepthFormat>(self.window_builder, self.context_builder, &events_loop); let mut encoder: gfx::Encoder<Resources, gfx_device_gl::CommandBuffer> = factory.create_command_buffer().into(); let mut glyph_brush_builder = gfx_glyph::GlyphBrushBuilder::using_font_bytes(self.font); let bold_font_id = glyph_brush_builder.add_font_bytes(self.bold_font.unwrap_or(self.font)); assert_eq!(bold_font_id, BOLD_FONT_ID); let mut glyph_brush = glyph_brush_builder.build(factory.clone()); let (window_width, window_height, _, _) = rtv.get_dimensions(); let window_width = window_width as u32; let window_height = window_height as u32; let hidpi = window.hidpi_factor(); let (cell_width, cell_height) = if let Some(cell_dimensions) = self.cell_dimensions { (cell_dimensions.x(), cell_dimensions.y()) } else { let section = gfx_glyph::Section { text: "@", scale: self.font_scale, font_id: FONT_ID, ..Default::default() }; let rect = glyph_brush.pixel_bounds(section).ok_or(Error::FailedToMeasureFont)?; (rect.width() as u32, rect.height() as u32) }; let cell_width = (cell_width as f32) * hidpi; let cell_height = (cell_height as f32) * hidpi; let max_grid_size = self.max_grid_size.unwrap_or(Size::new(MAX_WIDTH_IN_CELLS, MAX_HEIGHT_IN_CELLS)); let width_in_cells = ::std::cmp::min((window_width as f32 / cell_width) as u32, max_grid_size.x()); let height_in_cells = ::std::cmp::min((window_height as f32 / cell_height) as u32, max_grid_size.y()); let window_size_in_cells = Size::new(width_in_cells, height_in_cells); let grid = Grid::new(window_size_in_cells); let char_buf = String::with_capacity(1); let underline_width = self.underline_width.map(|w| hidpi * w as f32).unwrap_or_else(|| { cell_height as f32 / UNDERLINE_WIDTH_RATIO as f32 }); let underline_position = self.underline_position.map(|w| hidpi * w as f32).unwrap_or_else(|| { cell_height as f32 - (cell_height as f32 / UNDERLINE_POSITION_RATIO as f32) }); let background_renderer = BackgroundRenderer::new( window_width, window_height, cell_width, cell_height, window_size_in_cells, underline_width, underline_position, rtv.clone(), &mut factory, &mut encoder); let font_scale = gfx_glyph::Scale { x: self.font_scale.x * hidpi, y: self.font_scale.y * hidpi, }; let bold_font_scale = self.bold_font_scale.map(|s| { gfx_glyph::Scale { x: s.x * hidpi, y: s.y * hidpi, } }).unwrap_or(font_scale); Ok(Context { window, device, encoder, factory, rtv, dsv, events_loop, glyph_brush, grid, char_buf, cell_width, cell_height, max_grid_size, background_renderer, font_scale, bold_font_scale, closing: false, }) } } pub struct Context<'a> { window: glutin::GlWindow, device: gfx_device_gl::Device, encoder: gfx::Encoder<Resources, gfx_device_gl::CommandBuffer>, factory: gfx_device_gl::Factory, rtv: gfx::handle::RenderTargetView<Resources, ColourFormat>, dsv: gfx::handle::DepthStencilView<Resources, DepthFormat>, events_loop: glutin::EventsLoop, glyph_brush: gfx_glyph::GlyphBrush<'a, Resources, gfx_device_gl::Factory>, grid: Grid<Cell>, char_buf: String, cell_width: f32, cell_height: f32, max_grid_size: Size, background_renderer: BackgroundRenderer<Resources>, font_scale: gfx_glyph::Scale, bold_font_scale: gfx_glyph::Scale, closing: bool, } impl<'a> Context<'a> { pub fn poll_input<F: FnMut(Input)>(&mut self, mut callback: F) { let mut closing = false; let mut resize = None; self.events_loop.poll_events(|event| { match event { Event::WindowEvent { event, .. } => { if let Some(event) = convert_event(event) { match event { InputEvent::Input(input) => callback(input), InputEvent::Quit => closing = true, InputEvent::Resize(width, height) => resize = Some((width, height)), } } } _ => (), }; }); if let Some((width, height)) = resize { self.handle_resize(width, height); } if closing { self.closing = true; callback(inputs::ETX); } } fn handle_resize(&mut self, width: u32, height: u32) { let (rtv, dsv) = gfx_window_glutin::new_views(&self.window); let width_in_cells = ::std::cmp::min((width as f32 / self.cell_width) as u32, self.max_grid_size.x()); let height_in_cells = ::std::cmp::min((height as f32 / self.cell_height) as u32, self.max_grid_size.y()); let window_size_in_cells = Size::new(width_in_cells, height_in_cells); self.background_renderer.handle_resize(width, height, window_size_in_cells, rtv.clone(), &mut self.factory, &mut self.encoder); self.rtv = rtv; self.dsv = dsv; self.grid.resize(window_size_in_cells); } } impl<'a> Renderer for Context<'a> { type Error = Error; fn render_at<V: View<T>, T>(&mut self, view: &mut V, data: &T, offset: Coord, depth: i32) -> Result<()> { if self.closing { return Ok(()); } self.grid.clear(); view.view(data, offset, depth, &mut self.grid); { let mut output_cells = self.background_renderer.map_cells(&mut self.factory); for ((coord, cell), output_cell) in izip!(self.grid.enumerate(), output_cells.iter_mut()) { self.char_buf.clear(); self.char_buf.push(cell.character); let (font_id, scale) = if cell.bold { (BOLD_FONT_ID, self.bold_font_scale) } else { (FONT_ID, self.font_scale) }; let section = gfx_glyph::Section { text: self.char_buf.as_str(), screen_position: ((coord.x * self.cell_width as i32) as f32, (coord.y * self.cell_height as i32) as f32), scale, font_id, color: cell.foreground_colour.0, ..Default::default() }; self.glyph_brush.queue(section); output_cell.foreground_colour = cell.foreground_colour.0; output_cell.background_colour = cell.background_colour.0; output_cell.underline = cell.underline as u32; } } self.encoder.clear(&self.rtv, [0.0, 0.0, 0.0, 1.0]); self.background_renderer.draw(&mut self.encoder); self.glyph_brush.draw_queued(&mut self.encoder, &self.rtv, &self.dsv).map_err(Error::GfxGlyph)?; self.encoder.flush(&mut self.device); self.window.swap_buffers().map_err(Error::GlutinContextError)?; self.device.cleanup(); Ok(()) } fn size(&self) -> Size { self.grid.size() } }