Skip to content

Commit

Permalink
bank tags: fix scrolling while reordering
Browse files Browse the repository at this point in the history
Dragging over one of the scroll buttons was broken because it would
rebuild the tab layer, deleting the dragged widget.

Split rebuilding tabs from layout, and use layout to perform scroll.
While doing so, avoid hiding the dragged widget.
  • Loading branch information
Adam- committed Feb 21, 2024
1 parent d48ba58 commit ee678a6
Showing 1 changed file with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public class TabInterface
private static final int NEWTAB_OP_NEW_TAB = 1;
private static final int NEWTAB_OP_IMPORT_TAB = 2;
private static final int NEWTAB_OP_OPEN_TAB_MENU = 3;
private static final int TAGTAB_CHILD_OFFSET = 4;

private final Client client;
private final ClientThread clientThread;
Expand Down Expand Up @@ -232,7 +233,7 @@ else if (event.getScriptId() == ScriptID.BANKMAIN_SIZE_CHECK && enabled)
clientThread.invokeLater(() ->
{
repositionButtons();
rebuildTabs();
layoutTabs();
});
}
}
Expand Down Expand Up @@ -759,20 +760,25 @@ public void onDraggingWidgetChanged(DraggingWidgetChanged event)
&& draggedWidget.getId() == ComponentID.BANK_ITEM_CONTAINER
&& draggedWidget.getItemId() != -1
&& draggedOn.getParent() == parent
&& draggedOn.getIndex() > 3) // skip buttons
&& draggedOn.getIndex() >= TAGTAB_CHILD_OFFSET) // skip buttons
{
// Tag an item dragged on a tag tab
log.debug("Dragged {} to tab {}", draggedWidget.getItemId(), Text.removeTags(draggedOn.getName()));
tagManager.addTag(draggedWidget.getItemId(), draggedOn.getName(), shiftDown);
reloadActiveTab();
}
else if ((tagTabActive && draggedWidget.getId() == ComponentID.BANK_ITEM_CONTAINER && draggedOn.getId() == ComponentID.BANK_ITEM_CONTAINER)
|| (draggedWidget.getParent() == parent && draggedOn.getParent() == parent))
|| (draggedWidget.getParent() == parent && draggedOn.getParent() == parent && draggedWidget.getIndex() >= TAGTAB_CHILD_OFFSET && draggedOn.getIndex() >= TAGTAB_CHILD_OFFSET))
{
// Reorder tag tabs
log.debug("Reorder tag tab {} <-> {}", draggedWidget, draggedOn);
moveTagTab(draggedWidget, draggedOn);
}
else
{
// Rebuild to avoid the dragged tab being left over due to it being excluded from being hidden when layouted
rebuildTabs();
}
}
else if (draggedWidget.getItemId() != -1)
{
Expand Down Expand Up @@ -948,7 +954,7 @@ private void scrollTab(int d)

config.position(tabScrollOffset);

rebuildTabs();
layoutTabs();
}

private void openNamedTag(String name, boolean relayout)
Expand Down Expand Up @@ -1043,19 +1049,14 @@ private void repositionButtons()
private void rebuildTabs()
{
// remove the tag tabs but keep the buttons and scroll component
parent.setChildren(Arrays.copyOf(parent.getChildren(), 4));

int y = scrollComponent.getOriginalY();
y += MARGIN;
parent.setChildren(Arrays.copyOf(parent.getChildren(), TAGTAB_CHILD_OFFSET));

var tabs = tabManager.getTabs();
for (int i = tabScrollOffset; i < tabScrollOffset + tabCount && i < tabs.size(); ++i)
for (TagTab tab : tabs)
{
TagTab tab = tabs.get(i);

Widget background = createGraphic(parent, ColorUtil.wrapWithColorTag(tab.getTag(), HILIGHT_COLOR),
(activeTab == tab ? TabSprites.TAB_BACKGROUND_ACTIVE : TabSprites.TAB_BACKGROUND).getSpriteId(),
-1, TAB_WIDTH, TAB_HEIGHT, MARGIN, y);
-1, TAB_WIDTH, TAB_HEIGHT, MARGIN, -1);
addTabActions(background);

Widget icon = createGraphic(
Expand All @@ -1064,8 +1065,42 @@ private void rebuildTabs()
-1,
tab.getIconItemId(),
Constants.ITEM_SPRITE_WIDTH, Constants.ITEM_SPRITE_HEIGHT,
MARGIN + 3, y + 4);
MARGIN + 3, -1);
addTabOptions(icon);
}

layoutTabs();
}

// layout the tabs for their position due to scroll or window resize
private void layoutTabs()
{
Widget[] children = parent.getChildren();
Widget draggedWidget = client.getDraggedWidget();
for (int i = TAGTAB_CHILD_OFFSET; i < children.length; ++i)
{
Widget child = children[i];
// avoid hiding dragged widget if scrolling from drag
if (draggedWidget != child)
{
child.setHidden(true);
}
}

int y = scrollComponent.getOriginalY();
y += MARGIN;

for (int i = tabScrollOffset; i < tabScrollOffset + tabCount && i * 2 + 1 < children.length - TAGTAB_CHILD_OFFSET; ++i)
{
Widget background = children[TAGTAB_CHILD_OFFSET + i * 2];
background.setOriginalY(y);
background.setHidden(false);
background.revalidate();

Widget icon = children[TAGTAB_CHILD_OFFSET + i * 2 + 1];
icon.setOriginalY(y + 4);
icon.setHidden(false);
icon.revalidate();

y += TAB_HEIGHT + MARGIN;
}
Expand Down

0 comments on commit ee678a6

Please sign in to comment.