Config.hx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package arm;
  2. import haxe.io.Bytes;
  3. import haxe.Json;
  4. import kha.Display;
  5. import kha.WindowOptions;
  6. import kha.WindowMode;
  7. import kha.System;
  8. import iron.data.Data;
  9. import zui.Zui;
  10. import arm.ui.UIBase;
  11. import arm.ui.UIHeader;
  12. import arm.render.RenderPathBase;
  13. import arm.sys.File;
  14. import arm.sys.Path;
  15. import arm.ConfigFormat;
  16. class Config {
  17. public static var raw: TConfig = null;
  18. public static var keymap: Dynamic;
  19. public static var configLoaded = false;
  20. public static var buttonAlign = zui.Zui.Align.Left;
  21. public static inline var defaultButtonSpacing = " ";
  22. public static var buttonSpacing = defaultButtonSpacing;
  23. public static function load(done: Void->Void) {
  24. try {
  25. Data.getBlob((Path.isProtected() ? Krom.savePath() : "") + "config.json", function(blob: kha.Blob) {
  26. configLoaded = true;
  27. raw = Json.parse(blob.toString());
  28. done();
  29. });
  30. }
  31. catch (e: Dynamic) {
  32. #if krom_linux
  33. try { // Protected directory
  34. Data.getBlob(Krom.savePath() + "config.json", function(blob: kha.Blob) {
  35. configLoaded = true;
  36. raw = Json.parse(blob.toString());
  37. done();
  38. });
  39. }
  40. catch (e: Dynamic) {
  41. done();
  42. }
  43. #else
  44. done();
  45. #end
  46. }
  47. }
  48. public static function save() {
  49. // Use system application data folder
  50. // when running from protected path like "Program Files"
  51. var path = (Path.isProtected() ? Krom.savePath() : Path.data() + Path.sep) + "config.json";
  52. var bytes = Bytes.ofString(Json.stringify(raw));
  53. Krom.fileSaveBytes(path, bytes.getData());
  54. #if krom_linux // Protected directory
  55. if (!File.exists(path)) Krom.fileSaveBytes(Krom.savePath() + "config.json", bytes.getData());
  56. #end
  57. }
  58. public static function init() {
  59. if (!configLoaded || raw == null) {
  60. raw = {};
  61. raw.locale = "system";
  62. raw.window_mode = 0;
  63. raw.window_resizable = true;
  64. raw.window_minimizable = true;
  65. raw.window_maximizable = true;
  66. raw.window_w = 1600;
  67. raw.window_h = 900;
  68. #if krom_darwin
  69. raw.window_w *= 2;
  70. raw.window_h *= 2;
  71. #end
  72. raw.window_x = -1;
  73. raw.window_y = -1;
  74. raw.window_scale = 1.0;
  75. var disp = Display.primary;
  76. if (disp.width >= 2560 && disp.height >= 1600) {
  77. raw.window_scale = 2.0;
  78. }
  79. #if (krom_android || krom_ios || krom_darwin)
  80. raw.window_scale = 2.0;
  81. #end
  82. raw.window_vsync = true;
  83. raw.window_frequency = disp.frequency;
  84. raw.rp_bloom = false;
  85. raw.rp_gi = false;
  86. raw.rp_vignette = 0.2;
  87. raw.rp_grain = 0.09;
  88. raw.rp_motionblur = false;
  89. #if (krom_android || krom_ios)
  90. raw.rp_ssao = false;
  91. #else
  92. raw.rp_ssao = true;
  93. #end
  94. raw.rp_ssr = false;
  95. raw.rp_supersample = 1.0;
  96. raw.version = Manifest.version;
  97. raw.sha = Main.sha;
  98. App.initConfig();
  99. }
  100. else {
  101. // Upgrade config format created by older ArmorPaint build
  102. // if (raw.version != Manifest.version) {
  103. // raw.version = Manifest.version;
  104. // save();
  105. // }
  106. if (raw.sha != Main.sha) {
  107. configLoaded = false;
  108. init();
  109. return;
  110. }
  111. }
  112. Zui.touchScroll = Zui.touchHold = Zui.touchTooltip = Config.raw.touch_ui;
  113. App.resHandle.position = raw.layer_res;
  114. loadKeymap();
  115. }
  116. public static function getOptions(): kha.SystemOptions {
  117. var windowMode = raw.window_mode == 0 ? WindowMode.Windowed : WindowMode.Fullscreen;
  118. var windowFeatures = None;
  119. if (raw.window_resizable) windowFeatures |= FeatureResizable;
  120. if (raw.window_maximizable) windowFeatures |= FeatureMaximizable;
  121. if (raw.window_minimizable) windowFeatures |= FeatureMinimizable;
  122. var title = "untitled - " + Manifest.title;
  123. return {
  124. title: title,
  125. window: {
  126. width: raw.window_w,
  127. height: raw.window_h,
  128. x: raw.window_x,
  129. y: raw.window_y,
  130. mode: windowMode,
  131. windowFeatures: windowFeatures
  132. },
  133. framebuffer: {
  134. samplesPerPixel: 1,
  135. verticalSync: raw.window_vsync,
  136. frequency: raw.window_frequency
  137. }
  138. };
  139. }
  140. public static function restore() {
  141. zui.Zui.Handle.global = new zui.Zui.Handle(); // Reset ui handles
  142. configLoaded = false;
  143. var _layout = raw.layout;
  144. init();
  145. raw.layout = _layout;
  146. App.initLayout();
  147. Translator.loadTranslations(raw.locale);
  148. applyConfig();
  149. loadTheme(raw.theme);
  150. }
  151. public static function importFrom(from: TConfig) {
  152. var _sha = raw.sha;
  153. var _version = raw.version;
  154. raw = from;
  155. raw.sha = _sha;
  156. raw.version = _version;
  157. zui.Zui.Handle.global = new zui.Zui.Handle(); // Reset ui handles
  158. loadKeymap();
  159. App.initLayout();
  160. Translator.loadTranslations(raw.locale);
  161. applyConfig();
  162. loadTheme(raw.theme);
  163. }
  164. public static function applyConfig() {
  165. Config.raw.rp_ssao = Context.raw.hssao.selected;
  166. Config.raw.rp_ssr = Context.raw.hssr.selected;
  167. Config.raw.rp_bloom = Context.raw.hbloom.selected;
  168. Config.raw.rp_gi = Context.raw.hvxao.selected;
  169. Config.raw.rp_supersample = getSuperSampleSize(Context.raw.hsupersample.position);
  170. iron.object.Uniforms.defaultFilter = Config.raw.rp_supersample < 1.0 ? kha.graphics4.TextureFilter.PointFilter : kha.graphics4.TextureFilter.LinearFilter;
  171. save();
  172. Context.raw.ddirty = 2;
  173. var current = @:privateAccess kha.graphics2.Graphics.current;
  174. if (current != null) current.end();
  175. RenderPathBase.applyConfig();
  176. if (current != null) current.begin(false);
  177. }
  178. public static function loadKeymap() {
  179. if (raw.keymap == "default.json") { // Built-in default
  180. keymap = App.defaultKeymap;
  181. }
  182. else {
  183. Data.getBlob("keymap_presets/" + raw.keymap, function(blob: kha.Blob) {
  184. keymap = Json.parse(blob.toString());
  185. // Fill in undefined keys with defaults
  186. for (field in Reflect.fields(App.defaultKeymap)) {
  187. if (!Reflect.hasField(keymap, field)) {
  188. Reflect.setField(keymap, field, Reflect.field(App.defaultKeymap, field));
  189. }
  190. }
  191. });
  192. }
  193. }
  194. public static function saveKeymap() {
  195. if (raw.keymap == "default.json") return;
  196. var path = Data.dataPath + "keymap_presets/" + raw.keymap;
  197. var bytes = Bytes.ofString(Json.stringify(keymap));
  198. Krom.fileSaveBytes(path, bytes.getData());
  199. }
  200. public static inline function getSuperSampleQuality(f: Float): Int {
  201. return f == 0.25 ? 0 :
  202. f == 0.5 ? 1 :
  203. f == 1.0 ? 2 :
  204. f == 1.5 ? 3 :
  205. f == 2.0 ? 4 : 5;
  206. }
  207. public static inline function getSuperSampleSize(i: Int): Float {
  208. return i == 0 ? 0.25 :
  209. i == 1 ? 0.5 :
  210. i == 2 ? 1.0 :
  211. i == 3 ? 1.5 :
  212. i == 4 ? 2.0 : 4.0;
  213. }
  214. static function getTextureRes(): Int {
  215. var res = App.resHandle.position;
  216. return res == Res128 ? 128 :
  217. res == Res256 ? 256 :
  218. res == Res512 ? 512 :
  219. res == Res1024 ? 1024 :
  220. res == Res2048 ? 2048 :
  221. res == Res4096 ? 4096 :
  222. res == Res8192 ? 8192 :
  223. res == Res16384 ? 16384 : 0;
  224. }
  225. public static function getTextureResX(): Int {
  226. return Context.raw.projectAspectRatio == 2 ? Std.int(getTextureRes() / 2) : getTextureRes();
  227. }
  228. public static function getTextureResY(): Int {
  229. return Context.raw.projectAspectRatio == 1 ? Std.int(getTextureRes() / 2) : getTextureRes();
  230. }
  231. public static function getTextureResBias(): Float {
  232. var res = App.resHandle.position;
  233. return res == Res128 ? 16.0 :
  234. res == Res256 ? 8.0 :
  235. res == Res512 ? 4.0 :
  236. res == Res1024 ? 2.0 :
  237. res == Res2048 ? 1.5 :
  238. res == Res4096 ? 1.0 :
  239. res == Res8192 ? 0.5 :
  240. res == Res16384 ? 0.25 : 1.0;
  241. }
  242. public static function getTextureResPos(i: Int): Int {
  243. return i == 128 ? Res128 :
  244. i == 256 ? Res256 :
  245. i == 512 ? Res512 :
  246. i == 1024 ? Res1024 :
  247. i == 2048 ? Res2048 :
  248. i == 4096 ? Res4096 :
  249. i == 8192 ? Res8192 :
  250. i == 16384 ? Res16384 : 0;
  251. }
  252. public static function loadTheme(theme: String, tagRedraw = true) {
  253. if (theme == "default.json") { // Built-in default
  254. App.theme = zui.Themes.dark;
  255. }
  256. else {
  257. Data.getBlob("themes/" + theme, function(b: kha.Blob) {
  258. App.theme = Json.parse(b.toString());
  259. });
  260. }
  261. App.theme.FILL_WINDOW_BG = true;
  262. if (tagRedraw) {
  263. for (ui in App.getUIs()) ui.t = App.theme;
  264. UIBase.inst.tagUIRedraw();
  265. }
  266. if (Config.raw.touch_ui) {
  267. // Enlarge elements
  268. App.theme.FULL_TABS = true;
  269. App.theme.ELEMENT_H = 24 + 6;
  270. App.theme.BUTTON_H = 22 + 6;
  271. App.theme.FONT_SIZE = 13 + 2;
  272. App.theme.ARROW_SIZE = 5 + 2;
  273. App.theme.CHECK_SIZE = 15 + 4;
  274. App.theme.CHECK_SELECT_SIZE = 8 + 2;
  275. buttonAlign = zui.Zui.Align.Left;
  276. buttonSpacing = "";
  277. }
  278. else {
  279. App.theme.FULL_TABS = false;
  280. buttonAlign = zui.Zui.Align.Left;
  281. buttonSpacing = defaultButtonSpacing;
  282. }
  283. }
  284. public static function enablePlugin(f: String) {
  285. raw.plugins.push(f);
  286. Plugin.start(f);
  287. }
  288. public static function disablePlugin(f: String) {
  289. raw.plugins.remove(f);
  290. Plugin.stop(f);
  291. }
  292. }