You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
181 lines
4.9 KiB
181 lines
4.9 KiB
7 years ago
|
const { mk } = require('../utils')
|
||
7 years ago
|
|
||
|
module.exports = function attachDebugScreen (screen) {
|
||
7 years ago
|
const debugCanvas = mk('canvas')
|
||
|
const ctx = debugCanvas.getContext('2d')
|
||
|
|
||
|
debugCanvas.style.position = 'absolute'
|
||
|
// hackity hack should probably set this in CSS
|
||
|
debugCanvas.style.top = '6px'
|
||
|
debugCanvas.style.left = '6px'
|
||
7 years ago
|
debugCanvas.style.pointerEvents = 'none'
|
||
7 years ago
|
|
||
|
let addCanvas = function () {
|
||
7 years ago
|
if (!debugCanvas.parentNode) screen.canvas.parentNode.appendChild(debugCanvas)
|
||
7 years ago
|
}
|
||
|
let removeCanvas = function () {
|
||
7 years ago
|
if (debugCanvas.parentNode) debugCanvas.parentNode.removeChild(debugCanvas)
|
||
7 years ago
|
}
|
||
|
let updateCanvasSize = function () {
|
||
7 years ago
|
let { width, height, devicePixelRatio } = screen.window
|
||
|
let cellSize = screen.getCellSize()
|
||
7 years ago
|
debugCanvas.width = width * cellSize.width * devicePixelRatio
|
||
|
debugCanvas.height = height * cellSize.height * devicePixelRatio
|
||
|
debugCanvas.style.width = `${width * cellSize.width}px`
|
||
|
debugCanvas.style.height = `${height * cellSize.height}px`
|
||
|
}
|
||
|
|
||
7 years ago
|
let startTime, endTime, lastReason
|
||
7 years ago
|
let cells = new Map()
|
||
7 years ago
|
let clippedRects = []
|
||
7 years ago
|
|
||
|
let startDrawing
|
||
|
|
||
7 years ago
|
screen._debug = {
|
||
7 years ago
|
drawStart (reason) {
|
||
7 years ago
|
lastReason = reason
|
||
7 years ago
|
startTime = Date.now()
|
||
7 years ago
|
clippedRects = []
|
||
7 years ago
|
},
|
||
|
drawEnd () {
|
||
|
endTime = Date.now()
|
||
7 years ago
|
console.log(`Draw: ${lastReason} (${(endTime - startTime)} ms) with fancy graphics: ${screen.window.graphics}`)
|
||
7 years ago
|
startDrawing()
|
||
|
},
|
||
|
setCell (cell, flags) {
|
||
|
cells.set(cell, [flags, Date.now()])
|
||
7 years ago
|
},
|
||
|
clipRect (...args) {
|
||
|
clippedRects.push(args)
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
let clipPattern
|
||
|
{
|
||
|
let patternCanvas = document.createElement('canvas')
|
||
|
patternCanvas.width = patternCanvas.height = 12
|
||
|
let pctx = patternCanvas.getContext('2d')
|
||
|
pctx.lineWidth = 1
|
||
|
pctx.strokeStyle = '#00f'
|
||
|
pctx.beginPath()
|
||
|
pctx.moveTo(0, 0)
|
||
|
pctx.lineTo(0 - 4, 12)
|
||
|
pctx.moveTo(4, 0)
|
||
|
pctx.lineTo(4 - 4, 12)
|
||
|
pctx.moveTo(8, 0)
|
||
|
pctx.lineTo(8 - 4, 12)
|
||
|
pctx.moveTo(12, 0)
|
||
|
pctx.lineTo(12 - 4, 12)
|
||
|
pctx.moveTo(16, 0)
|
||
|
pctx.lineTo(16 - 4, 12)
|
||
|
pctx.stroke()
|
||
|
clipPattern = ctx.createPattern(patternCanvas, 'repeat')
|
||
|
}
|
||
|
|
||
7 years ago
|
let isDrawing = false
|
||
|
|
||
|
let drawLoop = function () {
|
||
7 years ago
|
if (isDrawing) window.requestAnimationFrame(drawLoop)
|
||
7 years ago
|
|
||
7 years ago
|
let { devicePixelRatio, width, height } = screen.window
|
||
|
let { width: cellWidth, height: cellHeight } = screen.getCellSize()
|
||
7 years ago
|
let screenLength = width * height
|
||
|
let now = Date.now()
|
||
|
|
||
|
ctx.setTransform(devicePixelRatio, 0, 0, devicePixelRatio, 0, 0)
|
||
|
ctx.clearRect(0, 0, width * cellWidth, height * cellHeight)
|
||
|
|
||
|
let activeCells = 0
|
||
|
for (let cell = 0; cell < screenLength; cell++) {
|
||
|
if (!cells.has(cell) || cells.get(cell)[0] === 0) continue
|
||
|
|
||
|
let [flags, timestamp] = cells.get(cell)
|
||
|
let elapsedTime = (now - timestamp) / 1000
|
||
|
|
||
|
if (elapsedTime > 1) continue
|
||
|
|
||
|
activeCells++
|
||
|
ctx.globalAlpha = 0.5 * Math.max(0, 1 - elapsedTime)
|
||
|
|
||
|
let x = cell % width
|
||
|
let y = Math.floor(cell / width)
|
||
|
|
||
|
if (flags & 1) {
|
||
|
// redrawn
|
||
|
ctx.fillStyle = '#f0f'
|
||
|
}
|
||
|
if (flags & 2) {
|
||
|
// updated
|
||
|
ctx.fillStyle = '#0f0'
|
||
|
}
|
||
|
|
||
|
ctx.fillRect(x * cellWidth, y * cellHeight, cellWidth, cellHeight)
|
||
|
|
||
|
if (flags & 4) {
|
||
|
// wide cell
|
||
|
ctx.lineWidth = 2
|
||
|
ctx.strokeStyle = '#f00'
|
||
|
ctx.strokeRect(x * cellWidth, y * cellHeight, cellWidth, cellHeight)
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
if (clippedRects.length) {
|
||
|
ctx.globalAlpha = 0.5
|
||
|
ctx.beginPath()
|
||
|
|
||
|
for (let rect of clippedRects) {
|
||
|
ctx.rect(...rect)
|
||
|
}
|
||
|
|
||
|
ctx.fillStyle = clipPattern
|
||
|
ctx.fill()
|
||
|
}
|
||
|
|
||
7 years ago
|
if (activeCells === 0) {
|
||
|
isDrawing = false
|
||
|
removeCanvas()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
startDrawing = function () {
|
||
|
if (isDrawing) return
|
||
|
addCanvas()
|
||
|
updateCanvasSize()
|
||
|
isDrawing = true
|
||
|
drawLoop()
|
||
|
}
|
||
7 years ago
|
|
||
|
// debug toolbar
|
||
|
const toolbar = mk('div')
|
||
|
toolbar.classList.add('debug-toolbar')
|
||
|
let toolbarAttached = false
|
||
|
|
||
|
const attachToolbar = function () {
|
||
|
screen.canvas.parentNode.appendChild(toolbar)
|
||
|
}
|
||
|
const detachToolbar = function () {
|
||
|
toolbar.parentNode.removeChild(toolbar)
|
||
|
}
|
||
|
|
||
|
screen.on('update-window:debug', debug => {
|
||
|
if (debug !== toolbarAttached) {
|
||
|
toolbarAttached = debug
|
||
|
if (debug) attachToolbar()
|
||
|
else detachToolbar()
|
||
|
}
|
||
|
})
|
||
|
|
||
|
screen.on('draw', () => {
|
||
|
if (!toolbarAttached) return
|
||
|
let cursorCell = screen.cursor.y * screen.window.width + screen.cursor.x
|
||
|
let cellFG = screen.screenFG[cursorCell]
|
||
|
let cellBG = screen.screenBG[cursorCell]
|
||
|
let cellCode = (screen.screen[cursorCell] || '').codePointAt(0)
|
||
|
let cellAttrs = screen.screenAttrs[cursorCell]
|
||
7 years ago
|
let hexcode = cellCode.toString(16).toUpperCase()
|
||
|
if (hexcode.length < 4) hexcode = `0000${hexcode}`.substr(-4)
|
||
|
hexcode = `U+${hexcode}`
|
||
|
toolbar.textContent = `Cursor cell (${cursorCell}): ${hexcode} FG: ${cellFG} BG: ${cellBG} Attrs: ${cellAttrs.toString(2)}`
|
||
7 years ago
|
})
|
||
7 years ago
|
}
|