Home > OS >  Python Ursina hide UI
Python Ursina hide UI

Time:04-01

I have an GUI inventory and I don't know how to hide it and all the items there are inside. I tried to make the class Inventory(Entity) enabled or not and it didn't worked because it only gets enables and never disabled. This is my first post so, please, be nice.

My code:

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController


app = Ursina()

class Inventory(Entity):
    def __init__(self):
        player.enabled = False
        super().__init__(
            parent = camera.ui,
            model = 'quad',
            scale = (.5, .8),
            origin = (-.5, .5),
            position = (-.3,.4),
            texture = 'white_cube',
            texture_scale = (5,8),
            color = color.dark_gray
            )

        self.item_parent = Entity(parent=self, scale=(1/5,1/8))
        enable = False


def input(key):

        if key == 'f':
            inventory_enable()


            
def inventory_enable():

    inventory = Inventory()
    Inventory().enable = False
    ...

CodePudding user response:

Setting entity.enabled = False will deactivate it and all it's children. To make it reappear, set it back to True.

CodePudding user response:


app = Ursina()




class Inventory(Entity):
    def __init__(self):
        player.enabled = False
        super().__init__(
            parent = camera.ui,

            )

        self.inventory_ui = Entity(parent = self,
            model = 'quad',
            scale = (.5, .8),
            origin = (-.5, .5),
            position = (-.3,.4),
            texture = 'white_cube',
            texture_scale = (5,8),
            color = color.dark_gray,
            enable = True
        )

        self.item_parent = Entity(parent=self.inventory_ui, scale=(1/5,1/8))
        
    def find_free_spot(self):                                                      
        taken_spots = [(int(e.x), int(e.y)) for e in self.item_parent.children]    
        for y in range(8):                                                         
            for x in range(5):                                                     
                if not (x,-y) in taken_spots:                                      
                    return (x,-y)                                                  


    def append(self, item):
        icon = Draggable(
            parent = Inventory().item_parent,
            model = 'quad',
            texture = item,
            color = color.white,
            origin = (-.5,.5),
            position = self.find_free_spot(),
            z = -.1,
            )
        name = item.replace('_', ' ').title()
        icon.tooltip = Tooltip(name)
        icon.tooltip.background.color = color.color(0,0,0,.8)


        def drag():                                                     
            icon.org_pos = (icon.x, icon.y)                             

        def drop():
            icon.x = int(icon.x)
            icon.y = int(icon.y)



            '''if the spot is taken, swap positions'''
            for c in self.children:                                     
                if c == icon:                                           
                    continue                                            

                if c.x == icon.x and c.y == icon.y:                     
                    print('swap positions')                             
                    c.position = icon.org_pos                           


        icon.drag = drag                                                
        icon.drop = drop



        
#removed load_texture
grass_texture = "assets/grass.png"
soil_texture = "assets/soil.png"
stone_texture = "assets/stone.png"
wood_texture = "assets/wood.png"

sky_texture = load_texture("assets/skybox.png")

current_texture = grass_texture

def update():
    global current_texture
    if held_keys['1']: current_texture = grass_texture
    if held_keys['2']: current_texture = soil_texture
    if held_keys['3']: current_texture = stone_texture
    if held_keys['4']: current_texture = wood_texture

    # added
    if held_keys['g']:
        save_game()

    if held_keys['left mouse'] or held_keys['right mouse']:
        hand.active()
    else:
        hand.passive()



def input(key):
        key_f = 0
        
        if key == 'escape':
            quit()
        if key == 'f' and key_f == 0:
            key_f = 1
            inventory_enable()
        else:
            key_f = 0
            inventory_close()



def inventory_close():
    Inventory().inventory_ui.enable = False
    
def inventory_enable():

    inventory = Inventory()
    Inventory().enable = False
    def add_item():                                                                  
        Inventory().append(random.choice(('bag', 'bow_arrow', 'gem', 'orb', 'sword'))) 

    for i in range(7):                                                  
        add_item()                                                      

    add_item_button = Button(                                           
        scale = (.1,.1),                                                
        x = -.5,                                                        
        color = color.lime.tint(-.25),                                  
        text = ' ',                                                     
        tooltip = Tooltip('Add random item'),                           
        on_click = add_item                                             
    )

    

    ```
  • Related