GUI Elements

GUI Elements
gui_elements.py
  1"""
  2Show how to use GUI elements.
  3
  4You can run this example with:
  5python -m arcade.examples.gui_elements_example
  6
  7To style, see the style example or use a yaml file.
  8See:
  9https://github.com/pvcraven/arcade/blob/development/arcade/resources/style/default.yml
 10and the UIStyle.from_file() command.
 11
 12"""
 13import arcade
 14
 15import arcade.gui
 16from arcade.gui import UIManager
 17
 18
 19class MyFlatButton(arcade.gui.UIFlatButton):
 20    """
 21    To capture a button click, subclass the button and override on_click.
 22    """
 23    def on_click(self):
 24        """ Called when user lets off button """
 25        print("Click flat button. ")
 26
 27class MyGhostFlatButton(arcade.gui.UIGhostFlatButton):
 28    """
 29    For this subclass, we create a custom init, that takes in another
 30    parameter, the UI text box. We use that parameter and print the contents
 31    of the text entry box when the ghost button is clicked.
 32    """
 33
 34    def __init__(self, center_x, center_y, input_box):
 35        super().__init__(
 36            'GhostFlatButton',
 37            center_x=center_x,
 38            center_y=center_y,
 39            width=250,
 40            # height=20
 41        )
 42        self.input_box = input_box
 43
 44    def on_click(self):
 45        """ Called when user lets off button """
 46        print(f"Click ghost flat button. {self.input_box.text}")
 47
 48
 49class MyView(arcade.View):
 50    """
 51    Main view. Really the only view in this example. """
 52    def __init__(self):
 53        super().__init__()
 54
 55        self.ui_manager = UIManager()
 56
 57    def on_draw(self):
 58        """ Draw this view. GUI elements are automatically drawn. """
 59        arcade.start_render()
 60
 61    def on_show_view(self):
 62        """ Called once when view is activated. """
 63        self.setup()
 64        arcade.set_background_color(arcade.color.BLACK)
 65
 66    def on_hide_view(self):
 67        self.ui_manager.unregister_handlers()
 68
 69    def setup(self):
 70        """ Set up this view. """
 71        self.ui_manager.purge_ui_elements()
 72
 73        y_slot = self.window.height // 4
 74        left_column_x = self.window.width // 4
 75        right_column_x = 3 * self.window.width // 4
 76
 77        # left side elements
 78        self.ui_manager.add_ui_element(arcade.gui.UILabel(
 79            'UILabel',
 80            center_x=left_column_x,
 81            center_y=y_slot * 3,
 82        ))
 83
 84        ui_input_box = arcade.gui.UIInputBox(
 85            center_x=left_column_x,
 86            center_y=y_slot * 2,
 87            width=300
 88        )
 89        ui_input_box.text = 'UIInputBox'
 90        ui_input_box.cursor_index = len(ui_input_box.text)
 91        self.ui_manager.add_ui_element(ui_input_box)
 92
 93        button_normal = arcade.load_texture(':resources:gui_basic_assets/red_button_normal.png')
 94        hovered_texture = arcade.load_texture(':resources:gui_basic_assets/red_button_hover.png')
 95        pressed_texture = arcade.load_texture(':resources:gui_basic_assets/red_button_press.png')
 96        button = arcade.gui.UIImageButton(
 97            center_x=left_column_x,
 98            center_y=y_slot * 1,
 99            normal_texture=button_normal,
100            hover_texture=hovered_texture,
101            press_texture=pressed_texture,
102            text='UIImageButton'
103        )
104        self.ui_manager.add_ui_element(button)
105
106        # right side elements
107        button = MyFlatButton(
108            'FlatButton',
109            center_x=right_column_x,
110            center_y=y_slot * 1,
111            width=250,
112            # height=20
113        )
114        self.ui_manager.add_ui_element(button)
115
116        button = MyGhostFlatButton(
117            center_x=right_column_x,
118            center_y=y_slot * 2,
119            input_box=ui_input_box
120        )
121        self.ui_manager.add_ui_element(button)
122
123
124if __name__ == '__main__':
125    window = arcade.Window(title='ARCADE_GUI')
126    view = MyView()
127    window.show_view(view)
128    arcade.run()