Skip to content

Commit

Permalink
More components (#23)
Browse files Browse the repository at this point in the history
* added Button and test, changed stuff

Implemented button.py and wrote a test, moved pygame initialization stuff to guipy/__init__.py, added default font util, added optional function to textbox, changed sub_coords() to sub_vector() (and add_vector()), and added a black border to @Zjjc123's slider_test.py

* added Switch and test, changed stuff

Implemented switch.py and test, refactored button, added _render() to some components to contain the visual design, added TRANSPARENT color.

* added AutoPlot (in plot.py) and test

Implemented AutoPlot and test, fixed some range issues in Plot, slight change to translate(), commented switch.py

* comments on AutoPlot

also added to gitignore to be sneaky

* DOES NOT WORK but what I'm going for

* what he was going for

and some other stuff that I don't feel like listing

* Update

* added Dropdown component and test

Dropdown is in menu.py

* various updates

Mainly combined render() and update() for all components, and added back get_surf() method.

* plot updates

Plots now use float_format() from utils.py to format the numbers on the axes. Will probably change later to shorten numbers so they don't run off the screen.

* Documentation and stuff

also made some changes to ranging in plot.py

* Update README.md

* Update README.md

Co-authored-by: Jason Zhang <zjjc123@hotmail.com>
  • Loading branch information
CaseyHackerMan and Zjjc123 authored Sep 15, 2022
1 parent dcfe71f commit f6a81da
Show file tree
Hide file tree
Showing 19 changed files with 961 additions and 212 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,6 @@ dmypy.json

# DS_Store
.DS_Store

# secret tests
tests/test.py
48 changes: 22 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ poetry add guipylib


```python
import sys
import colorsys
import pygame

from guipy.manager import GUIManager
from guipy.components.slider import Slider

import pygame

pygame.init()
from guipy.manager import GUIManager
from guipy.utils import *

winW = 1280
winH = 720
Expand All @@ -38,37 +35,36 @@ root = pygame.display.set_mode((winW, winH))

man = GUIManager()

mySlider = Slider(height=50, width=500, thickness=5,
radius=12, initial_val=.4)
mySlider2 = Slider(height=50, width=500, thickness=5,
radius=12, initial_val=0)
mySlider3 = Slider(height=50, width=500, thickness=5,
radius=12, initial_val=.5)
mySlider4 = Slider(height=50, width=500, thickness=5,
radius=12, initial_val=.5)
mySlider = Slider(height=50, width=500, thickness=5, radius=12, initial_val=0.4)
mySlider2 = Slider(height=50, width=500, thickness=5, radius=12, initial_val=0)
mySlider3 = Slider(height=50, width=500, thickness=5, radius=12, initial_val=0.5)
mySlider4 = Slider(height=50, width=500, thickness=5, radius=12, initial_val=0.5)

man.add(mySlider, (0, 25))
man.add(mySlider2, (0, 75))
man.add(mySlider3, (0, 125))
man.add(mySlider4, (0, 175))

while True:
for event in pygame.event.get():
running = True
while running:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
sys.exit()

root.fill((50, 50, 50))
running = False

color = tuple(i * 255 for i in colorsys.hls_to_rgb(mySlider2.get_val(),
mySlider3.get_val(), mySlider4.get_val()))
root.fill(DARK_GREY)

pygame.draw.circle(root, color, (winW/2, winH/2),
10 + mySlider.get_val() * 100)
color = tuple(
i * 255
for i in colorsys.hls_to_rgb(mySlider2.val, mySlider3.val, mySlider4.val)
)
center = (winW // 2, winH // 2)
r = 10 + mySlider.val * 100
pygame.draw.circle(root, color, center, r)
pygame.draw.circle(root, BLACK, center, r, 3)

man.draw(root)
man.update(pygame.mouse.get_pos())
man.update(pygame.mouse.get_pos(), events, root)
pygame.display.update()

```

## Documentation
Expand Down
4 changes: 4 additions & 0 deletions guipy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import pygame

pygame.init()
pygame.font.init()
33 changes: 29 additions & 4 deletions guipy/components/_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,41 @@

class Component:
def __init__(self, width, height):
"""
Component init. Should create a surface for the component
"""
self.width = width
self.height = height
self.root = pygame.Surface((width, height))

self.root = pygame.Surface((width, height)).convert_alpha()
def _draw(self):
"""
Draws the component
"""

def get_surf(self):
"""
Returns the surface of the component
:return: Surface
"""
return self.root

def draw(self):
pass
def _collide(self, rel_mouse):
"""
Helper method to detect collisions
:param rel_mouse: Mouse position relative to the component
:return: True if the mouse is inside the component
"""
return 0 <= rel_mouse[0] < self.width and 0 <= rel_mouse[1] < self.height

def update(self, rel_mouse, events):
pass
"""
Update the component. Gets the surface ready to be used
:param rel_mouse: Mouse position relative to the component
:param events: Pygame event list
"""
self._draw()
125 changes: 125 additions & 0 deletions guipy/components/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import pygame
from guipy.components._component import Component
from guipy.utils import *


class Button(Component):
"""
Button component
"""

def __init__(self, width=None, height=None, font=None, text="Bruh"):
"""
Button init
:param width: button width in pixels, defaults to fit text
:param height: button height in pixels, defaults to fit text
:param font: pygame Font object to use
:param text: text to display on the button
"""
if font == None:
self.font = get_default_font()
else:
self.font = font

min_w, min_h = self.font.size(text)
if width == None:
width = min_w + 6
if height == None:
height = min_h

self.width = width
self.height = height
self.off_surf = pygame.Surface((self.width, self.height)).convert_alpha()
self.on_surf = pygame.Surface((self.width, self.height)).convert_alpha()

self.pressed = False

self.set_text(text)

self.cb = None

def get_val(self):
"""
Get current state
:return: State as a boolean
"""
return self.pressed

def set_callback(self, cb):
"""
Set the callback to be run when the button is released
:param cb: Callback function
:return: self
"""
self.cb = cb
return self

def _draw(self):
text_surf = self.font.render(self.text, True, BLACK)

x, y = sub_vector((self.width, self.height), text_surf.get_size())
pos = (x // 2, y // 2)

self.off_surf.fill(TRANSPARENT)
surf = self.off_surf
pygame.draw.rect(surf, LIGHT_GREY, surf.get_rect())
pygame.draw.rect(surf, DARK_GREY, surf.get_rect(), 1)
surf.blit(text_surf, pos)

self.on_surf.fill(TRANSPARENT)
surf = self.on_surf
pygame.draw.rect(surf, GREY, surf.get_rect())
pygame.draw.rect(surf, DARK_GREY, surf.get_rect(), 1)
surf.blit(text_surf, pos)

def set_text(self, text):
"""
Sets the text on the button and redraws the surface
:param text:
:return: self
"""
self.text = text
self._draw()
return self

def get_surf(self):
"""
Get the button's surface
:return: Surface
"""
if self.pressed:
return self.on_surf
else:
return self.off_surf

def update(self, rel_mouse, events):
"""
Update the button
:param rel_mouse: Relative mouse position
:param events: Pygame Event list
"""
on_click = False
on_release = False
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
on_click = True
if event.type == pygame.MOUSEBUTTONUP:
on_release = True

in_comp = self._collide(rel_mouse)

if on_click and in_comp:
self.pressed = True

if on_release and self.pressed:
if in_comp and self.cb != None:
self.cb(self)
self.pressed = False
Loading

0 comments on commit f6a81da

Please sign in to comment.