Commit 0a2a2007 authored by Eisha Chen-yen-su's avatar Eisha Chen-yen-su

WIP

parent 6f39fb31
This diff is collapsed.
......@@ -9,6 +9,7 @@ gdk = "0.9.0"
gdk-pixbuf = "0.5.0"
gio = "0.5.0"
glib = "0.6.0"
image = "*"
[dependencies.gtk]
features = ["v3_22"]
......
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.0 -->
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.22"/>
<object class="GtkMenu" id="app_menu">
......@@ -19,17 +19,6 @@
<property name="can_focus">False</property>
<property name="default_width">600</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">
<object class="GtkHeaderBar" id="header_bar">
<property name="visible">True</property>
......@@ -64,5 +53,12 @@
</child>
</object>
</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>
</interface>
......@@ -7,6 +7,7 @@ use std::sync::{Arc, RwLock};
use crate::backend;
use crate::window;
use bkmessages::{BackendCommand, BackendUpdate};
pub fn launch() {
let app: gtk::Application =
......@@ -16,7 +17,7 @@ pub fn launch() {
let (cmd_sender, cmd_receiver) = channel::<String>();
let cmd_sender = Arc::new(RwLock::new(cmd_sender));
let (update_sender, update_receiver) = channel::<String>();
let (update_sender, update_receiver) = channel::<BackendUpdate>();
let update_receiver = Arc::new(RwLock::new(update_receiver));
backend::launch(cmd_receiver, update_sender);
......
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
pub fn launch(cmd_chan: Receiver<String>, update_chan: Sender<String>) {
use image::{ImageBuffer, Luma};
use super::foundry::Grid;
use super::bkmessages::BackendUpdate;
const image_buffer_path: &str = "/tmp/automata_buffer.png";
pub fn launch(cmd_chan: Receiver<String>, update_chan: Sender<BackendUpdate>) {
thread::spawn(move || {
let mut count = 0;
let mut grid: Option<Grid> = None;
loop {
match cmd_chan.recv() {
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));
}
}
let img =
ImageBuffer::<Luma<u8>, _>::from_raw(width as u32, height as u32, bytes)
.unwrap();
img.save(image_buffer_path).unwrap();
update_chan
.send(String::from(format!(
"Action button clicked {} times!",
count
)))
.send(BackendUpdate::NewState(String::from(image_buffer_path)))
.unwrap();
}
Err(_) => {
......
pub enum BackendCommand {
Clear,
Randomize,
NextGen(u32),
}
pub enum BackendUpdate {
NewState(String),
Err(String),
}
extern crate gdk_pixbuf;
extern crate gio;
extern crate glib;
extern crate gtk;
extern crate image;
extern crate foundry;
// http://gtk-rs.org/tuto/closures
#[macro_export]
macro_rules! clone {
......@@ -23,6 +28,7 @@ macro_rules! clone {
mod app;
mod backend;
mod bkmessages;
mod static_resource;
mod window;
......
extern crate gio;
extern crate glib;
extern crate gtk;
use gdk_pixbuf::Pixbuf;
use gtk::prelude::*;
use std::sync::{Arc, RwLock};
use std::sync::mpsc::{Receiver, Sender};
use std::sync::{Arc, RwLock};
use bkmessages::BackendUpdate;
pub fn new_window(
cmd_chan: Arc<RwLock<Sender<String>>>,
update_chan: Arc<RwLock<Receiver<String>>>,
update_chan: Arc<RwLock<Receiver<BackendUpdate>>>,
) -> gtk::ApplicationWindow {
let builder: gtk::Builder =
gtk::Builder::new_from_resource("/org/gnome/Automata/gtk/window.ui");
......@@ -23,16 +22,21 @@ pub fn new_window(
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
.get_object("action_button")
.expect("action_button not found");
btn_action.connect_clicked(clone!(cmd_chan, update_chan => move |_| {
cmd_chan.read().unwrap().send(String::from("Button clicked!")).unwrap();
let msg = update_chan.read().unwrap().recv().unwrap();
let mut path = String::new();
match update_chan.read().unwrap().recv().unwrap() {
BackendUpdate::NewState(p) => path = p,
BackendUpdate::Err(msg) => println!("Error: {}", msg),
}
hello_label.set_text(&msg);
grid_da.set_from_file(path);
}));
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