Skip to content

Commit

Permalink
Update drawable.py and interactiondrawable.py
Browse files Browse the repository at this point in the history
Code refactored
  • Loading branch information
TeaCondemns committed Feb 19, 2023
1 parent dce785c commit eeb9acf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ debug_textview = pygex.gui.TextView(


debug_button = pygex.gui.ButtonView('[!]', pos=(5, 5), render_content_during_initialization=False)
debug_button.get_background_drawable().get_content().set_border_radius(90)
debug_button.get_background_drawable().get_content().color = pygex.color.C_RED | 0xaa000000
debug_button.get_background_drawable().get_content_drawable().set_border_radius(90)
debug_button.get_background_drawable().get_content_drawable().color = pygex.color.C_RED | 0xaa000000
debug_button.set_hint(
'Close debug menu',
hint_gravity=pygex.gui.Hint.GRAVITY_LEFT | pygex.gui.Hint.GRAVITY_UNDER_CENTER
Expand Down Expand Up @@ -95,6 +95,8 @@ while True:
if debug_button.x == 5 and debug_textview.visibility == pygex.gui.VISIBILITY_VISIBLE:
debug_button.x += debug_textview.get_computed_background_width()

window.render_views()

if debug_button.is_clicked:
open_or_close_debug_menu()

Expand Down
2 changes: 1 addition & 1 deletion pygex/gui/buttonview.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def render(self, surface: SurfaceType):
if self.visibility != VISIBILITY_VISIBLE:
return

if self._background_drawable_is_interaction_drawable and self._background_drawable.is_need_to_be_rendered():
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())

super().render(surface)
Expand Down
15 changes: 9 additions & 6 deletions pygex/gui/drawable/drawable.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ def __init__(self, border_radius_or_radii: int | Sequence[int], border_width: in
if isinstance(border_radius_or_radii, int):
self.set_border_radius(border_radius_or_radii)
else:
self.set_radii(border_radius_or_radii)
self.radii = border_radius_or_radii

self.border_width = border_width
self.border_color = border_color

@property
def has_border_radii(self):
return (
self.border_top_left_radius,
Expand All @@ -23,21 +24,23 @@ def has_border_radii(self):
self.border_bottom_right_radius
) != (-1, -1, -1, -1)

def get_radii(self):
@property
def radii(self):
return (
self.border_top_left_radius,
self.border_top_right_radius,
self.border_bottom_left_radius,
self.border_bottom_right_radius
)

def set_radii(self, radii: Sequence[int]):
@radii.setter
def radii(self, new_radii: Sequence[int]):
(
self.border_top_left_radius,
self.border_top_right_radius,
self.border_bottom_left_radius,
self.border_bottom_right_radius
) = radii
) = new_radii

def set_border_radius(self, radius: int):
self.border_top_left_radius = radius
Expand Down Expand Up @@ -69,7 +72,7 @@ def render(self, size: Sequence[int]) -> SurfaceType | None:
for layer in self.layers[1:]:
output_surface.blit(layer.render(size), (0, 0))

if self.has_border_radii():
if self.has_border_radii:
output_surface = round_corners(
output_surface,
self.border_top_left_radius,
Expand Down Expand Up @@ -154,7 +157,7 @@ def __init__(
def render(self, size: Sequence[int]) -> SurfaceType:
output_surface = gradient(size, self.colors, self.is_vertical)

if self.has_border_radii():
if self.has_border_radii:
output_surface = round_corners(
output_surface,
self.border_top_left_radius,
Expand Down
25 changes: 14 additions & 11 deletions pygex/gui/drawable/interactiondrawable.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(
if content is None:
border_radius_or_radii = -1
else:
border_radius_or_radii = content.get_radii()
border_radius_or_radii = content.radii

super().__init__(border_radius_or_radii, border_width, border_color)

Expand All @@ -43,23 +43,26 @@ def __init__(
self.effect_alpha_value_difference_for_start_decreasing_effect_border_alpha_value = 80
self.effect_alpha_decreasing_per_frame = 3

@property
def is_need_to_be_rendered(self):
return self._is_in_process

def set_effect_color(self, effect_color: COLOR_TYPE):
self._effect_color = effect_color
self._effect_color_rgba = ahex_to_rgba(color_as_int(effect_color))

def get_effect_color(self):
@property
def effect_color(self):
return self._effect_color

def set_content(self, content: Drawable):
self._content_drawable = content
self._content_buffered_size = (-1, -1)
@effect_color.setter
def effect_color(self, new_effect_color: COLOR_TYPE):
self._effect_color = new_effect_color
self._effect_color_rgba = ahex_to_rgba(color_as_int(new_effect_color))

def get_content(self):
def get_content_drawable(self):
return self._content_drawable

def set_content_drawable(self, content: Drawable):
self._content_drawable = content
self._content_buffered_size = (-1, -1)

def set_interaction_status(self, interaction_status: int, animate=True):
if interaction_status == IS_NO_INTERACTION and self._interaction_status != IS_NO_INTERACTION and animate:
self._is_in_process = True
Expand Down Expand Up @@ -120,7 +123,7 @@ def render(self, size: Sequence[int]) -> SurfaceType | None:
output_surface.blit(effect_surface, (0, 0))

# STEP 3: rounding and bordering the output surface
if self.has_border_radii():
if self.has_border_radii:
output_surface = round_corners(
output_surface,
self.border_top_left_radius,
Expand Down

0 comments on commit eeb9acf

Please sign in to comment.