Commit fe941ece authored by Eisha Chen-yen-su's avatar Eisha Chen-yen-su

Add a "Next gen" button

parent 5877a274
...@@ -27,12 +27,37 @@ ...@@ -27,12 +27,37 @@
<property name="subtitle">A cellular automata application for GNOME</property> <property name="subtitle">A cellular automata application for GNOME</property>
<property name="show_close_button">True</property> <property name="show_close_button">True</property>
<child> <child>
<object class="GtkButton" id="randomize_button"> <object class="GtkBox">
<property name="label">Randomize</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">False</property>
<property name="receives_default">True</property> <property name="spacing">6</property>
<property name="always_show_image">True</property> <child>
<object class="GtkButton" id="randomize_button">
<property name="label">Randomize</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="always_show_image">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="next_gen_button">
<property name="label" translatable="yes">Next gen</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object> </object>
</child> </child>
<child> <child>
......
...@@ -12,21 +12,50 @@ const image_buffer_path: &str = "/tmp/automata_buffer.png"; ...@@ -12,21 +12,50 @@ const image_buffer_path: &str = "/tmp/automata_buffer.png";
pub fn launch(cmd_chan: Receiver<BackendCommand>, update_chan: Sender<BackendUpdate>) { pub fn launch(cmd_chan: Receiver<BackendCommand>, update_chan: Sender<BackendUpdate>) {
thread::spawn(move || { thread::spawn(move || {
let mut grid: Option<Grid> = None; let mut grid: Option<Grid> = None;
let width = 300;
let height = 200;
loop { loop {
match cmd_chan.recv() { match cmd_chan.recv() {
Ok(_) => { Ok(cmd) => {
if grid.is_none() { match cmd {
grid = Some(Grid::new_random( BackendCommand::Randomize => {
&String::from("#Toroidal Life"), if grid.is_none() {
true, grid = Some(Grid::new_random(
&vec![2, 3], &String::from("#Toroidal Life"),
&vec![3], true,
80, &vec![2, 3],
24, &vec![3],
)); width,
} else { height,
grid.as_mut().unwrap().randomize(); ));
} else {
grid.as_mut().unwrap().randomize();
}
}
BackendCommand::NextGen(n) => {
if grid.is_none() {
update_chan
.send(BackendUpdate::Err(String::from("grid not initialized")))
.unwrap();
continue;
} else {
for _ in 0..n {
grid.as_mut().unwrap().next_gen();
}
}
}
BackendCommand::Clear => {
grid = Some(Grid::new(
&String::from("#Toroidal Life"),
true,
&vec![2, 3],
&vec![3],
width,
height,
))
}
} }
let width = grid.as_ref().unwrap().get_width(); let width = grid.as_ref().unwrap().get_width();
......
...@@ -39,6 +39,23 @@ pub fn new_window( ...@@ -39,6 +39,23 @@ pub fn new_window(
grid_da.set_from_file(path); grid_da.set_from_file(path);
})); }));
let grid_da: gtk::Image = builder.get_object("grid_da").expect("grid_da not found");
let btn_ng: gtk::Button = builder
.get_object("next_gen_button")
.expect("next_gen_button not found");
btn_ng.connect_clicked(clone!(cmd_chan, update_chan => move |_| {
cmd_chan.read().unwrap().send(BackendCommand::NextGen(1)).unwrap();
let mut path = String::new();
match update_chan.read().unwrap().recv().unwrap() {
BackendUpdate::NewState(p) => path = p,
BackendUpdate::Err(msg) => println!("Error: {}", msg),
}
grid_da.set_from_file(path);
}));
let mnu_about: gtk::MenuItem = builder let mnu_about: gtk::MenuItem = builder
.get_object("about_menu") .get_object("about_menu")
.expect("about_menu not found"); .expect("about_menu not found");
......
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