-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
26b1c3b
commit 3b48cdb
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: >k::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; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ edition = "2021" | |
|
||
[dependencies] | ||
libc = "0.2" | ||
rand = "0.8.5" | ||
url = "2.4.0" | ||
|
||
|
||
|