|
2 | 2 |
|
3 | 3 | This example demonstrates how to create a custom text input
|
4 | 4 | which hides the contents behind a custom character, as often
|
5 |
| -required for login screens |
| 5 | +required for login screens. |
| 6 | +
|
| 7 | +Due to a bug in the current version of pyglet, the example uses ENTER to switch |
| 8 | +fields instead of TAB. This will be fixed in future versions. |
| 9 | +(https://github.com/pyglet/pyglet/issues/1197) |
6 | 10 |
|
7 | 11 | If arcade and Python are properly installed, you can run this example with:
|
8 | 12 | python -m arcade.examples.gui.exp_hidden_password
|
|
11 | 15 | from __future__ import annotations
|
12 | 16 |
|
13 | 17 | import arcade
|
14 |
| -from arcade.gui import UIManager, UIInputText, UIOnClickEvent |
| 18 | +from arcade.gui import UIInputText, UIOnClickEvent, UIView |
15 | 19 | from arcade.gui.experimental.password_input import UIPasswordInput
|
16 | 20 | from arcade.gui.widgets.buttons import UIFlatButton
|
17 | 21 | from arcade.gui.widgets.layout import UIGridLayout, UIAnchorLayout
|
18 | 22 | from arcade.gui.widgets.text import UILabel
|
| 23 | +from arcade import resources |
| 24 | + |
| 25 | +# Load kenny fonts shipped with arcade |
| 26 | +resources.load_system_fonts() |
19 | 27 |
|
20 | 28 |
|
21 |
| -class MyView(arcade.View): |
| 29 | +class MyView(UIView): |
22 | 30 | def __init__(self):
|
23 | 31 | super().__init__()
|
24 |
| - self.ui = UIManager() |
| 32 | + self.background_color = arcade.uicolor.BLUE_BELIZE_HOLE |
25 | 33 |
|
26 | 34 | grid = UIGridLayout(
|
27 | 35 | size_hint=(0, 0), # wrap children
|
28 |
| - row_count=3, # user, pw and login button |
| 36 | + row_count=5, # title | user, pw | login button |
29 | 37 | column_count=2, # label and input field
|
30 | 38 | vertical_spacing=10,
|
31 | 39 | horizontal_spacing=5,
|
32 | 40 | )
|
| 41 | + grid.with_padding(all=50) |
| 42 | + grid.with_background(color=arcade.uicolor.GREEN_GREEN_SEA) |
| 43 | + |
| 44 | + title = grid.add( |
| 45 | + UILabel(text="Login", width=150, font_size=20, font_name="Kenney Future"), |
| 46 | + column=0, |
| 47 | + row=0, |
| 48 | + column_span=2, |
| 49 | + ) |
| 50 | + title.with_padding(bottom=20) |
33 | 51 |
|
34 |
| - grid.add(UILabel(text="Username:", width=80), column=0, row=0) |
35 |
| - self.username_input = grid.add(UIInputText(), column=1, row=0) |
36 |
| - |
37 |
| - grid.add(UILabel(text="Password:", width=80), column=0, row=1) |
38 |
| - self.password_input = grid.add(UIPasswordInput(), column=1, row=1) |
| 52 | + grid.add(UILabel(text="Username:", width=80, font_name="Kenney Future"), column=0, row=1) |
| 53 | + self.username_input = grid.add( |
| 54 | + UIInputText(width=150, font_name="Kenney Future"), column=1, row=1 |
| 55 | + ) |
39 | 56 |
|
40 |
| - self.login_button = grid.add(UIFlatButton(text="Login"), column=0, row=2, column_span=2) |
| 57 | + grid.add(UILabel(text="Password:", width=80, font_name="Kenney Future"), column=0, row=2) |
| 58 | + self.password_input = grid.add( |
| 59 | + UIPasswordInput(width=150, font_name="Kenney Future"), column=1, row=2 |
| 60 | + ) |
| 61 | + self.password_input.with_background(color=arcade.uicolor.GREEN_GREEN_SEA) |
| 62 | + # set background to prevent full render on blinking caret |
| 63 | + |
| 64 | + self.login_button = grid.add( |
| 65 | + UIFlatButton(text="Login", height=30, width=150, size_hint=(1, None)), |
| 66 | + column=0, |
| 67 | + row=3, |
| 68 | + column_span=2, |
| 69 | + ) |
41 | 70 | self.login_button.on_click = self.on_login
|
42 | 71 |
|
| 72 | + # add warning label |
| 73 | + self.warning_label = grid.add( |
| 74 | + UILabel( |
| 75 | + text="Use [enter] to switch fields, then enter to login", |
| 76 | + width=150, |
| 77 | + font_size=10, |
| 78 | + font_name="Kenney Future", |
| 79 | + ), |
| 80 | + column=0, |
| 81 | + row=4, |
| 82 | + column_span=2, |
| 83 | + ) |
| 84 | + |
43 | 85 | anchor = UIAnchorLayout() # to center grid on screen
|
44 | 86 | anchor.add(grid)
|
45 | 87 |
|
46 |
| - self.ui.add(anchor) |
47 |
| - |
48 |
| - def on_login(self, event: UIOnClickEvent): |
49 |
| - print(f"User logged in with: {self.username_input.text} {self.password_input.text}") |
50 |
| - |
51 |
| - def on_show_view(self): |
52 |
| - self.window.background_color = arcade.color.DARK_BLUE_GRAY |
53 |
| - # Enable UIManager when view is shown to catch window events |
54 |
| - self.ui.enable() |
55 |
| - |
56 |
| - def on_hide_view(self): |
57 |
| - # Disable UIManager when view gets inactive |
58 |
| - self.ui.disable() |
59 |
| - |
60 |
| - def on_draw(self): |
61 |
| - self.clear() |
62 |
| - self.ui.draw() |
| 88 | + self.add_widget(anchor) |
| 89 | + |
| 90 | + # activate username input field |
| 91 | + self.username_input.activate() |
| 92 | + |
| 93 | + def on_key_press(self, symbol: int, modifiers: int) -> bool | None: |
| 94 | + # if username field active, switch fields with enter |
| 95 | + if self.username_input.active: |
| 96 | + if symbol == arcade.key.ENTER: |
| 97 | + self.username_input.deactivate() |
| 98 | + self.password_input.activate() |
| 99 | + return True |
| 100 | + # if password field active, login with enter |
| 101 | + elif self.password_input.active: |
| 102 | + if symbol == arcade.key.ENTER: |
| 103 | + self.password_input.deactivate() |
| 104 | + self.on_login(None) |
| 105 | + return True |
| 106 | + return False |
| 107 | + |
| 108 | + def on_login(self, event: UIOnClickEvent | None): |
| 109 | + username = self.username_input.text.strip() |
| 110 | + password = self.password_input.text.strip() |
| 111 | + print(f"User logged in with: {username} {password}") |
63 | 112 |
|
64 | 113 |
|
65 | 114 | if __name__ == "__main__":
|
|
0 commit comments