diff --git a/editor/src/error.rs b/editor/src/error.rs index 7dbfb18380..c3d2f53cbc 100644 --- a/editor/src/error.rs +++ b/editor/src/error.rs @@ -26,10 +26,6 @@ pub enum EdError { }, #[snafu(display("MissingGlyphDims: glyph_dim_rect_opt was None for model. It needs to be set using the example_code_glyph_rect function."))] MissingGlyphDims {}, - #[snafu(display("EmptyGlyphsPerLine: glyphs_per_line was none, it is necessary for highlighting of the code."))] - EmptyGlyphsPerLine { - backtrace: Backtrace, - }, } pub type EdResult = std::result::Result; diff --git a/editor/src/graphics/primitives/text.rs b/editor/src/graphics/primitives/text.rs index c90f4b88cb..b996daa5f8 100644 --- a/editor/src/graphics/primitives/text.rs +++ b/editor/src/graphics/primitives/text.rs @@ -3,13 +3,10 @@ use super::rect::Rect; use crate::graphics::colors::CODE_COLOR; -use crate::graphics::style::CODE_FONT_SIZE; +use crate::graphics::style::{CODE_FONT_SIZE, CODE_TXT_XY}; use ab_glyph::{FontArc, Glyph, InvalidFont}; use cgmath::{Vector2, Vector4}; -use itertools::Itertools; use wgpu_glyph::{ab_glyph, GlyphBrush, GlyphBrushBuilder, GlyphCruncher, Section}; -use bumpalo::collections::Vec as BumpVec; -use bumpalo::Bump; #[derive(Debug)] pub struct Text { @@ -39,7 +36,7 @@ impl Default for Text { // necessary to get dimensions for caret pub fn example_code_glyph_rect(glyph_brush: &mut GlyphBrush<()>) -> Rect { let code_text = Text { - position: (30.0, 90.0).into(), //TODO 30.0 90.0 should be an arg + position: CODE_TXT_XY.into(), area_bounds: (std::f32::INFINITY, std::f32::INFINITY).into(), color: CODE_COLOR.into(), text: "a".to_owned(), @@ -86,40 +83,12 @@ fn section_from_text( } // returns glyphs per line -pub fn queue_text_draw<'a>(text: &Text, glyph_brush: &mut GlyphBrush<()>, arena: &'a Bump, selectable: bool) -> Option> { +pub fn queue_text_draw(text: &Text, glyph_brush: &mut GlyphBrush<()>) { let layout = layout_from_text(text); let section = section_from_text(text, layout); glyph_brush.queue(section.clone()); - - if selectable { - let mut glyphs_per_line: BumpVec = BumpVec::new_in(arena); - - let glyph_section_iter = glyph_brush.glyphs_custom_layout(section, &layout); - - let first_glyph_opt = glyph_section_iter.next(); - - if let Some(first_glyph) = first_glyph_opt { - let mut line_y_coord = first_glyph.glyph.scale.y; - let mut glyphs_on_line = 0; - - for glyph in glyph_section_iter { - let curr_y_coord = glyph.glyph.scale.y; - if curr_y_coord != line_y_coord { - line_y_coord = curr_y_coord; - glyphs_per_line.push(glyphs_on_line); - glyphs_on_line = 0; - } else { - glyphs_on_line += 1; - } - } - } - - Some(glyphs_per_line) - } else { - None - } } fn glyph_to_rect(glyph: &wgpu_glyph::SectionGlyph) -> Rect { @@ -144,7 +113,7 @@ pub fn glyph_top_y(glyph: &Glyph) -> f32 { } pub fn glyph_width(glyph: &Glyph) -> f32 { - glyph.scale.x * 0.5 + glyph.scale.x * 0.4765 } pub fn build_glyph_brush( diff --git a/editor/src/graphics/style.rs b/editor/src/graphics/style.rs index cd9cb50b98..a32deb6558 100644 --- a/editor/src/graphics/style.rs +++ b/editor/src/graphics/style.rs @@ -1 +1,2 @@ pub const CODE_FONT_SIZE: f32 = 40.0; +pub const CODE_TXT_XY: (f32, f32) = (30.0, 90.0); diff --git a/editor/src/lib.rs b/editor/src/lib.rs index caf1b30ff8..0324eadcbc 100644 --- a/editor/src/lib.rs +++ b/editor/src/lib.rs @@ -13,8 +13,7 @@ extern crate pest; #[macro_use] extern crate pest_derive; -use crate::error::EdError::{MissingGlyphDims}; -use crate::error::{EmptyGlyphsPerLine}; +use crate::error::EdError::MissingGlyphDims; use crate::error::{print_err, EdResult}; use crate::graphics::colors::{CARET_COLOR, CODE_COLOR, TXT_COLOR}; use crate::graphics::lowlevel::buffer::create_rect_buffers; @@ -25,12 +24,13 @@ use crate::graphics::primitives::text::{ build_glyph_brush, example_code_glyph_rect, queue_text_draw, Text, }; use crate::graphics::style::CODE_FONT_SIZE; +use crate::graphics::style::CODE_TXT_XY; use crate::selection::create_selection_rects; use crate::tea::ed_model::EdModel; use crate::tea::{ed_model, update}; -use crate::vec_result::get_res; use bumpalo::collections::Vec as BumpVec; use bumpalo::Bump; +use cgmath::Vector2; use ed_model::Position; use pipelines::RectResources; use std::error::Error; @@ -42,7 +42,6 @@ use winit::dpi::PhysicalSize; use winit::event; use winit::event::{Event, ModifiersState}; use winit::event_loop::ControlFlow; -use snafu::ensure; pub mod error; pub mod graphics; @@ -217,12 +216,17 @@ fn run_event_loop() -> Result<(), Box> { .expect("Failed to acquire next SwapChainFrame") .output; - let glyph_bounds_rects = - queue_all_text(&size, &ed_model.lines, ed_model.caret_pos, &mut glyph_brush, &arena); + queue_all_text( + &size, + &ed_model.lines, + ed_model.caret_pos, + CODE_TXT_XY.into(), + &mut glyph_brush, + ); match draw_all_rects( &ed_model, - &glyph_bounds_rects, + &ed_model.glyph_dim_rect_opt, &arena, &mut encoder, &frame.view, @@ -269,7 +273,7 @@ fn run_event_loop() -> Result<(), Box> { fn draw_all_rects( ed_model: &EdModel, - glyph_bounds_rects: &[Vec], + glyph_dim_rect_opt: &Option, arena: &Bump, encoder: &mut CommandEncoder, texture_view: &TextureView, @@ -278,17 +282,20 @@ fn draw_all_rects( ) -> EdResult<()> { let mut all_rects: BumpVec = BumpVec::new_in(arena); + let glyph_rect = if let Some(glyph_dim_rect) = glyph_dim_rect_opt { + glyph_dim_rect + } else { + return Err(MissingGlyphDims {}); + }; + if let Some(selection) = ed_model.selection_opt { - let mut selection_rects = create_selection_rects(selection, glyph_bounds_rects, &arena)?; + let mut selection_rects = + create_selection_rects(selection, &ed_model.lines, glyph_rect, &arena)?; all_rects.append(&mut selection_rects); } - all_rects.push(make_caret_rect( - ed_model.caret_pos, - glyph_bounds_rects, - ed_model.glyph_dim_rect_opt, - )?); + all_rects.push(make_caret_rect(ed_model.caret_pos, glyph_rect)?); let rect_buffers = create_rect_buffers(gpu_device, encoder, &all_rects); @@ -325,50 +332,29 @@ fn begin_render_pass<'a>( }) } -fn make_caret_rect( - caret_pos: Position, - glyph_bound_rects: &[Vec], - glyph_dim_rect_opt: Option, -) -> EdResult { - let mut glyph_rect = if let Some(glyph_dim_rect) = glyph_dim_rect_opt { - glyph_dim_rect - } else { - return Err(MissingGlyphDims{}); - }; +fn make_caret_rect(caret_pos: Position, glyph_dim_rect: &Rect) -> EdResult { + let caret_y = + glyph_dim_rect.top_left_coords.y + (caret_pos.line as f32) * glyph_dim_rect.height; - let caret_y = glyph_rect.top_left_coords.y + (caret_pos.line as f32) * glyph_rect.height; - - if caret_pos.column > 0 && glyph_bound_rects.len() > caret_pos.line { - let indx = caret_pos.column - 1; - let glyph_rect_line = get_res(caret_pos.line, glyph_bound_rects)?; - - if glyph_rect_line.len() > indx { - glyph_rect = *get_res(indx, glyph_rect_line)?; - } - }; - - let caret_x = if caret_pos.column == 0 { - glyph_rect.top_left_coords.x - } else { - glyph_rect.top_left_coords.x + glyph_rect.width - }; + let caret_x = + glyph_dim_rect.top_left_coords.x + glyph_dim_rect.width * (caret_pos.column as f32); Ok(Rect { top_left_coords: (caret_x, caret_y).into(), - height: glyph_rect.height, - width: 3.0, + height: glyph_dim_rect.height, + width: 2.0, color: CARET_COLOR, }) } // returns bounding boxes for every glyph -fn queue_all_text<'a>( +fn queue_all_text( size: &PhysicalSize, lines: &[String], caret_pos: Position, + code_coords: Vector2, glyph_brush: &mut GlyphBrush<()>, - arena: &'a Bump -) -> EdResult> { +) { let area_bounds = (size.width as f32, size.height as f32).into(); let main_label = Text { @@ -381,7 +367,7 @@ fn queue_all_text<'a>( }; let code_text = Text { - position: (30.0, 90.0).into(), //TODO 30 90 should be an arg + position: code_coords, area_bounds, color: CODE_COLOR.into(), text: lines.join(""), @@ -390,7 +376,7 @@ fn queue_all_text<'a>( }; let caret_pos_label = Text { - position: (30.0, 530.0).into(), + position: (30.0, (size.height as f32) - 45.0).into(), area_bounds, color: TXT_COLOR.into(), text: format!("Ln {}, Col {}", caret_pos.line, caret_pos.column), @@ -398,11 +384,9 @@ fn queue_all_text<'a>( ..Default::default() }; - queue_text_draw(&main_label, glyph_brush, arena, false); + queue_text_draw(&main_label, glyph_brush); - queue_text_draw(&caret_pos_label, glyph_brush, arena, false); + queue_text_draw(&caret_pos_label, glyph_brush); - let glyphs_per_line_opt = queue_text_draw(&code_text, glyph_brush, arena, true); - - ensure!(glyphs_per_line_opt.is_some(), EmptyGlyphsPerLine{}) + queue_text_draw(&code_text, glyph_brush); } diff --git a/editor/src/selection.rs b/editor/src/selection.rs index d3aa8dabae..85f60ab3fd 100644 --- a/editor/src/selection.rs +++ b/editor/src/selection.rs @@ -43,7 +43,8 @@ fn validate_selection(selection: RawSelection) -> EdResult { pub fn create_selection_rects<'a>( raw_sel: RawSelection, - glyph_bound_rects: &[Vec], + lines: &[String], + glyph_dim_rect: &Rect, arena: &'a Bump, ) -> EdResult> { let valid_sel = validate_selection(raw_sel)?; @@ -51,25 +52,17 @@ pub fn create_selection_rects<'a>( let mut all_rects: BumpVec = BumpVec::new_in(arena); + let height = glyph_dim_rect.height; + let start_y = glyph_dim_rect.top_left_coords.y + height * (start_pos.line as f32); + let line_start_x = glyph_dim_rect.top_left_coords.x; + if start_pos.line == end_pos.line { - let start_glyph_rect = get_res( - start_pos.column, - get_res(start_pos.line, glyph_bound_rects)?, - )?; - - let stop_glyph_rect = get_res( - end_pos.column - 1, - get_res(end_pos.line, glyph_bound_rects)?, - )?; - - let top_left_coords = start_glyph_rect.top_left_coords; - - let height = start_glyph_rect.height; - let width = (stop_glyph_rect.top_left_coords.x - start_glyph_rect.top_left_coords.x) - + stop_glyph_rect.width; + let width = ((end_pos.column as f32) * glyph_dim_rect.width) + - ((start_pos.column as f32) * glyph_dim_rect.width); + let sel_rect_x = line_start_x + ((start_pos.column as f32) * glyph_dim_rect.width); all_rects.push(Rect { - top_left_coords, + top_left_coords: (sel_rect_x, start_y).into(), width, height, color: colors::SELECT_COLOR, @@ -78,21 +71,14 @@ pub fn create_selection_rects<'a>( Ok(all_rects) } else { // first line - let start_line = get_res(start_pos.line, glyph_bound_rects)?; + let end_col = get_res(start_pos.line, lines)?.len(); + let width = ((end_col as f32) * glyph_dim_rect.width) + - ((start_pos.column as f32) * glyph_dim_rect.width); - let start_glyph_rect = get_res(start_pos.column, start_line)?; - - let start_line_last_glyph_rect = get_res(start_line.len() - 1, start_line)?; - - let top_left_coords = start_glyph_rect.top_left_coords; - - let height = start_glyph_rect.height; - let width = (start_line_last_glyph_rect.top_left_coords.x - - start_glyph_rect.top_left_coords.x) - + start_line_last_glyph_rect.width; + let sel_rect_x = line_start_x + ((start_pos.column as f32) * glyph_dim_rect.width); all_rects.push(Rect { - top_left_coords, + top_left_coords: (sel_rect_x, start_y).into(), width, height, color: colors::SELECT_COLOR, @@ -103,21 +89,14 @@ pub fn create_selection_rects<'a>( let first_mid_line = start_pos.line + 1; for i in first_mid_line..(first_mid_line + nr_mid_lines) { - let mid_line = get_res(i, glyph_bound_rects)?; + let mid_line_len = get_res(i, lines)?.len(); - let mid_line_first_glyph_rect = get_res(0, mid_line)?; + let width = (mid_line_len as f32) * glyph_dim_rect.width; - let mid_line_last_glyph_rect = get_res(mid_line.len() - 1, mid_line)?; - - let top_left_coords = mid_line_first_glyph_rect.top_left_coords; - - let height = mid_line_first_glyph_rect.height; - let width = (mid_line_last_glyph_rect.top_left_coords.x - - mid_line_first_glyph_rect.top_left_coords.x) - + mid_line_last_glyph_rect.width; + let sel_rect_y = start_y + ((i - start_pos.line) as f32) * glyph_dim_rect.height; all_rects.push(Rect { - top_left_coords, + top_left_coords: (line_start_x, sel_rect_y).into(), width, height, color: colors::SELECT_COLOR, @@ -126,21 +105,13 @@ pub fn create_selection_rects<'a>( //last line if end_pos.column > 0 { - let stop_line = get_res(end_pos.line, glyph_bound_rects)?; + let sel_rect_y = + start_y + ((end_pos.line - start_pos.line) as f32) * glyph_dim_rect.height; - let stop_line_first_glyph_rect = get_res(0, stop_line)?; - - let stop_glyph_rect = get_res(end_pos.column - 1, stop_line)?; - - let top_left_coords = stop_line_first_glyph_rect.top_left_coords; - - let height = stop_glyph_rect.height; - let width = (stop_glyph_rect.top_left_coords.x - - stop_line_first_glyph_rect.top_left_coords.x) - + stop_glyph_rect.width; + let width = (end_pos.column as f32) * glyph_dim_rect.width; all_rects.push(Rect { - top_left_coords, + top_left_coords: (line_start_x, sel_rect_y).into(), width, height, color: colors::SELECT_COLOR,