Skip to content

Commit dc1c445

Browse files
committed
🚧 widgets 2,,,,,,,,,,,
1 parent 8d50c43 commit dc1c445

5 files changed

Lines changed: 54 additions & 34 deletions

File tree

common/src/main/java/org/modogthedev/superposition/blockentity/PanelBlockEntity.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries)
162162
}
163163
}
164164
} else {
165-
for (int i = 0; i < widgets.size(); i++) {
165+
for (int i = 0; i < size; i++) {
166166
CompoundTag widgetTag = widgetListTag.getCompound(i);
167167
widgets.get(i).read(widgetTag);
168168
}
@@ -181,11 +181,10 @@ protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries)
181181

182182
@Override
183183
public void loadSyncedData(CompoundTag tag) {
184-
super.loadSyncedData(tag);
185-
for (int i = 0; i < widgets.size(); i++) {
186-
if (tag.contains("widget-"+i)) {
187-
CompoundTag widgetTag = tag.getCompound("widget-" + i);
188-
widgets.get(i).loadSyncedData(widgetTag);
184+
for (Widget widget : widgets) {
185+
if (tag.contains("widget-" + widget.getUuid())) {
186+
CompoundTag widgetTag = tag.getCompound("widget-" + widget.getUuid());
187+
widget.loadSyncedData(widgetTag);
189188
}
190189
}
191190
if (tag.contains("front_height")) {
@@ -200,6 +199,7 @@ public void loadSyncedData(CompoundTag tag) {
200199
updateHeight(true);
201200
updateHeight(false);
202201
rebuild();
202+
super.loadSyncedData(tag);
203203
}
204204

205205
@Override
@@ -212,12 +212,12 @@ public void setupConfigTooltips(Player player) {
212212
super.setupConfigTooltips(player);
213213
if (player == null) {
214214
for (int i = 0; i < widgets.size(); i++) {
215-
widgets.get(i).addConfiguration(this, i, player);
215+
widgets.get(i).addConfiguration(this, player);
216216
}
217217
} else if (lastTargeted != null) {
218218
for (int i = 0; i < widgets.size(); i++) {
219219
if (widgets.get(i).equals(lastTargeted)) {
220-
lastTargeted.addConfiguration(this, i, player);
220+
lastTargeted.addConfiguration(this, player);
221221
}
222222
}
223223
lastTargeted = null;

common/src/main/java/org/modogthedev/superposition/client/renderer/ui/SuperpositionUITooltipRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class SuperpositionUITooltipRenderer {
5353
public static boolean selected;
5454

5555
public static void keyPress(long windowPointer, int key, int scanCode, int action, int modifiers) {
56-
if (editableTooltip != null && action != GLFW.GLFW_RELEASE) {
56+
if (editableTooltip != null && editingEditable && action != GLFW.GLFW_RELEASE) {
5757
cursorPos = Mth.clamp(cursorPos, 0, editableTooltip.getText().length());
5858
switch (key) {
5959
case 256 -> {

common/src/main/java/org/modogthedev/superposition/system/widget/Widget.java

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.HashMap;
1919
import java.util.List;
2020
import java.util.Map;
21+
import java.util.UUID;
2122
import java.util.function.Consumer;
2223
import java.util.function.Supplier;
2324

@@ -26,6 +27,7 @@ public class Widget implements Cloneable {
2627
private String name = "deviceName";
2728
private String color = "0xff3333";
2829
private Map<String,Consumer<String>> editable = new HashMap<>();
30+
private UUID uuid = UUID.randomUUID();
2931

3032
private Vector2i position = new Vector2i();
3133

@@ -127,6 +129,7 @@ public void write(CompoundTag tag) {
127129
tag.putInt("x", position.x);
128130
tag.putInt("y", position.y);
129131
tag.putString("color", color);
132+
tag.putUUID("uuid", uuid);
130133
}
131134

132135
/**
@@ -142,23 +145,15 @@ public void read(CompoundTag tag) {
142145
if (tag.contains("x")) {
143146
position.set(tag.getInt("x"), tag.getInt("y"));
144147
}
145-
}
146-
147-
/**
148-
* Load data on the client that was sent from a client to modify this widget.
149-
*/
150-
public void loadSyncedData(CompoundTag tag) {
151-
for (String key : editable.keySet()) {
152-
if (tag.contains(key)) {
153-
editable.get(key).accept(tag.getString(key));
154-
}
148+
if (tag.contains("uuid")) {
149+
uuid = tag.getUUID("uuid");
155150
}
156151
}
152+
157153
public Color getColor(Color color) {
158154
Color widgetColor = getColor();
159155
return new Color((widgetColor.getRed()/255f) * (color.getRed()/255f), (widgetColor.getGreen()/255f) * (color.getGreen()/255f), (widgetColor.getBlue()/255f) * (color.getBlue()/255f), (widgetColor.getAlpha()/255f) * (color.getAlpha()/255f));
160156
}
161-
162157
private Color getColor() {
163158
try {
164159
int colorI = NumberUtils.createNumber(this.color).intValue();
@@ -168,27 +163,57 @@ private Color getColor() {
168163
return new Color(1f, 1f, 1f, 1f);
169164
}
170165

171-
public void addConfiguration(PanelBlockEntity panel, int index, Player player) {
172-
addEditable(panel, "Name", index, () -> name, (s -> this.name = s));
173-
addEditable(panel,"Color", index, () -> color, (s -> color = s));
166+
/**
167+
* Load data on the client that was sent from a client to modify this widget.
168+
*/
169+
public void loadSyncedData(CompoundTag tag) {
170+
for (String key : editable.keySet()) {
171+
if (tag.contains(key)) {
172+
editable.get(key).accept(tag.getString(key));
173+
}
174+
}
174175
}
175176

177+
public void addConfiguration(PanelBlockEntity panel, Player player) {
178+
addEditable(panel, "Name", () -> name, this::setName);
179+
addEditable(panel,"Color", () -> color, this::setColor);
180+
}
176181

177-
public void addEditable(PanelBlockEntity panel, String name, int index, Supplier<String> read, Consumer<String> set) {
178-
panel.addEditableTaggedConfigTooltip(name, "widget-" + index, read, set);
182+
/**
183+
* Adds an editable field on the widget.
184+
* @param panel The panel block entity the widget is attached to.
185+
* @param name This name is used both for display and for the tag key.
186+
* @param read Retrieves the fields value.
187+
* @param set Sets the field value. Note that values are final in a lambda so you must call a method to update the widgets fields. You can also use a method reference.
188+
*/
189+
public void addEditable(PanelBlockEntity panel, String name, Supplier<String> read, Consumer<String> set) {
190+
panel.addEditableTaggedConfigTooltip(name, "widget-" + uuid, read, set);
179191
editable.put(name,set);
180192
}
181193

194+
protected void setName(String name) {
195+
this.name = name;
196+
}
197+
198+
protected void setColor(String color) {
199+
this.color = color;
200+
}
201+
182202
@Override
183203
protected Object clone() throws CloneNotSupportedException {
184204
getLocation();
185205
return super.clone();
186206
}
187207

208+
public UUID getUuid() {
209+
return uuid;
210+
}
211+
188212
public Widget makeClone() {
189213
try {
190214
Widget clone = (Widget) clone();
191215
clone.position = (Vector2i) position.clone();
216+
clone.uuid = UUID.randomUUID();
192217
return clone;
193218
} catch (CloneNotSupportedException ignored) {
194219

common/src/main/java/org/modogthedev/superposition/system/widget/widgets/ButtonWidget.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import net.minecraft.nbt.CompoundTag;
44
import net.minecraft.util.Mth;
5-
import net.minecraft.world.entity.player.Player;
65
import net.minecraft.world.level.Level;
76
import org.joml.Vector3f;
87
import org.modogthedev.superposition.blockentity.PanelBlockEntity;
@@ -64,10 +63,6 @@ public void read(CompoundTag tag) {
6463
}
6564
}
6665

67-
@Override
68-
public void addConfiguration(PanelBlockEntity panel, int index, Player player) {
69-
super.addConfiguration(panel, index, player);
70-
}
7166

7267
@Override
7368
public Vector3f getBounds() {

common/src/main/java/org/modogthedev/superposition/system/widget/widgets/GaugeWidget.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ public void read(CompoundTag tag) {
5959
}
6060

6161
@Override
62-
public void addConfiguration(PanelBlockEntity panel, int index, Player player) {
63-
super.addConfiguration(panel, index, player);
64-
addEditable(panel,"Minimum",index,() -> String.valueOf(minimum),(s -> {
62+
public void addConfiguration(PanelBlockEntity panel, Player player) {
63+
super.addConfiguration(panel, player);
64+
addEditable(panel,"Minimum",() -> String.valueOf(minimum),(s -> {
6565
try {
6666
minimum = Float.parseFloat(s);
6767
} catch (NumberFormatException ignored) {}
6868
}
6969
));
7070

71-
addEditable(panel,"Maximum",index,() -> String.valueOf(maximum),(s -> {
71+
addEditable(panel,"Maximum",() -> String.valueOf(maximum),(s -> {
7272
try {
7373
maximum = Float.parseFloat(s);
7474
} catch (NumberFormatException ignored) {}

0 commit comments

Comments
 (0)