added underdeveloped but working image support (no freeing or samplers)
This commit is contained in:
@@ -8,7 +8,7 @@ pub struct Regioned {
|
||||
impl<Ctx: 'static> Widget<Ctx> for Regioned {
|
||||
fn draw(&self, painter: &mut Painter<Ctx>) {
|
||||
painter.region.select(&self.region);
|
||||
painter.draw(&self.inner);
|
||||
painter.draw_inner(&self.inner);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
63
src/core/image.rs
Normal file
63
src/core/image.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use image::DynamicImage;
|
||||
|
||||
use crate::{Color, TextureHandle, Widget, WidgetFnRet, render::RectPrimitive};
|
||||
|
||||
pub struct Image {
|
||||
handle: Option<TextureHandle>,
|
||||
}
|
||||
|
||||
impl<Ctx> Widget<Ctx> for Image {
|
||||
fn draw(&self, painter: &mut crate::Painter<Ctx>) {
|
||||
if let Some(handle) = &self.handle {
|
||||
painter.draw_texture(handle);
|
||||
} else {
|
||||
painter.write(RectPrimitive {
|
||||
color: Color::MAGENTA,
|
||||
inner_radius: 0.0,
|
||||
radius: 0.0,
|
||||
thickness: 0.0,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn image<Ctx>(image: impl LoadableImage) -> WidgetFnRet!(Image, Ctx) {
|
||||
let image = match image.get_image() {
|
||||
Ok(image) => Some(image),
|
||||
Err(e) => {
|
||||
println!("Failed to load image: {e}");
|
||||
None
|
||||
}
|
||||
};
|
||||
move |ui| Image {
|
||||
handle: image.map(|image| ui.add_texture(image)),
|
||||
}
|
||||
}
|
||||
|
||||
pub trait LoadableImage {
|
||||
fn get_image(self) -> Result<DynamicImage, String>;
|
||||
}
|
||||
|
||||
impl LoadableImage for &str {
|
||||
fn get_image(self) -> Result<DynamicImage, String> {
|
||||
image::open(self).map_err(|e| format!("{e:?}"))
|
||||
}
|
||||
}
|
||||
|
||||
impl LoadableImage for String {
|
||||
fn get_image(self) -> Result<DynamicImage, String> {
|
||||
image::open(self).map_err(|e| format!("{e:?}"))
|
||||
}
|
||||
}
|
||||
|
||||
impl<const LEN: usize> LoadableImage for &[u8; LEN] {
|
||||
fn get_image(self) -> Result<DynamicImage, String> {
|
||||
image::load_from_memory(self).map_err(|e| format!("{e:?}"))
|
||||
}
|
||||
}
|
||||
|
||||
impl LoadableImage for DynamicImage {
|
||||
fn get_image(self) -> Result<DynamicImage, String> {
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
mod frame;
|
||||
mod image;
|
||||
mod num;
|
||||
mod rect;
|
||||
mod sense;
|
||||
@@ -7,6 +8,7 @@ mod stack;
|
||||
mod trait_fns;
|
||||
|
||||
pub use frame::*;
|
||||
pub use image::*;
|
||||
pub use num::*;
|
||||
pub use rect::*;
|
||||
pub use sense::*;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{primitive::RoundedRectData, Painter, UiNum, UiColor, Widget};
|
||||
use crate::{Painter, UiColor, UiNum, Widget, render::RectPrimitive};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Rect {
|
||||
@@ -29,7 +29,7 @@ impl Rect {
|
||||
|
||||
impl<Ctx> Widget<Ctx> for Rect {
|
||||
fn draw(&self, painter: &mut Painter<Ctx>) {
|
||||
painter.write(RoundedRectData {
|
||||
painter.write(RectPrimitive {
|
||||
color: self.color,
|
||||
radius: self.radius,
|
||||
thickness: self.thickness,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{Dir, Painter, Sign, UiNum, UiRegion, UIScalar, Widget, WidgetId};
|
||||
use crate::{Dir, Painter, Sign, UIScalar, UiNum, UiRegion, Widget, WidgetId};
|
||||
|
||||
pub struct Span {
|
||||
pub children: Vec<(WidgetId, SpanLen)>,
|
||||
|
||||
@@ -7,7 +7,7 @@ pub struct Stack {
|
||||
impl<Ctx> Widget<Ctx> for Stack {
|
||||
fn draw(&self, painter: &mut crate::Painter<Ctx>) {
|
||||
for child in &self.children {
|
||||
painter.draw_within(child, painter.region);
|
||||
painter.draw(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user