Skip to content

Commit

Permalink
Add rust demo grid (#571)
Browse files Browse the repository at this point in the history
* Add rust demo grid

* Update src/Library/demos/Grid/code.rs

Co-authored-by: Hofer-Julian <[email protected]>

* Update src/Library/demos/Grid/code.rs

Co-authored-by: Hofer-Julian <[email protected]>

* Update src/Library/demos/Grid/code.rs

Co-authored-by: Hofer-Julian <[email protected]>

---------

Co-authored-by: Hofer-Julian <[email protected]>
  • Loading branch information
M-Sabrina and Hofer-Julian authored Sep 4, 2023
1 parent 26b1c3b commit 3b48cdb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/Library/demos/Grid/code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::workbench;
use glib::clone;
use gtk::glib;
use gtk::prelude::*;
use rand::Rng;
use std::cell::Cell;
use std::rc::Rc;

pub fn main() {
let button_ids = [
"button00", "button01", "button02", "button10", "button11", "button12", "button20",
"button21", "button22",
];

let step = Rc::new(Cell::new(1));
for id in button_ids {
let button: gtk::Button = workbench::builder().object(id).unwrap();
button.connect_clicked(clone!(@strong step => move |button| {
on_clicked(button, step.clone())
}));
}
}

fn on_clicked(button: &gtk::Button, step: Rc<Cell<i32>>) {
// Check access for user action
let image: gtk::Image = button.child().unwrap().downcast().unwrap();
if image.icon_name().is_some() {
return;
}
// Store and show user action
image.set_icon_name(Some("cross-large-symbolic"));
// Calculate pc reaction
let mut pc_is_thinking = true;
while pc_is_thinking {
let random_row: f64 = rand::thread_rng().gen();
let random_col: f64 = rand::thread_rng().gen();
let pc_is_thinking_row = (random_row * 3.0).floor().to_string();
let pc_is_thinking_col = (random_col * 3.0).floor().to_string();
let temp: gtk::Button = workbench::builder()
.object(format!("button{pc_is_thinking_row}{pc_is_thinking_col}"))
.unwrap();
let temp_image: gtk::Image = temp.child().unwrap().downcast().unwrap();
if temp_image.icon_name().is_none() {
// Store and show pc reaction
temp_image.set_icon_name(Some("circle-outline-thick-symbolic"));
pc_is_thinking = false;
step.set(step.get() + 2);
}
if step.get() >= 8 {
pc_is_thinking = false;
}
}
}
2 changes: 1 addition & 1 deletion src/langs/rust/template/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/langs/rust/template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dependencies]
libc = "0.2"
rand = "0.8.5"
url = "2.4.0"


Expand Down

0 comments on commit 3b48cdb

Please sign in to comment.