Skip to content

Commit dcff5ba

Browse files
committed
Add paintutils
1 parent f8d3680 commit dcff5ba

File tree

3 files changed

+108
-3
lines changed

3 files changed

+108
-3
lines changed

computercraft/server.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from .subapis.keys import KeysAPI
2222
from .subapis.multishell import MultishellAPI
2323
from .subapis.os import OSAPI
24+
from .subapis.paintutils import PaintutilsAPI
2425
from .subapis.peripheral import PeripheralAPI
2526
from .subapis.pocket import PocketAPI
2627
from .subapis.rednet import RednetAPI
@@ -66,16 +67,17 @@ def __init__(self, nid, program, cleanup_callback):
6667
self.keys = KeysAPI(self, 'keys')
6768
self.multishell = MultishellAPI(self, 'multishell')
6869
self.os = OSAPI(self, 'os')
70+
self.paintutils = PaintutilsAPI(self, 'paintutils')
6971
self.peripheral = PeripheralAPI(self, 'peripheral')
7072
self.pocket = PocketAPI(self, 'pocket')
7173
self.rednet = RednetAPI(self, 'rednet')
7274
self.redstone = RedstoneAPI(self, 'redstone')
7375
self.settings = SettingsAPI(self, 'settings')
74-
self.shell = ShellAPI(self, 'shell') # TODO: autocomplete functions
75-
self.term = TermAPI(self, 'term') # TODO: window redirections
76+
self.shell = ShellAPI(self, 'shell')
77+
self.term = TermAPI(self, 'term')
7678
self.textutils = TextutilsAPI(self, 'textutils')
7779
self.turtle = TurtleAPI(self, 'turtle')
78-
self.window = WindowAPI(self, 'window') # TODO: unimplemented
80+
self.window = WindowAPI(self, 'window')
7981

8082
async def prog_wrap():
8183
err = None
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
3+
from .base import BaseSubAPI
4+
from ..rproc import nil, integer, fact_array
5+
6+
7+
array_2d_integer = fact_array(fact_array(integer))
8+
9+
10+
class PaintutilsAPI(BaseSubAPI):
11+
async def parseImage(self, data: str) -> List[List[int]]:
12+
return array_2d_integer(await self._send('parseImage', data))
13+
14+
async def loadImage(self, path: str) -> List[List[int]]:
15+
return array_2d_integer(await self._send('loadImage', path))
16+
17+
async def drawPixel(self, x: int, y: int, color: int = None):
18+
return nil(await self._send('drawPixel', x, y, color))
19+
20+
async def drawLine(self, startX: int, startY: int, endX: int, endY: int, color: int = None):
21+
return nil(await self._send('drawLine', startX, startY, endX, endY, color))
22+
23+
async def drawBox(self, startX: int, startY: int, endX: int, endY: int, color: int = None):
24+
return nil(await self._send('drawBox', startX, startY, endX, endY, color))
25+
26+
async def drawFilledBox(self, startX: int, startY: int, endX: int, endY: int, color: int = None):
27+
return nil(await self._send('drawFilledBox', startX, startY, endX, endY, color))
28+
29+
async def drawImage(self, image: List[List[int]], xPos: int, yPos: int):
30+
return nil(await self._send('drawImage', image, xPos, yPos))

testmod.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,4 +1734,77 @@ async def test_redirect_to_remote_monitor(api):
17341734
await api.print('Test finished successfully')
17351735

17361736

1737+
pixels = '''
1738+
0000000030030033333333330000000003000000000000000
1739+
0333300000000033333333300000000000333333000000330
1740+
0803000000803033333333000000000000880330300003000
1741+
0800800030330333333333000300883000888880000033000
1742+
3333000000003333333333300080038880000080000888003
1743+
33333ddd3333333333333333300000333330000000000d033
1744+
333dddddd3333333333333333333333333333333333ddd333
1745+
3333ccdd333333333333344444444333333333333dddddd33
1746+
333cc33d3333333333334444444444333333333335d3cc33d
1747+
5ddc33333333333333344444444444433333333333333cd55
1748+
dddc555d3333333333344444444444433333333333d5dc5dd
1749+
d5dd5dd4bbbbbbbbb999b00b00300b3bb9999bbbb4ddddddd
1750+
ddd55444bb999993bbb33390b030bb9999bbbbbbb444ddddd
1751+
55dd44bbbbbbbbbbbbb9bb3003003bbb339bbbbbbbb44444d
1752+
dd444bbbbbbbbbbb99933bbb0030b999bbbbbbbbbbbbbbb44
1753+
444bbbbbbbbbbbbbbb9bbb33b309933bbbbbbbbbbbbbbbbbb
1754+
bbbbbbbbbbbbbbbbbbbb9bbbb3bbbb99bbbbbbbbbbbbbbbbb
1755+
bbbbbbbbbbbbbbbbbbbbbb399399bbbbbbbbbbbbbbbbbbbbb
1756+
'''.strip()
1757+
1758+
1759+
async def test_paintutils(api):
1760+
from computercraft.subapis.paintutils import PaintutilsAPI
1761+
tbl = await get_object_table(api, 'paintutils')
1762+
assert get_class_table(PaintutilsAPI) == tbl
1763+
1764+
async with api.fs.open('img.nfp', 'w') as f:
1765+
await f.write(pixels)
1766+
1767+
# from pprint import pprint
1768+
int_pixels = await api.paintutils.loadImage('img.nfp')
1769+
assert len(int_pixels) > 0
1770+
assert len(int_pixels[0]) > 0
1771+
assert await api.paintutils.parseImage(pixels) == int_pixels
1772+
1773+
assert await api.paintutils.drawImage(int_pixels, 1, 1) is None
1774+
1775+
await asyncio.sleep(2)
1776+
1777+
await api.term.setTextColor(api.colors.white)
1778+
await api.term.setBackgroundColor(api.colors.black)
1779+
await api.term.clear()
1780+
await api.term.setBackgroundColor(api.colors.green)
1781+
1782+
by = 3
1783+
bx = 3
1784+
1785+
assert await api.paintutils.drawPixel(bx, by) is None
1786+
assert await api.paintutils.drawPixel(bx + 1, by, api.colors.red) is None
1787+
1788+
bx += 5
1789+
1790+
assert await api.paintutils.drawLine(bx, by, bx + 3, by + 3) is None
1791+
assert await api.paintutils.drawLine(bx + 3, by, bx, by + 3, api.colors.red) is None
1792+
1793+
bx += 5
1794+
assert await api.paintutils.drawBox(bx, by, bx + 3, by + 3) is None
1795+
bx += 5
1796+
assert await api.paintutils.drawBox(bx, by, bx + 3, by + 3, api.colors.red) is None
1797+
1798+
bx += 5
1799+
assert await api.paintutils.drawFilledBox(bx, by, bx + 3, by + 3) is None
1800+
bx += 5
1801+
assert await api.paintutils.drawFilledBox(bx, by, bx + 3, by + 3, api.colors.red) is None
1802+
1803+
await api.term.setCursorPos(1, by + 6)
1804+
1805+
await asyncio.sleep(2)
1806+
1807+
await api.print('Test finished successfully')
1808+
1809+
17371810
# vector won't be implemented, use python equivalent

0 commit comments

Comments
 (0)