Commit 1f9cc88c authored by Eisha Chen-yen-su's avatar Eisha Chen-yen-su

WIP

parent 6f39fb31
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.0 --> <!-- Generated with glade 3.22.1 -->
<interface> <interface>
<requires lib="gtk+" version="3.22"/> <requires lib="gtk+" version="3.22"/>
<object class="GtkMenu" id="app_menu"> <object class="GtkMenu" id="app_menu">
...@@ -19,17 +19,6 @@ ...@@ -19,17 +19,6 @@
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="default_width">600</property> <property name="default_width">600</property>
<property name="default_height">300</property> <property name="default_height">300</property>
<child>
<object class="GtkLabel" id="label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label">Hello, World!</property>
<attributes>
<attribute name="weight" value="bold"/>
<attribute name="scale" value="2"/>
</attributes>
</object>
</child>
<child type="titlebar"> <child type="titlebar">
<object class="GtkHeaderBar" id="header_bar"> <object class="GtkHeaderBar" id="header_bar">
<property name="visible">True</property> <property name="visible">True</property>
...@@ -64,5 +53,12 @@ ...@@ -64,5 +53,12 @@
</child> </child>
</object> </object>
</child> </child>
<child>
<object class="GtkImage" id="grid_da">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-missing-image</property>
</object>
</child>
</object> </object>
</interface> </interface>
use std::sync::mpsc::{Receiver, Sender}; use std::sync::mpsc::{Receiver, Sender};
use std::thread; use std::thread;
pub fn launch(cmd_chan: Receiver<String>, update_chan: Sender<String>) { use glib::Bytes;
use super::foundry::Grid;
use super::bkmessages::BackendUpdate;
pub fn launch(cmd_chan: Receiver<String>, update_chan: Sender<BackendUpdate>) {
thread::spawn(move || { thread::spawn(move || {
let mut count = 0; let mut grid: Option<Grid> = None;
loop { loop {
match cmd_chan.recv() { match cmd_chan.recv() {
Ok(_) => { Ok(_) => {
count += 1; if grid.is_none() {
grid = Some(Grid::new_random(
&String::from("#Toroidal Life"),
true,
&vec![2, 3],
&vec![3],
80,
24,
));
} else {
grid.as_mut().unwrap().randomize();
}
let width = grid.as_ref().unwrap().get_width();
let height = grid.as_ref().unwrap().get_height();
let mut bytes: Vec<u8> = Vec::with_capacity(width * height);
for y in 0..height {
for x in 0..width {
bytes.push(grid.as_ref().unwrap().get_cell_state(x as i64, y as i64));
}
}
update_chan update_chan
.send(String::from(format!( .send(BackendUpdate::NewState(bytes))
"Action button clicked {} times!",
count
)))
.unwrap(); .unwrap();
} }
Err(_) => { Err(_) => {
......
pub enum BackendCommand {
Clear,
Randomize,
NextGen(u32),
}
pub enum BackendUpdate {
NewState(String),
Err(String),
}
extern crate gdk_pixbuf;
extern crate gio; extern crate gio;
extern crate glib; extern crate glib;
extern crate gtk; extern crate gtk;
extern crate foundry;
// http://gtk-rs.org/tuto/closures // http://gtk-rs.org/tuto/closures
#[macro_export] #[macro_export]
macro_rules! clone { macro_rules! clone {
...@@ -23,6 +26,7 @@ macro_rules! clone { ...@@ -23,6 +26,7 @@ macro_rules! clone {
mod app; mod app;
mod backend; mod backend;
mod bkmessages;
mod static_resource; mod static_resource;
mod window; mod window;
......
extern crate gio; use gdk_pixbuf::Pixbuf;
extern crate glib;
extern crate gtk;
use gtk::prelude::*; use gtk::prelude::*;
use std::sync::{Arc, RwLock};
use std::sync::mpsc::{Receiver, Sender}; use std::sync::mpsc::{Receiver, Sender};
use std::sync::{Arc, RwLock};
use bkmessages::BackendUpdate;
pub fn new_window( pub fn new_window(
cmd_chan: Arc<RwLock<Sender<String>>>, cmd_chan: Arc<RwLock<Sender<String>>>,
update_chan: Arc<RwLock<Receiver<String>>>, update_chan: Arc<RwLock<Receiver<BackendUpdate>>>,
) -> gtk::ApplicationWindow { ) -> gtk::ApplicationWindow {
let builder: gtk::Builder = let builder: gtk::Builder =
gtk::Builder::new_from_resource("/org/gnome/Automata/gtk/window.ui"); gtk::Builder::new_from_resource("/org/gnome/Automata/gtk/window.ui");
...@@ -23,16 +22,23 @@ pub fn new_window( ...@@ -23,16 +22,23 @@ pub fn new_window(
gtk::Inhibit(false) gtk::Inhibit(false)
}); });
let hello_label: gtk::Label = builder.get_object("label").expect("label not found"); let grid_da: gtk::Image = builder.get_object("grid_da").expect("grid_da not found");
let btn_action: gtk::Button = builder let btn_action: gtk::Button = builder
.get_object("action_button") .get_object("action_button")
.expect("action_button not found"); .expect("action_button not found");
btn_action.connect_clicked(clone!(cmd_chan, update_chan => move |_| { btn_action.connect_clicked(clone!(cmd_chan, update_chan => move |_| {
cmd_chan.read().unwrap().send(String::from("Button clicked!")).unwrap(); cmd_chan.read().unwrap().send(String::from("Button clicked!")).unwrap();
let msg = update_chan.read().unwrap().recv().unwrap(); let mut grid: Vec<u8> = Vec::new();
match update_chan.read().unwrap().recv().unwrap() {
BackendUpdate::NewState(bytes) => grid = bytes,
BackendUpdate::Err(msg) => println!("Error: {}", msg),
}
let grid = Pixbuf::new_from_vec(grid, gdk_pixbuf::Colorspace::Rgb, false, 8, );
hello_label.set_text(&msg); grid_da.set_from_pixbuf();
})); }));
let mnu_about: gtk::MenuItem = builder let mnu_about: gtk::MenuItem = builder
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment