history.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. let history_steps: step_t[];
  2. let history_undo_i: i32 = 0; // Undo layer
  3. let history_undos: i32 = 0; // Undos available
  4. let history_redos: i32 = 0; // Redos available
  5. ///if (is_paint || is_sculpt)
  6. let history_push_undo: bool = false; // Store undo on next paint
  7. let history_undo_layers: slot_layer_t[] = null;
  8. ///end
  9. ///if is_sculpt
  10. let history_push_undo2: bool = false;
  11. ///end
  12. function history_undo() {
  13. if (history_undos > 0) {
  14. let active: i32 = history_steps.length - 1 - history_redos;
  15. let step: step_t = history_steps[active];
  16. if (step.name == tr("Edit Nodes")) {
  17. history_swap_canvas(step);
  18. }
  19. ///if (is_paint || is_sculpt)
  20. else if (step.name == tr("New Layer") || step.name == tr("New Black Mask") || step.name == tr("New White Mask") || step.name == tr("New Fill Mask")) {
  21. context_raw.layer = project_layers[step.layer];
  22. slot_layer_delete(context_raw.layer);
  23. context_raw.layer = project_layers[step.layer > 0 ? step.layer - 1 : 0];
  24. }
  25. else if (step.name == tr("New Group")) {
  26. context_raw.layer = project_layers[step.layer];
  27. // The layer below is the only layer in the group. Its layer masks are automatically unparented, too.
  28. project_layers[step.layer - 1].parent = null;
  29. slot_layer_delete(context_raw.layer);
  30. context_raw.layer = project_layers[step.layer > 0 ? step.layer - 1 : 0];
  31. }
  32. else if (step.name == tr("Delete Layer")) {
  33. let parent: slot_layer_t = step.layer_parent > 0 ? project_layers[step.layer_parent - 1] : null;
  34. let l: slot_layer_t = slot_layer_create("", step.layer_type, parent);
  35. array_insert(project_layers, step.layer, l);
  36. context_set_layer(l);
  37. history_undo_i = history_undo_i - 1 < 0 ? config_raw.undo_steps - 1 : history_undo_i - 1;
  38. let lay: slot_layer_t = history_undo_layers[history_undo_i];
  39. slot_layer_swap(l, lay);
  40. l.mask_opacity = step.layer_opacity;
  41. l.blending = step.layer_blending;
  42. l.object_mask = step.layer_object;
  43. make_material_parse_mesh_material();
  44. // Undo at least second time in order to avoid empty groups
  45. if (step.layer_type == layer_slot_type_t.GROUP) {
  46. app_notify_on_next_frame(function () {
  47. let active: i32 = history_steps.length - 1 - history_redos;
  48. // 1. Undo deleting group masks
  49. let n: i32 = 1;
  50. while (history_steps[active - n].layer_type == layer_slot_type_t.MASK) {
  51. history_undo();
  52. ++n;
  53. }
  54. // 2. Undo a mask to have a non empty group
  55. history_undo();
  56. });
  57. }
  58. }
  59. else if (step.name == tr("Clear Layer")) {
  60. history_undo_i = history_undo_i - 1 < 0 ? config_raw.undo_steps - 1 : history_undo_i - 1;
  61. let lay: slot_layer_t = history_undo_layers[history_undo_i];
  62. slot_layer_swap(context_raw.layer, lay);
  63. context_raw.layer_preview_dirty = true;
  64. }
  65. else if (step.name == tr("Duplicate Layer")) {
  66. let children: slot_layer_t[] = slot_layer_get_recursive_children(project_layers[step.layer]);
  67. let position: i32 = step.layer + 1;
  68. if (children != null) {
  69. position += children.length;
  70. }
  71. context_raw.layer = project_layers[position];
  72. slot_layer_delete(context_raw.layer);
  73. }
  74. else if (step.name == tr("Order Layers")) {
  75. let target: slot_layer_t = project_layers[step.prev_order];
  76. project_layers[step.prev_order] = project_layers[step.layer];
  77. project_layers[step.layer] = target;
  78. }
  79. else if (step.name == tr("Merge Layers")) {
  80. context_raw.layer = project_layers[step.layer];
  81. slot_layer_delete(context_raw.layer);
  82. let parent: slot_layer_t = step.layer_parent > 0 ? project_layers[step.layer_parent - 2] : null;
  83. let l: slot_layer_t = slot_layer_create("", step.layer_type, parent);
  84. array_insert(project_layers, step.layer, l);
  85. context_set_layer(l);
  86. history_undo_i = history_undo_i - 1 < 0 ? config_raw.undo_steps - 1 : history_undo_i - 1;
  87. let lay: slot_layer_t = history_undo_layers[history_undo_i];
  88. slot_layer_swap(context_raw.layer, lay);
  89. l = slot_layer_create("", step.layer_type, parent);
  90. array_insert(project_layers, step.layer + 1, l);
  91. context_set_layer(l);
  92. history_undo_i = history_undo_i - 1 < 0 ? config_raw.undo_steps - 1 : history_undo_i - 1;
  93. lay = history_undo_layers[history_undo_i];
  94. slot_layer_swap(context_raw.layer, lay);
  95. context_raw.layer.mask_opacity = step.layer_opacity;
  96. context_raw.layer.blending = step.layer_blending;
  97. context_raw.layer.object_mask = step.layer_object;
  98. context_raw.layers_preview_dirty = true;
  99. make_material_parse_mesh_material();
  100. }
  101. else if (step.name == tr("Apply Mask")) {
  102. // First restore the layer(s)
  103. let mask_pos: i32 = step.layer;
  104. let current_layer: slot_layer_t = null;
  105. // The layer at the old mask position is a mask, i.e. the layer had multiple masks before.
  106. if (slot_layer_is_mask(project_layers[mask_pos])) {
  107. current_layer = project_layers[mask_pos].parent;
  108. }
  109. else if (slot_layer_is_layer(project_layers[mask_pos]) || slot_layer_is_group(project_layers[mask_pos])) {
  110. current_layer = project_layers[mask_pos];
  111. }
  112. let layers_to_restore: slot_layer_t[] = slot_layer_is_group(current_layer) ? slot_layer_get_children(current_layer) : [current_layer];
  113. layers_to_restore.reverse();
  114. for (let i: i32 = 0; i < layers_to_restore.length; ++i) {
  115. let layer: slot_layer_t = layers_to_restore[i];
  116. // Replace the current layer's content with the old one
  117. context_raw.layer = layer;
  118. history_undo_i = history_undo_i - 1 < 0 ? config_raw.undo_steps - 1 : history_undo_i - 1;
  119. let old_layer: slot_layer_t = history_undo_layers[history_undo_i];
  120. slot_layer_swap(context_raw.layer, old_layer);
  121. }
  122. // Now restore the applied mask
  123. history_undo_i = history_undo_i - 1 < 0 ? config_raw.undo_steps - 1 : history_undo_i - 1;
  124. let mask: slot_layer_t = history_undo_layers[history_undo_i];
  125. base_new_mask(false, current_layer, mask_pos);
  126. slot_layer_swap(context_raw.layer, mask);
  127. context_raw.layers_preview_dirty = true;
  128. context_set_layer(context_raw.layer);
  129. }
  130. else if (step.name == tr("Invert Mask")) {
  131. app_notify_on_init(function (step: step_t) {
  132. context_raw.layer = project_layers[step.layer];
  133. slot_layer_invert_mask(context_raw.layer);
  134. }, step);
  135. }
  136. else if (step.name == "Apply Filter") {
  137. history_undo_i = history_undo_i - 1 < 0 ? config_raw.undo_steps - 1 : history_undo_i - 1;
  138. let lay: slot_layer_t = history_undo_layers[history_undo_i];
  139. context_set_layer(project_layers[step.layer]);
  140. slot_layer_swap(context_raw.layer, lay);
  141. base_new_mask(false, context_raw.layer);
  142. slot_layer_swap(context_raw.layer, lay);
  143. context_raw.layer_preview_dirty = true;
  144. }
  145. else if (step.name == tr("To Fill Layer") || step.name == tr("To Fill Mask")) {
  146. slot_layer_to_paint_layer(context_raw.layer);
  147. history_undo_i = history_undo_i - 1 < 0 ? config_raw.undo_steps - 1 : history_undo_i - 1;
  148. let lay: slot_layer_t = history_undo_layers[history_undo_i];
  149. slot_layer_swap(context_raw.layer, lay);
  150. }
  151. else if (step.name == tr("To Paint Layer") || step.name == tr("To Paint Mask")) {
  152. history_undo_i = history_undo_i - 1 < 0 ? config_raw.undo_steps - 1 : history_undo_i - 1;
  153. let lay: slot_layer_t = history_undo_layers[history_undo_i];
  154. slot_layer_swap(context_raw.layer, lay);
  155. context_raw.layer.fill_layer = project_materials[step.material];
  156. }
  157. else if (step.name == tr("Layer Opacity")) {
  158. context_set_layer(project_layers[step.layer]);
  159. let t: f32 = context_raw.layer.mask_opacity;
  160. context_raw.layer.mask_opacity = step.layer_opacity;
  161. step.layer_opacity = t;
  162. make_material_parse_mesh_material();
  163. }
  164. else if (step.name == tr("Layer Blending")) {
  165. context_set_layer(project_layers[step.layer]);
  166. let t: blend_type_t = context_raw.layer.blending;
  167. context_raw.layer.blending = step.layer_blending;
  168. step.layer_blending = t;
  169. make_material_parse_mesh_material();
  170. }
  171. else if (step.name == tr("Delete Node Group")) {
  172. array_insert(project_material_groups, step.canvas_group, { canvas: null, nodes: zui_nodes_create() });
  173. history_swap_canvas(step);
  174. }
  175. else if (step.name == tr("New Material")) {
  176. context_raw.material = project_materials[step.material];
  177. step.canvas = context_raw.material.canvas;
  178. slot_material_delete(context_raw.material);
  179. }
  180. else if (step.name == tr("Delete Material")) {
  181. context_raw.material = slot_material_create(project_materials[0].data);
  182. array_insert(project_materials, step.material, context_raw.material);
  183. context_raw.material.canvas = step.canvas;
  184. ui_nodes_canvas_changed();
  185. ui_nodes_hwnd.redraws = 2;
  186. }
  187. else if (step.name == tr("Duplicate Material")) {
  188. context_raw.material = project_materials[step.material];
  189. step.canvas = context_raw.material.canvas;
  190. slot_material_delete(context_raw.material);
  191. }
  192. else { // Paint operation
  193. history_undo_i = history_undo_i - 1 < 0 ? config_raw.undo_steps - 1 : history_undo_i - 1;
  194. let lay: slot_layer_t = history_undo_layers[history_undo_i];
  195. context_select_paint_object(project_paint_objects[step.object]);
  196. context_set_layer(project_layers[step.layer]);
  197. slot_layer_swap(context_raw.layer, lay);
  198. context_raw.layer_preview_dirty = true;
  199. }
  200. ///end
  201. history_undos--;
  202. history_redos++;
  203. context_raw.ddirty = 2;
  204. ///if (is_paint || is_sculpt)
  205. ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
  206. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  207. if (ui_view2d_show) {
  208. ui_view2d_hwnd.redraws = 2;
  209. }
  210. if (config_raw.touch_ui) {
  211. // Refresh undo & redo buttons
  212. ui_menubar_menu_handle.redraws = 2;
  213. }
  214. ///end
  215. }
  216. }
  217. function history_redo() {
  218. if (history_redos > 0) {
  219. let active: i32 = history_steps.length - history_redos;
  220. let step: step_t = history_steps[active];
  221. if (step.name == tr("Edit Nodes")) {
  222. history_swap_canvas(step);
  223. }
  224. ///if (is_paint || is_sculpt)
  225. else if (step.name == tr("New Layer") || step.name == tr("New Black Mask") || step.name == tr("New White Mask") || step.name == tr("New Fill Mask")) {
  226. let parent: slot_layer_t = step.layer_parent > 0 ? project_layers[step.layer_parent - 1] : null;
  227. let l: slot_layer_t = slot_layer_create("", step.layer_type, parent);
  228. array_insert(project_layers, step.layer, l);
  229. if (step.name == tr("New Black Mask")) {
  230. app_notify_on_next_frame(function (l: slot_layer_t) {
  231. slot_layer_clear(l, 0x00000000);
  232. }, l);
  233. }
  234. else if (step.name == tr("New White Mask")) {
  235. app_notify_on_next_frame(function (l: slot_layer_t) {
  236. slot_layer_clear(l, 0xffffffff);
  237. }, l);
  238. }
  239. else if (step.name == tr("New Fill Mask")) {
  240. context_raw.material = project_materials[step.material];
  241. app_notify_on_next_frame(function (l: slot_layer_t) {
  242. slot_layer_to_fill_layer(l);
  243. }, l);
  244. }
  245. context_raw.layer_preview_dirty = true;
  246. context_set_layer(l);
  247. }
  248. else if (step.name == tr("New Group")) {
  249. let l: slot_layer_t = project_layers[step.layer - 1];
  250. let group: slot_layer_t = base_new_group();
  251. array_remove(project_layers, group);
  252. array_insert(project_layers, step.layer, group);
  253. l.parent = group;
  254. context_set_layer(group);
  255. }
  256. else if (step.name == tr("Delete Layer")) {
  257. context_raw.layer = project_layers[step.layer];
  258. history_swap_active();
  259. slot_layer_delete(context_raw.layer);
  260. // Redoing the last delete would result in an empty group
  261. // Redo deleting all group masks + the group itself
  262. if (step.layer_type == layer_slot_type_t.LAYER && history_steps.length >= active + 2 && (history_steps[active + 1].layer_type == layer_slot_type_t.GROUP || history_steps[active + 1].layer_type == layer_slot_type_t.MASK)) {
  263. app_notify_on_next_frame(function () {
  264. let active: i32 = history_steps.length - history_redos;
  265. let n: i32 = 1;
  266. while (history_steps[active + n].layer_type == layer_slot_type_t.MASK) {
  267. ++n;
  268. }
  269. for (let i: i32 = 0; i < n; ++i) {
  270. history_redo();
  271. }
  272. });
  273. }
  274. }
  275. else if (step.name == tr("Clear Layer")) {
  276. context_raw.layer = project_layers[step.layer];
  277. history_swap_active();
  278. slot_layer_clear(context_raw.layer);
  279. context_raw.layer_preview_dirty = true;
  280. }
  281. else if (step.name == tr("Duplicate Layer")) {
  282. context_raw.layer = project_layers[step.layer];
  283. app_notify_on_next_frame(function () {
  284. base_duplicate_layer(context_raw.layer);
  285. });
  286. }
  287. else if (step.name == tr("Order Layers")) {
  288. let target: slot_layer_t = project_layers[step.prev_order];
  289. project_layers[step.prev_order] = project_layers[step.layer];
  290. project_layers[step.layer] = target;
  291. }
  292. else if (step.name == tr("Merge Layers")) {
  293. context_raw.layer = project_layers[step.layer + 1];
  294. app_notify_on_init(history_redo_merge_layers);
  295. app_notify_on_init(base_merge_down);
  296. }
  297. else if (step.name == tr("Apply Mask")) {
  298. context_raw.layer = project_layers[step.layer];
  299. if (slot_layer_is_group_mask(context_raw.layer)) {
  300. let group: slot_layer_t = context_raw.layer.parent;
  301. let layers: slot_layer_t[] = slot_layer_get_children(group);
  302. array_insert(layers, 0, context_raw.layer);
  303. history_copy_merging_layers2(layers);
  304. }
  305. else {
  306. history_copy_merging_layers2([context_raw.layer, context_raw.layer.parent]);
  307. }
  308. app_notify_on_next_frame(function () {
  309. slot_layer_apply_mask(context_raw.layer);
  310. context_set_layer(context_raw.layer);
  311. context_raw.layers_preview_dirty = true;
  312. });
  313. }
  314. else if (step.name == tr("Invert Mask")) {
  315. app_notify_on_init(function (step: step_t) {
  316. context_raw.layer = project_layers[step.layer];
  317. slot_layer_invert_mask(context_raw.layer);
  318. }, step);
  319. }
  320. else if (step.name == tr("Apply Filter")) {
  321. let lay: slot_layer_t = history_undo_layers[history_undo_i];
  322. context_set_layer(project_layers[step.layer]);
  323. slot_layer_swap(context_raw.layer, lay);
  324. base_new_mask(false, lay);
  325. slot_layer_swap(context_raw.layer, lay);
  326. context_raw.layer_preview_dirty = true;
  327. history_undo_i = (history_undo_i + 1) % config_raw.undo_steps;
  328. }
  329. else if (step.name == tr("To Fill Layer") || step.name == tr("To Fill Mask")) {
  330. let lay: slot_layer_t = history_undo_layers[history_undo_i];
  331. slot_layer_swap(context_raw.layer, lay);
  332. context_raw.layer.fill_layer = project_materials[step.material];
  333. history_undo_i = (history_undo_i + 1) % config_raw.undo_steps;
  334. }
  335. else if (step.name == tr("To Paint Layer") || step.name == tr("To Paint Mask")) {
  336. slot_layer_to_paint_layer(context_raw.layer);
  337. let lay: slot_layer_t = history_undo_layers[history_undo_i];
  338. slot_layer_swap(context_raw.layer, lay);
  339. history_undo_i = (history_undo_i + 1) % config_raw.undo_steps;
  340. }
  341. else if (step.name == tr("Layer Opacity")) {
  342. context_set_layer(project_layers[step.layer]);
  343. let t: f32 = context_raw.layer.mask_opacity;
  344. context_raw.layer.mask_opacity = step.layer_opacity;
  345. step.layer_opacity = t;
  346. make_material_parse_mesh_material();
  347. }
  348. else if (step.name == tr("Layer Blending")) {
  349. context_set_layer(project_layers[step.layer]);
  350. let t: blend_type_t = context_raw.layer.blending;
  351. context_raw.layer.blending = step.layer_blending;
  352. step.layer_blending = t;
  353. make_material_parse_mesh_material();
  354. }
  355. else if (step.name == tr("Delete Node Group")) {
  356. history_swap_canvas(step);
  357. array_remove(project_material_groups, project_material_groups[step.canvas_group]);
  358. }
  359. else if (step.name == tr("New Material")) {
  360. context_raw.material = slot_material_create(project_materials[0].data);
  361. array_insert(project_materials, step.material, context_raw.material);
  362. context_raw.material.canvas = step.canvas;
  363. ui_nodes_canvas_changed();
  364. ui_nodes_hwnd.redraws = 2;
  365. }
  366. else if (step.name == tr("Delete Material")) {
  367. context_raw.material = project_materials[step.material];
  368. step.canvas = context_raw.material.canvas;
  369. slot_material_delete(context_raw.material);
  370. }
  371. else if (step.name == tr("Duplicate Material")) {
  372. context_raw.material = slot_material_create(project_materials[0].data);
  373. array_insert(project_materials, step.material, context_raw.material);
  374. context_raw.material.canvas = step.canvas;
  375. ui_nodes_canvas_changed();
  376. ui_nodes_hwnd.redraws = 2;
  377. }
  378. else { // Paint operation
  379. let lay: slot_layer_t = history_undo_layers[history_undo_i];
  380. context_select_paint_object(project_paint_objects[step.object]);
  381. context_set_layer(project_layers[step.layer]);
  382. slot_layer_swap(context_raw.layer, lay);
  383. context_raw.layer_preview_dirty = true;
  384. history_undo_i = (history_undo_i + 1) % config_raw.undo_steps;
  385. }
  386. ///end
  387. history_undos++;
  388. history_redos--;
  389. context_raw.ddirty = 2;
  390. ///if (is_paint || is_sculpt)
  391. ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
  392. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  393. if (ui_view2d_show) {
  394. ui_view2d_hwnd.redraws = 2;
  395. }
  396. if (config_raw.touch_ui) {
  397. // Refresh undo & redo buttons
  398. ui_menubar_menu_handle.redraws = 2;
  399. }
  400. ///end
  401. }
  402. }
  403. function history_reset() {
  404. ///if (is_paint || is_sculpt)
  405. history_steps = [{name: tr("New"), layer: 0, layer_type: layer_slot_type_t.LAYER, layer_parent: -1, object: 0, material: 0, brush: 0}];
  406. ///end
  407. ///if is_lab
  408. history_steps = [{name: tr("New")}];
  409. ///end
  410. history_undos = 0;
  411. history_redos = 0;
  412. history_undo_i = 0;
  413. }
  414. ///if (is_paint || is_sculpt)
  415. function history_edit_nodes(canvas: zui_node_canvas_t, canvas_type: i32, canvas_group: i32 = -1) {
  416. ///end
  417. ///if is_lab
  418. function history_edit_nodes(canvas: zui_node_canvas_t, canvas_group: i32 = -1) {
  419. ///end
  420. let step: step_t = history_push(tr("Edit Nodes"));
  421. step.canvas_group = canvas_group;
  422. ///if (is_paint || is_sculpt)
  423. step.canvas_type = canvas_type;
  424. ///end
  425. step.canvas = json_parse(json_stringify(canvas));
  426. }
  427. ///if (is_paint || is_sculpt)
  428. function history_paint() {
  429. let is_mask: bool = slot_layer_is_mask(context_raw.layer);
  430. history_copy_to_undo(context_raw.layer.id, history_undo_i, is_mask);
  431. history_push_undo = false;
  432. history_push(tr(ui_toolbar_tool_names[context_raw.tool]));
  433. }
  434. function history_new_layer() {
  435. history_push(tr("New Layer"));
  436. }
  437. function history_new_black_mask() {
  438. history_push(tr("New Black Mask"));
  439. }
  440. function history_new_white_mask() {
  441. history_push(tr("New White Mask"));
  442. }
  443. function history_new_fill_mask() {
  444. history_push(tr("New Fill Mask"));
  445. }
  446. function history_new_group() {
  447. history_push(tr("New Group"));
  448. }
  449. function history_duplicate_layer() {
  450. history_push(tr("Duplicate Layer"));
  451. }
  452. function history_delete_layer() {
  453. history_swap_active();
  454. history_push(tr("Delete Layer"));
  455. }
  456. function history_clear_layer() {
  457. history_swap_active();
  458. history_push(tr("Clear Layer"));
  459. }
  460. function history_order_layers(prevOrder: i32) {
  461. let step: step_t = history_push(tr("Order Layers"));
  462. step.prev_order = prevOrder;
  463. }
  464. function history_merge_layers() {
  465. history_copy_merging_layers();
  466. let step: step_t = history_push(tr("Merge Layers"));
  467. step.layer -= 1; // Merge down
  468. if (slot_layer_has_masks(context_raw.layer)) {
  469. step.layer -= slot_layer_get_masks(context_raw.layer).length;
  470. }
  471. history_steps.shift(); // Merge consumes 2 steps
  472. history_undos--;
  473. // TODO: use undo layer in app_merge_down to save memory
  474. }
  475. function history_apply_mask() {
  476. if (slot_layer_is_group_mask(context_raw.layer)) {
  477. let group: slot_layer_t = context_raw.layer.parent;
  478. let layers: slot_layer_t[] = slot_layer_get_children(group);
  479. array_insert(layers, 0, context_raw.layer);
  480. history_copy_merging_layers2(layers);
  481. }
  482. else {
  483. history_copy_merging_layers2([context_raw.layer, context_raw.layer.parent]);
  484. }
  485. history_push(tr("Apply Mask"));
  486. }
  487. function history_invert_mask() {
  488. history_push(tr("Invert Mask"));
  489. }
  490. function history_apply_filter() {
  491. history_copy_to_undo(context_raw.layer.id, history_undo_i, true);
  492. history_push(tr("Apply Filter"));
  493. }
  494. function history_to_fill_layer() {
  495. history_copy_to_undo(context_raw.layer.id, history_undo_i, false);
  496. history_push(tr("To Fill Layer"));
  497. }
  498. function history_to_fill_mask() {
  499. history_copy_to_undo(context_raw.layer.id, history_undo_i, true);
  500. history_push(tr("To Fill Mask"));
  501. }
  502. function history_to_paint_layer() {
  503. history_copy_to_undo(context_raw.layer.id, history_undo_i, false);
  504. history_push(tr("To Paint Layer"));
  505. }
  506. function history_to_paint_mask() {
  507. history_copy_to_undo(context_raw.layer.id, history_undo_i, true);
  508. history_push(tr("To Paint Mask"));
  509. }
  510. function history_layer_opacity() {
  511. history_push(tr("Layer Opacity"));
  512. }
  513. // function history_layer_object() {
  514. // history_push("Layer Object");
  515. // }
  516. function history_layer_blending() {
  517. history_push(tr("Layer Blending"));
  518. }
  519. function history_new_material() {
  520. let step: step_t = history_push(tr("New Material"));
  521. step.canvas_type = 0;
  522. step.canvas = json_parse(json_stringify(context_raw.material.canvas));
  523. }
  524. function history_delete_material() {
  525. let step: step_t = history_push(tr("Delete Material"));
  526. step.canvas_type = 0;
  527. step.canvas = json_parse(json_stringify(context_raw.material.canvas));
  528. }
  529. function history_duplicate_material() {
  530. let step: step_t = history_push(tr("Duplicate Material"));
  531. step.canvas_type = 0;
  532. step.canvas = json_parse(json_stringify(context_raw.material.canvas));
  533. }
  534. function history_delete_material_group(group: node_group_t) {
  535. let step: step_t = history_push(tr("Delete Node Group"));
  536. step.canvas_type = canvas_type_t.MATERIAL;
  537. step.canvas_group = array_index_of(project_material_groups, group);
  538. step.canvas = json_parse(json_stringify(group.canvas));
  539. }
  540. ///end
  541. function history_push(name: string): step_t {
  542. ///if (krom_windows || krom_linux || krom_darwin)
  543. let filename: string = project_filepath == "" ? ui_files_filename : substring(project_filepath, string_last_index_of(project_filepath, path_sep) + 1, project_filepath.length - 4);
  544. sys_title_set(filename + "* - " + manifest_title);
  545. ///end
  546. if (config_raw.touch_ui) {
  547. // Refresh undo & redo buttons
  548. ui_menubar_menu_handle.redraws = 2;
  549. }
  550. if (history_undos < config_raw.undo_steps) {
  551. history_undos++;
  552. }
  553. if (history_redos > 0) {
  554. for (let i: i32 = 0; i < history_redos; ++i) {
  555. history_steps.pop();
  556. }
  557. history_redos = 0;
  558. }
  559. ///if (is_paint || is_sculpt)
  560. let opos: i32 = array_index_of(project_paint_objects, context_raw.paint_object);
  561. let lpos: i32 = array_index_of(project_layers, context_raw.layer);
  562. let mpos: i32 = array_index_of(project_materials, context_raw.material);
  563. let bpos: i32 = array_index_of(project_brushes, context_raw.brush);
  564. array_push(history_steps, {
  565. name: name,
  566. layer: lpos,
  567. layer_type: slot_layer_is_mask(context_raw.layer) ? layer_slot_type_t.MASK : slot_layer_is_group(context_raw.layer) ? layer_slot_type_t.GROUP : layer_slot_type_t.LAYER,
  568. layer_parent: context_raw.layer.parent == null ? -1 : array_index_of(project_layers, context_raw.layer.parent),
  569. object: opos,
  570. material: mpos,
  571. brush: bpos,
  572. layer_opacity: context_raw.layer.mask_opacity,
  573. layer_object: context_raw.layer.object_mask,
  574. layer_blending: context_raw.layer.blending
  575. });
  576. ///end
  577. ///if is_lab
  578. array_push(history_steps, {
  579. name: name
  580. });
  581. ///end
  582. while (history_steps.length > config_raw.undo_steps + 1) {
  583. history_steps.shift();
  584. }
  585. return history_steps[history_steps.length - 1];
  586. }
  587. ///if (is_paint || is_sculpt)
  588. function history_redo_merge_layers() {
  589. history_copy_merging_layers();
  590. }
  591. function history_copy_merging_layers() {
  592. let lay: slot_layer_t = context_raw.layer;
  593. history_copy_to_undo(lay.id, history_undo_i, slot_layer_is_mask(context_raw.layer));
  594. let below: i32 = array_index_of(project_layers, lay) - 1;
  595. lay = project_layers[below];
  596. history_copy_to_undo(lay.id, history_undo_i, slot_layer_is_mask(context_raw.layer));
  597. }
  598. function history_copy_merging_layers2(layers: slot_layer_t[]) {
  599. for (let i: i32 = 0; i < layers.length; ++i) {
  600. let layer: slot_layer_t = layers[i];
  601. history_copy_to_undo(layer.id, history_undo_i, slot_layer_is_mask(layer));
  602. }
  603. }
  604. function history_swap_active() {
  605. let undo_layer: slot_layer_t = history_undo_layers[history_undo_i];
  606. slot_layer_swap(undo_layer, context_raw.layer);
  607. history_undo_i = (history_undo_i + 1) % config_raw.undo_steps;
  608. }
  609. function history_copy_to_undo(from_id: i32, to_id: i32, is_mask: bool) {
  610. ///if is_sculpt
  611. is_mask = true;
  612. ///end
  613. if (is_mask) {
  614. render_path_set_target("texpaint_undo" + to_id);
  615. render_path_bind_target("texpaint" + from_id, "tex");
  616. // render_path_draw_shader("shader_datas/copy_pass/copyR8_pass");
  617. render_path_draw_shader("shader_datas/copy_pass/copy_pass");
  618. }
  619. else {
  620. render_path_set_target("texpaint_undo" + to_id, ["texpaint_nor_undo" + to_id, "texpaint_pack_undo" + to_id]);
  621. render_path_bind_target("texpaint" + from_id, "tex0");
  622. render_path_bind_target("texpaint_nor" + from_id, "tex1");
  623. render_path_bind_target("texpaint_pack" + from_id, "tex2");
  624. render_path_draw_shader("shader_datas/copy_mrt3_pass/copy_mrt3_pass");
  625. }
  626. history_undo_i = (history_undo_i + 1) % config_raw.undo_steps;
  627. }
  628. ///end
  629. function history_get_canvas_owner(step: step_t): any {
  630. ///if (is_paint || is_sculpt)
  631. return step.canvas_group == -1 ?
  632. project_materials[step.material] :
  633. project_material_groups[step.canvas_group];
  634. ///end
  635. ///if is_lab
  636. return null;
  637. ///end
  638. }
  639. function history_swap_canvas(step: step_t) {
  640. ///if (is_paint || is_sculpt)
  641. if (step.canvas_type == 0) {
  642. let _canvas: zui_node_canvas_t = history_get_canvas_owner(step).canvas;
  643. history_get_canvas_owner(step).canvas = step.canvas;
  644. step.canvas = _canvas;
  645. context_raw.material = project_materials[step.material];
  646. }
  647. else {
  648. let _canvas: zui_node_canvas_t = project_brushes[step.brush].canvas;
  649. project_brushes[step.brush].canvas = step.canvas;
  650. step.canvas = _canvas;
  651. context_raw.brush = project_brushes[step.brush];
  652. }
  653. ///end
  654. ///if is_lab
  655. let _canvas: zui_node_canvas_t = history_get_canvas_owner(step).canvas;
  656. history_get_canvas_owner(step).canvas = step.canvas;
  657. step.canvas = _canvas;
  658. ///end
  659. ui_nodes_canvas_changed();
  660. ui_nodes_hwnd.redraws = 2;
  661. }
  662. type step_t = {
  663. name?: string;
  664. canvas?: zui_node_canvas_t; // Node history
  665. canvas_group?: i32;
  666. ///if (is_paint || is_sculpt)
  667. layer?: i32;
  668. layer_type?: layer_slot_type_t;
  669. layer_parent?: i32;
  670. object?: i32;
  671. material?: i32;
  672. brush?: i32;
  673. layer_opacity?: f32;
  674. layer_object?: i32;
  675. layer_blending?: i32;
  676. prev_order?: i32; // Previous layer position
  677. canvas_type?: i32;
  678. ///end
  679. };