Skip to content

Commit

Permalink
feat(graphics): ✨ Added a checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsunami014 (Max) authored and Tsunami014 (Max) committed Dec 15, 2024
1 parent 84f636d commit 530752a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
65 changes: 65 additions & 0 deletions BlazeSudio/graphics/GUI/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import pygame
from typing import Callable
from string import printable
from random import random
from BlazeSudio.graphics import mouse, options as GO
from BlazeSudio.graphics.GUI.base import Element, ReturnGroup, ReturnState
from BlazeSudio.graphics.GUI.theme import GLOBALTHEME
from BlazeSudio.graphics.GUI.events import Dropdown

__all__ = [
'Switch',
'Checkbox',
'InputBox',
'NumInputBox',
'Empty',
Expand Down Expand Up @@ -74,6 +76,69 @@ def set(self, newState):
"""Set the state of the switch (on or off)"""
self.state = newState

class Checkbox(Element):
type = GO.TCHECKBOX
def __init__(self, G, pos: GO.P___, size=40, thickness=5, check_size=15, radius=5, default=False):
"""
A checkbox that can be either checked or unchecked.
Args:
G (Graphic): The graphic screen to attach to.
pos (GO.P___): The position of this element in the screen.
size (int, optional): The size of this element. Defaults to 40.
thickness (int, optional): The thickness of the lines. Defaults to 5.
check_size (int, optional): The size of the check mark arrow. Defaults to 20.
radius (int, optional): The border radius of the drawn boxes. Defaults to 5.
default (bool, optional): Whether the checkbox is either on or off on creation. Defaults to False.
"""
self.thickness = thickness
self.BR = radius
self.Csize = check_size
self.rerandomise()
super().__init__(G, pos, (size, size))
self.state = default

def rerandomise(self):
self.randoms = [random()+1, random()+1, random()+3, random()*2+4]

def update(self, mousePos, events):
x, y = self.stackP()
if mousePos:
mcollides = pygame.Rect(x, y, *self.size).collidepoint(mousePos)
else:
mcollides = False
if not self.G.pause:
if mcollides:
if pygame.mouse.get_pressed()[0]:
mouse.Mouse.set(mouse.MouseState.CLICKING)
else:
mouse.Mouse.set(mouse.MouseState.HOVER)
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN and \
event.button == pygame.BUTTON_LEFT and mcollides:
self.state = not self.state
self.rerandomise()
return ReturnState.CALL

def draw(self):
x, y = self.stackP()
pygame.draw.rect(self.G.WIN, (125, 125, 125), pygame.Rect(x, y, *self.size), self.thickness, self.BR)
if self.state:
midp = (x+self.size[0]/2, y+self.size[1]-(self.size[1]/self.randoms[3]))
p1 = (midp[0]-self.Csize*self.randoms[0], midp[1]-self.Csize)
p2 = (midp[0]+self.Csize*self.randoms[1], midp[1]-self.Csize*self.randoms[2])
pygame.draw.line(self.G.WIN, GO.CGREEN, p1, midp, self.thickness+1)
pygame.draw.line(self.G.WIN, GO.CGREEN, midp, p2, self.thickness+1)
for p in (p1, midp, p2):
pygame.draw.circle(self.G.WIN, GO.CGREEN, p, self.thickness//2+1)

def get(self):
"""Get the state of the checkbox (on or off)"""
return self.state
def set(self, newState):
"""Set the state of the checkbox (on or off)"""
self.state = newState

# InputBoxes code modified from https://stackoverflow.com/questions/46390231/how-can-i-create-a-text-input-box-with-pygame

# TODO: A text cursor
Expand Down
15 changes: 9 additions & 6 deletions BlazeSudio/graphics/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import pygame.freetype
from string import printable

# TODO: Make these all enums

# TODO: Modify the __str__ and __repr__ of the class to a name
def Base(cls=None, default=True, str=True, addhash=True):
def wrap(clss):
Expand Down Expand Up @@ -407,12 +409,13 @@ def __repr__(self): return str(self)
TNUMBOX = T___(2, 'Numbox' )
TTEXTBOX = T___(3, 'Textbox' )
TSWITCH = T___(4, 'Switch' )
TFRAME = T___(5, 'Frame' )
TLAYOUT = T___(6, 'Layout' )
TSTATIC = T___(7, 'Static' )
TCOLOURPICK = T___(8, 'ColourPick')
TTOAST = T___(9, 'Toast' )
TEMPTY = T___(10, 'Empty' )
TCHECKBOX = T___(5, 'Checkbox' )
TFRAME = T___(6, 'Frame' )
TLAYOUT = T___(7, 'Layout' )
TSTATIC = T___(8, 'Static' )
TCOLOURPICK = T___(9, 'ColourPick')
TTOAST = T___(10, 'Toast' )
TEMPTY = T___(11, 'Empty' )


# Resizes
Expand Down
6 changes: 3 additions & 3 deletions demos.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ def _LoadUI(self): # Load the graphics in!
self['speshs'].append(L)
self.sw = GUI.Switch(L, L.LP)
L.grid = [
[GUI.Text(L, L.LP, 'HI'), GUI.Text(L, L.LP, 'HELLO'), GUI.Text(L, L.LP, 'BYE')],
[GUI.Text(L, L.LP, 'HEHE'), GUI.Text(L, L.LP, 'YES'), GUI.Text(L, L.LP, 'NO')],
[GUI.Text(L, L.LP, 'HAVE'), GUI.Text(L, L.LP, 'A'), GUI.Text(L, L.LP, 'NICEDAY')],
[GUI.Text(L, L.LP, 'HI'), None, GUI.Text(L, L.LP, 'BYE')],
[GUI.Text(L, L.LP, 'YES'), GUI.Checkbox(L, L.LP), GUI.Text(L, L.LP, 'NO')],
[GUI.Checkbox(L, L.LP, 20, check_size=10), GUI.Checkbox(L, L.LP, thickness=2), GUI.Checkbox(L, L.LP, check_size=40)],
[GUI.Button(L, L.LP, GO.CORANGE, 'Hello!'), None, self.sw]
]

Expand Down

0 comments on commit 530752a

Please sign in to comment.