Compare commits

..

No commits in common. '32e9ccc97a137f607efccf21e2890cb9a5a22012' and '749eb316160ecc41aa5101376a074592d1c1a870' have entirely different histories.

  1. 1
      Cargo.toml
  2. 64
      src/editor_render.rs
  3. 30
      src/main.rs

@ -8,5 +8,4 @@ edition = "2021"
opt-level = 3 opt-level = 3
[dependencies] [dependencies]
clipboard = "0.5.0"
sdl2 = { version = "0.35.2" } sdl2 = { version = "0.35.2" }

@ -7,17 +7,9 @@ use std::{fs::File, path::Path, io::Read};
use sdl2::rect::Point; use sdl2::rect::Point;
pub struct GlyphMetrics { const GLYPH_WIDTH: usize = 8;
width: usize, const GLYPH_HEIGHT: usize = 14;
height: usize, const GLYPH_AREA: usize = GLYPH_WIDTH * GLYPH_HEIGHT;
}
impl GlyphMetrics {
fn area(&self) -> usize {
self.width * self.height
}
}
type Glyph = Vec<Point>; type Glyph = Vec<Point>;
/// Reads the file and turns it into a Vec of u8s /// Reads the file and turns it into a Vec of u8s
@ -35,20 +27,16 @@ fn read_file(file_name: String) -> Vec<u8> {
file_content file_content
} }
pub fn generate_glyph_data() -> (Vec<Glyph>, GlyphMetrics) { pub fn generate_glyph_atlas() -> Vec<Glyph> {
// Retrieve font data from file // Retrieve font data from file
let file_path = String::from("./fonts/Terminus14x8.data"); let file_path = String::from("./fonts/Terminus14x8.data");
let contents = read_file(file_path); let contents = read_file(file_path);
// Get glyph metrics
let glyph_metrics = GlyphMetrics { width: 8, height: 16 };
let glyph_width = glyph_metrics.width;
// Get width of image for proper positioning of pixels // Get width of image for proper positioning of pixels
let width_left_byte = contents[0]; let width_left_byte = contents[0];
let width_right_byte = contents[1]; let width_right_byte = contents[1];
let width_bytes = [width_left_byte, width_right_byte]; let number = [width_left_byte, width_right_byte];
let width = u16::from_be_bytes(width_bytes); let width = u16::from_be_bytes(number);
println!("Left Byte: {width_left_byte}, Right Byte: {width_right_byte}, Byte Pair: {width}"); println!("Left Byte: {width_left_byte}, Right Byte: {width_right_byte}, Byte Pair: {width}");
let gtable_prune = &contents[width as usize + 2 ..]; let gtable_prune = &contents[width as usize + 2 ..];
@ -58,14 +46,13 @@ pub fn generate_glyph_data() -> (Vec<Glyph>, GlyphMetrics) {
for glyph in 0..96 { for glyph in 0..96 {
let mut new_glyph: Glyph = vec![]; let mut new_glyph: Glyph = vec![];
let glyph_area = glyph_metrics.area(); for p in 0..GLYPH_AREA as u16 {
for p in 0..glyph_area as u16 { let x = p % GLYPH_WIDTH as u16;
let x = p % glyph_width as u16; let y = p / GLYPH_WIDTH as u16;
let y = p / glyph_width as u16;
let multiplier = y * width; let multiplier = y * width;
let offset = glyph * glyph_width as u16; let offset = glyph * GLYPH_WIDTH as u16;
let position = (x + multiplier + offset) as usize; let position = (x as u16 + multiplier + offset) as usize;
if gtable_prune[position] == 1 { if gtable_prune[position] == 1 {
new_glyph.push(Point::new(x as i32, y as i32)); new_glyph.push(Point::new(x as i32, y as i32));
@ -73,16 +60,13 @@ pub fn generate_glyph_data() -> (Vec<Glyph>, GlyphMetrics) {
} }
glyph_atlas.push(new_glyph); glyph_atlas.push(new_glyph);
} }
(glyph_atlas, glyph_metrics) glyph_atlas
} }
/// Method for generating points to render, using given string /// Method for generating points to render, using given string
pub fn draw_text(glyph_atlas: &Vec<Glyph>, glyph_metrics: &GlyphMetrics, content: &str, offset: Point) -> Vec<Point> { pub fn draw_text(content: &str, glyph_atlas: Vec<Glyph>) -> Vec<Point> {
let mut points: Vec<Point> = vec![]; let mut points: Vec<Point> = vec![];
let glyph_width = glyph_metrics.width;
let glyph_height = glyph_metrics.height;
let lines = content.split('\n'); let lines = content.split('\n');
for (y, chars) in lines.enumerate() { for (y, chars) in lines.enumerate() {
for (x, chara) in chars.chars().enumerate() { for (x, chara) in chars.chars().enumerate() {
@ -94,12 +78,12 @@ pub fn draw_text(glyph_atlas: &Vec<Glyph>, glyph_metrics: &GlyphMetrics, content
} }
for pixel in &glyph_atlas[index - 32] { for pixel in &glyph_atlas[index - 32] {
let x_glyph = x * glyph_width; let x_offset = x * GLYPH_WIDTH;
let y_glyph = y * glyph_height; let y_offset = y * GLYPH_HEIGHT;
let positioned_pixel = Point::new( let positioned_pixel = Point::new(
pixel.x + x_glyph as i32 + offset.x, pixel.x + x_offset as i32,
pixel.y + y_glyph as i32 + offset.y pixel.y + y_offset as i32,
); );
points.push(positioned_pixel); points.push(positioned_pixel);
} }
@ -108,12 +92,10 @@ pub fn draw_text(glyph_atlas: &Vec<Glyph>, glyph_metrics: &GlyphMetrics, content
points points
} }
pub fn draw_cursor(glyph_metrics: &GlyphMetrics, mut cursor_position: usize, content: &str, offset: Point) -> (Point, Point) { pub fn draw_cursor(content: &str, mut cursor_position: usize) -> (Point, Point) {
let glyph_width = glyph_metrics.width;
let glyph_height = glyph_metrics.height;
let mut x = 0; let mut x = 0;
let mut y = 0; let mut y = 0;
if cursor_position > 0 { if cursor_position > 0 {
cursor_position = cursor_position.checked_sub(1).unwrap_or(0); cursor_position = cursor_position.checked_sub(1).unwrap_or(0);
for (idx, chara) in content.chars().enumerate() { for (idx, chara) in content.chars().enumerate() {
@ -124,15 +106,15 @@ pub fn draw_cursor(glyph_metrics: &GlyphMetrics, mut cursor_position: usize, con
y += 1; y += 1;
} }
if idx == cursor_position { if idx == cursor_position {
let point_a = Point::new((x * glyph_width) as i32 + offset.x, let point_a = Point::new((x * GLYPH_WIDTH) as i32,
(y * glyph_height) as i32 + offset.y (y * GLYPH_HEIGHT) as i32
); );
let point_b = Point::new(point_a.x, let point_b = Point::new(point_a.x,
point_a.y + glyph_height as i32 point_a.y + GLYPH_HEIGHT as i32
); );
return (point_a, point_b) return (point_a, point_b)
} }
} }
} }
(Point::new(offset.x, offset.y), Point::new(offset.x, offset.y + glyph_height as i32)) (Point::new(0, 0), Point::new(0, GLYPH_HEIGHT as i32))
} }

@ -1,10 +1,8 @@
extern crate sdl2; extern crate sdl2;
use clipboard::{ClipboardProvider, ClipboardContext};
use sdl2::event::Event; use sdl2::event::Event;
use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::rect::Point;
mod editor_render; mod editor_render;
@ -18,8 +16,7 @@ struct ModifierKeys {
} }
pub fn main() -> Result<(), String> { pub fn main() -> Result<(), String> {
let mut clipboard_context: ClipboardContext = ClipboardProvider::new().unwrap(); let glyph_atlas = editor_render::generate_glyph_atlas();
let (glyph_atlas, glyph_metrics) = editor_render::generate_glyph_data();
let sdl_context = sdl2::init()?; let sdl_context = sdl2::init()?;
let video_subsys = sdl_context.video()?; let video_subsys = sdl_context.video()?;
@ -37,8 +34,6 @@ pub fn main() -> Result<(), String> {
let mut cursor_position = 0; let mut cursor_position = 0;
let mut selection_anchor: Option<usize> = None; let mut selection_anchor: Option<usize> = None;
let pad_offset = Point::new(10, 10);
let mut draw_text = |text: &str, pos: usize| -> Result<(), String> { let mut draw_text = |text: &str, pos: usize| -> Result<(), String> {
// Draw background // Draw background
canvas.set_draw_color(Color::RGB(32, 32, 32)); canvas.set_draw_color(Color::RGB(32, 32, 32));
@ -46,22 +41,12 @@ pub fn main() -> Result<(), String> {
// Draw text // Draw text
canvas.set_draw_color(Color::RGB(240, 240, 240)); canvas.set_draw_color(Color::RGB(240, 240, 240));
let fb_text = editor_render::draw_text( let fb_text = editor_render::draw_text(text, glyph_atlas.clone());
&glyph_atlas,
&glyph_metrics,
text,
pad_offset
);
canvas.draw_points(&fb_text[..])?; canvas.draw_points(&fb_text[..])?;
// Draw cursor // Draw cursor
canvas.set_draw_color(Color::RGB(64, 240, 240)); canvas.set_draw_color(Color::RGB(64, 240, 240));
let fb_cursor = editor_render::draw_cursor( let fb_cursor = editor_render::draw_cursor(text, pos);
&glyph_metrics,
pos,
text,
pad_offset
);
canvas.draw_line(fb_cursor.0, fb_cursor.1)?; canvas.draw_line(fb_cursor.0, fb_cursor.1)?;
canvas.present(); canvas.present();
@ -117,7 +102,6 @@ pub fn main() -> Result<(), String> {
}; };
match (modifier_keys.shift, modifier_keys.ctrl, modifier_keys.alt) { match (modifier_keys.shift, modifier_keys.ctrl, modifier_keys.alt) {
// All modifiers up
(false, false, false) => { (false, false, false) => {
match keycode { match keycode {
// DELETE key // DELETE key
@ -150,7 +134,6 @@ pub fn main() -> Result<(), String> {
// Left/Back arrow // Left/Back arrow
Some(Keycode::Left) => { Some(Keycode::Left) => {
selection_anchor = None;
cursor_position = usize::checked_sub(cursor_position, 1) cursor_position = usize::checked_sub(cursor_position, 1)
.unwrap_or(0); .unwrap_or(0);
draw_text(&buffer, cursor_position)? draw_text(&buffer, cursor_position)?
@ -158,7 +141,6 @@ pub fn main() -> Result<(), String> {
// Right/Forward arrow // Right/Forward arrow
Some(Keycode::Right) => { Some(Keycode::Right) => {
selection_anchor = None;
cursor_position = (cursor_position + 1).min(buffer.len()); cursor_position = (cursor_position + 1).min(buffer.len());
draw_text(&buffer, cursor_position)? draw_text(&buffer, cursor_position)?
}, },
@ -177,14 +159,11 @@ pub fn main() -> Result<(), String> {
} }
}, },
// CTRL down
(false, true, false) => { (false, true, false) => {
match keycode { match keycode {
Some(Keycode::Z) => println!("Undo"), Some(Keycode::Z) => println!("Undo"),
Some(Keycode::X) => println!("Cut"), Some(Keycode::X) => println!("Cut"),
Some(Keycode::C) => { Some(Keycode::C) => println!("Copy"),
clipboard_context.set_contents(buffer.clone()).unwrap()
},
Some(Keycode::V) => println!("Paste"), Some(Keycode::V) => println!("Paste"),
// BACKSPACE key // BACKSPACE key
@ -235,7 +214,6 @@ pub fn main() -> Result<(), String> {
} }
} }
} }
format!("{selection_anchor:?}");
println!("{buffer}"); println!("{buffer}");
Ok(()) Ok(())
} }

Loading…
Cancel
Save