|
| 1 | +# Create Your Own Widget |
| 2 | + |
| 3 | +You cannot only use the Widgets we created, but you can also create your own ones. |
| 4 | +This small example will show you how to create your first Widget and add it to your Activity. |
| 5 | +The result of our little example will be a widget that displays a player's head to the left and a player's name. |
| 6 | +This Widget will be applied in a vertical list with multiple entries. |
| 7 | + |
| 8 | +## Basics |
| 9 | + |
| 10 | +First of all, every Widget is a subclass of `AbstractWidget`, you can also extend other widgets like |
| 11 | +for example the `ButtonWidget` or the `VerticalListWidget` to get some basic functionality. The `AutoWidget` annotation |
| 12 | +is necessary on every Widget. |
| 13 | + |
| 14 | +### Generics |
| 15 | + |
| 16 | +The generic type of the AbstractWidget (`AbstractWidget<...>`) defines the type of the children of this widget. |
| 17 | +If you want your widget to have children of only a specific type, you can define it here, otherwise just use `Widget`. |
| 18 | +In reality this is used for stuff like this: |
| 19 | +```java |
| 20 | +public class DropdownListWidget<T> extends VerticalListWidget<DropdownEntryWidget<T>> {} |
| 21 | +``` |
| 22 | + |
| 23 | +### User interaction / Custom rendering |
| 24 | + |
| 25 | +=== ":octicons-file-code-16: ExampleWidget" |
| 26 | + ```java |
| 27 | + @AutoWidget |
| 28 | + public class ExampleWidget extends AbstractWidget<Widget> { |
| 29 | + |
| 30 | + @Override |
| 31 | + public void renderWidget(ScreenContext context) { |
| 32 | + if (this.isVisible()) { |
| 33 | + // optional: custom rendering |
| 34 | + } |
| 35 | + |
| 36 | + super.renderWidget(context); // Render children |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public boolean mouseClicked(MutableMouse mouse, MouseButton mouseButton) { |
| 41 | + // optional: handle mouse interactions |
| 42 | + return super.mouseClicked(mouse, mouseButton); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public boolean keyPressed(Key key, InputType type) { |
| 47 | + // optional: handle key press |
| 48 | + return super.keyPressed(key, type); |
| 49 | + } |
| 50 | + |
| 51 | + /* |
| 52 | + There are more methods that you can override for user input like: |
| 53 | + - mouseDragged(...) |
| 54 | + - mouseScrolled(...) |
| 55 | + - mouseReleased(...) |
| 56 | + - keyReleased(...) |
| 57 | + - fileDropped(...) -> there may be limitations in older Minecraft versions, for file interactions you should always provide an optional file chooser (e.g. FileChooserWidget) |
| 58 | + */ |
| 59 | + } |
| 60 | + ``` |
| 61 | + |
| 62 | +### Children / Custom style |
| 63 | + |
| 64 | +Just like Activities, Widgets can also have children and styles. In your custom widget, you can add children in the initialize() method just like |
| 65 | +you can do it in the Activity. |
| 66 | + |
| 67 | +=== ":octicons-file-code-16: UserListActivity" |
| 68 | + ```java |
| 69 | + @AutoActivity |
| 70 | + @Link("user-list-activity.lss") |
| 71 | + public class UserListActivity extends SimpleActivity { |
| 72 | + |
| 73 | + @Override |
| 74 | + public void initialize(Parent parent) { |
| 75 | + super.initialize(parent); |
| 76 | + |
| 77 | + VerticalListWidget<UserWidget> users = new VerticalListWidget<>(); |
| 78 | + users.addId("users"); |
| 79 | + this.document().addChild(users); |
| 80 | + |
| 81 | + users.addChild(new UserWidget("LabyMod")); |
| 82 | + users.addChild(new UserWidget("LabyStudio")); |
| 83 | + users.addChild(new UserWidget("LabyMarco")); |
| 84 | + |
| 85 | + ButtonWidget addButton = ButtonWidget.text("Add User"); |
| 86 | + addButton.setPressable(() -> { |
| 87 | + // addChildInitialized can be used to add children to a widget |
| 88 | + // after the initialize() method has already been called |
| 89 | + users.addChildInitialized(new UserWidget("Test")); |
| 90 | + |
| 91 | + // Keep in mind that the VerticalListWidget users will get recreated when the Activity is being initialized again. |
| 92 | + // This means that for example when resizing the window all "Test" users will be removed again, you can |
| 93 | + // prevent this by saving the usernames in a separate list and adding to this list |
| 94 | + }); |
| 95 | + addButton.addId("add-button"); |
| 96 | + this.document().addChild(addButton); |
| 97 | + } |
| 98 | + } |
| 99 | + ``` |
| 100 | + |
| 101 | +=== ":octicons-file-code-16: user-list-activity.lss" |
| 102 | + ```css |
| 103 | + .users { |
| 104 | + left: 50%; |
| 105 | + alignment-x: center; |
| 106 | + top: 30; |
| 107 | + |
| 108 | + width: fit-content; |
| 109 | + height: fit-content; |
| 110 | + |
| 111 | + User { |
| 112 | + height: fit-content; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + .add-button { |
| 117 | + left: 50%; |
| 118 | + alignment-x: center; |
| 119 | + top: 10; |
| 120 | + |
| 121 | + width: 50; |
| 122 | + height: 20; |
| 123 | + } |
| 124 | + ``` |
| 125 | + |
| 126 | +=== ":octicons-file-code-16: UserWidget" |
| 127 | + ```java |
| 128 | + @AutoWidget |
| 129 | + @Link("user-widget.lss") |
| 130 | + public class UserWidget extends AbstractWidget<Widget> { |
| 131 | + |
| 132 | + private final String username; |
| 133 | + |
| 134 | + public UserWidget(String username) { |
| 135 | + this.username = username; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void initialize(Parent parent) { |
| 140 | + super.initialize(parent); |
| 141 | + |
| 142 | + IconWidget head = new IconWidget(Icon.head(this.username)); |
| 143 | + head.addId("head"); |
| 144 | + this.addChild(head); |
| 145 | + |
| 146 | + ComponentWidget username = ComponentWidget.text(this.username); |
| 147 | + username.addId("username"); |
| 148 | + this.addChild(username); |
| 149 | + } |
| 150 | + } |
| 151 | + ``` |
| 152 | + |
| 153 | +=== ":octicons-file-code-16: user-widget.lss" |
| 154 | + ```css |
| 155 | + .head { |
| 156 | + // align left |
| 157 | + left: 0; |
| 158 | + |
| 159 | + // center vertically |
| 160 | + top: 50%; |
| 161 | + alignment-y: center; |
| 162 | + |
| 163 | + width: 12; |
| 164 | + height: 12; |
| 165 | + } |
| 166 | + |
| 167 | + .username { |
| 168 | + // align next to the head |
| 169 | + left: 14; |
| 170 | + |
| 171 | + // center vertically |
| 172 | + top: 50%; |
| 173 | + alignment-y: center; |
| 174 | + |
| 175 | + // width/height is set automatically for a ComponentWidget |
| 176 | + } |
| 177 | + ``` |
| 178 | + |
| 179 | +=== ":octicons-file-media-24: Result" |
| 180 | +  |
0 commit comments