Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set_dimensions now support dynamic sizes #514

Merged
merged 4 commits into from
Feb 26, 2024

Conversation

LondonClass
Copy link
Contributor

@LondonClass LondonClass commented Feb 15, 2024

Fixes #498

Copy link

codecov bot commented Feb 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.62%. Comparing base (38291c9) to head (02c20ca).
Report is 14 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #514      +/-   ##
==========================================
- Coverage   95.71%   95.62%   -0.10%     
==========================================
  Files          84       84              
  Lines       11676    11788     +112     
==========================================
+ Hits        11176    11272      +96     
- Misses        500      516      +16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@MyreMylar
Copy link
Owner

I have tested this with a small program and using button.set_dimensions(-1,-1) does not change the button permanently into a dynamically sized button, it just adjusts the button size dynamically once. I would expect calling the function like this to make it behave like creating a button with dynamic dimensions.

Test program to compare:

import pygame

from pygame import Rect
from pygame_gui import UIManager
from pygame_gui.elements import UIButton


pygame.init()

pygame.display.set_caption('Quick Start')
window_surface = pygame.display.set_mode((800, 600))
manager = UIManager((800, 600))

background = pygame.Surface((800, 600))
background.fill((50, 50, 50))


hello_button = UIButton(Rect(350, 280, 100, 30), 'Hello')  # button starts with static dimensions
hello_button_2 = UIButton(Rect(350, 180, -1, -1), 'Hello')  # button that starts with dynamic dimensions
clock = pygame.time.Clock()
is_running = True

while is_running:
    time_delta = clock.tick(60)/1000.0
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            is_running = False

        if event.type == pygame.KEYDOWN and event.key == pygame.K_d:
            hello_button.set_dimensions((-1, -1))  # press 'd' to convert to dynamic dimensions
        if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
            hello_button.set_text("Long string of text")  # press 's' and 'a' to toggle text in the buttons
            hello_button_2.set_text("Long string of text")
        if event.type == pygame.KEYDOWN and event.key == pygame.K_a:
            hello_button.set_text("Hello")
            hello_button_2.set_text("Hello")

        manager.process_events(event)

    manager.update(time_delta)

    window_surface.blit(background, (0, 0))
    manager.draw_ui(window_surface)

    pygame.display.update()

@LondonClass
Copy link
Contributor Author

@MyreMylar This may disrupt the previous code, causing the element to become dynamic unexpectedly. (For example, when an error occurs and a negative value is passed in)

@MyreMylar
Copy link
Owner

@MyreMylar This may disrupt the previous code, causing the element to become dynamic unexpectedly. (For example, when an error occurs and a negative value is passed in)

I think that is a risk that we have to take given the API of passing in -1 to indicate a dynamic dimension.

The alternative would be adding some keyword only parameters that override whatever is specified in the dimensions e.g:

element.set_dimensions((0,0), dynamic_width=True. dynamic_height=True)

Copy link
Owner

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, LGTM 👍

Works how I would expect now.

@MyreMylar MyreMylar merged commit 7bdaa52 into MyreMylar:main Feb 26, 2024
11 checks passed
@LondonClass LondonClass deleted the set_dimensions-modification branch March 23, 2024 05:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UIButton.set_dimensions() method does not support dynamic sizes
2 participants