Skip to content

Commit

Permalink
Merge pull request #3 from Times0/wip
Browse files Browse the repository at this point in the history
fixed fonts issues with pygame
  • Loading branch information
Times0 authored Dec 15, 2023
2 parents b4eb9f5 + dd77124 commit 662a95c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "PygameUIKit"
version = "0.0.6"
version = "0.0.7"
authors = [
{ name="Times0", email="[email protected]" },
]
Expand Down
3 changes: 2 additions & 1 deletion src/PygameUIKit/barchart.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self, values: list[int],
labels: list[str] = None,
max_value=None,
ui_group=None,
font=pg.font.SysFont("Arial", 15),
labels_color=pg.Color("black")):
super().__init__(ui_group=ui_group)
self.values = values[:]
Expand All @@ -30,7 +31,7 @@ def __init__(self, values: list[int],
self.labels = labels
self.values_displayed = self.values[:]


self.font = font
self.colors = sns.color_palette("husl", len(self.values))
self.colors = color_from_float(self.colors)

Expand Down
15 changes: 9 additions & 6 deletions src/PygameUIKit/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
from . import utilis

pg.font.init()
FONT = pg.font.Font(None, 25)


# Instanciating a default font here leads to a low level pygame error when restarting an app with a font instanciated in
# the previous run


class EasyButton(EasyObject):
Expand Down Expand Up @@ -120,7 +123,8 @@ def draw(self, screen, x, y):


class ButtonImageText(ButtonImage):
def __init__(self, image, onclick_f, text, text_color=Color("white"), font=FONT, image_hover=None,
def __init__(self, image, onclick_f, text, text_color=Color("white"), font=pg.font.SysFont("Arial", 15),
image_hover=None,
text_offset=(0, 0)):
super().__init__(image, onclick_f, hover_image=image_hover)
self.text = text
Expand Down Expand Up @@ -217,8 +221,8 @@ def __init__(self,
text="",
onclick_f=None,
rect_color=Color("black"),
font=FONT,
border_radius=0,
font=None,
font_color=None,
outline_color=None,
fixed_width=None,
Expand All @@ -238,8 +242,7 @@ def __init__(self,
h = self.text_surface.get_height() + 20

if fixed_width:
w = fixed_width
self.fixed_width = w
self.fixed_width = fixed_width
else:
self.fixed_width = None
super().__init__(w, h, rect_color, onclick_f, border_radius=border_radius, ui_group=ui_group,
Expand Down Expand Up @@ -278,7 +281,7 @@ def __init__(self, *, rect_color=(0, 0, 0),
text_during="",
text_after="",
border_radius=0,
font=FONT,
font=pg.font.SysFont("Arial", 15),
text_color=(255, 255, 255)):
self.text_before = text_before
self.text_during = text_during
Expand Down
8 changes: 3 additions & 5 deletions src/PygameUIKit/dropdown.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os

import pygame.draw
from pygame import Color
import pygame as pg

Expand All @@ -18,8 +16,9 @@


class ComboBox(ButtonText):
def __init__(self, elements: list[str], ui_group=None, font_color=Color("black")):
super().__init__(ui_group=ui_group, text=elements[0], onclick_f=self.open, border_radius=5, fixed_width=200,
def __init__(self, elements: list[str], ui_group=None, font=None, font_color=Color("black")):
super().__init__(ui_group=ui_group, text=elements[0],
onclick_f=self.open, border_radius=5, fixed_width=200, font=font,
font_color=best_contrast_color(font_color))
self.text = elements[0]
self.rect.height = self.text_rect.height + PADDING * 2
Expand Down Expand Up @@ -91,7 +90,6 @@ def change_selected(self, index):
self.actions[index]()
self.text = self.elements[self.selected_index]


def open(self):
self.is_open = not self.is_open
self._render_elements()
Expand Down
6 changes: 4 additions & 2 deletions src/PygameUIKit/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@


class Slider(EasyObject):
def __init__(self, min, max, step, show_value=False, font_color=Color("black"), ui_group=None):
def __init__(self, min, max, step, show_value=False, font=pygame.font.SysFont("Arial", 15, bold=True),
font_color=Color("black"), ui_group=None):
super().__init__(ui_group=ui_group)
self.min = min
self.max = max
Expand All @@ -22,6 +23,7 @@ def __init__(self, min, max, step, show_value=False, font_color=Color("black"),
self.circle_radius = 10
self.rect = pygame.Rect(0, 0, 100, self.circle_radius / 2)
self.dragging = False
self.font = font
self.font_color = font_color

self.on_change = lambda: None
Expand Down Expand Up @@ -97,4 +99,4 @@ def on_hover(self):

def on_unhover(self):
self.hovered = False
self.circle_radius = 10
self.circle_radius = 10
8 changes: 3 additions & 5 deletions src/PygameUIKit/super_object.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import pygame as pg
import pygame.font
from pygame import Color

pg.font.init()
FONT = pg.font.SysFont("Arial", 20)
pygame.font.init()


class EasyObject:
def __init__(self, *, ui_group=None, font=FONT):
def __init__(self, *, ui_group=None):
if ui_group:
ui_group.add(self)
self.font = font
self.x = 0
self.y = 0
self.w = 0
self.h = 0
self.rect = pg.Rect(self.x, self.y, self.w, self.h)

self.hovered = False

def update_pos(self, x, y):
Expand Down
3 changes: 1 addition & 2 deletions src/PygameUIKit/text_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
pg.font.init()
COLOR_INACTIVE = pg.Color('lightskyblue3')
COLOR_ACTIVE = pg.Color('dodgerblue2')
FONT = pg.font.SysFont('Arial', 20)
TIME_OUT_FIRST = 500
TIME_OUT = 40

Expand All @@ -19,7 +18,7 @@ def is_char(unicode):
class InputBox(EasyObject):
def __init__(self, *,
text="",
font: pg.font.Font = FONT,
font: pg.font.Font = None,
fixed_width: int = None,
text_color=pg.Color('black'),
border_radius=0,
Expand Down
10 changes: 6 additions & 4 deletions src/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ def __init__(self):
self.clock = pygame.time.Clock()

self.easy_objects = Group()
self.text_input = text_input.InputBox(fixed_width=200, border_radius=2, ui_group=self.easy_objects)
self.text_input = text_input.InputBox(fixed_width=200, border_radius=2, ui_group=self.easy_objects,font=def_font)
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 = slider.Slider(0, 100, 1, show_value=True, ui_group=self.easy_objects,font=def_font)
self.slider.connect(self.change_values)

self.dropdown = dropdown.ComboBox(["Hello", "World", "And", "You"], ui_group=self.easy_objects)
self.dropdown = dropdown.ComboBox(["Hello", "World", "And", "You"], ui_group=self.easy_objects,font=def_font)

data = [10, 20, 30, 40, 50]
self.chart = BarChart(data, ui_group=self.easy_objects, max_value=100)
self.chart = BarChart(data, ui_group=self.easy_objects, max_value=100,font=def_font)

for i in range(4):
self.dropdown.add_action(i, lambda i=i: self.text_input.set_text(self.dropdown.elements[i]))
Expand Down Expand Up @@ -97,5 +97,7 @@ def do_nothing():

if __name__ == '__main__':
pygame.init()
def_font = pygame.font.SysFont("Arial", 20)

Demo().run()
pygame.quit()

0 comments on commit 662a95c

Please sign in to comment.