rIcon support.

This commit is contained in:
Kelvin Sherlock 2020-08-08 18:33:12 -04:00
parent e7e86e8a36
commit 352e6e57fb
3 changed files with 106 additions and 1 deletions

3
cli.py
View File

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

18
examples/icon.prez Normal file
View File

@ -0,0 +1,18 @@
rIcon(
color=True,
height = 4,
width = 4,
image = bytes.fromhex(
"ffff"
"f00f"
"f00f"
"ffff"
),
mask = ( # will call bytes.fromhex
"0000"
"0ff0"
"0ff0"
"0000"
)
)

86
icon.py Normal file
View File

@ -0,0 +1,86 @@
from base import rObject
import struct
__all__ = ["rIcon"]
def rez_hex(bb, width=16, indent = 0, comma = False):
max = (len(bb) + width-1) // width
data = [bb[x*width:x*width+width] for x in range(0, max)]
prefix = "\t" * indent
tmp = "\n".join([prefix + '$"' + x.hex() + '"' for x in data])
if comma: tmp += ",\n"
else: tmp += "\n"
return tmp
class rIcon(rObject):
rName = "rIcon"
rType = 0x8001
def __init__(self, *,
height = None,
width = None,
# size = None, # height / width tuple
image = None, # bytes/byte array
mask = None, # bytes/ bytearray
color = True,
**kwargs
):
super().__init__(**kwargs)
if height == None and width == None:
raise TypeError("rIcon: missing height/width or size")
if type(image) == str: image = bytes.fromhex(image)
if type(mask) == str: mask = bytes.fromhex(mask)
if type(image) not in (bytes, bytearray):
raise TypeError("rIcon: bad image type ({})".format(type(image)))
if type(mask) not in (bytes, bytearray):
raise TypeError("rIcon: bad mask type ({})".format(type(mask)))
# can't support, eg 1 x 1 since that's only half a byte.
if width & 0x01:
raise ValueError("rIcon: width must be even.")
expected = height * width // 2
if len(image) != expected:
raise ValueError("rIcon: bad image size")
if len(mask) != expected:
raise ValueError("rIcon: bad mask size")
self.type = 0x8000 if color else 0x0000
self.image = bytes(image)
self.mask = bytes(mask)
self.height = height
self.width = width
def __bytes__(self):
bb = struct.pack("<4H",
self.type,
len(self.image),
self.height,
self.width
)
return b"".join([bb, self.image, self.mask])
def _rez_string(self):
s = (
"\t0x{:04x}, /* type */\n"
"\t{:d}, /* height */\n"
"\t{:d}, /* width */\n"
).format(self.type, self.height, self.width)
s += "\n\t/*image */\n"
s += rez_hex(self.image, width = self.width // 2, indent = 1, comma = True)
s += "\n\t/*mask */\n"
s += rez_hex(self.mask, width = self.width // 2, indent = 1, comma = False)
return s