Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GusInfiniteLinesOfCode committed Mar 21, 2024
1 parent 71bea8f commit c8139f9
Show file tree
Hide file tree
Showing 23 changed files with 1,357 additions and 31 deletions.
Binary file added __pycache__/UIpanel.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/script.cpython-311.pyc
Binary file not shown.
65 changes: 34 additions & 31 deletions easy_ui_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
# Initialize Pygame
pygame.init()
if __name__ == "__main__":
from button import Button
from input_feild import InputField
from TextElement import TextElement
from checkbox import CheckBox
from color import ColorPickerInputField
from Image import ImageElement
from numeric_input import NumericInputField
from elements.button import Button
from elements.input_feild import InputField
from elements.TextElement import TextElement
from elements.checkbox import CheckBox
from elements.color import ColorPickerInputField
from elements.Image import ImageElement
from elements.numeric_input import NumericInputField
from script import Script
from UIpanel import UIPanel
from slider import Slider
from elements.slider import Slider


# from Nodes import

Expand All @@ -28,15 +29,15 @@
BUTTON_HOVER_COLOR = (100, 100, 100)
WHITE = (255, 255, 255)


# Set the screen size
infoObject: object = pygame.display.Info()
WIDTH, HEIGHT = infoObject.current_w, infoObject.current_h
screen_width, screen_height = infoObject.current_w, infoObject.current_h
screen: pygame.Surface = pygame.display.set_mode(
(infoObject.current_w, infoObject.current_h)
)
pygame_icon = pygame.image.load(
r"terraria_styled_game\program recources\Screenshot 2023-09-21 181742.png"
r"recources\program recources\Screenshot 2023-09-21 181742.png"
)
pygame.display.set_icon(pygame_icon) # pygame.display.toggle_fullscreen()
pygame.display.set_caption("Square Pixel")
Expand Down Expand Up @@ -94,6 +95,7 @@ def update_font_size(selected_element, scroll_direction):
if isinstance(selected_element, Button):
selected_element.font_size += scroll_direction * 2


def create_button(text, x, y, width, height, command, additional_data=None):
"""
Create a button widget
Expand All @@ -111,7 +113,7 @@ def create_button(text, x, y, width, height, command, additional_data=None):
- Return the Button object
"""
butn = Button(text, x, y, width, height, command, additional_data)
buttons .append(butn)
buttons.append(butn)
return butn


Expand Down Expand Up @@ -245,6 +247,10 @@ def delete_selected_element():
text_elements.remove(selected_element)
elif isinstance(selected_element, CheckBox):
checkboxes.remove(selected_element)
elif isinstance(selected_element, Slider):
sliders.remove(selected_element)
elif isinstance(selected_element, Image):
images.remove(selected_element)

selected_element = None

Expand Down Expand Up @@ -278,45 +284,44 @@ def export_ui_elements():
global buttons, input_fields, checkboxes, images, sliders, text_elements
code = [
"if __name__ == '__main__':",
" from SquarePixels.uimanagement.button import Button",
" from SquarePixels.uimanagement.input_feild import InputField",
" from SquarePixels.uimanagement.TextElement import TextElement",
" from SquarePixels.uimanagement.checkbox import CheckBox",
" from SquarePixels.uimanagement.color import ColorPickerInputField",
" from SquarePixels.uimanagement.Image import ImageElement",
" from SquarePixels.uimanagement.slider import Slider",

" from SquarePixels.uimanagement.elements.button import Button",
" from SquarePixels.uimanagement.elements.input_feild import InputField",
" from SquarePixels.uimanagement.elements.TextElement import TextElement",
" from SquarePixels.uimanagement.elements.checkbox import CheckBox",
" from SquarePixels.uimanagement.elements.color import ColorPickerInputField",
" from SquarePixels.uimanagement.elements.Image import ImageElement",
" from SquarePixels.uimanagement.elements.slider import Slider",
]

# Create buttons
# Create buttons screen_width/{screen_width/x}
for index, button in enumerate(buttons):
code.append(
f"button{index + 1} = Button('{button.text}',{button.x}, {button.y}, {button.width}, {button.height}, None)"
f"button{index + 1} = Button('{button.text}', WIDTH / {WIDTH / button.x}, HEIGHT / {HEIGHT / button.y}, WIDTH / {WIDTH / button.width}, HEIGHT / {HEIGHT / button.height}, None)"
)

# Create input fields
for index, input_field in enumerate(input_fields):
code.append(
f"input_field{index + 1} = InputField({input_field.x}, {input_field.y}, {input_field.width}, {input_field.height}, '{input_field.placeholder}')"
f"input_field{index + 1} = InputField(WIDTH / {WIDTH / input_field.x}, HEIGHT / {HEIGHT / input_field.y}, WIDTH / {WIDTH / input_field.width}, HEIGHT / {HEIGHT / input_field.height}, '{input_field.placeholder}')"
)
for index, text_element in enumerate(text_elements):
code.append(
f"TextElement{index + 1} = TextElement({text_element.x}, {text_element.y}, {text_element.width}, {text_element.height}, '{text_element.placeholder}')"
f"TextElement{index + 1} = TextElement(WIDTH / {WIDTH / text_element.x}, HEIGHT / {HEIGHT / text_element.y}, {text_element.text}, {text_element.font_size})"
)
for index, checkbox in enumerate(checkboxes):
code.append(
f"CheckBox{index + 1} = CheckBox({checkbox.x}, {checkbox.y}, {checkbox.width}, {checkbox.height}, '{checkbox.placeholder}')"
f"CheckBox{index + 1} = CheckBox(WIDTH / {WIDTH / checkbox.x}, HEIGHT / {HEIGHT / checkbox.y}, WIDTH / {WIDTH / checkbox.width}, HEIGHT / {HEIGHT / checkbox.height}')"
)

for index, image in enumerate(images):
code.append(
f"Image{index + 1} = ImageElement({image.x}, {image.y}, {image.width}, {image.height})"
f"Image{index + 1} = ImageElement(WIDTH / {WIDTH / image.x}, HEIGHT / {HEIGHT / image.y}, WIDTH / {WIDTH / image.width}, HEIGHT / {HEIGHT / image.height})"
)
for index, slider in enumerate(sliders):
code.append(
f"SliderElement{index + 1} = Slider({slider.x}, {slider.y}, {slider.width}, {slider.height}, {slider.min_value}, {slider.max_value}, {slider.default_value}, {slider.command}, {slider.additional_data}, {slider.color}, {slider.colortwo}, {slider.text}, {slider.text_position_below}, {slider.size}"
f"SliderElement{index + 1} = Slider(WIDTH / {WIDTH / slider.x}, HEIGHT / {HEIGHT / slider.y}, WIDTH / {WIDTH / slider.width}, HEIGHT / {HEIGHT / slider.height}, {slider.min_value}, {slider.max_value}, {slider.value}, {slider.command}, {slider.additional_data}, {slider.color}, {slider.colortwo}, {slider.text}, {slider.text_position_below}, {slider.size})"
)
Slider()


code.append("code copied to clipboard")
result = "\n".join(code)
pyperclip.copy(result)
Expand Down Expand Up @@ -673,9 +678,7 @@ def handle_events():
for slider in sliders:
if (
slider.x < event.pos[0] < slider.x + slider.width
and slider.y
< event.pos[1]
< slider.y + slider.height
and slider.y < event.pos[1] < slider.y + slider.height
):
slider.active = True
selected_element = slider
Expand Down
37 changes: 37 additions & 0 deletions elements/Image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import pygame
from PIL import Image, ImageDraw, ImageOps


class ImageElement:
def __init__(self, x, y, image, crop_mode="None"):
self.x = x
self.y = y
self.image = image
self.crop_mode = crop_mode # "None" or "Circle"
self.active = False
self.width, self.height = self.image.size
self.widtho, self.heighto = self.image.size
self.text = ""
self.bold = False
self.italics = False
self.underlined = False
self.font_name = "Recources\Fonts\PixelifySans-Regular.ttf"
self.color = (0, 0, 0)
self.size = self.width * self.height

def draw(self, screen):
# Optionally apply cropping based on the selected cropping mode
if self.crop_mode == "Circle":
# Crop the image to a circle using PIL
mask = Image.new("L", (self.widtho, self.heighto), 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0, self.widtho, self.heighto), fill=255)
self.image.putalpha(mask)

# Draw the image on the screen
pygame_image = pygame.image.fromstring(
self.image.tobytes(), self.image.size, self.image.mode
)
pygame_image = pygame.transform.scale(pygame_image, (self.width, self.height))
screen.blit(pygame_image, (self.x, self.y))
77 changes: 77 additions & 0 deletions elements/TextElement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import pygame


class TextElement:
def __init__(self, x, y, text, font_size):
"""
Initialize a button object
Args:
x: x-coordinate of the button
y: y-coordinate of the button
text: Text to display on the button
font_size: Font size of the text
Returns:
self: Button object
- Sets the x and y coordinates
- Sets the text and font size
- Sets default color, size and other attributes
- Initializes other properties like hover, width, height etc.
"""
self.x = x
self.y = y
self.text = text
self.font_size = font_size
self.color = (255, 255, 255) # Default text color
self.size = font_size
self.hovered = False
self.width = 50
self.height = 25
self.bold = False
self.italics = False
self.underlined = False
self.font_name = None

def draw(self, screen):
"""
Renders text on a screen surface.
Args:
screen: The screen surface to render text on
Returns:
None: Does not return anything
- Loads the font based on properties of the Text object
- Renders the text surface using the font and text properties
- Blits/draws the text surface onto the screen surface at the x,y position"""
font = pygame.font.Font(self.font_name, self.size)
font.set_bold(self.bold)
font.set_italic(self.italics)
font.set_underline(self.underlined)
text_surface = font.render(self.text, True, self.color)
screen.blit(text_surface, (self.x, self.y))

def change_text(self, event):
"""
Change text on mouse events
Args:
event: pygame event object
Returns:
None
Processing Logic:
- Check if mouse is hovering over button on MOUSEMOTION
- Set button to active if mouse is pressed on button on MOUSEBUTTONDOWN
- Change text if a key is pressed while button is active on KEYDOWN
"""
if event.type == pygame.MOUSEMOTION:
self.hovered = (
self.x < event.pos[0] < self.x + self.width
and self.y < event.pos[1] < self.y + self.height
)
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.hovered:
self.active = True
else:
self.active = False
if event.type == pygame.KEYDOWN and self.active:
if event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
Binary file added elements/__pycache__/Image.cpython-311.pyc
Binary file not shown.
Binary file added elements/__pycache__/TextElement.cpython-311.pyc
Binary file not shown.
Binary file added elements/__pycache__/button.cpython-311.pyc
Binary file not shown.
Binary file added elements/__pycache__/checkbox.cpython-311.pyc
Binary file not shown.
Binary file added elements/__pycache__/color.cpython-311.pyc
Binary file not shown.
Binary file added elements/__pycache__/input_feild.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file added elements/__pycache__/slider.cpython-311.pyc
Binary file not shown.
118 changes: 118 additions & 0 deletions elements/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import pygame


if __name__ == "__main__":
# Constants
infoObject: object = pygame.display.Info()
WIDTH, HEIGHT = infoObject.current_w, infoObject.current_h
BACKGROUND_COLOR = (0, 0, 0)
font = pygame.font.Font("Recources\Fonts\PixelifySans-Regular.ttf", 36)
# Define colors
WHITE = (255, 255, 255)
BUTTON_COLOR = (50, 50, 50)
BUTTON_HOVER_COLOR = (100, 100, 100)
white = (255, 255, 255)


class Button:
"""Button class for creating interactive buttons in the game."""

def __init__(
self,
text,
x,
y,
width,
height,
command,
additional_data: list = None,
color=(255, 255, 255),
):
"""
Initialize a button.
Args:
text (str): The text displayed on the button.
x (int): The x-coordinate of the button's top-left corner.
y (int): The y-coordinate of the button's top-left corner.
width (int): The width of the button.
height (int): The height of the button.
command (function): The function to be executed when the button is clicked.
aditional data (list): arguments the buttons command needs to run
"""
self.text = text
self.x = x
self.y = y
self.width = width
self.height = height
self.command = command
self.additional_data = additional_data
self.hovered = False
self.size = 36
self.active = False
self.bold = False
self.italics = False
self.underlined = False
self.font_name = "Recources\Fonts\PixelifySans-Regular.ttf"
self.color = color

def draw(self, screen):
"""Draw the button on the screen."""
font = pygame.font.Font(self.font_name, self.size)
font.set_bold(self.bold)
font.set_italic(self.italics)
font.set_underline(self.underlined)
color = BUTTON_HOVER_COLOR if self.hovered else BUTTON_COLOR
pygame.draw.rect(screen, color, (self.x, self.y, self.width, self.height))
text = font.render(self.text, True, self.color)
screen.blit(
text,
(
self.x + self.width // 2 - text.get_width() // 2,
self.y + self.height // 2 - text.get_height() // 2,
),
)

def handle_event(self, event):
"""
Handle events related to the button.
Args:
event: The Pygame event to be processed.
"""
if event.type == pygame.MOUSEMOTION:
self.hovered = (
self.x < event.pos[0] < self.x + self.width
and self.y < event.pos[1] < self.y + self.height
)
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.hovered:
self.active = True
if self.additional_data != None:
a = self.command(*self.additional_data)
else:
a = self.command()
if a:
return a
else:
self.active = False

def selected(self):
self.hovered = True

def change_text(self, event):
if event.type == pygame.MOUSEMOTION:
self.hovered = (
self.x < event.pos[0] < self.x + self.width
and self.y < event.pos[1] < self.y + self.height
)
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.hovered:
self.active = True
else:
self.active = False
if event.type == pygame.KEYDOWN and self.active:
if event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
Loading

0 comments on commit c8139f9

Please sign in to comment.