MyreMylar / pygame_gui

A GUI system for pygame.
MIT License
698 stars 83 forks source link

anchor = {'right': 'right'} doesn't place the UIElement #486

Closed rugubara closed 9 months ago

rugubara commented 11 months ago

Describe the bug Alignment of UIPanel and UILabel using anchor = {'right': 'right'} hides the element being aligned.

To Reproduce baseRect is the bounding rect of the pygame surface

  1. 
        panelRect = baseRect.copy()
        panelRect.h = panelRect. h//3
        panelRect.w = panelRect.w - 36
        self.optionsPanel` = pygame_gui.elements.UIPanel(baseRect, manager=self.uimanager)
        self.userConfigPanel = pygame_gui.elements.UIPanel(panelRect, manager=self.uimanager,
                                                           container=self.optionsPanel,
                                                           anchors={'top': 'top',
                                                                    'right': 'right'})  ```
  2. Run and observe no panel

  3. replace right with left or centerx - and observe the correct placement of the panel.

  4. Repeat for UILabel and observe similar behaviour

Expected behaviour I expected to see that the userConfigPanel is placed glued to the top and right edges of the optionsPanel.

Screenshots Operation with anchor={'right': 'right'} image Operation with anchor={'left': 'left'} image

Platform and software (please complete the following information):

Additional context The output of the pygame-gui debug:

Layer: 0

root_container: thickness - 3

Layer: 1

panel panel.#panel_container: thickness - 2

Layer: 2

panel.panel panel.panel.#panel_container: thickness - 0

Full code of the method, creating the UI:


        # поделим экран на 3 равных горизонтальных полосы
        baseRect = self.screen.get_bounding_rect()
        baseRect.width = baseRect.width
        zeroRect = pygame.Rect(0 ,0 ,0 ,0)
        gutterRect = pygame.Rect(0 ,0 ,18 ,18)
        antigutterRect= pygame.Rect(0 ,0 ,-18 ,-18)

        panelRect = baseRect.copy()
        panelRect.h = panelRect. h//3
        # panelRect.w = panelRect.w - 36
        self.optionsPanel = pygame_gui.elements.UIPanel(baseRect, manager=self.uimanager)
        self.userConfigPanel = pygame_gui.elements.UIPanel(panelRect, manager=self.uimanager,
                                                           container=self.optionsPanel,
                                                           anchors={'top': 'top',
                                                                    'left': 'left'}) ``` 
GimLala commented 11 months ago

I was a bit confused with the anchoring system as well, but x and y values need to be negative in order for it to be placed properly. so you will need to set: panelRect.x = -panelRect.x - panelRect.width and panelRect.y = -panelRect.y - panelRect.height before creating the panel. The coordinates of the relative rectangle will place the top left of the panel relative to the bottom right of the container.

This should fix the issue, however I do admit that we should not have to subtract the width and height from the positions in order to properly place the panel. @MyreMylar should look into this