Skip to content

Commit

Permalink
feat: making the themes flexible to configure
Browse files Browse the repository at this point in the history
  • Loading branch information
kerodekroma committed Oct 29, 2024
1 parent fe235a3 commit 17917fc
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 6 deletions.
File renamed without changes
File renamed without changes
Binary file added assets/custom_theme/one/handler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def __init__(self):
self.slider = py_singl_slider.PySinglSlider(20, 20, 0, 100, 10)
self.slider_init_value = py_singl_slider.PySinglSlider(20, 70, 0, 200, 15)
self.slider_two = py_singl_slider.PySinglSlider(20, 120, 0, 150, 10, 'two')
self.slider_custom_theme = py_singl_slider.PySinglSlider(20, 160, 0, 120, 10, 'one', 'assets/custom_theme')

#font
self.font = pygame.font.Font('assets/font/PixelSimpel.otf', 32)

Expand All @@ -26,6 +28,7 @@ async def render(self):
self.slider.listen_event(event)
self.slider_init_value.listen_event(event)
self.slider_two.listen_event(event)
self.slider_custom_theme.listen_event(event)

self.screen.fill((10, 220, 10))

Expand All @@ -50,6 +53,12 @@ async def render(self):
self.screen.blit(text_b, (slider_rect_two.width + 32, slider_rect_two.y - 9))
self.slider_two.render(self.screen)

# slider with custom theme
text_b = self.font.render("{0:.2f}".format( self.slider_custom_theme.value ), True, (10, 20, 200))
slider_rect_custom_theme = self.slider_custom_theme.get_rect()
self.screen.blit(text_b, (slider_rect_custom_theme.width + 32, slider_rect_custom_theme.y - 9))
self.slider_custom_theme.render(self.screen)

#update the display
pygame.display.flip()
# clock
Expand Down
19 changes: 13 additions & 6 deletions py_singl_slider/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import pygame
import os

def load_image(image_name, theme_name, theme_folder_path=None):
"""Helper function to load an image from the assets directory."""
image_path = os.path.join(os.path.dirname(__file__), f'theme/{theme_name}', image_name)
if theme_folder_path != None:
image_path = f'{theme_folder_path}/{theme_name}/{image_name}'
return pygame.image.load(image_path).convert_alpha()

class PySinglSlider:
def __init__(self, x=0, y=0, min_value=0, max_value=150, initial_value=0, theme_name= 'one', theme_path='assets/theme'):
def __init__(self, x=0, y=0, min_value=0, max_value=150, initial_value=0, theme_name= 'one', theme_path=None):
self.scale = 2
self.initial_value = initial_value
self.min_value = min_value
Expand All @@ -20,10 +28,9 @@ def __init__(self, x=0, y=0, min_value=0, max_value=150, initial_value=0, theme_
def get_rect(self):
return pygame.Rect(self.bar_x, self.bar_y, self.bg_bar_center.get_rect().width, self.bg_bar_center.get_rect().height)

def setup_assets(self, theme_folder_path, theme_name='one'):
theme_path = f'{theme_folder_path}/{theme_name}'
def setup_assets(self, theme_folder_path=None, theme_name='one'):
# handler
img_handler = pygame.image.load(f'{theme_path}/handler.png').convert_alpha()
img_handler = load_image('handler.png', theme_name, theme_folder_path)
self.bg_handler = pygame.Surface((img_handler.get_width() * self.scale, img_handler.get_height() * self.scale))

# handler rect
Expand All @@ -37,7 +44,7 @@ def setup_assets(self, theme_folder_path, theme_name='one'):

# bar center background
self.bar_width = self.max_value * self.scale
img_bar_center = pygame.image.load(f'{theme_path}/bar_center.png').convert_alpha()
img_bar_center = load_image('bar_center.png', theme_name, theme_folder_path)
self.bg_bar_center = pygame.Surface((self.bar_width + self.handler_rect.width, img_bar_center.get_height() * self.scale)).convert_alpha()
self.bar_height = img_bar_center.get_height() * self.scale

Expand All @@ -53,7 +60,7 @@ def setup_assets(self, theme_folder_path, theme_name='one'):

# bar corners
# LEFT
img_bar_corner = pygame.image.load(f'{theme_path}/bar_corner.png').convert_alpha()
img_bar_corner = load_image('bar_corner.png', theme_name, theme_folder_path)
self.bg_bar_corner = pygame.Surface((img_bar_corner.get_width() * self.scale, img_bar_corner.get_height() * self.scale)).convert_alpha()
scaled = pygame.transform.scale(img_bar_corner, (img_bar_corner.get_width() * self.scale, img_bar_corner.get_height() * self.scale))
self.bg_bar_corner.blit(scaled, (0, 0))
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added py_singl_slider/theme/two/bar_center.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added py_singl_slider/theme/two/bar_corner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes

0 comments on commit 17917fc

Please sign in to comment.