Texture marker implementation

This commit is contained in:
ApsZoldat
2023-11-07 22:42:42 +03:00
parent cdb6e3f85b
commit 6dff68e7d9
5 changed files with 89 additions and 5 deletions

View File

@@ -64,7 +64,8 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
MinimapMarker::new,
ShapeMarker::new,
TextMarker::new,
LineMarker::new
LineMarker::new,
TextureMarker::new
);
}
@@ -612,7 +613,7 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
public static abstract class ObjectiveMarker{
/** Makes sure markers are only added once. */
public transient boolean wasAdded;
//** Hides the marker, used by world processors */
/** Hides the marker, used by world processors */
public boolean hidden = false;
/** Called in the overlay draw layer.*/
@@ -632,6 +633,8 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
}
public void setText(String text, boolean fetch){}
public void setTexture(String textureName){}
/** @return The localized type-name of this objective, defaulting to the class simple name without the "Marker" prefix. */
public String typeName(){
String className = getClass().getSimpleName().replace("Marker", "");
@@ -1001,6 +1004,65 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
}
}
/** Displays a texture with specified name. */
public static class TextureMarker extends ObjectiveMarker{
public @TilePos Vec2 pos = new Vec2();
public float rotation = 0f, width = 0f, height = 0f; // Zero width/height scales marker to original texture's size
public String textureName = "";
public Color color = Color.white.cpy();
private transient TextureRegion fetchedRegion;
public TextureMarker(String textureName, float x, float y){
this.textureName = textureName;
this.pos.set(x, y);
}
public TextureMarker(){}
@Override
public void control(LMarkerControl type, double p1, double p2, double p3){
switch(type){
case x -> pos.x = (float)p1 * tilesize;
case y -> pos.y = (float)p1 * tilesize;
case pos -> pos.set((float)p1 * tilesize, (float)p2 * tilesize);
case rotation -> rotation = (float)p1;
case textureWidth -> width = (float)p1 * tilesize;
case textureHeight -> height = (float)p1 * tilesize;
case color -> {
Log.info("test");
color.set(Tmp.c1.fromDouble(p1));
}
default -> super.control(type, p1, p2, p3);
}
}
@Override
public void draw(){
if(hidden || textureName.isEmpty()) return;
if(fetchedRegion == null) fetchedRegion = Core.atlas.find(textureName);
// Zero width/height scales marker to original texture's size
if(width < 1e-5) width = fetchedRegion.width * fetchedRegion.scl() * Draw.xscl;
if(height < 1e-5) height = fetchedRegion.height * fetchedRegion.scl() * Draw.yscl;
if(fetchedRegion.found()){
Draw.color(color);
Draw.rect(fetchedRegion, pos.x, pos.y, width, height, rotation);
}else{
Draw.color(Color.white);
Draw.rect("error", pos.x, pos.y, width, height, rotation);
}
}
@Override
public void setTexture(String textureName){
this.textureName = textureName;
fetchedRegion = Core.atlas.find(textureName);
}
}
/** For arrays or {@link Seq}s; does not create element rearrangement buttons. */
@Target(FIELD)
@Retention(RUNTIME)

View File

@@ -1555,7 +1555,7 @@ public class LExecutor{
@Override
public void run(LExecutor exec){
//set default to succes
//set default to success
exec.setnum(outSuccess, 1);
if(headless && type != MessageType.mission) {
exec.textBuffer.setLength(0);
@@ -1879,6 +1879,13 @@ public class LExecutor{
}else if(type == LMarkerControl.flushText){
marker.setText(exec.textBuffer.toString(), true);
exec.textBuffer.setLength(0);
}else if(type == LMarkerControl.texture){
if(exec.obj(p1) != null){
StringBuilder res = new StringBuilder(exec.obj(p1).toString());
if(exec.obj(p2) != null) res.append("-").append(exec.obj(p2).toString());
if(exec.obj(p3) != null) res.append("-").append(exec.obj(p3).toString());
marker.setTexture(res.toString());
}
}else{
marker.control(type, exec.num(p1), exec.num(p2), exec.num(p3));
}
@@ -1944,5 +1951,13 @@ public class LExecutor{
}
}
@Remote(called = Loc.server, variants = Variant.both, unreliable = true)
public static void updateMarkerTexture(int id, String textureName){
var marker = state.markers.get(id);
if(marker != null){
marker.setTexture(textureName);
}
}
//endregion
}

View File

@@ -24,7 +24,10 @@ public enum LMarkerControl{
shapeFill("true/false"),
shapeOutline("true/false"),
setShape("sides", "fill", "outline"),
color("color");
color("color"),
texture("name", "-", "-"),
textureWidth("width"),
textureHeight("height");
public final String[] params;

View File

@@ -1943,7 +1943,7 @@ public class LStatements{
b.clicked(() -> showSelect(b, LMarkerControl.all, type, t -> {
type = t;
rebuild(table);
}, 2, cell -> cell.size(140, 50)));
}, 3, cell -> cell.size(140, 50)));
}, Styles.logict, () -> {}).size(190, 40).color(table.color).left().padLeft(2);
row(table);