From c452ca6f17dab9a8b0cc7ec77d3fa962ade0d53c Mon Sep 17 00:00:00 2001 From: aprzn Date: Sat, 4 Feb 2023 21:30:32 -0500 Subject: [PATCH] Integrated OuterLevels into the list in the UI --- src/gd.rs | 56 +++++++++++++++------- src/main.rs | 130 +++++++++++++--------------------------------------- 2 files changed, 72 insertions(+), 114 deletions(-) diff --git a/src/gd.rs b/src/gd.rs index 23c7ae6..0464c94 100644 --- a/src/gd.rs +++ b/src/gd.rs @@ -4,6 +4,8 @@ use std::io::{Read, Cursor}; use base64::engine::{general_purpose::URL_SAFE, Engine}; use flate2::read::GzDecoder; use gd_plist::Value; +use eframe::epaint::text::LayoutJob; +use eframe::egui::TextFormat; struct User { name: String, @@ -27,6 +29,44 @@ pub struct OuterLevel { revision: Option, // k46 } +impl OuterLevel { + pub fn load_all() -> Vec { + let plist = get_local_level_plist(); + let levels: Vec = plist.as_dictionary().and_then(|dict| dict.get("LLM_01")).unwrap() + .as_dictionary().unwrap().into_iter().filter(|(key, _)| key.as_str() != "_isArr").map(|(_, val)| { + let mut builder = LevelBuilder::new(); + let props = val.as_dictionary().unwrap(); + if let Some(title) = props.get("k2") { + builder.with_name(title.as_string().unwrap().into()); + } + if let Some(rev) = props.get("k46") { + builder.with_revision(rev.as_signed_integer().unwrap().into()); + } + builder.build_outer_level().unwrap() + }).collect(); + levels + } + + pub fn display_name(&self) -> LayoutJob { + match self.revision { + Some(rev) => { + let mut job = LayoutJob::default(); + job.append(&format!("{} ", self.name), 0f32, TextFormat::default()); + job.append(&format!("(rev {})", rev), 0f32, TextFormat { + italics: true, + ..Default::default() + }); + job + }, + None => { + let mut job = LayoutJob::default(); + job.append(&self.name, 0f32, TextFormat::default()); + job + }, + } + } +} + pub fn gd_path() -> PathBuf { let mut path_buf = home::home_dir().unwrap(); #[cfg(unix)] @@ -106,19 +146,3 @@ fn get_local_level_plist() -> Value { Value::from_reader(Cursor::new(plist)).unwrap() } -pub fn get_outer_levels() -> Vec { - let plist = get_local_level_plist(); - let levels: Vec = plist.as_dictionary().and_then(|dict| dict.get("LLM_01")).unwrap() - .as_dictionary().unwrap().into_iter().filter(|(key, _)| key.as_str() != "_isArr").map(|(_, val)| { - let mut builder = LevelBuilder::new(); - let props = val.as_dictionary().unwrap(); - if let Some(title) = props.get("k2") { - builder.with_name(title.as_string().unwrap().into()); - } - if let Some(rev) = props.get("k46") { - builder.with_revision(rev.as_signed_integer().unwrap().into()); - } - builder.build_outer_level().unwrap() - }).collect(); - levels -} diff --git a/src/main.rs b/src/main.rs index e951d4f..2b733bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,87 +5,12 @@ use eframe::egui; use std::boxed::Box; use std::collections::VecDeque; -/*use iced::{ - widget::{ - button, - column, - row, - text, - scrollable, - Column, - Row, - }, - Application, - Settings, - Element, - Alignment, - Theme, - executor, - Command, -}; -use rodio::{ - source::Source, - Decoder, - OutputStream, - Sink, -}; - - -struct Guider { -} - -#[derive(Debug, Clone, Copy)] -enum Message { - Msg, -} - -impl Application for Guider { - type Executor = executor::Default; - type Flags = (); - type Message = Message; - type Theme = Theme; - fn new(_flags: Self::Flags) -> (Self, Command) { - (Self {}, Command::none()) - } - - fn view(&self) -> Element { - // column![button("pause").on_press(Message::Pause), button("play").on_press(Message::Play)].into() - row![ - scrollable(["lvl 1", "lvl 2", "lvl 3"].iter().fold( - Column::new(), |col, item| col.push(button(*item).on_press(Message::Msg)) - )), - column![ - row![ - column![text("Some Title Info?")], - column![ - text("G"), - text("Y"), - text("O"), - ] - ], - row![text("zoom in"), text("zoom out"), text("delete"), text("add"), text("edit")], - text("view canvas"), - ] - ].align_items(Alignment::Fill).into() - } - - fn update(&mut self, message: Message) -> Command { - match message { - Message::Msg => {println!("Message received!")} - } - Command::none() - } - - fn title(&self) -> String { - String::from("Guider") - } -} - -*/ struct PipeDash { - selected_level: Option, + msg_queue: VecDeque, + selected_level: Option, selected_color: Option, + level_list: Vec, } #[derive(Debug, PartialEq, Eq)] @@ -98,29 +23,36 @@ enum Color { #[derive(Debug)] enum Message { ColorSelected(Color), + LevelSelected(usize), } + impl PipeDash { fn new(_cc: &eframe::CreationContext) -> Self { Self { selected_level: None, selected_color: None, + msg_queue: VecDeque::new(), + level_list: gd::OuterLevel::load_all(), } } + } impl eframe::App for PipeDash { fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { - let mut message_queue: VecDeque = VecDeque::new(); + use Message::*; + use Color::*; ctx.set_pixels_per_point(2f32); - egui::SidePanel::left("level_picker").show(ctx, |ui| { - if ui.selectable_label(self.selected_level == Some(0), "lbl 1").clicked() { - self.selected_level = Some(0); - } - if ui.selectable_label(self.selected_level == Some(1), "lbl 2").clicked() { - self.selected_level = Some(1); - } + egui::SidePanel::left("level_picker").default_width(100f32).show(ctx, |ui| { + ui.with_layout(egui::Layout::top_down_justified(egui::Align::Min), |ui| { + for (idx, level) in self.level_list.iter().enumerate() { + if ui.selectable_label(self.selected_level == Some(idx), level.display_name()).clicked() { + self.msg_queue.push_back(LevelSelected(idx)); + } + } + }) }); egui::CentralPanel::default().show(ctx, |ui| { ui.vertical_centered_justified(|ui| { @@ -131,15 +63,15 @@ impl eframe::App for PipeDash { }); ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| { ui.vertical(|ui| { - if ui.selectable_label(self.selected_color == Some(Color::Orange), "orange").clicked() { - message_queue.push_back(Message::ColorSelected(Color::Orange)); - }; - if ui.selectable_label(self.selected_color == Some(Color::Yellow), "yellow").clicked() { - message_queue.push_back(Message::ColorSelected(Color::Yellow)); - }; - if ui.selectable_label(self.selected_color == Some(Color::Green), "green").clicked() { - message_queue.push_back(Message::ColorSelected(Color::Green)); - }; + if ui.selectable_label(self.selected_color == Some(Orange), "orange").clicked() { + self.msg_queue.push_back(ColorSelected(Orange)); + } + if ui.selectable_label(self.selected_color == Some(Yellow), "yellow").clicked() { + self.msg_queue.push_back(ColorSelected(Yellow)); + } + if ui.selectable_label(self.selected_color == Some(Green), "green").clicked() { + self.msg_queue.push_back(ColorSelected(Green)); + } }); }) }); @@ -147,19 +79,21 @@ impl eframe::App for PipeDash { }); }); - for message in message_queue.drain(..) { + for message in self.msg_queue.drain(..) { println!("{:?}", message); match message { Message::ColorSelected(color) => { self.selected_color = Some(color); - } + }, + Message::LevelSelected(idx) => { + self.selected_level = Some(idx); + }, } } } } fn main() { - println!("{:?}", gd::get_outer_levels()); let app: PipeDash; let opts = eframe::NativeOptions::default(); eframe::run_native("PipeDash", opts, Box::new(|cc| Box::new(PipeDash::new(cc))));