Got buttons working.

This commit is contained in:
The Wobbler 2023-08-25 16:15:01 +02:00
parent b35fd9fa7e
commit e867318c7f
3 changed files with 277 additions and 20 deletions

135
buntcheck.py Normal file
View file

@ -0,0 +1,135 @@
#!/usr/bin/python3
seven_row = [] # put the row of 7 in a list.
for number in range(15, 256, 6):
seven_row.append(number)
color_names = [
"black",
"orange",
"dark_green",
"dark_yellow",
"dark_blue",
"purple",
"dark_turquoise",
"light_gray",
"gray",
"red",
"green",
"yellow",
"blue",
"pink",
"turquoise",
"white"
]
color_ansi = {
"reset": "\033[0m",
"rs": "\033[0m",
"bold": "\033[1m",
"italic": "\033[3m",
"underline": "\033[4m",
"slow_blink": "\033[5m",
"rapid_blink": "\033[6m"
}
for color in range(0, 16):
color_ansi[color_names[color]] = f"\033[38;5;{color}m"
reset = color_ansi["reset"]
def get_text_colors(only_default: bool=False):
color_codes = []
for color in range(0, 16):
color_codes.append(f"\033[38;5;{color}m")
if only_default:
return color_codes
else:
for color in range(16, 256):
color_codes.append(f"\033[38;5;{color}m")
return color_codes
def get_background_colors(only_default: bool = False):
color_codes = []
for color in range(0, 16):
color_codes.append(f"\033[48;5;{color}m")
if only_default:
return color_codes
else:
for color in range(16, 256):
color_codes.append(f"\033[48;5;{color}m")
return color_codes
# Font effects
def print_font_effects():
print("Font effects")
print("reset: \\033[0m")
print(color_ansi["bold"] + "Bold: \\033[1m" + reset)
print(color_ansi["italic"] + "Italic: \\033[3m" + reset)
print(color_ansi["underline"] + "Underline: \\033[4m" + reset)
print(color_ansi["slow_blink"] + "Slow blink: \\033[5m" + reset)
print(color_ansi["rapid_blink"] + "Rapid blink: \\033[6m" + reset)
# Text colors
def print_text_colors():
print("Text")
print("Linux, BSD, div. Unixe, Apple & Windows ANSII 16 Colors:")
color_codes = ""
for color in range(0, 16):
color_codes += f"\033[38;5;{color}m\\033[38;5;{color}m "
color_codes += " " * (1 - (len(str(color)) - 1))
if color == 7:
color_codes += f"{reset}\n"
print(color_codes, reset)
print("\nLinux, BSD, div. Unixe XTERM 256 Colors:")
color_codes = ""
for color in range(16, 256):
color_codes += f"\033[38;5;{color}m\\033[38;5;{color}m "
color_codes += " " * (2 - (len(str(color)) - 1))
if color in seven_row:
color_codes += f"{reset}\n"
print(color_codes, reset)
# Background
def print_background_colors():
print("Background")
print("Linux, BSD, div. Unixe, Apple & Windows ANSII 16 Colors:")
color_codes = ""
for color in range(0, 16):
color_codes += f"\033[48;5;{color}m\\033[48;5;{color}m "
color_codes += " " * (1 - (len(str(color)) - 1))
if color == 7:
color_codes += f"{reset}\n"
print(color_codes, reset)
print("\nLinux, BSD, div. Unixe XTERM 256 Colors:")
color_codes = ""
for color in range(16, 256):
color_codes += f"\033[48;5;{color}m\\033[48;5;{color}m "
color_codes += " " * (2 - (len(str(color)) - 1))
if color in seven_row:
color_codes += f"{reset}\n"
print(color_codes, reset)
if __name__ == "__main__":
print()
print_font_effects()
print()
print_text_colors()
print_background_colors()

View file

@ -1,6 +1,7 @@
#!/usr/bin/python3
import pygame
import text
ignore_errors = False
@ -9,28 +10,30 @@ buttons = {
}
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()
class NotEnoughInformationError(Exception):
def __init__(self, error):
super().__init__(error)
log.write(error, "error")
class Button: # Create class Button to make creating buttons easier.
def __init__(self, position: tuple, size: tuple, function, page: str, mouse_button: str="left", rect: pygame.rect=None, button_type: str="click"):
self.x, self.y = position # Make variables class wide
class Button:
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.mouse_button = mouse_button
self.rect = rect
self.type = button_type
if not page in buttons:
if page not in buttons:
buttons[page] = []
buttons[page].append(self) # Add button to button-list.
buttons[page].append(self)
def check(self, mouse_pos: tuple): # Check if the button is pressed.
x = self.x # Make variable names shorter
def check(self, mouse_pos: tuple): # check if a button is pressed
x = self.x
y = self.y
w = self.width
@ -38,17 +41,40 @@ class Button: # Create class Button to make creating buttons easier.
mx, my = mouse_pos
if self.rect is None:
if mx >= x and mx <= x + w and my >= y and my <= y + h:
self.function() # If button pressed, execute the function.
self.function()
else:
if self.rect.collidepoint((mx, my)): # Check for collide point of rect
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.page = page
self.mouse_button = mouse_button
self.button_type = button_type
if page not in buttons:
buttons[page] = []
buttons[page].append(self)
def check(self, mouse_pos: tuple):
mx, my = mouse_pos
if self.rect.collidepoint((mx, my)):
self.function()
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
@ -56,12 +82,6 @@ class HoverTitle:
self.collideable = collideable
self.delay = delay
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:
print()
raise(NotEnoughInformationError(error))
def check_button(button: Button, pressed: tuple, mouse_button: str, mouse_position: tuple):
mx, my = mouse_position # Make variable names shorter
@ -100,3 +120,11 @@ def check_buttons(mouse_pos: tuple, mouse_buttons, page: str, popups: list=[]):
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 example():
ht = HoverTitle()
if __name__ == "__main__":
example()

94
text.py Normal file
View file

@ -0,0 +1,94 @@
#!/usr/bin/python3
import random
import buntcheck
def format_string(text: str, prefix: str = "§", suffix: str = "",
auto_rs: bool = True): # formats the text e.g. text after "§red" is colored red
color_ansi = buntcheck.color_ansi
for color in color_ansi:
text = text.replace(prefix + color + suffix, color_ansi[color])
if auto_rs:
text += color_ansi["reset"]
return text
msg_types_ncl = {
"info": "[info]: ",
"ok": format_string("[OK]: "),
"warning": format_string("[WARNING]: "),
"error": format_string("[ERROR]: ")
}
msg_types = {
"info": "[info]: ",
"ok": format_string("[§green§boldOK§rs]: "),
"warning": format_string("§yellow§bold[WARNING]: ", auto_rs=False),
"error": format_string("§red§bold[ERROR]: ", auto_rs=False)
}
def rainbow(text): # makes the string rainbow-colored
color_codes = buntcheck.get_text_colors()
text_out = ""
for character in text:
text_out += random.choice(color_codes) + character
return text_out
def example(): # just an example of this script
print('This:'
'\ntext = "§boldThis§rs §underlineis§rs §italican "'
'\ntext += rainbow("Example!")'
'\nprint(color_name_to_ansi(text))'
'\n\nmakes this:')
text = "§boldThis§rs §underlineis§rs §italican "
text += rainbow("Example!")
print(format_string(text))
class Log:
def __init__(self, log_path: str=None, no_console: bool=False, no_colors: bool=False):
self.log_path = log_path
self.no_console = no_console
self.no_colors = no_colors
self.log = ""
def write(self, msg: str, msg_type: str = "info", prefix: str = ""):
if self.no_colors:
msg = prefix + msg_types_ncl[msg_type] + msg
else:
msg = prefix + msg_types[msg_type] + msg
msg += format_string("§rs")
if not self.no_console:
print(msg)
if self.log_path is not None:
log_file = open(self.log_path, "a")
log_file.write(msg + "\n")
log_file.close()
self.log += msg + "\n"
def read(self):
if self.log_path is not None:
log_file = open(self.log_path, "r")
log_content = log_file.read()
log_file.close()
return log_content
else:
return self.log
if __name__ == "__main__":
example()