use crate::settings::Font;
use super::SharedOwnership;
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum FontKind {
Timer,
Times,
Text,
}
impl FontKind {
pub fn is_monospaced(self) -> bool {
self != FontKind::Text
}
}
pub trait ResourceAllocator {
type PathBuilder: PathBuilder<Path = Self::Path>;
type Path: SharedOwnership;
type Image: SharedOwnership;
type Font;
type Label: Label;
fn path_builder(&mut self) -> Self::PathBuilder;
fn build_circle(&mut self, x: f32, y: f32, r: f32) -> Self::Path {
const C: f64 = 0.551915024494;
let c = (C * r as f64) as f32;
let mut builder = self.path_builder();
builder.move_to(x, y - r);
builder.curve_to(x + c, y - r, x + r, y - c, x + r, y);
builder.curve_to(x + r, y + c, x + c, y + r, x, y + r);
builder.curve_to(x - c, y + r, x - r, y + c, x - r, y);
builder.curve_to(x - r, y - c, x - c, y - r, x, y - r);
builder.close();
builder.finish()
}
fn create_image(&mut self, data: &[u8]) -> Option<(Self::Image, f32)>;
fn create_font(&mut self, font: Option<&Font>, kind: FontKind) -> Self::Font;
fn create_label(
&mut self,
text: &str,
font: &mut Self::Font,
max_width: Option<f32>,
) -> Self::Label;
fn update_label(
&mut self,
label: &mut Self::Label,
text: &str,
font: &mut Self::Font,
max_width: Option<f32>,
);
}
pub trait Label: SharedOwnership {
fn width(&self, scale: f32) -> f32;
fn width_without_max_width(&self, scale: f32) -> f32;
}
pub trait PathBuilder {
type Path: SharedOwnership;
fn move_to(&mut self, x: f32, y: f32);
fn line_to(&mut self, x: f32, y: f32);
fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32);
fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32);
fn close(&mut self);
fn finish(self) -> Self::Path;
}
pub struct MutPathBuilder<PB>(PB);
impl<PB: PathBuilder> PathBuilder for MutPathBuilder<PB> {
type Path = PB::Path;
fn move_to(&mut self, x: f32, y: f32) {
self.0.move_to(x, y)
}
fn line_to(&mut self, x: f32, y: f32) {
self.0.line_to(x, y)
}
fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
self.0.quad_to(x1, y1, x, y)
}
fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
self.0.curve_to(x1, y1, x2, y2, x, y)
}
fn close(&mut self) {
self.0.close()
}
fn finish(self) -> Self::Path {
self.0.finish()
}
}
impl<A: ResourceAllocator> ResourceAllocator for &mut A {
type PathBuilder = MutPathBuilder<A::PathBuilder>;
type Path = A::Path;
type Image = A::Image;
type Font = A::Font;
type Label = A::Label;
fn path_builder(&mut self) -> Self::PathBuilder {
MutPathBuilder((*self).path_builder())
}
fn create_image(&mut self, data: &[u8]) -> Option<(Self::Image, f32)> {
(*self).create_image(data)
}
fn create_font(&mut self, font: Option<&Font>, kind: FontKind) -> Self::Font {
(*self).create_font(font, kind)
}
fn create_label(
&mut self,
text: &str,
font: &mut Self::Font,
max_width: Option<f32>,
) -> Self::Label {
(*self).create_label(text, font, max_width)
}
fn update_label(
&mut self,
label: &mut Self::Label,
text: &str,
font: &mut Self::Font,
max_width: Option<f32>,
) {
(*self).update_label(label, text, font, max_width)
}
}