ui/src/elements/label.rs

308 lines
7.7 KiB
Rust
Raw Normal View History

2024-04-26 06:28:05 +00:00
use crate::{
2025-02-27 09:43:07 +00:00
builder::validator::labelinfo::LabelInfo, gui_handler::gui::iconizable::IconizablePositioning,
2024-04-26 06:28:05 +00:00
prelude::*,
};
use super::{
fill_type::FillType,
wrapper::{IconizableWrapper, TextableWrapper},
};
2023-01-16 09:53:52 +00:00
2025-04-10 05:58:35 +00:00
use ecs::Commands;
2024-04-26 06:28:05 +00:00
use vulkan_rs::prelude::*;
2023-01-16 09:53:52 +00:00
use std::sync::{
Arc, RwLock,
2025-03-04 10:37:45 +00:00
atomic::{AtomicBool, Ordering::SeqCst},
2023-01-16 09:53:52 +00:00
};
use anyhow::Result;
pub struct LabelBuilder {
text_alignment: TextAlignment,
text_ratio: f32,
text_color: Color,
text: Option<String>,
background: Option<FillTypeInfo>,
}
impl LabelBuilder {
pub fn set_text(mut self, text: impl Into<String>) -> Self {
self.text = Some(text.into());
self
}
pub fn set_ratio(mut self, ratio: f32) -> Self {
self.text_ratio = ratio;
self
}
pub fn set_text_color(mut self, text_color: impl Into<Color>) -> Self {
self.text_color = text_color.into();
self
}
pub fn set_text_alignment(mut self, text_alignment: TextAlignment) -> Self {
self.text_alignment = text_alignment;
self
}
pub fn set_background(mut self, background: impl Into<FillTypeInfo>) -> Self {
self.background = Some(background.into());
self
}
2025-03-04 11:25:02 +00:00
pub fn build(self, gui_handler: &mut GuiHandler) -> Result<Arc<Label>> {
2025-03-04 10:37:45 +00:00
let framable = Framable::new(gui_handler.width(), gui_handler.height(), false)?;
2023-01-16 09:53:52 +00:00
let textable_wrapper = TextableWrapper::new(
framable.clone(),
self.text_color,
self.text_ratio,
self.text_alignment,
);
if let Some(text) = &self.text {
2025-03-04 10:37:45 +00:00
textable_wrapper.set_text(gui_handler, text, false)?;
2023-01-16 09:53:52 +00:00
}
let background = self
.background
2025-03-04 10:37:45 +00:00
.map(|info| FillType::new(gui_handler, framable.clone(), info))
2023-01-16 09:53:52 +00:00
.transpose()?;
2024-04-26 06:28:05 +00:00
let info_icon = IconizableWrapper::new(
2025-03-04 10:37:45 +00:00
gui_handler,
2024-04-26 06:28:05 +00:00
framable.clone(),
None,
Some(IconizablePositioning {
left: 1.2,
right: 0.0,
top: 1.2,
bottom: 0.0,
}),
0,
)?;
2023-01-16 09:53:52 +00:00
Ok(Arc::new(Label {
framable,
background: RwLock::new(background),
textable_wrapper,
2024-04-26 06:28:05 +00:00
info_icon,
2023-01-16 09:53:52 +00:00
visible: AtomicBool::new(false),
}))
}
}
pub struct Label {
pub(crate) framable: Arc<Framable>,
background: RwLock<Option<FillType>>,
textable_wrapper: TextableWrapper,
2024-04-26 06:28:05 +00:00
info_icon: IconizableWrapper,
2023-01-16 09:53:52 +00:00
visible: AtomicBool,
}
impl Label {
pub fn builder() -> LabelBuilder {
LabelBuilder {
text_alignment: TextAlignment::Center,
text_ratio: 0.7,
text_color: Color::Black,
text: None,
background: None,
}
}
2025-03-04 11:25:02 +00:00
pub fn set_text(&self, gui_handler: &mut GuiHandler, text: impl ToString) -> Result<()> {
2025-03-04 10:37:45 +00:00
self.textable_wrapper
.set_text(gui_handler, text, self.visible())
2023-01-16 09:53:52 +00:00
}
pub fn text(&self) -> Result<Option<String>> {
self.textable_wrapper.text()
}
2025-03-04 11:25:02 +00:00
pub fn set_text_color(&self, gui_handler: &mut GuiHandler, text_color: Color) -> Result<()> {
2025-03-04 10:37:45 +00:00
self.textable_wrapper
.set_text_color(gui_handler, text_color)
2023-01-16 09:53:52 +00:00
}
2025-03-04 10:37:45 +00:00
pub fn set_alignment(
&self,
2025-03-04 11:25:02 +00:00
gui_handler: &mut GuiHandler,
2025-03-04 10:37:45 +00:00
alignment: TextAlignment,
) -> Result<()> {
self.textable_wrapper.set_alignment(gui_handler, alignment)
2023-01-16 09:53:52 +00:00
}
2025-03-04 11:25:02 +00:00
pub fn set_text_ratio(&self, gui_handler: &mut GuiHandler, ratio: f32) -> Result<()> {
2025-03-04 10:37:45 +00:00
self.textable_wrapper.set_height_ratio(gui_handler, ratio)
2023-01-16 09:53:52 +00:00
}
pub fn text_ratio(&self) -> f32 {
self.textable_wrapper.height_ratio()
}
2025-03-04 11:25:02 +00:00
pub fn set_info_icon(&self, gui_handler: &mut GuiHandler, button: &Arc<Image>) -> Result<()> {
2025-03-04 10:37:45 +00:00
self.info_icon.set_icon(gui_handler, button, self.visible())
2024-04-26 06:28:05 +00:00
}
2025-03-04 11:25:02 +00:00
pub fn clear_info_icon(&self, gui_handler: &mut GuiHandler) -> Result<()> {
2025-03-04 10:37:45 +00:00
self.info_icon.clear_icon(gui_handler, self.visible())
2024-04-26 06:28:05 +00:00
}
2025-03-04 10:37:45 +00:00
pub fn set_background(
&self,
2025-03-04 11:25:02 +00:00
gui_handler: &mut GuiHandler,
2025-03-04 10:37:45 +00:00
background: impl Into<FillTypeInfo>,
) -> Result<()> {
super::set_background(
gui_handler,
self.visible(),
&self.framable,
&self.background,
background,
)
2023-01-16 09:53:52 +00:00
}
2025-03-04 11:25:02 +00:00
pub fn try_from(label_info: &LabelInfo, gui_handler: &mut GuiHandler) -> Result<Arc<Self>> {
2023-01-16 09:53:52 +00:00
let text = label_info.text.read().unwrap().clone();
let color = label_info.text_color;
let mut label_builder = Label::builder()
.set_text_color(color)
.set_text_alignment(label_info.text_alignment);
if let Some(background_type) = &label_info.background_type {
label_builder = label_builder.set_background(background_type.clone());
}
if let Some(ratio) = label_info.text_ratio {
label_builder = label_builder.set_ratio(ratio);
}
if !text.is_empty() {
label_builder = label_builder.set_text(text);
}
2025-03-04 10:37:45 +00:00
label_builder.build(gui_handler)
2023-01-16 09:53:52 +00:00
}
2025-03-04 11:25:02 +00:00
fn disable_base(&self, gui_handler: &mut GuiHandler) -> Result<()> {
2025-03-04 10:37:45 +00:00
self.framable.delete(gui_handler)?;
2023-01-16 09:53:52 +00:00
2025-03-04 10:37:45 +00:00
self.textable_wrapper.disable(gui_handler)?;
self.info_icon.disable(gui_handler)?;
2023-01-16 09:53:52 +00:00
if let Some(background) = self.background.read().unwrap().as_ref() {
2025-03-04 10:37:45 +00:00
background.disable(gui_handler)?;
2023-01-16 09:53:52 +00:00
}
Ok(())
}
}
2025-03-04 11:25:02 +00:00
impl GuiElementTraits for Label {
fn gridable(&self) -> Option<&dyn Gridable> {
2023-01-16 09:53:52 +00:00
Some(self)
}
fn visibility(&self) -> Option<&dyn Visibility> {
Some(self)
}
2025-03-04 11:25:02 +00:00
fn downcast<'a>(&'a self) -> Option<GuiElement<'a>> {
2023-01-16 09:53:52 +00:00
Some(GuiElement::Label(self))
}
}
impl Visibility for Label {
fn visible(&self) -> bool {
self.visible.load(SeqCst)
}
2025-04-10 05:58:35 +00:00
fn set_visibility(
&self,
_commands: &mut Commands,
gui_handler: &mut GuiHandler,
visibility: bool,
) -> Result<()> {
2023-01-16 09:53:52 +00:00
if visibility != self.visible() {
self.visible.store(visibility, SeqCst);
if visibility {
2025-03-04 10:37:45 +00:00
self.framable.add(gui_handler)?;
2023-01-16 09:53:52 +00:00
2025-03-04 10:37:45 +00:00
self.textable_wrapper.enable(gui_handler)?;
self.info_icon.enable(gui_handler)?;
2023-01-16 09:53:52 +00:00
if let Some(background) = self.background.read().unwrap().as_ref() {
2025-03-04 10:37:45 +00:00
background.enable(gui_handler)?;
2023-01-16 09:53:52 +00:00
}
} else {
2025-03-04 10:37:45 +00:00
self.disable_base(gui_handler)?;
2023-01-16 09:53:52 +00:00
}
}
Ok(())
}
}
2025-03-04 11:25:02 +00:00
impl Gridable for Label {
2023-01-16 09:53:52 +00:00
fn set_frame(
&self,
2025-03-04 11:25:02 +00:00
gui_handler: &mut GuiHandler,
2023-01-16 09:53:52 +00:00
x: i32,
y: i32,
w: u32,
h: u32,
vert_align: VerticalAlign,
hori_align: HorizontalAlign,
) -> Result<()> {
2025-03-04 10:37:45 +00:00
self.framable
.set_frame(gui_handler, x, y, w, h, vert_align, hori_align);
2023-01-16 09:53:52 +00:00
2025-03-04 10:37:45 +00:00
self.textable_wrapper.update(gui_handler)?;
self.info_icon.update_frame(gui_handler)?;
2023-01-16 09:53:52 +00:00
if let Some(background) = self.background.read().unwrap().as_ref() {
2025-03-04 10:37:45 +00:00
background.update_frame(gui_handler)?;
2023-01-16 09:53:52 +00:00
}
Ok(())
}
2025-03-04 11:25:02 +00:00
fn selectable(&self) -> Option<&Arc<Selectable>> {
2023-01-16 09:53:52 +00:00
None
}
fn type_name(&self) -> &str {
"Label"
}
fn set_layer(&self, layer: i32) -> Result<()> {
self.framable.set_ui_layer(layer);
self.textable_wrapper.set_ui_layer(layer)?;
2024-04-26 06:34:40 +00:00
self.info_icon.set_ui_layer(layer)?;
2023-01-16 09:53:52 +00:00
if let Some(background) = self.background.read().unwrap().as_ref() {
background.set_ui_layer(layer);
}
Ok(())
}
2024-04-21 05:41:08 +00:00
fn position_extent(&self) -> (i32, i32, u32, u32) {
self.framable.position()
}
2023-01-16 09:53:52 +00:00
}