Config.hx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package arm;
  2. import haxe.Json;
  3. import haxe.io.Bytes;
  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.UISidebar;
  11. import arm.render.Inc;
  12. import arm.sys.File;
  13. import arm.sys.Path;
  14. import arm.Enums;
  15. import arm.ConfigFormat;
  16. import arm.KeymapFormat;
  17. class Config {
  18. public static var raw: TConfig = null;
  19. public static var keymap: TKeymap;
  20. public static var configLoaded = false;
  21. public static var buttonAlign = zui.Zui.Align.Left;
  22. public static var buttonSpacing = " ";
  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_motionblur = false;
  88. #if (krom_android || krom_ios)
  89. raw.rp_ssgi = false;
  90. #else
  91. raw.rp_ssgi = true;
  92. #end
  93. raw.rp_ssr = false;
  94. raw.rp_supersample = 1.0;
  95. raw.version = Main.version;
  96. raw.sha = Main.sha;
  97. App.initConfig();
  98. }
  99. else {
  100. // Upgrade config format created by older ArmorPaint build
  101. // if (raw.version != Main.version) {
  102. // raw.version = Main.version;
  103. // save();
  104. // }
  105. if (raw.sha != Main.sha) {
  106. configLoaded = false;
  107. init();
  108. return;
  109. }
  110. }
  111. Zui.touchScroll = Zui.touchHold = Zui.touchTooltip = Config.raw.touch_ui;
  112. App.resHandle.position = raw.layer_res;
  113. loadKeymap();
  114. }
  115. public static function getOptions(): kha.SystemOptions {
  116. var windowMode = raw.window_mode == 0 ? WindowMode.Windowed : WindowMode.Fullscreen;
  117. var windowFeatures = None;
  118. if (raw.window_resizable) windowFeatures |= FeatureResizable;
  119. if (raw.window_maximizable) windowFeatures |= FeatureMaximizable;
  120. if (raw.window_minimizable) windowFeatures |= FeatureMinimizable;
  121. var title = "untitled - " + Main.title;
  122. return {
  123. title: title,
  124. window: {
  125. width: raw.window_w,
  126. height: raw.window_h,
  127. x: raw.window_x,
  128. y: raw.window_y,
  129. mode: windowMode,
  130. windowFeatures: windowFeatures
  131. },
  132. framebuffer: {
  133. samplesPerPixel: 1,
  134. verticalSync: raw.window_vsync,
  135. frequency: raw.window_frequency
  136. }
  137. };
  138. }
  139. public static function restore() {
  140. zui.Zui.Handle.global = new zui.Zui.Handle(); // Reset ui handles
  141. configLoaded = false;
  142. var _layout = raw.layout;
  143. init();
  144. raw.layout = _layout;
  145. App.initLayout();
  146. Translator.loadTranslations(raw.locale);
  147. applyConfig();
  148. loadTheme(raw.theme);
  149. }
  150. public static function importFrom(from: TConfig) {
  151. var _sha = raw.sha;
  152. var _version = raw.version;
  153. raw = from;
  154. raw.sha = _sha;
  155. raw.version = _version;
  156. zui.Zui.Handle.global = new zui.Zui.Handle(); // Reset ui handles
  157. loadKeymap();
  158. App.initLayout();
  159. Translator.loadTranslations(raw.locale);
  160. applyConfig();
  161. loadTheme(raw.theme);
  162. }
  163. public static function applyConfig() {
  164. Config.raw.rp_ssgi = Context.hssgi.selected;
  165. Config.raw.rp_ssr = Context.hssr.selected;
  166. Config.raw.rp_bloom = Context.hbloom.selected;
  167. Config.raw.rp_gi = Context.hvxao.selected;
  168. Config.raw.rp_supersample = getSuperSampleSize(Context.hsupersample.position);
  169. iron.object.Uniforms.defaultFilter = Config.raw.rp_supersample < 1.0 ? kha.graphics4.TextureFilter.PointFilter : kha.graphics4.TextureFilter.LinearFilter;
  170. save();
  171. Context.ddirty = 2;
  172. var current = @:privateAccess kha.graphics2.Graphics.current;
  173. if (current != null) current.end();
  174. Inc.applyConfig();
  175. if (current != null) current.begin(false);
  176. }
  177. public static function loadKeymap() {
  178. Data.getBlob("keymap_presets/" + raw.keymap, function(blob: kha.Blob) {
  179. keymap = Json.parse(blob.toString());
  180. });
  181. }
  182. public static function saveKeymap() {
  183. var path = Data.dataPath + "keymap_presets/" + raw.keymap;
  184. var bytes = Bytes.ofString(Json.stringify(keymap));
  185. Krom.fileSaveBytes(path, bytes.getData());
  186. }
  187. public static inline function getSuperSampleQuality(f: Float): Int {
  188. return f == 0.25 ? 0 :
  189. f == 0.5 ? 1 :
  190. f == 1.0 ? 2 :
  191. f == 1.5 ? 3 :
  192. f == 2.0 ? 4 : 5;
  193. }
  194. public static inline function getSuperSampleSize(i: Int): Float {
  195. return i == 0 ? 0.25 :
  196. i == 1 ? 0.5 :
  197. i == 2 ? 1.0 :
  198. i == 3 ? 1.5 :
  199. i == 4 ? 2.0 : 4.0;
  200. }
  201. static function getTextureRes(): Int {
  202. var res = App.resHandle.position;
  203. return res == Res128 ? 128 :
  204. res == Res256 ? 256 :
  205. res == Res512 ? 512 :
  206. res == Res1024 ? 1024 :
  207. res == Res2048 ? 2048 :
  208. res == Res4096 ? 4096 :
  209. res == Res8192 ? 8192 :
  210. res == Res16384 ? 16384 : 0;
  211. }
  212. public static function getTextureResX(): Int {
  213. return Context.projectAspectRatio == 2 ? Std.int(getTextureRes() / 2) : getTextureRes();
  214. }
  215. public static function getTextureResY(): Int {
  216. return Context.projectAspectRatio == 1 ? Std.int(getTextureRes() / 2) : getTextureRes();
  217. }
  218. public static function getTextureResBias(): Float {
  219. var res = App.resHandle.position;
  220. return res == Res128 ? 16.0 :
  221. res == Res256 ? 8.0 :
  222. res == Res512 ? 4.0 :
  223. res == Res1024 ? 2.0 :
  224. res == Res2048 ? 1.5 :
  225. res == Res4096 ? 1.0 :
  226. res == Res8192 ? 0.5 :
  227. res == Res16384 ? 0.25 : 1.0;
  228. }
  229. public static function getTextureResPos(i: Int): Int {
  230. return i == 128 ? Res128 :
  231. i == 256 ? Res256 :
  232. i == 512 ? Res512 :
  233. i == 1024 ? Res1024 :
  234. i == 2048 ? Res2048 :
  235. i == 4096 ? Res4096 :
  236. i == 8192 ? Res8192 :
  237. i == 16384 ? Res16384 : 0;
  238. }
  239. public static function loadTheme(theme: String, tagRedraw = true) {
  240. if (theme == "default.json") { // Built-in default
  241. App.theme = zui.Themes.dark;
  242. }
  243. else {
  244. Data.getBlob("themes/" + theme, function(b: kha.Blob) {
  245. App.theme = Json.parse(b.toString());
  246. });
  247. }
  248. App.theme.FILL_WINDOW_BG = true;
  249. if (tagRedraw) {
  250. for (ui in App.getUIs()) ui.t = App.theme;
  251. UISidebar.inst.tagUIRedraw();
  252. }
  253. if (Config.raw.touch_ui) {
  254. // Enlarge elements
  255. App.theme.FULL_TABS = true;
  256. App.theme.ELEMENT_H = 24 + 4;
  257. App.theme.BUTTON_H = 22 + 4;
  258. App.theme.FONT_SIZE = 13 + 2;
  259. App.theme.ARROW_SIZE = 5 + 2;
  260. App.theme.CHECK_SIZE = 15 + 4;
  261. App.theme.CHECK_SELECT_SIZE = 8 + 2;
  262. buttonAlign = zui.Zui.Align.Center;
  263. buttonSpacing = "";
  264. }
  265. else {
  266. App.theme.FULL_TABS = false;
  267. buttonAlign = zui.Zui.Align.Left;
  268. buttonSpacing = " ";
  269. }
  270. }
  271. public static function enablePlugin(f: String) {
  272. raw.plugins.push(f);
  273. Plugin.start(f);
  274. }
  275. public static function disablePlugin(f: String) {
  276. raw.plugins.remove(f);
  277. Plugin.stop(f);
  278. }
  279. }