-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Change box alignment to use PositionalAnchors (#84)
* Fix flake8 moan * feat: change box alignment to use PositionAnchors * feat: Focus now working correctly (I hope). More tests need to be added; but manual verification looks good. Box size is broken * fix: Text box size; base Box bounding rect of children; button text to use Textbox instead of pyglet label. * fix: Text box alignment and dynamic size of Boxes * fix: Ensure bounding rects end up with integer width * fix: Aligning anchors with each other needs to be calculated in world coordinates * fix: Fix all linters; optimise imports and fix tests * fix: Fix typing problem
- Loading branch information
1 parent
d1c3910
commit b53c6af
Showing
59 changed files
with
1,531 additions
and
931 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[pytest] | ||
log_format = %(asctime)s.%(msecs)03d %(levelname)s %(message)s | ||
log_date_format = %Y-%m-%dT%H:%M:%S | ||
log_cli = true | ||
log_cli_level = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"""Module of alignment definitions.""" | ||
|
||
from dataclasses import dataclass | ||
from enum import Enum | ||
|
||
import cocos | ||
|
||
|
||
class HorizontalAlignment(Enum): | ||
"""Enum of possible values for horizontal alignment in pyglet.""" | ||
|
||
left = "left" | ||
right = "right" | ||
center = "center" | ||
|
||
|
||
class VerticalAlignment(Enum): | ||
"""Enum of possible values for vertical alignment in pyglet.""" | ||
|
||
bottom = "bottom" | ||
center = "center" | ||
top = "top" | ||
|
||
|
||
class VerticalTextAlignment(Enum): | ||
"""Enum of possible values for vertical alignment of text in pyglet.""" | ||
|
||
bottom = "bottom" | ||
center = "center" | ||
top = "top" | ||
# Baseline is the bottom of the first line of text, as opposed to `bottom` which is | ||
# the bottom of the pyglet text layout. | ||
baseline = "baseline" | ||
|
||
|
||
class ZIndexEnum(Enum): | ||
"""Indicators of where in the stack of cocos children to add a child.""" | ||
|
||
top = "top" | ||
bottom = "bottom" | ||
|
||
|
||
@dataclass(frozen=True) | ||
class PositionalAnchor: | ||
"""Pair of alignments that define a relative anchor point in 2D space.""" | ||
|
||
horizontal: HorizontalAlignment | ||
vertical: VerticalAlignment | ||
|
||
@property | ||
def x(self) -> HorizontalAlignment: | ||
"""Synonym for `self.horizontal`.""" | ||
return self.horizontal | ||
|
||
@property | ||
def y(self) -> VerticalAlignment: | ||
"""Synonym for `self.vertical`.""" | ||
return self.vertical | ||
|
||
def get_coord_in_rect(self, width: int, height: int) -> cocos.draw.Point2: | ||
""" | ||
Get the (x, y) coordinate of this anchor in the given rect. | ||
:param width: The width of the rect to consider. | ||
:param height: The height of the rect to consider. | ||
:return: cocos.draw.Point2 of the anchor position. | ||
""" | ||
x, y = 0.0, 0.0 | ||
|
||
if self.horizontal == HorizontalAlignment.left: | ||
x = 0 | ||
elif self.horizontal == HorizontalAlignment.center: | ||
x = width / 2 | ||
elif self.horizontal == HorizontalAlignment.right: | ||
x = width | ||
|
||
if self.vertical == VerticalAlignment.bottom: | ||
y = 0 | ||
elif self.vertical == VerticalAlignment.center: | ||
y = height / 2 | ||
elif self.vertical == VerticalAlignment.top: | ||
y = height | ||
|
||
return cocos.draw.Point2(x, y) | ||
|
||
|
||
# Define the 9 basic anchor points of a rectangle. | ||
LeftTop = PositionalAnchor(HorizontalAlignment.left, VerticalAlignment.top) | ||
LeftCenter = PositionalAnchor(HorizontalAlignment.left, VerticalAlignment.center) | ||
LeftBottom = PositionalAnchor(HorizontalAlignment.left, VerticalAlignment.bottom) | ||
CenterTop = PositionalAnchor(HorizontalAlignment.center, VerticalAlignment.top) | ||
CenterCenter = PositionalAnchor(HorizontalAlignment.center, VerticalAlignment.center) | ||
CenterBottom = PositionalAnchor(HorizontalAlignment.center, VerticalAlignment.bottom) | ||
RightTop = PositionalAnchor(HorizontalAlignment.right, VerticalAlignment.top) | ||
RightCenter = PositionalAnchor(HorizontalAlignment.right, VerticalAlignment.center) | ||
RightBottom = PositionalAnchor(HorizontalAlignment.right, VerticalAlignment.bottom) |
Oops, something went wrong.