use std::ops::Range;
use wgpu::{util::BufferInitDescriptor, PrimitiveTopology};
use crate::{
core::components::{
material::Material,
ui::{font::Font, ui_image::UiImage},
},
rendering::{Renderable2D, RenderableUi},
};
use crate::core::components::color::Color;
use crate::core::components::maths::padding::Padding;
use crate::core::resources::asset_manager::AssetRef;
use crate::core::world::Resources;
pub struct UiText {
text: String,
font_ref: AssetRef<Font>,
font_size: usize,
font_color: Option<Color>,
padding: Padding,
pub(crate) dirty: bool,
pub(crate) sync_fn: Option<fn(&mut Resources) -> String>
}
impl UiText {
pub fn new(text: String, font_ref: AssetRef<Font>) -> Self {
Self { text, font_ref, dirty: true, font_size: 10, font_color: None, sync_fn: None, padding: Padding::default() }
}
pub fn sync_value(mut self, sync_function: fn(&mut Resources) -> String) -> Self
{
self.sync_fn = Some(sync_function);
self
}
pub fn text(&self) -> &String {
&self.text
}
pub fn font_size(&self) -> usize {
self.font_size
}
pub fn padding(&self) -> &Padding {
&self.padding
}
pub fn font_color(&self) -> &Option<Color> {
&self.font_color
}
pub fn set_text(&mut self, text: String) {
if text.ne(&self.text) {
self.text = text;
self.dirty = true;
}
}
pub fn set_padding(&mut self, padding: Padding) {
self.padding = padding;
}
pub fn font_ref(&self) -> &AssetRef<Font> {
&self.font_ref
}
pub fn with_font_size(mut self, font_size: usize) -> Self{
self.font_size = font_size;
self
}
pub fn with_font_color(mut self, color: Color) -> Self{
self.font_color = Some(color);
self
}
}
#[derive(Debug)]
pub(crate) struct UiTextImage(pub(crate)
UiImage);
impl Renderable2D for UiTextImage {
fn vertex_buffer_descriptor(&mut self, material: Option<&Material>) -> BufferInitDescriptor {
self.0.vertex_buffer_descriptor(material)
}
fn indexes_buffer_descriptor(&self) -> BufferInitDescriptor {
self.0.indexes_buffer_descriptor()
}
fn range(&self) -> Range<u32> {
self.0.range()
}
fn topology() -> PrimitiveTopology {
wgpu::PrimitiveTopology::TriangleList
}
fn dirty(&self) -> bool {
false
}
fn set_dirty(&mut self, _is_dirty: bool) {}
}
impl RenderableUi for UiTextImage {}