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

Add a "Next gen" button

parent 5877a274
......@@ -26,6 +26,11 @@
<property name="title">Automata</property>
<property name="subtitle">A cellular automata application for GNOME</property>
<property name="show_close_button">True</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">6</property>
<child>
<object class="GtkButton" id="randomize_button">
<property name="label">Randomize</property>
......@@ -34,6 +39,26 @@
<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>
</child>
<child>
<object class="GtkMenuButton" id="menu_button">
......
......@@ -12,22 +12,51 @@ const image_buffer_path: &str = "/tmp/automata_buffer.png";
pub fn launch(cmd_chan: Receiver<BackendCommand>, update_chan: Sender<BackendUpdate>) {
thread::spawn(move || {
let mut grid: Option<Grid> = None;
let width = 300;
let height = 200;
loop {
match cmd_chan.recv() {
Ok(_) => {
Ok(cmd) => {
match cmd {
BackendCommand::Randomize => {
if grid.is_none() {
grid = Some(Grid::new_random(
&String::from("#Toroidal Life"),
true,
&vec![2, 3],
&vec![3],
80,
24,
width,
height,
));
} 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 height = grid.as_ref().unwrap().get_height();
......
......@@ -39,6 +39,23 @@ pub fn new_window(
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
.get_object("about_menu")
.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