From 7db4d7e659a72f50e329e87649f4c9ebabaab51b Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Fri, 25 Sep 2020 14:19:13 +0100 Subject: [PATCH] basic functionality --- src/main.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0669bff..e5561a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,9 @@ #![windows_subsystem = "windows"] +use bitsy_parser::game::Game; use gio::prelude::*; use gtk::prelude::*; -use gtk::{Orientation, Adjustment}; +use gtk::Orientation; use std::env::args; const SPACING: i32 = 16; @@ -30,7 +31,7 @@ fn error_popup(message: &str) { }); } -fn add_text_box(container: >k::Box, label: &str) -> gtk::TextView { +fn add_text_box(container: >k::Box, label: &str) -> gtk::TextBuffer { let vertical = gtk::Box::new(Orientation::Vertical, SPACING); vertical.set_property_expand(true); @@ -42,12 +43,31 @@ fn add_text_box(container: >k::Box, label: &str) -> gtk::TextView { let scrolled_window = gtk::ScrolledWindow::new(h, v); scrolled_window.set_property_expand(true); let text_view = gtk::TextView::new(); + text_view.set_property_monospace(true); scrolled_window.add(&text_view); vertical.add(&scrolled_window); container.add(&vertical); - text_view + text_view.get_buffer().unwrap() +} + +fn parse_or_error(game_data: String, label: &str) -> Result { + let main = Game::from(game_data); + if main.is_ok() { + Ok(main.unwrap()) + } else { + error_popup(&format!( + "Couldn't parse {} game data: {:?}", + label, + main.unwrap_err())); + Err(()) + } +} + +fn buffer_to_string(buffer: >k::TextBuffer) -> String { + let (start, end) = &buffer.get_bounds(); + buffer.get_text(start, end, false).unwrap().to_string() } fn build_ui(application: >k::Application) { @@ -60,13 +80,32 @@ fn build_ui(application: >k::Application) { let vertical = gtk::Box::new(Orientation::Vertical, SPACING); - let input_main = add_text_box(&vertical, "main game data"); - let input_additional = add_text_box(&vertical, "additional game data"); + let buffer_main = add_text_box(&vertical, "main game data"); + let buffer_additional = add_text_box(&vertical, "additional game data"); let button = gtk::Button::with_label("mix!"); vertical.add(&button); - let output = add_text_box(&vertical, "output"); + let separator = gtk::Separator::new(Orientation::Vertical); + vertical.add(&separator); + + let buffer_output = add_text_box(&vertical, "output"); + + button.connect_clicked(move |_| { + let main = buffer_to_string(&buffer_main); + let additional = buffer_to_string(&buffer_additional); + + let main = parse_or_error(main, "main"); + let additional = parse_or_error(additional, "additional"); + + if main.is_ok() && additional.is_ok() { + let mut main = main.unwrap(); + let additional = additional.unwrap(); + main.merge(additional); + + buffer_output.set_text(&main.to_string()); + } + }); window.add(&vertical); window.show_all();