Skip to content

Commit

Permalink
Merge pull request #1 from Times0/wip
Browse files Browse the repository at this point in the history
Wip
  • Loading branch information
Times0 authored Dec 13, 2023
2 parents 7b29fa0 + 9e4b345 commit ef45466
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
python -m twine upload --skip-existing --verbose --repository pypi dist/*
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} # Set this secret in your repository settings
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
21 changes: 20 additions & 1 deletion src/PygameUIKit/button.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import Enum

import pygame as pg
from pygame import Rect
from pygame.color import Color
Expand Down Expand Up @@ -60,6 +62,10 @@ def _on_click(self):
pg.mouse.set_system_cursor(pg.SYSTEM_CURSOR_ARROW)
self.onclick_f()

def connect(self, func, when="on_click"):
if when == "on_click":
self.onclick_f = func


class ButtonRect(EasyButton):
def __init__(self, w, h, color, onclick_f, outline_color=None, border_radius=0, ui_group=None):
Expand Down Expand Up @@ -200,6 +206,12 @@ def success(self):
self.isSucces = True


class TextAlignment(Enum):
LEFT = 1
RIGHT = 2
CENTER = 3


class ButtonText(ButtonRect):
def __init__(self,
text="",
Expand All @@ -210,7 +222,9 @@ def __init__(self,
font_color=None,
outline_color=None,
fixed_width=None,
text_align=TextAlignment.LEFT,
ui_group=None):
self.text_align = text_align
self.text = text
if font_color is None:
self.font_color = utilis.best_contrast_color(rect_color)
Expand Down Expand Up @@ -249,7 +263,12 @@ def change_text(self, new_text):

def draw(self, screen, x, y):
super().draw(screen, x, y)
screen.blit(self.text_surface, (x + 10, y + 10))
if self.text_align == TextAlignment.LEFT:
screen.blit(self.text_surface, (x + 10, y + 10))
elif self.text_align == TextAlignment.RIGHT:
screen.blit(self.text_surface, (x + self.rect.w - self.text_surface.get_width() - 10, y + 10))
elif self.text_align == TextAlignment.CENTER:
screen.blit(self.text_surface, (x + self.rect.w // 2 - self.text_surface.get_width() // 2, y + 10))


class ButtonThreadText(ButtonRect):
Expand Down
28 changes: 21 additions & 7 deletions src/PygameUIKit/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@


class Slider(EasyObject):

def __init__(self, min, max, step, show_value=False, ui_group=None):
def __init__(self, min, max, step, show_value=False, font_color=Color("black"), ui_group=None):
super().__init__(ui_group=ui_group)
self.min = min
self.max = max
Expand All @@ -23,17 +22,23 @@ def __init__(self, min, max, step, show_value=False, ui_group=None):
self.circle_radius = 10
self.rect = pygame.Rect(0, 0, 100, self.circle_radius / 2)
self.dragging = False
self.font_color = font_color

self.on_change = lambda: None

def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.inflate(self.circle_radius+10, self.circle_radius+10).collidepoint(event.pos):
if self.rect.inflate(self.circle_radius + 10, self.circle_radius + 10).collidepoint(event.pos):
self.dragging = True
elif event.type == pygame.MOUSEBUTTONUP:
self.dragging = False

elif event.type == pygame.MOUSEMOTION:
if not self.hovered and self.rect.inflate(self.circle_radius + 10, self.circle_radius + 10).collidepoint(
event.pos):
self.on_hover()
elif self.hovered and not self.rect.inflate(self.circle_radius + 10,
self.circle_radius + 10).collidepoint(event.pos):
self.on_unhover()
if self.dragging:
x, y = event.pos
v = self.min + (x - self.rect.x) / self.rect.w * (self.max - self.min)
Expand Down Expand Up @@ -62,15 +67,16 @@ def draw_circle(self, win):
value_pos = self.rect.x + self.rect.w * (self.current_value / self.max)
y = self.rect.y + self.rect.h // 2

pygame.draw.circle(win, COLOR_CIRCLE1, (value_pos, y), self.circle_radius)
pygame.draw.circle(win, COLOR_CIRCLE2, (value_pos, y), self.circle_radius - 2)
rad_size = self.circle_radius if not self.dragging else 15
pygame.draw.circle(win, COLOR_CIRCLE1, (value_pos, y), rad_size)
pygame.draw.circle(win, COLOR_CIRCLE2, (value_pos, y), rad_size - 2)

def draw(self, win, x, y, w, h):
self.rect = pygame.Rect(x, y, w, h)
self.draw_bar(win)
self.draw_circle(win)
if self.show_value:
text = self.font.render(str(self.current_value), True, Color("black"))
text = self.font.render(str(self.current_value), True, self.font_color)
win.blit(text, (x + w + 10, y + h // 2 - text.get_height() // 2))

def get_value(self):
Expand All @@ -84,3 +90,11 @@ def connect(self, f, when="on_change", ):
"""
if when == "on_change":
self.on_change = f

def on_hover(self):
self.hovered = True
self.circle_radius = 12

def on_unhover(self):
self.hovered = False
self.circle_radius = 10
2 changes: 2 additions & 0 deletions src/PygameUIKit/super_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def __init__(self, *, ui_group=None, font=FONT):
self.h = 0
self.rect = pg.Rect(self.x, self.y, self.w, self.h)

self.hovered = False

def update_pos(self, x, y):
""" This is used when an object is on a surface. Its position relative to the surface might be (0,0)
but its position relative to the screen is not. This function updates the position of the object
Expand Down
11 changes: 9 additions & 2 deletions src/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def __init__(self):
self.btn_pause = button.ButtonTwoStates(img_play, img_stop, do_nothing, ui_group=self.easy_objects)
self.btn_png = button.ButtonPngIcon(img_play, self.change_values, inflate=10, ui_group=self.easy_objects)

self.btn_pause.connect(self.toggle_dance)

self.slider = slider.Slider(0, 100, 1, show_value=True, ui_group=self.easy_objects)
self.slider.connect(self.change_values)

Expand All @@ -45,6 +47,8 @@ def __init__(self):
for i in range(4):
self.dropdown.add_action(i, lambda i=i: self.text_input.set_text(self.dropdown.elements[i]))

self.dancing = False

def run(self):
while not self.done:
dt = self.clock.tick(60) / 1000
Expand All @@ -65,7 +69,8 @@ def events(self):

def update(self, dt):
self.easy_objects.update(dt)

if self.dancing:
self.change_values()
def draw(self, win):
W, H = self.screen.get_size()
win.fill(Color(224, 224, 224))
Expand All @@ -80,9 +85,11 @@ def draw(self, win):
def change_values(self):
i = random.randint(0, len(self.chart.values) - 1)
new_value = random.randint(0, 100)
print(f"changing {i} to {new_value}")
self.chart.change_value(i, new_value)

def toggle_dance(self):
self.dancing = not self.dancing


def do_nothing():
pass
Expand Down

0 comments on commit ef45466

Please sign in to comment.