| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- package arm;
- import haxe.Json;
- import haxe.io.Bytes;
- import kha.Display;
- import kha.WindowOptions;
- import kha.WindowMode;
- import kha.System;
- import iron.data.Data;
- import zui.Zui;
- import arm.ui.UISidebar;
- import arm.render.Inc;
- import arm.sys.File;
- import arm.sys.Path;
- import arm.Enums;
- import arm.ConfigFormat;
- import arm.KeymapFormat;
- class Config {
- public static var raw: TConfig = null;
- public static var keymap: TKeymap;
- public static var configLoaded = false;
- public static var buttonAlign = zui.Zui.Align.Left;
- public static var buttonSpacing = " ";
- public static function load(done: Void->Void) {
- try {
- Data.getBlob((Path.isProtected() ? Krom.savePath() : "") + "config.json", function(blob: kha.Blob) {
- configLoaded = true;
- raw = Json.parse(blob.toString());
- done();
- });
- }
- catch (e: Dynamic) {
- #if krom_linux
- try { // Protected directory
- Data.getBlob(Krom.savePath() + "config.json", function(blob: kha.Blob) {
- configLoaded = true;
- raw = Json.parse(blob.toString());
- done();
- });
- }
- catch (e: Dynamic) {
- done();
- }
- #else
- done();
- #end
- }
- }
- public static function save() {
- // Use system application data folder
- // when running from protected path like "Program Files"
- var path = (Path.isProtected() ? Krom.savePath() : Path.data() + Path.sep) + "config.json";
- var bytes = Bytes.ofString(Json.stringify(raw));
- Krom.fileSaveBytes(path, bytes.getData());
- #if krom_linux // Protected directory
- if (!File.exists(path)) Krom.fileSaveBytes(Krom.savePath() + "config.json", bytes.getData());
- #end
- }
- public static function init() {
- if (!configLoaded || raw == null) {
- raw = {};
- raw.locale = "system";
- raw.window_mode = 0;
- raw.window_resizable = true;
- raw.window_minimizable = true;
- raw.window_maximizable = true;
- raw.window_w = 1600;
- raw.window_h = 900;
- #if krom_darwin
- raw.window_w *= 2;
- raw.window_h *= 2;
- #end
- raw.window_x = -1;
- raw.window_y = -1;
- raw.window_scale = 1.0;
- var disp = Display.primary;
- if (disp.width >= 2560 && disp.height >= 1600) {
- raw.window_scale = 2.0;
- }
- #if (krom_android || krom_ios || krom_darwin)
- raw.window_scale = 2.0;
- #end
- raw.window_vsync = true;
- raw.window_frequency = disp.frequency;
- raw.rp_bloom = false;
- raw.rp_gi = false;
- raw.rp_vignette = 0.2;
- raw.rp_motionblur = false;
- #if (krom_android || krom_ios)
- raw.rp_ssgi = false;
- #else
- raw.rp_ssgi = true;
- #end
- raw.rp_ssr = false;
- raw.rp_supersample = 1.0;
- raw.version = Main.version;
- raw.sha = Main.sha;
- App.initConfig();
- }
- else {
- // Upgrade config format created by older ArmorPaint build
- // if (raw.version != Main.version) {
- // raw.version = Main.version;
- // save();
- // }
- if (raw.sha != Main.sha) {
- configLoaded = false;
- init();
- return;
- }
- }
- Zui.touchScroll = Zui.touchHold = Zui.touchTooltip = Config.raw.touch_ui;
- App.resHandle.position = raw.layer_res;
- loadKeymap();
- }
- public static function getOptions(): kha.SystemOptions {
- var windowMode = raw.window_mode == 0 ? WindowMode.Windowed : WindowMode.Fullscreen;
- var windowFeatures = None;
- if (raw.window_resizable) windowFeatures |= FeatureResizable;
- if (raw.window_maximizable) windowFeatures |= FeatureMaximizable;
- if (raw.window_minimizable) windowFeatures |= FeatureMinimizable;
- var title = "untitled - " + Main.title;
- return {
- title: title,
- window: {
- width: raw.window_w,
- height: raw.window_h,
- x: raw.window_x,
- y: raw.window_y,
- mode: windowMode,
- windowFeatures: windowFeatures
- },
- framebuffer: {
- samplesPerPixel: 1,
- verticalSync: raw.window_vsync,
- frequency: raw.window_frequency
- }
- };
- }
- public static function restore() {
- zui.Zui.Handle.global = new zui.Zui.Handle(); // Reset ui handles
- configLoaded = false;
- var _layout = raw.layout;
- init();
- raw.layout = _layout;
- App.initLayout();
- Translator.loadTranslations(raw.locale);
- applyConfig();
- loadTheme(raw.theme);
- }
- public static function importFrom(from: TConfig) {
- var _sha = raw.sha;
- var _version = raw.version;
- raw = from;
- raw.sha = _sha;
- raw.version = _version;
- zui.Zui.Handle.global = new zui.Zui.Handle(); // Reset ui handles
- loadKeymap();
- App.initLayout();
- Translator.loadTranslations(raw.locale);
- applyConfig();
- loadTheme(raw.theme);
- }
- public static function applyConfig() {
- Config.raw.rp_ssgi = Context.hssgi.selected;
- Config.raw.rp_ssr = Context.hssr.selected;
- Config.raw.rp_bloom = Context.hbloom.selected;
- Config.raw.rp_gi = Context.hvxao.selected;
- Config.raw.rp_supersample = getSuperSampleSize(Context.hsupersample.position);
- iron.object.Uniforms.defaultFilter = Config.raw.rp_supersample < 1.0 ? kha.graphics4.TextureFilter.PointFilter : kha.graphics4.TextureFilter.LinearFilter;
- save();
- Context.ddirty = 2;
- var current = @:privateAccess kha.graphics2.Graphics.current;
- if (current != null) current.end();
- Inc.applyConfig();
- if (current != null) current.begin(false);
- }
- public static function loadKeymap() {
- Data.getBlob("keymap_presets/" + raw.keymap, function(blob: kha.Blob) {
- keymap = Json.parse(blob.toString());
- });
- }
- public static function saveKeymap() {
- var path = Data.dataPath + "keymap_presets/" + raw.keymap;
- var bytes = Bytes.ofString(Json.stringify(keymap));
- Krom.fileSaveBytes(path, bytes.getData());
- }
- public static inline function getSuperSampleQuality(f: Float): Int {
- return f == 0.25 ? 0 :
- f == 0.5 ? 1 :
- f == 1.0 ? 2 :
- f == 1.5 ? 3 :
- f == 2.0 ? 4 : 5;
- }
- public static inline function getSuperSampleSize(i: Int): Float {
- return i == 0 ? 0.25 :
- i == 1 ? 0.5 :
- i == 2 ? 1.0 :
- i == 3 ? 1.5 :
- i == 4 ? 2.0 : 4.0;
- }
- static function getTextureRes(): Int {
- var res = App.resHandle.position;
- return res == Res128 ? 128 :
- res == Res256 ? 256 :
- res == Res512 ? 512 :
- res == Res1024 ? 1024 :
- res == Res2048 ? 2048 :
- res == Res4096 ? 4096 :
- res == Res8192 ? 8192 :
- res == Res16384 ? 16384 : 0;
- }
- public static function getTextureResX(): Int {
- return Context.projectAspectRatio == 2 ? Std.int(getTextureRes() / 2) : getTextureRes();
- }
- public static function getTextureResY(): Int {
- return Context.projectAspectRatio == 1 ? Std.int(getTextureRes() / 2) : getTextureRes();
- }
- public static function getTextureResBias(): Float {
- var res = App.resHandle.position;
- return res == Res128 ? 16.0 :
- res == Res256 ? 8.0 :
- res == Res512 ? 4.0 :
- res == Res1024 ? 2.0 :
- res == Res2048 ? 1.5 :
- res == Res4096 ? 1.0 :
- res == Res8192 ? 0.5 :
- res == Res16384 ? 0.25 : 1.0;
- }
- public static function getTextureResPos(i: Int): Int {
- return i == 128 ? Res128 :
- i == 256 ? Res256 :
- i == 512 ? Res512 :
- i == 1024 ? Res1024 :
- i == 2048 ? Res2048 :
- i == 4096 ? Res4096 :
- i == 8192 ? Res8192 :
- i == 16384 ? Res16384 : 0;
- }
- public static function loadTheme(theme: String, tagRedraw = true) {
- if (theme == "default.json") { // Built-in default
- App.theme = zui.Themes.dark;
- }
- else {
- Data.getBlob("themes/" + theme, function(b: kha.Blob) {
- App.theme = Json.parse(b.toString());
- });
- }
- App.theme.FILL_WINDOW_BG = true;
- if (tagRedraw) {
- for (ui in App.getUIs()) ui.t = App.theme;
- UISidebar.inst.tagUIRedraw();
- }
- if (Config.raw.touch_ui) {
- // Enlarge elements
- App.theme.FULL_TABS = true;
- App.theme.ELEMENT_H = 24 + 4;
- App.theme.BUTTON_H = 22 + 4;
- App.theme.FONT_SIZE = 13 + 2;
- App.theme.ARROW_SIZE = 5 + 2;
- App.theme.CHECK_SIZE = 15 + 4;
- App.theme.CHECK_SELECT_SIZE = 8 + 2;
- buttonAlign = zui.Zui.Align.Center;
- buttonSpacing = "";
- }
- else {
- App.theme.FULL_TABS = false;
- buttonAlign = zui.Zui.Align.Left;
- buttonSpacing = " ";
- }
- }
- public static function enablePlugin(f: String) {
- raw.plugins.push(f);
- Plugin.start(f);
- }
- public static function disablePlugin(f: String) {
- raw.plugins.remove(f);
- Plugin.stop(f);
- }
- }
|