mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-08-30 17:17:55 +00:00
More examples
Yay!
This commit is contained in:
@@ -341,3 +341,6 @@ b0651082f472baee8f0fa8ec7ba95f433e2637bb
|
||||
|
||||
Cleanup examples for the various modules
|
||||
```
|
||||
|
||||
Ignored Documentation Changes, these are locate
|
||||
```
|
||||
|
@@ -64,6 +64,10 @@ end
|
||||
--
|
||||
-- @treturn table|nil The parsed image data, suitable for use with
|
||||
-- @{paintutils.drawImage}, or `nil` if the file does not exist.
|
||||
-- @usage Load an image and draw it.
|
||||
--
|
||||
-- local image = paintutils.loadImage("test-image.nfp")
|
||||
-- paintutils.drawImage(image, term.getCursorPos())
|
||||
function loadImage(path)
|
||||
expect(1, path, "string")
|
||||
|
||||
@@ -107,6 +111,7 @@ end
|
||||
-- @tparam number endY The end y position of the line.
|
||||
-- @tparam[opt] number colour The @{colors|color} of this pixel. This will be
|
||||
-- the current background colour if not specified.
|
||||
-- @usage paintutils.drawLine(2, 3, 30, 7, colors.red)
|
||||
function drawLine(startX, startY, endX, endY, colour)
|
||||
expect(1, startX, "number")
|
||||
expect(2, startY, "number")
|
||||
@@ -170,6 +175,7 @@ end
|
||||
-- @tparam number endY The end y position of the line.
|
||||
-- @tparam[opt] number colour The @{colors|color} of this pixel. This will be
|
||||
-- the current background colour if not specified.
|
||||
-- @usage paintutils.drawBox(2, 3, 30, 7, colors.red)
|
||||
function drawBox(startX, startY, endX, endY, nColour)
|
||||
expect(1, startX, "number")
|
||||
expect(2, startY, "number")
|
||||
@@ -222,6 +228,7 @@ end
|
||||
-- @tparam number endY The end y position of the line.
|
||||
-- @tparam[opt] number colour The @{colors|color} of this pixel. This will be
|
||||
-- the current background colour if not specified.
|
||||
-- @usage paintutils.drawFilledBox(2, 3, 30, 7, colors.red)
|
||||
function drawFilledBox(startX, startY, endX, endY, nColour)
|
||||
expect(1, startX, "number")
|
||||
expect(2, startY, "number")
|
||||
|
@@ -1,18 +1,19 @@
|
||||
--- Provides a simple implementation of multitasking.
|
||||
--
|
||||
-- Functions are not actually executed simultaniously, but rather this API will
|
||||
-- automatically switch between them whenever they yield (eg whenever they call
|
||||
-- @{coroutine.yield}, or functions that call that - eg `os.pullEvent` - or
|
||||
-- functions that call that, etc - basically, anything that causes the function
|
||||
-- to "pause").
|
||||
--
|
||||
-- Each function executed in "parallel" gets its own copy of the event queue,
|
||||
-- and so "event consuming" functions (again, mostly anything that causes the
|
||||
-- script to pause - eg `sleep`, `rednet.receive`, most of the `turtle` API,
|
||||
-- etc) can safely be used in one without affecting the event queue accessed by
|
||||
-- the other.
|
||||
--
|
||||
-- @module parallel
|
||||
--[[- Provides a simple implementation of multitasking.
|
||||
|
||||
Functions are not actually executed simultaniously, but rather this API will
|
||||
automatically switch between them whenever they yield (eg whenever they call
|
||||
@{coroutine.yield}, or functions that call that - eg `os.pullEvent` - or
|
||||
functions that call that, etc - basically, anything that causes the function
|
||||
to "pause").
|
||||
|
||||
Each function executed in "parallel" gets its own copy of the event queue,
|
||||
and so "event consuming" functions (again, mostly anything that causes the
|
||||
script to pause - eg `sleep`, `rednet.receive`, most of the `turtle` API,
|
||||
etc) can safely be used in one without affecting the event queue accessed by
|
||||
the other.
|
||||
|
||||
@module parallel
|
||||
]]
|
||||
|
||||
local function create(...)
|
||||
local tFns = table.pack(...)
|
||||
@@ -70,21 +71,53 @@ local function runUntilLimit(_routines, _limit)
|
||||
end
|
||||
end
|
||||
|
||||
--- Switches between execution of the functions, until any of them
|
||||
-- finishes. If any of the functions errors, the message is propagated upwards
|
||||
-- from the @{parallel.waitForAny} call.
|
||||
--
|
||||
-- @tparam function ... The functions this task will run
|
||||
--[[- Switches between execution of the functions, until any of them
|
||||
finishes. If any of the functions errors, the message is propagated upwards
|
||||
from the @{parallel.waitForAny} call.
|
||||
|
||||
@tparam function ... The functions this task will run
|
||||
@usage Print a message every second until the `q` key is pressed.
|
||||
|
||||
local function tick()
|
||||
while true do
|
||||
os.sleep(1)
|
||||
print("Tick")
|
||||
end
|
||||
end
|
||||
local function wait_for_q()
|
||||
repeat
|
||||
local _, key = os.pullEvent("key")
|
||||
until key == keys.q
|
||||
print("Q was pressed!")
|
||||
end
|
||||
|
||||
parallel.waitForAny(tick, wait_for_q)
|
||||
print("Everything done!")
|
||||
]]
|
||||
function waitForAny(...)
|
||||
local routines = create(...)
|
||||
return runUntilLimit(routines, #routines - 1)
|
||||
end
|
||||
|
||||
--- Switches between execution of the functions, until all of them are
|
||||
-- finished. If any of the functions errors, the message is propagated upwards
|
||||
-- from the @{parallel.waitForAll} call.
|
||||
--
|
||||
-- @tparam function ... The functions this task will run
|
||||
--[[- Switches between execution of the functions, until all of them are
|
||||
finished. If any of the functions errors, the message is propagated upwards
|
||||
from the @{parallel.waitForAll} call.
|
||||
|
||||
@tparam function ... The functions this task will run
|
||||
@usage Start off two timers and wait for them both to run.
|
||||
|
||||
local function a()
|
||||
os.sleep(1)
|
||||
print("A is done")
|
||||
end
|
||||
local function b()
|
||||
os.sleep(3)
|
||||
print("B is done")
|
||||
end
|
||||
|
||||
parallel.waitForAll(a, b)
|
||||
print("Everything done!")
|
||||
]]
|
||||
function waitForAll(...)
|
||||
local routines = create(...)
|
||||
return runUntilLimit(routines, 0)
|
||||
|
@@ -252,8 +252,8 @@ local function drawCanvasLine(y)
|
||||
bg = bg .. color_hex_lookup[pixel or canvasColour]
|
||||
else
|
||||
text = text .. "\127"
|
||||
fg = fg .. color_hex_lookup[canvasColour]
|
||||
bg = bg .. color_hex_lookup[colours.grey]
|
||||
fg = fg .. color_hex_lookup[colours.grey]
|
||||
bg = bg .. color_hex_lookup[canvasColour]
|
||||
end
|
||||
end
|
||||
term.setCursorPos(1, y)
|
||||
|
Reference in New Issue
Block a user