Skip to content

Commit

Permalink
wip: discard renders outside of valid bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Jul 25, 2023
1 parent 467adf8 commit d890054
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 deletions.
2 changes: 2 additions & 0 deletions lib/Data/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ class DataUserConfig extends CoreBase {
for (const controlId of controlsToRemove) {
this.controls.deleteControl(controlId)
}

this.graphics.discardAllOutOfBoundsControls()
}

if (save) {
Expand Down
91 changes: 65 additions & 26 deletions lib/Graphics/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,40 +127,78 @@ class GraphicsController extends CoreBase {
}

#drawAndCacheBank(location) {
const imageId = location ? formatLocation(location) : undefined
const gridSize = this.userconfig.getKey('gridSize')
const locationIsInBounds =
location &&
gridSize &&
location.column <= gridSize.maxColumn &&
location.column >= gridSize.minColumn &&
location.row <= gridSize.maxRow &&
location.row >= gridSize.minRow

const controlId = this.page.getControlIdAt(location)
const control = this.controls.getControl(controlId)
const buttonStyle = control?.getDrawStyle?.()

// Update the internal b_text_1_4 variable
const variableValue = buttonStyle?.text
if (location) {
setImmediate(() => {
const values = {
[`b_text_${location.pageNumber}_${location.row}_${location.column}`]: variableValue,
}
let render
if (location && locationIsInBounds && buttonStyle && buttonStyle.style) {
// Update the internal b_text_1_4 variable
const variableValue = buttonStyle?.text
if (location) {
setImmediate(() => {
const values = {
[`b_text_${location.pageNumber}_${location.row}_${location.column}`]: variableValue,
}

const bank = xyToOldBankIndex(location.column, location.row)
if (bank) values[`b_text_${location.pageNumber}_${bank}`] = variableValue

this.instance.variable.setVariableValues('internal', values)
})
}

const bank = xyToOldBankIndex(location.column, location.row)
if (bank) values[`b_text_${location.pageNumber}_${bank}`] = variableValue
const pagename = this.page.getPageName(location.pageNumber)

this.instance.variable.setVariableValues('internal', values)
})
render = GraphicsRenderer.drawBankImage(this.draw_options, buttonStyle, location, pagename)
} else {
render = GraphicsRenderer.drawBlank(this.draw_options, location)
}

if (buttonStyle && buttonStyle.style) {
const pagename = location ? this.page.getPageName(location.pageNumber) : undefined

const render = GraphicsRenderer.drawBankImage(this.draw_options, buttonStyle, location, pagename)
this.renders[imageId] = render

return render
} else {
const render = GraphicsRenderer.drawBlank(this.draw_options, location)
// Only cache the render, if it is within the valid bounds
if (locationIsInBounds && location) {
if (!this.renders[location.pageNumber]) this.renders[location.pageNumber] = {}
if (!this.renders[location.pageNumber][location.row]) this.renders[location.pageNumber][location.row] = {}
this.renders[location.pageNumber][location.row][location.column] = render
}

this.renders[imageId] = render
return render
}

return render
/**
* Discard any renders for controls that are outside of the valid grid bounds
* @returns
* @access public
*/
discardAllOutOfBoundsControls() {
const { minColumn, maxColumn, minRow, maxRow } = this.userconfig.getKey('gridSize')

for (const page of Object.values(this.pages)) {
for (const row of Object.keys(page.controls)) {
const rowObj = page.controls[row]
if (!rowObj) continue

if (row < minRow || row > maxRow) {
// Row is out of bounds, delete it all
delete page.controls[row]
} else {
for (const column of Object.keys(rowObj)) {
if (column < minColumn || column > maxColumn) {
// Column is out of bounds
delete rowObj[column]
}
}
}
}
}
}

Expand All @@ -180,8 +218,7 @@ class GraphicsController extends CoreBase {
}

getBank(location) {
const renderId = formatLocation(location)
let render = this.renders[renderId]
let render = this.renders[location.pageNumber]?.[location.row]?.[location.column]
if (render) return render

render = this.#drawAndCacheBank(location)
Expand All @@ -190,7 +227,9 @@ class GraphicsController extends CoreBase {
return render
}

this.logger.silly('!!!! ERROR: UNEXPECTED ERROR while fetching image for unbuffered bank: ' + renderId)
this.logger.silly(
`!!!! ERROR: UNEXPECTED ERROR while fetching image for unbuffered bank: ${formatLocation(location)}`
)

// continue gracefully, even though something is terribly wrong
return new ImageResult(Buffer.alloc(72 * 72 * 3))
Expand Down

0 comments on commit d890054

Please sign in to comment.