add <image> widget

This commit is contained in:
galister
2025-12-23 22:10:46 +09:00
parent 17453ae1d9
commit b352269556
19 changed files with 686 additions and 26 deletions

113
wgui/src/widget/image.rs Normal file
View File

@@ -0,0 +1,113 @@
use std::sync::atomic::{AtomicUsize, Ordering};
use slotmap::Key;
use crate::{
drawing::{self, ImagePrimitive, PrimitiveExtent},
event::CallbackDataCommon,
globals::Globals,
layout::WidgetID,
renderer_vk::text::custom_glyph::CustomGlyphData,
widget::{util::WLength, WidgetStateFlags},
};
use super::{WidgetObj, WidgetState};
static AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug, Default)]
pub struct WidgetImageParams {
pub glyph_data: Option<CustomGlyphData>,
pub border: f32,
pub border_color: drawing::Color,
pub round: WLength,
}
#[derive(Debug, Default)]
pub struct WidgetImage {
params: WidgetImageParams,
id: WidgetID,
content_key: usize,
}
impl WidgetImage {
pub fn create(params: WidgetImageParams) -> WidgetState {
WidgetState::new(
WidgetStateFlags::default(),
Box::new(Self {
params,
id: WidgetID::null(),
content_key: AUTO_INCREMENT.fetch_add(1, Ordering::Relaxed),
}),
)
}
pub fn set_content(&mut self, common: &mut CallbackDataCommon, content: Option<CustomGlyphData>) {
if self.params.glyph_data == content {
return;
}
self.params.glyph_data = content;
common.mark_widget_dirty(self.id);
}
pub fn get_content(&self) -> Option<CustomGlyphData> {
self.params.glyph_data.clone()
}
}
impl WidgetObj for WidgetImage {
fn draw(&mut self, state: &mut super::DrawState, _params: &super::DrawParams) {
let boundary = drawing::Boundary::construct_relative(state.transform_stack);
let Some(content) = self.params.glyph_data.clone() else {
return;
};
let round_units = match self.params.round {
WLength::Units(units) => units as u8,
WLength::Percent(percent) => (f32::min(boundary.size.x, boundary.size.y) * percent / 2.0) as u8,
};
state.primitives.push(drawing::RenderPrimitive::Image(
PrimitiveExtent {
boundary,
transform: state.transform_stack.get().transform,
},
ImagePrimitive {
content,
content_key: self.content_key,
border: self.params.border,
border_color: self.params.border_color,
round_units,
},
));
}
fn measure(
&mut self,
_globals: &Globals,
_known_dimensions: taffy::Size<Option<f32>>,
_available_space: taffy::Size<taffy::AvailableSpace>,
) -> taffy::Size<f32> {
taffy::Size::ZERO
}
fn get_id(&self) -> WidgetID {
self.id
}
fn set_id(&mut self, id: WidgetID) {
self.id = id;
}
fn get_type(&self) -> super::WidgetType {
super::WidgetType::Sprite
}
fn debug_print(&self) -> String {
String::default()
}
}

View File

@@ -17,6 +17,7 @@ use crate::{
};
pub mod div;
pub mod image;
pub mod label;
pub mod rectangle;
pub mod sprite;

View File

@@ -50,6 +50,10 @@ impl WidgetSprite {
}
pub fn set_content(&mut self, common: &mut CallbackDataCommon, content: Option<CustomGlyphData>) {
if self.params.glyph_data == content {
return;
}
self.params.glyph_data = content;
common.mark_widget_dirty(self.id);
}