1
|
|
|
package de.pewpewproject.lasertag.client.screen; |
2
|
|
|
|
3
|
|
|
import net.minecraft.client.gui.Drawable; |
4
|
|
|
import net.minecraft.client.gui.DrawableHelper; |
5
|
|
|
import net.minecraft.client.gui.Element; |
6
|
|
|
import net.minecraft.client.gui.Selectable; |
7
|
|
|
import net.minecraft.client.gui.screen.Screen; |
8
|
|
|
import net.minecraft.client.gui.widget.ButtonWidget; |
9
|
|
|
import net.minecraft.client.util.math.MatrixStack; |
10
|
|
|
import net.minecraft.entity.player.PlayerEntity; |
11
|
|
|
import net.minecraft.text.Text; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Base class for screens in the lasertag game manager gui |
15
|
|
|
* |
16
|
|
|
* @author Étienne Muser |
17
|
|
|
*/ |
18
|
|
|
public abstract class GameManagerScreen extends Screen { |
19
|
|
|
|
20
|
|
|
private int numberOfListedButtons = 0; |
21
|
|
|
|
22
|
|
|
private final Screen parent; |
23
|
|
|
protected final PlayerEntity player; |
24
|
|
|
|
25
|
|
|
protected static final int horizontalPadding = 20; |
26
|
|
|
protected static final int verticalPadding = 15; |
27
|
|
|
protected static final int buttonPadding = 10; |
28
|
|
|
protected static final int buttonHeight = 20; |
29
|
|
|
protected static final int buttonWidth = 200; |
30
|
|
|
protected static final int textColor = 0xFFFFFFFF; |
31
|
|
|
|
32
|
|
|
public GameManagerScreen(Screen parent, String translatableTitle, PlayerEntity player) { |
33
|
|
|
super(Text.translatable(translatableTitle)); |
34
|
|
|
|
35
|
|
|
this.parent = parent; |
36
|
|
|
this.player = player; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public void close() { |
40
|
|
|
this.client.setScreen(this.parent); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
@Override |
44
|
|
|
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { |
45
|
|
|
|
46
|
|
|
this.drawBackground(matrices); |
47
|
|
|
this.textRenderer.drawWithShadow(matrices, title, horizontalPadding, verticalPadding, textColor); |
48
|
|
|
DrawableHelper.fill(matrices, horizontalPadding, verticalPadding + this.textRenderer.fontHeight + 1, this.width - horizontalPadding, verticalPadding + this.textRenderer.fontHeight + 2, 0xFFFFFFFF); |
49
|
|
|
|
50
|
|
|
super.render(matrices, mouseX, mouseY, delta); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The init method of the screen. Override to add more gui elements. Must call super.init() first! |
55
|
|
|
*/ |
56
|
|
|
@Override |
57
|
|
|
protected void init() { |
58
|
|
|
numberOfListedButtons = 0; |
59
|
|
|
|
60
|
|
|
addBackButton(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Adds a button to the main button list |
65
|
|
|
* |
66
|
|
|
* @param translatableName The button text as the translatable key |
67
|
|
|
* @param pressAction The button click callback |
68
|
|
|
* @return The created button widget |
69
|
|
|
*/ |
70
|
|
|
protected ButtonWidget addListedButton(String translatableName, ButtonWidget.PressAction pressAction) { |
71
|
|
|
|
72
|
|
|
var button = this.addDrawableChild(new ButtonWidget(horizontalPadding, verticalPadding + textRenderer.fontHeight + buttonPadding + numberOfListedButtons * (buttonHeight + buttonPadding), buttonWidth, buttonHeight, Text.translatable(translatableName), pressAction)); |
73
|
|
|
|
74
|
|
|
++numberOfListedButtons; |
75
|
|
|
|
76
|
|
|
return button; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Adds a widget to the main button list |
81
|
|
|
* |
82
|
|
|
* @param buttonGenerator Method creating the button |
83
|
|
|
* @param <T> The type of the widget |
84
|
|
|
* @return The created widget |
85
|
|
|
*/ |
86
|
|
|
protected <T extends Element & Drawable & Selectable> T addListedWidget(IListedButtonGenerator<T> buttonGenerator) { |
87
|
|
|
var button = this.addDrawableChild(buttonGenerator.generate(horizontalPadding, verticalPadding + textRenderer.fontHeight + buttonPadding + numberOfListedButtons * (buttonHeight + buttonPadding), buttonWidth, buttonHeight)); |
88
|
|
|
|
89
|
|
|
++numberOfListedButtons; |
90
|
|
|
|
91
|
|
|
return button; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Functional interface for generating a widget for <code>addListedWidget</code> |
96
|
|
|
* @param <T> The type of the widget to be generated |
97
|
|
|
*/ |
98
|
|
|
protected interface IListedButtonGenerator<T extends Element & Drawable & Selectable> { |
99
|
|
|
T generate(int x, int y, int width, int height); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private void addBackButton() { |
103
|
|
|
|
104
|
|
|
var buttonTranslatableText = this.parent == null ? "gui.exit" : "gui.back"; |
105
|
|
|
|
106
|
|
|
this.addDrawableChild(new ButtonWidget(this.width - horizontalPadding - (buttonWidth / 2), this.height - verticalPadding - buttonHeight, buttonWidth / 2, buttonHeight, Text.translatable(buttonTranslatableText), button -> { |
107
|
|
|
this.close(); |
108
|
|
|
})); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private void drawBackground(MatrixStack matrices) { |
112
|
|
|
DrawableHelper.fill(matrices, 0, 0, this.width, this.height, 0x80000000); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|