Skip to content

Commit ff65a02

Browse files
authored
Allow scrolling list items to have a dynamic size (#66)
* Allow scrolling list items to have a dynamic size, second edition of #59
1 parent 76b6fef commit ff65a02

File tree

8 files changed

+309
-38
lines changed

8 files changed

+309
-38
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public Pane(final PaneParams params)
7676
height = a.get(1);
7777
});
7878

79-
params.getScaledInteger("pos", params.getParentView().x, params.getParentView().y, a -> {
79+
params.getScaledInteger("pos", params.getParentLeft(), params.getParentTop(), a -> {
8080
x = a.get(0);
8181
y = a.get(1);
8282
});

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,76 @@ public PaneParams(final Node n)
3333
children = new ArrayList<>(node.getChildNodes().getLength());
3434
}
3535

36+
/**
37+
* Get the node type.
38+
*
39+
* @return the name of the node.
40+
*/
3641
public String getType()
3742
{
3843
return node.getNodeName();
3944
}
4045

46+
/**
47+
* Get the parent for this pane.
48+
*
49+
* @return the parent.
50+
*/
4151
public View getParentView()
4252
{
4353
return parentView;
4454
}
4555

56+
/**
57+
* Set the parent for this pane.
58+
*
59+
* @param parent the new parent.
60+
*/
4661
public void setParentView(final View parent)
4762
{
4863
parentView = parent;
4964
}
5065

66+
/**
67+
* Get the width of the parent, if any. Defaults to 0 if no parent has been set.
68+
*
69+
* @return the width.
70+
*/
5171
public int getParentWidth()
5272
{
5373
return parentView != null ? parentView.getInteriorWidth() : 0;
5474
}
5575

76+
/**
77+
* Get the height of the parent, if any. Defaults to 0 if no parent has been set.
78+
*
79+
* @return the height.
80+
*/
5681
public int getParentHeight()
5782
{
5883
return parentView != null ? parentView.getInteriorHeight() : 0;
5984
}
6085

86+
/**
87+
* Get the left position of the parent, if any. Defaults to 0 if no parent has been set.
88+
*
89+
* @return the left position.
90+
*/
91+
public int getParentLeft()
92+
{
93+
return parentView != null ? parentView.x : 0;
94+
}
95+
96+
/**
97+
* Get the top position of the parent, if any. Defaults to 0 if no parent has been set.
98+
*
99+
* @return the top position.
100+
*/
101+
public int getParentTop()
102+
{
103+
return parentView != null ? parentView.y : 0;
104+
}
105+
61106
public List<PaneParams> getChildren()
62107
{
63108
if (!children.isEmpty()) return children;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public static void onClientTickEvent(final ClientTickEvent event)
6666
}));
6767
window.addChild(createTestGuiButton(1, "Tooltip Positioning", new ResourceLocation(BlockUI.MOD_ID, "gui/test2.xml")));
6868
window.addChild(createTestGuiButton(2, "ItemIcon To BlockState", new ResourceLocation(BlockUI.MOD_ID, "gui/test3.xml"), BlockStateTestGui::setup));
69+
window.addChild(createTestGuiButton(3, "Dynamic ScrollingLists", new ResourceLocation(BlockUI.MOD_ID, "gui/test4.xml"), DynamicScrollingListGui::setup));
6970
window.open();
7071
}
7172
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.ldtteam.blockui.mod;
2+
3+
import com.ldtteam.blockui.Pane;
4+
import com.ldtteam.blockui.controls.Text;
5+
import com.ldtteam.blockui.views.BOWindow;
6+
import com.ldtteam.blockui.views.ScrollingList;
7+
import com.ldtteam.blockui.views.ScrollingListContainer.RowSizeModifier;
8+
import net.minecraft.network.chat.Component;
9+
10+
/**
11+
* Sets up gui for dynamic scrolling lists.
12+
*/
13+
public class DynamicScrollingListGui
14+
{
15+
public static void setup(final BOWindow window)
16+
{
17+
final ScrollingList list1 = window.findPaneOfTypeByID("list1", ScrollingList.class);
18+
list1.setDataProvider(new ScrollingList.DataProvider()
19+
{
20+
@Override
21+
public int getElementCount()
22+
{
23+
return 10;
24+
}
25+
26+
@Override
27+
public void updateElement(final int index, final Pane rowPane)
28+
{
29+
rowPane.findPaneByType(Text.class).setText(Component.literal("Hi " + index));
30+
}
31+
});
32+
33+
final ScrollingList list2 = window.findPaneOfTypeByID("list2", ScrollingList.class);
34+
list2.setDataProvider(new ScrollingList.DataProvider()
35+
{
36+
@Override
37+
public int getElementCount()
38+
{
39+
return 20;
40+
}
41+
42+
@Override
43+
public void modifyRowSize(final int index, final RowSizeModifier modifier)
44+
{
45+
if (index % 2 == 0)
46+
{
47+
modifier.setSize(100, 40);
48+
}
49+
}
50+
51+
@Override
52+
public void updateElement(final int index, final Pane rowPane)
53+
{
54+
rowPane.findPaneByType(Text.class).setText(Component.literal("Hi " + index));
55+
}
56+
});
57+
}
58+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.ldtteam.blockui.util;
2+
3+
import com.ldtteam.blockui.mod.Log;
4+
import net.minecraftforge.fml.loading.FMLEnvironment;
5+
6+
/**
7+
* Utility class for throwing errors which is safe during production.
8+
*/
9+
public class SafeError
10+
{
11+
/**
12+
* Safe error throw call that only throws an exception during development, but logs an error in production instead so no crashes to desktop may occur.
13+
*
14+
* @param exception the exception instance.
15+
*/
16+
public static void throwInDev(final RuntimeException exception)
17+
{
18+
if (FMLEnvironment.production)
19+
{
20+
Log.getLogger().error(exception.getMessage(), exception);
21+
}
22+
else
23+
{
24+
throw exception;
25+
}
26+
}
27+
}

src/main/java/com/ldtteam/blockui/views/ScrollingList.java

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

33
import com.ldtteam.blockui.Pane;
44
import com.ldtteam.blockui.PaneParams;
5+
import com.ldtteam.blockui.views.ScrollingListContainer.RowSizeModifier;
56

67
import java.util.List;
78
import java.util.function.IntSupplier;
@@ -14,11 +15,10 @@
1415
*/
1516
public class ScrollingList extends ScrollingView
1617
{
17-
protected int childSpacing = 0;
18+
protected int childSpacing = 0;
1819
// Runtime
1920
protected DataProvider dataProvider;
20-
private PaneParams listNodeParams;
21-
private int maxHeight;
21+
private int maxHeight;
2222

2323
/**
2424
* Default constructor required by Blockout.
@@ -42,6 +42,7 @@ public ScrollingList(final PaneParams params)
4242

4343
/**
4444
* Max height setter.
45+
*
4546
* @param maxHeight the height to set.
4647
*/
4748
public void setMaxHeight(final int maxHeight)
@@ -78,7 +79,7 @@ public void setDataProvider(final DataProvider p)
7879
*/
7980
public void refreshElementPanes()
8081
{
81-
((ScrollingListContainer) container).refreshElementPanes(dataProvider, listNodeParams, maxHeight, childSpacing);
82+
((ScrollingListContainer) container).refreshElementPanes(dataProvider, maxHeight, childSpacing);
8283
}
8384

8485
@Override
@@ -88,7 +89,7 @@ public void onUpdate()
8889
refreshElementPanes();
8990
}
9091

91-
@Override
92+
@Override
9293
protected ScrollingContainer createScrollingContainer()
9394
{
9495
return new ScrollingListContainer(this);
@@ -105,7 +106,10 @@ public void parseChildren(final PaneParams params)
105106

106107
// Get the PaneParams for this child, because we'll need it in the future
107108
// to create more nodes
108-
listNodeParams = childNodes.get(0);
109+
if (container instanceof ScrollingListContainer scrollingListContainer)
110+
{
111+
scrollingListContainer.setListNodeParams(childNodes.get(0));
112+
}
109113
}
110114

111115
/**
@@ -131,6 +135,17 @@ public interface DataProvider
131135
*/
132136
int getElementCount();
133137

138+
/**
139+
* Override this to pick a custom size for this element. Event contains the logic to modify the old size.
140+
*
141+
* @param index the index of the row/list element.
142+
* @param modifier the object used to modify the size.
143+
*/
144+
default void modifyRowSize(int index, final RowSizeModifier modifier)
145+
{
146+
// No implementation by default
147+
}
148+
134149
/**
135150
* Override this to update the Panes for a given row.
136151
*

0 commit comments

Comments
 (0)