11"use strict" ;
22
33let timer = "stop" ;
4- let timerId = 0 ;
54let generationFigure = 0 ;
6- let timerTime = 1000 ;
75let isDragging = false ;
86let dragMode = false ; // true: 黒にする, false: 白にする
7+ let isPlacingTemplate = false ;
8+ let patternShape = [ ] ;
9+ let patternHeight = 0 ;
10+ let patternWidth = 0 ;
11+ let previewCells = [ ] ;
912
1013//変数設定
1114let boardSize = 20 ; //盤面の大きさ(20x20)
@@ -51,20 +54,49 @@ function renderBoard() {
5154 button . style . height = `${ cellSize } px` ;
5255 button . style . padding = "0" ; //cellSizeが小さいとき、セルが横長になることを防ぐ
5356 button . style . display = "block" ; //cellSizeが小さいとき、行間が空きすぎるのを防ぐ
57+ button . onclick = ( ) => {
58+ if ( isPlacingTemplate ) {
59+ clearPreview ( ) ;
60+ isPlacingTemplate = false ;
61+ if ( i + patternHeight < boardSize + 1 && j + patternWidth < boardSize + 1 ) {
62+ for ( let r = 0 ; r < patternHeight ; r ++ ) {
63+ for ( let c = 0 ; c < patternWidth ; c ++ ) {
64+ const boardRow = i + r ;
65+ const boardCol = j + c ;
66+ board [ boardRow ] [ boardCol ] = patternShape [ r ] [ c ] === 1 ;
67+ }
68+ }
69+ rerender ( ) ;
70+ generationChange ( 0 ) ;
71+ stop ( ) ;
72+ } else {
73+ window . parent . postMessage (
74+ {
75+ type : "Size shortage" ,
76+ data : { } ,
77+ } ,
78+ "*" ,
79+ ) ;
80+ }
81+ }
82+ } ;
5483 button . onmousedown = ( e ) => {
5584 e . preventDefault ( ) ;
56- if ( timer === "stop" ) {
85+ if ( timer === "stop" && ! isPlacingTemplate ) {
5786 isDragging = true ;
5887 board [ i ] [ j ] = ! board [ i ] [ j ] ;
5988 dragMode = board [ i ] [ j ] ;
6089 button . style . backgroundColor = board [ i ] [ j ] ? "black" : "white" ;
6190 }
6291 } ;
6392 button . onmouseenter = ( ) => {
64- if ( isDragging && timer === "stop" && board [ i ] [ j ] !== dragMode ) {
93+ if ( isDragging && timer === "stop" && board [ i ] [ j ] !== dragMode && ! isPlacingTemplate ) {
6594 board [ i ] [ j ] = dragMode ;
6695 button . style . backgroundColor = board [ i ] [ j ] ? "black" : "white" ;
6796 }
97+ if ( isPlacingTemplate ) {
98+ drawPreview ( i , j ) ;
99+ }
68100 } ;
69101 td . appendChild ( button ) ;
70102 tr . appendChild ( td ) ;
@@ -73,6 +105,37 @@ function renderBoard() {
73105 }
74106}
75107
108+ table . onmouseleave = ( ) => {
109+ if ( isPlacingTemplate ) {
110+ clearPreview ( ) ;
111+ }
112+ } ;
113+
114+ function drawPreview ( row , col ) {
115+ clearPreview ( ) ;
116+ for ( let r = 0 ; r < patternHeight ; r ++ ) {
117+ for ( let c = 0 ; c < patternWidth ; c ++ ) {
118+ if ( patternShape [ r ] [ c ] === 1 ) {
119+ const boardRow = row + r ;
120+ const boardCol = col + c ;
121+ if ( boardRow < boardSize && boardCol < boardSize ) {
122+ const cell = table . rows [ boardRow ] . cells [ boardCol ] . firstChild ;
123+ cell . style . backgroundColor = "gray" ;
124+ previewCells . push ( { row : boardRow , col : boardCol } ) ;
125+ }
126+ }
127+ }
128+ }
129+ }
130+
131+ function clearPreview ( ) {
132+ previewCells . forEach ( ( cellPos ) => {
133+ const cell = table . rows [ cellPos . row ] . cells [ cellPos . col ] . firstChild ;
134+ cell . style . backgroundColor = board [ cellPos . row ] [ cellPos . col ] ? "black" : "white" ;
135+ } ) ;
136+ previewCells = [ ] ;
137+ }
138+
76139function rerender ( ) {
77140 // 2回目以降の盤面生成
78141 for ( let i = 0 ; i < boardSize ; i ++ ) {
@@ -158,30 +221,25 @@ function progressBoard() {
158221 rerender ( ) ;
159222}
160223
161- function resetTimer ( ) {
162- if ( timer !== "stop" ) {
163- timer = "stop" ;
164- clearInterval ( timerId ) ;
165- }
166- }
167-
168224//イベント
169225
226+ on . progress = ( ) => {
227+ progressBoard ( ) ;
228+ } ;
229+
170230on . play = ( ) => {
171231 timer = "start" ;
172- timerId = setInterval ( progressBoard , timerTime ) ;
173232} ;
174233
175234on . pause = ( ) => {
176- resetTimer ( ) ;
235+ timer = "stop" ;
177236} ;
178237
179238on . board_reset = ( ) => {
180239 //すべて白にBoardを変更
181240 board = Array . from ( { length : boardSize } , ( ) => Array . from ( { length : boardSize } , ( ) => false ) ) ;
182241 renderBoard ( ) ;
183242 generationChange ( 0 ) ;
184- resetTimer ( ) ;
185243} ;
186244
187245on . board_randomize = ( ) => {
@@ -191,15 +249,6 @@ on.board_randomize = () => {
191249 ) ;
192250 renderBoard ( ) ;
193251 generationChange ( 0 ) ;
194- resetTimer ( ) ;
195- } ;
196-
197- on . timer_change = ( ms ) => {
198- timerTime = ms ;
199- if ( timer === "start" ) {
200- clearInterval ( timerId ) ;
201- timerId = setInterval ( progressBoard , timerTime ) ;
202- }
203252} ;
204253
205254on . request_sync = ( ) => {
@@ -216,11 +265,12 @@ on.request_sync = () => {
216265 console . log ( "generationFigure:" , generationFigure , "boardSize:" , boardSize ) ;
217266} ;
218267
219- on . place_template = ( newBoard ) => {
220- board = newBoard ;
221- renderBoard ( ) ;
222- generationChange ( 0 ) ;
223- resetTimer ( ) ;
268+ on . place_template = ( template ) => {
269+ patternShape = template ;
270+ patternHeight = patternShape . length ;
271+ patternWidth = patternShape [ 0 ] . length ;
272+ isPlacingTemplate = true ;
273+ table . style . cursor = "crosshair" ;
224274 stop ( ) ;
225275} ;
226276
@@ -233,6 +283,5 @@ on.apply_board = (newBoard) => {
233283 board = newBoard ;
234284 renderBoard ( ) ;
235285 generationChange ( 0 ) ;
236- resetTimer ( ) ;
237286 stop ( ) ;
238287} ;
0 commit comments