config.ts 12 KB

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