text and cursor offset

type-render
korin 3 years ago
parent 749eb31616
commit 85301aa13b
  1. 1
      Cargo.toml
  2. 21
      src/editor_render.rs
  3. 25
      src/main.rs

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

@ -10,6 +10,7 @@ use sdl2::rect::Point;
const GLYPH_WIDTH: usize = 8;
const GLYPH_HEIGHT: usize = 14;
const GLYPH_AREA: usize = GLYPH_WIDTH * GLYPH_HEIGHT;
type Glyph = Vec<Point>;
/// Reads the file and turns it into a Vec of u8s
@ -52,7 +53,7 @@ pub fn generate_glyph_atlas() -> Vec<Glyph> {
let multiplier = y * width;
let offset = glyph * GLYPH_WIDTH as u16;
let position = (x as u16 + multiplier + offset) as usize;
let position = (x + multiplier + offset) as usize;
if gtable_prune[position] == 1 {
new_glyph.push(Point::new(x as i32, y as i32));
@ -64,7 +65,7 @@ pub fn generate_glyph_atlas() -> Vec<Glyph> {
}
/// Method for generating points to render, using given string
pub fn draw_text(content: &str, glyph_atlas: Vec<Glyph>) -> Vec<Point> {
pub fn draw_text(glyph_atlas: &Vec<Glyph>, content: &str, offset: Point) -> Vec<Point> {
let mut points: Vec<Point> = vec![];
let lines = content.split('\n');
@ -78,12 +79,12 @@ pub fn draw_text(content: &str, glyph_atlas: Vec<Glyph>) -> Vec<Point> {
}
for pixel in &glyph_atlas[index - 32] {
let x_offset = x * GLYPH_WIDTH;
let y_offset = y * GLYPH_HEIGHT;
let x_glyph = x * GLYPH_WIDTH;
let y_glyph = y * GLYPH_HEIGHT;
let positioned_pixel = Point::new(
pixel.x + x_offset as i32,
pixel.y + y_offset as i32,
pixel.x + x_glyph as i32 + offset.x,
pixel.y + y_glyph as i32 + offset.y
);
points.push(positioned_pixel);
}
@ -92,7 +93,7 @@ pub fn draw_text(content: &str, glyph_atlas: Vec<Glyph>) -> Vec<Point> {
points
}
pub fn draw_cursor(content: &str, mut cursor_position: usize) -> (Point, Point) {
pub fn draw_cursor(mut cursor_position: usize, content: &str, offset: Point) -> (Point, Point) {
let mut x = 0;
let mut y = 0;
@ -106,8 +107,8 @@ pub fn draw_cursor(content: &str, mut cursor_position: usize) -> (Point, Point)
y += 1;
}
if idx == cursor_position {
let point_a = Point::new((x * GLYPH_WIDTH) as i32,
(y * GLYPH_HEIGHT) as i32
let point_a = Point::new((x * GLYPH_WIDTH) as i32 + offset.x,
(y * GLYPH_HEIGHT) as i32 + offset.y
);
let point_b = Point::new(point_a.x,
point_a.y + GLYPH_HEIGHT as i32
@ -116,5 +117,5 @@ pub fn draw_cursor(content: &str, mut cursor_position: usize) -> (Point, Point)
}
}
}
(Point::new(0, 0), Point::new(0, GLYPH_HEIGHT as i32))
(Point::new(offset.x, offset.y), Point::new(offset.x, offset.y + GLYPH_HEIGHT as i32))
}

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

Loading…
Cancel
Save