88 lines
2.1 KiB
Lua

local GUI = require("GUI")
local system = require("System")
local keyboard = require("Keyboard")
local screen = require("Screen")
---------------------------------------------------------------------------------
-- Add a new window to MineOS workspace
local workspace, window, menu = system.addWindow(GUI.filledWindow(1, 1, 82, 28, 0x000000))
local disp = window:addChild(GUI.object(2, 4, 1, 1))
local cursorX, cursorY = 1, 1
local lineFrom = 1
local lines = {}
local input = ""
disp.draw = function(disp)
local x, y = disp.x, disp.y
for i = lineFrom, #lines do
screen.drawText(x, y, 0xFFFFFF, lines[i])
y = y + 1
end
local text = "> " .. input
screen.drawText(x, y, 0xFFFFFF, text)
screen.drawText(x + unicode.len(text), y, 0x00A8FF, "")
end
window.addLine = function(line)
table.insert(lines, line)
if #lines - lineFrom + 1 > disp.height - 1 then
lineFrom = lineFrom + 1
end
end
local overrideWindowEventHandler = window.eventHandler
window.eventHandler = function(workspace, window, ...)
local e = {...}
if e[1] == "scroll" then
lineFrom = lineFrom + (e[5] > 0 and -1 or 1)
if lineFrom < 1 then
lineFrom = 1
elseif lineFrom > #lines then
lineFrom = #lines
end
workspace:draw()
elseif e[1] == "key_down" and GUI.focusedItem == window then
-- Return
if e[4] == 28 then
window.addLine("> " .. input)
input = ""
elseif not keyboard.isControl(e[4]) then
local char = unicode.char(e[3])
input = input .. char
end
workspace:draw()
end
overrideWindowEventHandler(workspace, window, ...)
end
-- Create callback function with resizing rules when window changes its' size
window.onResize = function(newWidth, newHeight)
window.backgroundPanel.width, window.backgroundPanel.height = newWidth, newHeight
disp.width, disp.height = newWidth - 2, newHeight - 3
end
local overrideWindowRemove = window.remove
window.remove = function(...)
system.consoleWindow = nil
overrideWindowRemove(...)
end
---------------------------------------------------------------------------------
system.consoleWindow = window
window.onResize(window.width, window.height)
workspace:draw()