Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -23,45 +23,60 @@ def render_board():
|
|
23 |
html_content = f"""
|
24 |
<style>
|
25 |
#chess-board {{
|
26 |
-
user-select: none;
|
27 |
position: relative;
|
28 |
width: 400px;
|
29 |
margin: auto;
|
30 |
}}
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
}}
|
35 |
.piece {{
|
36 |
cursor: grab;
|
|
|
|
|
|
|
|
|
37 |
}}
|
38 |
.square {{
|
39 |
-
|
40 |
}}
|
41 |
.highlight {{
|
42 |
-
fill:
|
43 |
-
|
|
|
|
|
44 |
}}
|
45 |
-
.
|
46 |
-
|
47 |
-
|
48 |
}}
|
49 |
</style>
|
50 |
|
51 |
<div id="chess-board">
|
52 |
{board_svg}
|
|
|
53 |
</div>
|
54 |
|
55 |
<script>
|
56 |
(function() {{
|
57 |
const board = document.querySelector('#chess-board');
|
58 |
-
|
|
|
|
|
|
|
59 |
let startSquare = null;
|
|
|
60 |
let isDragging = false;
|
61 |
-
let
|
62 |
-
|
63 |
-
let originalTransform = null;
|
64 |
-
|
65 |
// Convertir les coordonnées en notation algébrique
|
66 |
function getSquareFromCoords(x, y) {{
|
67 |
const boardRect = board.getBoundingClientRect();
|
@@ -74,46 +89,55 @@ def render_board():
|
|
74 |
return null;
|
75 |
}}
|
76 |
|
77 |
-
//
|
78 |
-
function
|
79 |
-
const
|
80 |
-
|
81 |
-
|
82 |
}}
|
83 |
|
84 |
-
//
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
const piece = e.target.closest('.piece');
|
87 |
if (piece && piece.classList.contains('white')) {{
|
88 |
-
isDragging = true;
|
89 |
-
draggedPiece = piece;
|
90 |
-
dragStartX = e.clientX;
|
91 |
-
dragStartY = e.clientY;
|
92 |
-
startSquare = getSquareFromCoords(e.clientX, e.clientY);
|
93 |
-
originalTransform = window.getComputedStyle(piece).transform;
|
94 |
-
|
95 |
-
piece.classList.add('dragging');
|
96 |
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
}}
|
98 |
}});
|
99 |
|
100 |
-
//
|
101 |
document.addEventListener('mousemove', function(e) {{
|
102 |
-
if (isDragging &&
|
103 |
-
|
|
|
|
|
104 |
}}
|
105 |
}});
|
106 |
|
107 |
-
//
|
108 |
document.addEventListener('mouseup', function(e) {{
|
109 |
-
if (isDragging &&
|
110 |
const endSquare = getSquareFromCoords(e.clientX, e.clientY);
|
111 |
|
112 |
-
// Réinitialiser la position de la pièce
|
113 |
-
draggedPiece.style.transform = originalTransform;
|
114 |
-
draggedPiece.classList.remove('dragging');
|
115 |
-
|
116 |
-
// Envoyer le coup si valide
|
117 |
if (startSquare && endSquare && startSquare !== endSquare) {{
|
118 |
window.parent.postMessage({{
|
119 |
type: 'chess_move',
|
@@ -122,9 +146,14 @@ def render_board():
|
|
122 |
}}, '*');
|
123 |
}}
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
isDragging = false;
|
126 |
-
draggedPiece = null;
|
127 |
-
startSquare = null;
|
128 |
}}
|
129 |
}});
|
130 |
}})();
|
|
|
23 |
html_content = f"""
|
24 |
<style>
|
25 |
#chess-board {{
|
|
|
26 |
position: relative;
|
27 |
width: 400px;
|
28 |
margin: auto;
|
29 |
}}
|
30 |
+
#chess-board svg {{
|
31 |
+
position: absolute;
|
32 |
+
top: 0;
|
33 |
+
left: 0;
|
34 |
+
width: 100%;
|
35 |
+
height: 100%;
|
36 |
+
pointer-events: none;
|
37 |
+
}}
|
38 |
+
#chess-board svg * {{
|
39 |
+
pointer-events: auto;
|
40 |
}}
|
41 |
.piece {{
|
42 |
cursor: grab;
|
43 |
+
pointer-events: auto;
|
44 |
+
}}
|
45 |
+
.piece.dragging {{
|
46 |
+
cursor: grabbing;
|
47 |
}}
|
48 |
.square {{
|
49 |
+
pointer-events: auto;
|
50 |
}}
|
51 |
.highlight {{
|
52 |
+
fill: rgba(255, 255, 0, 0.3);
|
53 |
+
}}
|
54 |
+
.possible-move {{
|
55 |
+
fill: rgba(0, 255, 0, 0.3);
|
56 |
}}
|
57 |
+
.ghost-piece {{
|
58 |
+
opacity: 0.3;
|
59 |
+
pointer-events: none;
|
60 |
}}
|
61 |
</style>
|
62 |
|
63 |
<div id="chess-board">
|
64 |
{board_svg}
|
65 |
+
<div id="drag-layer" style="position: absolute; top: 0; left: 0; pointer-events: none;"></div>
|
66 |
</div>
|
67 |
|
68 |
<script>
|
69 |
(function() {{
|
70 |
const board = document.querySelector('#chess-board');
|
71 |
+
const svg = board.querySelector('svg');
|
72 |
+
const dragLayer = document.getElementById('drag-layer');
|
73 |
+
let selectedPiece = null;
|
74 |
+
let ghostPiece = null;
|
75 |
let startSquare = null;
|
76 |
+
let currentSquare = null;
|
77 |
let isDragging = false;
|
78 |
+
let offset = {{ x: 0, y: 0 }};
|
79 |
+
|
|
|
|
|
80 |
// Convertir les coordonnées en notation algébrique
|
81 |
function getSquareFromCoords(x, y) {{
|
82 |
const boardRect = board.getBoundingClientRect();
|
|
|
89 |
return null;
|
90 |
}}
|
91 |
|
92 |
+
// Créer une copie fantôme de la pièce pour le glissement
|
93 |
+
function createGhostPiece(piece) {{
|
94 |
+
const clone = piece.cloneNode(true);
|
95 |
+
clone.classList.add('ghost-piece');
|
96 |
+
return clone;
|
97 |
}}
|
98 |
|
99 |
+
// Mettre à jour la position du fantôme
|
100 |
+
function updateGhostPosition(x, y) {{
|
101 |
+
if (ghostPiece) {{
|
102 |
+
ghostPiece.style.transform = `translate(${{x - offset.x}}px, ${{y - offset.y}}px)`;
|
103 |
+
}}
|
104 |
+
}}
|
105 |
+
|
106 |
+
// Gérer le début du glissement
|
107 |
+
svg.addEventListener('mousedown', function(e) {{
|
108 |
const piece = e.target.closest('.piece');
|
109 |
if (piece && piece.classList.contains('white')) {{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
e.preventDefault();
|
111 |
+
selectedPiece = piece;
|
112 |
+
const rect = piece.getBoundingClientRect();
|
113 |
+
offset.x = e.clientX - rect.left;
|
114 |
+
offset.y = e.clientY - rect.top;
|
115 |
+
|
116 |
+
// Créer et positionner le fantôme
|
117 |
+
ghostPiece = createGhostPiece(piece);
|
118 |
+
dragLayer.appendChild(ghostPiece);
|
119 |
+
updateGhostPosition(e.clientX, e.clientY);
|
120 |
+
|
121 |
+
startSquare = getSquareFromCoords(e.clientX, e.clientY);
|
122 |
+
isDragging = true;
|
123 |
+
selectedPiece.classList.add('dragging');
|
124 |
}}
|
125 |
}});
|
126 |
|
127 |
+
// Gérer le glissement
|
128 |
document.addEventListener('mousemove', function(e) {{
|
129 |
+
if (isDragging && ghostPiece) {{
|
130 |
+
e.preventDefault();
|
131 |
+
updateGhostPosition(e.clientX, e.clientY);
|
132 |
+
currentSquare = getSquareFromCoords(e.clientX, e.clientY);
|
133 |
}}
|
134 |
}});
|
135 |
|
136 |
+
// Gérer la fin du glissement
|
137 |
document.addEventListener('mouseup', function(e) {{
|
138 |
+
if (isDragging && selectedPiece) {{
|
139 |
const endSquare = getSquareFromCoords(e.clientX, e.clientY);
|
140 |
|
|
|
|
|
|
|
|
|
|
|
141 |
if (startSquare && endSquare && startSquare !== endSquare) {{
|
142 |
window.parent.postMessage({{
|
143 |
type: 'chess_move',
|
|
|
146 |
}}, '*');
|
147 |
}}
|
148 |
|
149 |
+
// Nettoyer
|
150 |
+
if (ghostPiece) {{
|
151 |
+
dragLayer.removeChild(ghostPiece);
|
152 |
+
ghostPiece = null;
|
153 |
+
}}
|
154 |
+
selectedPiece.classList.remove('dragging');
|
155 |
+
selectedPiece = null;
|
156 |
isDragging = false;
|
|
|
|
|
157 |
}}
|
158 |
}});
|
159 |
}})();
|