@@ -6,6 +6,11 @@ let generationFigure = 0;
66let timerTime = 1000 ;
77let isDragging = false ;
88let dragMode = false ; // true: 黒にする, false: 白にする
9+ let isPlacingTemplate = false ;
10+ let patternShape = [ ] ;
11+ let patternHeight = 0 ;
12+ let patternWidth = 0 ;
13+ let previewCells = [ ] ;
914
1015//変数設定
1116let boardSize = 20 ; //盤面の大きさ(20x20)
@@ -51,20 +56,50 @@ function renderBoard() {
5156 button . style . height = `${ cellSize } px` ;
5257 button . style . padding = "0" ; //cellSizeが小さいとき、セルが横長になることを防ぐ
5358 button . style . display = "block" ; //cellSizeが小さいとき、行間が空きすぎるのを防ぐ
59+ button . onclick = ( ) => {
60+ if ( isPlacingTemplate ) {
61+ clearPreview ( ) ;
62+ isPlacingTemplate = false ;
63+ if ( i + patternHeight < boardSize + 1 && j + patternWidth < boardSize + 1 ) {
64+ for ( let r = 0 ; r < patternHeight ; r ++ ) {
65+ for ( let c = 0 ; c < patternWidth ; c ++ ) {
66+ const boardRow = i + r ;
67+ const boardCol = j + c ;
68+ board [ boardRow ] [ boardCol ] = patternShape [ r ] [ c ] === 1 ;
69+ }
70+ }
71+ rerender ( ) ;
72+ generationChange ( 0 ) ;
73+ resetTimer ( ) ;
74+ stop ( ) ;
75+ } else {
76+ window . parent . postMessage (
77+ {
78+ type : "Size shortage" ,
79+ data : { } ,
80+ } ,
81+ "*" ,
82+ ) ;
83+ }
84+ }
85+ } ;
5486 button . onmousedown = ( e ) => {
5587 e . preventDefault ( ) ;
56- if ( timer === "stop" ) {
88+ if ( timer === "stop" && ! isPlacingTemplate ) {
5789 isDragging = true ;
5890 board [ i ] [ j ] = ! board [ i ] [ j ] ;
5991 dragMode = board [ i ] [ j ] ;
6092 button . style . backgroundColor = board [ i ] [ j ] ? "black" : "white" ;
6193 }
6294 } ;
6395 button . onmouseenter = ( ) => {
64- if ( isDragging && timer === "stop" && board [ i ] [ j ] !== dragMode ) {
96+ if ( isDragging && timer === "stop" && board [ i ] [ j ] !== dragMode && ! isPlacingTemplate ) {
6597 board [ i ] [ j ] = dragMode ;
6698 button . style . backgroundColor = board [ i ] [ j ] ? "black" : "white" ;
6799 }
100+ if ( isPlacingTemplate ) {
101+ drawPreview ( i , j ) ;
102+ }
68103 } ;
69104 td . appendChild ( button ) ;
70105 tr . appendChild ( td ) ;
@@ -73,6 +108,37 @@ function renderBoard() {
73108 }
74109}
75110
111+ table . onmouseleave = ( ) => {
112+ if ( isPlacingTemplate ) {
113+ clearPreview ( ) ;
114+ }
115+ } ;
116+
117+ function drawPreview ( row , col ) {
118+ clearPreview ( ) ;
119+ for ( let r = 0 ; r < patternHeight ; r ++ ) {
120+ for ( let c = 0 ; c < patternWidth ; c ++ ) {
121+ if ( patternShape [ r ] [ c ] === 1 ) {
122+ const boardRow = row + r ;
123+ const boardCol = col + c ;
124+ if ( boardRow < boardSize && boardCol < boardSize ) {
125+ const cell = table . rows [ boardRow ] . cells [ boardCol ] . firstChild ;
126+ cell . style . backgroundColor = "gray" ;
127+ previewCells . push ( { row : boardRow , col : boardCol } ) ;
128+ }
129+ }
130+ }
131+ }
132+ }
133+
134+ function clearPreview ( ) {
135+ previewCells . forEach ( ( cellPos ) => {
136+ const cell = table . rows [ cellPos . row ] . cells [ cellPos . col ] . firstChild ;
137+ cell . style . backgroundColor = board [ cellPos . row ] [ cellPos . col ] ? "black" : "white" ;
138+ } ) ;
139+ previewCells = [ ] ;
140+ }
141+
76142function rerender ( ) {
77143 // 2回目以降の盤面生成
78144 for ( let i = 0 ; i < boardSize ; i ++ ) {
@@ -216,11 +282,12 @@ on.request_sync = () => {
216282 console . log ( "generationFigure:" , generationFigure , "boardSize:" , boardSize ) ;
217283} ;
218284
219- on . place_template = ( newBoard ) => {
220- board = newBoard ;
221- renderBoard ( ) ;
222- generationChange ( 0 ) ;
223- resetTimer ( ) ;
285+ on . place_template = ( template ) => {
286+ patternShape = template ;
287+ patternHeight = patternShape . length ;
288+ patternWidth = patternShape [ 0 ] . length ;
289+ isPlacingTemplate = true ;
290+ table . style . cursor = "crosshair" ;
224291 stop ( ) ;
225292} ;
226293
0 commit comments