Skip to content

Commit 68932c2

Browse files
committed
Improved Rectangle and NpatchInfo (added some properties etc.)
1 parent 25deeb5 commit 68932c2

File tree

1 file changed

+106
-6
lines changed

1 file changed

+106
-6
lines changed

raylibpy/__init__.py

Lines changed: 106 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,20 +1908,91 @@ def yiq(self, value: Union[Seq, Vector4, Vector3]) -> None:
19081908
ColorPtr = POINTER(Color)
19091909

19101910

1911-
class Rectangle(Structure):
1911+
class _Rectangle(Structure):
19121912
_fields_ = [
19131913
('x', c_float),
19141914
('y', c_float),
19151915
('width', c_float),
19161916
('height', c_float),
19171917
]
19181918

1919+
1920+
class Rectangle(_Rectangle):
1921+
1922+
@classmethod
1923+
def from_ltrb(cls, *args) -> 'Rectangle':
1924+
"""Alternate constructor."""
1925+
result = _flatten((int, float), *args, map_to=float)
1926+
if len(result) != 4:
1927+
raise ValueError("Too many or too few initializers ({} instead of 4).".format(len(result)))
1928+
1929+
l, t, r, b = result
1930+
return cls(l, t, r - l, t - b)
1931+
1932+
def __init__(self, *args) -> None:
1933+
"""Constructor."""
1934+
result = _flatten((int, float), *args, map_to=float)
1935+
if len(result) != 4:
1936+
raise ValueError("Too many or too few initializers ({} instead of 4).".format(len(result)))
1937+
super(Rectangle, self).__init__(*result)
1938+
19191939
def __str__(self) -> str:
19201940
return "({}, {}, {}, {})".format(self.x, self.y, self.width, self.height)
19211941

19221942
def __repr__(self) -> str:
19231943
return "{}({}, {}, {}, {})".format(self.__class__.__qualname__, self.x, self.y, self.width, self.height)
19241944

1945+
@property
1946+
def empty(self) -> bool:
1947+
"""Returns whether the rec's width or height is below or equal zero."""
1948+
return self.width <= 0 or self.height <= 0
1949+
1950+
@property
1951+
def r(self) -> float:
1952+
"""Gets or sets the right-most rect coordinate."""
1953+
return self.x + self.width
1954+
1955+
@r.setter
1956+
def r(self, value: float) -> None:
1957+
self.x = float(value) - self.width
1958+
1959+
@property
1960+
def b(self) -> float:
1961+
"""Gets or sets the bottom-most rect coordinate."""
1962+
return self.y + self.height
1963+
1964+
@b.setter
1965+
def b(self, value: float) -> None:
1966+
self.y = float(value) - self.height
1967+
1968+
@property
1969+
def center(self) -> Vector2:
1970+
"""Gets or sets the rec position relative to its center."""
1971+
return Vector2(self.x + self.width * 0.5,
1972+
self.y + self.height * 0.5)
1973+
1974+
@center.setter
1975+
def center(self, value: Union[Seq, Vector2]) -> None:
1976+
self.pos = _vec2(value) - self.size * 0.5
1977+
1978+
@property
1979+
def pos(self) -> Vector2:
1980+
"""Gets or sets the rec top-left coordinate."""
1981+
return Vector2(self.x, self.y)
1982+
1983+
@pos.setter
1984+
def pos(self, value: Union[Seq, Vector2]) -> None:
1985+
self.x, self.y = map(float, value)
1986+
1987+
@property
1988+
def size(self) -> Vector2:
1989+
"""Gets or sets the rec dimensions."""
1990+
return Vector2(self.width, self.height)
1991+
1992+
@size.setter
1993+
def size(self, value: Union[Seq, Vector2]) -> None:
1994+
self.width, self.height = map(float, value)
1995+
19251996

19261997
class Image(Structure):
19271998
_fields_ = [
@@ -1991,7 +2062,7 @@ def __str__(self) -> str:
19912062
return "(RENDERTEXTURE: {}w, {}h, texture: {}, depth: {})".format(self.width, self.height, self.texture, self.depth)
19922063

19932064

1994-
class NPatchInfo(Structure):
2065+
class _NPatchInfo(Structure):
19952066
_fields_ = [
19962067
('sourceRec', Rectangle),
19972068
('left', c_int),
@@ -2001,11 +2072,36 @@ class NPatchInfo(Structure):
20012072
('type', c_int),
20022073
]
20032074

2004-
def set_border_width(self, a: float, b: float, c: float, d: float):
2005-
self.borderWidth = (c_float * 4)(_float(a), _float(b), _float(c), _float(d))
20062075

2007-
def set_padding(self, a: int, b: int, c: int, d: int):
2008-
self.padding = (c_int * 4)(_int(a), _int(b), _int(c), _int(d))
2076+
class NPatchInfo(_NPatchInfo):
2077+
2078+
def __init__(self, source_rec: 'Rectangle', left: int=1, top:int=1, right: int=1, bottom: int=1, npatch_type: Union[int, 'NPatchType']=0) -> None:
2079+
if npatch_type not in NPatchType:
2080+
npatch_type = {
2081+
0: NPT_9PATCH,
2082+
1: NPT_3PATCH_VERTICAL,
2083+
2: NPT_3PATCH_VERTICAL
2084+
}.get(npatch_type, NPT_9PATCH)
2085+
2086+
super(NPatchInfo, self).__init__(source_rec, left, top, right, bottom, npatch_type)
2087+
2088+
def __str__(self) -> str:
2089+
"""Textual representation."""
2090+
npt = {
2091+
0: NPT_9PATCH,
2092+
1: NPT_3PATCH_VERTICAL,
2093+
2: NPT_3PATCH_VERTICAL
2094+
}.get(self.type, NPT_9PATCH).name
2095+
return "(NPATCHINFO: rec: {0.sourceRec}, ltrb: [{0.left}, {0.top}, {0.right}, {0.bottom}], type: {1})".format(self, npt)
2096+
2097+
def __repr__(self) -> str:
2098+
rc = repr(self.sourceRec)
2099+
npt = {
2100+
0: NPT_9PATCH,
2101+
1: NPT_3PATCH_VERTICAL,
2102+
2: NPT_3PATCH_VERTICAL
2103+
}.get(self.type, NPT_9PATCH).name
2104+
return "{0.__class__.__qualname__}({1}, {0.left}, {0.top}, {0.right}, {0.bottom}, {2})".format(self, rc, npt)
20092105

20102106

20112107
class CharInfo(Structure):
@@ -5024,3 +5120,7 @@ def set_audio_stream_volume(stream: AudioStream, volume: float) -> None:
50245120
def set_audio_stream_pitch(stream: AudioStream, pitch: float) -> None:
50255121
"""Set pitch for audio stream (1.0 is base level)"""
50265122
return _rl.SetAudioStreamPitch(stream, _float(pitch))
5123+
5124+
np = NPatchInfo(Rectangle(5, 5, 10, 10), 1, 1, 1, 1, 0)
5125+
print(repr(np))
5126+
print(np)

0 commit comments

Comments
 (0)