Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize quest database search #108

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package betterquesting.api2.client.gui.panels.lists;

import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.annotation.Nonnull;

import org.lwjgl.input.Mouse;

import betterquesting.api.storage.BQ_Settings;
import betterquesting.api.utils.RenderUtils;
import betterquesting.api2.client.gui.controls.IValueIO;
Expand All @@ -10,15 +18,10 @@
import betterquesting.api2.client.gui.panels.IGuiCanvas;
import betterquesting.api2.client.gui.panels.IGuiPanel;
import net.minecraft.client.renderer.GlStateManager;
import org.lwjgl.input.Mouse;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;

public class CanvasScrolling implements IGuiCanvas {
private final List<IGuiPanel> guiPanels = new CopyOnWriteArrayList<>();

protected final List<IGuiPanel> guiPanels = new CopyOnWriteArrayList<>();
private final IGuiRect transform;
private boolean enabled = true;

Expand Down Expand Up @@ -50,7 +53,7 @@ public class CanvasScrolling implements IGuiCanvas {

// Enables the auto-disabling panels outside the cropped region. Useful for very large lists
private boolean useBlocking = true;
private final CanvasCullingManager cullingManager = new CanvasCullingManager();
protected final CanvasCullingManager cullingManager = new CanvasCullingManager();
private final GuiRectangle refRect = new GuiRectangle(0, 0, 0, 0);

public CanvasScrolling(IGuiRect rect) {
Expand Down Expand Up @@ -94,30 +97,24 @@ public CanvasScrolling enableBlocking(boolean state) {
return this;
}

public IGuiRect getScrollBounds() {
return this.scrollBounds;
}
public IGuiRect getScrollBounds() { return this.scrollBounds; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the standard formatting appears to include a new line between {, }, and the statement, as it was before this change. since IDEA will automatically do this shortening, theres no real benefit to doing it manually. i think this part should be reverted


public int getScrollX() {
return Math.round(scrollBounds.getX() + scrollBounds.getWidth() * scrollX.readValue());
}
public int getScrollX() { return Math.round(scrollBounds.getX() + scrollBounds.getWidth() * scrollX.readValue()); }

public int getScrollY() {
return Math.round(scrollBounds.getY() + scrollBounds.getHeight() * scrollY.readValue());
}
public int getScrollY() { return Math.round(scrollBounds.getY() + scrollBounds.getHeight() * scrollY.readValue()); }

public float getZoom() {
return zoomScale.readValue();
}
public float getZoom() { return zoomScale.readValue(); }

public void setScrollX(int sx) {
if (scrollBounds.getWidth() <= 0) return;
if (scrollBounds.getWidth() <= 0)
return;
scrollX.writeValueRaw((sx - scrollBounds.getX()) / (float) scrollBounds.getWidth());
lsx = this.getScrollX();
}

public void setScrollY(int sy) {
if (scrollBounds.getHeight() <= 0) return;
if (scrollBounds.getHeight() <= 0)
return;
scrollY.writeValueRaw((sy - scrollBounds.getY()) / (float) scrollBounds.getHeight());
lsy = this.getScrollY();
}
Expand All @@ -135,29 +132,21 @@ public void initPanel() {
}

@Override
public void setEnabled(boolean state) {
this.enabled = state;
}
public void setEnabled(boolean state) { this.enabled = state; }

@Override
public boolean isEnabled() {
return this.enabled;
}
public boolean isEnabled() { return this.enabled; }

@Override
public IGuiRect getTransform() {
return transform;
}
public IGuiRect getTransform() { return transform; }

@Nonnull
@Override
public List<IGuiPanel> getChildren() {
return this.guiPanels;
}
@Nonnull @Override
public List<IGuiPanel> getChildren() { return this.guiPanels; }

@Override
public void drawPanel(int mx, int my, float partialTick) {
if (!isRectEqual(refRect, transform)) refreshScrollBounds();
if (!isRectEqual(refRect, transform))
refreshScrollBounds();

float zs = zoomScale.readValue();

Expand Down Expand Up @@ -308,7 +297,8 @@ public boolean onMouseRelease(int mx, int my, int click) {
boolean used = false;

if (!hasDragged) {
if (!transform.contains(mx, my)) return false;
if (!transform.contains(mx, my))
return false;

float zs = zoomScale.readValue();
int tx = transform.getX();
Expand All @@ -327,7 +317,8 @@ public boolean onMouseRelease(int mx, int my, int click) {
}

if (isDragging) {
if (!Mouse.isButtonDown(0) && !Mouse.isButtonDown(2)) isDragging = false;
if (!Mouse.isButtonDown(0) && !Mouse.isButtonDown(2))
isDragging = false;
return true;
}

Expand All @@ -336,7 +327,8 @@ public boolean onMouseRelease(int mx, int my, int click) {

@Override
public boolean onMouseScroll(int mx, int my, int scroll) {
if (scroll == 0 || !transform.contains(mx, my)) return false;
if (scroll == 0 || !transform.contains(mx, my))
return false;

float zs = zoomScale.readValue();
int tx = transform.getX();
Expand Down Expand Up @@ -410,8 +402,8 @@ public boolean onKeyTyped(char c, int keycode) {
break;
}
}
/*if(!used && c == 'c')

/*if(!used && c == 'c')
{
setScrollX(0);
setScrollY(0);
Expand All @@ -422,7 +414,8 @@ public boolean onKeyTyped(char c, int keycode) {

@Override
public List<String> getTooltip(int mx, int my) {
if (!transform.contains(mx, my) || isDragging) return null;
if (!transform.contains(mx, my) || isDragging)
return null;

float zs = zoomScale.readValue();
int tx = transform.getX();
Expand Down Expand Up @@ -457,7 +450,8 @@ public void addPanel(IGuiPanel panel) {
}

public void addCulledPanel(IGuiPanel panel, boolean useCulling) {
if (panel == null || guiPanels.contains(panel)) return;
if (panel == null || guiPanels.contains(panel))
return;

guiPanels.add(panel);
guiPanels.sort(ComparatorGuiDepth.INSTANCE);
Expand Down Expand Up @@ -554,11 +548,10 @@ public void resetCanvas() {
refreshScrollBounds();
}

private List<IGuiPanel> getVisiblePanels() {
return useBlocking ? cullingManager.getVisiblePanels() : guiPanels;
}
private List<IGuiPanel> getVisiblePanels() { return useBlocking ? cullingManager.getVisiblePanels() : guiPanels; }

private boolean isRectEqual(IGuiRect r1, IGuiRect r2) {
return r1.getX() == r2.getX() && r1.getY() == r2.getY() && r1.getWidth() == r2.getWidth() && r1.getHeight() == r2.getHeight();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package betterquesting.api2.client.gui.panels.lists;

import java.util.ArrayList;

import betterquesting.api2.client.gui.misc.ComparatorGuiDepth;
import betterquesting.api2.client.gui.misc.IGuiRect;
import betterquesting.api2.client.gui.panels.IGuiPanel;

public class CanvasScrollingBuffered extends CanvasScrolling {

private final ArrayList<IGuiPanel> buffer = new ArrayList<>();

public CanvasScrollingBuffered(IGuiRect rect) {
super(rect);
}

public void addPanelToBuffer(IGuiPanel panel) {
if (panel != null)
buffer.add(panel);
}

public void flushBuffer() {
if (buffer.isEmpty())
return;
for (IGuiPanel panel : buffer) {
if (guiPanels.contains(panel))
continue;

guiPanels.add(panel);
cullingManager.addPanel(panel, true);
panel.initPanel();
}
buffer.clear();

guiPanels.sort(ComparatorGuiDepth.INSTANCE);

this.refreshScrollBounds();
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package betterquesting.api2.client.gui.panels.lists;

import betterquesting.api2.client.gui.misc.IGuiRect;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.google.common.base.Stopwatch;

import java.util.*;
import java.util.concurrent.TimeUnit;
import betterquesting.api2.client.gui.misc.IGuiRect;

public abstract class CanvasSearch<T, E> extends CanvasScrolling {
public abstract class CanvasSearch<T, E> extends CanvasScrollingBuffered {

private String searchTerm = "";
private Iterator<E> searching = null;
Expand Down Expand Up @@ -39,6 +44,8 @@ public void drawPanel(int mx, int my, float partialTick) {
super.drawPanel(mx, my, partialTick);
}

public boolean isSearching() { return searching != null || !pendingResults.isEmpty(); }

public void refreshSearch() {
this.resetCanvas();
this.searchIdx = 0;
Expand Down Expand Up @@ -71,6 +78,10 @@ private void updateSearch() {
savedResults.addAll(tmp);

searchTime.stop();

if (!searching.hasNext())
searching = null;

}

private void updateResults() {
Expand All @@ -80,20 +91,24 @@ private void updateResults() {

searchTime.reset().start();

while (!pendingResults.isEmpty() && searchTime.elapsed(TimeUnit.MILLISECONDS) < 100) {
if (addResult(pendingResults.poll(), searchIdx, resultWidth)) searchIdx++;
int count = 0;
while (!pendingResults.isEmpty() && searchTime.elapsed(TimeUnit.MILLISECONDS) < 10 && count < 200) {
if (addResult(pendingResults.poll(), searchIdx, resultWidth)) {
searchIdx++;
count++;
}
}

searchTime.stop();
flushBuffer();
}

public List<T> getResults() {
return Collections.unmodifiableList(savedResults);
}
public List<T> getResults() { return Collections.unmodifiableList(savedResults); }

protected abstract Iterator<E> getIterator();

protected abstract void queryMatches(E value, String query, final ArrayDeque<T> results);

protected abstract boolean addResult(T entry, int index, int cachedWidth);

}
Loading
Loading