More work done.

-Improved: Lang System now uses Jsons with optional layers.
-Improved: EventBus SubscribeEvent no longer requires class definition for everything.
This commit is contained in:
Speiger 2022-04-24 00:55:00 +02:00
parent 98e1fbb9fe
commit ede8b1d1a1
11 changed files with 76 additions and 56 deletions

View File

@ -83,7 +83,6 @@ public class ZipAsset implements IAsset
{
return ImageIO.read(getStream());
}
@Override
public BufferedReader getStringReader() throws IOException
{

View File

@ -4,6 +4,11 @@ public class I18n
{
static Language CURRENT_LANGUAGE;
public static boolean has(String key)
{
return CURRENT_LANGUAGE.has(key);
}
public static String translate(String key)
{
return CURRENT_LANGUAGE.translate(key);

View File

@ -2,11 +2,13 @@ package speiger.src.coreengine.assets.language;
import java.util.Map;
import speiger.src.collections.objects.utils.maps.Object2ObjectMaps;
public class Language
{
String language;
String code;
Map<String, String> translations;
Map<String, String> translations = Object2ObjectMaps.empty();
public Language(String code, String language)
{
@ -21,7 +23,12 @@ public class Language
public void clear()
{
translations = null;
translations = Object2ObjectMaps.empty();
}
public boolean has(String s)
{
return translations.containsKey(s);
}
public String translate(String key)

View File

@ -3,6 +3,7 @@ package speiger.src.coreengine.assets.language;
import java.util.Map;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import speiger.src.collections.objects.maps.impl.hash.Object2ObjectOpenHashMap;
@ -11,12 +12,11 @@ import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.AssetManager;
import speiger.src.coreengine.assets.MultiAsset;
import speiger.src.coreengine.assets.reloader.IReloadableResource;
import speiger.src.coreengine.utils.collections.iterators.IterableWrapper;
public class LanguageManager implements IReloadableResource
{
final AssetLocation location = AssetLocation.of("lang");
Map<String, Language> languages = new Object2ObjectOpenHashMap<String, Language>();
Map<String, Language> languages = new Object2ObjectOpenHashMap<>();
AssetManager assets;
String currentLanguage;
@ -52,8 +52,8 @@ public class LanguageManager implements IReloadableResource
if(loadLanguage(lang))
{
I18n.CURRENT_LANGUAGE = languages.get(lang);
currentLanguage = lang;
Language currentLang = languages.get(currentLanguage);
currentLanguage = lang;
if(currentLang != null)
{
currentLang.clear();
@ -70,7 +70,7 @@ public class LanguageManager implements IReloadableResource
{
return false;
}
Map<String, String> map = new Object2ObjectOpenHashMap<String, String>();
Map<String, String> map = new Object2ObjectOpenHashMap<>();
loadLanguage(loadingLang, map);
if(loadingLang != "en_US")
{
@ -88,17 +88,9 @@ public class LanguageManager implements IReloadableResource
{
try
{
for(String line : IterableWrapper.wrap(language.getAsset(i).getStringReader()))
for(Map.Entry<String, JsonElement> element : language.getAsset(i).getJsonObject().entrySet())
{
if(line.isEmpty() || line.startsWith("#") || line.startsWith("//"))
{
continue;
}
String[] split = line.split("=", 1);
if(split.length == 2)
{
data.putIfAbsent(split[0].trim(), split[1].trim());
}
loadEntry(element.getKey(), element.getValue(), data);
}
}
catch(Exception e)
@ -113,9 +105,21 @@ public class LanguageManager implements IReloadableResource
}
}
protected void preLoadLanguage(JsonObject object)
protected void loadEntry(String basePath, JsonElement el, Map<String, String> data)
{
try
if(el.isJsonPrimitive()) data.put(basePath, el.getAsString());
else if(el.isJsonObject())
{
for(Map.Entry<String, JsonElement> elements : el.getAsJsonObject().entrySet())
{
String key = elements.getKey();
if(key.isEmpty()) loadEntry(basePath, elements.getValue(), data);
else loadEntry(basePath+"."+key, elements.getValue(), data);
}
}
}
protected void preLoadLanguage(JsonObject object)
{
JsonArray array = object.getAsJsonArray("languages");
if(array == null)
@ -142,10 +146,5 @@ public class LanguageManager implements IReloadableResource
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

View File

@ -42,7 +42,7 @@ public final class FacingList implements Iterable<Facing>, Predicate<Facing>
{
code = (byte)MathUtils.clamp(0, 15, initCode);
Vec2i pos = Vec2i.mutable();
ObjectList<Facing> facings = new ObjectArrayList<Facing>();
ObjectList<Facing> facings = new ObjectArrayList<>();
for(int i = 0;i<4;i++)
{
if((code & 1 << i) != 0)

View File

@ -2,6 +2,7 @@ package speiger.src.coreengine.rendering.gui.helper.constrains;
import java.util.function.BooleanSupplier;
import speiger.src.coreengine.math.misc.Facing;
import speiger.src.coreengine.rendering.gui.GuiComponent;
import speiger.src.coreengine.rendering.gui.helper.constrains.Constrain.Target;
import speiger.src.coreengine.utils.functions.FloatSupplier;
@ -51,6 +52,18 @@ public class Constrains
public static Constrains verticalScrollBar(BooleanSupplier supply, float offset, float barSize) { return invParent(barSize, Target.X).yPos(offset).width(barSize).height(new ConditionalConstraint(supply, new ParentConstrain(offset * 0.5F), new ParentConstrain((barSize + offset) * 0.5F))).build(); }
public static Constrains border(float size, Facing side, float offset)
{
switch(side)
{
case NORTH: return new Constrains(new ParentConstrain(), new ParentConstrain(offset), new ParentConstrain(), new PixelConstrain(size));
case EAST: return new Constrains(new ParentConstrain(size+offset).invert(), new ParentConstrain(), new PixelConstrain(size), new ParentConstrain());
case SOUTH: return new Constrains(new ParentConstrain(), new ParentConstrain(size+offset).invert(), new ParentConstrain(), new PixelConstrain(size));
case WEST: return new Constrains(new ParentConstrain(offset), new ParentConstrain(), new PixelConstrain(size), new ParentConstrain());
default: return null;
}
}
public static ConstrainBuilder xPos(Constrain xPos) { return new ConstrainBuilder().set(xPos, Target.X); }
public static ConstrainBuilder yPos(Constrain yPos) { return new ConstrainBuilder().set(yPos, Target.Y); }
public static ConstrainBuilder width(Constrain width) { return new ConstrainBuilder().set(width, Target.WIDTH); }

View File

@ -12,7 +12,7 @@ import speiger.src.coreengine.rendering.input.bindings.InputBinding;
public class BindingRegistry
{
Map<BindingType, Int2ObjectMap<Set<InputBinding>>> handlers = new EnumMap<BindingType, Int2ObjectMap<Set<InputBinding>>>(BindingType.class);
Map<BindingType, Int2ObjectMap<Set<InputBinding>>> handlers = new EnumMap<>(BindingType.class);
public void registerBinding(InputBinding binding)
{

View File

@ -18,9 +18,9 @@ public class Camera
static final Vec3f X_ROTATION = Vec3f.of(1, 0, 0);
static final Vec3f Y_ROTATION = Vec3f.of(0, 1, 0);
Vec3f position = Vec3f.mutable(0, 0, 0);
Vec3f position = Vec3f.mutable();
Vec3f lastPosition = Vec3f.mutable();
Vec2f rotation = Vec2f.mutable(0);
Vec2f rotation = Vec2f.mutable();
Vec2f lastRotation = Vec2f.mutable();
boolean moved = false;
boolean rotated = false;
@ -31,7 +31,7 @@ public class Camera
ICameraController controller;
Frustrum frustrum;
boolean changed = false;
List<Consumer<Camera>> listeners = new ObjectArrayList<Consumer<Camera>>();
List<Consumer<Camera>> listeners = new ObjectArrayList<>();
public Camera(Window window)
{

View File

@ -54,7 +54,7 @@ public class EventBus
SubscribeEvent data = t.getAnnotation(SubscribeEvent.class);
if(data == null) return;
Consumer<Event> listener = new MethodListener(LOOKUP.unreflect(t).bindTo(obj));
getListeners(data.value()).addListener(data.priority(), listener);
getListeners((Class<? extends Event>)t.getParameterTypes()[0]).addListener(data.priority(), listener);
list.add(new EventListener(data.value(), listener));
}
catch(Exception e) { e.printStackTrace(); }

View File

@ -32,10 +32,7 @@ public class Listeners
public void addChild(Listeners listener)
{
if(childs == null)
{
childs = new ObjectArrayList<Listeners>();
}
if(childs == null) childs = new ObjectArrayList<>();
childs.add(listener);
}
@ -93,7 +90,7 @@ public class Listeners
public void rebuildListeners()
{
rebuild = false;
List<Consumer<Event>> result = new ObjectArrayList<Consumer<Event>>();
List<Consumer<Event>> result = new ObjectArrayList<>();
for(EventPriority entry : EventPriority.getPriorities())
{
getListeners(entry, result);

View File

@ -14,5 +14,5 @@ public @interface SubscribeEvent
{
public EventPriority priority() default EventPriority.MEDIUM;
public Class<? extends Event> value();
public Class<? extends Event> value() default Event.class;
}