Compare commits

..

No commits in common. "2ab5e569690923390e18012d7d4ce6d05cc1fed2" and "e867318c7f8752f7880e7116ffc925e4f11b6b5c" have entirely different histories.

2 changed files with 97 additions and 148 deletions

199
pg.py
View file

@ -3,133 +3,128 @@
import pygame
import text
ignore_errors = False
buttons = {
"popup_none": []
}
mouse_button_hold = False # Variable is True, while a mouse button is down to prevent from clicking multiple buttons at a time.
log = text.Log()
buttonlist = False
buttons = []
# colors
gray = (40, 40, 40)
white = (250, 250, 250)
default_font = pygame.font.Font(pygame.font.get_default_font(), 16)
class Hover:
def __init__(self, function=None, ca=None, position: tuple=None, size: tuple=None):
self.function = function
self.ca = ca
self.x, self.y = position
self.w, self.h = size
self.active = True
def check(self, mouse_pos: tuple):
if self.active:
mx, my = mouse_pos
if not self.ca is None:
if self.ca.position((mx, my)):
if self.function is None:
return True
else:
self.function()
return True
else:
x = self.x
y = self.y
w = self.w
h = self.h
if mx >= x and mx <= x + w and my >= y and my <= y + h:
if self.function is None:
return True
else:
self.function()
return True
return False
class NotEnoughInformationError(Exception):
def __init__(self, error):
super().__init__(error)
log.write(error, "error")
class Button:
def __init__(self, function=None, ca=None, position: tuple=None, size: tuple=None, key: int=0): # ca = collide able, key: 0 = left 1 = mouse wheel pressed 2 = right
def __init__(self, position: tuple, size: tuple, function, page: str, mouse_button: str="left", button_type: str="click"):
self.x, self.y = position
self.width, self.height = size
self.function = function
self.active = True
self.ca = ca
self.key = key
self.mouse_button = mouse_button
self.type = button_type
self.hover = Hover(function, ca, position, size)
if page not in buttons:
buttons[page] = []
if buttonlist:
buttons.append(self)
buttons[page].append(self)
def check(self, mouse_pos: tuple, pressed): # check if the button is pressed
if self.active:
if pressed[self.key]:
return self.hover.check(mouse_pos)
def check(self, mouse_pos: tuple): # check if a button is pressed
x = self.x
y = self.y
return False
w = self.width
h = self.height
mx, my = mouse_pos
if mx >= x and mx <= x + w and my >= y and my <= y + h:
self.function()
class TextButton:
def __init__(self, text: str, function=None, key: int=0, text_color: tuple=white, bg_color: tuple=gray, font: pygame.font.Font=default_font, padding: int=8):
self.text = text
class ButtonFromCollideable:
def __init__(self, rect: pygame.Rect, function, page: str, mouse_button: str="left", button_type: str="click"):
self.rect = rect
self.function = function
self.text_color = text_color
self.bg_color = bg_color
self.font = font
self.padding = padding
self.page = page
self.mouse_button = mouse_button
self.button_type = button_type
self.text_object = self.generate_text(text)
self.background = self.generate_background()
if page not in buttons:
buttons[page] = []
self.button = Button(function, self.background, key=key)
buttons[page].append(self)
def generate_text(self, text):
self.text = text
def check(self, mouse_pos: tuple):
mx, my = mouse_pos
return self.font.render(text, True, self.text_color)
def generate_background(self):
w, h = self.text_object.get_size()
w, h = w + self.padding * 2, h + self.padding * 2
return pygame.Surface((w, h))
def blit(self, surface: pygame.Surface, pos: tuple):
x, y = pos
surface.blit(self.background, pos)
surface.blit(self.text_object, (x + self.padding, y + self.padding))
def check(self, mouse_pos, pressed):
return self.button.check(mouse_pos, pressed)
if self.rect.collidepoint((mx, my)):
self.function()
# class HoverTitle: # coming soon
# def __init__(self, position: tuple=None, size: tuple=None, page: str=None, popup: str = None, collideable=None, delay: int=20):
# self.x, self.y = position
# self.width, self.height = size
# self.page = page
# self.popup = popup
# self.collideable = collideable
# self.delay = delay
class HoverTitle:
def __init__(self, position: tuple=None, size: tuple=None, page: str=None, popup: str = None, collideable=None, delay: int=20):
if position is None or size is None and collideable is None:
error = "You have to either pass position and size as argument or something collideable."
if ignore_errors:
log.write(error, "error")
else:
raise NotEnoughInformationError(error)
self.x, self.y = position # Make variables class wide
self.width, self.height = size
self.page = page
self.popup = popup
self.collideable = collideable
self.delay = delay
def bt_from_ca(ca, function): # bt_from_ca = button from collide able
return Button(function, ca)
def check_button(button: Button, pressed: tuple, mouse_button: str, mouse_position: tuple):
mx, my = mouse_position # Make variable names shorter
button_name_to_int = { # Convert button name to button number for better understanding.
"left": 0,
"middle": 1,
"right": 2
}
if pressed[button_name_to_int[mouse_button]]: # If the selected mouse button is pressed
if button.mouse_button == mouse_button: # If the button can be clicked with the selected mouse button
if button.type == "click": # If the click has to start on the button
if not mouse_button_hold: # If the click just started
button.check((mx, my)) # Check if the button is clicked.
elif button.type == "press": # If the click can start everywhere outside the button.
button.check((mx, my)) # Check if the button is pressed.
def bt_from_cords(function, position: tuple, size: tuple):
return Button(function, position=position, size=size)
def check_buttons(mouse_pos: tuple, mouse_buttons, page: str, popups: list=[]):
global mouse_button_hold # Make mouse_button_hold global to change it script wide.
pressed = mouse_buttons # Make variable names shorter
for button in buttons[page]: # Check for all buttons that are on the current page.
check_button(button, pressed, "left", mouse_pos) # Check buttons that can be clicked with the left mouse button.
check_button(button, pressed, "middle", mouse_pos) # Check buttons that can be clicked with the middle mouse button.
check_button(button, pressed, "right", mouse_pos) # Check buttons that can be clicked with the right mouse button.
for popup in popups: # Check for all popups
for button in buttons[popup]: # Check for all buttons that are on the popup.
check_button(button, pressed, "left", mouse_pos) # Check buttons that can be clicked with the left mouse button.
check_button(button, pressed, "middle", mouse_pos) # Check buttons that can be clicked with the middle mouse button.
check_button(button, pressed, "right", mouse_pos) # Check buttons that can be clicked with the right mouse button.
if pressed[0] or pressed[1] or pressed[2]: # If the left, middle or right mouse button is pressed.
mouse_button_hold = True # Buttonpress has already begun.
def check_buttons(mouse_pos, pressed):
for button in buttons:
button.check(mouse_pos, pressed)
def example():
ht = HoverTitle()
if __name__ == "__main__":
example()

View file

@ -1,46 +0,0 @@
#!/usr/bin/python3
class Settings:
def __init__(self, path: str=None):
self.path = path
if path is None:
self.settings = {"abc" : "abftdc"}
else:
self.load_from_file(path)
def load_from_file(self, path):
file = open(path, "r")
self.settings = eval(file.read())
file.close()
def save_settings(self):
file = open(self.path, "w")
new_settings_str = "{\n"
for key in self.settings.keys:
new_settings_str += "\t" + repr(key) + ": " + repr(self.settings[key]) + ",\n"
new_settings_str = new_settings_str[:-2] + "\n}"
file.write(new_settings_str)
file.close()
def __repr__(self):
return repr(self.settings)
def __getitem__(self, key):
return self.settings[key]
def __setitem__(self, key, value):
self.settings[key] = value
def __delitem__(self, key):
self.settings.pop(key)
def __iter__(self):
return self.settings
def __len__(self):
return len(self.settings)