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.
397 lines
12 KiB
397 lines
12 KiB
7 years ago
|
const { mk } = require('../utils')
|
||
7 years ago
|
|
||
7 years ago
|
module.exports = function attachDebugger (screen, connection) {
|
||
7 years ago
|
const debugCanvas = mk('canvas')
|
||
|
const ctx = debugCanvas.getContext('2d')
|
||
|
|
||
7 years ago
|
debugCanvas.classList.add('debug-canvas')
|
||
7 years ago
|
|
||
7 years ago
|
let mouseHoverCell = null
|
||
|
let updateToolbar
|
||
|
|
||
|
let onMouseMove = e => {
|
||
|
mouseHoverCell = screen.screenToGrid(e.offsetX, e.offsetY)
|
||
|
startDrawing()
|
||
|
updateToolbar()
|
||
|
}
|
||
|
let onMouseOut = () => (mouseHoverCell = null)
|
||
|
|
||
7 years ago
|
let addCanvas = function () {
|
||
7 years ago
|
if (!debugCanvas.parentNode) {
|
||
|
screen.canvas.parentNode.appendChild(debugCanvas)
|
||
7 years ago
|
screen.canvas.addEventListener('mousemove', onMouseMove)
|
||
|
screen.canvas.addEventListener('mouseout', onMouseOut)
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
let removeCanvas = function () {
|
||
7 years ago
|
if (debugCanvas.parentNode) {
|
||
|
debugCanvas.parentNode.removeChild(debugCanvas)
|
||
7 years ago
|
screen.canvas.removeEventListener('mousemove', onMouseMove)
|
||
|
screen.canvas.removeEventListener('mouseout', onMouseOut)
|
||
7 years ago
|
onMouseOut()
|
||
|
}
|
||
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 drawInfo = mk('div')
|
||
|
drawInfo.classList.add('draw-info')
|
||
|
|
||
7 years ago
|
let startTime, endTime, lastReason
|
||
7 years ago
|
let cells = new Map()
|
||
7 years ago
|
let clippedRects = []
|
||
7 years ago
|
let updateFrames = []
|
||
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(drawInfo.textContent = `Draw: ${lastReason} (${(endTime - startTime)} ms) with 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
|
},
|
||
|
pushFrame (frame) {
|
||
|
frame.push(Date.now())
|
||
|
updateFrames.push(frame)
|
||
|
startDrawing()
|
||
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
|
||
7 years ago
|
let lastDrawTime = 0
|
||
|
let t = 0
|
||
7 years ago
|
|
||
|
let drawLoop = function () {
|
||
7 years ago
|
if (isDrawing) window.requestAnimationFrame(drawLoop)
|
||
7 years ago
|
|
||
7 years ago
|
let dt = (Date.now() - lastDrawTime) / 1000
|
||
|
lastDrawTime = Date.now()
|
||
|
t += dt
|
||
|
|
||
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
|
let didDrawUpdateFrames = false
|
||
|
if (updateFrames.length) {
|
||
|
let framesToDelete = []
|
||
|
for (let frame of updateFrames) {
|
||
|
let time = frame[4]
|
||
|
let elapsed = Date.now() - time
|
||
|
if (elapsed > 1000) framesToDelete.push(frame)
|
||
|
else {
|
||
|
didDrawUpdateFrames = true
|
||
|
ctx.globalAlpha = 1 - elapsed / 1000
|
||
|
ctx.strokeStyle = '#ff0'
|
||
|
ctx.lineWidth = 2
|
||
|
ctx.strokeRect(frame[0] * cellWidth, frame[1] * cellHeight, frame[2] * cellWidth, frame[3] * cellHeight)
|
||
|
}
|
||
|
}
|
||
|
for (let frame of framesToDelete) {
|
||
|
updateFrames.splice(updateFrames.indexOf(frame), 1)
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
if (mouseHoverCell) {
|
||
|
ctx.save()
|
||
|
ctx.globalAlpha = 1
|
||
|
ctx.lineWidth = 1 + 0.5 * Math.sin(t * 10)
|
||
|
ctx.strokeStyle = '#fff'
|
||
|
ctx.lineJoin = 'round'
|
||
|
ctx.setLineDash([2, 2])
|
||
|
ctx.lineDashOffset = t * 10
|
||
|
ctx.strokeRect(mouseHoverCell[0] * cellWidth, mouseHoverCell[1] * cellHeight, cellWidth, cellHeight)
|
||
|
ctx.lineDashOffset += 2
|
||
|
ctx.strokeStyle = '#000'
|
||
|
ctx.strokeRect(mouseHoverCell[0] * cellWidth, mouseHoverCell[1] * cellHeight, cellWidth, cellHeight)
|
||
|
ctx.restore()
|
||
|
}
|
||
|
|
||
7 years ago
|
if (activeCells === 0 && !mouseHoverCell && !didDrawUpdateFrames) {
|
||
7 years ago
|
isDrawing = false
|
||
|
removeCanvas()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
startDrawing = function () {
|
||
|
if (isDrawing) return
|
||
|
addCanvas()
|
||
|
updateCanvasSize()
|
||
|
isDrawing = true
|
||
7 years ago
|
lastDrawTime = Date.now()
|
||
7 years ago
|
drawLoop()
|
||
|
}
|
||
7 years ago
|
|
||
|
// debug toolbar
|
||
|
const toolbar = mk('div')
|
||
|
toolbar.classList.add('debug-toolbar')
|
||
|
let toolbarAttached = false
|
||
7 years ago
|
|
||
|
const heartbeat = mk('div')
|
||
|
heartbeat.classList.add('heartbeat')
|
||
|
heartbeat.textContent = '❤'
|
||
|
toolbar.appendChild(heartbeat)
|
||
|
|
||
7 years ago
|
const dataDisplay = mk('div')
|
||
|
dataDisplay.classList.add('data-display')
|
||
|
toolbar.appendChild(dataDisplay)
|
||
7 years ago
|
|
||
7 years ago
|
const internalDisplay = mk('div')
|
||
|
internalDisplay.classList.add('internal-display')
|
||
|
toolbar.appendChild(internalDisplay)
|
||
7 years ago
|
|
||
7 years ago
|
toolbar.appendChild(drawInfo)
|
||
7 years ago
|
|
||
7 years ago
|
const buttons = mk('div')
|
||
|
buttons.classList.add('toolbar-buttons')
|
||
|
toolbar.appendChild(buttons)
|
||
|
|
||
7 years ago
|
// heartbeat
|
||
|
connection.on('heartbeat', () => {
|
||
|
heartbeat.classList.remove('beat')
|
||
|
window.requestAnimationFrame(() => {
|
||
|
heartbeat.classList.add('beat')
|
||
|
})
|
||
|
})
|
||
|
|
||
7 years ago
|
{
|
||
|
const redraw = mk('button')
|
||
|
redraw.textContent = 'Redraw'
|
||
|
redraw.addEventListener('click', e => {
|
||
|
screen.renderer.resetDrawn()
|
||
|
screen.renderer.draw('debug-redraw')
|
||
|
})
|
||
|
buttons.appendChild(redraw)
|
||
|
|
||
|
const fancyGraphics = mk('button')
|
||
|
fancyGraphics.textContent = 'Toggle Graphics'
|
||
|
fancyGraphics.addEventListener('click', e => {
|
||
|
screen.window.graphics = +!screen.window.graphics
|
||
|
})
|
||
|
buttons.appendChild(fancyGraphics)
|
||
|
}
|
||
7 years ago
|
|
||
|
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()
|
||
7 years ago
|
else {
|
||
|
detachToolbar()
|
||
|
removeCanvas()
|
||
|
}
|
||
7 years ago
|
}
|
||
|
})
|
||
|
|
||
7 years ago
|
const displayCellAttrs = attrs => {
|
||
|
let result = attrs.toString(16)
|
||
|
if (attrs & 1 || attrs & 2) {
|
||
|
result += ':has('
|
||
|
if (attrs & 1) result += 'fg'
|
||
|
if (attrs & 2) result += (attrs & 1 ? ',' : '') + 'bg'
|
||
|
result += ')'
|
||
|
}
|
||
|
let attributes = []
|
||
7 years ago
|
if (attrs & (1 << 2)) attributes.push('\\[bold]bold\\()')
|
||
|
if (attrs & (1 << 3)) attributes.push('\\[underline]underln\\()')
|
||
|
if (attrs & (1 << 4)) attributes.push('\\[invert]invert\\()')
|
||
7 years ago
|
if (attrs & (1 << 5)) attributes.push('blink')
|
||
7 years ago
|
if (attrs & (1 << 6)) attributes.push('\\[italic]italic\\()')
|
||
|
if (attrs & (1 << 7)) attributes.push('\\[strike]strike\\()')
|
||
|
if (attrs & (1 << 8)) attributes.push('\\[overline]overln\\()')
|
||
|
if (attrs & (1 << 9)) attributes.push('\\[faint]faint\\()')
|
||
7 years ago
|
if (attrs & (1 << 10)) attributes.push('fraktur')
|
||
|
if (attributes.length) result += ':' + attributes.join()
|
||
|
return result.trim()
|
||
|
}
|
||
|
|
||
7 years ago
|
const formatColor = color => color < 256 ? color : `#${`000000${(color - 256).toString(16)}`.substr(-6)}`
|
||
|
const getCellData = cell => {
|
||
|
if (cell < 0 || cell > screen.screen.length) return '(-)'
|
||
7 years ago
|
let cellAttrs = screen.renderer.drawnScreenAttrs[cell] | 0
|
||
7 years ago
|
let cellFG = screen.renderer.drawnScreenFG[cell] | 0
|
||
|
let cellBG = screen.renderer.drawnScreenBG[cell] | 0
|
||
|
let fgText = formatColor(cellFG)
|
||
|
let bgText = formatColor(cellBG)
|
||
|
fgText += `\\[color=${screen.renderer.getColor(cellFG).replace(/ /g, '')}]●\\[]`
|
||
|
bgText += `\\[color=${screen.renderer.getColor(cellBG).replace(/ /g, '')}]●\\[]`
|
||
7 years ago
|
let cellCode = (screen.renderer.drawnScreen[cell] || '').codePointAt(0) | 0
|
||
7 years ago
|
let hexcode = cellCode.toString(16).toUpperCase()
|
||
|
if (hexcode.length < 4) hexcode = `0000${hexcode}`.substr(-4)
|
||
|
hexcode = `U+${hexcode}`
|
||
7 years ago
|
let x = cell % screen.window.width
|
||
|
let y = Math.floor(cell / screen.window.width)
|
||
7 years ago
|
return `((${y},${x})=${cell}:\\[bold]${hexcode}\\[]:F${fgText}:B${bgText}:A(${displayCellAttrs(cellAttrs)}))`
|
||
7 years ago
|
}
|
||
|
|
||
|
const setFormattedText = (node, text) => {
|
||
|
node.innerHTML = ''
|
||
|
|
||
|
let match
|
||
|
let attrs = {}
|
||
|
|
||
|
let pushSpan = content => {
|
||
|
let span = mk('span')
|
||
|
node.appendChild(span)
|
||
|
span.textContent = content
|
||
|
for (let key in attrs) span[key] = attrs[key]
|
||
|
}
|
||
|
|
||
7 years ago
|
while ((match = text.match(/\\\[(.*?)\]/))) {
|
||
7 years ago
|
if (match.index > 0) pushSpan(text.substr(0, match.index))
|
||
|
|
||
|
attrs = { style: '' }
|
||
|
let data = match[1].split(' ')
|
||
|
for (let attr of data) {
|
||
|
if (!attr) continue
|
||
|
let key, value
|
||
|
if (attr.indexOf('=') > -1) {
|
||
|
key = attr.substr(0, attr.indexOf('='))
|
||
|
value = attr.substr(attr.indexOf('=') + 1)
|
||
|
} else {
|
||
|
key = attr
|
||
|
value = true
|
||
|
}
|
||
|
|
||
|
if (key === 'bold') attrs.style += 'font-weight:bold;'
|
||
|
if (key === 'italic') attrs.style += 'font-style:italic;'
|
||
|
if (key === 'underline') attrs.style += 'text-decoration:underline;'
|
||
|
if (key === 'invert') attrs.style += 'background:#000;filter:invert(1);'
|
||
|
if (key === 'strike') attrs.style += 'text-decoration:line-through;'
|
||
|
if (key === 'overline') attrs.style += 'text-decoration:overline;'
|
||
|
if (key === 'faint') attrs.style += 'opacity:0.5;'
|
||
|
else if (key === 'color') attrs.style += `color:${value};`
|
||
|
else attrs[key] = value
|
||
|
}
|
||
|
|
||
|
text = text.substr(match.index + match[0].length)
|
||
|
}
|
||
|
|
||
|
if (text) pushSpan(text)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
let internalInfo = {}
|
||
|
|
||
7 years ago
|
updateToolbar = () => {
|
||
|
if (!toolbarAttached) return
|
||
|
let text = `C((${screen.cursor.y},${screen.cursor.x}),hang:${screen.cursor.hanging},vis:${screen.cursor.visible})`
|
||
|
if (mouseHoverCell) {
|
||
|
text += ' m' + getCellData(mouseHoverCell[1] * screen.window.width + mouseHoverCell[0])
|
||
|
}
|
||
7 years ago
|
setFormattedText(dataDisplay, text)
|
||
|
|
||
7 years ago
|
if ('flags' in internalInfo) {
|
||
|
// we got ourselves some internal data
|
||
7 years ago
|
let text = ' '
|
||
7 years ago
|
text += ` flags:${internalInfo.flags.toString(2)}`
|
||
|
text += ` curAttrs:${internalInfo.cursorAttrs.toString(2)}`
|
||
7 years ago
|
text += ` Region:${internalInfo.regionStart}->${internalInfo.regionEnd}`
|
||
|
text += ` Charset:${internalInfo.charsetGx} (0:${internalInfo.charsetG0},1:${internalInfo.charsetG1})`
|
||
|
text += ` Heap:${internalInfo.freeHeap}`
|
||
|
text += ` Clients:${internalInfo.clientCount}`
|
||
7 years ago
|
setFormattedText(internalDisplay, text)
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
|
screen.on('draw', updateToolbar)
|
||
7 years ago
|
screen.on('internal', data => {
|
||
|
internalInfo = data
|
||
|
updateToolbar()
|
||
|
})
|
||
7 years ago
|
}
|