Skip to content

Commit 86e12a3

Browse files
committed
Add checkbox, macro checklist provider (#73)
1 parent 17fec4a commit 86e12a3

File tree

15 files changed

+344
-76
lines changed

15 files changed

+344
-76
lines changed

src/main/java/com/ldtteam/blockui/Loader.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import net.minecraft.util.profiling.ProfilerFiller;
1010
import org.w3c.dom.Document;
1111
import org.xml.sax.SAXException;
12+
13+
import javax.xml.parsers.DocumentBuilder;
14+
import javax.xml.parsers.DocumentBuilderFactory;
15+
import javax.xml.parsers.ParserConfigurationException;
1216
import java.io.IOException;
1317
import java.io.InputStream;
1418
import java.util.HashMap;
1519
import java.util.Map;
1620
import java.util.function.Function;
17-
import javax.xml.parsers.DocumentBuilder;
18-
import javax.xml.parsers.DocumentBuilderFactory;
19-
import javax.xml.parsers.ParserConfigurationException;
2021

2122
/**
2223
* Utilities to load xml files.
@@ -49,6 +50,7 @@ private Loader()
4950
register("overlay", OverlayView::new);
5051
register("gradient", Gradient::new);
5152
register("zoomdragview", ZoomDragView::new);
53+
register("checkbox", CheckBox::new);
5254
}
5355

5456
/**

src/main/java/com/ldtteam/blockui/PaneParams.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,24 +196,25 @@ public String getString(final String name, final String def)
196196
/**
197197
* Get the resource location from the name
198198
* @param name the attribute name
199-
* @param def the default value to fallback to
200199
* @return the parsed resource location
201200
*/
202201
@Nullable
203202
public ResourceLocation getResource(final String name)
204203
{
205-
return getProperty(name, Parsers.RESOURCE, null);
204+
return getResource(name, (ResourceLocation) null);
206205
}
207206

208207
/**
209208
* Get the resource location from the name
210209
* @param name the attribute name
211210
* @param def the default value to fallback to
212211
* @return the parsed resource location
212+
* @deprecated use {@link PaneParams#getResource(String, ResourceLocation)} instead.
213213
*/
214+
@Deprecated(forRemoval = true, since = "1.20.1")
214215
public ResourceLocation getResource(final String name, final String def)
215216
{
216-
return getProperty(name, Parsers.RESOURCE, new ResourceLocation(def));
217+
return getResource(name, new ResourceLocation(def));
217218
}
218219

219220
/**
@@ -236,8 +237,8 @@ public ResourceLocation getResource(final String name, final ResourceLocation de
236237
@Nullable
237238
public ResourceLocation getResource(final String name, final Consumer<ResourceLocation> loader)
238239
{
239-
ResourceLocation rl = getResource(name, "");
240-
if (!rl.getPath().isEmpty())
240+
final ResourceLocation rl = getResource(name);
241+
if (rl != null && !rl.getPath().isEmpty())
241242
{
242243
loader.accept(rl);
243244
return rl;
@@ -267,7 +268,7 @@ public List<MutableComponent> getMultilineText(final String name, List<MutableCo
267268
{
268269
return getProperty(name, Parsers.MULTILINE, def);
269270
}
270-
271+
271272
/**
272273
* Get the localized String attribute from the name and revert to the default if not present.
273274
*

src/main/java/com/ldtteam/blockui/controls/Button.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public void setHandler(final ButtonHandler h)
7676
handler = h;
7777
}
7878

79+
/**
80+
* @return true if any handler is present
81+
*/
82+
public boolean hasHandler()
83+
{
84+
return handler != null;
85+
}
86+
7987
/**
8088
* Play click sound and find the proper handler.
8189
*

src/main/java/com/ldtteam/blockui/controls/ButtonImage.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
import com.mojang.blaze3d.systems.RenderSystem;
99
import net.minecraft.client.gui.components.AbstractButton;
1010
import net.minecraft.client.gui.components.WidgetSprites;
11+
import com.ldtteam.blockui.util.records.SizeI;
12+
import com.mojang.blaze3d.vertex.PoseStack;
1113
import net.minecraft.client.renderer.texture.MissingTextureAtlasSprite;
1214
import net.minecraft.resources.ResourceLocation;
13-
14-
import java.util.Objects;
15-
1615
import net.minecraft.util.Mth;
1716
import net.neoforged.fml.loading.FMLEnvironment;
1817

18+
import java.util.Objects;
19+
1920
/**
2021
* Clickable image.
2122
*/
@@ -310,13 +311,34 @@ public void drawSelf(final BOGuiGraphics target, final double mx, final double m
310311
RenderSystem.defaultBlendFunc();
311312

312313
resolvedTextures.getAndPrepare(isEnabled(), wasCursorInPane).blit(target.pose(), x, y, width, height);
314+
postDrawBackground(ms, bind, x, y, width, height, u, v, w, h, mapWidth, mapHeight);
313315

314316
RenderSystem.disableBlend();
315317
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
316318

317319
super.drawSelf(target, mx, my);
318320
}
319321

322+
/**
323+
* Called after drawing the button.
324+
*/
325+
public void postDrawBackground(
326+
final PoseStack ms,
327+
final ResourceLocation image,
328+
final int x,
329+
final int y,
330+
final int width,
331+
final int height,
332+
final int u,
333+
final int v,
334+
final int w,
335+
final int h,
336+
final int mapWidth,
337+
final int mapHeight)
338+
{
339+
// No-op
340+
}
341+
320342
@Override
321343
public void setSize(final int w, final int h)
322344
{
@@ -356,4 +378,4 @@ public void setTextRenderBox(final int textWidth, final int textHeight)
356378
this.textHeight = Mth.clamp(textHeight, 0, height - textOffsetY);
357379
recalcTextRendering();
358380
}
359-
}
381+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.ldtteam.blockui.controls;
2+
3+
import com.ldtteam.blockui.PaneParams;
4+
import com.mojang.blaze3d.vertex.PoseStack;
5+
import net.minecraft.resources.ResourceLocation;
6+
7+
/**
8+
* Checkbox used for toggling a checkmark on and off.
9+
*/
10+
public class CheckBox extends ButtonImage
11+
{
12+
/**
13+
* The image for the checkmark to render over the button.
14+
*/
15+
protected ResourceLocation checkmarkImage;
16+
17+
/**
18+
* Whether the button is checked or not.
19+
*/
20+
private boolean checked = false;
21+
22+
/**
23+
* Default constructor. Makes a small square button.
24+
*/
25+
public CheckBox()
26+
{
27+
super();
28+
}
29+
30+
/**
31+
* Constructor called by the xml loader.
32+
*
33+
* @param params PaneParams provided in the xml.
34+
*/
35+
public CheckBox(final PaneParams params)
36+
{
37+
super(params);
38+
39+
checkmarkImage = params.getResource("checkmark");
40+
}
41+
42+
@Override
43+
public boolean handleClick(final double mx, final double my)
44+
{
45+
checked = !checked;
46+
super.handleClick(mx, my);
47+
return true;
48+
}
49+
50+
/**
51+
* Set the checkmark image.
52+
*
53+
* @param loc ResourceLocation for the checkmark.
54+
*/
55+
public void setCheckmarkImage(final ResourceLocation loc)
56+
{
57+
this.checkmarkImage = loc;
58+
}
59+
60+
@Override
61+
public void postDrawBackground(
62+
final PoseStack ms,
63+
final ResourceLocation image,
64+
final int x,
65+
final int y,
66+
final int width,
67+
final int height,
68+
final int u,
69+
final int v,
70+
final int w,
71+
final int h,
72+
final int mapWidth,
73+
final int mapHeight)
74+
{
75+
if (checked)
76+
{
77+
blit(ms, checkmarkImage, x, y, width, height, u, v, w, h, mapWidth, mapHeight);
78+
}
79+
}
80+
81+
/**
82+
* Get if the checkbox is currently checked or not.
83+
*
84+
* @return true if so.
85+
*/
86+
public boolean isChecked()
87+
{
88+
return checked;
89+
}
90+
91+
/**
92+
* Set whether the checkbox is checked or not.
93+
*
94+
* @param checked the checked state.
95+
*/
96+
public void setChecked(final boolean checked)
97+
{
98+
this.checked = checked;
99+
}
100+
}

src/main/java/com/ldtteam/blockui/mod/ClientEventSubscriber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static void onClientTickEvent(final ClientTickEvent event)
7777
}));
7878
window.addChild(createTestGuiButton(id++, "Tooltip Positioning", new ResourceLocation(BlockUI.MOD_ID, "gui/test2.xml")));
7979
window.addChild(createTestGuiButton(id++, "ItemIcon To BlockState", new ResourceLocation(BlockUI.MOD_ID, "gui/test3.xml"), BlockStateTestGui::setup));
80-
window.addChild(createTestGuiButton(id++, "Dynamic ScrollingLists", new ResourceLocation(BlockUI.MOD_ID, "gui/test4.xml"), DynamicScrollingListGui::setup));
80+
window.addChild(createTestGuiButton(id++, "Scrolling Lists", new ResourceLocation(BlockUI.MOD_ID, "gui/test4.xml"), ScrollingListsGui::setup));
8181
window.open();
8282
}
8383
}

src/main/java/com/ldtteam/blockui/mod/DynamicScrollingListGui.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)