Properly erroring for invalid indexes in gpu.get now and (probably?) fixing a bug in term.isWide.

This commit is contained in:
Florian Nücke 2014-07-20 19:02:33 +02:00
parent ef9506fd82
commit cb0e51efbb
3 changed files with 27 additions and 14 deletions

View File

@ -81,17 +81,20 @@ function term.setCursorBlink(enabled)
end
function term.isWide(x, y)
local charLeft, char = component.gpu.get(x - 1, y), component.gpu.get(x, y)
local char = component.gpu.get(x, y)
if unicode.isWide(char) then
-- The char at the specified position is a wide char.
return true
elseif char == " " and charLeft and unicode.isWide(charLeft) then
-- The char left to the specified position is a wide char.
return true, true
else
-- Not a wide char.
return false
end
if char == " " and x > 1 then
local charLeft = component.gpu.get(x - 1, y)
if charLeft and unicode.isWide(charLeft) then
-- The char left to the specified position is a wide char.
return true, true
end
end
-- Not a wide char.
return false
end
function term.isAvailable()

View File

@ -273,25 +273,31 @@ class TextBuffer(val owner: Container) extends ManagedComponent with api.compone
override def getForegroundColor(column: Int, row: Int) =
if (isForegroundFromPalette(column, row)) {
PackedColor.extractForeground(data.color(row)(column))
PackedColor.extractForeground(color(column, row))
}
else {
PackedColor.unpackForeground(data.color(row)(column), data.format)
PackedColor.unpackForeground(color(column, row), data.format)
}
override def isForegroundFromPalette(column: Int, row: Int) =
data.format.isFromPalette(PackedColor.extractForeground(data.color(row)(column)))
data.format.isFromPalette(PackedColor.extractForeground(color(column, row)))
override def getBackgroundColor(column: Int, row: Int) =
if (isBackgroundFromPalette(column, row)) {
PackedColor.extractBackground(data.color(row)(column))
PackedColor.extractBackground(color(column, row))
}
else {
PackedColor.unpackBackground(data.color(row)(column), data.format)
PackedColor.unpackBackground(color(column, row), data.format)
}
override def isBackgroundFromPalette(column: Int, row: Int) =
data.format.isFromPalette(PackedColor.extractBackground(data.color(row)(column)))
data.format.isFromPalette(PackedColor.extractBackground(color(column, row)))
private def color(column: Int, row: Int) = {
if (column < 0 || column >= getWidth || row < 0 || row >= getHeight)
throw new IndexOutOfBoundsException()
else data.color(row)(column)
}
@SideOnly(Side.CLIENT)
override def renderText() = relativeLitArea != 0 && proxy.render()

View File

@ -95,7 +95,11 @@ class TextBuffer(var width: Int, var height: Int, initialFormat: PackedColor.Col
}
/** Get the char at the specified index. */
def get(col: Int, row: Int) = buffer(row)(col)
def get(col: Int, row: Int) = {
if (col < 0 || col >= width || row < 0 || row >= height)
throw new IndexOutOfBoundsException()
else buffer(row)(col)
}
/** String based fill starting at a specified location. */
def set(col: Int, row: Int, s: String, vertical: Boolean): Boolean =