trying to figure out a simple way to load iamges.

daddy
elfein727 2021-11-28 02:49:03 -08:00
parent 094c99d5d2
commit 837d29b2ae
3 changed files with 70 additions and 2 deletions

View File

@ -6,3 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
druid = { version = "0.7.0", features = ["image"] }
palette = "0.6.0"
image = "0.23.14"

BIN
emma_and_mujika.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 MiB

View File

@ -1,3 +1,68 @@
fn main() {
println!("Hello, world!");
use std::borrow::Cow;
use std::fs::File;
use std::io::Read;
use std::sync::{Arc, Mutex};
use druid::piet::{Brush, FixedGradient, ImageFormat, InterpolationMode, IntoBrush, PaintBrush};
use druid::widget::{Align, Flex, Image, Label, Painter, TextBox};
use druid::{
AppLauncher, Color, Data, Env, ImageBuf, Lens, LocalizedString, RenderContext, Widget,
WidgetExt, WindowDesc,
};
use image::io::Reader as ImageReader;
use image::GenericImageView;
struct Tab {
buf: ImageBuf,
}
impl<P: RenderContext> IntoBrush<P> for Tab {
fn make_brush<'a>(
&'a self,
piet: &mut P,
bbox: impl FnOnce() -> druid::Rect,
) -> Cow<'a, P::Brush> {
let ctx = self.to_owned().buf.to_image(piet);
piet.draw_image(&ctx, bbox(), InterpolationMode::Bilinear);
Cow::Owned(piet.solid_brush(Color::rgba(0.0, 0.0, 0.0, 1.0)))
}
}
struct KamintaState {
tabs: Vec<Tab>,
}
const TEST_IMAGE_PATH: &str = "emma_and_mujika.png";
fn main() {
let mut app_state = KamintaState { tabs: vec![] };
let img = ImageReader::open(TEST_IMAGE_PATH)
.unwrap()
.decode()
.unwrap();
app_state.tabs.push(Tab {
buf: ImageBuf::from_raw(
img.to_bytes(),
ImageFormat::Rgb,
img.dimensions().0.try_into().unwrap(),
img.dimensions().1.try_into().unwrap(),
),
});
println!["HERE ===================="];
let main_window = WindowDesc::new(build_root_widget)
.title("Kaminta")
.window_size((800.0, 600.0));
AppLauncher::with_window(main_window)
.launch(Arc::new(Mutex::new(app_state)))
.expect("failed to launch");
}
fn build_root_widget() -> impl Widget<Arc<Mutex<KamintaState>>> {
let img = Painter::new(|ctx, data: &Arc<Mutex<KamintaState>>, env| {
let bounds = ctx.size().to_rect();
ctx.fill(bounds, data.lock().unwrap().tabs.get(0).unwrap());
});
img
}