map-editor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. (function(){
  2. var domReady = (window.MG && window.MG.domReady) || window.__MG_domReady || function(cb){ if (document.readyState==='loading') document.addEventListener('DOMContentLoaded', cb); else cb(); };
  3. domReady(function(){
  4. // On attend que la variable mapData soit définie dans la vue (home.php injecte mapData)
  5. if (typeof window.mapData === 'undefined' && typeof mapData !== 'undefined') {
  6. window.mapData = mapData;
  7. }
  8. // --- Début du code de MapEditor (copié depuis l'ancien fichier non-modulaire) ---
  9. class MapEditor {
  10. constructor() {
  11. this.canvas = document.getElementById('mapCanvas');
  12. this.container = document.querySelector('.map-canvas-container');
  13. this.zoomLevel = 1;
  14. this.panOffset = { x: 0, y: 0 };
  15. this.isDragging = false;
  16. this.lastMousePos = { x: 0, y: 0 };
  17. this.selectedTile = null;
  18. this.tileSize = 50; // Rayon de l'hexagone (du centre au sommet)
  19. this.init();
  20. }
  21. init() {
  22. if (!this.canvas) {
  23. console.error('MapEditor: Élément canvas non trouvé!');
  24. return;
  25. }
  26. if (!this.container) {
  27. console.error('MapEditor: Conteneur non trouvé!');
  28. return;
  29. }
  30. this.setupCanvas();
  31. this.renderMap();
  32. this.setupEventListeners();
  33. this.hideLoading();
  34. this.fitToScreen();
  35. }
  36. setupCanvas() {
  37. this.canvas.style.transformOrigin = '0 0';
  38. }
  39. axialToPixel(q, r) {
  40. if (window.MG && window.MG.geom && typeof window.MG.geom.axialToPixel === 'function') {
  41. return window.MG.geom.axialToPixel(this.tileSize, q, r);
  42. }
  43. // fallback local implementation
  44. const size = this.tileSize;
  45. const x = size * Math.sqrt(3) * (q + r / 2);
  46. const y = size * 1.5 * r;
  47. return { x, y };
  48. }
  49. renderMap() {
  50. this.canvas.innerHTML = '';
  51. const hexWidth = this.tileSize * Math.sqrt(3);
  52. const hexHeight = this.tileSize * 2;
  53. const mapWidth = (window.mapData && window.mapData.width) || 5;
  54. const mapHeight = (window.mapData && window.mapData.height) || 5;
  55. const radius = Math.floor((Math.min(mapWidth, mapHeight) - 1) / 2);
  56. if (radius < 0) return;
  57. const positions = [];
  58. let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
  59. for (let q = -radius; q <= radius; q++) {
  60. const rMin = Math.max(-radius, -q - radius);
  61. const rMax = Math.min(radius, -q + radius);
  62. for (let r = rMin; r <= rMax; r++) {
  63. const { x, y } = this.axialToPixel(q, r);
  64. positions.push({ q, r, x, y });
  65. const halfW = hexWidth / 2;
  66. const halfH = hexHeight / 2;
  67. if (x - halfW < minX) minX = x - halfW;
  68. if (y - halfH < minY) minY = y - halfH;
  69. if (x + halfW > maxX) maxX = x + halfW;
  70. if (y + halfH > maxY) maxY = y + halfH;
  71. }
  72. }
  73. const padding = 20;
  74. const canvasWidth = (maxX - minX) + hexWidth + padding;
  75. const canvasHeight = (maxY - minY) + hexHeight + padding;
  76. this.canvas.style.width = canvasWidth + 'px';
  77. this.canvas.style.height = canvasHeight + 'px';
  78. const offsetX = -minX + padding / 2;
  79. const offsetY = -minY + padding / 2;
  80. for (const pos of positions) {
  81. this.renderHexagon(pos.q, pos.r, pos.x + offsetX, pos.y + offsetY, hexWidth, hexHeight);
  82. }
  83. }
  84. renderHexagon(q, r, x, y, hexWidth, hexHeight) {
  85. const hex = document.createElement('div');
  86. hex.className = 'hexagon';
  87. hex.dataset.q = q;
  88. hex.dataset.r = r;
  89. const expansion = Math.round(hexWidth * 0.004 * 1000) / 1000;
  90. const adjWidth = hexWidth + expansion;
  91. const adjHeight = hexHeight + expansion;
  92. const halfW = adjWidth / 2;
  93. const halfH = adjHeight / 2;
  94. hex.style.width = adjWidth + 'px';
  95. hex.style.height = adjHeight + 'px';
  96. hex.style.left = (Math.round((x - halfW) * 1000) / 1000) + 'px';
  97. hex.style.top = (Math.round((y - halfH) * 1000) / 1000) + 'px';
  98. hex.style.position = 'absolute';
  99. hex.style.cursor = 'pointer';
  100. hex.style.transition = 'background-color 0.2s';
  101. hex.style.backgroundColor = '#ffffff';
  102. hex.style.clipPath = 'polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%)';
  103. hex.addEventListener('click', (e) => {
  104. e.stopPropagation();
  105. this.selectTile(q, r);
  106. });
  107. hex.addEventListener('mouseenter', () => {
  108. if (!this.selectedTile || this.selectedTile.q !== q || this.selectedTile.r !== r) {
  109. hex.style.backgroundColor = '#e3f2fd';
  110. }
  111. });
  112. hex.addEventListener('mouseleave', () => {
  113. if (!this.selectedTile || this.selectedTile.q !== q || this.selectedTile.r !== r) {
  114. hex.style.backgroundColor = '#ffffff';
  115. }
  116. });
  117. this.canvas.appendChild(hex);
  118. }
  119. selectTile(q, r) {
  120. if (this.selectedTile) {
  121. const prevHex = this.canvas.querySelector(`[data-q="${this.selectedTile.q}"][data-r="${this.selectedTile.r}"]`);
  122. if (prevHex) {
  123. prevHex.style.backgroundColor = '#ffffff';
  124. prevHex.style.borderColor = '#666';
  125. }
  126. }
  127. this.selectedTile = { q, r };
  128. const hex = this.canvas.querySelector(`[data-q="${q}"][data-r="${r}"]`);
  129. if (hex) {
  130. hex.style.backgroundColor = '#2196f3';
  131. hex.style.borderColor = '#1976d2';
  132. }
  133. this.showTileInfo(q, r);
  134. }
  135. showTileInfo(q, r) {
  136. const tileInfo = document.getElementById('tileInfo');
  137. const tilePosition = document.getElementById('tilePosition');
  138. const tileType = document.getElementById('tileType');
  139. const tileProperties = document.getElementById('tileProperties');
  140. tilePosition.textContent = `Q: ${q}, R: ${r}`;
  141. tileType.textContent = 'Vide';
  142. tileProperties.innerHTML = `...`;
  143. tileInfo.style.display = 'block';
  144. }
  145. setupEventListeners() {
  146. document.getElementById('zoomIn').addEventListener('click', (e) => { e.preventDefault(); this.zoom(1.2); });
  147. document.getElementById('zoomOut').addEventListener('click', (e) => { e.preventDefault(); this.zoom(0.8); });
  148. document.getElementById('fitToScreen').addEventListener('click', (e) => { e.preventDefault(); this.fitToScreen(); });
  149. this.container.addEventListener('mousedown', (e) => {
  150. if (e.target === this.container) {
  151. this.isDragging = true;
  152. this.lastMousePos = { x: e.clientX, y: e.clientY };
  153. this.canvas.style.cursor = 'grabbing';
  154. }
  155. });
  156. document.addEventListener('mousemove', (e) => {
  157. if (this.isDragging) {
  158. const deltaX = e.clientX - this.lastMousePos.x;
  159. const deltaY = e.clientY - this.lastMousePos.y;
  160. this.panOffset.x += deltaX;
  161. this.panOffset.y += deltaY;
  162. this.updateTransform();
  163. this.lastMousePos = { x: e.clientX, y: e.clientY };
  164. }
  165. });
  166. document.addEventListener('mouseup', () => { this.isDragging = false; this.canvas.style.cursor = 'grab'; });
  167. this.container.addEventListener('click', () => {
  168. if (this.selectedTile) { this.selectTile(null, null); document.getElementById('tileInfo').style.display = 'none'; }
  169. });
  170. }
  171. zoom(factor) {
  172. if (window.MG && window.MG.geom && typeof window.MG.geom.computeZoom === 'function') {
  173. this.zoomLevel = window.MG.geom.computeZoom(this.zoomLevel, factor);
  174. } else {
  175. this.zoomLevel *= factor;
  176. this.zoomLevel = Math.max(0.1, Math.min(5, this.zoomLevel));
  177. }
  178. this.updateTransform();
  179. const el = document.getElementById('zoomLevel'); if (el) el.textContent = Math.round(this.zoomLevel * 100);
  180. }
  181. fitToScreen() { const containerRect = this.container.getBoundingClientRect(); const canvasRect = this.canvas.getBoundingClientRect(); const scaleX = containerRect.width / canvasRect.width; const scaleY = containerRect.height / canvasRect.height; const scale = Math.min(scaleX, scaleY) * 0.9; this.zoomLevel = scale; this.panOffset = { x: 0, y: 0 }; this.updateTransform(); document.getElementById('zoomLevel').textContent = Math.round(this.zoomLevel * 100); }
  182. updateTransform() { const tx = Math.round(this.panOffset.x); const ty = Math.round(this.panOffset.y); this.canvas.style.transform = `translate(${tx}px, ${ty}px) scale(${this.zoomLevel})`; }
  183. hideLoading() { const loading = document.getElementById('mapLoading'); if (loading) { loading.classList.remove('d-flex'); loading.style.display = 'none'; } }
  184. }
  185. // Initialiser l'éditeur
  186. (function() {
  187. function initMapEditor() {
  188. try { new MapEditor(); } catch (error) { console.error('Erreur lors de la création de MapEditor:', error); }
  189. }
  190. if (document.readyState === 'loading') {
  191. document.addEventListener('DOMContentLoaded', initMapEditor);
  192. } else {
  193. initMapEditor();
  194. }
  195. })();
  196. // --- Fin du code MapEditor ---
  197. });
  198. })();