|
1
|
|
|
package de.pewpewproject.lasertag.client.screen; |
|
2
|
|
|
|
|
3
|
|
|
import de.pewpewproject.lasertag.client.screen.widget.*; |
|
4
|
|
|
import de.pewpewproject.lasertag.networking.NetworkingConstants; |
|
5
|
|
|
import io.netty.buffer.Unpooled; |
|
6
|
|
|
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; |
|
7
|
|
|
import net.minecraft.client.MinecraftClient; |
|
8
|
|
|
import net.minecraft.client.gui.Drawable; |
|
9
|
|
|
import net.minecraft.client.gui.screen.Screen; |
|
10
|
|
|
import net.minecraft.client.gui.widget.ButtonWidget; |
|
11
|
|
|
import net.minecraft.client.gui.widget.TextFieldWidget; |
|
12
|
|
|
import net.minecraft.entity.player.PlayerEntity; |
|
13
|
|
|
import net.minecraft.network.PacketByteBuf; |
|
14
|
|
|
import net.minecraft.text.Text; |
|
15
|
|
|
|
|
16
|
|
|
import java.util.ArrayList; |
|
17
|
|
|
import java.util.List; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The lasertag settings presets screen of the lasertag game manager |
|
21
|
|
|
* |
|
22
|
|
|
* @author Étienne Muser |
|
23
|
|
|
*/ |
|
24
|
|
|
public class LasertagGameManagerSettingsPresetsScreen extends GameManagerScreen { |
|
25
|
|
|
|
|
26
|
|
|
private ListWidget<String, String> list; |
|
27
|
|
|
private TextFieldWidget presetNameTextFieldWidget; |
|
28
|
|
|
|
|
29
|
|
|
public LasertagGameManagerSettingsPresetsScreen(Screen parent, PlayerEntity player) { |
|
30
|
|
|
super(parent, "gui.game_manager.settings.presets_screen_title", player); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Reload the lists data source |
|
35
|
|
|
*/ |
|
36
|
|
|
public void resetList() { |
|
37
|
|
|
this.list.refreshDataSource(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
@Override |
|
41
|
|
|
public boolean mouseScrolled(double mouseX, double mouseY, double amount) { |
|
42
|
|
|
// Forward the mouse scrolled event to the list |
|
43
|
|
|
return this.list.mouseScrolled(mouseX, mouseY, amount); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
@Override |
|
47
|
|
|
public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { |
|
48
|
|
|
// Forward the mouse dragged event to the list |
|
49
|
|
|
return this.list.mouseDragged(mouseX, mouseY, button, deltaX, deltaY); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
@Override |
|
53
|
|
|
public void tick() { |
|
54
|
|
|
// Forward the tick event to the preset name text field widget |
|
55
|
|
|
this.presetNameTextFieldWidget.tick(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
@Override |
|
59
|
|
|
protected void init() { |
|
60
|
|
|
super.init(); |
|
61
|
|
|
this.addAdditionalButtons(); |
|
62
|
|
|
|
|
63
|
|
|
var columns = new ArrayList<ListColumn<String, String>>(2); |
|
64
|
|
|
|
|
65
|
|
|
columns.add(new ListColumn<>(this::getPresetNameColumn, s -> s, 2)); |
|
66
|
|
|
columns.add(new ListColumn<>(this::getLoadColumn, s -> s, 1)); |
|
67
|
|
|
columns.add(new ListColumn<>(this::getDeleteColumn, s -> s, 1)); |
|
68
|
|
|
|
|
69
|
|
|
var columnsDefinition = new ListColumnsDefinition<>(columns); |
|
70
|
|
|
|
|
71
|
|
|
var availableHeight = this.height - (2 * verticalPadding + this.textRenderer.fontHeight + 2 * buttonPadding + buttonHeight); |
|
72
|
|
|
|
|
73
|
|
|
this.list = this.addDrawableChild(ListWidget.fromAvailableHeight(horizontalPadding, verticalPadding + textRenderer.fontHeight + buttonPadding, |
|
74
|
|
|
this.width - 2 * horizontalPadding, availableHeight, |
|
75
|
|
|
this::getPresetNames, |
|
76
|
|
|
columnsDefinition, this, this.textRenderer)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Add the preset name text field and save button widgets |
|
81
|
|
|
*/ |
|
82
|
|
|
private void addAdditionalButtons() { |
|
83
|
|
|
|
|
84
|
|
|
this.presetNameTextFieldWidget = this.addDrawableChild(new TextFieldWidget(this.textRenderer, horizontalPadding + 1, this.height - verticalPadding - buttonHeight + 1, buttonWidth - 1, buttonHeight - 2, Text.translatable("gui.game_manager.settings.presets.name"))); |
|
85
|
|
|
this.presetNameTextFieldWidget.setText("Unnamed_preset"); |
|
86
|
|
|
|
|
87
|
|
|
this.addDrawableChild(new ButtonWidget(horizontalPadding + buttonWidth + 4, this.height - verticalPadding - buttonHeight, buttonWidth / 2, buttonHeight, Text.translatable("gui.game_manager.settings.presets.save"), button -> { |
|
88
|
|
|
// Create packet buffer |
|
89
|
|
|
var buf = new PacketByteBuf(Unpooled.buffer()); |
|
90
|
|
|
|
|
91
|
|
|
// Get the preset name |
|
92
|
|
|
var presetName = this.presetNameTextFieldWidget.getText(); |
|
93
|
|
|
|
|
94
|
|
|
buf.writeString(presetName); |
|
95
|
|
|
|
|
96
|
|
|
ClientPlayNetworking.send(NetworkingConstants.CLIENT_TRIGGER_SAVE_PRESET, buf); |
|
97
|
|
|
|
|
98
|
|
|
this.presetNameTextFieldWidget.setText("Unnamed_preset"); |
|
99
|
|
|
})); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get the template for the preset name column of the list |
|
104
|
|
|
* |
|
105
|
|
|
* @param desc The cell description |
|
106
|
|
|
* @return The cell template |
|
107
|
|
|
*/ |
|
108
|
|
|
private Drawable getPresetNameColumn(ListCell<String> desc) { |
|
109
|
|
|
var startY = desc.y() + (desc.height() / 2) - (this.textRenderer.fontHeight / 2); |
|
110
|
|
|
return new LabelWidget(desc.x() + 5, startY, this.textRenderer, Text.literal(desc.value())); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get the template for the load button column of the list |
|
115
|
|
|
* |
|
116
|
|
|
* @param desc The cell description |
|
117
|
|
|
* @return The cell template |
|
118
|
|
|
*/ |
|
119
|
|
|
private Drawable getLoadColumn(ListCell<String> desc) { |
|
120
|
|
|
return new ButtonWidget(desc.x() + 1, desc.y() + 1, desc.width() - 2, desc.height() - 2, Text.translatable("gui.load"), button -> { |
|
121
|
|
|
// Create packet buffer |
|
122
|
|
|
var buf = new PacketByteBuf(Unpooled.buffer()); |
|
123
|
|
|
|
|
124
|
|
|
buf.writeString(desc.value()); |
|
125
|
|
|
|
|
126
|
|
|
ClientPlayNetworking.send(NetworkingConstants.CLIENT_TRIGGER_LOAD_PRESET, buf); |
|
127
|
|
|
}); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get the template for the delete button column of the list |
|
132
|
|
|
* |
|
133
|
|
|
* @param desc The cell description |
|
134
|
|
|
* @return The cell template |
|
135
|
|
|
*/ |
|
136
|
|
|
private Drawable getDeleteColumn(ListCell<String> desc) { |
|
137
|
|
|
|
|
138
|
|
|
return new ButtonWidget(desc.x() + 1, desc.y() + 1, desc.width() - 2, desc.height() - 2, Text.translatable("gui.delete"), button -> { |
|
139
|
|
|
// Create packet buffer |
|
140
|
|
|
var buf = new PacketByteBuf(Unpooled.buffer()); |
|
141
|
|
|
|
|
142
|
|
|
buf.writeString(desc.value()); |
|
143
|
|
|
|
|
144
|
|
|
ClientPlayNetworking.send(NetworkingConstants.CLIENT_TRIGGER_DELETE_PRESET, buf); |
|
145
|
|
|
}); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* The data source for the list |
|
150
|
|
|
* |
|
151
|
|
|
* @return The preset names |
|
152
|
|
|
*/ |
|
153
|
|
|
private List<String> getPresetNames() { |
|
154
|
|
|
|
|
155
|
|
|
// Get the game managers |
|
156
|
|
|
var gameManager = MinecraftClient.getInstance().world.getClientLasertagManager(); |
|
157
|
|
|
var settingsPresetsNamesManager = gameManager.getSettingsPresetsNameManager(); |
|
158
|
|
|
|
|
159
|
|
|
return new ArrayList<>(settingsPresetsNamesManager.getSettingsPresetNames()); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|