Home > Back-end >  Ursina, how to change font in dropdownbutton
Ursina, how to change font in dropdownbutton

Time:10-30

I wish to change font or width in dropdownbutton in ursina, any help? cheer.

My simple dropdownbutton is the following

from ursina import *                    # Import the ursina engine
from ursina.prefabs.dropdown_menu import DropdownMenu, DropdownMenuButton

app = Ursina()                          # Initialise your Ursina app

def val_but(txt):
    print(txt)

DropdownMenu(text='File', buttons=(
    DropdownMenuButton('New', on_click = val_but('New')),
    DropdownMenuButton('Open'),
    DropdownMenu('Reopen Project', buttons=(
        DropdownMenuButton('Project 1'),
        DropdownMenuButton('Project 2'),
        )),
    DropdownMenuButton('Save'),
    DropdownMenu('Options', buttons=(
        DropdownMenuButton('Option a'),
        DropdownMenuButton('Option b'),
        )),
    DropdownMenuButton('Exit'),
    ), scale=(.2,.05), text_origin=(0,0), position=(-.58,.37))

app.run()                               # Run the app

I found how to change scale, text_origin and position as You can see in the last row, but still looking for change font and width of the inside text of dropdownbutton

CodePudding user response:

I found to myself a way to change the font and wish to share it, I feel that there is a more efficient way but anyway:

Step 1: copy the font file in the current ursina directory.

Step 2: use text_entity.font attribute.

Here a working example:

from ursina import *                    # Import the ursina engine
from ursina.prefabs.dropdown_menu import DropdownMenu, DropdownMenuButton

app = Ursina()                          # Initialise your Ursina app

def val_but(txt):
    print(txt)


class DropdownMenuButton(Button):
    def __init__(self, text='', **kwargs):
        super().__init__(
            scale=(.3,.025),
            origin=(-.5, .5),
            color=color.azure,
            pressed_scale=1,
            text=text,
            **kwargs
            )
        print(str(self.text_entity.width))
        self.text_entity.scale_y=1.4
        self.text_entity.scale_x=.25
        self.text_entity.font='AbyssinicaSIL-Regular.ttf'
        print(str(self.text_entity.width))
        self.on_click = self.test_but

    def test_but(self):
        print(self.text)


class DropdownMenu(DropdownMenuButton):

    def __init__(self, text='', buttons=list(), **kwargs):
        super().__init__(text=text)
        self.position = window.top_left
        self.buttons = buttons
        for i, b in enumerate(self.buttons):
            b.world_parent = self
            b.original_scale = b.scale
            b.x = 0
            b.y = -i-1 *.98
            b.enabled = False

            if isinstance(b, DropdownMenu):
                for e in b.buttons:
                    e.x = 1
                    e.y  = 1

        self.arrow_symbol = Text(world_parent=self, text='>', origin=(.5,.5), position=(.95, 0), color=color.gray)
        for key, value in kwargs.items():
            setattr(self, key, value)

    def open(self):
        for i, b in enumerate(self.buttons):
            invoke(setattr, self.buttons[i], 'enabled', True, delay=(i*.02))

    def close(self):
        for i, b in enumerate(reversed(self.buttons)):
            b.enabled = False

    def on_mouse_enter(self):
        super().on_mouse_enter()
        self.open()

    def input(self, key):
        if key == 'left mouse down' and mouse.hovered_entity and mouse.hovered_entity.has_ancestor(self):
            self.close()

    def update(self):
        if self.hovered or mouse.hovered_entity and mouse.hovered_entity.has_ancestor(self):
            return

        self.close()

window.title = 'My Game'                # The window title
window.icon = 'tigre.ico'
window.borderless = False               # Show a border
window.fullscreen = False               # Do not go Fullscreen
# window.exit_button.visible = False      # Do not show the in-game red X that loses the window
window.fps_counter.enabled = True       # Show the FPS (Frames per second) counter

DropdownMenu(text='File', buttons=(
    DropdownMenuButton('New'),
    DropdownMenuButton('Open'),
    DropdownMenu('Reopen Project', buttons=(
        DropdownMenuButton('Project 1'),
        DropdownMenuButton('Project 2'),
        )),
    DropdownMenuButton('Save'),
    DropdownMenu('Options', buttons=(
        DropdownMenuButton('Option a'),
        DropdownMenuButton('Option b'),
        )),
    DropdownMenuButton('Exit'),
    ), scale=(.4,.05), text_origin=(0,0), position=(-.58,.37),
    color=color.pink)

def input(key):
    print(key)

app.run()                               # Run the app

Thanks to Pokepetter for suggestion.

  • Related