Integrated OuterLevels into the list in the UI

main
aprzn 2 years ago
parent 0a1753dffd
commit c452ca6f17

@ -4,6 +4,8 @@ use std::io::{Read, Cursor};
use base64::engine::{general_purpose::URL_SAFE, Engine}; use base64::engine::{general_purpose::URL_SAFE, Engine};
use flate2::read::GzDecoder; use flate2::read::GzDecoder;
use gd_plist::Value; use gd_plist::Value;
use eframe::epaint::text::LayoutJob;
use eframe::egui::TextFormat;
struct User { struct User {
name: String, name: String,
@ -27,6 +29,44 @@ pub struct OuterLevel {
revision: Option<i64>, // k46 revision: Option<i64>, // k46
} }
impl OuterLevel {
pub fn load_all() -> Vec<OuterLevel> {
let plist = get_local_level_plist();
let levels: Vec<OuterLevel> = 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 { pub fn gd_path() -> PathBuf {
let mut path_buf = home::home_dir().unwrap(); let mut path_buf = home::home_dir().unwrap();
#[cfg(unix)] #[cfg(unix)]
@ -106,19 +146,3 @@ fn get_local_level_plist() -> Value {
Value::from_reader(Cursor::new(plist)).unwrap() Value::from_reader(Cursor::new(plist)).unwrap()
} }
pub fn get_outer_levels() -> Vec<OuterLevel> {
let plist = get_local_level_plist();
let levels: Vec<OuterLevel> = 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
}

@ -5,87 +5,12 @@ use eframe::egui;
use std::boxed::Box; use std::boxed::Box;
use std::collections::VecDeque; 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::Message>) {
(Self {}, Command::none())
}
fn view(&self) -> Element<Self::Message> {
// 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<Self::Message> {
match message {
Message::Msg => {println!("Message received!")}
}
Command::none()
}
fn title(&self) -> String {
String::from("Guider")
}
}
*/
struct PipeDash { struct PipeDash {
selected_level: Option<i32>, msg_queue: VecDeque<Message>,
selected_level: Option<usize>,
selected_color: Option<Color>, selected_color: Option<Color>,
level_list: Vec<gd::OuterLevel>,
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
@ -98,29 +23,36 @@ enum Color {
#[derive(Debug)] #[derive(Debug)]
enum Message { enum Message {
ColorSelected(Color), ColorSelected(Color),
LevelSelected(usize),
} }
impl PipeDash { impl PipeDash {
fn new(_cc: &eframe::CreationContext) -> Self { fn new(_cc: &eframe::CreationContext) -> Self {
Self { Self {
selected_level: None, selected_level: None,
selected_color: None, selected_color: None,
msg_queue: VecDeque::new(),
level_list: gd::OuterLevel::load_all(),
} }
} }
} }
impl eframe::App for PipeDash { impl eframe::App for PipeDash {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
let mut message_queue: VecDeque<Message> = VecDeque::new(); use Message::*;
use Color::*;
ctx.set_pixels_per_point(2f32); ctx.set_pixels_per_point(2f32);
egui::SidePanel::left("level_picker").show(ctx, |ui| { egui::SidePanel::left("level_picker").default_width(100f32).show(ctx, |ui| {
if ui.selectable_label(self.selected_level == Some(0), "lbl 1").clicked() { ui.with_layout(egui::Layout::top_down_justified(egui::Align::Min), |ui| {
self.selected_level = Some(0); 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));
} }
if ui.selectable_label(self.selected_level == Some(1), "lbl 2").clicked() {
self.selected_level = Some(1);
} }
})
}); });
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
ui.vertical_centered_justified(|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.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
ui.vertical(|ui| { ui.vertical(|ui| {
if ui.selectable_label(self.selected_color == Some(Color::Orange), "orange").clicked() { if ui.selectable_label(self.selected_color == Some(Orange), "orange").clicked() {
message_queue.push_back(Message::ColorSelected(Color::Orange)); self.msg_queue.push_back(ColorSelected(Orange));
}; }
if ui.selectable_label(self.selected_color == Some(Color::Yellow), "yellow").clicked() { if ui.selectable_label(self.selected_color == Some(Yellow), "yellow").clicked() {
message_queue.push_back(Message::ColorSelected(Color::Yellow)); self.msg_queue.push_back(ColorSelected(Yellow));
}; }
if ui.selectable_label(self.selected_color == Some(Color::Green), "green").clicked() { if ui.selectable_label(self.selected_color == Some(Green), "green").clicked() {
message_queue.push_back(Message::ColorSelected(Color::Green)); 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); println!("{:?}", message);
match message { match message {
Message::ColorSelected(color) => { Message::ColorSelected(color) => {
self.selected_color = Some(color); self.selected_color = Some(color);
} },
Message::LevelSelected(idx) => {
self.selected_level = Some(idx);
},
} }
} }
} }
} }
fn main() { fn main() {
println!("{:?}", gd::get_outer_levels());
let app: PipeDash; let app: PipeDash;
let opts = eframe::NativeOptions::default(); let opts = eframe::NativeOptions::default();
eframe::run_native("PipeDash", opts, Box::new(|cc| Box::new(PipeDash::new(cc)))); eframe::run_native("PipeDash", opts, Box::new(|cc| Box::new(PipeDash::new(cc))));

Loading…
Cancel
Save