mirror of
https://github.com/CCLeonOS/LeonOS.git
synced 2026-03-03 06:47:00 +00:00
feat: 初始提交 LeonOS 实现
添加 LeonOS 的基本实现,包括: - 核心 API 模块(colors, disk, gps, keys, multishell, parallel, rednet, redstone, settings, vector) - 命令行程序(about, alias, bg, clear, copy, delete, edit, fg, help, list, lua, mkdir, move, paint, peripherals, programs, reboot, set, shutdown, threads) - 系统启动脚本和包管理 - 文档(README.md, LICENSE) - 开发工具(devbin)和更新程序 实现功能: - 完整的线程管理系统 - 兼容 ComputerCraft 的 API 设计 - 改进的 shell 和命令补全系统 - 多标签终端支持 - 设置管理系统
This commit is contained in:
158
data/computercraft/lua/rom/apis/paintutils.lua
Normal file
158
data/computercraft/lua/rom/apis/paintutils.lua
Normal file
@@ -0,0 +1,158 @@
|
||||
-- rc.paintutils
|
||||
|
||||
local term = require("term")
|
||||
local expect = require("cc.expect").expect
|
||||
local textutils = require("textutils")
|
||||
|
||||
local p = {}
|
||||
|
||||
function p.parseImage(str)
|
||||
expect(1, str, "string")
|
||||
return textutils.unserialize(str)
|
||||
end
|
||||
|
||||
function p.loadImage(path)
|
||||
expect(1, path, "string")
|
||||
local handle = io.open(path)
|
||||
if handle then
|
||||
local data = handle:read("a")
|
||||
handle:close()
|
||||
return p.parseImage(data)
|
||||
end
|
||||
end
|
||||
|
||||
function p.drawPixel(x, y, color)
|
||||
expect(1, x, "number")
|
||||
expect(2, y, "number")
|
||||
expect(2, color, "number", "nil")
|
||||
if color then term.setBackgroundColor(color) end
|
||||
term.at(x, y).write(" ")
|
||||
end
|
||||
|
||||
local function drawSteep(x0, y0, x1, y1, color)
|
||||
local distX = x1 - x0
|
||||
local distY = y1 - y0
|
||||
|
||||
local diff = 2*distX - distY
|
||||
|
||||
local x = x0
|
||||
for y=y0, y1, 1 do
|
||||
p.drawPixel(x, y, color)
|
||||
if diff > 0 then
|
||||
x = x + 1
|
||||
diff = diff + 2 * (distX - distY)
|
||||
else
|
||||
diff = diff + 2*distX
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function drawShallow(x0, y0, x1, y1, color)
|
||||
local distX, distY = x1 - x0, y1 - y0
|
||||
local diff = 2*distY - distX
|
||||
local y = y0
|
||||
|
||||
for x=x0, x1, 1 do
|
||||
p.drawPixel(x, y, color)
|
||||
if diff > 0 then
|
||||
y = y + 1
|
||||
diff = diff - 2*distX
|
||||
end
|
||||
diff = diff + 2*distY
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function p.drawLine(_startX, _startY, _endX, _endY, color)
|
||||
expect(1, _startX, "number")
|
||||
expect(2, _startY, "number")
|
||||
expect(3, _endX, "number")
|
||||
expect(4, _endY, "number")
|
||||
expect(5, color, "number")
|
||||
local startX, startY, endX, endY =
|
||||
math.min(_startX, _endX), math.min(_startY, _endY),
|
||||
math.max(_startX, _endX), math.max(_startY, _endY)
|
||||
|
||||
if startX == endX and startY == endY then
|
||||
return p.drawPixel(startX, startY, color)
|
||||
elseif startX == endX then
|
||||
if color then term.setBackgroundColor(color) end
|
||||
for y=startY, endY, 1 do
|
||||
term.at(startX, y).write(" ")
|
||||
end
|
||||
elseif startY == endY then
|
||||
if color then term.setBackgroundColor(color) end
|
||||
term.at(startX, startY).write((" "):rep(endX - startX))
|
||||
end
|
||||
|
||||
if (endY - startY) < (endX - startX) then
|
||||
drawShallow(startX, startY, endX, endY)
|
||||
else
|
||||
drawSteep(startX, startY, endX, endY)
|
||||
end
|
||||
end
|
||||
|
||||
function p.drawBox(startX, startY, endX, endY, color)
|
||||
expect(1, startX, "number")
|
||||
expect(2, startY, "number")
|
||||
expect(3, endX, "number")
|
||||
expect(4, endY, "number")
|
||||
expect(5, color, "number")
|
||||
|
||||
local col = string.format("%x", math.floor(math.log(color, 2)))
|
||||
local ht, hc = (" "):rep(endX-startX+1), col:rep(endX-startX+1)
|
||||
|
||||
term.at(startX, startY).blit(ht, hc, hc)
|
||||
for i=startY, endY do
|
||||
term.at(startX, i).blit(" ", col, col)
|
||||
term.at(endX, i).blit(" ", col, col)
|
||||
end
|
||||
term.at(startX, endY).blit(ht, hc, hc)
|
||||
end
|
||||
|
||||
function p.drawFilledBox(startX, startY, endX, endY, color)
|
||||
expect(1, startX, "number")
|
||||
expect(2, startY, "number")
|
||||
expect(3, endX, "number")
|
||||
expect(4, endY, "number")
|
||||
expect(5, color, "number")
|
||||
|
||||
local col = string.format("%x", math.floor(math.log(color, 2)))
|
||||
local ht, hc = (" "):rep(endX-startX+1), col:rep(endX-startX+1)
|
||||
|
||||
for y=startY, endY, 1 do
|
||||
term.at(startX, y).blit(ht, hc, hc)
|
||||
end
|
||||
end
|
||||
|
||||
function p.drawImage(img, x, y, frame)
|
||||
expect(1, img, "table")
|
||||
expect(2, x, "number")
|
||||
expect(3, y, "number")
|
||||
expect(4, frame, "number", "nil")
|
||||
|
||||
frame = frame or 1
|
||||
if not img[frame] then
|
||||
return nil, "invalid frame index " .. frame
|
||||
end
|
||||
|
||||
if img.palette then
|
||||
for k, v in pairs(img.palette) do
|
||||
term.setPaletteColor(k, table.unpack(v))
|
||||
end
|
||||
end
|
||||
|
||||
if img[frame].palette then
|
||||
for k, v in pairs(img[frame].palette) do
|
||||
term.setPaletteColor(k, table.unpack(v))
|
||||
end
|
||||
end
|
||||
|
||||
for i, line in ipairs(img[frame]) do
|
||||
term.at(x+i-1, y).blit(table.unpack(line))
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
return p
|
||||
Reference in New Issue
Block a user