libcosmic/examples/text/src/main.rs

322 lines
12 KiB
Rust
Raw Normal View History

2022-10-03 11:45:46 -06:00
use orbclient::{Color, EventOption, Renderer, Window, WindowFlag};
2022-10-03 13:13:35 -06:00
use std::{
cmp,
2022-10-04 15:04:22 -06:00
env,
fs,
marker::PhantomData,
2022-10-03 13:13:35 -06:00
time::Instant,
};
2022-10-05 09:16:51 -06:00
use text::{
Font,
FontSystem,
};
2022-10-03 11:45:46 -06:00
fn main() {
2022-10-04 16:02:46 -06:00
let display_scale = match orbclient::get_display_size() {
Ok((w, h)) => {
println!("Display size: {}, {}", w, h);
(h as i32 / 1600) + 1
},
Err(err) => {
println!("Failed to get display size: {}", err);
1
}
};
2022-10-03 11:45:46 -06:00
let mut window = Window::new_flags(
2022-10-04 16:02:46 -06:00
-1,
-1,
1024 * display_scale as u32,
768 * display_scale as u32,
2022-10-03 11:45:46 -06:00
"COSMIC TEXT",
&[WindowFlag::Resizable]
).unwrap();
#[cfg(feature = "mono")]
2022-10-04 15:04:22 -06:00
let default_text = include_str!("../res/mono.txt");
#[cfg(not(feature = "mono"))]
2022-10-04 15:04:22 -06:00
let default_text = include_str!("../res/proportional.txt");
2022-10-05 08:10:35 -06:00
let mut text = if let Some(arg) = env::args().nth(1) {
2022-10-04 15:04:22 -06:00
fs::read_to_string(&arg).expect("failed to open file")
} else {
default_text.to_string()
};
2022-10-03 11:45:46 -06:00
let bg_color = Color::rgb(0x34, 0x34, 0x34);
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
let font_sizes = [
(10, 14), // Caption
(14, 20), // Body
(20, 28), // Title 4
(24, 32), // Title 3
(28, 36), // Title 2
(32, 44), // Title 1
2022-10-03 11:45:46 -06:00
];
let font_size_default = 2; // Title 4
let mut font_size_i = font_size_default;
let mut font_system = FontSystem::new();
font_system.add(
Font::new(include_bytes!("../../../res/Fira/FiraSans-Regular.otf"), 0).unwrap()
);
font_system.add(
Font::new(include_bytes!("../../../res/Fira/FiraMono-Regular.otf"), 0).unwrap()
);
font_system.add(
Font::new(include_bytes!("../../../res/FreeFont/FreeSans.ttf"), 0).unwrap()
);
font_system.add(
Font::new(include_bytes!("../../../res/FreeFont/FreeSerif.ttf"), 0).unwrap()
);
font_system.add(
Font::new(include_bytes!("../../../res/FreeFont/FreeMono.ttf"), 0).unwrap()
);
#[cfg(feature = "mono")]
let font_pattern = &["Mono"];
#[cfg(not(feature = "mono"))]
let font_pattern = &["Sans", "Serif"];
let font_matches = font_system.matches(font_pattern).unwrap();
2022-10-03 11:45:46 -06:00
2022-10-05 08:10:35 -06:00
let mut shape_lines = Vec::new();
let mut layout_lines = Vec::new();
2022-10-04 20:58:59 -06:00
let mut cursor_line = 0;
let mut cursor_glyph = 0;
let mut mouse_x = -1;
let mut mouse_y = -1;
2022-10-04 15:04:22 -06:00
let mut mouse_left = false;
2022-10-03 11:45:46 -06:00
let mut redraw = true;
let mut relayout = true;
2022-10-05 08:10:35 -06:00
let mut reshape = true;
2022-10-03 11:45:46 -06:00
let mut scroll = 0;
loop {
2022-10-04 16:02:46 -06:00
let (mut font_size, mut line_height) = font_sizes[font_size_i];
font_size *= display_scale;
line_height *= display_scale;
2022-10-03 11:45:46 -06:00
2022-10-05 08:10:35 -06:00
if reshape {
let instant = Instant::now();
shape_lines = font_matches.shape(&text);
reshape = false;
relayout = true;
let duration = instant.elapsed();
println!("reshape: {:?}", duration);
}
if relayout {
2022-10-03 13:13:35 -06:00
let instant = Instant::now();
2022-10-03 11:45:46 -06:00
layout_lines.clear();
for line in shape_lines.iter() {
2022-10-04 20:58:59 -06:00
let line_width = window.width() as i32 - 16 * display_scale;
2022-10-04 15:04:22 -06:00
line.layout(font_size, line_width, &mut layout_lines);
}
relayout = false;
2022-10-05 08:10:35 -06:00
redraw = true;
2022-10-03 13:13:35 -06:00
let duration = instant.elapsed();
println!("relayout: {:?}", duration);
}
if redraw {
2022-10-03 13:13:35 -06:00
let instant = Instant::now();
window.set(bg_color);
let window_lines = (window.height() as i32 + line_height - 1) / line_height;
scroll = cmp::max(0, cmp::min(
layout_lines.len() as i32 - (window_lines - 1),
scroll
));
2022-10-04 20:58:59 -06:00
for &hitbox in &[true, false] {
let line_x = 8 * display_scale;
let mut line_y = line_height;
for (line_i, line) in layout_lines.iter().skip(scroll as usize).enumerate() {
if line_y >= window.height() as i32 {
break;
}
2022-10-04 15:04:22 -06:00
2022-10-04 20:58:59 -06:00
if hitbox {
if mouse_left
&& mouse_y >= line_y - font_size
&& mouse_y < line_y - font_size + line_height
{
2022-10-04 21:32:26 -06:00
cursor_line = line_i + scroll as usize;
cursor_glyph = line.glyphs.len();
2022-10-04 20:58:59 -06:00
for (glyph_i, glyph) in line.glyphs.iter().enumerate() {
if mouse_x >= line_x + glyph.x as i32
&& mouse_x <= line_x + (glyph.x + glyph.w) as i32
{
cursor_glyph = glyph_i;
}
}
}
} else {
if cursor_line == line_i + scroll as usize {
2022-10-04 21:32:26 -06:00
if cursor_glyph >= line.glyphs.len() {
let x = match line.glyphs.last() {
Some(glyph) => glyph.x + glyph.w,
None => 0.0
};
window.rect(
line_x + x as i32,
line_y - font_size,
(font_size / 2) as u32,
line_height as u32,
Color::rgba(0xFF, 0xFF, 0xFF, 0x20)
);
} else {
let glyph = &line.glyphs[cursor_glyph];
2022-10-04 15:04:22 -06:00
window.rect(
2022-10-04 20:58:59 -06:00
line_x + glyph.x as i32,
2022-10-04 15:04:22 -06:00
line_y - font_size,
2022-10-04 20:58:59 -06:00
glyph.w as u32,
2022-10-04 15:04:22 -06:00
line_height as u32,
Color::rgba(0xFF, 0xFF, 0xFF, 0x20)
);
println!("{}, {}: '{}'", glyph.start, glyph.end, &text[glyph.start..glyph.end]);
}
}
2022-10-05 09:16:51 -06:00
line.draw(|x, y, v| {
let c = (v * 255.0) as u32;
window.pixel(line_x + x, line_y + y, Color{
data: c << 24 | (font_color.data & 0x00FF_FFFF)
});
});
2022-10-04 20:58:59 -06:00
}
2022-10-03 11:45:46 -06:00
2022-10-04 20:58:59 -06:00
line_y += line_height;
}
2022-10-03 11:45:46 -06:00
}
window.sync();
redraw = false;
2022-10-03 13:13:35 -06:00
let duration = instant.elapsed();
println!("redraw: {:?}", duration);
2022-10-03 11:45:46 -06:00
}
for event in window.events() {
match event.to_option() {
EventOption::Key(event) => if event.pressed {
match event.scancode {
2022-10-04 20:58:59 -06:00
orbclient::K_UP => if cursor_line > 0 {
cursor_line -= 1;
redraw = true;
},
orbclient::K_DOWN => if cursor_line + 1 < layout_lines.len() {
cursor_line += 1;
redraw = true;
},
orbclient::K_LEFT => {
2022-10-05 08:10:35 -06:00
let line = &layout_lines[cursor_line];
if cursor_glyph > line.glyphs.len() {
cursor_glyph = line.glyphs.len();
2022-10-04 20:58:59 -06:00
redraw = true;
}
if cursor_glyph > 0 {
cursor_glyph -= 1;
redraw = true;
}
},
orbclient::K_RIGHT => {
2022-10-05 08:10:35 -06:00
let line = &layout_lines[cursor_line];
if cursor_glyph > line.glyphs.len() {
cursor_glyph = line.glyphs.len();
2022-10-04 20:58:59 -06:00
redraw = true;
}
2022-10-05 08:10:35 -06:00
if cursor_glyph < line.glyphs.len() {
2022-10-04 20:58:59 -06:00
cursor_glyph += 1;
redraw = true;
}
},
2022-10-05 08:10:35 -06:00
orbclient::K_BKSP => {
let line = &layout_lines[cursor_line];
if cursor_glyph > line.glyphs.len() {
cursor_glyph = line.glyphs.len();
redraw = true;
}
if cursor_glyph > 0 {
cursor_glyph -= 1;
text.remove(line.glyphs[cursor_glyph].start);
reshape = true;
}
},
orbclient::K_DEL => {
let line = &layout_lines[cursor_line];
if cursor_glyph < line.glyphs.len() {
text.remove(line.glyphs[cursor_glyph].start);
reshape = true;
}
},
2022-10-03 11:45:46 -06:00
orbclient::K_0 => {
font_size_i = font_size_default;
relayout = true;
2022-10-03 11:45:46 -06:00
},
orbclient::K_MINUS => if font_size_i > 0 {
font_size_i -= 1;
relayout = true;
2022-10-03 11:45:46 -06:00
},
orbclient::K_EQUALS => if font_size_i + 1 < font_sizes.len() {
font_size_i += 1;
relayout = true;
2022-10-03 11:45:46 -06:00
},
_ => (),
}
},
2022-10-05 08:10:35 -06:00
EventOption::TextInput(event) => {
let line = &layout_lines[cursor_line];
if cursor_glyph >= line.glyphs.len() {
match line.glyphs.last() {
Some(glyph) => {
text.insert(glyph.end, event.character);
cursor_glyph += 1;
reshape = true;
},
None => () // TODO
}
} else {
let glyph = &line.glyphs[cursor_glyph];
text.insert(glyph.start, event.character);
cursor_glyph += 1;
reshape = true;
}
},
EventOption::Mouse(event) => {
mouse_x = event.x;
mouse_y = event.y;
2022-10-04 15:04:22 -06:00
if mouse_left {
redraw = true;
}
},
2022-10-04 15:04:22 -06:00
EventOption::Button(event) => {
if event.left != mouse_left {
mouse_left = event.left;
if mouse_left {
redraw = true;
}
}
}
2022-10-03 11:45:46 -06:00
EventOption::Resize(_) => {
relayout = true;
2022-10-03 11:45:46 -06:00
},
EventOption::Scroll(event) => {
scroll -= event.y * 3;
redraw = true;
},
EventOption::Quit(_) => return,
_ => (),
}
}
}
}