From 3b48cdbd7c394428632bc7df044916338ab58553 Mon Sep 17 00:00:00 2001 From: Sabrina Meindlhumer <58905866+M-Sabrina@users.noreply.github.com> Date: Mon, 4 Sep 2023 23:34:46 +0200 Subject: [PATCH] Add rust demo grid (#571) * Add rust demo grid * Update src/Library/demos/Grid/code.rs Co-authored-by: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> * Update src/Library/demos/Grid/code.rs Co-authored-by: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> * Update src/Library/demos/Grid/code.rs Co-authored-by: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> --------- Co-authored-by: Hofer-Julian <30049909+Hofer-Julian@users.noreply.github.com> --- src/Library/demos/Grid/code.rs | 53 ++++++++++++++++++++++++++++++ src/langs/rust/template/Cargo.lock | 2 +- src/langs/rust/template/Cargo.toml | 1 + 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/Library/demos/Grid/code.rs diff --git a/src/Library/demos/Grid/code.rs b/src/Library/demos/Grid/code.rs new file mode 100644 index 000000000..33e8b7757 --- /dev/null +++ b/src/Library/demos/Grid/code.rs @@ -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: >k::Button, step: Rc>) { + // 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; + } + } +} diff --git a/src/langs/rust/template/Cargo.lock b/src/langs/rust/template/Cargo.lock index 0cfaacc07..299354516 100644 --- a/src/langs/rust/template/Cargo.lock +++ b/src/langs/rust/template/Cargo.lock @@ -356,6 +356,7 @@ dependencies = [ "gtk4", "libadwaita", "libc", + "rand", "url", ] @@ -1947,4 +1948,3 @@ dependencies = [ "quote", "syn 1.0.109", ] - diff --git a/src/langs/rust/template/Cargo.toml b/src/langs/rust/template/Cargo.toml index 843ee75e0..bfc7e24da 100644 --- a/src/langs/rust/template/Cargo.toml +++ b/src/langs/rust/template/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] libc = "0.2" +rand = "0.8.5" url = "2.4.0"