Skip to content

Commit

Permalink
Port SchemBrowser optimization to SchemDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
buthed010203 committed Nov 10, 2024
1 parent 41e2042 commit 4dbc635
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
4 changes: 2 additions & 2 deletions core/src/mindustry/client/ui/SchematicBrowserDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ public void draw() { // Update the name in the draw method as update() is called
t.add("@none").color(Color.lightGray);
}
}
}) {
}){
/** Custom hit implementation that respects the cullingArea. Allows for significantly larger schem repos without lag */
@Override
public Element hit(float x, float y, boolean touchable) {
public Element hit(float x, float y, boolean touchable){
// if (cullingArea == null) return super.hit(x, y, touchable); // Fallback to vanilla behavior if cullingArea is null

float cullLeft = cullingArea.x;
Expand Down
7 changes: 5 additions & 2 deletions core/src/mindustry/game/Schematics.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void loadSync(){

/** Load all schematics in the folder immediately.*/
public void load(){
Time.mark();
all.clear();

var await = Seq.<Future<?>>with();
Expand All @@ -94,8 +95,11 @@ public void load(){
})));

loadLoadouts();
Time.mark();
await.each(Threads::await);
Log.debug("Awaited schematics for @ms", Time.elapsed());
all.sort();
Log.debug("Loaded @ schematics in @ms", all.size, Time.elapsed());

if(shadowBuffer == null){
Core.app.post(() -> shadowBuffer = new FrameBuffer(maxSchematicSize + padding + 8, maxSchematicSize + padding + 8));
Expand Down Expand Up @@ -210,8 +214,7 @@ public FrameBuffer getBuffer(Schematic schematic){
Seq<Schematic> keys = previews.orderedKeys().copy();
for(int i = 0; i < previews.size - max; i++){
//dispose and remove unneeded previews
previews.get(keys.get(i)).dispose();
previews.remove(keys.get(i));
previews.remove(keys.get(i)).dispose();
}
//update last clear time
lastClearTime = Time.millis();
Expand Down
31 changes: 30 additions & 1 deletion core/src/mindustry/ui/dialogs/SchematicsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import arc.graphics.g2d.*;
import arc.input.*;
import arc.math.*;
import arc.math.geom.*;
import arc.scene.*;
import arc.scene.event.*;
import arc.scene.style.*;
import arc.scene.ui.*;
import arc.scene.ui.ImageButton.*;
Expand Down Expand Up @@ -125,7 +128,7 @@ void setup(){

cont.row();

cont.pane(t -> {
cont.pane(new Table(t -> {
t.top();

t.update(() -> {
Expand Down Expand Up @@ -244,6 +247,32 @@ public void draw() { // Update the name in the draw method as update() is called
};

rebuildPane.run();
}){
@Override
public Element hit(float x, float y, boolean touchable){
if (cullingArea == null) return super.hit(x, y, touchable); // Fallback to vanilla behavior if cullingArea is null. Only happens for a few frames on open

float cullLeft = cullingArea.x;
float cullRight = cullLeft + cullingArea.width;
float cullBottom = cullingArea.y;
float cullTop = cullBottom + cullingArea.height;
if(x > cullRight || y > cullTop || x + getWidth() < cullLeft || y + getHeight() < cullBottom) return null; // Whole table is outside of culling bounds.

Vec2 point = Tmp.v5;
Element[] childrenArray = children.items;
for(int i = children.size - 1; i >= 0; i--){
Element child = childrenArray[i];
if(!child.visible || (child.x > cullRight || child.y > cullTop || child.x + child.getWidth() < cullLeft || child.y + child.getHeight() < cullBottom) && child.cullable) continue;
child.parentToLocalCoordinates(point.set(x, y));
Element hit = child.hit(point.x, point.y, touchable);
if(hit != null) return hit;
}

// Element.hit pasted (to check if the table was the thing hit)
if(touchable && this.touchable != Touchable.enabled) return null;
Element e = this;
return x >= e.translation.x && x < width + e.translation.x && y >= e.translation.y && y < height + e.translation.y ? this : null;
}
}).grow().scrollX(false);
}

Expand Down

0 comments on commit 4dbc635

Please sign in to comment.