1
0

svg.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Thanks to @japsu and their project https://github.com/japsu/jatsi for the example!
  2. use dioxus::{events::MouseEvent, prelude::*};
  3. fn main() {
  4. dioxus::desktop::launch(app);
  5. }
  6. fn app(cx: Scope) -> Element {
  7. let val = use_state(&cx, || 5);
  8. cx.render(rsx! {
  9. div {
  10. user_select: "none",
  11. webkit_user_select: "none",
  12. margin_left: "10%",
  13. margin_right: "10%",
  14. h1 { "Click die to generate a new value" }
  15. div {
  16. cursor: "pointer",
  17. height: "80%",
  18. width: "80%",
  19. Die {
  20. value: **val,
  21. keep: true,
  22. onclick: move |_| {
  23. use rand::Rng;
  24. let mut rng = rand::thread_rng();
  25. val.set(rng.gen_range(1..6));
  26. }
  27. }
  28. }
  29. }
  30. })
  31. }
  32. #[derive(Props)]
  33. pub struct DieProps<'a> {
  34. pub value: u64,
  35. pub keep: bool,
  36. pub onclick: EventHandler<'a, MouseEvent>,
  37. }
  38. const DOTS: [(i64, i64); 7] = [(-1, -1), (-1, -0), (-1, 1), (1, -1), (1, 0), (1, 1), (0, 0)];
  39. const DOTS_FOR_VALUE: [[bool; 7]; 6] = [
  40. [false, false, false, false, false, false, true],
  41. [false, false, true, true, false, false, false],
  42. [false, false, true, true, false, false, true],
  43. [true, false, true, true, false, true, false],
  44. [true, false, true, true, false, true, true],
  45. [true, true, true, true, true, true, false],
  46. ];
  47. const OFFSET: i64 = 600;
  48. const DOT_RADIUS: &str = "200";
  49. const HELD_COLOR: &str = "#aaa";
  50. const UNHELD_COLOR: &str = "#ddd";
  51. // A six-sided die (D6) with dots.
  52. #[allow(non_snake_case)]
  53. pub fn Die<'a>(cx: Scope<'a, DieProps<'a>>) -> Element {
  54. let &DieProps { value, keep, .. } = cx.props;
  55. let active_dots = &DOTS_FOR_VALUE[(value - 1) as usize];
  56. let fill = if keep { HELD_COLOR } else { UNHELD_COLOR };
  57. let dots = DOTS
  58. .iter()
  59. .zip(active_dots.iter())
  60. .filter(|(_, &active)| active)
  61. .map(|((x, y), _)| {
  62. let dcx = x * OFFSET;
  63. let dcy = y * OFFSET;
  64. rsx!(circle {
  65. cx: "{dcx}",
  66. cy: "{dcy}",
  67. r: "{DOT_RADIUS}",
  68. fill: "#333"
  69. })
  70. });
  71. rsx!(cx,
  72. svg {
  73. onclick: move |e| cx.props.onclick.call(e),
  74. prevent_default: "onclick",
  75. "dioxus-prevent-default": "onclick",
  76. class: "die",
  77. view_box: "-1000 -1000 2000 2000",
  78. rect {
  79. x: "-1000",
  80. y: "-1000",
  81. width: "2000",
  82. height: "2000",
  83. rx: "{DOT_RADIUS}",
  84. fill: "{fill}",
  85. }
  86. dots
  87. }
  88. )
  89. }