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

Text Scaling and Font Size Issues in Level Editor #748

Open
ShivamKR12 opened this issue Jan 24, 2025 · 3 comments · May be fixed by #752
Open

Text Scaling and Font Size Issues in Level Editor #748

ShivamKR12 opened this issue Jan 24, 2025 · 3 comments · May be fixed by #752

Comments

@ShivamKR12
Copy link

Hi @pokepetter,

I’ve been working on customizing the Level Editor in Ursina and encountered some issues related to text scaling and font size adjustments. Specifically:

  1. Global Text Size: Changing Text.size affects some text entities but not others, leading to inconsistent scaling.
  2. Help Menu Tooltip: Despite modifying scale, original_scale, and Text.size, there seems to be no noticeable change in the font size of the help menu tooltip text.
  3. Entity List Text: Similarly, adjusting scale and Text.size for entity-related text components (like self.entity_list_text) does not always yield the expected changes.

Here’s an example of the modifications I’ve tried for the help menu tooltip:

class Help(Button):
    def __init__(self, **kwargs):
        super().__init__(
            parent=self.ui,
            text='?',
            scale=5,
            model='circle',
            origin=(-.5, .5),
            text_origin=(0, 0),
            position=window.top_left
        )
        self.tooltip = Text(
            position=self.position + Vec3(.05, -.05, -10),
            font='VeraMono.ttf',
            enabled=False,
            text="Help menu text here...",
            background=True,
            scale=5  # Attempted scale change
        )
        self.tooltip.size = 2.5  # Attempted font size adjustment

Unfortunately, none of the above changes had a significant effect on the font size or appearance of the help menu text.


Questions:

  1. Is there a specific method or property I need to use to reliably adjust font sizes for text entities like tooltips or entity lists in the Level Editor?
  2. Could this behavior be related to how text entities inherit scaling from parent objects?
  3. Is this a limitation or bug in the current implementation of the Text class in Ursina?

Environment:

  • Ursina Version: 7.0.0
  • Python Version: 3.12
  • Operating System: Windows 11

Any guidance or suggestions would be greatly appreciated!

@pokepetter
Copy link
Owner

The intended way to globally scale text is something like Text.size *= 1.25for example. This seems to work with the level editor. However, the buttons' sizes are not tied to the text size, which means you can make the text go outside the button if you try. I don't see any easy solutions to fix that though, since the UI would have to be designed for all the UI elements to be scalable. It might be easier to implement custom UI scaling for the level editor, eventually.

@ShivamKR12
Copy link
Author

Hi @pokepetter,

I'm encountering an issue where the following scripts cause the window to immediately close without any error messages or traceback. The scripts are meant to implement a scaled level editor using DropdownMenu, DropdownMenuButton, and other UI components from Ursina. Below are two examples:

Script 1:

from ursina import *
from ursina.prefabs.dropdown_menu import DropdownMenu, DropdownMenuButton
from ursina.prefabs.file_browser import FileBrowser
from textwrap import dedent

def main():
    app = Ursina(borderless=False)
    window.fullscreen = False
    window.size = (1280, 720)
    window.position = (100, 100)
    
    editor = ScaledLevelEditor()
    editor.eternal = True  # Prevent auto-destruction
    
    app.run()

class ScaledLevelEditor(Entity):
    def __init__(self):
        super().__init__(eternal=True)
        self.setup_ui()
        
    def setup_ui(self):
        Text.default_resolution = 1080
        Text.size = 24  # Absolute size instead of multiplication
        
        self.ui = Entity(parent=camera.ui, eternal=True)
        
        # File menu
        self.file_menu = DropdownMenu(
            parent=self.ui,
            text='File',
            buttons=(
                DropdownMenuButton('New'),
                DropdownMenuButton('Open'),
                DropdownMenuButton('Save'),
            ),
            position=(-0.4, 0.45),
            scale=(0.15, 0.05)
        )
        
        # Scale menu text
        self.file_menu.text_entity.world_scale = 1.5
        for button in self.file_menu.buttons:
            button.text_entity.world_scale = 1.5
        
        # Entity list
        self.entity_list = Entity(
            parent=self.ui,
            position=(-0.4, 0),
            scale=(0.3, 0.8)
        )
        
        self.entity_list_text = Text(
            parent=self.entity_list,
            text='Entities:',
            world_scale=2,
            position=(0, 0.45)
        )
        
        # Grid button
        self.grid_button = Button(
            parent=self.ui,
            text='Grid',
            scale=(0.1, 0.05),
            position=(0.35, 0.45)
        )
        self.grid_button.text_entity.world_scale = 1.5
        
        # Model buttons
        self.model_buttons = []
        for i in range(9):
            x = -0.4 + (i % 3) * 0.3
            y = 0.25 - (i // 3) * 0.15
            btn = Button(
                parent=self.ui,
                text=str(i+1),
                scale=(0.08, 0.08),
                position=(x, y)
            )
            btn.text_entity.world_scale = 1.5
            self.model_buttons.append(btn)
        
        # Help button
        self.help_button = Button(
            parent=self.ui,
            text='?',
            scale=(0.08, 0.08),
            position=(0.45, 0.45)
        )
        self.help_button.text_entity.world_scale = 1.5
        
        self.help_tooltip = Text(
            parent=self.ui,
            text=dedent('''
                Hotkeys:
                left mouse    : place/select
                right mouse   : remove
                middle mouse : drag
                scroll wheel : rotate
                f            : move to mouse
                1,2,3...     : select model
                shift+1,2,3  : save model
            ''').strip(),
            world_scale=1.5,
            enabled=False,
            background=True,
            position=(0.2, 0.2)
        )

    def input(self, key):
        if key == 'escape':
            self.enabled = not self.enabled
        if self.help_button.hovered:
            self.help_tooltip.enabled = True
        else:
            self.help_tooltip.enabled = False

if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        print(f"Error: {e}")

Script 2:

from ursina import *
from ursina.prefabs.dropdown_menu import DropdownMenu, DropdownMenuButton
from ursina.prefabs.file_browser import FileBrowser


class ScaledLevelEditor(Entity):
    def __init__(self):
        window.fullscreen = False
        window.size = (1280, 720)  # Fixed window size
        
        super().__init__()
        Text.size *= 2
        Text.default_resolution = 1080
        
        self.ui = Entity(parent=camera.ui, ignore_paused=True)
        
        # Toolbar
        self.toolbar = Entity(parent=self.ui)
        self.toolbar.scale_y = 0.1
        self.toolbar.y = 0.45
        
        # File menu
        self.file_menu = DropdownMenu(
            text='File',
            buttons=(
                DropdownMenuButton('New'),
                DropdownMenuButton('Open'),
                DropdownMenuButton('Save'),
            ),
            scale=(0.2, 0.05),
            position=(-0.4, 0.45)
        )
        for button in self.file_menu.buttons:
            button.text_entity.scale *= 2
        
        # Entity list
        self.entity_list = Entity(
            parent=self.ui,
            position=(-0.4, 0),
            scale=(0.3, 0.8)
        )
        self.entity_list_text = Text(
            parent=self.entity_list,
            text='Entities:',
            scale=2,
            position=(0, 0.45)
        )
        
        # Help button
        self.help_button = Button(
            parent=self.ui,
            text='?',
            scale=(0.1, 0.1),
            position=(0.45, 0.45)
        )
        self.help_button.text_entity.scale *= 2
        
        # Help tooltip
        self.help_tooltip = Text(
            parent=self.ui,
            text=dedent('''
                Hotkeys:
                left mouse    : place/select
                right mouse   : remove
                middle mouse : drag
                scroll wheel : rotate
                f            : move to mouse
                1,2,3...     : select model
                shift+1,2,3  : save model
            ''').strip(),
            scale=2,
            enabled=False,
            background=True,
            position=(0.2, 0.2)
        )
        
        # Grid toggle with scaled button
        self.grid_button = ScaledButton(
            parent=self.ui,
            text='Grid',
            scale=.08,
            position=(.35, .45)
        )
        
        # Model selection buttons
        self.model_buttons = []
        for i in range(9):
            x = -.4 + (i % 3) * .4
            y = .25 - (i // 3) * .2
            btn = ScaledButton(
                parent=self.ui,
                text=str(i+1),
                scale=.08,
                position=(x, y)
            )
            self.model_buttons.append(btn)

    def input(self, key):
        if key == 'escape':
            self.enabled = not self.enabled
        if self.help_button.hovered:
            self.help_tooltip.enabled = True
        else:
            self.help_tooltip.enabled = False

class ScaledButton(Button):
    def __init__(self, **kwargs):
        super().__init__()
        self.scale *= 2  # Increase button size
        self.text_entity.scale *= 2  # Scale text proportionally
        for key, value in kwargs.items():
            setattr(self, key, value)

class ScaledDropdownMenu(DropdownMenu):
    def __init__(self, text, buttons):
        super().__init__(text=text, buttons=buttons)
        self.scale *= 2
        self.text_entity.scale *= 2
        for b in self.buttons:
            b.scale *= 2
            b.text_entity.scale *= 2

class ScaledDropdownMenuButton(DropdownMenuButton):
    def __init__(self, text):
        super().__init__(text=text)
        self.scale *= 2
        self.text_entity.scale *= 2

if __name__ == '__main__':
    try:
        app = Ursina()
        editor = ScaledLevelEditor()
        app.run()
    except Exception as e:
        print(f"An error occurred: {e}")

Console Log Output:

iPS C:\Users\Shivam\AppData\Local\Programs\Python\Python312> & C:/Users/Shivam/AppData/Local/Programs/Python/Python312/python.exe c:/Users/Shivam/AppData/Local/Programs/Python/Python312/levelEditor.py
info: Using primary monitor: Monitor(x=0, y=0, width=1366, height=768, width_mm=344, height_mm=193, name='\\\\.\\DISPLAY1', is_primary=True)
:prc(warning): Invalid integer value for ConfigVariable win-size: 614.4000244140625
:prc(warning): Invalid integer value for ConfigVariable win-size: 1092.800048828125
Known pipe types:
  wglGraphicsPipe
(3 aux display modules not yet loaded.)
set window position: Vec2(137, 77)
:prc(warning): changing default value for ConfigVariable paste-emit-keystrokes from '1' to '0'.
:pnmimage:png(warning): iCCP: known incorrect sRGB profile
package_folder: C:\Users\Shivam\AppData\Local\Programs\Python\Python312\Lib\site-packages\ursina
asset_folder: c:\Users\Shivam\AppData\Local\Programs\Python\Python312
PS C:\Users\Shivam\AppData\Local\Programs\Python\Python312> 

Details:

  1. Both scripts appear to initialize properly, but they do not create the DropdownMenu, buttons, or other entities as the window immediately closes.
  2. I'm using Python 3.12 and the latest version of Ursina installed via pip.
  3. Adjusting window size, resolution, and fullscreen options does not resolve the issue.
  4. The warnings about win-size and paste-emit-keystrokes appear, but it’s unclear if they are relevant to this problem.

Do you have any insights into why this might be happening? Could it be related to compatibility with Python 3.12 or the eternal=True attribute on entities? Any guidance would be appreciated!

ShivamKR12 added a commit to ShivamKR12/ursina that referenced this issue Jan 28, 2025
Fixes pokepetter#748

Update `LevelEditor` and `Help` classes to ensure consistent text scaling and font size adjustments.

* **LevelEditor Class:**
  - Add `ui_scale_factor` attribute for default scale.
  - Modify `origin_mode_menu` and `local_global_menu` to scale with `ui_scale_factor`.
  - Update `update_text_scale` function to scale text entities in button groups.
  - Adjust `editor_camera` position scaling.
  - Scale `gizmo`, `gizmo_toggler`, and `quick_grabber` with `ui_scale_factor`.
  - Scale `inspector` and `help` with `ui_scale_factor`.
  - Scale `point_renderer` model thickness.
  - Scale `cubes` in `render_selection`.
  - Scale `ui` in `on_enable` and `on_disable` methods.
  - Adjust `entity_list_text` and `selected_renderer` scale.
  - Update `text_field` scale in `InspectorButton`.

* **Help Class:**
  - Modify `tooltip` scale to 1 for noticeable text size changes.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/pokepetter/ursina/issues/748?shareId=XXXX-XXXX-XXXX-XXXX).
@ShivamKR12
Copy link
Author

Hi @pokepetter,

I’ve been working on addressing the problem mentioned in this issue and have made some changes to level_editor.py as part of [Pull Request #752](#752).

Progress So Far:

  • The text size issue has been partially resolved. The text now scales more appropriately in many areas.
  • However, there are still a few places where text overflow occurs (e.g., in Hierarcy List and tooltips).
  • Additionally, the mismatch between button sizes/UI element sizes and their corresponding text is yet to be resolved. Some UI elements appear too small or too large for their text, leading to alignment issues.

Could you please review the changes in the pull request and provide your guidance on how best to address the remaining issues? Your input would be invaluable in ensuring this gets fully resolved.

Thank you!
Shivam

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 a pull request may close this issue.

2 participants