move rToolStartup to a separate file and add typed constants.

This commit is contained in:
Kelvin Sherlock 2020-08-03 12:50:11 -04:00
parent 41ebc1ba0d
commit 09d9718d69
4 changed files with 111 additions and 62 deletions

62
base.py
View File

@ -11,7 +11,7 @@ from constants import *
__all__ = ["rObject", "rText", "rTextBlock", "rTextForLETextBox2",
"rAlertString", "rErrorString", "rComment", "rPString",
"rCString", "rWString", "rC1InputString", "rStringList",
"rTwoRects", "rRectList", "rToolStartup"]
"rTwoRects", "rRectList"]
#define KeyEquiv array[1]{ char; char; _mybase_ word; _mybase_ word; }
@ -473,63 +473,3 @@ class rRectList(rObject):
if self.rects: rv += "\n"
rv += "\t}"
return rv;
class rToolStartup(rObject):
"""
mode: 320 or 640
tools: tool number or (tool number, version)
"""
rName = "rToolStartup"
rType = 0x8013
def __init__(self, *tools, mode=640, **kwargs):
super().__init__(**kwargs)
self.mode = 640
self.tools = tools
if mode not in (320,640): raise ValueError("Bad mode: {}".format(mode))
for x in tools:
if type(x) == int: continue
if type(x) == tuple and len(x) == 2:
a,b = x
if type(a) == int and type(b) == int: continue
raise ValueError("Bad tool: {}".format(x))
def __bytes__(self):
bb = struct.pack("<HHHIH",
0, # flags,
0x80 if self.mode == 640 else 0x00,
0, 0,
len(self.tools)
)
for x in self.tools:
if type(x) == tuple: a,b = x
else: a = x; b = 0
bb += struct.pack("<HH", a, b)
return bb
def _rez_string(self):
rv = (
"\t{}, /* mode */\n"
"\t{{\n".format(
"mode640" if self.mode == 640 else "mode320"
))
tmp = []
for x in self.tools:
if type(x) == tuple: a,b = x
else: a = x; b = 0
tmp.append("\t\t{}, 0x{:04x}".format(a, b))
rv += ",\n".join(tmp)
if self.tools: rv += "\n"
rv += "\t}"
return rv

3
cli.py
View File

@ -26,11 +26,12 @@ def rez_scope():
import sound
import rect
import version
import tool_startup
import constants
# could do: mod = importlib.import_module("base"), etc.
scope = {}
for mod in (base, window, control, menu, sound, rect, version, constants):
for mod in (base, window, control, menu, sound, rect, version, tool_startup, constants):
if hasattr(mod, '__all__'): keys = mod.__all__
else: keys = [x for x in dir(mod) if x[0] != '_']

18
examples/tools.prez Normal file
View File

@ -0,0 +1,18 @@
#
#
#
rToolStartup(0xc080, 1, 2, 3, 4, 5)
rToolStartup(mode320, (1, 0), (37, 0x0203))
rToolStartup(mode640 | fUseShadowing | fFastPortAware,
(3, 0x300),
(4, 0x301),
(5, 0x302),
(6, 0x300),
(8, 0x301),
(11, 0x200),
(30, 0x100)
)

90
tool_startup.py Normal file
View File

@ -0,0 +1,90 @@
from base import rObject
from utils import *
import enum
import struct
__all__ = ["rToolStartup"]
# can't be in utils since that's a different __all__
def export_enum(cls):
global __all__
members = cls.__members__
globals().update(members)
if __all__ != None: __all__.extend(list(members))
return cls
@export_enum
class Flags(enum.Flag):
mode320 = 0
mode640 = 0x80
fFastPortAware = 0x4000
fUseShadowing = 0x8000
class rToolStartup(rObject):
"""
mode: 320 or 640
tools: tool number or (tool number, version)
"""
rName = "rToolStartup"
rType = 0x8013
# mode | 0x4000 - fastport aware
# mode | 0x8000 - hardware shadowing
def __init__(self, mode, *tools, **kwargs):
super().__init__(**kwargs)
if type(mode) == Flags: mode = mode.value
elif type(mode) == int: pass
else: raise TypeError("rToolStartup: bad mode: {} ({})".format(mode, type(mode)))
self.mode = mode
for x in tools:
if type(x) == int: continue
if type(x) == tuple and len(x) == 2:
a,b = x
if type(a) == int and type(b) == int: continue
raise Type("rToolStartup: bad tool: {}".format(x))
self.tools = [x if type(x) == tuple else (x, 0) for x in tools]
def __bytes__(self):
bb = struct.pack("<HHHIH",
0, # flags,
self.mode,
0, 0,
len(self.tools)
)
for a,b in self.tools:
bb += struct.pack("<HH", a, b)
return bb
def _rez_string(self):
mode = None
if self.mode in (0x00, 0x80):
mode = {0x00: "mode320", 0x80: "mode640"}[self.mode]
else:
mode = "0x{:04x}".format(self.mode)
rv = (
"\t{}, /* mode */\n"
"\t{{\n".format(
mode
))
tmp = []
for a, b in self.tools:
tmp.append("\t\t{}, 0x{:04x}".format(a, b))
rv += ",\n".join(tmp)
if self.tools: rv += "\n"
rv += "\t}"
return rv