Created
November 9, 2023 20:55
-
-
Save loloof64/81a22821c98601640d2bc7e2804cf46b to your computer and use it in GitHub Desktop.
A simple dice with Rui framework (for Rust)
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
[package] | |
name = "dice" | |
version = "0.1.0" | |
edition = "2021" | |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |
[dependencies] | |
rui = "0.6.1" | |
vger = "0.2.7" | |
euclid = "0.22.9" | |
fastrand = "2.0.1" |
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
use rui::*; | |
use vger::color::Color; | |
fn main() { | |
rui(state( | |
|| fastrand::u8(0..6), | |
|dice_value, cx| { | |
let value = cx[dice_value]; | |
vstack(( | |
canvas(move |_, rect, vger| { | |
const SIDE: f32 = 100.0f32; | |
let point_diameter = SIDE / 9.0; | |
let dice_center = [ | |
rect.origin.x + (rect.size.width / 2.0), | |
rect.origin.y + (rect.size.height / 2.0), | |
]; | |
let dice_origin = | |
[dice_center[0] - (SIDE / 2.0), dice_center[1] - (SIDE / 2.0)]; | |
let points_paint = vger.color_paint(Color::gray(0.0)); | |
let value = value + 1; | |
let bg_color = vger.color_paint(Color::hex("#F4FA58").unwrap()); | |
let bg_rectangle = euclid::rect(dice_origin[0], dice_origin[1], SIDE, SIDE); | |
vger.fill_rect(bg_rectangle, 10.0, bg_color); | |
if (value == 4) || (value == 5) || (value == 6) { | |
let point_center = [ | |
dice_origin[0] + point_diameter * 1.5, | |
dice_origin[1] + point_diameter * 7.5, | |
]; | |
vger.fill_circle(point_center, point_diameter / 2.0, points_paint); | |
} | |
if (value == 2) || (value == 3) || (value == 4) || (value == 5) || (value == 6) | |
{ | |
let point_center = [ | |
dice_origin[0] + point_diameter * 7.5, | |
dice_origin[1] + point_diameter * 7.5, | |
]; | |
vger.fill_circle(point_center, point_diameter / 2.0, points_paint); | |
} | |
if value == 6 { | |
let point_center = [ | |
dice_origin[0] + point_diameter * 1.5, | |
dice_origin[1] + point_diameter * 4.5, | |
]; | |
vger.fill_circle(point_center, point_diameter / 2.0, points_paint); | |
} | |
if (value == 1) || (value == 3) || (value == 5) { | |
let point_center = [ | |
dice_origin[0] + point_diameter * 4.5, | |
dice_origin[1] + point_diameter * 4.5, | |
]; | |
vger.fill_circle(point_center, point_diameter / 2.0, points_paint); | |
} | |
if value == 6 { | |
let point_center = [ | |
dice_origin[0] + point_diameter * 7.5, | |
dice_origin[1] + point_diameter * 4.5, | |
]; | |
vger.fill_circle(point_center, point_diameter / 2.0, points_paint); | |
} | |
if (value == 2) || (value == 3) || (value == 4) || (value == 5) || (value == 6) | |
{ | |
let point_center = [ | |
dice_origin[0] + point_diameter * 1.5, | |
dice_origin[1] + point_diameter * 1.5, | |
]; | |
vger.fill_circle(point_center, point_diameter / 2.0, points_paint); | |
} | |
if (value == 4) || (value == 5) || (value == 6) { | |
let point_center = [ | |
dice_origin[0] + point_diameter * 7.5, | |
dice_origin[1] + point_diameter * 1.5, | |
]; | |
vger.fill_circle(point_center, point_diameter / 2.0, points_paint); | |
} | |
}) | |
.padding(Auto), | |
button("new value", move |cx| { | |
cx[dice_value] = fastrand::u8(0..6); | |
}) | |
.padding(Auto), | |
)) | |
}, | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment