config.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. let config_raw: config_t = null;
  2. let config_keymap: map_t<string, string>;
  3. let config_loaded: bool = false;
  4. let config_button_align: ui_align_t = ui_align_t.LEFT;
  5. let config_default_button_spacing: string = " ";
  6. let config_button_spacing: string = config_default_button_spacing;
  7. function config_load() {
  8. let path: string = "";
  9. if (path_is_protected()) {
  10. path += iron_internal_save_path();
  11. }
  12. path += "config.json";
  13. let blob: buffer_t = data_get_blob(path);
  14. ///if arm_linux
  15. if (blob == null) { // Protected directory
  16. blob = data_get_blob(iron_internal_save_path() + "config.json");
  17. }
  18. ///end
  19. if (blob != null) {
  20. config_loaded = true;
  21. config_raw = json_parse(sys_buffer_to_string(blob));
  22. }
  23. }
  24. function config_save() {
  25. // Use system application data folder
  26. // when running from protected path like "Program Files"
  27. let path: string = "";
  28. if (path_is_protected()) {
  29. path += iron_internal_save_path();
  30. }
  31. else {
  32. path += path_data();
  33. path += path_sep;
  34. }
  35. path += "config.json";
  36. json_encode_begin();
  37. json_encode_string("locale", config_raw.locale);
  38. json_encode_i32("window_mode", config_raw.window_mode);
  39. json_encode_i32("window_w", config_raw.window_w);
  40. json_encode_i32("window_h", config_raw.window_h);
  41. json_encode_i32("window_x", config_raw.window_x);
  42. json_encode_i32("window_y", config_raw.window_y);
  43. json_encode_bool("window_resizable", config_raw.window_resizable);
  44. json_encode_bool("window_maximizable", config_raw.window_maximizable);
  45. json_encode_bool("window_minimizable", config_raw.window_minimizable);
  46. json_encode_bool("window_vsync", config_raw.window_vsync);
  47. json_encode_i32("window_frequency", config_raw.window_frequency);
  48. json_encode_f32("window_scale", config_raw.window_scale);
  49. json_encode_f32("rp_supersample", config_raw.rp_supersample);
  50. json_encode_bool("rp_ssao", config_raw.rp_ssao);
  51. json_encode_bool("rp_bloom", config_raw.rp_bloom);
  52. json_encode_bool("rp_gi", config_raw.rp_gi);
  53. json_encode_f32("rp_vignette", config_raw.rp_vignette);
  54. json_encode_f32("rp_grain", config_raw.rp_grain);
  55. json_encode_string("version", config_raw.version);
  56. json_encode_string("sha", config_raw.sha);
  57. json_encode_string_array("recent_projects", config_raw.recent_projects);
  58. json_encode_string_array("bookmarks", config_raw.bookmarks);
  59. json_encode_string_array("plugins", config_raw.plugins);
  60. json_encode_string("keymap", config_raw.keymap);
  61. json_encode_string("theme", config_raw.theme);
  62. json_encode_i32("undo_steps", config_raw.undo_steps);
  63. json_encode_f32("camera_pan_speed", config_raw.camera_pan_speed);
  64. json_encode_f32("camera_zoom_speed", config_raw.camera_zoom_speed);
  65. json_encode_f32("camera_rotation_speed", config_raw.camera_rotation_speed);
  66. json_encode_i32("zoom_direction", config_raw.zoom_direction);
  67. json_encode_bool("wrap_mouse", config_raw.wrap_mouse);
  68. json_encode_bool("show_asset_names", config_raw.show_asset_names);
  69. json_encode_bool("touch_ui", config_raw.touch_ui);
  70. json_encode_bool("splash_screen", config_raw.splash_screen);
  71. json_encode_i32_array("layout", config_raw.layout);
  72. json_encode_i32_array("layout_tabs", config_raw.layout_tabs);
  73. json_encode_i32("workspace", config_raw.workspace);
  74. json_encode_i32("camera_controls", config_raw.camera_controls);
  75. json_encode_string("server", config_raw.server);
  76. json_encode_i32("viewport_mode", config_raw.viewport_mode);
  77. json_encode_i32("pathtrace_mode", config_raw.pathtrace_mode);
  78. json_encode_bool("pressure_radius", config_raw.pressure_radius);
  79. json_encode_f32("pressure_sensitivity", config_raw.pressure_sensitivity);
  80. json_encode_f32("displace_strength", config_raw.displace_strength);
  81. json_encode_i32("layer_res", config_raw.layer_res);
  82. json_encode_bool("brush_live", config_raw.brush_live);
  83. json_encode_bool("brush_3d", config_raw.brush_3d);
  84. json_encode_bool("node_preview", config_raw.node_preview);
  85. json_encode_bool("pressure_hardness", config_raw.pressure_hardness);
  86. json_encode_bool("pressure_angle", config_raw.pressure_angle);
  87. json_encode_bool("pressure_opacity", config_raw.pressure_opacity);
  88. json_encode_bool("material_live", config_raw.material_live);
  89. json_encode_bool("brush_depth_reject", config_raw.brush_depth_reject);
  90. json_encode_bool("brush_angle_reject", config_raw.brush_angle_reject);
  91. json_encode_i32("dilate", config_raw.dilate);
  92. json_encode_i32("dilate_radius", config_raw.dilate_radius);
  93. json_encode_bool("gpu_inference", config_raw.gpu_inference);
  94. json_encode_string("blender", config_raw.blender);
  95. json_encode_i32("atlas_res", config_raw.atlas_res);
  96. json_encode_bool("grid_snap", config_raw.grid_snap);
  97. let config_json: string = json_encode_end();
  98. let buffer: buffer_t = sys_string_to_buffer(config_json);
  99. iron_file_save_bytes(path, buffer, 0);
  100. ///if arm_linux // Protected directory
  101. if (!file_exists(path)) {
  102. iron_file_save_bytes(iron_internal_save_path() + "config.json", buffer, 0);
  103. }
  104. ///end
  105. }
  106. function config_init() {
  107. if (!config_loaded || config_raw == null) {
  108. config_raw = {};
  109. config_raw.locale = "system";
  110. config_raw.window_mode = 0;
  111. config_raw.window_resizable = true;
  112. config_raw.window_minimizable = true;
  113. config_raw.window_maximizable = true;
  114. config_raw.window_w = 1600;
  115. config_raw.window_h = 900;
  116. ///if arm_macos
  117. config_raw.window_w *= 2;
  118. config_raw.window_h *= 2;
  119. ///end
  120. config_raw.window_x = -1;
  121. config_raw.window_y = -1;
  122. config_raw.window_scale = 1.0;
  123. if (sys_display_width() >= 2560 && sys_display_height() >= 1600) {
  124. config_raw.window_scale = 2.0;
  125. }
  126. ///if (arm_android || arm_ios || arm_macos)
  127. config_raw.window_scale = 2.0;
  128. ///end
  129. config_raw.window_vsync = true;
  130. config_raw.window_frequency = sys_display_frequency();
  131. config_raw.rp_bloom = false;
  132. config_raw.rp_gi = false;
  133. config_raw.rp_vignette = 0.2;
  134. config_raw.rp_grain = 0.09;
  135. ///if (arm_android || arm_ios || is_forge)
  136. config_raw.rp_ssao = false;
  137. ///else
  138. config_raw.rp_ssao = true;
  139. ///end
  140. config_raw.rp_supersample = 1.0;
  141. config_raw.version = manifest_version;
  142. config_raw.sha = config_get_sha();
  143. base_init_config();
  144. }
  145. else {
  146. // Upgrade config format created by older ArmorPaint build
  147. // if (config_raw.version != manifest_version) {
  148. // config_raw.version = manifest_version;
  149. // save();
  150. // }
  151. if (config_raw.sha != config_get_sha()) {
  152. config_loaded = false;
  153. config_init();
  154. return;
  155. }
  156. }
  157. ui_touch_scroll = config_raw.touch_ui;
  158. ui_touch_hold = config_raw.touch_ui;
  159. ui_touch_tooltip = config_raw.touch_ui;
  160. base_res_handle.position = config_raw.layer_res;
  161. keymap_load();
  162. }
  163. function config_get_sha(): string {
  164. let blob: buffer_t = data_get_blob("version.json");
  165. if (blob == null) {
  166. return "undefined";
  167. }
  168. let v: version_t = json_parse(sys_buffer_to_string(blob));
  169. return v.sha;
  170. }
  171. function config_get_date(): string {
  172. let blob: buffer_t = data_get_blob("version.json");
  173. if (blob == null) {
  174. return "undefined";
  175. }
  176. let v: version_t = json_parse(sys_buffer_to_string(blob));
  177. return v.date;
  178. }
  179. function config_get_options(): iron_window_options_t {
  180. let window_mode: window_mode_t = config_raw.window_mode == 0 ? window_mode_t.WINDOWED : window_mode_t.FULLSCREEN;
  181. let features: window_features_t = window_features_t.NONE;
  182. if (config_raw.window_resizable) {
  183. features |= window_features_t.RESIZABLE;
  184. }
  185. if (config_raw.window_maximizable) {
  186. features |= window_features_t.MAXIMIZABLE;
  187. }
  188. if (config_raw.window_minimizable) {
  189. features |= window_features_t.MINIMIZABLE;
  190. }
  191. let title: string = "untitled - " + manifest_title;
  192. let ops: iron_window_options_t = {
  193. title: title,
  194. width: config_raw.window_w,
  195. height: config_raw.window_h,
  196. x: config_raw.window_x,
  197. y: config_raw.window_y,
  198. mode: window_mode,
  199. features: features,
  200. vsync: config_raw.window_vsync,
  201. frequency: config_raw.window_frequency
  202. };
  203. return ops;
  204. }
  205. function config_restore() {
  206. ui_children = map_create(); // Reset ui handles
  207. config_loaded = false;
  208. let _layout: i32[] = config_raw.layout;
  209. config_init();
  210. config_raw.layout = _layout;
  211. base_init_layout();
  212. translator_load_translations(config_raw.locale);
  213. config_apply();
  214. config_load_theme(config_raw.theme);
  215. }
  216. function config_import_from(from: config_t) {
  217. let _sha: string = config_raw.sha;
  218. let _version: string = config_raw.version;
  219. config_raw = from;
  220. config_raw.sha = _sha;
  221. config_raw.version = _version;
  222. ui_children = map_create(); // Reset ui handles
  223. keymap_load();
  224. base_init_layout();
  225. translator_load_translations(config_raw.locale);
  226. config_apply();
  227. config_load_theme(config_raw.theme);
  228. }
  229. function config_apply() {
  230. config_raw.rp_ssao = context_raw.hssao.selected;
  231. config_raw.rp_bloom = context_raw.hbloom.selected;
  232. config_raw.rp_gi = context_raw.hvxao.selected;
  233. config_raw.rp_supersample = config_get_super_sample_size(context_raw.hsupersample.position);
  234. config_save();
  235. context_raw.ddirty = 2;
  236. let current: gpu_texture_t = _draw_current;
  237. let in_use: bool = gpu_in_use;
  238. if (in_use) draw_end();
  239. render_path_base_apply_config();
  240. if (in_use) draw_begin(current);
  241. }
  242. function config_get_super_sample_quality(f: f32): i32 {
  243. return f == 0.25 ? 0 :
  244. f == 0.5 ? 1 :
  245. f == 1.0 ? 2 :
  246. f == 1.5 ? 3 :
  247. f == 2.0 ? 4 : 5;
  248. }
  249. function config_get_super_sample_size(i: i32): f32 {
  250. return i == 0 ? 0.25 :
  251. i == 1 ? 0.5 :
  252. i == 2 ? 1.0 :
  253. i == 3 ? 1.5 :
  254. i == 4 ? 2.0 : 4.0;
  255. }
  256. function config_texture_res_size(pos: i32): i32 {
  257. return pos == texture_res_t.RES128 ? 128 :
  258. pos == texture_res_t.RES256 ? 256 :
  259. pos == texture_res_t.RES512 ? 512 :
  260. pos == texture_res_t.RES1024 ? 1024 :
  261. pos == texture_res_t.RES2048 ? 2048 :
  262. pos == texture_res_t.RES4096 ? 4096 :
  263. pos == texture_res_t.RES8192 ? 8192 :
  264. pos == texture_res_t.RES16384 ? 16384 : 0;
  265. }
  266. function config_get_texture_res(): i32 {
  267. let res: i32 = base_res_handle.position;
  268. return config_texture_res_size(res);
  269. }
  270. function config_get_layer_res(): i32 {
  271. let res: i32 = config_raw.layer_res;
  272. return config_texture_res_size(res);
  273. }
  274. function config_get_atlas_res(): i32 {
  275. let res: i32 = config_raw.atlas_res;
  276. return config_texture_res_size(res);
  277. }
  278. function config_get_texture_res_x(): i32 {
  279. return context_raw.project_aspect_ratio == 2 ? math_floor(config_get_texture_res() / 2) : config_get_texture_res();
  280. }
  281. function config_get_texture_res_y(): i32 {
  282. return context_raw.project_aspect_ratio == 1 ? math_floor(config_get_texture_res() / 2) : config_get_texture_res();
  283. }
  284. function config_get_texture_res_pos(i: i32): i32 {
  285. return i == 128 ? texture_res_t.RES128 :
  286. i == 256 ? texture_res_t.RES256 :
  287. i == 512 ? texture_res_t.RES512 :
  288. i == 1024 ? texture_res_t.RES1024 :
  289. i == 2048 ? texture_res_t.RES2048 :
  290. i == 4096 ? texture_res_t.RES4096 :
  291. i == 8192 ? texture_res_t.RES8192 :
  292. i == 16384 ? texture_res_t.RES16384 : 0;
  293. }
  294. function config_load_theme(theme: string, tag_redraw: bool = true) {
  295. base_theme = ui_theme_create();
  296. if (theme != "default.json") {
  297. let b: buffer_t = data_get_blob("themes/" + theme);
  298. let parsed: ui_theme_t = json_parse(sys_buffer_to_string(b));
  299. base_theme = parsed;
  300. }
  301. base_theme.FILL_WINDOW_BG = true;
  302. if (tag_redraw) {
  303. for (let i: i32 = 0; i < base_get_uis().length; ++i) {
  304. let ui: ui_t = base_get_uis()[i];
  305. ui.ops.theme = base_theme;
  306. }
  307. ui_base_tag_ui_redraw();
  308. }
  309. if (config_raw.touch_ui) {
  310. // Enlarge elements
  311. base_theme.FULL_TABS = true;
  312. base_theme.ELEMENT_H = 24 + 6;
  313. base_theme.BUTTON_H = 22 + 6;
  314. base_theme.FONT_SIZE = 13 + 2;
  315. base_theme.ARROW_SIZE = 5 + 2;
  316. base_theme.CHECK_SIZE = 15 + 4;
  317. base_theme.CHECK_SELECT_SIZE = 8 + 2;
  318. config_button_align = ui_align_t.LEFT;
  319. config_button_spacing = "";
  320. }
  321. else {
  322. base_theme.FULL_TABS = false;
  323. config_button_align = ui_align_t.LEFT;
  324. config_button_spacing = config_default_button_spacing;
  325. }
  326. }
  327. function config_enable_plugin(f: string) {
  328. array_push(config_raw.plugins, f);
  329. plugin_start(f);
  330. }
  331. function config_disable_plugin(f: string) {
  332. array_remove(config_raw.plugins, f);
  333. plugin_stop(f);
  334. }
  335. type version_t = {
  336. sha: string;
  337. date: string;
  338. };
  339. type config_t = {
  340. // The locale should be specified in ISO 639-1 format:
  341. // https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
  342. // "system" is a special case that will use the system locale
  343. locale?: string;
  344. // Window
  345. window_mode?: i32; // window, fullscreen
  346. window_w?: i32;
  347. window_h?: i32;
  348. window_x?: i32;
  349. window_y?: i32;
  350. window_resizable?: bool;
  351. window_maximizable?: bool;
  352. window_minimizable?: bool;
  353. window_vsync?: bool;
  354. window_frequency?: i32;
  355. window_scale?: f32;
  356. // Render path
  357. rp_supersample?: f32;
  358. rp_ssao?: bool;
  359. rp_bloom?: bool;
  360. rp_gi?: bool;
  361. rp_vignette?: f32;
  362. rp_grain?: f32;
  363. // Application
  364. version?: string;
  365. sha?: string; // Commit id
  366. recent_projects?: string[]; // Recently opened projects
  367. bookmarks?: string[]; // Bookmarked folders in browser
  368. plugins?: string[]; // List of enabled plugins
  369. keymap?: string; // Link to keymap file
  370. theme?: string; // Link to theme file
  371. undo_steps?: i32; // Number of undo steps to preserve
  372. camera_pan_speed?: f32;
  373. camera_zoom_speed?: f32;
  374. camera_rotation_speed?: f32;
  375. zoom_direction?: i32;
  376. wrap_mouse?: bool;
  377. show_asset_names?: bool;
  378. touch_ui?: bool;
  379. splash_screen?: bool;
  380. layout?: i32[]; // Sizes
  381. layout_tabs?: i32[]; // Active tabs
  382. workspace?: i32;
  383. camera_controls?: i32; // Orbit, rotate
  384. server?: string;
  385. viewport_mode: i32;
  386. pathtrace_mode: i32;
  387. pressure_radius?: bool; // Pen pressure controls
  388. pressure_sensitivity?: f32;
  389. displace_strength?: f32;
  390. layer_res?: i32;
  391. brush_live?: bool;
  392. brush_3d?: bool;
  393. node_preview?: bool;
  394. pressure_hardness?: bool;
  395. pressure_angle?: bool;
  396. pressure_opacity?: bool;
  397. material_live?: bool;
  398. brush_depth_reject?: bool;
  399. brush_angle_reject?: bool;
  400. dilate?: i32;
  401. dilate_radius?: i32;
  402. gpu_inference?: bool;
  403. blender?: string;
  404. atlas_res: i32; // Forge
  405. grid_snap: bool;
  406. };