about summary refs log tree commit diff stats
path: root/app/assets/javascripts/display2.js
blob: ddf3968e77c336c9862ef7cc20976e98f0338ab9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
var SYM_TYPE_NONE               = 0
var SYM_TYPE_HORIZONTAL         = 1
var SYM_TYPE_VERTICAL           = 2
var SYM_TYPE_ROTATIONAL         = 3
var SYM_TYPE_ROTATE_LEFT        = 4
var SYM_TYPE_ROTATE_RIGHT       = 5
var SYM_TYPE_FLIP_XY            = 6
var SYM_TYPE_FLIP_NEG_XY        = 7
var SYM_TYPE_PARALLEL_H         = 8
var SYM_TYPE_PARALLEL_V         = 9
var SYM_TYPE_PARALLEL_H_FLIP    = 10
var SYM_TYPE_PARALLEL_V_FLIP    = 11
var SYM_TYPE_PILLAR_PARALLEL    = 12
var SYM_TYPE_PILLAR_HORIZONTAL  = 13
var SYM_TYPE_PILLAR_VERTICAL    = 14
var SYM_TYPE_PILLAR_ROTATIONAL  = 15

namespace(function() {

window.draw = function(puzzle, target='puzzle') {
  if (puzzle == null) return
  var svg = document.getElementById(target)
  console.info('Drawing', puzzle, 'into', svg)
  while (svg.firstChild) svg.removeChild(svg.firstChild)

  // Prevent context menu popups within the puzzle
  svg.oncontextmenu = function(event) {
    event.preventDefault()
  }

  if (puzzle.pillar === true) {
    // 41*width + 30*2 (padding) + 10*2 (border)
    var pixelWidth = 41 * puzzle.width + 80
  } else {
    // 41*(width-1) + 24 (extra edge) + 30*2 (padding) + 10*2 (border)
    var pixelWidth = 41 * puzzle.width + 63
  }
  var pixelHeight = 41 * puzzle.height + 63
  svg.setAttribute('viewbox', '0 0 ' + pixelWidth + ' ' + pixelHeight)
  svg.setAttribute('width', pixelWidth)
  svg.setAttribute('height', pixelHeight)

  var rect = createElement('rect')
  svg.appendChild(rect)
  rect.setAttribute('stroke-width', 10)
  rect.setAttribute('stroke', window.BORDER)
  rect.setAttribute('fill', window.OUTER_BACKGROUND)
  // Accounting for the border thickness
  rect.setAttribute('x', 5)
  rect.setAttribute('y', 5)
  rect.setAttribute('width', pixelWidth - 10) // Removing border
  rect.setAttribute('height', pixelHeight - 10) // Removing border

  drawCenters(puzzle, svg)
  drawGrid(puzzle, svg, target)
  drawStartAndEnd(puzzle, svg)
  // Draw cell symbols after so they overlap the lines, if necessary
  drawSymbols(puzzle, svg, target)

  // For pillar puzzles, add faders for the left and right sides
  if (puzzle.pillar === true) {
    var defs = window.createElement('defs')
    defs.id = 'cursorPos'
    defs.innerHTML = '' +
    '<linearGradient id="fadeInLeft">\n' +
    '  <stop offset="0%"   stop-opacity="1.0" stop-color="' + window.OUTER_BACKGROUND + '"></stop>\n' +
    '  <stop offset="25%"  stop-opacity="1.0" stop-color="' + window.OUTER_BACKGROUND + '"></stop>\n' +
    '  <stop offset="100%" stop-opacity="0.0" stop-color="' + window.OUTER_BACKGROUND + '"></stop>\n' +
    '</linearGradient>\n' +
    '<linearGradient id="fadeOutRight">\n' +
    '  <stop offset="0%"   stop-opacity="0.0" stop-color="' + window.OUTER_BACKGROUND + '"></stop>\n' +
    '  <stop offset="100%" stop-opacity="1.0" stop-color="' + window.OUTER_BACKGROUND + '"></stop>\n' +
    '</linearGradient>\n'
    svg.appendChild(defs)

    var leftBox = window.createElement('rect')
    leftBox.setAttribute('x', 16)
    leftBox.setAttribute('y', 10)
    leftBox.setAttribute('width', 48)
    leftBox.setAttribute('height', 41 * puzzle.height + 43)
    leftBox.setAttribute('fill', 'url(#fadeInLeft)')
    leftBox.setAttribute('style', 'pointer-events: none')
    svg.appendChild(leftBox)

    var rightBox = window.createElement('rect')
    rightBox.setAttribute('x', 41 * puzzle.width + 22)
    rightBox.setAttribute('y', 10)
    rightBox.setAttribute('width', 30)
    rightBox.setAttribute('height', 41 * puzzle.height + 43)
    rightBox.setAttribute('fill', 'url(#fadeOutRight)')
    rightBox.setAttribute('style', 'pointer-events: none')
    svg.appendChild(rightBox)
  }
}

function drawCenters(puzzle, svg) {
  // @Hack that I am not fixing. This switches the puzzle's grid to a floodfilled grid
  // where null represents cells which are part of the outside
  var savedGrid = puzzle.switchToMaskedGrid()
  if (puzzle.pillar === true) {
    for (var y=1; y<puzzle.height; y += 2) {
      if (puzzle.getCell(-1, y) == null) continue // Cell borders the outside

      var rect = createElement('rect')
      rect.setAttribute('x', 28)
      rect.setAttribute('y', 41 * y + 11)
      rect.setAttribute('width', 24)
      rect.setAttribute('height', 82)
      rect.setAttribute('fill', window.BACKGROUND)
      svg.appendChild(rect)
    }
  }

  for (var x=1; x<puzzle.width; x += 2) {
    for (var y=1; y<puzzle.height; y += 2) {
      if (puzzle.grid[x][y] == null) continue // Cell borders the outside

      var rect = createElement('rect')
      rect.setAttribute('x', 41 * x + 11)
      rect.setAttribute('y', 41 * y + 11)
      rect.setAttribute('width', 82)
      rect.setAttribute('height', 82)
      rect.setAttribute('fill', window.BACKGROUND)
      rect.setAttribute('shape-rendering', 'crispedges') // Otherwise they don't meet behind gaps
      svg.appendChild(rect)
    }
  }
  puzzle.grid = savedGrid
}

function drawGrid(puzzle, svg, target) {
  for (var x=0; x<puzzle.width; x++) {
    for (var y=0; y<puzzle.height; y++) {
      var cell = puzzle.grid[x][y]
      if (cell != null && cell.gap === window.GAP_FULL) continue
      if (cell != null && cell.gap === window.GAP_BREAK) {
        var params = {
          'width':58,
          'height':58,
          'x': x*41 + 23,
          'y': y*41 + 23,
          'class': target + '_' + x + '_' + y,
          'type': 'gap',
        }
        if (x%2 === 0 && y%2 === 1) params.rot = 1
        drawSymbolWithSvg(svg, params)
        continue
      }

      var line = createElement('line')
      line.setAttribute('stroke-width', 24)
      line.setAttribute('stroke-linecap', 'round')
      line.setAttribute('stroke', window.FOREGROUND)
      if (x%2 === 1 && y%2 === 0) { // Horizontal
        if (cell.gap === window.GAP_BREAK) continue
        line.setAttribute('x1', (x-1)*41 + 52)
        // Adjust the length if it's a pillar -- the grid is not as wide!
        if (puzzle.pillar === true && x === puzzle.width - 1) {
          line.setAttribute('x2', (x+1)*41 + 40)
        } else {
          line.setAttribute('x2', (x+1)*41 + 52)
        }
        line.setAttribute('y1', y*41 + 52)
        line.setAttribute('y2', y*41 + 52)
        svg.appendChild(line)
      } else if (x%2 === 0 && y%2 === 1) { // Vertical
        if (cell.gap === window.GAP_BREAK) continue
        line.setAttribute('x1', x*41 + 52)
        line.setAttribute('x2', x*41 + 52)
        line.setAttribute('y1', (y-1)*41 + 52)
        line.setAttribute('y2', (y+1)*41 + 52)
        svg.appendChild(line)
      } else if (x%2 === 0 && y%2 === 0) { // Intersection
        var surroundingLines = 0
        if (cell.end != null) surroundingLines++
        var leftCell = puzzle.getCell(x - 1, y)
        if (leftCell != null && leftCell.gap !== window.GAP_FULL) surroundingLines++
        var rightCell = puzzle.getCell(x + 1, y)
        if (rightCell != null && rightCell.gap !== window.GAP_FULL) surroundingLines++
        var topCell = puzzle.getCell(x, y - 1)
        if (topCell != null && topCell.gap !== window.GAP_FULL) surroundingLines++
        var bottomCell = puzzle.getCell(x, y + 1)
        if (bottomCell != null && bottomCell.gap !== window.GAP_FULL) surroundingLines++

        if (surroundingLines === 1) {
          // Add square caps for dead ends which are non-endpoints
          var rect = createElement('rect')
          rect.setAttribute('x', x*41 + 40)
          rect.setAttribute('y', y*41 + 40)
          rect.setAttribute('width', 24)
          rect.setAttribute('height', 24)
          rect.setAttribute('fill', window.FOREGROUND)
          svg.appendChild(rect)
        } else if (surroundingLines > 1) {
          // Add rounding for other intersections (handling gap-only corners)
          var circ = createElement('circle')
          circ.setAttribute('cx', x*41 + 52)
          circ.setAttribute('cy', y*41 + 52)
          circ.setAttribute('r', 12)
          circ.setAttribute('fill', window.FOREGROUND)
          svg.appendChild(circ)
        }
      }
    }
  }
  // Determine if left-side needs a 'wrap indicator'
  if (puzzle.pillar === true) {
    var x = 0;
    for (var y=0; y<puzzle.height; y+=2) {
      var cell = puzzle.getCell(x-1, y)
      if (cell == null || cell.gap === window.GAP_FULL) continue
      var line = createElement('line')
      line.setAttribute('stroke-width', 24)
      line.setAttribute('stroke-linecap', 'round')
      line.setAttribute('stroke', window.FOREGROUND)
      line.setAttribute('x1', x*41 + 40)
      line.setAttribute('x2', x*41 + 52)
      line.setAttribute('y1', y*41 + 52)
      line.setAttribute('y2', y*41 + 52)
      svg.appendChild(line)
    }
  }
}

function drawSymbols(puzzle, svg, target) {
  for (var x=0; x<puzzle.width; x++) {
    for (var y=0; y<puzzle.height; y++) {
      var cell = puzzle.grid[x][y]
      if (cell == null) continue
      var params = {
        'width':58,
        'height':58,
        'x': x*41 + 23,
        'y': y*41 + 23,
        'class': target + '_' + x + '_' + y,
      }
      if (cell.dot > window.DOT_NONE) {
        params.type = 'dot'
        if (cell.dot === window.DOT_BLACK) params.color = 'black'
        else if (cell.dot === window.DOT_BLUE) params.color = window.LINE_PRIMARY
        else if (cell.dot === window.DOT_YELLOW) params.color = window.LINE_SECONDARY
        else if (cell.dot === window.DOT_INVISIBLE) {
          params.color = window.FOREGROUND
          // This makes the invisible dots visible, but only while we're in the editor.
          if (document.getElementById('metaButtons') != null) {
            params.stroke = 'black'
            params.strokeWidth = '2px'
          }
        }
        drawSymbolWithSvg(svg, params)
      } else if (cell.gap === window.GAP_BREAK) {
        // Gaps were handled above, while drawing the grid.
      } else if (x%2 === 1 && y%2 === 1) {
        // Generic draw for all other elements
        Object.assign(params, cell)
        window.drawSymbolWithSvg(svg, params, puzzle.settings.CUSTOM_MECHANICS)
      }
    }
  }
}

function drawStartAndEnd(puzzle, svg) {
  for (var x=0; x<puzzle.width; x++) {
    for (var y=0; y<puzzle.height; y++) {
      var cell = puzzle.grid[x][y]
      if (cell == null) continue
      if (cell.end != null) {
        window.drawSymbolWithSvg(svg, {
          'type': 'end',
          'width': 58,
          'height': 58,
          'dir': cell.end,
          'x': x*41 + 23,
          'y': y*41 + 23,
        })
      }

      if (cell.start === true) {
        var symStart = null
        if (puzzle.symType != SYM_TYPE_NONE) {
          var sym = puzzle.getSymmetricalPos(x, y)
          window.drawSymbolWithSvg(svg, {
            'type': 'start',
            'width': 58,
            'height': 58,
            'x': sym.x*41 + 23,
            'y': sym.y*41 + 23,
          })
          symStart = svg.lastChild
          symStart.style.display = 'none'
          symStart.id = 'symStart_' + svg.id + '_' + x + '_' + y
        }

        window.drawSymbolWithSvg(svg, {
          'type': 'start',
          'width': 58,
          'height': 58,
          'x': x*41 + 23,
          'y': y*41 + 23,
        })
        var start = svg.lastChild
        start.id = 'start_' + svg.id + '_' + x + '_' + y

        // ;(function(a){}(a))
        // This syntax is used to forcibly copy all of the arguments
        ;(function(puzzle, x, y, start, symStart) {
          start.onpointerdown = function(event) {
            window.trace(event, puzzle, {'x':x, 'y':y}, start, symStart)
          }
        }(puzzle, x, y, start, symStart))
      }
    }
  }
}

})