mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-09 22:54:44 +03:00
commit
7f770981e0
@ -24,6 +24,8 @@ pub enum EdError {
|
||||
err_msg: String,
|
||||
backtrace: Backtrace,
|
||||
},
|
||||
#[snafu(display("MissingGlyphDims: glyph_dim_rect_opt was None for model. It needs to be set using the example_code_glyph_rect function."))]
|
||||
MissingGlyphDims {},
|
||||
}
|
||||
|
||||
pub type EdResult<T, E = EdError> = std::result::Result<T, E>;
|
||||
|
@ -1 +1,5 @@
|
||||
pub const WHITE: [f32; 3] = [1.0, 1.0, 1.0];
|
||||
pub const TXT_COLOR: [f32; 4] = [0.4666, 0.2, 1.0, 1.0];
|
||||
pub const CODE_COLOR: [f32; 4] = [0.0, 0.05, 0.46, 1.0];
|
||||
pub const CARET_COLOR: [f32; 3] = WHITE;
|
||||
pub const SELECT_COLOR: [f32; 3] = [0.45, 0.61, 1.0];
|
||||
|
@ -1,6 +1,6 @@
|
||||
// Adapted from https://github.com/sotrh/learn-wgpu
|
||||
// by Benjamin Hansen, licensed under the MIT license
|
||||
use crate::graphics::lowlevel::vertex::Vertex;
|
||||
use super::vertex::Vertex;
|
||||
use crate::graphics::primitives::rect::Rect;
|
||||
use bumpalo::collections::Vec as BumpVec;
|
||||
use wgpu::util::{BufferInitDescriptor, DeviceExt};
|
||||
|
@ -1,3 +1,4 @@
|
||||
pub mod buffer;
|
||||
pub mod ortho;
|
||||
pub mod pipelines;
|
||||
pub mod vertex;
|
||||
|
@ -66,6 +66,7 @@ pub fn update_ortho_buffer(
|
||||
cmd_queue.submit(Some(encoder.finish()));
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OrthoResources {
|
||||
pub buffer: Buffer,
|
||||
pub bind_group_layout: BindGroupLayout,
|
||||
|
71
editor/src/graphics/lowlevel/pipelines.rs
Normal file
71
editor/src/graphics/lowlevel/pipelines.rs
Normal file
@ -0,0 +1,71 @@
|
||||
use super::ortho::{init_ortho, OrthoResources};
|
||||
use super::vertex::Vertex;
|
||||
|
||||
pub struct RectResources {
|
||||
pub pipeline: wgpu::RenderPipeline,
|
||||
pub ortho: OrthoResources,
|
||||
}
|
||||
|
||||
pub fn make_rect_pipeline(
|
||||
gpu_device: &wgpu::Device,
|
||||
swap_chain_descr: &wgpu::SwapChainDescriptor,
|
||||
) -> RectResources {
|
||||
let ortho = init_ortho(swap_chain_descr.width, swap_chain_descr.height, gpu_device);
|
||||
|
||||
let pipeline_layout = gpu_device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
bind_group_layouts: &[&ortho.bind_group_layout],
|
||||
push_constant_ranges: &[],
|
||||
label: Some("Rectangle pipeline layout"),
|
||||
});
|
||||
let pipeline = create_render_pipeline(
|
||||
&gpu_device,
|
||||
&pipeline_layout,
|
||||
swap_chain_descr.format,
|
||||
&[Vertex::DESC],
|
||||
wgpu::include_spirv!("../shaders/rect.vert.spv"),
|
||||
wgpu::include_spirv!("../shaders/rect.frag.spv"),
|
||||
);
|
||||
|
||||
RectResources { pipeline, ortho }
|
||||
}
|
||||
|
||||
pub fn create_render_pipeline(
|
||||
device: &wgpu::Device,
|
||||
layout: &wgpu::PipelineLayout,
|
||||
color_format: wgpu::TextureFormat,
|
||||
vertex_descs: &[wgpu::VertexBufferDescriptor],
|
||||
vs_src: wgpu::ShaderModuleSource,
|
||||
fs_src: wgpu::ShaderModuleSource,
|
||||
) -> wgpu::RenderPipeline {
|
||||
let vs_module = device.create_shader_module(vs_src);
|
||||
let fs_module = device.create_shader_module(fs_src);
|
||||
|
||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Render pipeline"),
|
||||
layout: Some(&layout),
|
||||
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
||||
module: &vs_module,
|
||||
entry_point: "main",
|
||||
},
|
||||
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
|
||||
module: &fs_module,
|
||||
entry_point: "main",
|
||||
}),
|
||||
rasterization_state: None,
|
||||
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
color_states: &[wgpu::ColorStateDescriptor {
|
||||
format: color_format,
|
||||
color_blend: wgpu::BlendDescriptor::REPLACE,
|
||||
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
||||
write_mask: wgpu::ColorWrite::ALL,
|
||||
}],
|
||||
depth_stencil_state: None,
|
||||
sample_count: 1,
|
||||
sample_mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
vertex_state: wgpu::VertexStateDescriptor {
|
||||
index_format: wgpu::IndexFormat::Uint32,
|
||||
vertex_buffers: vertex_descs,
|
||||
},
|
||||
})
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
pub mod colors;
|
||||
pub mod lowlevel;
|
||||
pub mod primitives;
|
||||
pub mod style;
|
||||
|
@ -1,7 +1,9 @@
|
||||
// Adapted from https://github.com/sotrh/learn-wgpu
|
||||
// by Benjamin Hansen, licensed under the MIT license
|
||||
|
||||
use crate::graphics::primitives::rect::Rect;
|
||||
use super::rect::Rect;
|
||||
use crate::graphics::colors::CODE_COLOR;
|
||||
use crate::graphics::style::CODE_FONT_SIZE;
|
||||
use ab_glyph::{FontArc, Glyph, InvalidFont};
|
||||
use cgmath::{Vector2, Vector4};
|
||||
use itertools::Itertools;
|
||||
@ -25,22 +27,50 @@ impl Default for Text {
|
||||
area_bounds: (std::f32::INFINITY, std::f32::INFINITY).into(),
|
||||
color: (1.0, 1.0, 1.0, 1.0).into(),
|
||||
text: String::new(),
|
||||
size: 16.0,
|
||||
size: CODE_FONT_SIZE,
|
||||
visible: true,
|
||||
centered: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// returns bounding boxes for every glyph
|
||||
pub fn queue_text_draw(text: &Text, glyph_brush: &mut GlyphBrush<()>) -> Vec<Vec<Rect>> {
|
||||
let layout = wgpu_glyph::Layout::default().h_align(if text.centered {
|
||||
// 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
|
||||
area_bounds: (std::f32::INFINITY, std::f32::INFINITY).into(),
|
||||
color: CODE_COLOR.into(),
|
||||
text: "a".to_owned(),
|
||||
size: CODE_FONT_SIZE,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let layout = layout_from_text(&code_text);
|
||||
|
||||
let section = section_from_text(&code_text, layout);
|
||||
|
||||
let mut glyph_section_iter = glyph_brush.glyphs_custom_layout(section, &layout);
|
||||
|
||||
if let Some(glyph) = glyph_section_iter.next() {
|
||||
glyph_to_rect(glyph)
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
}
|
||||
|
||||
fn layout_from_text(text: &Text) -> wgpu_glyph::Layout<wgpu_glyph::BuiltInLineBreaker> {
|
||||
wgpu_glyph::Layout::default().h_align(if text.centered {
|
||||
wgpu_glyph::HorizontalAlign::Center
|
||||
} else {
|
||||
wgpu_glyph::HorizontalAlign::Left
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
let section = Section {
|
||||
fn section_from_text(
|
||||
text: &Text,
|
||||
layout: wgpu_glyph::Layout<wgpu_glyph::BuiltInLineBreaker>,
|
||||
) -> wgpu_glyph::Section {
|
||||
Section {
|
||||
screen_position: text.position.into(),
|
||||
bounds: text.area_bounds.into(),
|
||||
layout,
|
||||
@ -50,27 +80,21 @@ pub fn queue_text_draw(text: &Text, glyph_brush: &mut GlyphBrush<()>) -> Vec<Vec
|
||||
wgpu_glyph::Text::new(&text.text)
|
||||
.with_color(text.color)
|
||||
.with_scale(text.size),
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
// returns bounding boxes for every glyph
|
||||
pub fn queue_text_draw(text: &Text, glyph_brush: &mut GlyphBrush<()>) -> Vec<Vec<Rect>> {
|
||||
let layout = layout_from_text(text);
|
||||
|
||||
let section = section_from_text(text, layout);
|
||||
|
||||
glyph_brush.queue(section.clone());
|
||||
|
||||
let glyph_section_iter = glyph_brush.glyphs_custom_layout(section, &layout);
|
||||
|
||||
glyph_section_iter
|
||||
.map(|section_glyph| {
|
||||
let position = section_glyph.glyph.position;
|
||||
let px_scale = section_glyph.glyph.scale;
|
||||
let width = glyph_width(§ion_glyph.glyph);
|
||||
let height = px_scale.y;
|
||||
let top_y = glyph_top_y(§ion_glyph.glyph);
|
||||
|
||||
Rect {
|
||||
top_left_coords: [position.x, top_y].into(),
|
||||
width,
|
||||
height,
|
||||
color: [1.0, 1.0, 1.0],
|
||||
}
|
||||
})
|
||||
.map(glyph_to_rect)
|
||||
.group_by(|rect| rect.top_left_coords.y)
|
||||
.into_iter()
|
||||
.map(|(_y_coord, rect_group)| {
|
||||
@ -94,6 +118,21 @@ pub fn queue_text_draw(text: &Text, glyph_brush: &mut GlyphBrush<()>) -> Vec<Vec
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn glyph_to_rect(glyph: &wgpu_glyph::SectionGlyph) -> Rect {
|
||||
let position = glyph.glyph.position;
|
||||
let px_scale = glyph.glyph.scale;
|
||||
let width = glyph_width(&glyph.glyph);
|
||||
let height = px_scale.y;
|
||||
let top_y = glyph_top_y(&glyph.glyph);
|
||||
|
||||
Rect {
|
||||
top_left_coords: [position.x, top_y].into(),
|
||||
width,
|
||||
height,
|
||||
color: [1.0, 1.0, 1.0],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn glyph_top_y(glyph: &Glyph) -> f32 {
|
||||
let height = glyph.scale.y;
|
||||
|
||||
|
1
editor/src/graphics/style.rs
Normal file
1
editor/src/graphics/style.rs
Normal file
@ -0,0 +1 @@
|
||||
pub const CODE_FONT_SIZE: f32 = 40.0;
|
@ -1,4 +1,4 @@
|
||||
use crate::tea::model::Model;
|
||||
use crate::tea::ed_model::EdModel;
|
||||
use crate::tea::update::{move_caret_down, move_caret_left, move_caret_right, move_caret_up};
|
||||
use winit::event::{ElementState, ModifiersState, VirtualKeyCode};
|
||||
|
||||
@ -6,7 +6,7 @@ pub fn handle_keydown(
|
||||
elem_state: ElementState,
|
||||
virtual_keycode: VirtualKeyCode,
|
||||
modifiers: ModifiersState,
|
||||
model: &mut Model,
|
||||
model: &mut EdModel,
|
||||
) {
|
||||
use winit::event::VirtualKeyCode::*;
|
||||
|
||||
|
@ -13,21 +13,31 @@ extern crate pest;
|
||||
#[macro_use]
|
||||
extern crate pest_derive;
|
||||
|
||||
use crate::error::print_err;
|
||||
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;
|
||||
use crate::graphics::lowlevel::ortho::{init_ortho, update_ortho_buffer, OrthoResources};
|
||||
use crate::graphics::lowlevel::vertex::Vertex;
|
||||
use crate::graphics::lowlevel::ortho::update_ortho_buffer;
|
||||
use crate::graphics::lowlevel::pipelines;
|
||||
use crate::graphics::primitives::rect::Rect;
|
||||
use crate::graphics::primitives::text::{build_glyph_brush, queue_text_draw, Text};
|
||||
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::selection::create_selection_rects;
|
||||
use crate::tea::{model, update};
|
||||
use crate::util::is_newline;
|
||||
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 model::Position;
|
||||
use ed_model::Position;
|
||||
use pipelines::RectResources;
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use wgpu::{CommandEncoder, RenderPass, TextureView};
|
||||
use wgpu_glyph::GlyphBrush;
|
||||
use winit::dpi::PhysicalSize;
|
||||
use winit::event;
|
||||
use winit::event::{Event, ModifiersState};
|
||||
use winit::event_loop::ControlFlow;
|
||||
@ -108,12 +118,13 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
let mut swap_chain = gpu_device.create_swap_chain(&surface, &swap_chain_descr);
|
||||
|
||||
let (rect_pipeline, ortho) = make_rect_pipeline(&gpu_device, &swap_chain_descr);
|
||||
let rect_resources = pipelines::make_rect_pipeline(&gpu_device, &swap_chain_descr);
|
||||
|
||||
let mut glyph_brush = build_glyph_brush(&gpu_device, render_format)?;
|
||||
|
||||
let is_animating = true;
|
||||
let mut ed_model = model::init_model();
|
||||
let mut ed_model = ed_model::init_model();
|
||||
ed_model.glyph_dim_rect_opt = Some(example_code_glyph_rect(&mut glyph_brush));
|
||||
let mut keyboard_modifiers = ModifiersState::empty();
|
||||
|
||||
let arena = Bump::new();
|
||||
@ -159,7 +170,7 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||
size.width,
|
||||
size.height,
|
||||
&gpu_device,
|
||||
&ortho.buffer,
|
||||
&rect_resources.ortho.buffer,
|
||||
&cmd_queue,
|
||||
);
|
||||
}
|
||||
@ -168,7 +179,7 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||
event: event::WindowEvent::ReceivedCharacter(ch),
|
||||
..
|
||||
} => {
|
||||
update_text_state(&mut ed_model, &ch);
|
||||
update::update_text_state(&mut ed_model, &ch);
|
||||
}
|
||||
//Keyboard Input
|
||||
Event::WindowEvent {
|
||||
@ -207,36 +218,20 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||
let glyph_bounds_rects =
|
||||
queue_all_text(&size, &ed_model.lines, ed_model.caret_pos, &mut glyph_brush);
|
||||
|
||||
if let Some(selection) = ed_model.selection_opt {
|
||||
let selection_rects_res =
|
||||
create_selection_rects(selection, &glyph_bounds_rects, &arena);
|
||||
|
||||
match selection_rects_res {
|
||||
Ok(selection_rects) => {
|
||||
if !selection_rects.is_empty() {
|
||||
let rect_buffers = create_rect_buffers(
|
||||
&gpu_device,
|
||||
&mut encoder,
|
||||
&selection_rects,
|
||||
);
|
||||
|
||||
let mut render_pass = begin_render_pass(&mut encoder, &frame.view);
|
||||
|
||||
render_pass.set_pipeline(&rect_pipeline);
|
||||
render_pass.set_bind_group(0, &ortho.bind_group, &[]);
|
||||
render_pass
|
||||
.set_vertex_buffer(0, rect_buffers.vertex_buffer.slice(..));
|
||||
render_pass.set_index_buffer(rect_buffers.index_buffer.slice(..));
|
||||
render_pass.draw_indexed(0..rect_buffers.num_rects, 0, 0..1);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
begin_render_pass(&mut encoder, &frame.view);
|
||||
print_err(&e) //TODO draw error text on screen
|
||||
}
|
||||
match draw_all_rects(
|
||||
&ed_model,
|
||||
&glyph_bounds_rects,
|
||||
&arena,
|
||||
&mut encoder,
|
||||
&frame.view,
|
||||
&gpu_device,
|
||||
&rect_resources,
|
||||
) {
|
||||
Ok(()) => (),
|
||||
Err(e) => {
|
||||
print_err(&e);
|
||||
begin_render_pass(&mut encoder, &frame.view);
|
||||
}
|
||||
} else {
|
||||
begin_render_pass(&mut encoder, &frame.view);
|
||||
}
|
||||
|
||||
// draw all text
|
||||
@ -270,6 +265,42 @@ fn run_event_loop() -> Result<(), Box<dyn Error>> {
|
||||
})
|
||||
}
|
||||
|
||||
fn draw_all_rects(
|
||||
ed_model: &EdModel,
|
||||
glyph_bounds_rects: &[Vec<Rect>],
|
||||
arena: &Bump,
|
||||
encoder: &mut CommandEncoder,
|
||||
texture_view: &TextureView,
|
||||
gpu_device: &wgpu::Device,
|
||||
rect_resources: &RectResources,
|
||||
) -> EdResult<()> {
|
||||
let mut all_rects: BumpVec<Rect> = BumpVec::new_in(arena);
|
||||
|
||||
if let Some(selection) = ed_model.selection_opt {
|
||||
let mut selection_rects = create_selection_rects(selection, glyph_bounds_rects, &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,
|
||||
)?);
|
||||
|
||||
let rect_buffers = create_rect_buffers(gpu_device, encoder, &all_rects);
|
||||
|
||||
let mut render_pass = begin_render_pass(encoder, texture_view);
|
||||
|
||||
render_pass.set_pipeline(&rect_resources.pipeline);
|
||||
render_pass.set_bind_group(0, &rect_resources.ortho.bind_group, &[]);
|
||||
render_pass.set_vertex_buffer(0, rect_buffers.vertex_buffer.slice(..));
|
||||
render_pass.set_index_buffer(rect_buffers.index_buffer.slice(..));
|
||||
render_pass.draw_indexed(0..rect_buffers.num_rects, 0, 0..1);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn begin_render_pass<'a>(
|
||||
encoder: &'a mut CommandEncoder,
|
||||
texture_view: &'a TextureView,
|
||||
@ -292,101 +323,73 @@ fn begin_render_pass<'a>(
|
||||
})
|
||||
}
|
||||
|
||||
fn make_rect_pipeline(
|
||||
gpu_device: &wgpu::Device,
|
||||
swap_chain_descr: &wgpu::SwapChainDescriptor,
|
||||
) -> (wgpu::RenderPipeline, OrthoResources) {
|
||||
let ortho = init_ortho(swap_chain_descr.width, swap_chain_descr.height, gpu_device);
|
||||
fn make_caret_rect(
|
||||
caret_pos: Position,
|
||||
glyph_bound_rects: &[Vec<Rect>],
|
||||
glyph_dim_rect_opt: Option<Rect>,
|
||||
) -> EdResult<Rect> {
|
||||
let mut glyph_rect = if let Some(glyph_dim_rect) = glyph_dim_rect_opt {
|
||||
glyph_dim_rect
|
||||
} else {
|
||||
return Err(MissingGlyphDims {});
|
||||
};
|
||||
|
||||
let pipeline_layout = gpu_device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
bind_group_layouts: &[&ortho.bind_group_layout],
|
||||
push_constant_ranges: &[],
|
||||
label: Some("Rectangle pipeline layout"),
|
||||
});
|
||||
let pipeline = create_render_pipeline(
|
||||
&gpu_device,
|
||||
&pipeline_layout,
|
||||
swap_chain_descr.format,
|
||||
&[Vertex::DESC],
|
||||
wgpu::include_spirv!("graphics/shaders/rect.vert.spv"),
|
||||
wgpu::include_spirv!("graphics/shaders/rect.frag.spv"),
|
||||
);
|
||||
let caret_y = glyph_rect.top_left_coords.y + (caret_pos.line as f32) * glyph_rect.height;
|
||||
|
||||
(pipeline, ortho)
|
||||
}
|
||||
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)?;
|
||||
|
||||
fn create_render_pipeline(
|
||||
device: &wgpu::Device,
|
||||
layout: &wgpu::PipelineLayout,
|
||||
color_format: wgpu::TextureFormat,
|
||||
vertex_descs: &[wgpu::VertexBufferDescriptor],
|
||||
vs_src: wgpu::ShaderModuleSource,
|
||||
fs_src: wgpu::ShaderModuleSource,
|
||||
) -> wgpu::RenderPipeline {
|
||||
let vs_module = device.create_shader_module(vs_src);
|
||||
let fs_module = device.create_shader_module(fs_src);
|
||||
if glyph_rect_line.len() > indx {
|
||||
glyph_rect = *get_res(indx, glyph_rect_line)?;
|
||||
}
|
||||
};
|
||||
|
||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("Render pipeline"),
|
||||
layout: Some(&layout),
|
||||
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
||||
module: &vs_module,
|
||||
entry_point: "main",
|
||||
},
|
||||
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
|
||||
module: &fs_module,
|
||||
entry_point: "main",
|
||||
}),
|
||||
rasterization_state: None,
|
||||
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
color_states: &[wgpu::ColorStateDescriptor {
|
||||
format: color_format,
|
||||
color_blend: wgpu::BlendDescriptor::REPLACE,
|
||||
alpha_blend: wgpu::BlendDescriptor::REPLACE,
|
||||
write_mask: wgpu::ColorWrite::ALL,
|
||||
}],
|
||||
depth_stencil_state: None,
|
||||
sample_count: 1,
|
||||
sample_mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
vertex_state: wgpu::VertexStateDescriptor {
|
||||
index_format: wgpu::IndexFormat::Uint32,
|
||||
vertex_buffers: vertex_descs,
|
||||
},
|
||||
let caret_x = if caret_pos.column == 0 {
|
||||
glyph_rect.top_left_coords.x
|
||||
} else {
|
||||
glyph_rect.top_left_coords.x + glyph_rect.width
|
||||
};
|
||||
|
||||
Ok(Rect {
|
||||
top_left_coords: (caret_x, caret_y).into(),
|
||||
height: glyph_rect.height,
|
||||
width: 3.0,
|
||||
color: CARET_COLOR,
|
||||
})
|
||||
}
|
||||
|
||||
// returns bounding boxes for every glyph
|
||||
fn queue_all_text(
|
||||
size: &winit::dpi::PhysicalSize<u32>,
|
||||
size: &PhysicalSize<u32>,
|
||||
lines: &[String],
|
||||
caret_pos: Position,
|
||||
glyph_brush: &mut wgpu_glyph::GlyphBrush<()>,
|
||||
glyph_brush: &mut GlyphBrush<()>,
|
||||
) -> Vec<Vec<Rect>> {
|
||||
let area_bounds = (size.width as f32, size.height as f32).into();
|
||||
|
||||
let main_label = Text {
|
||||
position: (30.0, 30.0).into(),
|
||||
area_bounds,
|
||||
color: (0.4666, 0.2, 1.0, 1.0).into(),
|
||||
color: TXT_COLOR.into(),
|
||||
text: String::from("Enter some text:"),
|
||||
size: 40.0,
|
||||
size: CODE_FONT_SIZE,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let code_text = Text {
|
||||
position: (30.0, 90.0).into(),
|
||||
position: (30.0, 90.0).into(), //TODO 30 90 should be an arg
|
||||
area_bounds,
|
||||
color: (0.0, 0.05, 0.46, 1.0).into(),
|
||||
color: CODE_COLOR.into(),
|
||||
text: lines.join(""),
|
||||
size: 40.0,
|
||||
size: CODE_FONT_SIZE,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let caret_pos_label = Text {
|
||||
position: (30.0, 530.0).into(),
|
||||
area_bounds,
|
||||
color: (0.4666, 0.2, 1.0, 1.0).into(),
|
||||
color: TXT_COLOR.into(),
|
||||
text: format!("Ln {}, Col {}", caret_pos.line, caret_pos.column),
|
||||
size: 30.0,
|
||||
..Default::default()
|
||||
@ -398,53 +401,3 @@ fn queue_all_text(
|
||||
|
||||
queue_text_draw(&code_text, glyph_brush)
|
||||
}
|
||||
|
||||
fn update_text_state(ed_model: &mut model::Model, received_char: &char) {
|
||||
ed_model.selection_opt = None;
|
||||
|
||||
match received_char {
|
||||
'\u{8}' | '\u{7f}' => {
|
||||
// In Linux, we get a '\u{8}' when you press backspace,
|
||||
// but in macOS we get '\u{7f}'.
|
||||
if let Some(last_line) = ed_model.lines.last_mut() {
|
||||
if !last_line.is_empty() {
|
||||
last_line.pop();
|
||||
} else if ed_model.lines.len() > 1 {
|
||||
ed_model.lines.pop();
|
||||
}
|
||||
ed_model.caret_pos =
|
||||
update::move_caret_left(ed_model.caret_pos, None, false, &ed_model.lines).0;
|
||||
}
|
||||
}
|
||||
'\u{e000}'..='\u{f8ff}' | '\u{f0000}'..='\u{ffffd}' | '\u{100000}'..='\u{10fffd}' => {
|
||||
// These are private use characters; ignore them.
|
||||
// See http://www.unicode.org/faq/private_use.html
|
||||
}
|
||||
ch if is_newline(ch) => {
|
||||
if let Some(last_line) = ed_model.lines.last_mut() {
|
||||
last_line.push(*received_char)
|
||||
}
|
||||
ed_model.lines.push(String::new());
|
||||
ed_model.caret_pos = Position {
|
||||
line: ed_model.caret_pos.line + 1,
|
||||
column: 0,
|
||||
};
|
||||
|
||||
ed_model.selection_opt = None;
|
||||
}
|
||||
_ => {
|
||||
let nr_lines = ed_model.lines.len();
|
||||
|
||||
if let Some(last_line) = ed_model.lines.last_mut() {
|
||||
last_line.push(*received_char);
|
||||
|
||||
ed_model.caret_pos = Position {
|
||||
line: nr_lines - 1,
|
||||
column: last_line.len(),
|
||||
};
|
||||
|
||||
ed_model.selection_opt = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::error::{EdResult, InvalidSelection};
|
||||
use crate::graphics::colors;
|
||||
use crate::graphics::primitives::rect::Rect;
|
||||
use crate::tea::model::RawSelection;
|
||||
use crate::tea::ed_model::RawSelection;
|
||||
use crate::vec_result::get_res;
|
||||
use bumpalo::collections::Vec as BumpVec;
|
||||
use bumpalo::Bump;
|
||||
@ -72,7 +72,7 @@ pub fn create_selection_rects<'a>(
|
||||
top_left_coords,
|
||||
width,
|
||||
height,
|
||||
color: colors::WHITE,
|
||||
color: colors::SELECT_COLOR,
|
||||
});
|
||||
|
||||
Ok(all_rects)
|
||||
@ -95,7 +95,7 @@ pub fn create_selection_rects<'a>(
|
||||
top_left_coords,
|
||||
width,
|
||||
height,
|
||||
color: colors::WHITE,
|
||||
color: colors::SELECT_COLOR,
|
||||
});
|
||||
|
||||
//middle lines
|
||||
@ -120,7 +120,7 @@ pub fn create_selection_rects<'a>(
|
||||
top_left_coords,
|
||||
width,
|
||||
height,
|
||||
color: colors::WHITE,
|
||||
color: colors::SELECT_COLOR,
|
||||
});
|
||||
}
|
||||
|
||||
@ -143,7 +143,7 @@ pub fn create_selection_rects<'a>(
|
||||
top_left_coords,
|
||||
width,
|
||||
height,
|
||||
color: colors::WHITE,
|
||||
color: colors::SELECT_COLOR,
|
||||
});
|
||||
}
|
||||
|
||||
@ -154,7 +154,7 @@ pub fn create_selection_rects<'a>(
|
||||
#[cfg(test)]
|
||||
mod test_parse {
|
||||
use crate::error::{EdResult, OutOfBounds};
|
||||
use crate::tea::model::{Position, RawSelection};
|
||||
use crate::tea::ed_model::{Position, RawSelection};
|
||||
use crate::tea::update::{move_caret_down, move_caret_left, move_caret_right, move_caret_up};
|
||||
use crate::vec_result::get_res;
|
||||
use core::cmp::Ordering;
|
||||
|
@ -1,20 +1,24 @@
|
||||
use crate::graphics::primitives::rect::Rect;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Model {
|
||||
pub struct EdModel {
|
||||
pub lines: Vec<String>,
|
||||
pub caret_pos: Position,
|
||||
pub selection_opt: Option<RawSelection>,
|
||||
pub glyph_dim_rect_opt: Option<Rect>,
|
||||
}
|
||||
|
||||
pub fn init_model() -> Model {
|
||||
Model {
|
||||
pub fn init_model() -> EdModel {
|
||||
EdModel {
|
||||
lines: vec![String::new()],
|
||||
caret_pos: Position { line: 0, column: 0 },
|
||||
selection_opt: None,
|
||||
glyph_dim_rect_opt: None,
|
||||
}
|
||||
}
|
||||
|
||||
//Is model.rs the right place for these structs?
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct Position {
|
||||
pub line: usize,
|
@ -1,2 +1,2 @@
|
||||
pub mod model;
|
||||
pub mod ed_model;
|
||||
pub mod update;
|
||||
|
@ -1,4 +1,5 @@
|
||||
use crate::tea::model::{Position, RawSelection};
|
||||
use super::ed_model::EdModel;
|
||||
use super::ed_model::{Position, RawSelection};
|
||||
use crate::util::is_newline;
|
||||
use std::cmp::{max, min};
|
||||
|
||||
@ -291,3 +292,53 @@ pub fn move_caret_down(
|
||||
|
||||
(new_caret_pos, new_selection_opt)
|
||||
}
|
||||
|
||||
pub fn update_text_state(ed_model: &mut EdModel, received_char: &char) {
|
||||
ed_model.selection_opt = None;
|
||||
|
||||
match received_char {
|
||||
'\u{8}' | '\u{7f}' => {
|
||||
// In Linux, we get a '\u{8}' when you press backspace,
|
||||
// but in macOS we get '\u{7f}'.
|
||||
if let Some(last_line) = ed_model.lines.last_mut() {
|
||||
if !last_line.is_empty() {
|
||||
last_line.pop();
|
||||
} else if ed_model.lines.len() > 1 {
|
||||
ed_model.lines.pop();
|
||||
}
|
||||
ed_model.caret_pos =
|
||||
move_caret_left(ed_model.caret_pos, None, false, &ed_model.lines).0;
|
||||
}
|
||||
}
|
||||
'\u{e000}'..='\u{f8ff}' | '\u{f0000}'..='\u{ffffd}' | '\u{100000}'..='\u{10fffd}' => {
|
||||
// These are private use characters; ignore them.
|
||||
// See http://www.unicode.org/faq/private_use.html
|
||||
}
|
||||
ch if is_newline(ch) => {
|
||||
if let Some(last_line) = ed_model.lines.last_mut() {
|
||||
last_line.push(*received_char)
|
||||
}
|
||||
ed_model.lines.push(String::new());
|
||||
ed_model.caret_pos = Position {
|
||||
line: ed_model.caret_pos.line + 1,
|
||||
column: 0,
|
||||
};
|
||||
|
||||
ed_model.selection_opt = None;
|
||||
}
|
||||
_ => {
|
||||
let nr_lines = ed_model.lines.len();
|
||||
|
||||
if let Some(last_line) = ed_model.lines.last_mut() {
|
||||
last_line.push(*received_char);
|
||||
|
||||
ed_model.caret_pos = Position {
|
||||
line: nr_lines - 1,
|
||||
column: last_line.len(),
|
||||
};
|
||||
|
||||
ed_model.selection_opt = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user