r/pythonarcade • • Nov 19 '18

UI and viewport

Hey,

I am want to add some UI to my game,

wondering if there is a way to draw something skipping the viewport.

I could update the UI elements every time to subtract the current viewport, but maybe there is another method, I did not see.

Greetings

2 Upvotes

6 comments sorted by

View all comments

Show parent comment

2

u/Fonus2 Nov 19 '18 edited Nov 19 '18

I ended up with following prototype (not handling scale, resize, performance etc.):

import arcade

class UiItem:
    
    def __init__(self, imagefilepath, x=0, y=0, scale=1):
        self.x = x
        self.y = y
        
        self.sprite = arcade.Sprite(imagefilepath, scale)
        self.sprite.center_x = x
        self.sprite.center_y = y
        
    def update(self):
        left, right, bottom, top = arcade.get_viewport()
        self.sprite.center_x = self.x + left
        self.sprite.center_y = self.y + bottom  
        
    def draw(self):
        self.sprite.draw()

Wich should place most stuff correctly.

As for mouse click:

def on_mouse_motion(self, x, y, dx, dy):  
        self.cursor.x = x
        self.cursor.y = y


def on_mouse_press(self, x, y, button, modifiers):
    if button == arcade.MOUSE_BUTTON_LEFT:
        left, right, bottom, top = arcade.get_viewport()
        x = x + left
        y = y + bottom

self.cursor is ofcourse an UiItem.

1

u/eruvanos Nov 20 '18

Do you know if there is any UI component collection out there?

Beside some simple click stuff, it takes more to support text fields, drag and drop.

Would be nice to find something like that.

1

u/Fonus2 Nov 20 '18

None tailored for Arcade, guess that you can try using something that pygame uses or try to adapt a generic one.