Skip to content

Commit

Permalink
Added margin support
Browse files Browse the repository at this point in the history
  • Loading branch information
TeaCondemns committed May 8, 2023
1 parent cbaf3d8 commit c8b5914
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 34 deletions.
7 changes: 4 additions & 3 deletions pygex/gui/buttonview.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from pygex.color import TYPE_COLOR, COLOR_WHITE, COLOR_GREEN, COLOR_TRANSPARENT, to_readable_color, replace_alpha
from pygex.gui.view import DEFAULT_SIZE, DEFAULT_PADDING, DEFAULT_POSITION, GRAVITY_CENTER_HORIZONTAL
from pygex.gui.view import DEFAULT_SIZE, DEFAULT_PADDING, DEFAULT_POSITION, GRAVITY_CENTER, DEFAULT_MARGIN
from pygex.gui.drawable.interactiondrawable import FadingDrawable
from pygex.gui.drawable.drawable import Drawable, ColorDrawable
from pygex.text import ALIGN_CENTER, DEFAULT_FONT_SIZE
from pygex.gui.view import GRAVITY_CENTER_VERTICAL
from pygex.gui.textview import TextView
from pygame.font import FontType
from typing import Sequence
Expand All @@ -17,7 +16,8 @@ def __init__(
size: Sequence[int] = DEFAULT_SIZE,
pos: Sequence[float | int] = DEFAULT_POSITION,
padding: Sequence[int] = DEFAULT_PADDING,
content_gravity=GRAVITY_CENTER_HORIZONTAL | GRAVITY_CENTER_VERTICAL,
margin: Sequence[int] = DEFAULT_MARGIN,
content_gravity=GRAVITY_CENTER,
text_align=ALIGN_CENTER,
text_line_spacing: float | int = 0,
text_lines_number: int = ...,
Expand All @@ -36,6 +36,7 @@ def __init__(
size,
pos,
padding,
margin,
content_gravity,
text_align,
text_line_spacing,
Expand Down
22 changes: 12 additions & 10 deletions pygex/gui/linearlayout.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from pygex.gui.view import View, DEFAULT_PADDING, DEFAULT_SIZE, DEFAULT_POSITION, DEFAULT_GRAVITY, SIZE_WRAP_CONTENT
from pygex.gui.view import GRAVITY_RIGHT, GRAVITY_BOTTOM, GRAVITY_CENTER_HORIZONTAL, GRAVITY_CENTER_VERTICAL
from pygex.gui.view import VISIBILITY_GONE, DEFAULT_MARGIN
from pygex.color import TYPE_COLOR, COLOR_TRANSPARENT
from pygex.gui.drawable.drawable import Drawable
from pygex.gui.view import VISIBILITY_GONE
from pygame.surface import SurfaceType
from pygex.image import AlphaSurface
from pygame.event import Event
Expand All @@ -23,6 +23,7 @@ def __init__(
size: Sequence[int] = DEFAULT_SIZE,
pos: Sequence[float | int] = DEFAULT_POSITION,
padding: Sequence[int] = DEFAULT_PADDING,
margin: Sequence[int] = DEFAULT_MARGIN,
content_gravity: int = DEFAULT_GRAVITY,
background_drawable_or_color: Drawable | TYPE_COLOR = COLOR_TRANSPARENT,
prerender_during_initialization: bool = True
Expand All @@ -31,6 +32,7 @@ def __init__(
size,
pos,
padding,
margin,
content_gravity,
background_drawable_or_color,
prerender_during_initialization=False
Expand All @@ -52,7 +54,7 @@ def __init__(
self._views.remove(view)
continue

view_background_computed_size = view.get_computed_background_size()
view_background_computed_size = view.get_computed_background_size(apply_margin=True)

self._buffered_view_sizes.append(view_background_computed_size)
self._buffered_views_oriented_total_width += view_background_computed_size[0]
Expand Down Expand Up @@ -107,7 +109,7 @@ def rebufferize_sizes_for_view(self, view: View):
return

view_index = self._views.index(view)
view_background_computed_size = view.get_computed_background_size()
view_background_computed_size = view.get_computed_background_size(apply_margin=True)
buffered_view_background_computed_size = self._buffered_view_sizes[view_index]

self._buffered_views_oriented_total_width += (
Expand All @@ -134,7 +136,7 @@ def add_view(self, view: View):
view._parent = self
self._views.append(view)

view_background_computed_size = view.get_computed_background_size()
view_background_computed_size = view.get_computed_background_size(apply_margin=True)

self._buffered_view_sizes.append(view_background_computed_size)
self._buffered_views_oriented_total_width += view_background_computed_size[0]
Expand Down Expand Up @@ -178,17 +180,17 @@ def get_computed_content_height(self):

return self.get_computed_background_height() - self.padding_vertical

def get_computed_background_width(self):
def get_computed_background_width(self, apply_margin=False):
if self._width == SIZE_WRAP_CONTENT:
return self.get_computed_content_width() + self.padding_horizontal

return super().get_computed_background_width()
return super().get_computed_background_width(apply_margin)

def get_computed_background_height(self):
def get_computed_background_height(self, apply_margin=False):
if self._height == SIZE_WRAP_CONTENT:
return self.get_computed_content_height() + self.padding_vertical

return super().get_computed_background_width()
return super().get_computed_background_width(apply_margin)

def process_event(self, e: Event, offsetted_mouse_x: int, offsetted_mouse_y: int) -> bool:
if self.visibility == VISIBILITY_GONE or not self.enabled:
Expand Down Expand Up @@ -288,9 +290,9 @@ def render_content_surface(self):
continue

if self._orientation == ORIENTATION_HORIZONTAL:
next_children_x_off += view_size[0]
next_children_x_off += view_size[0] + view._margin_right
else:
next_children_y_off += view_size[1]
next_children_y_off += view_size[1] + view._margin_bottom


__all__ = 'LinearLayout', 'ORIENTATION_HORIZONTAL', 'ORIENTATION_VERTICAL'
4 changes: 3 additions & 1 deletion pygex/gui/textview.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pygex.gui.view import View, DEFAULT_SIZE, DEFAULT_PADDING, DEFAULT_POSITION, DEFAULT_GRAVITY
from pygex.gui.view import View, DEFAULT_SIZE, DEFAULT_PADDING, DEFAULT_POSITION, DEFAULT_GRAVITY, DEFAULT_MARGIN
from pygex.text import ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_BLOCK, DEFAULT_FONT_SIZE, TextRenderer
from pygex.color import TYPE_COLOR, COLOR_BLACK, COLOR_TRANSPARENT
from pygex.gui.drawable.drawable import Drawable
Expand All @@ -15,6 +15,7 @@ def __init__(
size: Sequence[int] = DEFAULT_SIZE,
pos: Sequence[float | int] = DEFAULT_POSITION,
padding: Sequence[int] = DEFAULT_PADDING,
margin: Sequence[int] = DEFAULT_MARGIN,
content_gravity=DEFAULT_GRAVITY,
text_align=ALIGN_LEFT,
text_line_spacing: float | int = 0,
Expand All @@ -29,6 +30,7 @@ def __init__(
size,
pos,
padding,
margin,
content_gravity,
background_drawable_or_color,

Expand Down
115 changes: 95 additions & 20 deletions pygex/gui/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

SIZE_MATCH_PARENT = -1

NO_PADDING = (0,) * 4
DEFAULT_MARGIN = NO_PADDING = (0,) * 4
DEFAULT_PADDING = (8,) * 4
DEFAULT_SIZE = (SIZE_WRAP_CONTENT, SIZE_WRAP_CONTENT)
DEFAULT_POSITION = (0,) * 2
Expand All @@ -42,12 +42,14 @@ def __init__(
size: Sequence[int],
pos: Sequence[float | int],
padding: Sequence[int],
margin: Sequence[int],
content_gravity: int,
background_drawable_or_color: Drawable | TYPE_COLOR,
prerender_during_initialization: bool
):
self._width, self._height = size
self._padding_left, self._padding_top, self._padding_right, self._padding_bottom = padding
self._margin_left, self._margin_top, self._margin_right, self._margin_bottom = margin

self._background_drawable_is_interaction_drawable = False
self._background_surface_buffer: SurfaceType | None = None
Expand Down Expand Up @@ -221,6 +223,75 @@ def padding_horizontal(self) -> int:
def padding_vertical(self) -> int:
return self._padding_top + self._padding_bottom


@property
def margin_left(self) -> int:
return self._margin_left

@margin_left.setter
def margin_left(self, new_value: int):
old_value = self._margin_left
self._margin_left = new_value

if new_value != old_value:
self.render_background_surface(force_render=True)

@property
def margin_top(self) -> int:
return self._margin_top

@margin_top.setter
def margin_top(self, new_value: int):
old_value = self._margin_top
self._margin_top = new_value

if new_value != old_value:
self.render_background_surface(force_render=True)

@property
def margin_right(self) -> int:
return self._margin_right

@margin_right.setter
def margin_right(self, new_value: int):
old_value = self._margin_right
self._margin_right = new_value

if new_value != old_value:
self.render_background_surface(force_render=True)

@property
def margin_bottom(self) -> int:
return self._margin_bottom

@margin_bottom.setter
def margin_bottom(self, new_value: int):
old_value = self._margin_bottom
self._margin_bottom = new_value

if new_value != old_value:
self.render_background_surface(force_render=True)

@property
def margin(self) -> tuple[int]:
return self._margin_left, self._margin_top, self._margin_right, self._margin_bottom

@margin.setter
def margin(self, new_value: Sequence[int]):
old_value = self._margin_left, self._margin_top, self._margin_right, self._margin_bottom
self._margin_left, self._margin_top, self._margin_right, self._margin_bottom = new_value

if new_value != old_value:
self.render_background_surface(force_render=True)

@property
def margin_horizontal(self) -> int:
return self._margin_left + self._margin_right

@property
def margin_vertical(self) -> int:
return self._margin_top + self._margin_bottom

@property
def is_clicked(self):
return self._interaction_state == INTERACTION_STATE_END_OF_INTERACTION
Expand All @@ -235,8 +306,8 @@ def buffered_content_surface(self) -> SurfaceType | None:

def get_bounds(self):
return Rect(
self.x,
self.y,
self.x + self._margin_left,
self.y + self._margin_top,
self.get_computed_background_width(),
self.get_computed_background_height(),
)
Expand All @@ -254,46 +325,49 @@ def get_computed_content_height(self):
def get_computed_content_size(self):
return self.get_computed_content_width(), self.get_computed_content_height()

def get_computed_background_width(self):
def get_computed_background_width(self, apply_margin=False):
if self._width == SIZE_MATCH_PARENT:
if self._parent is None or not isinstance(self._parent, View):
return pg_win_get_size()[0]
return pg_win_get_size()[0] - self.margin_horizontal

if self._parent._width == SIZE_WRAP_CONTENT:
# ATTENTION: if there is no such condition, there will be an infinite recursion
return self.min_width
return self.min_width + self.margin_horizontal

return self._parent.get_computed_background_width() - self._parent.padding_horizontal
return self._parent.get_computed_background_width() - self._parent.padding_horizontal \
- self.margin_horizontal

if self._width == SIZE_WRAP_CONTENT:
if self.buffered_content_surface is None:
return self.min_width
return self.min_width + self.margin_horizontal * apply_margin

return self.buffered_content_surface.get_width() + self.padding_horizontal
return self.buffered_content_surface.get_width() + self.padding_horizontal \
+ self.margin_horizontal * apply_margin

return self._width
return self._width + self.margin_horizontal * apply_margin

def get_computed_background_height(self):
def get_computed_background_height(self, apply_margin=False):
if self._height == SIZE_MATCH_PARENT:
if self._parent is None or not isinstance(self._parent, View):
return pg_win_get_size()[1]
return pg_win_get_size()[1] - self.margin_vertical

if self._parent._height == SIZE_WRAP_CONTENT:
# ATTENTION: if there is no such condition, there will be an infinite recursion
return self.min_height
return self.min_height + self.margin_vertical

return self._parent.get_computed_background_height() - self._parent.padding_vertical
return self._parent.get_computed_background_height() - self._parent.padding_vertical - self.margin_vertical

if self._height == SIZE_WRAP_CONTENT:
if self.buffered_content_surface is None:
return self.min_height
return self.min_height + self.margin_vertical * apply_margin

return self.buffered_content_surface.get_height() + self.padding_vertical
return self.buffered_content_surface.get_height() + self.padding_vertical \
+ self.margin_vertical * apply_margin

return self._height
return self._height + self.margin_vertical * apply_margin

def get_computed_background_size(self):
return self.get_computed_background_width(), self.get_computed_background_height()
def get_computed_background_size(self, apply_margin=False):
return self.get_computed_background_width(apply_margin), self.get_computed_background_height(apply_margin)

def get_background_drawable(self) -> Drawable | None:
return self._background_drawable
Expand Down Expand Up @@ -417,7 +491,7 @@ def render(self, surface: SurfaceType, x_off: float | int, y_off: float | int, p
if self.visibility != VISIBILITY_VISIBLE:
return

render_x, render_y = self.x + x_off, self.y + y_off
render_x, render_y = self.x + x_off + self._margin_left, self.y + y_off + self._margin_top

if self._background_drawable_is_interaction_drawable and self._background_drawable.is_need_to_be_rendered:
self._background_surface_buffer = self._background_drawable.render(self.get_computed_background_size())
Expand Down Expand Up @@ -469,6 +543,7 @@ def render(self, surface: SurfaceType, x_off: float | int, y_off: float | int, p

__all__ = (
'NO_PADDING',
'DEFAULT_MARGIN',
'DEFAULT_PADDING',
'DEFAULT_SIZE',
'DEFAULT_POSITION',
Expand Down

0 comments on commit c8b5914

Please sign in to comment.