Schematic preview improvements

This commit is contained in:
Anuken
2019-10-20 12:48:39 -04:00
parent 3c65e366e8
commit b5e9f280e6
4 changed files with 61 additions and 31 deletions

View File

@@ -5,13 +5,12 @@ import io.anuke.arc.collection.IntIntMap.*;
import io.anuke.arc.files.*;
import io.anuke.arc.util.ArcAnnotate.*;
import io.anuke.mindustry.*;
import io.anuke.mindustry.game.Schematics.*;
import io.anuke.mindustry.type.*;
import io.anuke.mindustry.world.*;
import static io.anuke.mindustry.Vars.*;
public class Schematic implements Publishable{
public class Schematic implements Publishable, Comparable<Schematic>{
public final Array<Stile> tiles;
public StringMap tags;
public int width, height;
@@ -90,10 +89,15 @@ public class Schematic implements Publishable{
@Override
public FileHandle createSteamPreview(String id){
FileHandle preview = tmpDirectory.child("schematic_preview_" + id + ".png");
schematics.savePreview(this, PreviewRes.high, preview);
schematics.savePreview(this, preview);
return preview;
}
@Override
public int compareTo(Schematic schematic){
return name().compareTo(schematic.name());
}
public static class Stile{
public @NonNull Block block;
public short x, y;

View File

@@ -31,15 +31,16 @@ public class Schematics implements Loadable{
private static final byte version = 0;
private static final int padding = 2;
private static final int resolution = 32;
private OptimizedByteArrayOutputStream out = new OptimizedByteArrayOutputStream(1024);
private Array<Schematic> all = new Array<>();
private OrderedMap<Schematic, ObjectMap<PreviewRes, FrameBuffer>> previews = new OrderedMap<>();
private OrderedMap<Schematic, FrameBuffer> previews = new OrderedMap<>();
private FrameBuffer shadowBuffer;
public Schematics(){
Events.on(DisposeEvent.class, e -> {
previews.each((schem, m) -> m.each((res, buffer) -> buffer.dispose()));
previews.each((schem, m) -> m.dispose());
previews.clear();
shadowBuffer.dispose();
});
@@ -60,6 +61,8 @@ public class Schematics implements Loadable{
platform.getWorkshopContent(Schematic.class).each(this::loadFile);
all.sort();
Core.app.post(() -> {
shadowBuffer = new FrameBuffer(maxSchematicSize + padding + 2, maxSchematicSize + padding + 2);
});
@@ -95,8 +98,8 @@ public class Schematics implements Loadable{
}
}
public void savePreview(Schematic schematic, PreviewRes res, FileHandle file){
FrameBuffer buffer = getBuffer(schematic, res);
public void savePreview(Schematic schematic, FileHandle file){
FrameBuffer buffer = getBuffer(schematic);
Draw.flush();
buffer.begin();
Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, buffer.getWidth(), buffer.getHeight());
@@ -104,16 +107,18 @@ public class Schematics implements Loadable{
buffer.end();
}
public Texture getPreview(Schematic schematic, PreviewRes res){
return getBuffer(schematic, res).getTexture();
public Texture getPreview(Schematic schematic){
return getBuffer(schematic).getTexture();
}
public FrameBuffer getBuffer(Schematic schematic, PreviewRes res){
if(!previews.getOr(schematic, ObjectMap::new).containsKey(res)){
int resolution = res.resolution;
public boolean hasPreview(Schematic schematic){
return previews.containsKey(schematic);
}
public FrameBuffer getBuffer(Schematic schematic){
if(!previews.containsKey(schematic)){
Draw.blend();
Draw.reset();
Time.mark();
Tmp.m1.set(Draw.proj());
Tmp.m2.set(Draw.trans());
FrameBuffer buffer = new FrameBuffer((schematic.width + padding) * resolution, (schematic.height + padding) * resolution);
@@ -170,11 +175,10 @@ public class Schematics implements Loadable{
Draw.proj(Tmp.m1);
Draw.trans(Tmp.m2);
previews.getOr(schematic, ObjectMap::new).put(res, buffer);
Log.info("Time taken: {0}", Time.elapsed());
previews.put(schematic, buffer);
}
return previews.get(schematic).get(res);
return previews.get(schematic);
}
/** Creates an array of build requests from a schematic's data, centered on the provided x+y coordinates. */
@@ -197,6 +201,11 @@ public class Schematics implements Loadable{
if(s.file != null){
s.file.delete();
}
if(previews.containsKey(s)){
previews.get(s).dispose();
previews.remove(s);
}
}
/** Creates a schematic from a world selection. */
@@ -367,14 +376,4 @@ public class Schematics implements Loadable{
}
//endregion
public enum PreviewRes{
low(8), med(8), high(32);
public final int resolution;
PreviewRes(int resolution){
this.resolution = resolution;
}
}
}

View File

@@ -265,6 +265,8 @@ public class DesktopInput extends InputHandler{
}
void pollInput(){
if(scene.getKeyboardFocus() instanceof TextField) return;
Tile selected = tileAt(Core.input.mouseX(), Core.input.mouseY());
int cursorX = tileX(Core.input.mouseX());
int cursorY = tileY(Core.input.mouseY());

View File

@@ -5,13 +5,13 @@ import io.anuke.arc.collection.*;
import io.anuke.arc.graphics.*;
import io.anuke.arc.graphics.Texture.*;
import io.anuke.arc.graphics.g2d.*;
import io.anuke.arc.scene.style.*;
import io.anuke.arc.scene.ui.*;
import io.anuke.arc.scene.ui.ImageButton.*;
import io.anuke.arc.scene.ui.TextButton.*;
import io.anuke.arc.scene.ui.layout.*;
import io.anuke.arc.util.*;
import io.anuke.mindustry.game.*;
import io.anuke.mindustry.game.Schematics.*;
import io.anuke.mindustry.gen.*;
import io.anuke.mindustry.graphics.*;
import io.anuke.mindustry.type.*;
@@ -109,10 +109,10 @@ public class SchematicsDialog extends FloatingDialog{
b.stack(new SchematicImage(s).setScaling(Scaling.fit), new Table(n -> {
n.top();
n.table(Styles.black3, c -> {
Label label = c.add(s.name()).style(Styles.outlineLabel).color(Color.white).top().growX().get();
Label label = c.add(s.name()).style(Styles.outlineLabel).color(Color.white).top().growX().maxWidth(200f - 8f).get();
label.setEllipsis(true);
label.setAlignment(Align.center);
}).growX().margin(1).pad(4).padBottom(0);
}).growX().margin(1).pad(4).maxWidth(200f - 8f).padBottom(0);
})).size(200f);
}, () -> {
if(sel[0].childrenPressed()) return;
@@ -221,9 +221,18 @@ public class SchematicsDialog extends FloatingDialog{
public float thickness = 4f;
public Color borderColor = Pal.gray;
private Schematic schematic;
boolean set;
public SchematicImage(Schematic s){
super(schematics.getPreview(s, PreviewRes.high));
super(Tex.clear);
setScaling(Scaling.fit);
schematic = s;
if(schematics.hasPreview(s)){
setPreview();
set = true;
}
}
@Override
@@ -231,6 +240,12 @@ public class SchematicsDialog extends FloatingDialog{
boolean checked = getParent().getParent() instanceof Button
&& ((Button)getParent().getParent()).isOver();
boolean wasSet = set;
if(!set){
Core.app.post(this::setPreview);
set = true;
}
Texture background = Core.assets.get("sprites/schematic-background.png", Texture.class);
TextureRegion region = Draw.wrap(background);
float xr = width / scaling;
@@ -241,7 +256,11 @@ public class SchematicsDialog extends FloatingDialog{
Draw.alpha(parentAlpha);
Draw.rect(region, x + width/2f, y + height/2f, width, height);
super.draw();
if(wasSet){
super.draw();
}else{
Draw.rect(Icon.loading.getRegion(), x + width/2f, y + height/2f, width/4f, height/4f);
}
Draw.color(checked ? Pal.accent : borderColor);
Draw.alpha(parentAlpha);
@@ -249,6 +268,12 @@ public class SchematicsDialog extends FloatingDialog{
Lines.rect(x, y, width, height);
Draw.reset();
}
private void setPreview(){
TextureRegionDrawable draw = new TextureRegionDrawable(new TextureRegion(schematics.getPreview(schematic)));
setDrawable(draw);
setScaling(Scaling.fit);
}
}
public static class SchematicInfoDialog extends FloatingDialog{