Skip to content

Commit ea26ffe

Browse files
author
Maximilian Stiede
committed
apply feedback
1 parent 0288d51 commit ea26ffe

File tree

7 files changed

+246
-222
lines changed

7 files changed

+246
-222
lines changed

chunky/src/java/se/llbit/chunky/renderer/scene/sky/CelestialBodyType.java

Lines changed: 0 additions & 219 deletions
This file was deleted.

chunky/src/java/se/llbit/chunky/renderer/scene/sky/Sun.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import se.llbit.chunky.renderer.Refreshable;
2525
import se.llbit.chunky.renderer.SceneIOProvider;
2626
import se.llbit.chunky.renderer.scene.Scene;
27-
import se.llbit.chunky.resources.Texture;
27+
import se.llbit.chunky.renderer.scene.sky.celestialbodies.CelestialBodyType;
2828
import se.llbit.json.JsonObject;
2929
import se.llbit.math.QuickMath;
3030
import se.llbit.math.Ray;
@@ -116,7 +116,7 @@ public class Sun implements JsonSerializable {
116116

117117
private final Refreshable scene;
118118

119-
private CelestialBodyType type = new CelestialBodyType.Sun();
119+
private CelestialBodyType type = CelestialBodyType.DEFAULT;
120120

121121
/**
122122
* Sun radius
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package se.llbit.chunky.renderer.scene.sky.celestialbodies;
2+
3+
import se.llbit.chunky.renderer.SceneIOProvider;
4+
import se.llbit.chunky.resources.Texture;
5+
import se.llbit.json.JsonObject;
6+
import se.llbit.log.Log;
7+
import se.llbit.util.Registerable;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
import java.util.function.Supplier;
12+
13+
public abstract class CelestialBodyType implements Registerable {
14+
private static final String CONFIG_TYPE_KEY = "celestialBodyType";
15+
16+
public final static CelestialBodyType DEFAULT = new Sun();
17+
18+
public static final Map<String, Supplier<CelestialBodyType>> TYPES = new HashMap<>(3);
19+
static {
20+
TYPES.put(Sun.ID, Sun::new);
21+
TYPES.put(Moon.ID, Moon::new);
22+
TYPES.put(Custom.ID, Custom::new);
23+
}
24+
25+
public static CelestialBodyType newFromJson(JsonObject obj) {
26+
String typeStr = obj.get(CONFIG_TYPE_KEY).asString(Sun.ID);
27+
Supplier<CelestialBodyType> create = TYPES.get(typeStr);
28+
if(create == null) {
29+
Log.warnf("Unknown celestial body type \"%s\"", typeStr);
30+
return DEFAULT;
31+
}
32+
CelestialBodyType type = create.get();
33+
type.importFromJson(obj);
34+
return type;
35+
}
36+
37+
protected void importFromJson(JsonObject obj) {}
38+
39+
public void appendToConfig(JsonObject obj) {
40+
obj.add(CONFIG_TYPE_KEY, getId());
41+
}
42+
43+
/**
44+
* will be called when a scene is loaded to load associated custom textures
45+
*/
46+
public void loadCustomTextures(SceneIOProvider ioContext) {
47+
}
48+
49+
public abstract Texture getTexture();
50+
51+
@Override
52+
public String getDescription() {
53+
return getName();
54+
}
55+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package se.llbit.chunky.renderer.scene.sky.celestialbodies;
2+
3+
import se.llbit.chunky.renderer.SceneIOProvider;
4+
import se.llbit.chunky.resources.Texture;
5+
import se.llbit.json.JsonObject;
6+
import se.llbit.log.Log;
7+
import se.llbit.resources.ImageLoader;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
12+
public class Custom extends CelestialBodyType {
13+
public static final String ID = "CUSTOM";
14+
15+
@Override
16+
public String getId() {
17+
return ID;
18+
}
19+
20+
@Override
21+
public String getName() {
22+
return "Custom";
23+
}
24+
25+
@Override
26+
public String getDescription() {
27+
return "Custom celestial body texture";
28+
}
29+
30+
private final Texture texture = new Texture();
31+
private String fileName;
32+
33+
public Custom() {
34+
texture.setTexture(Sun.texture);
35+
}
36+
37+
@Override
38+
protected void importFromJson(JsonObject obj) {
39+
fileName = obj.get("customTextureFile").asString(null);
40+
}
41+
42+
@Override
43+
public void appendToConfig(JsonObject obj) {
44+
super.appendToConfig(obj);
45+
obj.add("customTextureFile", fileName);
46+
}
47+
48+
public void loadCustomTextures(SceneIOProvider ioContext) {
49+
if (fileName != null) {
50+
try {
51+
setFile(ioContext.resolveLinkedFile(fileName));
52+
} catch (IOException ex) {
53+
Log.error("Failed to find custom skymap file: " + fileName);
54+
}
55+
}
56+
}
57+
58+
@Override
59+
public Texture getTexture() {
60+
return texture;
61+
}
62+
63+
public String getFileName() {
64+
return fileName;
65+
}
66+
67+
public void setFile(File file) {
68+
try {
69+
texture.setTexture(ImageLoader.read(file));
70+
} catch (IOException ex) {
71+
Log.error("Failed to load custom skymap: " + file, ex);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)