Improve the Text and line APIs

This commit is contained in:
Savanni D'Gerinel 2024-10-04 20:56:37 -04:00
parent de35ebb644
commit 9c56e988b2
2 changed files with 64 additions and 31 deletions

View File

@ -1,6 +1,6 @@
use std::{fs::File, io::Read, path::Path, rc::Rc, time::Duration}; use std::{fs::File, io::Read, path::Path, rc::Rc, time::Duration};
use cyberpunk::{AsymLine, GlowPen, Pen, Text}; use cyberpunk::{AsymLine, AsymLineCutout, GlowPen, Pen, Text};
use glib::Object; use glib::Object;
use gtk::{glib, prelude::*, subclass::prelude::*}; use gtk::{glib, prelude::*, subclass::prelude::*};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -87,17 +87,33 @@ impl CyberScreen {
start_y: height as f64 / 2., start_y: height as f64 / 2.,
start_length: 75., start_length: 75.,
height: 50., height: 50.,
total_length: 200., end_length: 75.,
invert: false, invert: false,
} }
.draw(&pen); .draw(&pen);
pen.stroke(); pen.stroke();
AsymLineCutout {
orientation: gtk::Orientation::Horizontal,
start_x: 50.,
start_y: height as f64 / 4. * 3.,
start_length: 75.,
cutout_length: 15.,
end_length: 75.,
height: 15.,
invert: true,
}.draw(&pen);
pen.stroke();
let tracery = pen.finish(); let tracery = pen.finish();
let _ = context.set_source(tracery); let _ = context.set_source(tracery);
let _ = context.paint(); let _ = context.paint();
Text::new("Test text".to_owned(), (0.7, 0., 1.)).draw(&context); let text = Text::new("Test text".to_owned(), &context, 64.);
let text_extents = text.extents();
context.move_to(20., text_extents.height() + 40.);
context.set_source_rgb(0.7, 0., 1.);
text.draw();
} }
}); });

View File

@ -10,19 +10,19 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
struct AsymLineCutout { pub struct AsymLineCutout {
orientation: gtk::Orientation, pub orientation: gtk::Orientation,
start_x: f64, pub start_x: f64,
start_y: f64, pub start_y: f64,
start_length: f64, pub start_length: f64,
total_length: f64, pub cutout_length: f64,
cutout_length: f64, pub end_length: f64,
height: f64, pub height: f64,
invert: bool, pub invert: bool,
} }
impl AsymLineCutout { impl AsymLineCutout {
fn draw(&self, pen: &impl Pen) { pub fn draw(&self, pen: &impl Pen) {
let dodge = if self.invert { let dodge = if self.invert {
self.height self.height
} else { } else {
@ -48,7 +48,15 @@ impl AsymLineCutout {
+ (self.height / 2.), + (self.height / 2.),
self.start_y + dodge / 2., self.start_y + dodge / 2.,
); );
pen.line_to(self.total_length, self.start_y + dodge / 2.); pen.line_to(
self.start_x
+ self.start_length
+ self.height
+ self.cutout_length
+ (self.height / 2.)
+ self.end_length,
self.start_y + dodge / 2.,
);
} }
gtk::Orientation::Vertical => { gtk::Orientation::Vertical => {
pen.move_to(self.start_x, self.start_y); pen.move_to(self.start_x, self.start_y);
@ -69,7 +77,15 @@ impl AsymLineCutout {
+ self.cutout_length + self.cutout_length
+ (self.height / 2.), + (self.height / 2.),
); );
pen.line_to(self.start_x + dodge / 2., self.total_length); pen.line_to(
self.start_x + dodge / 2.,
self.start_y
+ self.start_length
+ self.height
+ self.cutout_length
+ (self.height / 2.)
+ self.end_length,
);
} }
_ => panic!("unknown orientation"), _ => panic!("unknown orientation"),
} }
@ -93,7 +109,7 @@ pub struct AsymLine {
pub height: f64, pub height: f64,
// Total length of the entire line. // Total length of the entire line.
pub total_length: f64, pub end_length: f64,
// When normal, the angle dodge is upwards. When inverted, the angle dodge is downwards. // When normal, the angle dodge is upwards. When inverted, the angle dodge is downwards.
pub invert: bool, pub invert: bool,
@ -114,7 +130,10 @@ impl AsymLine {
self.start_x + self.start_length + self.height, self.start_x + self.start_length + self.height,
self.start_y + dodge, self.start_y + dodge,
); );
pen.line_to(self.start_x + self.total_length, self.start_y + dodge); pen.line_to(
self.start_x + self.start_length + self.height + self.end_length,
self.start_y + dodge,
);
} }
gtk::Orientation::Vertical => {} gtk::Orientation::Vertical => {}
_ => panic!("unknown orientation"), _ => panic!("unknown orientation"),
@ -238,25 +257,23 @@ impl Pen for GlowPen {
} }
} }
pub struct Text { pub struct Text<'a> {
content: String, content: String,
color: (f64, f64, f64), context: &'a Context,
} }
impl Text { impl<'a> Text<'a> {
pub fn new(content: String, color: (f64, f64, f64)) -> Self { pub fn new(content: String, context: &'a Context, size: f64) -> Self {
Self{ content, color }
}
pub fn draw(&self, context: &Context) {
context.select_font_face("Alegreya Sans SC", FontSlant::Normal, FontWeight::Bold); context.select_font_face("Alegreya Sans SC", FontSlant::Normal, FontWeight::Bold);
context.set_font_size(64.); context.set_font_size(size);
Self { content, context }
}
let extents = context.text_extents(&self.content).unwrap(); pub fn extents(&self) -> TextExtents {
context.move_to(20., extents.height() + 20.); self.context.text_extents(&self.content).unwrap()
}
context.set_source_rgb(self.color.0, self.color.1, self.color.2); pub fn draw(&self) {
let _ = context.show_text(&self.content); let _ = self.context.show_text(&self.content);
} }
} }