fixed undo/redo bugs

renamed rdraught-ui to rdraught-gtk
This commit is contained in:
2025-07-03 10:37:15 +08:00
parent dd0777ff9a
commit e5182c26f3
16 changed files with 293 additions and 70 deletions

27
rdraught-gtk/Cargo.toml Normal file
View File

@@ -0,0 +1,27 @@
[package]
name = "rdraught-gtk"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
version.workspace = true
[dependencies]
gtk4.workspace = true
gdk4.workspace = true
rdraught = {workspace = true, features = ["std"]}
librsvg.workspace = true
cairo-rs.workspace = true
gio.workspace = true
glib.workspace = true
[lib]
name = "rdraught_gtk"
crate-type = ["lib"]
bench = false
[[bin]]
name = "rdraught-gtk"
path = "src/main.rs"

View File

@@ -0,0 +1,254 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="270" height="90">
<!-- white king //-->
<g style="fill:none; fill-opacity:1; fill-rule:evenodd; stroke:#000000; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(0,0)">
<path
d="M 22.5,11.63 L 22.5,6"
style="fill:none; stroke:#000000; stroke-linejoin:miter;" />
<path
d="M 20,8 L 25,8"
style="fill:none; stroke:#000000; stroke-linejoin:miter;" />
<path
d="M 22.5,25 C 22.5,25 27,17.5 25.5,14.5 C 25.5,14.5 24.5,12 22.5,12 C 20.5,12 19.5,14.5 19.5,14.5 C 18,17.5 22.5,25 22.5,25"
style="fill:#ffffff; stroke:#000000; stroke-linecap:butt; stroke-linejoin:miter;" />
<path
d="M 11.5,37 C 17,40.5 27,40.5 32.5,37 L 32.5,30 C 32.5,30 41.5,25.5 38.5,19.5 C 34.5,13 25,16 22.5,23.5 L 22.5,27 L 22.5,23.5 C 19,16 9.5,13 6.5,19.5 C 3.5,25.5 11.5,29.5 11.5,29.5 L 11.5,37 z "
style="fill:#ffffff; stroke:#000000;" />
<path
d="M 11.5,30 C 17,27 27,27 32.5,30"
style="fill:none; stroke:#000000;" />
<path
d="M 11.5,33.5 C 17,30.5 27,30.5 32.5,33.5"
style="fill:none; stroke:#000000;" />
<path
d="M 11.5,37 C 17,34 27,34 32.5,37"
style="fill:none; stroke:#000000;" />
</g>
<!-- white queen //-->
<g style="opacity:1; fill:#ffffff; fill-opacity:1; fill-rule:evenodd; stroke:#000000; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(45,0)">
<path
d="M 9 13 A 2 2 0 1 1 5,13 A 2 2 0 1 1 9 13 z"
transform="translate(-1,-1)" />
<path
d="M 9 13 A 2 2 0 1 1 5,13 A 2 2 0 1 1 9 13 z"
transform="translate(15.5,-5.5)" />
<path
d="M 9 13 A 2 2 0 1 1 5,13 A 2 2 0 1 1 9 13 z"
transform="translate(32,-1)" />
<path
d="M 9 13 A 2 2 0 1 1 5,13 A 2 2 0 1 1 9 13 z"
transform="translate(7,-4.5)" />
<path
d="M 9 13 A 2 2 0 1 1 5,13 A 2 2 0 1 1 9 13 z"
transform="translate(24,-4)" />
<path
d="M 9,26 C 17.5,24.5 30,24.5 36,26 L 38,14 L 31,25 L 31,11 L 25.5,24.5 L 22.5,9.5 L 19.5,24.5 L 14,10.5 L 14,25 L 7,14 L 9,26 z "
style="stroke-linecap:butt;" />
<path
d="M 9,26 C 9,28 10.5,28 11.5,30 C 12.5,31.5 12.5,31 12,33.5 C 10.5,34.5 10.5,36 10.5,36 C 9,37.5 11,38.5 11,38.5 C 17.5,39.5 27.5,39.5 34,38.5 C 34,38.5 35.5,37.5 34,36 C 34,36 34.5,34.5 33,33.5 C 32.5,31 32.5,31.5 33.5,30 C 34.5,28 36,28 36,26 C 27.5,24.5 17.5,24.5 9,26 z "
style="stroke-linecap:butt;" />
<path
d="M 11.5,30 C 15,29 30,29 33.5,30"
style="fill:none;" />
<path
d="M 12,33.5 C 18,32.5 27,32.5 33,33.5"
style="fill:none;" />
</g>
<!-- white bishop //-->
<g style="opacity:1; fill:none; fill-rule:evenodd; fill-opacity:1; stroke:#000000; stroke-width:1.5; stroke-linecap:round; stroke-linejoin:round; stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(90,0)">
<g style="fill:#ffffff; stroke:#000000; stroke-linecap:butt;">
<path
d="M 9,36 C 12.39,35.03 19.11,36.43 22.5,34 C 25.89,36.43 32.61,35.03 36,36 C 36,36 37.65,36.54 39,38 C 38.32,38.97 37.35,38.99 36,38.5 C 32.61,37.53 25.89,38.96 22.5,37.5 C 19.11,38.96 12.39,37.53 9,38.5 C 7.646,38.99 6.677,38.97 6,38 C 7.354,36.06 9,36 9,36 z" />
<path
d="M 15,32 C 17.5,34.5 27.5,34.5 30,32 C 30.5,30.5 30,30 30,30 C 30,27.5 27.5,26 27.5,26 C 33,24.5 33.5,14.5 22.5,10.5 C 11.5,14.5 12,24.5 17.5,26 C 17.5,26 15,27.5 15,30 C 15,30 14.5,30.5 15,32 z" />
<path
d="M 25 8 A 2.5 2.5 0 1 1 20,8 A 2.5 2.5 0 1 1 25 8 z" />
</g>
<path
d="M 17.5,26 L 27.5,26 M 15,30 L 30,30 M 22.5,15.5 L 22.5,20.5 M 20,18 L 25,18"
style="fill:none; stroke:#000000; stroke-linejoin:miter;" />
</g>
<!-- white knight //-->
<g style="opacity:1; fill:none; fill-opacity:1; fill-rule:evenodd; stroke:#000000; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(135,0)">
<path
d="M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18"
style="fill:#ffffff; stroke:#000000;" />
<path
d="M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10"
style="fill:#ffffff; stroke:#000000;" />
<path
d="M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z"
style="fill:#000000; stroke:#000000;" />
<path
d="M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z"
transform="matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)"
style="fill:#000000; stroke:#000000;" />
</g>
<!-- white rook //-->
<g style="opacity:1; fill:#ffffff; fill-opacity:1; fill-rule:evenodd; stroke:#000000; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(180,0)">
<path
d="M 9,39 L 36,39 L 36,36 L 9,36 L 9,39 z "
style="stroke-linecap:butt;" />
<path
d="M 12,36 L 12,32 L 33,32 L 33,36 L 12,36 z "
style="stroke-linecap:butt;" />
<path
d="M 11,14 L 11,9 L 15,9 L 15,11 L 20,11 L 20,9 L 25,9 L 25,11 L 30,11 L 30,9 L 34,9 L 34,14"
style="stroke-linecap:butt;" />
<path
d="M 34,14 L 31,17 L 14,17 L 11,14" />
<path
d="M 31,17 L 31,29.5 L 14,29.5 L 14,17"
style="stroke-linecap:butt; stroke-linejoin:miter;" />
<path
d="M 31,29.5 L 32.5,32 L 12.5,32 L 14,29.5" />
<path
d="M 11,14 L 34,14"
style="fill:none; stroke:#000000; stroke-linejoin:miter;" />
</g>
<!-- white pawn //-->
<g transform="translate(225,0)">
<path
d="M 22,9 C 19.79,9 18,10.79 18,13 C 18,13.89 18.29,14.71 18.78,15.38 C 16.83,16.5 15.5,18.59 15.5,21 C 15.5,23.03 16.44,24.84 17.91,26.03 C 14.91,27.09 10.5,31.58 10.5,39.5 L 33.5,39.5 C 33.5,31.58 29.09,27.09 26.09,26.03 C 27.56,24.84 28.5,23.03 28.5,21 C 28.5,18.59 27.17,16.5 25.22,15.38 C 25.71,14.71 26,13.89 26,13 C 26,10.79 24.21,9 22,9 z "
style="opacity:1; fill:#ffffff; fill-opacity:1; fill-rule:nonzero; stroke:#000000; stroke-width:1.5; stroke-linecap:round; stroke-linejoin:miter; stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" />
</g>
<!-- black king //-->
<g style="fill:none; fill-opacity:1; fill-rule:evenodd; stroke:#000000; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(0,45)">
<path
d="M 22.5,11.63 L 22.5,6"
style="fill:none; stroke:#000000; stroke-linejoin:miter;" />
<path
d="M 22.5,25 C 22.5,25 27,17.5 25.5,14.5 C 25.5,14.5 24.5,12 22.5,12 C 20.5,12 19.5,14.5 19.5,14.5 C 18,17.5 22.5,25 22.5,25"
style="fill:#000000;fill-opacity:1; stroke-linecap:butt; stroke-linejoin:miter;" />
<path
d="M 11.5,37 C 17,40.5 27,40.5 32.5,37 L 32.5,30 C 32.5,30 41.5,25.5 38.5,19.5 C 34.5,13 25,16 22.5,23.5 L 22.5,27 L 22.5,23.5 C 19,16 9.5,13 6.5,19.5 C 3.5,25.5 11.5,29.5 11.5,29.5 L 11.5,37 z "
style="fill:#000000; stroke:#000000;" />
<path
d="M 20,8 L 25,8"
style="fill:none; stroke:#000000; stroke-linejoin:miter;" />
<path
d="M 32,29.5 C 32,29.5 40.5,25.5 38.03,19.85 C 34.15,14 25,18 22.5,24.5 L 22.51,26.6 L 22.5,24.5 C 20,18 9.906,14 6.997,19.85 C 4.5,25.5 11.85,28.85 11.85,28.85"
style="fill:none; stroke:#ffffff;" />
<path
d="M 11.5,30 C 17,27 27,27 32.5,30 M 11.5,33.5 C 17,30.5 27,30.5 32.5,33.5 M 11.5,37 C 17,34 27,34 32.5,37"
style="fill:none; stroke:#ffffff;" />
</g>
<!-- black queen //-->
<g style="opacity:1; fill:000000; fill-opacity:1; fill-rule:evenodd; stroke:#000000; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(45,45)">
<g style="fill:#000000; stroke:none;">
<circle cx="6" cy="12" r="2.75" />
<circle cx="14" cy="9" r="2.75" />
<circle cx="22.5" cy="8" r="2.75" />
<circle cx="31" cy="9" r="2.75" />
<circle cx="39" cy="12" r="2.75" />
</g>
<path
d="M 9,26 C 17.5,24.5 30,24.5 36,26 L 38.5,13.5 L 31,25 L 30.7,10.9 L 25.5,24.5 L 22.5,10 L 19.5,24.5 L 14.3,10.9 L 14,25 L 6.5,13.5 L 9,26 z"
style="stroke-linecap:butt; stroke:#000000;" />
<path
d="M 9,26 C 9,28 10.5,28 11.5,30 C 12.5,31.5 12.5,31 12,33.5 C 10.5,34.5 10.5,36 10.5,36 C 9,37.5 11,38.5 11,38.5 C 17.5,39.5 27.5,39.5 34,38.5 C 34,38.5 35.5,37.5 34,36 C 34,36 34.5,34.5 33,33.5 C 32.5,31 32.5,31.5 33.5,30 C 34.5,28 36,28 36,26 C 27.5,24.5 17.5,24.5 9,26 z"
style="stroke-linecap:butt;" />
<path
d="M 11,38.5 A 35,35 1 0 0 34,38.5"
style="fill:none; stroke:#000000; stroke-linecap:butt;" />
<path
d="M 11,29 A 35,35 1 0 1 34,29"
style="fill:none; stroke:#ffffff;" />
<path
d="M 12.5,31.5 L 32.5,31.5"
style="fill:none; stroke:#ffffff;" />
<path
d="M 11.5,34.5 A 35,35 1 0 0 33.5,34.5"
style="fill:none; stroke:#ffffff;" />
<path
d="M 10.5,37.5 A 35,35 1 0 0 34.5,37.5"
style="fill:none; stroke:#ffffff;" />
</g>
<!-- black bishop //-->
<g style="opacity:1; fill:none; fill-rule:evenodd; fill-opacity:1; stroke:#000000; stroke-width:1.5; stroke-linecap:round; stroke-linejoin:round; stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(90,45)">
<g style="fill:#000000; stroke:#000000; stroke-linecap:butt;">
<path
d="M 9,36 C 12.39,35.03 19.11,36.43 22.5,34 C 25.89,36.43 32.61,35.03 36,36 C 36,36 37.65,36.54 39,38 C 38.32,38.97 37.35,38.99 36,38.5 C 32.61,37.53 25.89,38.96 22.5,37.5 C 19.11,38.96 12.39,37.53 9,38.5 C 7.646,38.99 6.677,38.97 6,38 C 7.354,36.06 9,36 9,36 z" />
<path
d="M 15,32 C 17.5,34.5 27.5,34.5 30,32 C 30.5,30.5 30,30 30,30 C 30,27.5 27.5,26 27.5,26 C 33,24.5 33.5,14.5 22.5,10.5 C 11.5,14.5 12,24.5 17.5,26 C 17.5,26 15,27.5 15,30 C 15,30 14.5,30.5 15,32 z" />
<path
d="M 25 8 A 2.5 2.5 0 1 1 20,8 A 2.5 2.5 0 1 1 25 8 z" />
</g>
<path
d="M 17.5,26 L 27.5,26 M 15,30 L 30,30 M 22.5,15.5 L 22.5,20.5 M 20,18 L 25,18"
style="fill:none; stroke:#ffffff; stroke-linejoin:miter;" />
</g>
<!-- black knight //-->
<g style="opacity:1; fill:none; fill-opacity:1; fill-rule:evenodd; stroke:#000000; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(135,45)">
<path
d="M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18"
style="fill:#000000; stroke:#000000;" />
<path
d="M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10"
style="fill:#000000; stroke:#000000;" />
<path
d="M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z"
style="fill:#ffffff; stroke:#ffffff;" />
<path
d="M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z"
transform="matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)"
style="fill:#ffffff; stroke:#ffffff;" />
<path
d="M 24.55,10.4 L 24.1,11.85 L 24.6,12 C 27.75,13 30.25,14.49 32.5,18.75 C 34.75,23.01 35.75,29.06 35.25,39 L 35.2,39.5 L 37.45,39.5 L 37.5,39 C 38,28.94 36.62,22.15 34.25,17.66 C 31.88,13.17 28.46,11.02 25.06,10.5 L 24.55,10.4 z "
style="fill:#ffffff; stroke:none;" />
</g>
<!-- black rook //-->
<g style="opacity:1; fill:000000; fill-opacity:1; fill-rule:evenodd; stroke:#000000; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(180,45)">
<path
d="M 9,39 L 36,39 L 36,36 L 9,36 L 9,39 z "
style="stroke-linecap:butt;" />
<path
d="M 12.5,32 L 14,29.5 L 31,29.5 L 32.5,32 L 12.5,32 z "
style="stroke-linecap:butt;" />
<path
d="M 12,36 L 12,32 L 33,32 L 33,36 L 12,36 z "
style="stroke-linecap:butt;" />
<path
d="M 14,29.5 L 14,16.5 L 31,16.5 L 31,29.5 L 14,29.5 z "
style="stroke-linecap:butt;stroke-linejoin:miter;" />
<path
d="M 14,16.5 L 11,14 L 34,14 L 31,16.5 L 14,16.5 z "
style="stroke-linecap:butt;" />
<path
d="M 11,14 L 11,9 L 15,9 L 15,11 L 20,11 L 20,9 L 25,9 L 25,11 L 30,11 L 30,9 L 34,9 L 34,14 L 11,14 z "
style="stroke-linecap:butt;" />
<path
d="M 12,35.5 L 33,35.5 L 33,35.5"
style="fill:none; stroke:#ffffff; stroke-width:1; stroke-linejoin:miter;" />
<path
d="M 13,31.5 L 32,31.5"
style="fill:none; stroke:#ffffff; stroke-width:1; stroke-linejoin:miter;" />
<path
d="M 14,29.5 L 31,29.5"
style="fill:none; stroke:#ffffff; stroke-width:1; stroke-linejoin:miter;" />
<path
d="M 14,16.5 L 31,16.5"
style="fill:none; stroke:#ffffff; stroke-width:1; stroke-linejoin:miter;" />
<path
d="M 11,14 L 34,14"
style="fill:none; stroke:#ffffff; stroke-width:1; stroke-linejoin:miter;" />
</g>
<!-- black pawn //-->
<g transform="translate(225,45)">
<path
d="M 22,9 C 19.79,9 18,10.79 18,13 C 18,13.89 18.29,14.71 18.78,15.38 C 16.83,16.5 15.5,18.59 15.5,21 C 15.5,23.03 16.44,24.84 17.91,26.03 C 14.91,27.09 10.5,31.58 10.5,39.5 L 33.5,39.5 C 33.5,31.58 29.09,27.09 26.09,26.03 C 27.56,24.84 28.5,23.03 28.5,21 C 28.5,18.59 27.17,16.5 25.22,15.38 C 25.71,14.71 26,13.89 26,13 C 26,10.79 24.21,9 22,9 z "
style="opacity:1; fill:#000000; fill-opacity:1; fill-rule:nonzero; stroke:#000000; stroke-width:1.5; stroke-linecap:round; stroke-linejoin:miter; stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -0,0 +1,19 @@
use glib::ExitCode;
use rdraught::draughts::DraughtsGame;
use rdraught::{draughts::Piece, draughts::Player, position::Position};
use rdraught_ui::run;
use std::collections::HashMap;
fn main() -> ExitCode {
let mut pieces = HashMap::<Position, Piece>::new();
pieces.insert(Position::new(2, 4), Piece::CrownedRedPawn);
pieces.insert(Position::new(5, 5), Piece::CrownedWhitePawn);
let game = DraughtsGame::new(
|p| match pieces.get(&p) {
None => Piece::NoPiece,
Some(piece) => *piece,
},
Player::Red,
);
run(game)
}

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="45" height="45">
<g style="fill:none; fill-opacity:0; fill-rule:evenodd; stroke:#ffc837; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(0,0)">
<path
d="M 22.5,11.63 L 22.5,6"
style="fill:none; stroke:#ffc837; stroke-linejoin:miter;" />
<path
d="M 20,8 L 25,8"
style="fill:none; stroke:#ffc837; stroke-linejoin:miter;" />
<path
d="M 22.5,25 C 22.5,25 27,17.5 25.5,14.5 C 25.5,14.5 24.5,12 22.5,12 C 20.5,12 19.5,14.5 19.5,14.5 C 18,17.5 22.5,25 22.5,25"
style="fill:#ffffff; stroke:#ffc837; stroke-linecap:butt; stroke-linejoin:miter;" />
<path
d="M 11.5,37 C 17,40.5 27,40.5 32.5,37 L 32.5,30 C 32.5,30 41.5,25.5 38.5,19.5 C 34.5,13 25,16 22.5,23.5 L 22.5,27 L 22.5,23.5 C 19,16 9.5,13 6.5,19.5 C 3.5,25.5 11.5,29.5 11.5,29.5 L 11.5,37 z "
style="fill:#ffffff; stroke:#ffc837;" />
<path
d="M 11.5,30 C 17,27 27,27 32.5,30"
style="fill:none; stroke:#ffc837;" />
<path
d="M 11.5,33.5 C 17,30.5 27,30.5 32.5,33.5"
style="fill:none; stroke:#ffc837;" />
<path
d="M 11.5,37 C 17,34 27,34 32.5,37"
style="fill:none; stroke:#ffc837;" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="45" height="45">
<g style="fill:none; fill-opacity:0; fill-rule:evenodd; stroke:#776b00; stroke-width:1.5; stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4; stroke-dasharray:none; stroke-opacity:1;" transform="translate(0,0)">
<path
d="M 22.5,11.63 L 22.5,6"
style="fill:none; stroke:#776b00; stroke-linejoin:miter;" />
<path
d="M 20,8 L 25,8"
style="fill:none; stroke:#776b00; stroke-linejoin:miter;" />
<path
d="M 22.5,25 C 22.5,25 27,17.5 25.5,14.5 C 25.5,14.5 24.5,12 22.5,12 C 20.5,12 19.5,14.5 19.5,14.5 C 18,17.5 22.5,25 22.5,25"
style="fill:#ffffff; stroke:#776b00; stroke-linecap:butt; stroke-linejoin:miter;" />
<path
d="M 11.5,37 C 17,40.5 27,40.5 32.5,37 L 32.5,30 C 32.5,30 41.5,25.5 38.5,19.5 C 34.5,13 25,16 22.5,23.5 L 22.5,27 L 22.5,23.5 C 19,16 9.5,13 6.5,19.5 C 3.5,25.5 11.5,29.5 11.5,29.5 L 11.5,37 z "
style="fill:#ffffff; stroke:#776b00;" />
<path
d="M 11.5,30 C 17,27 27,27 32.5,30"
style="fill:none; stroke:#776b00;" />
<path
d="M 11.5,33.5 C 17,30.5 27,30.5 32.5,33.5"
style="fill:none; stroke:#776b00;" />
<path
d="M 11.5,37 C 17,34 27,34 32.5,37"
style="fill:none; stroke:#776b00;" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -0,0 +1,12 @@
use gtk4::{AlertDialog, Window, prelude::IsA};
use rdraught::Player;
pub(crate) async fn create_dialog<W: IsA<Window>>(window: W, winner: Player) {
let msg = match winner {
Player::Red => String::from("Red player wins"),
Player::White => String::from("White player wins"),
};
let info_dialog = AlertDialog::builder().modal(true).message(msg).build();
info_dialog.show(Some(&window));
}

96
rdraught-gtk/src/geo2d.rs Normal file
View File

@@ -0,0 +1,96 @@
use std::clone::Clone;
use std::cmp::Eq;
use std::cmp::PartialEq;
use std::fmt::Display;
use std::marker::Copy;
use std::ops::Add;
use std::ops::Div;
use std::ops::Mul;
use std::ops::Neg;
use std::ops::Sub;
#[derive(Debug)]
pub struct Point {
x: f64,
y: f64,
}
impl Point {
pub fn x(&self) -> f64 {
self.x
}
pub fn y(&self) -> f64 {
self.y
}
pub fn new(x: f64, y: f64) -> Point {
Point { x, y }
}
}
impl Add<&Point> for &Point {
fn add(self, rhs: &Point) -> Self::Output {
Point {
x: self.x() + rhs.x(),
y: self.y() + rhs.y(),
}
}
type Output = Point;
}
impl Sub<&Point> for &Point {
fn sub(self, rhs: &Point) -> Self::Output {
Point {
x: self.x() - rhs.x(),
y: self.y() - rhs.y(),
}
}
type Output = Point;
}
impl Mul<f64> for &Point {
fn mul(self, rhs: f64) -> Self::Output {
Point::new(self.x * rhs, self.y * rhs)
}
type Output = Point;
}
impl Div<f64> for &Point {
fn div(self, rhs: f64) -> Self::Output {
Point::new(self.x / rhs, self.y / rhs)
}
type Output = Point;
}
impl Neg for &Point {
fn neg(self) -> Self::Output {
Point::new(-self.x(), -self.y())
}
type Output = Point;
}
impl PartialEq for Point {
fn eq(&self, other: &Self) -> bool {
self.x.eq(&other.x) && self.y.eq(&other.y)
}
}
impl Eq for Point {}
impl Clone for Point {
fn clone(&self) -> Self {
*self
}
}
impl Copy for Point {}
impl Display for Point {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Point({}, {})", self.x, self.y)
}
}
impl Point {}

View File

@@ -0,0 +1,62 @@
use gtk4::{Align, Application, Box, CheckButton, Label, Orientation, Window, prelude::*};
use crate::types::SharedMutable;
use rdraught::Player;
pub(crate) fn create(application: &Application, current_player: SharedMutable<Player>) -> Window {
let label = Label::builder().label("Main player:").build();
let red_player_button = CheckButton::builder().label("Red").active(true).build();
let white_player_button = CheckButton::builder()
.label("White")
.active(false)
.group(&red_player_button)
.build();
let current_player_1 = current_player.clone();
red_player_button.connect_toggled(move |b| {
if b.is_active() {
current_player_1.set(Player::Red);
}
});
let current_player_2 = current_player.clone();
white_player_button.connect_toggled(move |b| {
if b.is_active() {
current_player_2.set(Player::White);
}
});
let main_player_selector = Box::builder()
.valign(Align::Center)
.halign(Align::Center)
.spacing(10)
.orientation(Orientation::Horizontal)
.build();
main_player_selector.append(&label);
main_player_selector.append(&red_player_button);
main_player_selector.append(&white_player_button);
let layout = Box::builder()
.valign(Align::Center)
.halign(Align::Center)
.spacing(10)
.orientation(Orientation::Vertical)
.build();
layout.append(&main_player_selector);
layout.set_margin_top(10);
layout.set_margin_bottom(10);
layout.set_margin_start(10);
layout.set_margin_end(10);
let player_select_dialog = Window::builder()
.application(application)
.title("Rdraught")
.modal(true)
.child(&layout)
.visible(true)
.build();
player_select_dialog.connect_close_request(move |window| {
if let Some(application) = window.application() {
application.remove_window(window);
}
glib::Propagation::Proceed
});
player_select_dialog
}

8
rdraught-gtk/src/lib.rs Normal file
View File

@@ -0,0 +1,8 @@
mod geo2d;
mod final_dialog;
mod greeting_dialog;
mod rdraught_application;
mod types;
pub use rdraught_application::run;

7
rdraught-gtk/src/main.rs Normal file
View File

@@ -0,0 +1,7 @@
use glib::ExitCode;
use rdraught::draughts::DraughtsGame;
fn main() -> ExitCode {
let game = DraughtsGame::default();
rdraught_gtk::run(game)
}

View File

@@ -0,0 +1,605 @@
use super::geo2d::Point;
use core::f64::consts::PI;
use gdk4::cairo::{Context as CairoContext, Matrix, Rectangle};
use glib::ExitCode;
use gtk4::glib::{MainContext, Propagation};
use gtk4::{self as gtk, gdk::ffi::GDK_BUTTON_PRIMARY};
use gtk4::{Application, Button, DrawingArea, HeaderBar, prelude::*};
use rdraught::{
DraughtsBoard, DraughtsGame, Move, Piece, Player, Position, RDraughtApplication,
RectangularBoard,
};
use rsvg::SvgHandle;
const SQUARE_SIZE: f64 = 1.0;
use super::final_dialog;
use super::greeting_dialog;
use super::types;
use types::{SharedMutable, SharedMutableRef, new_shared_mut, new_shared_mut_ref};
const CROWN_RED: &[u8] = include_bytes!("crown_red.svg");
const CROWN_WHITE: &[u8] = include_bytes!("crown_white.svg");
fn transform_point(p: &Point, m: &Matrix) -> Point {
let (x, y) = m.transform_point(p.x(), p.y());
Point::new(x, y)
}
trait AugmentedRect {
fn from_points(tl: Point, br: Point) -> Rectangle;
fn center(&self) -> Point;
fn tl(&self) -> Point;
fn br(&self) -> Point;
fn contains(&self, p: &Point) -> bool;
}
impl AugmentedRect for Rectangle {
fn center(&self) -> Point {
Point::new(
self.x() + self.width() / 2.0,
self.y() + self.height() / 2.0,
)
}
fn tl(&self) -> Point {
Point::new(self.x(), self.y())
}
fn br(&self) -> Point {
Point::new(self.x() + self.width(), self.y() + self.height())
}
fn from_points(tl: Point, br: Point) -> Rectangle {
Rectangle::new(tl.x(), tl.y(), br.x() - tl.x(), br.y() - tl.y())
}
fn contains(&self, p: &Point) -> bool {
self.x() < p.x()
&& self.x() + self.width() > p.x()
&& self.y() < p.y()
&& self.y() + self.height() > p.y()
}
}
fn draw_piece(
cr: &CairoContext,
square: &Rectangle,
piece: Piece,
crown_red: &SvgHandle,
crown_white: &SvgHandle,
) -> Result<(), cairo::Error> {
if let Piece::NoPiece = piece {
Ok(())
} else {
cr.save()?;
let center = square.center();
let outer_radius = square.width() * 0.3;
let vertical_scale_factor = 0.8;
let matrix = {
let mut m1 = Matrix::identity();
m1.translate(0.0, -(center.y() - outer_radius));
let mut m2 = Matrix::identity();
m2.scale(1.0, vertical_scale_factor);
let mut m3 = Matrix::identity();
m3.translate(0.0, center.y() - outer_radius * vertical_scale_factor);
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
};
cr.set_matrix(Matrix::multiply(&matrix, &cr.matrix()));
cr.set_source_rgb(0.0, 0.0, 0.0);
let thickness = outer_radius * 0.3;
cr.arc(
center.x(),
center.y() + thickness / 2.0,
outer_radius,
0.0,
2.0 * PI,
);
cr.rectangle(
center.x() - outer_radius,
center.y() - thickness / 2.0,
outer_radius * 2.0,
thickness,
);
cr.arc(
center.x(),
center.y() - thickness / 2.0,
outer_radius,
0.0,
2.0 * PI,
);
cr.fill().unwrap();
let (color, crowned) = match piece {
Piece::NoPiece => return Ok(()),
Piece::SimpleRedPawn => ((1.0, 0.0, 0.0), false),
Piece::SimpleWhitePawn => ((1.0, 1.0, 1.0), false),
Piece::CrownedRedPawn => ((1.0, 0.0, 0.0), true),
Piece::CrownedWhitePawn => ((1.0, 1.0, 1.0), true),
};
let radius = square.width() * 0.275;
cr.set_source_rgb(color.0, color.1, color.2);
cr.arc(
center.x(),
center.y() - thickness / 2.0,
radius,
0.0,
2.0 * PI,
);
cr.fill()?;
cr.restore()?;
if crowned {
let renderer = match piece.player() {
Some(Player::Red) => rsvg::CairoRenderer::new(crown_red),
Some(Player::White) => rsvg::CairoRenderer::new(crown_white),
None => panic!("This should never happen"),
};
let m4 = {
let mut m1 = Matrix::identity();
m1.translate(-center.x(), -(center.y() - thickness / 1.0));
let mut m2 = Matrix::identity();
m2.scale(0.5, 0.5);
let mut m3 = Matrix::identity();
m3.translate(center.x(), center.y() - thickness / 1.0);
Matrix::multiply(&Matrix::multiply(&m1, &m2), &m3)
};
cr.set_matrix(Matrix::multiply(
&Matrix::multiply(&matrix, &m4),
&cr.matrix(),
));
renderer
.render_document(
cr,
&cairo::Rectangle::new(
square.tl().x(),
square.tl().y(),
square.width(),
square.height(),
),
)
.unwrap();
}
Ok(())
}
}
fn draw_score_bar(
cr: &CairoContext,
board: &Rectangle,
draughts_game: &DraughtsGame,
current_player: Player,
) {
fn modulate_score(relative_score: f64) -> f64 {
let x = relative_score;
1.0 - (f64::atan(8.0 * x - 4.0) / f64::atan(4.0) / 2.0 + 0.5)
}
let score_bar = Rectangle::new(
board.tl().x() - board.width() / 10.0,
board.tl().y(),
board.width() / 16.0,
board.height(),
);
let num_rects = 40usize;
let spacing = board.height() / 200.0;
let rect_height = (board.height() - spacing * (num_rects - 1) as f64) / (num_rects as f64);
let score_percentage = modulate_score(draughts_game.relative_score(current_player) as f64);
let threshold = (score_percentage * num_rects as f64) as usize;
let tl = score_bar.tl();
cr.save().unwrap();
cr.set_line_width(spacing / 4.0);
for i in 0..threshold {
match current_player {
Player::White => cr.set_source_rgb(1.0, 0.0, 0.0),
Player::Red => cr.set_source_rgb(1.0, 1.0, 1.0),
}
cr.rectangle(
tl.x(),
tl.y() + i as f64 * (rect_height + spacing),
score_bar.width(),
rect_height,
);
cr.fill().unwrap();
cr.set_source_rgb(0.0, 0.0, 0.0);
cr.rectangle(
tl.x(),
tl.y() + i as f64 * (rect_height + spacing),
score_bar.width(),
rect_height,
);
cr.stroke().unwrap();
}
for i in threshold..num_rects {
match current_player {
Player::White => cr.set_source_rgb(1.0, 1.0, 1.0),
Player::Red => cr.set_source_rgb(1.0, 0.0, 0.0),
}
cr.rectangle(
tl.x(),
tl.y() + i as f64 * (rect_height + spacing),
score_bar.width(),
rect_height,
);
cr.fill().unwrap();
cr.set_source_rgb(0.0, 0.0, 0.0);
cr.rectangle(
tl.x(),
tl.y() + i as f64 * (rect_height + spacing),
score_bar.width(),
rect_height,
);
cr.stroke().unwrap();
}
cr.restore().unwrap();
}
fn create_game_window(
rd: SharedMutableRef<RDraughtApplication>,
application: &Application,
current_player: Player,
) {
// Create a new window.
let window = gtk::ApplicationWindow::builder()
.application(application)
.title("Rdraught")
.default_width(800)
.default_height(800)
.build();
let header_bar = HeaderBar::new();
window.set_titlebar(Some(&header_bar));
let undo_button = Button::new();
undo_button.set_icon_name("edit-undo-symbolic");
undo_button.set_sensitive(false);
let redo_button = Button::new();
redo_button.set_icon_name("edit-redo-symbolic");
redo_button.set_sensitive(false);
let selected_piece: SharedMutable<Option<Position>> = new_shared_mut(None);
let available_moves = new_shared_mut_ref(Vec::<Move>::new());
// Create a DrawingArea widget where we will draw the chessboard.
let drawing_area = DrawingArea::new();
// Add the drawing area to the window.
window.set_child(Some(&drawing_area));
header_bar.pack_start(&undo_button);
{
let da = drawing_area.clone();
let rd = rd.clone();
let undo_btn_clone = undo_button.clone();
let redo_btn_clone = redo_button.clone();
let selected_piece = selected_piece.clone();
let available_moves = available_moves.clone();
undo_button.connect_clicked(move |_| {
let mut rd = rd.borrow_mut();
if rd.can_undo() {
rd.undo().ok();
redo_btn_clone.set_sensitive(rd.can_redo());
undo_btn_clone.set_sensitive(rd.can_undo());
selected_piece.set(None);
available_moves.borrow_mut().clear();
da.queue_draw();
}
});
}
header_bar.pack_start(&redo_button);
{
let da = drawing_area.clone();
let rd = rd.clone();
let undo_btn_clone = undo_button.clone();
let redo_btn_clone = redo_button.clone();
let selected_piece = selected_piece.clone();
let available_moves = available_moves.clone();
redo_button.connect_clicked(move |_| {
let mut rd = rd.borrow_mut();
if rd.can_redo() {
rd.redo().ok();
redo_btn_clone.set_sensitive(rd.can_redo());
undo_btn_clone.set_sensitive(rd.can_undo());
selected_piece.set(None);
available_moves.borrow_mut().clear();
da.queue_draw();
}
});
}
// Get the allocation information for the widget.
let board_width = SQUARE_SIZE * DraughtsBoard::rows() as f64;
let board_height = SQUARE_SIZE * DraughtsBoard::columns() as f64;
let board = Rectangle::from_points(Point::new(0.0, 0.0), Point::new(board_width, board_height));
let board_with_bar = Rectangle::from_points(
Point::new(-board_width / 10.0, 0.0),
Point::new(board_width, board_height),
);
let board_clone = board;
let crown_red_handle = {
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_RED));
rsvg::Loader::new()
.read_stream(
&stream,
None::<&gio::File>, // no base file as this document has no references
None::<&gio::Cancellable>, // no cancellable
)
.unwrap()
};
let crown_white_handle = {
let stream = gio::MemoryInputStream::from_bytes(&glib::Bytes::from_static(CROWN_WHITE));
rsvg::Loader::new()
.read_stream(
&stream,
None::<&gio::File>, // no base file as this document has no references
None::<&gio::Cancellable>, // no cancellable
)
.unwrap()
};
let xform = new_shared_mut_ref(Matrix::identity());
// Set the "draw" function of the drawing area. This callback is called
// whenever GTK needs to redraw this widget (for example, on first display or when resized).
{
let rd = rd.clone();
let xform = xform.clone();
let selected_piece = selected_piece.clone();
let available_moves = available_moves.clone();
drawing_area.set_draw_func(move |_widget, cr, width, height| {
let screen = Rectangle::from_points(
Point::new(0.0, 0.0),
Point::new(width as f64, height as f64),
);
let f = f64::min(
screen.width() / board_with_bar.width(),
screen.height() / board_with_bar.height(),
);
let screen_center = screen.center();
let board_center = board_with_bar.center();
let mut xform = xform.borrow_mut();
*xform = Matrix::multiply(
&Matrix::multiply(
&Matrix::new(1.0, 0.0, 0.0, 1.0, -board_center.x(), -board_center.y()),
&Matrix::new(f, 0.0, 0.0, f, 0.0, 0.0),
),
&Matrix::new(1.0, 0.0, 0.0, 1.0, screen_center.x(), screen_center.y()),
);
cr.set_matrix(*xform);
// Loop over rows and columns to draw each chessboard cell.
for row in 0..DraughtsBoard::rows() {
for col in 0..DraughtsBoard::columns() {
let position = match current_player {
Player::Red => Position::new(row as u8, (8 - 1 - col) as u8),
Player::White => Position::new((8 - 1 - row) as u8, col as u8),
};
let square = Rectangle::new(
col as f64 * SQUARE_SIZE,
row as f64 * SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE,
);
cr.save().unwrap();
// Alternate colors based on the sum of row and column indices.
if (row + col) % 2 == 0 {
cr.set_source_rgb(0.8, 0.8, 0.6);
} else {
cr.set_source_rgb(0.4, 0.4, 0.2);
}
// Draw and fill the square.
cr.rectangle(
square.tl().x(),
square.tl().y(),
square.width(),
square.height(),
);
cr.fill().unwrap();
draw_piece(
cr,
&square,
position.map_or(Piece::NoPiece, |p| rd.borrow().game().piece_at(p)),
&crown_red_handle,
&crown_white_handle,
)
.unwrap();
cr.restore().unwrap();
}
}
if let Some(selected_position) = selected_piece.get() {
let screen_position = match current_player {
Player::White => (8 - 1 - selected_position.row(), selected_position.col()),
Player::Red => (selected_position.row(), 8 - 1 - selected_position.col()),
};
let square = Rectangle::new(
screen_position.1 as f64 * SQUARE_SIZE,
screen_position.0 as f64 * SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE,
);
cr.save().unwrap();
cr.new_path();
cr.move_to(square.tl().x(), square.tl().y());
cr.line_to(square.tl().x(), square.br().y());
cr.line_to(square.br().x(), square.br().y());
cr.line_to(square.br().x(), square.tl().y());
cr.line_to(square.tl().x(), square.tl().y());
cr.clip();
cr.new_path();
cr.set_source_rgb(0.0, 0.0, 1.0);
cr.set_line_width((square.width() + square.height()) * 0.035);
cr.move_to(square.tl().x(), square.tl().y());
cr.line_to(square.tl().x(), square.br().y());
cr.line_to(square.br().x(), square.br().y());
cr.line_to(square.br().x(), square.tl().y());
cr.line_to(square.tl().x(), square.tl().y());
cr.stroke().unwrap();
cr.restore().unwrap();
}
let am = available_moves.borrow();
if !am.is_empty() {
for mv in am.iter() {
let end_pos = mv.get_end_position();
let screen_position = match current_player {
Player::White => (8 - 1 - end_pos.row(), end_pos.col()),
Player::Red => (end_pos.row(), 8 - 1 - end_pos.col()),
};
let square = Rectangle::new(
screen_position.1 as f64 * SQUARE_SIZE,
screen_position.0 as f64 * SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE,
);
cr.save().unwrap();
cr.new_path();
cr.move_to(square.tl().x(), square.tl().y());
cr.line_to(square.tl().x(), square.br().y());
cr.line_to(square.br().x(), square.br().y());
cr.line_to(square.br().x(), square.tl().y());
cr.line_to(square.tl().x(), square.tl().y());
cr.clip();
cr.new_path();
cr.set_source_rgb(0.0, 1.0, 0.0);
cr.set_line_width((square.width() + square.height()) * 0.035);
cr.move_to(square.tl().x(), square.tl().y());
cr.line_to(square.tl().x(), square.br().y());
cr.line_to(square.br().x(), square.br().y());
cr.line_to(square.br().x(), square.tl().y());
cr.line_to(square.tl().x(), square.tl().y());
cr.stroke().unwrap();
cr.restore().unwrap();
}
}
draw_score_bar(cr, &board, rd.borrow().game(), current_player);
});
}
let gesture = gtk::GestureClick::new();
// Set the gestures button to the right mouse button (=3)
gesture.set_button(GDK_BUTTON_PRIMARY as u32);
// Assign your handler to an event of the gesture (e.g. the `pressed` event)
{
let drawing_area = drawing_area.clone();
let available_moves = available_moves.clone();
let window = window.clone();
let undo_btn_clone = undo_button.clone();
let redo_btn_clone = redo_button.clone();
gesture.connect_pressed(move |gesture, _, x, y| {
gesture.set_state(gtk::EventSequenceState::Claimed);
if let Some(winner) = rd.borrow().game().winner() {
MainContext::default()
.spawn_local(final_dialog::create_dialog(window.clone(), winner));
} else {
let xform = xform.borrow();
let inverse = {
let mut m = *xform;
m.invert();
m
};
let p = transform_point(&Point::new(x, y), &inverse);
if board_clone.contains(&p) {
let p = &p - &board_clone.tl();
// println!("Point: {:?}", p);
let position = match current_player {
Player::White => Position::new(
(8.0 - (p.y() / SQUARE_SIZE)) as u8,
(p.x() / SQUARE_SIZE) as u8,
),
Player::Red => Position::new(
(p.y() / SQUARE_SIZE) as u8,
(8.0 - p.x() / SQUARE_SIZE) as u8,
),
};
// println!("Selected position: {:?}", position);
let piece = {
let rd = rd.borrow();
let draughts_game = rd.game();
position
.clone()
.map_or(Piece::NoPiece, |it| draughts_game.piece_at(it))
// println!("Selected piece: {:?}", piece);
};
let am = available_moves.replace(Vec::new());
let mut move_applied = false;
if !am.is_empty() {
if let Ok(pos) = position {
for mv in am.into_iter() {
if mv.get_end_position() == pos {
let mut rd_app = rd.borrow_mut();
// println!("Applied move: {:?}", mv);
rd_app.apply_move(mv).unwrap();
// let game_copy = rd_app.game().clone();
// thread::spawn(move || {
// if let (Some(mv), analyzed_moves) =
// game_copy.get_best_move(10)
// {
// println!(
// "Next best move: {:?}, analyzed moves: {}",
// mv, analyzed_moves
// );
// }
// });
move_applied = true;
break;
}
}
}
if move_applied {
selected_piece.set(None);
let rd = rd.borrow();
undo_btn_clone.set_sensitive(rd.can_undo());
redo_btn_clone.set_sensitive(rd.can_redo());
}
}
if !move_applied {
let position = position.ok();
match piece.player() {
Some(Player::Red) => selected_piece.set(position),
Some(Player::White) => selected_piece.set(position),
None => selected_piece.set(None),
}
if piece.player().is_none() {
selected_piece.set(None)
} else {
let mut am = available_moves.borrow_mut();
selected_piece.set(position);
if let Some(position) = position {
for mv in rd.borrow().game().moves_for_piece(position) {
am.push(mv);
}
}
}
}
} else {
selected_piece.set(None);
}
drawing_area.queue_draw();
}
});
}
// Assign the gesture to the treeview
drawing_area.add_controller(gesture);
window.present();
}
fn on_activate(application: &Application, game: DraughtsGame) {
// Initialize GTK before using any GTK functions.
if gtk::init().is_err() {
panic!("Failed to initialize GTK.");
}
let current_player = new_shared_mut(Player::Red);
let dialog = greeting_dialog::create(application, current_player.clone());
let application = application.clone();
dialog.connect_close_request(move |w| {
application.remove_window(w);
let rd = new_shared_mut_ref(RDraughtApplication::new(game.clone()));
create_game_window(rd.clone(), &application, current_player.get());
Propagation::Proceed
});
}
pub fn run(game: DraughtsGame) -> ExitCode {
// Create a new application with the builder pattern
let app = Application::builder()
.application_id("net.woggioni.rdraught")
.build();
app.connect_activate(move |it| on_activate(it, game.clone()));
app.run()
}

12
rdraught-gtk/src/types.rs Normal file
View File

@@ -0,0 +1,12 @@
use std::cell::{Cell, RefCell};
use std::rc::Rc;
pub(crate) type SharedMutable<T> = Rc<Cell<T>>;
pub(crate) type SharedMutableRef<T> = Rc<RefCell<T>>;
pub(crate) fn new_shared_mut_ref<T>(obj: T) -> SharedMutableRef<T> {
Rc::new(RefCell::new(obj))
}
pub(crate) fn new_shared_mut<T>(obj: T) -> SharedMutable<T> {
Rc::new(Cell::new(obj))
}