basic functionality

This commit is contained in:
Max Bradbury 2020-09-25 14:19:13 +01:00
parent 38d0476054
commit 7db4d7e659
1 changed files with 45 additions and 6 deletions

View File

@ -1,8 +1,9 @@
#![windows_subsystem = "windows"] #![windows_subsystem = "windows"]
use bitsy_parser::game::Game;
use gio::prelude::*; use gio::prelude::*;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{Orientation, Adjustment}; use gtk::Orientation;
use std::env::args; use std::env::args;
const SPACING: i32 = 16; const SPACING: i32 = 16;
@ -30,7 +31,7 @@ fn error_popup(message: &str) {
}); });
} }
fn add_text_box(container: &gtk::Box, label: &str) -> gtk::TextView { fn add_text_box(container: &gtk::Box, label: &str) -> gtk::TextBuffer {
let vertical = gtk::Box::new(Orientation::Vertical, SPACING); let vertical = gtk::Box::new(Orientation::Vertical, SPACING);
vertical.set_property_expand(true); vertical.set_property_expand(true);
@ -42,12 +43,31 @@ fn add_text_box(container: &gtk::Box, label: &str) -> gtk::TextView {
let scrolled_window = gtk::ScrolledWindow::new(h, v); let scrolled_window = gtk::ScrolledWindow::new(h, v);
scrolled_window.set_property_expand(true); scrolled_window.set_property_expand(true);
let text_view = gtk::TextView::new(); let text_view = gtk::TextView::new();
text_view.set_property_monospace(true);
scrolled_window.add(&text_view); scrolled_window.add(&text_view);
vertical.add(&scrolled_window); vertical.add(&scrolled_window);
container.add(&vertical); container.add(&vertical);
text_view text_view.get_buffer().unwrap()
}
fn parse_or_error(game_data: String, label: &str) -> Result<Game, ()> {
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: &gtk::TextBuffer) -> String {
let (start, end) = &buffer.get_bounds();
buffer.get_text(start, end, false).unwrap().to_string()
} }
fn build_ui(application: &gtk::Application) { fn build_ui(application: &gtk::Application) {
@ -60,13 +80,32 @@ fn build_ui(application: &gtk::Application) {
let vertical = gtk::Box::new(Orientation::Vertical, SPACING); let vertical = gtk::Box::new(Orientation::Vertical, SPACING);
let input_main = add_text_box(&vertical, "main game data"); let buffer_main = add_text_box(&vertical, "main game data");
let input_additional = add_text_box(&vertical, "additional game data"); let buffer_additional = add_text_box(&vertical, "additional game data");
let button = gtk::Button::with_label("mix!"); let button = gtk::Button::with_label("mix!");
vertical.add(&button); 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.add(&vertical);
window.show_all(); window.show_all();