diff --git a/README.md b/README.md index 64e67a214..ae952ef24 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ +The original author doesn't maintain vis-editor any more and also reject any pull request for the editor, so I fork his repository to work on for my game. +原作者已不再维护vis-editor,本分支上的维护用于我自己的游戏。 + +Install to Maven Local repository (default localtion is ~/.m2/repository/) +=== +./gradelw clean install + VisEditor ========= diff --git a/build-editor.gradle b/build-editor.gradle index 9eb36ea4f..99d215bd2 100644 --- a/build-editor.gradle +++ b/build-editor.gradle @@ -2,6 +2,7 @@ buildscript { ext.kotlinVersion = '1.1.1' repositories { + mavenLocal() mavenCentral() maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } maven { url "https://plugins.gradle.org/m2/" } @@ -53,6 +54,7 @@ allprojects { } repositories { + mavenLocal() mavenCentral() maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } maven { url "https://oss.sonatype.org/content/repositories/releases/" } diff --git a/build.gradle b/build.gradle index c249bebbb..6f172cb76 100644 --- a/build.gradle +++ b/build.gradle @@ -1,8 +1,10 @@ buildscript { - ext.kotlinVersion = '1.1.1' + ext.kotlinVersion = '1.2.20' repositories { + mavenLocal() mavenCentral() + maven { url "http://maven.aliyun.com/nexus/content/groups/public"} maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } maven { url "https://plugins.gradle.org/m2/" } } @@ -25,6 +27,7 @@ allprojects { apply plugin: "java" apply plugin: "nebula.provided-base" apply plugin: "nebula.optional-base" + apply plugin: "maven" ext { appName = 'vis' @@ -37,12 +40,21 @@ allprojects { } repositories { + mavenLocal() mavenCentral() maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } maven { url "https://oss.sonatype.org/content/repositories/releases/" } } } +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + } + } +} + nexusStaging { packageGroup = "com.kotcrab" username = ossrhUsername diff --git a/editor/build.gradle b/editor/build.gradle index 6b43fdd9c..b527faab1 100644 --- a/editor/build.gradle +++ b/editor/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'java' apply plugin: 'maven' apply plugin: 'kotlin' apply from: '../deploy.gradle' +//pply plugin: "maven-publish" group = 'com.kotcrab.vis' archivesBaseName = "vis-editor" diff --git a/editor/src/main/java/com/kotcrab/vis/editor/module/project/FontCacheModule.java b/editor/src/main/java/com/kotcrab/vis/editor/module/project/FontCacheModule.java index 4fd9c2e6c..68500d27c 100644 --- a/editor/src/main/java/com/kotcrab/vis/editor/module/project/FontCacheModule.java +++ b/editor/src/main/java/com/kotcrab/vis/editor/module/project/FontCacheModule.java @@ -47,7 +47,7 @@ public class FontCacheModule extends ProjectModule implements WatchListener { public static final int MIN_FONT_SIZE = 5; public static final int DEFAULT_FONT_SIZE = 20; - public static final String DEFAULT_TEXT = "The quick brown fox jumps over the lazy dog"; + public static final String DEFAULT_TEXT = "这是中文。The quick brown fox jumps over the lazy dog"; private FileAccessModule fileAccess; private AssetsWatcherModule watcherModule; diff --git a/editor/src/main/java/com/kotcrab/vis/editor/module/project/TtfEditorFont.java b/editor/src/main/java/com/kotcrab/vis/editor/module/project/TtfEditorFont.java index 1e3b34316..b6ff60b33 100644 --- a/editor/src/main/java/com/kotcrab/vis/editor/module/project/TtfEditorFont.java +++ b/editor/src/main/java/com/kotcrab/vis/editor/module/project/TtfEditorFont.java @@ -22,6 +22,7 @@ import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter; import com.badlogic.gdx.utils.ObjectMap; +import com.kotcrab.vis.ui.util.font.LazyBitmapFont; /** * TTF font provider that allows to get font for user provided size. TtfEditorFont can only provide fonts for one pixelsPerUnits value. @@ -47,7 +48,7 @@ public BitmapFont get (int size) { BitmapFont font = bitmapFonts.get(size); if (font == null) { - font = generator.generateFont(getParameterForSize(size)); + font = new LazyBitmapFont(generator, getParameterForSize(size)); font.setUseIntegerPositions(false); font.getData().setScale(1f / pixelsPerUnit); bitmapFonts.put(size, font); @@ -59,6 +60,7 @@ public BitmapFont get (int size) { private FreeTypeFontParameter getParameterForSize (int size) { FreeTypeFontParameter parameter = new FreeTypeFontParameter(); parameter.size = size; + parameter.incremental = true; parameter.minFilter = TextureFilter.Linear; parameter.magFilter = TextureFilter.Linear; return parameter; diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 963e116e4..9559bb9eb 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed May 10 11:00:00 CEST 2017 +#Mon Nov 20 12:37:50 CST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip diff --git a/runtime/src/main/java/com/kotcrab/vis/runtime/assets/TextureAsset.java b/runtime/src/main/java/com/kotcrab/vis/runtime/assets/TextureAsset.java new file mode 100644 index 000000000..519fe24b3 --- /dev/null +++ b/runtime/src/main/java/com/kotcrab/vis/runtime/assets/TextureAsset.java @@ -0,0 +1,11 @@ +package com.kotcrab.vis.runtime.assets; + +public class TextureAsset extends PathAsset { + @Deprecated + public TextureAsset () { + } + + public TextureAsset (String relativePath) { + super(relativePath); + } +} diff --git a/runtime/src/main/java/com/kotcrab/vis/runtime/font/FreeTypeFontProvider.java b/runtime/src/main/java/com/kotcrab/vis/runtime/font/FreeTypeFontProvider.java index 156f07fc7..188f3a89d 100644 --- a/runtime/src/main/java/com/kotcrab/vis/runtime/font/FreeTypeFontProvider.java +++ b/runtime/src/main/java/com/kotcrab/vis/runtime/font/FreeTypeFontProvider.java @@ -47,6 +47,6 @@ public void load (Array dependencies, VisAssetDescriptor asset) @Override public void setLoaders (AssetManager assetManager) { assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(assetManager.getFileHandleResolver())); - assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(assetManager.getFileHandleResolver())); + assetManager.setLoader(BitmapFont.class, ".ttf", new LazyFreetypeFontLoader(assetManager.getFileHandleResolver())); } } diff --git a/runtime/src/main/java/com/kotcrab/vis/runtime/font/LazyFreetypeFontLoader.java b/runtime/src/main/java/com/kotcrab/vis/runtime/font/LazyFreetypeFontLoader.java new file mode 100644 index 000000000..92982e2c3 --- /dev/null +++ b/runtime/src/main/java/com/kotcrab/vis/runtime/font/LazyFreetypeFontLoader.java @@ -0,0 +1,33 @@ +package com.kotcrab.vis.runtime.font; + +import com.badlogic.gdx.assets.AssetManager; +import com.badlogic.gdx.assets.loaders.FileHandleResolver; +import com.badlogic.gdx.files.FileHandle; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; +import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader; +import com.kotcrab.vis.runtime.util.font.LazyBitmapFont; + +public class LazyFreetypeFontLoader extends FreetypeFontLoader { + public LazyFreetypeFontLoader(FileHandleResolver resolver) { + super(resolver); + } + + @Override + public BitmapFont loadSync(AssetManager manager, String fileName, FileHandle file, FreeTypeFontLoaderParameter parameter) { + if(parameter == null) throw new RuntimeException("FreetypeFontParameter must be set in AssetManager#load to point at a TTF file!"); + FreeTypeFontGenerator generator = manager.get(parameter.fontFileName + ".gen", FreeTypeFontGenerator.class); +// BitmapFont font = generator.generateFont(parameter.fontParameters); + BitmapFont font = new LazyBitmapFont(generator, parameter.fontParameters); + return font; + } + +// private FreeTypeFontGenerator.FreeTypeFontParameter getParameterForSize (int size) { +// FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter(); +// parameter.size = size; +// parameter.incremental = true; +// parameter.minFilter = Texture.TextureFilter.Linear; +// parameter.magFilter = Texture.TextureFilter.Linear; +// return parameter; +// } +} diff --git a/runtime/src/main/java/com/kotcrab/vis/runtime/system/inflater/SpriteInflater.java b/runtime/src/main/java/com/kotcrab/vis/runtime/system/inflater/SpriteInflater.java index ca72c6b56..e34989a72 100644 --- a/runtime/src/main/java/com/kotcrab/vis/runtime/system/inflater/SpriteInflater.java +++ b/runtime/src/main/java/com/kotcrab/vis/runtime/system/inflater/SpriteInflater.java @@ -19,10 +19,12 @@ import com.artemis.Aspect; import com.artemis.ComponentMapper; import com.badlogic.gdx.assets.AssetManager; +import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.kotcrab.vis.runtime.RuntimeConfiguration; import com.kotcrab.vis.runtime.assets.AtlasRegionAsset; +import com.kotcrab.vis.runtime.assets.TextureAsset; import com.kotcrab.vis.runtime.assets.TextureRegionAsset; import com.kotcrab.vis.runtime.assets.VisAssetDescriptor; import com.kotcrab.vis.runtime.component.AssetReference; @@ -57,25 +59,36 @@ public void inserted (int entityId) { String atlasPath; String atlasRegion; + TextureAtlas atlas; + TextureRegion region; if (asset instanceof TextureRegionAsset) { TextureRegionAsset regionAsset = (TextureRegionAsset) asset; atlasPath = sceneTextureAtlasPath; atlasRegion = PathUtils.removeExtension(regionAsset.getPath()); + atlas = manager.get(atlasPath, TextureAtlas.class); + region = atlas.findRegion(atlasRegion); } else if (asset instanceof AtlasRegionAsset) { AtlasRegionAsset regionAsset = (AtlasRegionAsset) asset; atlasPath = regionAsset.getPath(); atlasRegion = regionAsset.getRegionName(); + atlas = manager.get(atlasPath, TextureAtlas.class); + region = atlas.findRegion(atlasRegion); + } else if (asset instanceof TextureAsset) { + TextureAsset textureAsset = (TextureAsset) asset; + manager.load(textureAsset.getPath(), Texture.class); + manager.finishLoading(); + Texture texture = manager.get(textureAsset.getPath(), Texture.class); + region = new TextureRegion(texture); + atlasRegion = textureAsset.getPath(); } else { throw new UnsupportedAssetDescriptorException(asset); } - TextureAtlas atlas = manager.get(atlasPath, TextureAtlas.class); - TextureRegion region = atlas.findRegion(atlasRegion); - if (region == null) throw new IllegalStateException("Can't load scene, gfx asset is missing: " + atlasRegion); + if (region == null) throw new IllegalStateException("Can't load scene, gfx asset is missing: " + atlasRegion); VisSprite sprite = spriteCm.create(entityId); sprite.setRegion(region); protoCm.get(entityId).fill(sprite); diff --git a/runtime/src/main/java/com/kotcrab/vis/runtime/util/font/LazyBitmapFont.java b/runtime/src/main/java/com/kotcrab/vis/runtime/util/font/LazyBitmapFont.java new file mode 100644 index 000000000..d738a2105 --- /dev/null +++ b/runtime/src/main/java/com/kotcrab/vis/runtime/util/font/LazyBitmapFont.java @@ -0,0 +1,147 @@ +package com.kotcrab.vis.runtime.util.font; + +import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.Pixmap; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.graphics.g2d.freetype.FreeType; +import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; +import com.badlogic.gdx.utils.GdxRuntimeException; + +import java.lang.reflect.Field; + +/** + * Created by dotw on 2017/11/23. + */ +public class LazyBitmapFont extends BitmapFont { + + private FreeTypeFontGenerator generator; + private FreeTypeFontGenerator.FreeTypeBitmapFontData data; + private FreeTypeFontGenerator.FreeTypeFontParameter parameter; + + public LazyBitmapFont(FreeTypeFontGenerator generator, FreeTypeFontGenerator.FreeTypeFontParameter param) { + super(); + if (generator == null) + throw new GdxRuntimeException("lazyBitmapFont global generator must be not null to use this constructor."); + this.generator = generator; + this.parameter = param; + this.data = new LazyBitmapFontData(generator, param.size, this); + try { + Field f = getClass().getSuperclass().getDeclaredField("data"); + f.setAccessible(true); + f.set(this, data); + } catch (Exception e) { + e.printStackTrace(); + } + + genrateData(); + } + + private void genrateData() { + FreeType.Face face = null; + try { + Field field = generator.getClass().getDeclaredField("face"); + field.setAccessible(true); + face = (FreeType.Face) field.get(generator); + } catch (Exception e) { + e.printStackTrace(); + return; + } + + // set general font data + FreeType.SizeMetrics fontMetrics = face.getSize().getMetrics(); + + // Set space glyph. + Glyph spaceGlyph = data.getGlyph(' '); + if (spaceGlyph == null) { + spaceGlyph = new Glyph(); + spaceGlyph.xadvance = (int) data.spaceWidth;//.spaceXadvance; + spaceGlyph.id = (int) ' '; + data.setGlyph(' ', spaceGlyph); + } + if (spaceGlyph.width == 0) + spaceGlyph.width = (int) (spaceGlyph.xadvance + data.padRight); + + // set general font data + data.flipped = parameter.flip; + data.ascent = FreeType.toInt(fontMetrics.getAscender()); + data.descent = FreeType.toInt(fontMetrics.getDescender()); + data.lineHeight = FreeType.toInt(fontMetrics.getHeight()); + + // determine x-height + for (char xChar : data.xChars) { + if (!face.loadChar(xChar, FreeType.FT_LOAD_DEFAULT)) + continue; + data.xHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight()); + break; + } + if (data.xHeight == 0) + throw new GdxRuntimeException("No x-height character found in font"); + for (char capChar : data.capChars) { + if (!face.loadChar(capChar, FreeType.FT_LOAD_DEFAULT)) + continue; + data.capHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight()); + break; + } + + // determine cap height + if (data.capHeight == 1) + throw new GdxRuntimeException("No cap character found in font"); + data.ascent = data.ascent - data.capHeight; + data.down = -data.lineHeight; + if (parameter.flip) { + data.ascent = -data.ascent; + data.down = -data.down; + } + + } + + @Override + public void dispose() { + setOwnsTexture(true); + super.dispose(); + data.dispose(); + + } + + public static class LazyBitmapFontData extends FreeTypeFontGenerator.FreeTypeBitmapFontData { + + private FreeTypeFontGenerator generator; + private int fontSize; + private LazyBitmapFont font; + private int page = 1; + + public LazyBitmapFontData(FreeTypeFontGenerator generator, int fontSize, LazyBitmapFont lbf) { + this.generator = generator; + this.fontSize = fontSize; + this.font = lbf; + } + + public Glyph getGlyph(char ch) { + Glyph glyph = super.getGlyph(ch); + if (glyph == null && ch != 0) + glyph = generateGlyph(ch); + return glyph; + } + + protected Glyph generateGlyph(char ch) { + FreeTypeFontGenerator.GlyphAndBitmap gab = generator.generateGlyphAndBitmap(ch, fontSize, false); + if (gab == null || gab.bitmap == null) + return null; + + Pixmap map = gab.bitmap.getPixmap(Pixmap.Format.RGBA8888, Color.WHITE, 1); + TextureRegion rg = new TextureRegion(new Texture(map)); + map.dispose(); + + font.getRegions().add(rg); + + gab.glyph.page = page++; + super.setGlyph(ch, gab.glyph); + setGlyphRegion(gab.glyph, rg); + + return gab.glyph; + } + + } +} diff --git a/runtime/src/main/java/com/kotcrab/vis/runtime/util/json/RuntimeJsonTags.java b/runtime/src/main/java/com/kotcrab/vis/runtime/util/json/RuntimeJsonTags.java index 412a7d3c0..5f1caf69c 100644 --- a/runtime/src/main/java/com/kotcrab/vis/runtime/util/json/RuntimeJsonTags.java +++ b/runtime/src/main/java/com/kotcrab/vis/runtime/util/json/RuntimeJsonTags.java @@ -41,6 +41,7 @@ public static void registerTags (JsonTagRegistrar registrar) { registrar.register("ShaderAsset", ShaderAsset.class); registrar.register("SoundAsset", SoundAsset.class); registrar.register("TextureRegionAsset", TextureRegionAsset.class); + registrar.register("TextureAsset", TextureAsset.class); registrar.register("TtfFontAsset", TtfFontAsset.class); registrar.register("Transform", Transform.class); diff --git a/settings.gradle b/settings.gradle index 0180d9002..d706434b0 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,4 @@ -def visuiOnly = true +def visuiOnly = false if (visuiOnly) { rootProject.name = 'vis-ui' diff --git a/ui/assets-raw/icon-folder-new.svg b/ui/assets-raw/icon-folder-new.svg new file mode 100644 index 000000000..e69de29bb diff --git a/ui/build.gradle b/ui/build.gradle index f7658ac3b..de1dc8261 100644 --- a/ui/build.gradle +++ b/ui/build.gradle @@ -11,6 +11,7 @@ sourceCompatibility = 1.6 dependencies { compile "com.badlogicgames.gdx:gdx:$gdxVersion" + compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" provided "com.apple:AppleJavaExtensions:$appleJavaExtensionsVersion", optional testCompile "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion" testCompile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" diff --git a/ui/src/main/java/com/kotcrab/vis/ui/VisUI.java b/ui/src/main/java/com/kotcrab/vis/ui/VisUI.java index 46babc78f..53f730128 100644 --- a/ui/src/main/java/com/kotcrab/vis/ui/VisUI.java +++ b/ui/src/main/java/com/kotcrab/vis/ui/VisUI.java @@ -20,9 +20,13 @@ import com.badlogic.gdx.Version; import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.files.FileHandle; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.TextureAtlas; +import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.GdxRuntimeException; +import com.kotcrab.vis.ui.util.font.LazyBitmapFont; /** * Allows to easily load VisUI skin and change default title alignment and I18N bundles. @@ -41,12 +45,20 @@ public class VisUI { private static SkinScale scale; private static Skin skin; + private static FreeTypeFontGenerator defaultFontGenerator; + private static FreeTypeFontGenerator smallFontGenerator; + + private static LazyBitmapFont defaultFont; + private static LazyBitmapFont smallFont; + /** Defines possible built-in skin scales. */ public enum SkinScale { /** Standard VisUI skin */ X1("com/kotcrab/vis/ui/skin/x1/uiskin.json", "default"), /** VisUI skin 2x upscaled */ - X2("com/kotcrab/vis/ui/skin/x2/uiskin.json", "x2"); + X2("com/kotcrab/vis/ui/skin/x2/uiskin.json", "x2"), + /** Support Chinese characters and English **/ + CH("com/kotcrab/vis/ui/skin/ch/", "x2"); private final String classpath; private final String sizesName; @@ -63,18 +75,42 @@ public FileHandle getSkinFile () { public String getSizesName () { return sizesName; } + + public String getClasspath() { return classpath; } } /** Loads default VisUI skin with {@link SkinScale#X1}. */ public static void load () { - load(SkinScale.X1); + load(SkinScale.CH); } /** Loads default VisUI skin for given {@link SkinScale}. */ - public static void load (SkinScale scale) { - VisUI.scale = scale; - load(scale.getSkinFile()); - } + public static void load(SkinScale scale) { + VisUI.scale = scale; + if (scale == SkinScale.CH) { + //generate default font + defaultFontGenerator = new FreeTypeFontGenerator(Gdx.files.classpath(scale.getClasspath() + "msyhbd.ttf")); + FreeTypeFontGenerator.FreeTypeFontParameter param = new FreeTypeFontGenerator.FreeTypeFontParameter(); + param.incremental = true; + param.size = 14; + defaultFont = new LazyBitmapFont(defaultFontGenerator, param); + //generate small font + smallFontGenerator = new FreeTypeFontGenerator(Gdx.files.classpath(scale.getClasspath() + "msyhbd.ttf")); + FreeTypeFontGenerator.FreeTypeFontParameter param2 = new FreeTypeFontGenerator.FreeTypeFontParameter(); + param2.incremental = true; + param2.size = 8; + smallFont = new LazyBitmapFont(smallFontGenerator, param); + //compose skin + Skin skin = new Skin(); + skin.addRegions(new TextureAtlas(Gdx.files.internal(scale.getClasspath() + "uiskin.atlas"))); + skin.add("default-font", defaultFont, BitmapFont.class); + skin.add("small-font", smallFont, BitmapFont.class); + skin.load(Gdx.files.internal(scale.getClasspath() + "uiskin.json")); + VisUI.skin = skin; + } else { + load(scale.getSkinFile()); + } + } /** Loads skin from provided internal file path. Skin must be compatible with default VisUI skin. */ public static void load (String internalVisSkinPath) { @@ -116,7 +152,15 @@ public static void dispose () { */ public static void dispose (boolean disposeSkin) { if (skin != null) { - if (disposeSkin) skin.dispose(); + if (disposeSkin) { + skin.dispose(); + if(scale==SkinScale.CH) { + defaultFontGenerator.dispose(); + smallFontGenerator.dispose(); + defaultFont.dispose(); + smallFont.dispose(); + } + } skin = null; } } diff --git a/ui/src/main/java/com/kotcrab/vis/ui/util/font/LazyBitmapFont.java b/ui/src/main/java/com/kotcrab/vis/ui/util/font/LazyBitmapFont.java new file mode 100644 index 000000000..c9d9ab68b --- /dev/null +++ b/ui/src/main/java/com/kotcrab/vis/ui/util/font/LazyBitmapFont.java @@ -0,0 +1,147 @@ +package com.kotcrab.vis.ui.util.font; + +import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.Pixmap; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.graphics.g2d.freetype.FreeType; +import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; +import com.badlogic.gdx.utils.GdxRuntimeException; + +import java.lang.reflect.Field; + +/** + * Created by dotw on 2017/11/23. + */ +public class LazyBitmapFont extends BitmapFont { + + private FreeTypeFontGenerator generator; + private FreeTypeFontGenerator.FreeTypeBitmapFontData data; + private FreeTypeFontGenerator.FreeTypeFontParameter parameter; + + public LazyBitmapFont(FreeTypeFontGenerator generator, FreeTypeFontGenerator.FreeTypeFontParameter param) { + super(); + if (generator == null) + throw new GdxRuntimeException("lazyBitmapFont global generator must be not null to use this constructor."); + this.generator = generator; + this.parameter = param; + this.data = new LazyBitmapFontData(generator, param.size, this); + try { + Field f = getClass().getSuperclass().getDeclaredField("data"); + f.setAccessible(true); + f.set(this, data); + } catch (Exception e) { + e.printStackTrace(); + } + + genrateData(); + } + + private void genrateData() { + FreeType.Face face = null; + try { + Field field = generator.getClass().getDeclaredField("face"); + field.setAccessible(true); + face = (FreeType.Face) field.get(generator); + } catch (Exception e) { + e.printStackTrace(); + return; + } + + // set general font data + FreeType.SizeMetrics fontMetrics = face.getSize().getMetrics(); + + // Set space glyph. + Glyph spaceGlyph = data.getGlyph(' '); + if (spaceGlyph == null) { + spaceGlyph = new Glyph(); + spaceGlyph.xadvance = (int) data.spaceWidth;//.spaceXadvance; + spaceGlyph.id = (int) ' '; + data.setGlyph(' ', spaceGlyph); + } + if (spaceGlyph.width == 0) + spaceGlyph.width = (int) (spaceGlyph.xadvance + data.padRight); + + // set general font data + data.flipped = parameter.flip; + data.ascent = FreeType.toInt(fontMetrics.getAscender()); + data.descent = FreeType.toInt(fontMetrics.getDescender()); + data.lineHeight = FreeType.toInt(fontMetrics.getHeight()); + + // determine x-height + for (char xChar : data.xChars) { + if (!face.loadChar(xChar, FreeType.FT_LOAD_DEFAULT)) + continue; + data.xHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight()); + break; + } + if (data.xHeight == 0) + throw new GdxRuntimeException("No x-height character found in font"); + for (char capChar : data.capChars) { + if (!face.loadChar(capChar, FreeType.FT_LOAD_DEFAULT)) + continue; + data.capHeight = FreeType.toInt(face.getGlyph().getMetrics().getHeight()); + break; + } + + // determine cap height + if (data.capHeight == 1) + throw new GdxRuntimeException("No cap character found in font"); + data.ascent = data.ascent - data.capHeight; + data.down = -data.lineHeight; + if (parameter.flip) { + data.ascent = -data.ascent; + data.down = -data.down; + } + + } + + @Override + public void dispose() { + setOwnsTexture(true); + super.dispose(); + data.dispose(); + + } + + public static class LazyBitmapFontData extends FreeTypeFontGenerator.FreeTypeBitmapFontData { + + private FreeTypeFontGenerator generator; + private int fontSize; + private LazyBitmapFont font; + private int page = 1; + + public LazyBitmapFontData(FreeTypeFontGenerator generator, int fontSize, LazyBitmapFont lbf) { + this.generator = generator; + this.fontSize = fontSize; + this.font = lbf; + } + + public Glyph getGlyph(char ch) { + Glyph glyph = super.getGlyph(ch); + if (glyph == null && ch != 0) + glyph = generateGlyph(ch); + return glyph; + } + + protected Glyph generateGlyph(char ch) { + FreeTypeFontGenerator.GlyphAndBitmap gab = generator.generateGlyphAndBitmap(ch, fontSize, false); + if (gab == null || gab.bitmap == null) + return null; + + Pixmap map = gab.bitmap.getPixmap(Pixmap.Format.RGBA8888, Color.WHITE, 1); + TextureRegion rg = new TextureRegion(new Texture(map)); + map.dispose(); + + font.getRegions().add(rg); + + gab.glyph.page = page++; + super.setGlyph(ch, gab.glyph); + setGlyphRegion(gab.glyph, rg); + + return gab.glyph; + } + + } +} diff --git a/ui/src/main/java/com/kotcrab/vis/ui/widget/VisValidatableTextField.java b/ui/src/main/java/com/kotcrab/vis/ui/widget/VisValidatableTextField.java index f230a085b..0ecd20740 100644 --- a/ui/src/main/java/com/kotcrab/vis/ui/widget/VisValidatableTextField.java +++ b/ui/src/main/java/com/kotcrab/vis/ui/widget/VisValidatableTextField.java @@ -87,6 +87,7 @@ public VisValidatableTextField (boolean restoreLastValid, InputValidator... vali } private void init () { + setOnlyFontChars(false); setProgrammaticChangeEvents(true); setIgnoreEqualsTextChange(false); } diff --git a/ui/src/main/java/com/kotcrab/vis/ui/widget/file/FileUtils.java b/ui/src/main/java/com/kotcrab/vis/ui/widget/file/FileUtils.java index b1e9a7053..19b69d32c 100644 --- a/ui/src/main/java/com/kotcrab/vis/ui/widget/file/FileUtils.java +++ b/ui/src/main/java/com/kotcrab/vis/ui/widget/file/FileUtils.java @@ -20,6 +20,7 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.utils.Array; +import com.badlogic.gdx.utils.Sort; import com.kotcrab.vis.ui.util.OsUtils; import java.io.File; @@ -118,8 +119,9 @@ public static Array sortFiles (FileHandle[] files, Comparator