A lot of Fixes and improvements.

-Changed: Downgraded from LWJGL 3 Version 3.3.0 to 3.2.4 because of Incompats with Submodules
-Changed: Reworked Asset Providers to use Java-NIO.
-Added: IAsset supports now SubFiles that are related to the main file.
-Fixed: Axises would trigger with Keyboard keys even if the keys were consumed.
-Added: GuiComponents can now be customized by adding custom renderers.
-Changed: TextField now only trigger user action if it can lose focus xD
This commit is contained in:
Speiger 2021-08-30 04:01:16 +02:00
parent 84522cc130
commit b00e330db9
18 changed files with 244 additions and 195 deletions

View File

@ -1,23 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<projectDescription> <projectDescription>
<name>SimpleJavaEngine</name> <name>SimpleJavaEngine</name>
<comment>Project SimpleJavaEngine created by Buildship.</comment> <comment></comment>
<projects> <projects/>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures> <natures>
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature> <nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures> </natures>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments/>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments/>
</buildCommand>
</buildSpec>
<linkedResources/>
<filteredResources/>
</projectDescription> </projectDescription>

View File

@ -1,4 +1,4 @@
org.gradle.jvmargs=-Xmx2G org.gradle.jvmargs=-Xmx2G
lwjglVersion = 3.3.0-SNAPSHOT lwjglVersion = 3.2.4-SNAPSHOT
lwjglNatives = natives-windows lwjglNatives = natives-windows

View File

@ -42,6 +42,11 @@ public final class AssetLocation
return of(domain+":"+location + "/" + subPath); return of(domain+":"+location + "/" + subPath);
} }
public AssetLocation alternate(String alterversion)
{
return of(domain+":"+location+"."+alterversion);
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -3,6 +3,8 @@ package speiger.src.coreengine.assets;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -16,36 +18,40 @@ import speiger.src.coreengine.assets.reloader.IReloadableResource;
public class AssetManager implements IReloadableResource public class AssetManager implements IReloadableResource
{ {
Map<String, DomainAssets> domains = new Object2ObjectLinkedOpenHashMap<String, DomainAssets>(); Map<String, DomainAssets> domains = new Object2ObjectLinkedOpenHashMap<>();
File file; Path path;
public AssetManager(File file) public AssetManager(File file)
{ {
this.file = file; this(file.toPath());
} }
public File getMainAsset() public AssetManager(Path path)
{ {
return file; this.path = path;
} }
public Path getMainAsset()
{
return path;
}
@Override @Override
public void reload() public void reload()
{ {
domains.clear(); domains.clear();
loadAssetPackage(file); loadAssetPackage(path);
} }
public void loadAssetPackage(File file) public void loadAssetPackage(File file)
{ {
if(file.isDirectory()) loadAssetPackage(file.toPath());
{
loadAssets(new FolderAssetPackage(file));
} }
else if(isZipFile(file))
public void loadAssetPackage(Path path)
{ {
loadAssets(new ZipAssetPackage(file)); if(Files.notExists(path)) return;
} if(Files.isDirectory(path)) loadAssets(new FolderAssetPackage(path));
else if(isZipFile(path)) loadAssets(new ZipAssetPackage(path));
} }
public void loadAssets(IAssetPackage packages) public void loadAssets(IAssetPackage packages)
@ -72,7 +78,7 @@ public class AssetManager implements IReloadableResource
DomainAssets asset = domains.get(location.getDomain()); DomainAssets asset = domains.get(location.getDomain());
if(asset == null) if(asset == null)
{ {
throw new FileNotFoundException("File["+location.toString()+"] not found"); throw new FileNotFoundException("Domain & File["+location.toString()+"] not found");
} }
return asset.getAsset(location); return asset.getAsset(location);
} }
@ -96,7 +102,7 @@ public class AssetManager implements IReloadableResource
DomainAssets asset = domains.get(location.getDomain()); DomainAssets asset = domains.get(location.getDomain());
if(asset == null) if(asset == null)
{ {
throw new FileNotFoundException("File["+location.toString()+"] not found"); throw new FileNotFoundException("Domain & File["+location.toString()+"] not found");
} }
return asset.getMultiAsset(location, subLocations); return asset.getMultiAsset(location, subLocations);
} }
@ -111,16 +117,10 @@ public class AssetManager implements IReloadableResource
return asset.getAllAssets(location); return asset.getAllAssets(location);
} }
protected boolean isZipFile(File file) protected boolean isZipFile(Path file)
{ {
try(ZipFile zip = new ZipFile(file)) try(ZipFile zip = new ZipFile(file.toFile())) { return true; }
{ catch(Exception e) { return false; }
return true;
}
catch(Exception e)
{
}
return false;
} }
static class DomainAssets static class DomainAssets
@ -137,15 +137,12 @@ public class AssetManager implements IReloadableResource
for(int i = packages.size() - 1;i>=0;i--) for(int i = packages.size() - 1;i>=0;i--)
{ {
IAsset asset = packages.get(i).getAsset(location); IAsset asset = packages.get(i).getAsset(location);
if(asset != null) if(asset != null) return asset;
{
return asset;
}
} }
throw new FileNotFoundException("File["+location.toString()+"] not found"); throw new FileNotFoundException("File["+location.toString()+"] not found");
} }
public MultiAsset getMultiAsset(AssetLocation location, AssetLocation[] subLocations) public MultiAsset getMultiAsset(AssetLocation location, AssetLocation... subLocations)
{ {
for(int i = packages.size() - 1;i>=0;i--) for(int i = packages.size() - 1;i>=0;i--)
{ {
@ -171,10 +168,7 @@ public class AssetManager implements IReloadableResource
for(IAssetPackage entry : packages) for(IAssetPackage entry : packages)
{ {
IAsset asset = entry.getAsset(location); IAsset asset = entry.getAsset(location);
if(asset != null) if(asset != null) assets.add(asset);
{
assets.add(asset);
}
} }
if(assets.isEmpty()) if(assets.isEmpty())
{ {

View File

@ -12,6 +12,8 @@ public interface IAsset extends Closeable
{ {
public AssetLocation getLocation(); public AssetLocation getLocation();
public IAsset subAsset(String alternative);
public InputStream getStream() throws IOException; public InputStream getStream() throws IOException;
public BufferedImage getTexture() throws Exception; public BufferedImage getTexture() throws Exception;

View File

@ -19,10 +19,7 @@ public class MultiAsset implements Closeable
{ {
for(int i = 0,m=assets.length;i<m;i++) for(int i = 0,m=assets.length;i<m;i++)
{ {
if(assets[i] != null) if(assets[i] != null) assets[i].close();
{
assets[i].close();
}
} }
assets = null; assets = null;
} }

View File

@ -1,41 +1,45 @@
package speiger.src.coreengine.assets.impl; package speiger.src.coreengine.assets.impl;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.Closeable; import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List; import java.util.List;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.coreengine.assets.AssetLocation; import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.IAsset; import speiger.src.coreengine.assets.IAsset;
import speiger.src.coreengine.utils.helpers.JsonUtil; import speiger.src.coreengine.utils.helpers.JsonUtil;
public class FileAsset implements IAsset public class FolderAsset implements IAsset
{ {
File file;
AssetLocation location; AssetLocation location;
List<Closeable> toClose = new ArrayList<Closeable>(); Path path;
List<Closeable> closeable;
public FileAsset(File file, AssetLocation location) public FolderAsset(AssetLocation location, Path path)
{
this(location, path, new ObjectArrayList<>());
}
public FolderAsset(AssetLocation location, Path path, List<Closeable> closeable)
{ {
this.file = file;
this.location = location; this.location = location;
this.path = path;
this.closeable = closeable;
} }
@Override @Override
public void close() throws IOException public void close() throws IOException
{ {
for(Closeable close : toClose) for(Closeable close : closeable)
{ {
try try
{ {
@ -46,7 +50,14 @@ public class FileAsset implements IAsset
e.printStackTrace(); e.printStackTrace();
} }
} }
toClose.clear(); closeable.clear();
}
@Override
public IAsset subAsset(String alternative)
{
Path newPath = path.resolveSibling(path.getFileName().toString()+"."+alternative);
return Files.exists(newPath) ? new FolderAsset(location.alternate(alternative), newPath, closeable) : null;
} }
@Override @Override
@ -58,30 +69,30 @@ public class FileAsset implements IAsset
@Override @Override
public InputStream getStream() throws IOException public InputStream getStream() throws IOException
{ {
return new BufferedInputStream(markClosed(new FileInputStream(file))); return markClosed(Files.newInputStream(path));
}
@Override
public BufferedReader getStringReader() throws IOException
{
return new BufferedReader(markClosed(new FileReader(file)));
} }
@Override @Override
public BufferedImage getTexture() throws Exception public BufferedImage getTexture() throws Exception
{ {
return ImageIO.read(file); return ImageIO.read(getStream());
}
@Override
public BufferedReader getStringReader() throws IOException
{
return markClosed(Files.newBufferedReader(path));
} }
@Override @Override
public JsonObject getJsonObject() throws IOException public JsonObject getJsonObject() throws IOException
{ {
return JsonUtil.loadFile(file); return JsonUtil.loadFile(path);
} }
protected <T extends Closeable> T markClosed(T value) protected <T extends Closeable> T markClosed(T value)
{ {
toClose.add(value); closeable.add(value);
return value; return value;
} }
} }

View File

@ -1,47 +1,54 @@
package speiger.src.coreengine.assets.impl; package speiger.src.coreengine.assets.impl;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.IOException;
import java.util.ArrayList; import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List; import java.util.List;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.coreengine.assets.AssetLocation; import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.IAsset; import speiger.src.coreengine.assets.IAsset;
import speiger.src.coreengine.assets.IAssetPackage; import speiger.src.coreengine.assets.IAssetPackage;
import speiger.src.coreengine.assets.impl.FolderAsset;
public class FolderAssetPackage implements IAssetPackage public class FolderAssetPackage implements IAssetPackage
{ {
static final FileFilter FOLDER_FILTER = new FileFilter(){ Path baseFolder;
@Override
public boolean accept(File f)
{
return f.isDirectory();
}
};
File file;
public FolderAssetPackage(File file) public FolderAssetPackage(File baseFolder)
{ {
this.file = file; this(baseFolder.toPath());
}
public FolderAssetPackage(Path baseFolder)
{
this.baseFolder = baseFolder;
} }
@Override @Override
public List<String> getDomains() public List<String> getDomains()
{ {
List<String> result = new ArrayList<String>(); List<String> domains = new ObjectArrayList<>();
File asset = new File(file, "assets/"); try(DirectoryStream<Path> dirs = Files.newDirectoryStream(baseFolder.resolve("assets")))
for(File file : asset.listFiles(FOLDER_FILTER))
{ {
result.add(file.getAbsolutePath().substring(asset.getAbsolutePath().length() + 1).replace("\\", "/").replace("\"", "/")); for(Path path : dirs)
{
domains.add(path.getFileName().toString());
} }
return result; }
catch(IOException e)
{
e.printStackTrace();
}
return domains;
} }
@Override @Override
public IAsset getAsset(AssetLocation location) public IAsset getAsset(AssetLocation location)
{ {
File asset = new File(file, location.getActualLocation()); Path path = baseFolder.resolve(location.getActualLocation());
return asset.isFile() ? new FileAsset(asset, location) : null; return Files.exists(path) ? new FolderAsset(location, path) : null;
} }
} }

View File

@ -1,13 +1,11 @@
package speiger.src.coreengine.assets.impl; package speiger.src.coreengine.assets.impl;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.nio.file.Files;
import java.util.zip.ZipEntry; import java.nio.file.Path;
import java.util.zip.ZipFile;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
@ -19,29 +17,44 @@ import speiger.src.coreengine.utils.helpers.JsonUtil;
public class ZipAsset implements IAsset public class ZipAsset implements IAsset
{ {
ZipFile file; Runnable closer;
ZipEntry entry;
AssetLocation location; AssetLocation location;
Runnable runnable; Path path;
public ZipAsset(ZipFile file, ZipEntry entry, AssetLocation location, Runnable runnable) public ZipAsset(Runnable closer, AssetLocation location, Path path)
{ {
this.file = file; this.closer = closer;
this.entry = entry;
this.location = location; this.location = location;
this.runnable = runnable; this.path = path;
} }
@Override @Override
public void close() throws IOException public void close() throws IOException
{ {
if(runnable != null) if(closer != null)
{ {
runnable.run(); closer.run();
runnable = null; closer = null;
} }
} }
private void closeInternal()
{
if(closer != null)
{
closer.run();
closer = null;
}
}
@Override
public IAsset subAsset(String alternative)
{
if(closer == null) return null;
Path newPath = path.resolveSibling(path.getFileName().toString()+"."+alternative);
return Files.exists(newPath) ? new ZipAsset(this::closeInternal, location.alternate(alternative), newPath) : null;
}
@Override @Override
public AssetLocation getLocation() public AssetLocation getLocation()
{ {
@ -51,25 +64,24 @@ public class ZipAsset implements IAsset
@Override @Override
public InputStream getStream() throws IOException public InputStream getStream() throws IOException
{ {
return new BufferedInputStream(file.getInputStream(entry)); return Files.newInputStream(path);
} }
@Override @Override
public BufferedImage getTexture() throws Exception public BufferedImage getTexture() throws Exception
{ {
return ImageIO.read(file.getInputStream(entry)); return ImageIO.read(getStream());
} }
@Override @Override
public BufferedReader getStringReader() throws IOException public BufferedReader getStringReader() throws IOException
{ {
return new BufferedReader(new InputStreamReader(file.getInputStream(entry))); return Files.newBufferedReader(path);
} }
@Override @Override
public JsonObject getJsonObject() throws IOException public JsonObject getJsonObject() throws IOException
{ {
return JsonUtil.loadFile(file.getInputStream(entry)); return JsonUtil.loadFile(path);
} }
} }

View File

@ -2,91 +2,73 @@ package speiger.src.coreengine.assets.impl;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.nio.file.DirectoryStream;
import java.util.Collections; import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.coreengine.assets.AssetLocation; import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.IAsset; import speiger.src.coreengine.assets.IAsset;
import speiger.src.coreengine.assets.IAssetPackage; import speiger.src.coreengine.assets.IAssetPackage;
import speiger.src.coreengine.utils.collections.iterators.IterableWrapper;
public class ZipAssetPackage implements IAssetPackage public class ZipAssetPackage implements IAssetPackage
{ {
public static final int ASSET_OFFSET = "assets/".length(); Path baseFolder;
File file; FileSystem cache;
ZipFile cache = null;
AtomicInteger usedReferences = new AtomicInteger(); AtomicInteger usedReferences = new AtomicInteger();
Runnable referenceCleaner = this::cleanReference;
public ZipAssetPackage(File file) public ZipAssetPackage(File file)
{ {
this.file = file; this(file.toPath());
} }
protected ZipFile getReference() throws IOException public ZipAssetPackage(Path baseFolder)
{ {
usedReferences.getAndIncrement(); this.baseFolder = baseFolder;
if(cache == null) cache = new ZipFile(file);
return cache;
} }
protected void onReferenceClosed() private void cleanReference()
{ {
int left = usedReferences.decrementAndGet(); int left = usedReferences.decrementAndGet();
if(left == 0 && cache != null) if(left == 0 && cache != null)
{ {
try try { cache.close(); }
{
cache.close();
}
catch(Exception e) {e.printStackTrace();} catch(Exception e) {e.printStackTrace();}
cache = null; cache = null;
} }
else if(left < 0) else if(left < 0) usedReferences.set(0);
{
usedReferences.set(0);
} }
protected FileSystem getReference() throws IOException
{
usedReferences.getAndIncrement();
if(cache == null) cache = FileSystems.newFileSystem(baseFolder, null);
return cache;
} }
@Override @Override
public List<String> getDomains() public List<String> getDomains()
{ {
try List<String> domains = new ObjectArrayList<>();
try(FileSystem system = FileSystems.newFileSystem(baseFolder, null))
{ {
List<String> result = new ArrayList<String>(); try(DirectoryStream<Path> dirs = Files.newDirectoryStream(system.getPath("assets")))
ZipFile zip = new ZipFile(file);
for(ZipEntry entry : IterableWrapper.wrap(zip.entries()))
{ {
String name = entry.getName(); for(Path path : dirs)
if(isPathValid(name))
{ {
result.add(name.substring(ASSET_OFFSET, name.length() - 1)); String s = path.getFileName().toString();
domains.add(s.substring(0, s.length()-1));
} }
} }
zip.close(); catch(Exception e) { e.printStackTrace(); }
return result;
} }
catch(IOException e) catch(Exception e) { e.printStackTrace(); }
{ return domains;
e.printStackTrace();
return Collections.emptyList();
}
}
private boolean isPathValid(String input)
{
if(input.startsWith("assets/"))
{
int index = input.indexOf("/", ASSET_OFFSET);
if(index != -1 && input.length() - index <= 1)
{
return true;
}
}
return false;
} }
@Override @Override
@ -94,18 +76,9 @@ public class ZipAssetPackage implements IAssetPackage
{ {
try try
{ {
ZipFile zip = getReference(); Path path = getReference().getPath(location.getActualLocation());
ZipEntry entry = zip.getEntry(location.getActualLocation()); return Files.exists(path) ? new ZipAsset(referenceCleaner, location, path) : null;
if(entry != null)
{
return new ZipAsset(zip, entry, location, this::onReferenceClosed);
} }
} catch(Exception e) { return null; }
catch(Exception e)
{
e.printStackTrace();
}
onReferenceClosed();
return null;
} }
} }

View File

@ -6,7 +6,6 @@ import speiger.src.collections.objects.sets.ObjectLinkedOpenHashSet;
public class ResourceReloader public class ResourceReloader
{ {
// public static ResourceReloader INSTANCE = new ResourceReloader();
Set<IReloadableResource> resources = new ObjectLinkedOpenHashSet<IReloadableResource>(); Set<IReloadableResource> resources = new ObjectLinkedOpenHashSet<IReloadableResource>();
boolean globalRemoval = false; boolean globalRemoval = false;
boolean reloading = false; boolean reloading = false;

View File

@ -23,6 +23,7 @@ import speiger.src.coreengine.rendering.gui.helper.box.GuiBox;
import speiger.src.coreengine.rendering.gui.helper.box.IGuiBox; import speiger.src.coreengine.rendering.gui.helper.box.IGuiBox;
import speiger.src.coreengine.rendering.gui.helper.constrains.ComponentConstrains; import speiger.src.coreengine.rendering.gui.helper.constrains.ComponentConstrains;
import speiger.src.coreengine.rendering.gui.helper.constrains.Constrain; import speiger.src.coreengine.rendering.gui.helper.constrains.Constrain;
import speiger.src.coreengine.rendering.gui.renderer.IComponentRenderer;
import speiger.src.coreengine.rendering.gui.renderer.UIRenderer; import speiger.src.coreengine.rendering.gui.renderer.UIRenderer;
import speiger.src.coreengine.rendering.input.Keyboard; import speiger.src.coreengine.rendering.input.Keyboard;
import speiger.src.coreengine.rendering.input.bindings.utils.BindingType; import speiger.src.coreengine.rendering.input.bindings.utils.BindingType;
@ -60,6 +61,7 @@ public abstract class GuiComponent extends FlagHolder
ComponentConstrains constraints = null; ComponentConstrains constraints = null;
Animator animation = null; Animator animation = null;
KeyBindAction binding = null; KeyBindAction binding = null;
IComponentRenderer<GuiComponent> customRenderer;
Set<Consumer<GuiComponent>>[] listeners = CollectionUtils.createSets(3, true); Set<Consumer<GuiComponent>>[] listeners = CollectionUtils.createSets(3, true);
Set<GuiComponent> children = new ObjectLinkedOpenHashSet<>(); Set<GuiComponent> children = new ObjectLinkedOpenHashSet<>();
Set<GuiComponent> popupChildren = new ObjectLinkedOpenHashSet<>(); Set<GuiComponent> popupChildren = new ObjectLinkedOpenHashSet<>();
@ -429,6 +431,12 @@ public abstract class GuiComponent extends FlagHolder
return this; return this;
} }
public <T extends GuiComponent> GuiComponent setCustomRenderer(IComponentRenderer<T> renderer)
{
customRenderer = (IComponentRenderer<GuiComponent>)renderer;
return this;
}
private void addBindingTooltip() private void addBindingTooltip()
{ {
tooltips.addComponent(binding.getTooltip(), new TextComponent(0F, 0F, 200F, 0F, "Key: "+ModType.getMods(binding.mod)+BindingType.KEYBOARD.getName(binding.key)).setLimitedHeight(false).setAlignment(Align.LEFT_TOP, Align.LEFT_TOP).setScale(0.5F)); tooltips.addComponent(binding.getTooltip(), new TextComponent(0F, 0F, 200F, 0F, "Key: "+ModType.getMods(binding.mod)+BindingType.KEYBOARD.getName(binding.key)).setLimitedHeight(false).setAlignment(Align.LEFT_TOP, Align.LEFT_TOP).setScale(0.5F));
@ -789,12 +797,23 @@ public abstract class GuiComponent extends FlagHolder
} }
public final void onRender(int mouseX, int mouseY, float particalTicks) public final void onRender(int mouseX, int mouseY, float particalTicks)
{
if(customRenderer != null)
{
customRenderer.onPreRender(this);
getRenderer().setVisibility(totalVisibility).setBrightness(brightness);
if(customRenderer.render(this)) renderChildren(mouseX, mouseY, particalTicks);
customRenderer.onPostRender(this);
getRenderer().resetEffects();
}
else
{ {
onPreRender(); onPreRender();
getRenderer().setVisibility(totalVisibility).setBrightness(brightness); getRenderer().setVisibility(totalVisibility).setBrightness(brightness);
if(renderSelf(mouseX, mouseY, particalTicks)) renderChildren(mouseX, mouseY, particalTicks); if(renderSelf(mouseX, mouseY, particalTicks)) renderChildren(mouseX, mouseY, particalTicks);
onPostRender(); onPostRender();
getRenderer().resetEffects(); getRenderer().resetEffects();
}
if(getGui() instanceof GuiScreenBase) if(getGui() instanceof GuiScreenBase)
{ {
((GuiScreenBase)getGui()).drawBox(this); ((GuiScreenBase)getGui()).drawBox(this);

View File

@ -580,6 +580,8 @@ public class TextFieldComponent extends GuiComponent
@Override @Override
public void onFocusLost() public void onFocusLost()
{
if(isFlagSet(FLAG_CAN_LOSE_FOCUS))
{ {
if(isFlagSet(FLAG_FOCUS)) if(isFlagSet(FLAG_FOCUS))
{ {
@ -587,6 +589,7 @@ public class TextFieldComponent extends GuiComponent
} }
setFocused(false); setFocused(false);
} }
}
protected void handleDoubleClick(int position) protected void handleDoubleClick(int position)
{ {

View File

@ -20,7 +20,7 @@ public class VerticalLayout implements Consumer<GuiComponent>
this.components.addAll(components); this.components.addAll(components);
for(int i = 0;i<components.size();i++) for(int i = 0;i<components.size();i++)
{ {
components.get(i).addChangeListener(this); components.get(i).addChangeListener(this).addCloseListener(this::removeComponent);
} }
this.box = box; this.box = box;
this.padding = padding; this.padding = padding;

View File

@ -5,7 +5,6 @@ import speiger.src.coreengine.rendering.gui.components.ButtonComponent;
import speiger.src.coreengine.rendering.gui.components.TextComponent; import speiger.src.coreengine.rendering.gui.components.TextComponent;
import speiger.src.coreengine.rendering.gui.components.TextFieldComponent; import speiger.src.coreengine.rendering.gui.components.TextFieldComponent;
import speiger.src.coreengine.rendering.gui.components.WindowComponent; import speiger.src.coreengine.rendering.gui.components.WindowComponent;
import speiger.src.coreengine.rendering.gui.helper.constrains.ComponentConstrains;
import speiger.src.coreengine.rendering.gui.helper.constrains.ParentConstrain; import speiger.src.coreengine.rendering.gui.helper.constrains.ParentConstrain;
import speiger.src.coreengine.rendering.gui.helper.constrains.PixelConstrain; import speiger.src.coreengine.rendering.gui.helper.constrains.PixelConstrain;
import speiger.src.coreengine.rendering.gui.helper.constrains.RelativeConstrain; import speiger.src.coreengine.rendering.gui.helper.constrains.RelativeConstrain;
@ -14,7 +13,7 @@ import speiger.src.coreengine.rendering.gui.helper.constrains.TextConstrain;
public class TextInputComponent extends WindowComponent public class TextInputComponent extends WindowComponent
{ {
TextComponent message = new TextComponent().setLimitedHeight(false).setTextScale(0.5F); TextComponent message = new TextComponent().setLimitedHeight(false).setTextScale(0.5F);
TextFieldComponent input = new TextFieldComponent(ColorObject.GRAY); TextFieldComponent input = new TextFieldComponent(ColorObject.GRAY).setCanLoseFocus(false).setInfiniteText(true).setMaxTextLength(Integer.MAX_VALUE).setFocused(true);
ButtonComponent confirm = new ButtonComponent("Confirm", ColorObject.DARK_GREEN); ButtonComponent confirm = new ButtonComponent("Confirm", ColorObject.DARK_GREEN);
ButtonComponent cancel = new ButtonComponent("Cancel", ColorObject.RED); ButtonComponent cancel = new ButtonComponent("Cancel", ColorObject.RED);
@ -46,13 +45,19 @@ public class TextInputComponent extends WindowComponent
confirm.getText().setTextScale(0.5F); confirm.getText().setTextScale(0.5F);
cancel.getText().setTextScale(0.5F); cancel.getText().setTextScale(0.5F);
input.getRawText().setTextScale(0.5F); input.getRawText().setTextScale(0.5F);
addChild(confirm.addChangeListener(minimizedListener).addUserActionListener((T) -> notifyListeners(LISTENER_USER_ACTION)).addUserActionListener(closeListener), new ComponentConstrains(new RelativeConstrain(0F), new ParentConstrain(10F).invert(), new RelativeConstrain(0.5F), new PixelConstrain(10F))); addChild(confirm.addChangeListener(minimizedListener).addUserActionListener(this::onListen), new RelativeConstrain(0F), new ParentConstrain(10F).invert(), new RelativeConstrain(0.5F), new PixelConstrain(10F));
addChild(cancel.addChangeListener(minimizedListener).addUserActionListener(closeListener), new ComponentConstrains(new RelativeConstrain(0.5F), new ParentConstrain(10F).invert(), new RelativeConstrain(0.5F), new PixelConstrain(10F))); addChild(cancel.addChangeListener(minimizedListener).addUserActionListener(closeListener), new RelativeConstrain(0.5F), new ParentConstrain(10F).invert(), new RelativeConstrain(0.5F), new PixelConstrain(10F));
addChild(message, new ComponentConstrains(new PixelConstrain(10F), new PixelConstrain(11F), new ParentConstrain(10F), TextConstrain.height(message))); addChild(message.addChangeListener(minimizedListener), new PixelConstrain(10F), new PixelConstrain(11F), new ParentConstrain(10F), TextConstrain.height(message));
addChild(input, new ComponentConstrains(new PixelConstrain(10F), TextConstrain.height(message).setPadding(15F), new ParentConstrain(10F), new PixelConstrain(12F))); addChild(input.addChangeListener(minimizedListener).addUserActionListener(this::onListen), new PixelConstrain(10F), TextConstrain.height(message).setPadding(15F), new ParentConstrain(10F), new PixelConstrain(12F));
getBox().setHeight(45F + message.getMetadata().getMaxHeight()); getBox().setHeight(45F + message.getMetadata().getMaxHeight());
} }
private void onListen()
{
notifyListeners(LISTENER_USER_ACTION);
closeListener.accept(this);
}
@Override @Override
public boolean isPopup() public boolean isPopup()
{ {

View File

@ -0,0 +1,10 @@
package speiger.src.coreengine.rendering.gui.renderer;
import speiger.src.coreengine.rendering.gui.GuiComponent;
public interface IComponentRenderer<T extends GuiComponent>
{
public void onPreRender(T component);
public boolean render(T component);
public void onPostRender(T component);
}

View File

@ -15,6 +15,7 @@ public class Keyboard
{ {
public static final Keyboard INSTANCE = new Keyboard(); public static final Keyboard INSTANCE = new Keyboard();
boolean[] pressedKeys = new boolean[350]; boolean[] pressedKeys = new boolean[350];
boolean[] nonConsumedKeys = new boolean[350];
EventBus bus; EventBus bus;
BiPredicate<Integer, Keyboard> filter; BiPredicate<Integer, Keyboard> filter;
@ -49,6 +50,7 @@ public class Keyboard
else else
{ {
pressedKeys[key] = false; pressedKeys[key] = false;
nonConsumedKeys[key] = false;
InputBinding.BINDINGS.updatePressing(BindingType.KEYBOARD, key, false); InputBinding.BINDINGS.updatePressing(BindingType.KEYBOARD, key, false);
} }
} }
@ -63,6 +65,7 @@ public class Keyboard
{ {
return; return;
} }
nonConsumedKeys[key] = true;
InputBinding.BINDINGS.updatePressing(BindingType.KEYBOARD, key, true); InputBinding.BINDINGS.updatePressing(BindingType.KEYBOARD, key, true);
} }
} }
@ -92,6 +95,11 @@ public class Keyboard
return key <= 162; return key <= 162;
} }
public static boolean isKeyFullyPressed(int key)
{
return INSTANCE.isPressed(key) && !INSTANCE.isConsumed(key);
}
public static boolean isKeyPressed(int key) public static boolean isKeyPressed(int key)
{ {
return INSTANCE.isPressed(key); return INSTANCE.isPressed(key);
@ -101,4 +109,9 @@ public class Keyboard
{ {
return pressedKeys[key]; return pressedKeys[key];
} }
public boolean isConsumed(int key)
{
return !nonConsumedKeys[key];
}
} }

View File

@ -21,7 +21,7 @@ public enum BindingType
@Override @Override
public boolean isPressed(int key) public boolean isPressed(int key)
{ {
return Keyboard.isKeyPressed(key); return Keyboard.isKeyFullyPressed(key);
} }
}, },
MOUSE MOUSE