feat(GUI): (WIP) first gui attempt, render single converted image

This commit is contained in:
Timofey Gelazoniya 2022-01-23 06:17:37 +03:00
parent 2507c010c0
commit b24d6a934c
Signed by: zeldon
GPG Key ID: 047886915281DD2A
4 changed files with 1860 additions and 6 deletions

1750
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,13 @@
[package] [package]
edition = "2021"
name = "gui" name = "gui"
version = "0.1.0" version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
cli = {path = "../cli"}
core = {path = "../core"}
eframe = "0.16.0"
image = "0.23.14"
rfd = "0.6.4"

59
gui/src/lib.rs Normal file
View File

@ -0,0 +1,59 @@
use eframe::egui::{Separator, Ui};
use eframe::epi::Image;
use eframe::{egui, epi};
const PADDING: f32 = 5.0;
pub fn render_header(ui: &mut Ui) {
ui.vertical_centered(|ui| {
ui.heading("RDR2Converter");
});
ui.add_space(PADDING);
let sep = Separator::default().spacing(20.);
ui.add(sep);
}
pub fn decode_image(buffer: &[u8]) -> Option<epi::Image> {
use image::GenericImageView;
let image = image::load_from_memory(buffer).ok()?;
let image_buffer = image.to_rgba8();
let size = [image.width() as usize, image.height() as usize];
let pixels = image_buffer.into_vec();
Some(epi::Image::from_rgba_unmultiplied(size, &pixels))
}
pub fn render_image(
ui: &mut egui::Ui,
frame: &epi::Frame,
tex_mngr: &mut TextureManager,
image: Option<epi::Image>,
) {
egui::ScrollArea::vertical()
.auto_shrink([false; 2])
.show(ui, |ui| {
if let Some(image) = image {
if let Some(texture_id) = tex_mngr.texture(frame, &image) {
let mut size = egui::Vec2::new(image.size[0] as f32, image.size[1] as f32);
size *= (ui.available_width() / size.x).min(1.0);
ui.image(texture_id, size);
} else {
ui.monospace("ERROR");
}
}
})
}
#[derive(Default)]
pub struct TextureManager {
texture_id: Option<egui::TextureId>,
}
impl TextureManager {
pub fn texture(&mut self, frame: &epi::Frame, image: &Image) -> Option<egui::TextureId> {
if let Some(texture_id) = self.texture_id.take() {
frame.free_texture(texture_id);
}
self.texture_id = Some(frame.alloc_texture(image.clone()));
self.texture_id
}
}

View File

@ -1,3 +1,49 @@
fn main() { #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
println!("Hello, world!");
use core::buffer;
use eframe::{
egui::{CentralPanel, Vec2},
epi::App,
run_native, NativeOptions,
};
use gui::TextureManager;
use std::path::PathBuf;
#[derive(Default)]
struct RDR2Converter {
pub tex_mngr: TextureManager,
picked_path: Option<PathBuf>,
}
impl App for RDR2Converter {
fn update(&mut self, ctx: &eframe::egui::CtxRef, frame: &eframe::epi::Frame) {
CentralPanel::default().show(ctx, |ui| {
gui::render_header(ui);
if ui.button("Open file...").clicked() {
if let Some(path) = rfd::FileDialog::new().pick_file() {
self.picked_path = Some(path);
}
}
// TODO: use threads and channels
if let Some(picked_path) = &self.picked_path {
let buf = buffer::read_file(&picked_path).unwrap();
let pic = buffer::parse_buf(&buf, b"JPEG", b"JSON", 12);
let image = gui::decode_image(&pic);
gui::render_image(ui, frame, &mut self.tex_mngr, image);
}
});
}
fn name(&self) -> &str {
"RDR2Converter"
}
}
fn main() {
let app = RDR2Converter::default();
let mut win_option = NativeOptions::default();
win_option.initial_window_size = Some(Vec2::new(540., 960.));
run_native(Box::new(app), win_option);
} }