Skip to content

Commit

Permalink
Allow skipping list updates (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thodor12 authored Aug 5, 2024
1 parent 8bc8bb6 commit 533164f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
29 changes: 29 additions & 0 deletions src/main/java/com/ldtteam/blockui/mod/ScrollingListsGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
Expand Down Expand Up @@ -127,5 +129,32 @@ public void updateElement(final int index, final Pane rowPane)

window.findPaneOfTypeByID("list4add", Button.class).setHandler(button -> renderAmount.getAndAdd(2));
window.findPaneOfTypeByID("list4remove", Button.class).setHandler(button -> renderAmount.getAndAdd(-2));

// Case 5: A list that will not update
final AtomicBoolean shouldRenderFlag = new AtomicBoolean();
final ScrollingList list5 = window.findPaneOfTypeByID("list5", ScrollingList.class);
list5.setDataProvider(new DataProvider()
{
@Override
public int getElementCount()
{
return 10;
}

@Override
public boolean shouldUpdate()
{
return shouldRenderFlag.get();
}

@Override
public void updateElement(final int index, final Pane rowPane)
{
shouldRenderFlag.set(false);
rowPane.findPaneByType(Text.class).setText(Component.literal("Hi " + index + " " + UUID.randomUUID()));
}
});

window.findPaneOfTypeByID("list5update", Button.class).setHandler(button -> shouldRenderFlag.set(true));
}
}
40 changes: 35 additions & 5 deletions src/main/java/com/ldtteam/blockui/views/ScrollingList.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,32 @@ public void updateElement(final int index, final Pane rowPane)
public void setDataProvider(final DataProvider p)
{
dataProvider = p;
refreshElementPanes();
refreshElementPanes(true);
}

/**
* Use the data provider to update all the element panes.
*/
public void refreshElementPanes()
{
((ScrollingListContainer) container).refreshElementPanes(dataProvider, maxHeight, childSpacing);
refreshElementPanes(true);
}

/**
* Use the data provider to update all the element panes.
*
* @param force should the list be forcefully updated.
*/
public void refreshElementPanes(final boolean force)
{
((ScrollingListContainer) container).refreshElementPanes(dataProvider, maxHeight, childSpacing, force);
}

@Override
public void onUpdate()
{
super.onUpdate();
refreshElementPanes();
refreshElementPanes(false);
}

@Override
Expand Down Expand Up @@ -185,13 +195,33 @@ public interface DataProvider
*/
int getElementCount();

/**
* Should all the children update again?
*
* @return true if the updates should be made
*/
default boolean shouldUpdate()
{
return true;
}

/**
* Should the specific child update again?
*
* @return true if the updates should be made
*/
default boolean shouldUpdate(final int index)
{
return true;
}

/**
* Override this to pick a custom size for this element. Event contains the logic to modify the old size.
*
* @param index the index of the row/list element.
* @param modifier the object used to modify the size.
*/
default void modifyRowSize(int index, final RowSizeModifier modifier)
default void modifyRowSize(final int index, final RowSizeModifier modifier)
{
// No implementation by default
}
Expand All @@ -214,6 +244,6 @@ public interface IPaneUpdater
* @param index The index to update.
* @param rowPane The pane to fill.
*/
void apply(int index, Pane rowPane);
void apply(final int index, final Pane rowPane);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,15 @@ public void setListNodeParams(final @NotNull PaneParams listNodeParams)
* @param dataProvider data provider object, shouldn't be null.
* @param height the maximum height of the parent.
* @param childSpacing the spacing between each row.
* @param force should the list be forcefully updated.
*/
public void refreshElementPanes(final DataProvider dataProvider, final int height, final int childSpacing)
public void refreshElementPanes(final DataProvider dataProvider, final int height, final int childSpacing, final boolean force)
{
if (dataProvider == null)
{
return;
}

int currentYpos = 0;

if (listNodeParams == null)
Expand All @@ -107,12 +113,17 @@ public void refreshElementPanes(final DataProvider dataProvider, final int heigh
return;
}

if (!force && !dataProvider.shouldUpdate())
{
return;
}

if (this.width != emptyTextComponent.getWidth() || this.height != emptyTextComponent.getHeight())
{
emptyTextComponent.setSize(this.width, this.height);
}

final int numElements = (dataProvider != null) ? dataProvider.getElementCount() : 0;
final int numElements = dataProvider.getElementCount();
if (numElements > 0)
{
if (emptyTextComponent.getParent() != null)
Expand Down Expand Up @@ -150,7 +161,10 @@ public void refreshElementPanes(final DataProvider dataProvider, final int heigh
child.setSize(modifier.width, modifier.height);
}

dataProvider.updateElement(i, child);
if (force || dataProvider.shouldUpdate(i))
{
dataProvider.updateElement(i, child);
}
}

currentYpos += elementHeight + childSpacing;
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/assets/blockui/gui/test4.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<window xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="block_ui.xsd" size="640 240" pause="true" lightbox="true">
<window xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="block_ui.xsd" size="640 480" type="FIXED_VANILLA" pause="true" lightbox="true">
<list id="list1" size="160 240">
<text size="152 20"/>
</list>
Expand All @@ -21,4 +21,8 @@
</list>
<button id="list4add" pos="480 200" size="160 20" label="Add item"/>
<button id="list4remove" pos="480 220" size="160 20" label="Remove item"/>
<list id="list5" size="160 200" pos="0 240" emptytext="This list is empty" emptyscale="0.8" emptycolor="white">
<label size="152 20"/>
</list>
<button id="list5update" pos="0 440" size="160 20" label="Update list"/>
</window>

0 comments on commit 533164f

Please sign in to comment.