functions < math

This commit is contained in:
Igor Timofeev 2018-05-14 12:00:09 +03:00
parent 542e312186
commit 00f19b2c46
3 changed files with 58 additions and 62 deletions

View File

@ -1321,8 +1321,8 @@ local function dropDownMenuItemDraw(item)
return item
end
local function dropDownMenuItemEventHandler(mainContainer, object, event)
if event == "touch" then
local function dropDownMenuItemEventHandler(mainContainer, object, e1)
if e1 == "touch" then
if object.type == GUI.dropDownMenuItemTypes.default then
object.pressed = true
mainContainer:drawOnScreen()
@ -1447,8 +1447,8 @@ local function dropDownMenuShow(menu)
local mainContainer = GUI.fullScreenContainer()
-- Удаляем олдпиксельсы, чтоб старое дерьмое не рисовалось во всяких комбобоксах
menu.oldPixels = nil
mainContainer:addChild(GUI.object(1, 1, mainContainer.width, mainContainer.height)).eventHandler = function(mainContainer, object, event)
if event == "touch" then
mainContainer:addChild(GUI.object(1, 1, mainContainer.width, mainContainer.height)).eventHandler = function(mainContainer, object, e1)
if e1 == "touch" then
buffer.paste(menu.x, menu.y, menu.oldPixels)
buffer.draw()
mainContainer:stopEventHandling()
@ -2429,8 +2429,8 @@ local function filesystemChooserSetMode(object, IOMode, filesystemMode)
object.filesystemMode = filesystemMode
end
local function filesystemChooserEventHandler(mainContainer, object, event)
if event == "touch" then
local function filesystemChooserEventHandler(mainContainer, object, e1)
if e1 == "touch" then
object.pressed = true
mainContainer:drawOnScreen()
@ -3329,8 +3329,8 @@ local function inputStartInput(input)
mainContainer:drawOnScreen()
end
local function inputEventHandler(mainContainer, input, event)
if event == "touch" then
local function inputEventHandler(mainContainer, input, e1)
if e1 == "touch" then
input:startInput()
end
end
@ -3428,14 +3428,14 @@ local function autoCompleteScroll(mainContainer, object, direction)
end
end
local function autoCompleteEventHandler(mainContainer, object, e1, e2, e3, e4, ...)
local function autoCompleteEventHandler(mainContainer, object, e1, e2, e3, e4, e5, ...)
if e1 == "touch" then
object.selectedItem = e4 - object.y + object.fromItem
mainContainer:drawOnScreen()
if object.onItemSelected then
os.sleep(0.2)
object.onItemSelected(mainContainer, object, e1, e2, e3, e4, ...)
object.onItemSelected(mainContainer, object, e1, e2, e3, e4, e5, ...)
end
elseif e1 == "scroll" then
autoCompleteScroll(mainContainer, object, -e5)
@ -3443,7 +3443,7 @@ local function autoCompleteEventHandler(mainContainer, object, e1, e2, e3, e4, .
elseif e1 == "key_down" then
if e4 == 28 then
if object.onItemSelected then
object.onItemSelected(mainContainer, object, e1, e2, e3, e4, ...)
object.onItemSelected(mainContainer, object, e1, e2, e3, e4, e5, ...)
end
elseif e4 == 200 then
object.selectedItem = object.selectedItem - 1
@ -3715,7 +3715,7 @@ function GUI.palette(x, y, startColor)
local function paletteUpdateCrestsCoordinates()
bigCrest.localX = math.floor((bigImage.width - 1) * palette.color.hsb.saturation) - 1
bigCrest.localY = math.floor((bigImage.height - 1) - (bigImage.height - 1) * palette.color.hsb.brightness)
miniCrest.localY = math.floor(palette.color.hsb.hue / 360 * miniImage.height)
miniCrest.localY = math.ceil(palette.color.hsb.hue / 360 * miniImage.height + 0.5)
end
local inputs
@ -3865,7 +3865,7 @@ function GUI.palette(x, y, startColor)
bigCrest.eventHandler = bigImage.eventHandler
miniImage.eventHandler = function(mainContainer, object, e1, e2, e3, e4)
if event == "touch" or event == "drag" then
if e1 == "touch" or e1 == "drag" then
miniCrest.localY = e4 - palette.y + 1
paletteSwitchColorFromHsb((e4 - miniImage.y) * 360 / miniImage.height, palette.color.hsb.saturation, palette.color.hsb.brightness)
paletteRefreshBigImage()
@ -3915,8 +3915,8 @@ function GUI.addFadeContainer(parentContainer, addPanel, addLayout, title)
if addPanel then
container.panel = container:addChild(GUI.panel(1, 1, container.width, container.height, 0x0, GUI.colors.fadeContainer.transparency))
container.panel.eventHandler = function(parentContainer, object, event)
if event == "touch" then
container.panel.eventHandler = function(parentContainer, object, e1)
if e1 == "touch" then
container:delete()
parentContainer:drawOnScreen()
end

View File

@ -14,8 +14,7 @@ local color = {}
-- Optimized Lua 5.3 bitwise support
if computer.getArchitecture and computer.getArchitecture() == "Lua 5.3" then
integerToRGB, RGBToInteger, blend, transition, to8Bit = load([[
local palette = select(1, ...)
local mathHuge = math.huge
local mathHuge, palette = math.huge, select(1, ...)
return
function(integerColor)
@ -27,21 +26,19 @@ if computer.getArchitecture and computer.getArchitecture() == "Lua 5.3" then
end,
function(color1, color2, transparency)
local invertedTransparency, r1, g1, b1, r2, g2, b2 = 1 - transparency, color1 >> 16, color1 >> 8 & 0xFF, color1 & 0xFF, color2 >> 16, color2 >> 8 & 0xFF, color2 & 0xFF
local invertedTransparency = 1 - transparency
return
(r2 * invertedTransparency + r1 * transparency) // 1 << 16 |
(g2 * invertedTransparency + g1 * transparency) // 1 << 8 |
(b2 * invertedTransparency + b1 * transparency) // 1
((color2 >> 16) * invertedTransparency + (color1 >> 16) * transparency) // 1 << 16 |
((color2 >> 8 & 0xFF) * invertedTransparency + (color1 >> 8 & 0xFF) * transparency) // 1 << 8 |
((color2 & 0xFF) * invertedTransparency + (color1 & 0xFF) * transparency) // 1
end,
function(color1, color2, position)
local r1, g1, b1, r2, g2, b2 = color1 >> 16, color1 >> 8 & 0xFF, color1 & 0xFF, color2 >> 16, color2 >> 8 & 0xFF, color2 & 0xFF
local r1, g1, b1 = color1 >> 16, color1 >> 8 & 0xFF, color1 & 0xFF
return
(r1 + (r2 - r1) * position) // 1 << 16 |
(g1 + (g2 - g1) * position) // 1 << 8 |
(b1 + (b2 - b1) * position) // 1
(r1 + ((color2 >> 16) - r1) * position) // 1 << 16 |
(g1 + ((color2 >> 8 & 0xFF) - g1) * position) // 1 << 8 |
(b1 + ((color2 & 0xFF) - b1) * position) // 1
end,
function(color24Bit)
@ -67,8 +64,7 @@ if computer.getArchitecture and computer.getArchitecture() == "Lua 5.3" then
]])(palette)
else
integerToRGB, RGBToInteger, blend, transition, to8Bit = load([[
local palette = select(1, ...)
local mathHuge = math.huge
local mathHuge, palette = math.huge, select(1, ...)
return
function(integerColor)
@ -91,18 +87,16 @@ else
r1 = r1 - r1 % 1
local g1 = (color1 - r1 * 65536) / 256
g1 = g1 - g1 % 1
local b1 = color1 - r1 * 65536 - g1 * 256
local r2 = color2 / 65536
r2 = r2 - r2 % 1
local g2 = (color2 - r2 * 65536) / 256
g2 = g2 - g2 % 1
local b2 = color2 - r2 * 65536 - g2 * 256
local r, g, b =
r2 * invertedTransparency + r1 * transparency,
g2 * invertedTransparency + g1 * transparency,
b2 * invertedTransparency + b1 * transparency
(color2 - r2 * 65536 - g2 * 256) * invertedTransparency + (color1 - r1 * 65536 - g1 * 256) * transparency
return
(r - r % 1) * 65536 +
@ -121,12 +115,11 @@ else
r2 = r2 - r2 % 1
local g2 = (color2 - r2 * 65536) / 256
g2 = g2 - g2 % 1
local b2 = color2 - r2 * 65536 - g2 * 256
local r, g, b =
r1 + (r2 - r1) * position,
g1 + (g2 - g1) * position,
b1 + (b2 - b1) * position
b1 + (color2 - r2 * 65536 - g2 * 256 - b1) * position
return
(r - r % 1) * 65536 +

View File

@ -19,8 +19,8 @@ local unicodeLen, unicodeSub = unicode.len, unicode.sub
--------------------------------------------------------------------------------
local function getCoordinates(index)
local integer, fractional = mathModf(index / bufferWidth)
return mathCeil(fractional * bufferWidth), integer + 1
local integer, fractional = index / bufferWidth, index % bufferWidth
return fractional, integer - fractional + 1
end
local function getIndex(x, y)
@ -133,7 +133,7 @@ end
local function get(x, y)
if x >= 1 and y >= 1 and x <= bufferWidth and y <= bufferHeight then
local index = getIndex(x, y)
local index = bufferWidth * (y - 1) + x
return newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index]
else
return 0x000000, 0x000000, " "
@ -142,13 +142,13 @@ end
local function set(x, y, background, foreground, symbol)
if x >= drawLimitX1 and y >= drawLimitY1 and x <= drawLimitX2 and y <= drawLimitY2 then
local index = getIndex(x, y)
local index = bufferWidth * (y - 1) + x
newFrameBackgrounds[index], newFrameForegrounds[index], newFrameSymbols[index] = background, foreground, symbol
end
end
local function square(x, y, width, height, background, foreground, symbol, transparency)
local index, indexStepOnReachOfSquareWidth = getIndex(x, y), bufferWidth - width
local index, indexStepOnReachOfSquareWidth = bufferWidth * (y - 1) + x, bufferWidth - width
for j = y, y + height - 1 do
if j >= drawLimitY1 and j <= drawLimitY2 then
for i = x, x + width - 1 do
@ -182,7 +182,7 @@ local function copy(x, y, width, height)
for j = y, y + height - 1 do
for i = x, x + width - 1 do
if i >= 1 and j >= 1 and i <= bufferWidth and j <= bufferHeight then
index = getIndex(i, j)
index = bufferWidth * (j - 1) + i
tableInsert(copyArray, newFrameBackgrounds[index])
tableInsert(copyArray, newFrameForegrounds[index])
tableInsert(copyArray, newFrameSymbols[index])
@ -197,13 +197,13 @@ local function copy(x, y, width, height)
return copyArray
end
local function paste(xStart, yStart, picture)
local function paste(startX, startY, picture)
local imageWidth = picture[1]
local bufferIndex, imageIndex, bufferIndexStepOnReachOfImageWidth = getIndex(xStart, yStart), 3, bufferWidth - imageWidth
local bufferIndex, imageIndex, bufferIndexStepOnReachOfImageWidth = bufferWidth * (startY - 1) + startX, 3, bufferWidth - imageWidth
for y = yStart, yStart + picture[2] - 1 do
for y = startY, startY + picture[2] - 1 do
if y >= drawLimitY1 and y <= drawLimitY2 then
for x = xStart, xStart + imageWidth - 1 do
for x = startX, startX + imageWidth - 1 do
if x >= drawLimitX1 and x <= drawLimitX2 then
newFrameBackgrounds[bufferIndex] = picture[imageIndex]
newFrameForegrounds[bufferIndex] = picture[imageIndex + 1]
@ -293,7 +293,7 @@ end
local function text(x, y, textColor, data, transparency)
if y >= drawLimitY1 and y <= drawLimitY2 then
local charIndex, bufferIndex = 1, getIndex(x, y)
local charIndex, bufferIndex = 1, bufferWidth * (y - 1) + x
for charIndex = 1, unicodeLen(data) do
if x >= drawLimitX1 and x <= drawLimitX2 then
@ -313,7 +313,7 @@ end
local function formattedText(x, y, data)
if y >= drawLimitY1 and y <= drawLimitY2 then
local charIndex, bufferIndex, textColor, char, number = 1, getIndex(x, y), 0xFFFFFF
local charIndex, bufferIndex, textColor, char, number = 1, bufferWidth * (y - 1) + x, 0xFFFFFF
while charIndex <= unicodeLen(text) do
if x >= drawLimitX1 and x <= drawLimitX2 then
@ -335,13 +335,13 @@ local function formattedText(x, y, data)
end
end
local function image(xStart, yStart, picture, blendForeground)
local function image(startX, startY, picture, blendForeground)
local imageWidth = picture[1]
local bufferIndex, imageIndex, bufferIndexStepOnReachOfImageWidth, imageIndexPlus1, imageIndexPlus2, imageIndexPlus3 = getIndex(xStart, yStart), 3, bufferWidth - imageWidth
local bufferIndex, imageIndex, bufferIndexStepOnReachOfImageWidth, imageIndexPlus1, imageIndexPlus2, imageIndexPlus3 = bufferWidth * (startY - 1) + startX, 3, bufferWidth - imageWidth
for y = yStart, yStart + picture[2] - 1 do
for y = startY, startY + picture[2] - 1 do
if y >= drawLimitY1 and y <= drawLimitY2 then
for x = xStart, xStart + imageWidth - 1 do
for x = startX, startX + imageWidth - 1 do
if x >= drawLimitX1 and x <= drawLimitX2 then
imageIndexPlus1, imageIndexPlus2, imageIndexPlus3 = imageIndex + 1, imageIndex + 2, imageIndex + 3
@ -424,12 +424,12 @@ end
local function semiPixelSet(x, y, color)
local yFixed = mathCeil(y / 2)
if x >= drawLimitX1 and yFixed >= drawLimitY1 and x <= drawLimitX2 and yFixed <= drawLimitY2 then
semiPixelRawSet(getIndex(x, yFixed), color, y % 2 == 0)
semiPixelRawSet(bufferWidth * (yFixed - 1) + x, color, y % 2 == 0)
end
end
local function semiPixelSquare(x, y, width, height, color)
local index, indexStepForward, indexStepBackward, jPercentTwoEqualsZero, jFixed = getIndex(x, mathCeil(y / 2)), (bufferWidth - width), width
local index, indexStepForward, indexStepBackward, jPercentTwoEqualsZero, jFixed = bufferWidth * (mathCeil(y / 2) - 1) + x, (bufferWidth - width), width
for j = y, y + height - 1 do
jPercentTwoEqualsZero = j % 2 == 0
@ -603,10 +603,12 @@ end
local function draw(force)
-- local oldClock = os.clock()
local index, indexStepOnEveryLine, changes = getIndex(drawLimitX1, drawLimitY1), (bufferWidth - drawLimitX2 + drawLimitX1 - 1), {}
local x, equalChars, charX, charIndex, currentForeground
local index, indexStepOnEveryLine, changes = bufferWidth * (drawLimitY1 - 1) + drawLimitX1, (bufferWidth - drawLimitX2 + drawLimitX1 - 1), {}
local x, equalChars, equalCharsIndex, charX, charIndex, currentForeground
local currentFrameBackground, currentFrameForeground, currentFrameSymbol, changesCurrentFrameBackground, changesCurrentFrameBackgroundCurrentFrameForeground
local changesCurrentFrameBackgroundCurrentFrameForegroundIndex
for y = drawLimitY1, drawLimitY2 do
x = drawLimitX1
while x <= drawLimitX2 do
@ -624,7 +626,7 @@ local function draw(force)
currentFrameSymbols[index] = currentFrameSymbol
-- Look for pixels with equal chars from right of current pixel
equalChars, charX, charIndex = {currentFrameSymbol}, x + 1, index + 1
equalChars, equalCharsIndex, charX, charIndex = {currentFrameSymbol}, 2, x + 1, index + 1
while charX <= drawLimitX2 do
-- Pixels becomes equal only if they have same background and (whitespace char or same foreground)
if
@ -639,7 +641,7 @@ local function draw(force)
currentFrameForegrounds[charIndex] = newFrameForegrounds[charIndex]
currentFrameSymbols[charIndex] = newFrameSymbols[charIndex]
tableInsert(equalChars, currentFrameSymbols[charIndex])
equalChars[equalCharsIndex], equalCharsIndex = currentFrameSymbols[charIndex], equalCharsIndex + 1
else
break
end
@ -650,14 +652,15 @@ local function draw(force)
-- Group pixels that need to be drawn by background and foreground
changes[currentFrameBackground] = changes[currentFrameBackground] or {}
changesCurrentFrameBackground = changes[currentFrameBackground]
changesCurrentFrameBackground[currentFrameForeground] = changesCurrentFrameBackground[currentFrameForeground] or {}
changesCurrentFrameBackground[currentFrameForeground] = changesCurrentFrameBackground[currentFrameForeground] or {index = 1}
changesCurrentFrameBackgroundCurrentFrameForeground = changesCurrentFrameBackground[currentFrameForeground]
tableInsert(changesCurrentFrameBackgroundCurrentFrameForeground, x)
tableInsert(changesCurrentFrameBackgroundCurrentFrameForeground, y)
tableInsert(changesCurrentFrameBackgroundCurrentFrameForeground, tableConcat(equalChars))
changesCurrentFrameBackgroundCurrentFrameForegroundIndex = changesCurrentFrameBackgroundCurrentFrameForeground.index
x, index = x + #equalChars - 1, index + #equalChars - 1
changesCurrentFrameBackgroundCurrentFrameForeground[changesCurrentFrameBackgroundCurrentFrameForegroundIndex], changesCurrentFrameBackgroundCurrentFrameForegroundIndex = x, changesCurrentFrameBackgroundCurrentFrameForegroundIndex + 1
changesCurrentFrameBackgroundCurrentFrameForeground[changesCurrentFrameBackgroundCurrentFrameForegroundIndex], changesCurrentFrameBackgroundCurrentFrameForegroundIndex = y, changesCurrentFrameBackgroundCurrentFrameForegroundIndex + 1
changesCurrentFrameBackgroundCurrentFrameForeground[changesCurrentFrameBackgroundCurrentFrameForegroundIndex], changesCurrentFrameBackgroundCurrentFrameForegroundIndex = tableConcat(equalChars), changesCurrentFrameBackgroundCurrentFrameForegroundIndex + 1
x, index, changesCurrentFrameBackgroundCurrentFrameForeground.index = x + equalCharsIndex - 2, index + equalCharsIndex - 2, changesCurrentFrameBackgroundCurrentFrameForegroundIndex
end
x, index = x + 1, index + 1