ui_nodes.ts 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. ///if is_paint
  2. let ui_nodes_show: bool = false;
  3. ///end
  4. ///if is_lab
  5. let ui_nodes_show: bool = true;
  6. ///end
  7. let ui_nodes_wx: i32;
  8. let ui_nodes_wy: i32;
  9. let ui_nodes_ww: i32;
  10. let ui_nodes_wh: i32;
  11. let ui_nodes_ui: ui_t;
  12. let ui_nodes_canvas_type: canvas_type_t = canvas_type_t.MATERIAL;
  13. let ui_nodes_show_menu: bool = false;
  14. let ui_nodes_show_menu_first: bool = true;
  15. let ui_nodes_hide_menu: bool = false;
  16. let ui_nodes_menu_category: i32 = 0;
  17. let ui_nodes_popup_x: f32 = 0.0;
  18. let ui_nodes_popup_y: f32 = 0.0;
  19. let ui_nodes_node_search_x: i32;
  20. let ui_nodes_node_search_y: i32;
  21. let ui_nodes_uichanged_last: bool = false;
  22. let ui_nodes_recompile_mat: bool = false; // Mat preview
  23. let ui_nodes_recompile_mat_final: bool = false;
  24. let ui_nodes_node_search_spawn: ui_node_t = null;
  25. let ui_nodes_node_search_offset: i32 = 0;
  26. let ui_nodes_last_canvas: ui_node_canvas_t = null;
  27. let ui_nodes_last_node_selected_id: i32 = -1;
  28. let ui_nodes_release_link: bool = false;
  29. let ui_nodes_is_node_menu_op: bool = false;
  30. let ui_nodes_grid: gpu_texture_t = null;
  31. let ui_nodes_grid_redraw: bool = true;
  32. let ui_nodes_grid_cell_w: i32 = 200;
  33. let ui_nodes_grid_small_cell_w: i32 = 40;
  34. let ui_nodes_hwnd: ui_handle_t = ui_handle_create();
  35. let ui_nodes_group_stack: node_group_t[] = [];
  36. let ui_nodes_controls_down: bool = false;
  37. let ui_nodes_tabs: slot_material_t[] = null;
  38. let ui_nodes_htab: ui_handle_t = ui_handle_create();
  39. let _ui_nodes_on_link_drag_link_drag: ui_node_link_t;
  40. let _ui_nodes_on_link_drag_node: ui_node_t;
  41. let _ui_nodes_on_socket_released_socket: ui_node_socket_t;
  42. let _ui_nodes_on_socket_released_node: ui_node_t;
  43. let _ui_nodes_htype: ui_handle_t = ui_handle_create();
  44. let _ui_nodes_hname: ui_handle_t = ui_handle_create();
  45. let _ui_nodes_hmin: ui_handle_t = ui_handle_create();
  46. let _ui_nodes_hmax: ui_handle_t = ui_handle_create();
  47. let _ui_nodes_hval0: ui_handle_t = ui_handle_create();
  48. let _ui_nodes_hval1: ui_handle_t = ui_handle_create();
  49. let _ui_nodes_hval2: ui_handle_t = ui_handle_create();
  50. let _ui_nodes_hval3: ui_handle_t = ui_handle_create();
  51. let _ui_nodes_on_canvas_released_selected: ui_node_t;
  52. let _ui_nodes_node_search_first: bool;
  53. let _ui_nodes_node_search_done: ()=>void;
  54. function ui_viewnodes_init() {
  55. ui_nodes_on_link_drag = ui_viewnodes_on_link_drag;
  56. ui_nodes_on_socket_released = ui_viewnodes_on_socket_released;
  57. ui_nodes_on_canvas_released = ui_viewnodes_on_canvas_released;
  58. ui_nodes_on_canvas_control = ui_viewnodes_on_canvas_control;
  59. ui_nodes_grid_snap = config_raw.grid_snap;
  60. nodes_material_init();
  61. let scale: f32 = config_raw.window_scale;
  62. let ops: ui_options_t = {
  63. theme: base_theme,
  64. font: base_font,
  65. color_wheel: base_color_wheel,
  66. black_white_gradient: base_color_wheel_gradient,
  67. scale_factor: scale
  68. };
  69. ui_nodes_ui = ui_create(ops);
  70. ui_nodes_ui.scroll_enabled = false;
  71. }
  72. function ui_viewnodes_on_link_drag(link_drag_id: i32, is_new_link: bool) {
  73. if (!is_new_link) {
  74. return;
  75. }
  76. let ui_nodes: ui_nodes_t = ui_nodes_get_nodes();
  77. let links: ui_node_link_t[] = ui_nodes_get_canvas(true).links;
  78. let link_drag: ui_node_link_t = ui_get_link(links, link_drag_id);
  79. let nodes: ui_node_t[] = ui_nodes_get_canvas(true).nodes;
  80. let node: ui_node_t = ui_get_node(nodes, link_drag.from_id > -1 ? link_drag.from_id : link_drag.to_id);
  81. let link_x: i32 = ui_nodes_ui._window_x + UI_NODE_X(node);
  82. let link_y: i32 = ui_nodes_ui._window_y + UI_NODE_Y(node);
  83. if (link_drag.from_id > -1) {
  84. link_x += UI_NODE_W(node);
  85. link_y += UI_OUTPUT_Y(node.outputs.length, link_drag.from_socket);
  86. }
  87. else {
  88. link_y += ui_nodes_INPUT_Y(ui_nodes_get_canvas(true), node.inputs, link_drag.to_socket) +
  89. UI_OUTPUTS_H(node.outputs.length) + UI_BUTTONS_H(node);
  90. }
  91. if (math_abs(mouse_x - link_x) > 5 || math_abs(mouse_y - link_y) > 5) { // Link length
  92. _ui_nodes_on_link_drag_link_drag = link_drag;
  93. _ui_nodes_on_link_drag_node = node;
  94. ui_nodes_node_search(-1, -1, function () {
  95. let ui_nodes: ui_nodes_t = ui_nodes_get_nodes();
  96. let node_selected_id: i32 = _ui_nodes_on_link_drag_node.id;
  97. if (ui_nodes.nodes_selected_id.length > 0) {
  98. node_selected_id = ui_nodes.nodes_selected_id[0];
  99. }
  100. let link_drag: ui_node_link_t = _ui_nodes_on_link_drag_link_drag;
  101. let nodes: ui_node_t[] = ui_nodes_get_canvas(true).nodes;
  102. let n: ui_node_t = ui_get_node(nodes, node_selected_id);
  103. if (link_drag.to_id == -1 && n.inputs.length > 0) {
  104. link_drag.to_id = n.id;
  105. let from_type: string = _ui_nodes_on_link_drag_node.outputs[link_drag.from_socket].type;
  106. // Connect to the first socket
  107. link_drag.to_socket = 0;
  108. // Try to find the first type-matching socket and use it if present
  109. for (let i: i32 = 0; i < n.inputs.length; ++i) {
  110. let socket: ui_node_socket_t = n.inputs[i];
  111. if (socket.type == from_type) {
  112. link_drag.to_socket = array_index_of(n.inputs, socket);
  113. break;
  114. }
  115. }
  116. array_push(ui_nodes_get_canvas(true).links, link_drag);
  117. }
  118. else if (link_drag.from_id == -1 && n.outputs.length > 0) {
  119. link_drag.from_id = n.id;
  120. link_drag.from_socket = 0;
  121. array_push(ui_nodes_get_canvas(true).links, link_drag);
  122. }
  123. ///if is_lab
  124. parser_logic_parse(ui_nodes_get_canvas(true));
  125. context_raw.rdirty = 5;
  126. ///end
  127. });
  128. }
  129. // Selecting which node socket to preview
  130. else if (ui_nodes.nodes_selected_id.length > 0 && node.id == ui_nodes.nodes_selected_id[0]) {
  131. context_raw.node_preview_socket = link_drag.from_id > -1 ? link_drag.from_socket : 0;
  132. ///if is_paint
  133. context_raw.node_preview_dirty = true;
  134. ///end
  135. }
  136. }
  137. function ui_viewnodes_on_socket_released(socket_id: i32) {
  138. let nodes: ui_nodes_t = ui_nodes_get_nodes();
  139. let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
  140. let socket: ui_node_socket_t = ui_get_socket(canvas.nodes, socket_id);
  141. let node: ui_node_t = ui_get_node(canvas.nodes, socket.node_id);
  142. if (ui_nodes_ui.input_released_r) {
  143. if (node.type == "GROUP_INPUT" || node.type == "GROUP_OUTPUT") {
  144. _ui_nodes_on_socket_released_socket = socket;
  145. _ui_nodes_on_socket_released_node = node;
  146. sys_notify_on_next_frame(function () {
  147. ui_menu_draw(function (ui: ui_t) {
  148. let socket: ui_node_socket_t = _ui_nodes_on_socket_released_socket;
  149. let node: ui_node_t = _ui_nodes_on_socket_released_node;
  150. if (ui_menu_button(tr("Edit"))) {
  151. _ui_nodes_htype.position = socket.type == "RGBA" ? 0 : socket.type == "VECTOR" ? 1 : 2;
  152. _ui_nodes_hname.text = socket.name;
  153. _ui_nodes_hmin.value = socket.min;
  154. _ui_nodes_hmax.value = socket.max;
  155. if (socket.type == "RGBA" || socket.type == "VECTOR") {
  156. _ui_nodes_hval0.value = socket.default_value[0];
  157. _ui_nodes_hval1.value = socket.default_value[1];
  158. _ui_nodes_hval2.value = socket.default_value[2];
  159. if (socket.type == "RGBA") {
  160. _ui_nodes_hval3.value = socket.default_value[3];
  161. }
  162. }
  163. else {
  164. _ui_nodes_hval0.value = socket.default_value[0];
  165. }
  166. sys_notify_on_next_frame(function () {
  167. ui_end_input();
  168. ui_box_show_custom(function (ui: ui_t) {
  169. let socket: ui_node_socket_t = _ui_nodes_on_socket_released_socket;
  170. let node: ui_node_t = _ui_nodes_on_socket_released_node;
  171. if (ui_tab(ui_handle(__ID__), tr("Socket"))) {
  172. let type_combo: string[] = [tr("Color"), tr("Vector"), tr("Value")];
  173. let type: i32 = ui_combo(_ui_nodes_htype, type_combo, tr("Type"), true);
  174. if (_ui_nodes_htype.changed) {
  175. _ui_nodes_hname.text = type == 0 ? tr("Color") : type == 1 ? tr("Vector") : tr("Value");
  176. }
  177. let name: string = ui_text_input(_ui_nodes_hname, tr("Name"));
  178. let min: f32 = ui_float_input(_ui_nodes_hmin, tr("Min"));
  179. let max: f32 = ui_float_input(_ui_nodes_hmax, tr("Max"));
  180. let default_value: f32_array_t = null;
  181. if (type == 0) {
  182. ui_row4();
  183. ui_float_input(_ui_nodes_hval0, tr("R"));
  184. ui_float_input(_ui_nodes_hval1, tr("G"));
  185. ui_float_input(_ui_nodes_hval2, tr("B"));
  186. ui_float_input(_ui_nodes_hval3, tr("A"));
  187. default_value = f32_array_create_xyzw(
  188. _ui_nodes_hval0.value, _ui_nodes_hval1.value, _ui_nodes_hval2.value, _ui_nodes_hval3.value);
  189. }
  190. else if (type == 1) {
  191. ui_row3();
  192. _ui_nodes_hval0.value = ui_float_input(_ui_nodes_hval0, tr("X"));
  193. _ui_nodes_hval1.value = ui_float_input(_ui_nodes_hval1, tr("Y"));
  194. _ui_nodes_hval2.value = ui_float_input(_ui_nodes_hval2, tr("Z"));
  195. default_value = f32_array_create_xyz(
  196. _ui_nodes_hval0.value, _ui_nodes_hval1.value, _ui_nodes_hval2.value);
  197. }
  198. else {
  199. let f: f32 = ui_float_input(_ui_nodes_hval0, tr("default_value"));
  200. default_value = f32_array_create_x(f);
  201. }
  202. if (ui_button(tr("OK"))) { // || ui.isReturnDown
  203. socket.name = name;
  204. socket.type = type == 0 ? "RGBA" : type == 1 ? "VECTOR" : "VALUE";
  205. socket.color = nodes_material_get_socket_color(socket.type);
  206. socket.min = min;
  207. socket.max = max;
  208. socket.default_value = default_value;
  209. ui_box_hide();
  210. nodes_material_sync_sockets(node);
  211. ui_nodes_hwnd.redraws = 2;
  212. }
  213. }
  214. }, 400, 250);
  215. });
  216. }
  217. if (ui_menu_button(tr("Delete"))) {
  218. let i: i32 = 0;
  219. let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
  220. // Remove links connected to the socket
  221. while (i < canvas.links.length) {
  222. let l: ui_node_link_t = canvas.links[i];
  223. if ((l.from_id == node.id && l.from_socket == array_index_of(node.outputs, socket)) ||
  224. (l.to_id == node.id && l.to_socket == array_index_of(node.inputs, socket))) {
  225. array_splice(canvas.links, i, 1);
  226. }
  227. else {
  228. i++;
  229. }
  230. }
  231. // Remove socket
  232. array_remove(node.inputs, socket);
  233. array_remove(node.outputs, socket);
  234. nodes_material_sync_sockets(node);
  235. }
  236. });
  237. });
  238. }
  239. else ui_viewnodes_on_canvas_released();
  240. }
  241. // Selecting which node socket to preview
  242. else if (nodes.nodes_selected_id.length > 0 && node.id == nodes.nodes_selected_id[0]) {
  243. let i: i32 = array_index_of(node.outputs, socket);
  244. if (i > -1) {
  245. context_raw.node_preview_socket = i;
  246. ///if is_paint
  247. context_raw.node_preview_dirty = true;
  248. ///end
  249. }
  250. }
  251. }
  252. function ui_viewnodes_on_canvas_released() {
  253. if (ui_nodes_ui.input_released_r &&
  254. context_in_nodes() &&
  255. math_abs(ui_nodes_ui.input_x - ui_nodes_ui.input_started_x) < 2 &&
  256. math_abs(ui_nodes_ui.input_y - ui_nodes_ui.input_started_y) < 2) {
  257. // Node selection
  258. let nodes: ui_nodes_t = ui_nodes_get_nodes();
  259. let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
  260. let selected: ui_node_t = null;
  261. for (let i: i32 = 0; i < canvas.nodes.length; ++i) {
  262. let node: ui_node_t = canvas.nodes[i];
  263. if (ui_input_in_rect(
  264. ui_nodes_ui._window_x + UI_NODE_X(node),
  265. ui_nodes_ui._window_y + UI_NODE_Y(node),
  266. UI_NODE_W(node),
  267. UI_NODE_H(canvas, node))) {
  268. selected = node;
  269. break;
  270. }
  271. }
  272. if (selected == null) {
  273. nodes.nodes_selected_id = [];
  274. }
  275. else if (array_index_of(nodes.nodes_selected_id, selected.id) == -1) {
  276. nodes.nodes_selected_id = [selected.id];
  277. }
  278. // Node context menu
  279. if (!ui_nodes_socket_released) {
  280. _ui_nodes_on_canvas_released_selected = selected;
  281. ui_menu_draw(function (ui_menu: ui_t) {
  282. let selected: ui_node_t = _ui_nodes_on_canvas_released_selected;
  283. ui_menu._y += 1;
  284. let is_protected: bool = selected == null ||
  285. ///if is_paint
  286. selected.type == "OUTPUT_MATERIAL_PBR" ||
  287. ///end
  288. selected.type == "GROUP_INPUT" ||
  289. selected.type == "GROUP_OUTPUT" ||
  290. selected.type == "brush_output_node";
  291. ui_menu.enabled = !is_protected;
  292. if (ui_menu_button(tr("Cut"), "ctrl+x")) {
  293. sys_notify_on_next_frame(function () {
  294. ui_nodes_hwnd.redraws = 2;
  295. ui_is_copy = true;
  296. ui_is_cut = true;
  297. ui_nodes_is_node_menu_op = true;
  298. });
  299. }
  300. if (ui_menu_button(tr("Copy"), "ctrl+c")) {
  301. sys_notify_on_next_frame(function () {
  302. ui_is_copy = true;
  303. ui_nodes_is_node_menu_op = true;
  304. });
  305. }
  306. ui_menu.enabled = ui_clipboard != "";
  307. if (ui_menu_button(tr("Paste"), "ctrl+v")) {
  308. sys_notify_on_next_frame(function () {
  309. ui_nodes_hwnd.redraws = 2;
  310. ui_is_paste = true;
  311. ui_nodes_is_node_menu_op = true;
  312. });
  313. }
  314. ui_menu.enabled = !is_protected;
  315. if (ui_menu_button(tr("Delete"), "delete")) {
  316. sys_notify_on_next_frame(function () {
  317. ui_nodes_hwnd.redraws = 2;
  318. ui_nodes_ui.is_delete_down = true;
  319. ui_nodes_is_node_menu_op = true;
  320. });
  321. }
  322. if (ui_menu_button(tr("Duplicate"))) {
  323. sys_notify_on_next_frame(function () {
  324. ui_nodes_hwnd.redraws = 2;
  325. ui_is_copy = true;
  326. ui_is_paste = true;
  327. ui_nodes_is_node_menu_op = true;
  328. });
  329. }
  330. if (selected != null && selected.type == "RGB") {
  331. if (ui_menu_button(tr("Add Swatch"))) {
  332. let color: f32_array_t = selected.outputs[0].default_value;
  333. let new_swatch: swatch_color_t = make_swatch(color_from_floats(color[0], color[1], color[2], color[3]));
  334. context_set_swatch(new_swatch);
  335. array_push(project_raw.swatches, new_swatch);
  336. ui_base_hwnds[tab_area_t.STATUS].redraws = 1;
  337. }
  338. }
  339. if (ui_nodes_canvas_type == canvas_type_t.MATERIAL) {
  340. ui_menu_separator(ui_menu);
  341. if (ui_menu_button(tr("2D View"))) {
  342. ui_base_show_2d_view(view_2d_type_t.NODE);
  343. }
  344. }
  345. ui_menu.enabled = true;
  346. });
  347. }
  348. }
  349. if (ui_nodes_ui.input_released) {
  350. let nodes: ui_nodes_t = ui_nodes_get_nodes();
  351. let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
  352. for (let i: i32 = 0; i < canvas.nodes.length; ++i) {
  353. let node: ui_node_t = canvas.nodes[i];
  354. if (ui_input_in_rect(ui_nodes_ui._window_x + UI_NODE_X(node), ui_nodes_ui._window_y + UI_NODE_Y(node), UI_NODE_W(node), UI_NODE_H(canvas, node))) {
  355. if (nodes.nodes_selected_id.length > 0 && node.id == nodes.nodes_selected_id[0]) {
  356. ui_view2d_hwnd.redraws = 2;
  357. if (sys_time() - context_raw.select_time < 0.25) ui_base_show_2d_view(view_2d_type_t.NODE);
  358. context_raw.select_time = sys_time();
  359. }
  360. break;
  361. }
  362. }
  363. }
  364. }
  365. function ui_viewnodes_on_canvas_control(): ui_canvas_control_t {
  366. let control: ui_canvas_control_t = ui_nodes_get_canvas_control(ui_nodes_ui, ui_nodes_controls_down);
  367. ui_nodes_controls_down = control.controls_down;
  368. return control;
  369. }
  370. function ui_nodes_get_canvas_control(ui: ui_t, controls_down: bool): ui_canvas_control_t {
  371. if (config_raw.wrap_mouse && controls_down) {
  372. if (ui.input_x < ui._window_x) {
  373. ui.input_x = ui._window_x + ui._window_w;
  374. iron_mouse_set_position(math_floor(ui.input_x), math_floor(ui.input_y));
  375. }
  376. else if (ui.input_x > ui._window_x + ui._window_w) {
  377. ui.input_x = ui._window_x;
  378. iron_mouse_set_position(math_floor(ui.input_x), math_floor(ui.input_y));
  379. }
  380. else if (ui.input_y < ui._window_y) {
  381. ui.input_y = ui._window_y + ui._window_h;
  382. iron_mouse_set_position(math_floor(ui.input_x), math_floor(ui.input_y));
  383. }
  384. else if (ui.input_y > ui._window_y + ui._window_h) {
  385. ui.input_y = ui._window_y;
  386. iron_mouse_set_position(math_floor(ui.input_x), math_floor(ui.input_y));
  387. }
  388. }
  389. if (operator_shortcut(map_get(config_keymap, "action_pan"), shortcut_type_t.STARTED) ||
  390. operator_shortcut(map_get(config_keymap, "action_zoom"), shortcut_type_t.STARTED) ||
  391. ui.input_started_r ||
  392. ui.input_wheel_delta != 0.0) {
  393. controls_down = true;
  394. }
  395. else if (!operator_shortcut(map_get(config_keymap, "action_pan"), shortcut_type_t.DOWN) &&
  396. !operator_shortcut(map_get(config_keymap, "action_zoom"), shortcut_type_t.DOWN) &&
  397. !ui.input_down_r &&
  398. ui.input_wheel_delta == 0.0) {
  399. controls_down = false;
  400. }
  401. if (!controls_down) {
  402. let cc: ui_canvas_control_t = {
  403. pan_x: 0,
  404. pan_y: 0,
  405. zoom: 0,
  406. controls_down: controls_down
  407. };
  408. return cc;
  409. }
  410. let pan: bool = ui.input_down_r || operator_shortcut(map_get(config_keymap, "action_pan"), shortcut_type_t.DOWN);
  411. let zoom_delta: f32 = operator_shortcut(map_get(config_keymap, "action_zoom"), shortcut_type_t.DOWN) ? ui_nodes_get_zoom_delta(ui) / 100.0 : 0.0;
  412. let control: ui_canvas_control_t = {
  413. pan_x: pan ? ui.input_dx : 0.0,
  414. pan_y: pan ? ui.input_dy : 0.0,
  415. zoom: ui.input_wheel_delta != 0.0 ? -ui.input_wheel_delta / 10 : zoom_delta,
  416. controls_down: controls_down
  417. };
  418. if (base_is_combo_selected()) {
  419. control.zoom = 0.0;
  420. }
  421. if (control.zoom != 0.0) {
  422. ui_nodes_grid_redraw = true;
  423. }
  424. return control;
  425. }
  426. function ui_nodes_get_zoom_delta(ui: ui_t): f32 {
  427. return config_raw.zoom_direction == zoom_direction_t.VERTICAL ? -ui.input_dy :
  428. config_raw.zoom_direction == zoom_direction_t.VERTICAL_INVERTED ? -ui.input_dy :
  429. config_raw.zoom_direction == zoom_direction_t.HORIZONTAL ? ui.input_dx :
  430. config_raw.zoom_direction == zoom_direction_t.HORIZONTAL_INVERTED ? ui.input_dx :
  431. -(ui.input_dy - ui.input_dx);
  432. }
  433. function ui_nodes_is_tab_selected(): bool {
  434. return ui_nodes_htab.position > 0 &&
  435. ui_nodes_htab.position % 2 == 1 && // [tab0, tab1, x, tab2, x, +]
  436. ui_nodes_tabs.length >= ui_nodes_htab.position / 2;
  437. }
  438. function ui_nodes_tab_index(): i32 {
  439. return (int)(ui_nodes_htab.position / 2);
  440. }
  441. function ui_nodes_get_canvas(groups: bool = false): ui_node_canvas_t {
  442. ///if is_paint
  443. if (ui_nodes_canvas_type == canvas_type_t.MATERIAL) {
  444. if (groups && ui_nodes_group_stack.length > 0) {
  445. return ui_nodes_group_stack[ui_nodes_group_stack.length - 1].canvas;
  446. }
  447. else if (ui_nodes_is_tab_selected()) {
  448. return ui_nodes_tabs[ui_nodes_tab_index()].canvas;
  449. }
  450. else {
  451. return ui_nodes_get_canvas_material();
  452. }
  453. }
  454. else {
  455. return context_raw.brush.canvas;
  456. }
  457. ///end
  458. ///if is_lab
  459. return project_canvas;
  460. ///end
  461. }
  462. function ui_nodes_get_canvas_material(): ui_node_canvas_t {
  463. return context_raw.material.canvas;
  464. }
  465. function ui_nodes_get_nodes(): ui_nodes_t {
  466. ///if is_paint
  467. if (ui_nodes_canvas_type == canvas_type_t.MATERIAL) {
  468. if (ui_nodes_group_stack.length > 0) {
  469. return ui_nodes_group_stack[ui_nodes_group_stack.length - 1].nodes;
  470. }
  471. else if (ui_nodes_is_tab_selected()) {
  472. return ui_nodes_tabs[ui_nodes_tab_index()].nodes;
  473. }
  474. else {
  475. return context_raw.material.nodes;
  476. }
  477. }
  478. else {
  479. return context_raw.brush.nodes;
  480. }
  481. ///end
  482. ///if is_lab
  483. if (ui_nodes_group_stack.length > 0) {
  484. return ui_nodes_group_stack[ui_nodes_group_stack.length - 1].nodes;
  485. }
  486. else {
  487. return project_nodes;
  488. }
  489. ///end
  490. }
  491. function ui_nodes_update() {
  492. if (!ui_nodes_show || !base_ui_enabled) {
  493. return;
  494. }
  495. ///if is_paint
  496. ui_nodes_wx = math_floor(sys_w()) + ui_toolbar_w(true);
  497. ///end
  498. ///if is_lab
  499. ui_nodes_wx = math_floor(sys_w());
  500. ///end
  501. ui_nodes_wy = ui_header_h * 2;
  502. if (ui_view2d_show) {
  503. ui_nodes_wy += sys_h() - config_raw.layout[layout_size_t.NODES_H];
  504. }
  505. let ww: i32 = config_raw.layout[layout_size_t.NODES_W];
  506. if (!ui_base_show) {
  507. ///if is_paint
  508. ww += config_raw.layout[layout_size_t.SIDEBAR_W] + ui_toolbar_w(true);
  509. ui_nodes_wx -= ui_toolbar_w(true);
  510. ///end
  511. ui_nodes_wy = 0;
  512. }
  513. let mx: i32 = mouse_x;
  514. let my: i32 = mouse_y;
  515. if (mx < ui_nodes_wx || mx > ui_nodes_wx + ww || my < ui_nodes_wy) {
  516. return;
  517. }
  518. if (ui_nodes_ui.is_typing || !ui_nodes_ui.input_enabled) {
  519. return;
  520. }
  521. let nodes: ui_nodes_t = ui_nodes_get_nodes();
  522. if (nodes.nodes_selected_id.length > 0 && ui_nodes_ui.is_key_pressed) {
  523. if (ui_nodes_ui.key_code == key_code_t.LEFT) {
  524. for (let i: i32 = 0; i < nodes.nodes_selected_id.length; ++i) {
  525. let n: i32 = nodes.nodes_selected_id[i];
  526. let _nodes: ui_node_t[] = ui_nodes_get_canvas(true).nodes;
  527. ui_get_node(_nodes, n).x -= 1;
  528. }
  529. }
  530. else if (ui_nodes_ui.key_code == key_code_t.RIGHT) {
  531. for (let i: i32 = 0; i < nodes.nodes_selected_id.length; ++i) {
  532. let n: i32 = nodes.nodes_selected_id[i];
  533. let _nodes: ui_node_t[] = ui_nodes_get_canvas(true).nodes;
  534. ui_get_node(_nodes, n).x += 1;
  535. }
  536. }
  537. if (ui_nodes_ui.key_code == key_code_t.UP) {
  538. for (let i: i32 = 0; i < nodes.nodes_selected_id.length; ++i) {
  539. let n: i32 = nodes.nodes_selected_id[i];
  540. let _nodes: ui_node_t[] = ui_nodes_get_canvas(true).nodes;
  541. ui_get_node(_nodes, n).y -= 1;
  542. }
  543. }
  544. else if (ui_nodes_ui.key_code == key_code_t.DOWN) {
  545. for (let i: i32 = 0; i < nodes.nodes_selected_id.length; ++i) {
  546. let n: i32 = nodes.nodes_selected_id[i];
  547. let _nodes: ui_node_t[] = ui_nodes_get_canvas(true).nodes;
  548. ui_get_node(_nodes, n).y += 1;
  549. }
  550. }
  551. }
  552. // Node search popup
  553. if (operator_shortcut(map_get(config_keymap, "node_search"))) {
  554. ui_nodes_node_search();
  555. }
  556. if (ui_nodes_node_search_spawn != null) {
  557. ui_nodes_ui.input_x = mouse_x; // Fix inputDX after popup removal
  558. ui_nodes_ui.input_y = mouse_y;
  559. ui_nodes_node_search_spawn = null;
  560. }
  561. if (operator_shortcut(map_get(config_keymap, "view_reset"))) {
  562. nodes.pan_x = 0.0;
  563. nodes.pan_y = 0.0;
  564. nodes.zoom = 1.0;
  565. }
  566. }
  567. function ui_nodes_canvas_changed() {
  568. ui_nodes_recompile_mat = true;
  569. ui_nodes_recompile_mat_final = true;
  570. }
  571. function ui_nodes_node_search(x: i32 = -1, y: i32 = -1, done: ()=>void = null) {
  572. _ui_nodes_node_search_first = true;
  573. _ui_nodes_node_search_done = done;
  574. ui_menu_draw(function (ui: ui_t) {
  575. ui_menu_h = ui_ELEMENT_H(ui) * 8;
  576. let search_handle: ui_handle_t = ui_handle(__ID__);
  577. let search: string = to_lower_case(ui_text_input(search_handle, "", ui_align_t.LEFT, true, true));
  578. ui.changed = false;
  579. if (_ui_nodes_node_search_first) {
  580. _ui_nodes_node_search_first = false;
  581. search_handle.text = "";
  582. ui_start_text_edit(search_handle); // Focus search bar
  583. }
  584. if (search_handle.changed) {
  585. ui_nodes_node_search_offset = 0;
  586. }
  587. if (ui.is_key_pressed) { // Move selection
  588. if (ui.key_code == key_code_t.DOWN && ui_nodes_node_search_offset < 6) {
  589. ui_nodes_node_search_offset++;
  590. }
  591. if (ui.key_code == key_code_t.UP && ui_nodes_node_search_offset > 0) {
  592. ui_nodes_node_search_offset--;
  593. }
  594. }
  595. let enter: bool = keyboard_down("enter");
  596. let count: i32 = 0;
  597. let BUTTON_COL: i32 = ui.ops.theme.BUTTON_COL;
  598. let FILL_BUTTON_BG: bool = ui.ops.theme.FILL_BUTTON_BG;
  599. ui.ops.theme.FILL_BUTTON_BG = true;
  600. ///if is_paint
  601. let node_list: node_list_t[] = ui_nodes_canvas_type == canvas_type_t.MATERIAL ? nodes_material_list : nodes_brush_list;
  602. ///end
  603. ///if is_lab
  604. let node_list: node_list_t[] = nodes_brush_list;
  605. ///end
  606. for (let i: i32 = 0; i < node_list.length; ++i) {
  607. let list: ui_node_t[] = node_list[i];
  608. for (let i: i32 = 0; i < list.length; ++i) {
  609. let n: ui_node_t = list[i];
  610. if (string_index_of(to_lower_case(tr(n.name)), search) >= 0) {
  611. ui.ops.theme.BUTTON_COL = count == ui_nodes_node_search_offset ? ui.ops.theme.HIGHLIGHT_COL : ui.ops.theme.SEPARATOR_COL;
  612. if (ui_button(tr(n.name), ui_align_t.LEFT) || (enter && count == ui_nodes_node_search_offset)) {
  613. ui_nodes_push_undo();
  614. let nodes: ui_nodes_t = ui_nodes_get_nodes();
  615. let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
  616. ui_nodes_node_search_spawn = ui_nodes_make_node(n, nodes, canvas); // Spawn selected node
  617. array_push(canvas.nodes, ui_nodes_node_search_spawn);
  618. nodes.nodes_selected_id = [ui_nodes_node_search_spawn.id];
  619. nodes.nodes_drag = true;
  620. ///if is_lab
  621. parser_logic_parse(canvas);
  622. ///end
  623. ui_nodes_hwnd.redraws = 2;
  624. if (enter) {
  625. ui.changed = true;
  626. count = 6; // Trigger break
  627. }
  628. if (_ui_nodes_node_search_done != null) {
  629. _ui_nodes_node_search_done();
  630. }
  631. }
  632. if (++count > 6) {
  633. break;
  634. }
  635. }
  636. }
  637. if (count > 6) {
  638. break;
  639. }
  640. }
  641. if (enter && count == 0) { // Hide popup on enter when node is not found
  642. ui.changed = true;
  643. search_handle.text = "";
  644. }
  645. ui.ops.theme.BUTTON_COL = BUTTON_COL;
  646. ui.ops.theme.FILL_BUTTON_BG = FILL_BUTTON_BG;
  647. }, x, y);
  648. }
  649. function ui_nodes_get_node_x(): i32 {
  650. return math_floor((mouse_x - ui_nodes_wx - UI_NODES_PAN_X()) / UI_NODES_SCALE());
  651. }
  652. function ui_nodes_get_node_y(): i32 {
  653. return math_floor((mouse_y - ui_nodes_wy - UI_NODES_PAN_Y()) / UI_NODES_SCALE());
  654. }
  655. function ui_nodes_draw_grid(zoom: f32): gpu_texture_t {
  656. let ww: i32 = config_raw.layout[layout_size_t.NODES_W];
  657. ///if is_paint
  658. if (!ui_base_show) {
  659. ww += config_raw.layout[layout_size_t.SIDEBAR_W] + ui_toolbar_w(true);
  660. }
  661. ///end
  662. let wh: i32 = sys_h();
  663. let step: f32 = ui_nodes_grid_cell_w * zoom;
  664. let mult: i32 = 5 * ui_SCALE(ui_nodes_ui);
  665. let w: i32 = math_floor(ww + step * mult);
  666. let h: i32 = math_floor(wh + step * mult);
  667. if (w < 1) {
  668. w = 1;
  669. }
  670. if (h < 1) {
  671. h = 1;
  672. }
  673. let grid: gpu_texture_t = gpu_create_render_target(w, h);
  674. draw_begin(grid, true, ui_nodes_ui.ops.theme.SEPARATOR_COL);
  675. let sep_col: i32 = ui_nodes_ui.ops.theme.SEPARATOR_COL;
  676. let line_primary: i32 = sep_col - 0x00050505;
  677. if (line_primary < 0xff000000) {
  678. line_primary = sep_col + 0x00050505;
  679. }
  680. let line_secondary: i32 = sep_col - 0x00090909;
  681. if (line_secondary < 0xff000000) {
  682. line_secondary = sep_col + 0x00090909;
  683. }
  684. draw_set_color(line_primary);
  685. step = ui_nodes_grid_small_cell_w * zoom;
  686. for (let i: i32 = 0; i < math_floor(h / step) + 1; ++i) {
  687. draw_line(0, i * step, w, i * step);
  688. }
  689. for (let i: i32 = 0; i < math_floor(w / step) + 1; ++i) {
  690. draw_line(i * step, 0, i * step, h);
  691. }
  692. draw_set_color(line_secondary);
  693. step = ui_nodes_grid_cell_w * zoom;
  694. for (let i: i32 = 0; i < math_floor(h / step) + 1; ++i) {
  695. draw_line(0, i * step, w, i * step);
  696. }
  697. for (let i: i32 = 0; i < math_floor(w / step) + 1; ++i) {
  698. draw_line(i * step, 0, i * step, h);
  699. }
  700. draw_end();
  701. return grid;
  702. }
  703. let _ui_nodes_render_tmp: (col: i32)=>void;
  704. function ui_nodes_render() {
  705. if (ui_nodes_recompile_mat) {
  706. ///if is_paint
  707. if (ui_nodes_canvas_type == canvas_type_t.BRUSH) {
  708. make_material_parse_brush();
  709. util_render_make_brush_preview();
  710. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  711. }
  712. else {
  713. let _material: slot_material_t = context_raw.material;
  714. if (ui_nodes_is_tab_selected()) {
  715. context_raw.material = ui_nodes_tabs[ui_nodes_tab_index()];
  716. }
  717. layers_is_fill_material() ?
  718. layers_update_fill_layers() :
  719. util_render_make_material_preview();
  720. context_raw.material = _material;
  721. if (ui_view2d_show && ui_view2d_type == view_2d_type_t.NODE) {
  722. ui_view2d_hwnd.redraws = 2;
  723. }
  724. }
  725. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  726. if (context_raw.split_view) {
  727. context_raw.ddirty = 2;
  728. }
  729. ///end
  730. ///if is_lab
  731. parser_logic_parse(project_canvas);
  732. ///end
  733. ui_nodes_recompile_mat = false;
  734. }
  735. else if (ui_nodes_recompile_mat_final) {
  736. ///if is_paint
  737. make_material_parse_paint_material();
  738. if (ui_nodes_canvas_type == canvas_type_t.MATERIAL && layers_is_fill_material()) {
  739. layers_update_fill_layers();
  740. util_render_make_material_preview();
  741. }
  742. let decal: bool = context_is_decal();
  743. if (decal) {
  744. util_render_make_decal_preview();
  745. }
  746. ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
  747. context_raw.node_preview_dirty = true;
  748. ///end
  749. ui_nodes_recompile_mat_final = false;
  750. }
  751. let nodes: ui_nodes_t = ui_nodes_get_nodes();
  752. if (nodes.nodes_selected_id.length > 0 && nodes.nodes_selected_id[0] != ui_nodes_last_node_selected_id) {
  753. ui_nodes_last_node_selected_id = nodes.nodes_selected_id[0];
  754. ///if is_paint
  755. context_raw.node_preview_dirty = true;
  756. ///end
  757. ///if is_lab
  758. context_raw.ddirty = 2; // Show selected node texture in viewport
  759. ui_header_handle.redraws = 2;
  760. ///end
  761. context_raw.node_preview_socket = 0;
  762. }
  763. // Remove dragged link when mouse is released out of the node viewport
  764. let c: ui_node_canvas_t = ui_nodes_get_canvas(true);
  765. if (ui_nodes_release_link && nodes.link_drag_id != -1) {
  766. array_remove(c.links, ui_get_link(c.links, nodes.link_drag_id));
  767. nodes.link_drag_id = -1;
  768. }
  769. ui_nodes_release_link = ui_nodes_ui.input_released;
  770. if (!ui_nodes_show || iron_window_width() == 0 || iron_window_height() == 0) {
  771. return;
  772. }
  773. ui_nodes_ui.input_enabled = base_ui_enabled;
  774. if (ui_nodes_grid_redraw) {
  775. if (ui_nodes_grid != null) {
  776. iron_delete_texture(ui_nodes_grid);
  777. }
  778. let ui_nodes: ui_nodes_t = ui_nodes_get_nodes();
  779. ui_nodes_grid = ui_nodes_draw_grid(ui_nodes.zoom);
  780. ui_nodes_grid_redraw = false;
  781. }
  782. ///if is_paint
  783. if (config_raw.node_preview && context_raw.node_preview_dirty) {
  784. ui_nodes_make_node_preview();
  785. }
  786. ///end
  787. // Start with UI
  788. ui_begin(ui_nodes_ui);
  789. // Make window
  790. ui_nodes_ww = config_raw.layout[layout_size_t.NODES_W];
  791. ///if is_paint
  792. ui_nodes_wx = math_floor(sys_w()) + ui_toolbar_w(true);
  793. ///end
  794. ///if is_lab
  795. ui_nodes_wx = math_floor(sys_w());
  796. ///end
  797. ui_nodes_wy = 0;
  798. ///if is_paint
  799. if (!ui_base_show) {
  800. ui_nodes_ww += config_raw.layout[layout_size_t.SIDEBAR_W] + ui_toolbar_w(true);
  801. ui_nodes_wx -= ui_toolbar_w(true);
  802. }
  803. ///end
  804. let ew: i32 = math_floor(ui_ELEMENT_W(ui_nodes_ui) * 0.7);
  805. ui_nodes_wh = sys_h();
  806. if (config_raw.layout[layout_size_t.HEADER] == 1) {
  807. ui_nodes_wh += ui_header_h * 2;
  808. }
  809. if (ui_view2d_show) {
  810. ui_nodes_wh = config_raw.layout[layout_size_t.NODES_H];
  811. ui_nodes_wy = sys_h() - config_raw.layout[layout_size_t.NODES_H] + ui_header_h;
  812. if (config_raw.layout[layout_size_t.HEADER] == 1) {
  813. ui_nodes_wy += ui_header_h;
  814. }
  815. if (!ui_base_show) {
  816. ui_nodes_wy -= ui_header_h * 2;
  817. }
  818. }
  819. if (ui_window(ui_nodes_hwnd, ui_nodes_wx, ui_nodes_wy, ui_nodes_ww, ui_nodes_wh)) {
  820. ui_tab(ui_nodes_htab, tr("Nodes"));
  821. // Additional tabs
  822. if (ui_nodes_canvas_type == canvas_type_t.MATERIAL) {
  823. if (ui_nodes_tabs == null) {
  824. ui_nodes_tabs = [];
  825. }
  826. for (let i: i32 = 0; i < ui_nodes_tabs.length; ++i) {
  827. ui_tab(ui_nodes_htab, ui_nodes_tabs[i].canvas.name);
  828. if (ui_tab(ui_nodes_htab, tr("x"))) {
  829. array_splice(ui_nodes_tabs, i, 1);
  830. ui_nodes_htab.position = 0;
  831. }
  832. }
  833. ///if is_paint
  834. if (ui_tab(ui_nodes_htab, tr("+"))) {
  835. array_push(ui_nodes_tabs, context_raw.material);
  836. }
  837. ///end
  838. }
  839. // Grid
  840. draw_set_color(0xffffffff);
  841. let step: f32 = ui_nodes_grid_cell_w * nodes.zoom;
  842. let x: f32 = math_fmod(UI_NODES_PAN_X(), step) - step;
  843. let y: f32 = math_fmod(UI_NODES_PAN_Y(), step) - step;
  844. draw_image(ui_nodes_grid, x, y);
  845. // Undo
  846. if (ui_nodes_ui.input_started || ui_nodes_ui.is_key_pressed) {
  847. ui_nodes_last_canvas = util_clone_canvas(ui_nodes_get_canvas(true));
  848. }
  849. // Nodes
  850. let _input_enabled: bool = ui_nodes_ui.input_enabled;
  851. ui_nodes_ui.input_enabled = _input_enabled && !ui_nodes_show_menu;
  852. ///if is_paint
  853. ui_nodes_ui.window_border_right = config_raw.layout[layout_size_t.SIDEBAR_W];
  854. ///end
  855. ui_nodes_ui.window_border_top = ui_header_h * 2;
  856. ui_nodes_ui.window_border_bottom = config_raw.layout[layout_size_t.STATUS_H];
  857. ui_node_canvas(nodes, c);
  858. ui_nodes_ui.input_enabled = _input_enabled;
  859. if (nodes.color_picker_callback != null) {
  860. context_raw.color_picker_previous_tool = context_raw.tool;
  861. context_select_tool(workspace_tool_t.PICKER);
  862. _ui_nodes_render_tmp = nodes.color_picker_callback;
  863. context_raw.color_picker_callback = function (color: swatch_color_t) {
  864. _ui_nodes_render_tmp(color.base);
  865. ui_nodes_hwnd.redraws = 2;
  866. ///if is_paint
  867. let material_live: bool = config_raw.material_live;
  868. ///end
  869. ///if is_lab
  870. let material_live: bool = true;
  871. ///end
  872. if (material_live) {
  873. ui_nodes_canvas_changed();
  874. }
  875. };
  876. nodes.color_picker_callback = null;
  877. }
  878. // Remove nodes with unknown id for this canvas type
  879. if (ui_is_paste) {
  880. ///if is_paint
  881. let node_list: node_list_t[] = ui_nodes_canvas_type == canvas_type_t.MATERIAL ? nodes_material_list : nodes_brush_list;
  882. ///end
  883. ///if is_lab
  884. let node_list: node_list_t[] = nodes_brush_list;
  885. ///end
  886. let i: i32 = 0;
  887. while (i++ < c.nodes.length) {
  888. let canvas_node: ui_node_t = c.nodes[i - 1];
  889. if (array_index_of(ui_nodes_exclude_remove, canvas_node.type) >= 0) {
  890. continue;
  891. }
  892. let found: bool = false;
  893. for (let i: i32 = 0; i < node_list.length; ++i) {
  894. let list: ui_node_t[] = node_list[i];
  895. for (let i: i32 = 0; i < list.length; ++i) {
  896. let list_node: ui_node_t = list[i];
  897. if (canvas_node.type == list_node.type) {
  898. found = true;
  899. break;
  900. }
  901. }
  902. if (found) {
  903. break;
  904. }
  905. }
  906. if (canvas_node.type == "GROUP" && !ui_nodes_can_place_group(canvas_node.name)) {
  907. found = false;
  908. }
  909. if (!found) {
  910. ui_remove_node(canvas_node, c);
  911. array_remove(nodes.nodes_selected_id, canvas_node.id);
  912. i--;
  913. }
  914. }
  915. }
  916. if (ui_nodes_is_node_menu_op) {
  917. ui_is_copy = false;
  918. ui_is_cut = false;
  919. ui_is_paste = false;
  920. ui_nodes_ui.is_delete_down = false;
  921. }
  922. // Recompile material on change
  923. if (ui_nodes_ui.changed) {
  924. ///if is_paint
  925. ui_nodes_recompile_mat = (ui_nodes_ui.input_dx != 0 || ui_nodes_ui.input_dy != 0 || !ui_nodes_uichanged_last) && config_raw.material_live; // Instant preview
  926. ///end
  927. ///if is_lab
  928. ui_nodes_recompile_mat = (ui_nodes_ui.input_dx != 0 || ui_nodes_ui.input_dy != 0 || !ui_nodes_uichanged_last); // Instant preview
  929. ///end
  930. }
  931. else if (ui_nodes_uichanged_last) {
  932. ui_nodes_canvas_changed();
  933. ui_nodes_push_undo(ui_nodes_last_canvas);
  934. }
  935. ui_nodes_uichanged_last = ui_nodes_ui.changed;
  936. // Node previews
  937. if (config_raw.node_preview && nodes.nodes_selected_id.length > 0) {
  938. let img: gpu_texture_t = null;
  939. let sel: ui_node_t = ui_get_node(c.nodes, nodes.nodes_selected_id[0]);
  940. ///if is_paint
  941. let single_channel: bool = sel.type == "LAYER_MASK";
  942. if (sel.type == "LAYER" || sel.type == "LAYER_MASK") {
  943. let id: i32 = sel.buttons[0].default_value[0];
  944. if (id < project_layers.length) {
  945. ///if is_paint
  946. img = project_layers[id].texpaint_preview;
  947. ///end
  948. }
  949. }
  950. else if (sel.type == "MATERIAL") {
  951. let id: i32 = sel.buttons[0].default_value[0];
  952. if (id < project_materials.length) {
  953. img = project_materials[id].image;
  954. }
  955. }
  956. else if (sel.type == "OUTPUT_MATERIAL_PBR") {
  957. img = context_raw.material.image;
  958. }
  959. else if (sel.type == "brush_output_node") {
  960. img = context_raw.brush.image;
  961. }
  962. else if (ui_nodes_canvas_type == canvas_type_t.MATERIAL) {
  963. img = context_raw.node_preview;
  964. }
  965. ///else
  966. let brush_node: logic_node_ext_t = parser_logic_get_logic_node(sel);
  967. if (brush_node != null) {
  968. img = logic_node_get_cached_image(brush_node.base);
  969. }
  970. ///end
  971. if (img != null) {
  972. let tw: f32 = 128 * ui_SCALE(ui_nodes_ui);
  973. let th: f32 = tw * (img.height / img.width);
  974. let tx: f32 = ui_nodes_ww - tw - 8 * ui_SCALE(ui_nodes_ui);
  975. let ty: f32 = ui_nodes_wh - th - 8 * ui_SCALE(ui_nodes_ui);
  976. let invert_y: bool = false;
  977. ///if is_paint
  978. if (single_channel) {
  979. draw_set_pipeline(ui_view2d_pipe);
  980. gpu_set_int(ui_view2d_channel_loc, 1);
  981. }
  982. ///end
  983. draw_set_color(0xffffffff);
  984. invert_y ?
  985. draw_scaled_image(img, tx, ty + th, tw, -th) :
  986. draw_scaled_image(img, tx, ty, tw, th);
  987. ///if is_paint
  988. if (single_channel) {
  989. draw_set_pipeline(null);
  990. }
  991. ///end
  992. }
  993. }
  994. // Menu
  995. draw_set_color(ui_nodes_ui.ops.theme.SEPARATOR_COL);
  996. draw_filled_rect(0, ui_ELEMENT_H(ui_nodes_ui), ui_nodes_ww, ui_ELEMENT_H(ui_nodes_ui) + ui_ELEMENT_OFFSET(ui_nodes_ui) * 2);
  997. draw_set_color(0xffffffff);
  998. let start_y: i32 = ui_ELEMENT_H(ui_nodes_ui) + ui_ELEMENT_OFFSET(ui_nodes_ui);
  999. ui_nodes_ui._x = 0;
  1000. ui_nodes_ui._y = 2 + start_y;
  1001. ui_nodes_ui._w = ew;
  1002. ///if is_paint
  1003. // Editable canvas name
  1004. let h: ui_handle_t = ui_handle(__ID__);
  1005. h.text = c.name;
  1006. ui_nodes_ui._w = math_floor(math_min(draw_string_width(ui_nodes_ui.ops.font, ui_nodes_ui.font_size, h.text) + 15 * ui_SCALE(ui_nodes_ui), 100 * ui_SCALE(ui_nodes_ui)));
  1007. let new_name: string = ui_text_input(h, "");
  1008. ui_nodes_ui._x += ui_nodes_ui._w + 3;
  1009. ui_nodes_ui._y = 2 + start_y;
  1010. ui_nodes_ui._w = ew;
  1011. if (h.changed) { // Check whether renaming is possible and update group links
  1012. if (ui_nodes_group_stack.length > 0) {
  1013. let can_rename: bool = true;
  1014. for (let i: i32 = 0; i < project_material_groups.length; ++i) {
  1015. let m: node_group_t = project_material_groups[i];
  1016. if (m.canvas.name == new_name) {
  1017. can_rename = false; // Name already used
  1018. }
  1019. }
  1020. if (can_rename) {
  1021. let old_name: string = c.name;
  1022. c.name = new_name;
  1023. let canvases: ui_node_canvas_t[] = [];
  1024. for (let i: i32 = 0; i < project_materials.length; ++i) {
  1025. let m: slot_material_t = project_materials[i];
  1026. array_push(canvases, m.canvas);
  1027. }
  1028. for (let i: i32 = 0; i < project_material_groups.length; ++i) {
  1029. let m: node_group_t = project_material_groups[i];
  1030. array_push(canvases, m.canvas);
  1031. }
  1032. for (let i: i32 = 0; i < canvases.length; ++i) {
  1033. let canvas: ui_node_canvas_t = canvases[i];
  1034. for (let i: i32 = 0; i < canvas.nodes.length; ++i) {
  1035. let n: ui_node_t = canvas.nodes[i];
  1036. if (n.type == "GROUP" && n.name == old_name) {
  1037. n.name = c.name;
  1038. }
  1039. }
  1040. }
  1041. }
  1042. }
  1043. else {
  1044. c.name = new_name;
  1045. }
  1046. }
  1047. ///end
  1048. ///if is_lab
  1049. ui_nodes_ui.window_border_top = 0;
  1050. ui_nodes_ext_draw_buttons(ew, start_y);
  1051. ///end
  1052. let _BUTTON_COL: i32 = ui_nodes_ui.ops.theme.BUTTON_COL;
  1053. ui_nodes_ui.ops.theme.BUTTON_COL = ui_nodes_ui.ops.theme.SEPARATOR_COL;
  1054. ///if is_paint
  1055. let cats: string[] = ui_nodes_canvas_type == canvas_type_t.MATERIAL ? nodes_material_categories : nodes_brush_categories;
  1056. ///end
  1057. ///if is_lab
  1058. let cats: string[] = nodes_brush_categories;
  1059. ///end
  1060. for (let i: i32 = 0; i < cats.length; ++i) {
  1061. if ((_ui_menu_button(tr(cats[i]))) || (ui_nodes_ui.is_hovered && ui_nodes_show_menu)) {
  1062. ui_nodes_show_menu = true;
  1063. ui_nodes_menu_category = i;
  1064. ui_nodes_popup_x = ui_nodes_wx + ui_nodes_ui._x;
  1065. ui_nodes_popup_y = ui_nodes_wy + ui_nodes_ui._y;
  1066. if (config_raw.touch_ui) {
  1067. ui_nodes_show_menu_first = true;
  1068. let menuw: i32 = math_floor(ew * 2.3);
  1069. ui_nodes_popup_x -= menuw / 2;
  1070. ui_nodes_popup_x += ui_nodes_ui._w / 2;
  1071. }
  1072. }
  1073. ui_nodes_ui._x += ui_nodes_ui._w + 3;
  1074. ui_nodes_ui._y = 2 + start_y;
  1075. }
  1076. if (config_raw.touch_ui) {
  1077. let _w: i32 = ui_nodes_ui._w;
  1078. ui_nodes_ui._w = math_floor(36 * ui_SCALE(ui_nodes_ui));
  1079. ui_nodes_ui._y = 4 * ui_SCALE(ui_nodes_ui) + start_y;
  1080. if (ui_menubar_icon_button(ui_nodes_ui, 2, 3)) {
  1081. ui_nodes_node_search(math_floor(ui_nodes_ui._window_x + ui_nodes_ui._x), math_floor(ui_nodes_ui._window_y + ui_nodes_ui._y));
  1082. }
  1083. ui_nodes_ui._w = _w;
  1084. }
  1085. else {
  1086. if (_ui_menu_button(tr("Search"))) {
  1087. ui_nodes_node_search_x = ui_nodes_ui._window_x + ui_nodes_ui._x;
  1088. ui_nodes_node_search_y = ui_nodes_ui._window_y + ui_nodes_ui._y;
  1089. // Allow for node menu to be closed first
  1090. sys_notify_on_init(function() {
  1091. ui_nodes_node_search(math_floor(ui_nodes_node_search_x), math_floor(ui_nodes_node_search_y));
  1092. });
  1093. }
  1094. }
  1095. if (ui_nodes_ui.is_hovered) {
  1096. ui_tooltip(tr("Search for nodes") + " (" + map_get(config_keymap, "node_search") + ")");
  1097. }
  1098. ui_nodes_ui._x += ui_nodes_ui._w + 3;
  1099. ui_nodes_ui._y = 2 + start_y;
  1100. ui_nodes_ui.ops.theme.BUTTON_COL = _BUTTON_COL;
  1101. // Close node group
  1102. if (ui_nodes_group_stack.length > 0 && _ui_menu_button(tr("Close"))) {
  1103. array_pop(ui_nodes_group_stack);
  1104. }
  1105. }
  1106. ui_end(!ui_nodes_show_menu);
  1107. if (ui_nodes_show_menu) {
  1108. ///if is_paint
  1109. let list: node_list_t[] = ui_nodes_canvas_type == canvas_type_t.MATERIAL ? nodes_material_list : nodes_brush_list;
  1110. ///end
  1111. ///if is_lab
  1112. let list: node_list_t[] = nodes_brush_list;
  1113. ///end
  1114. let category: ui_node_t[] = list[ui_nodes_menu_category];
  1115. let num_nodes: i32 = category.length;
  1116. ///if is_paint
  1117. let is_group_category: bool = ui_nodes_canvas_type == canvas_type_t.MATERIAL && nodes_material_categories[ui_nodes_menu_category] == "Group";
  1118. ///end
  1119. ///if is_lab
  1120. let is_group_category: bool = nodes_material_categories[ui_nodes_menu_category] == "Group";
  1121. ///end
  1122. if (is_group_category) {
  1123. num_nodes += project_material_groups.length;
  1124. }
  1125. let py: i32 = ui_nodes_popup_y;
  1126. let menuw: i32 = math_floor(ew * 2.3);
  1127. draw_begin(null);
  1128. ui_begin_region(ui_nodes_ui, math_floor(ui_nodes_popup_x), math_floor(py), menuw);
  1129. let _FILL_BUTTON_BG: i32 = ui_nodes_ui.ops.theme.FILL_BUTTON_BG;
  1130. ui_nodes_ui.ops.theme.FILL_BUTTON_BG = false;
  1131. let _ELEMENT_OFFSET: i32 = ui_nodes_ui.ops.theme.ELEMENT_OFFSET;
  1132. ui_nodes_ui.ops.theme.ELEMENT_OFFSET = 0;
  1133. let _ELEMENT_H: i32 = ui_nodes_ui.ops.theme.ELEMENT_H;
  1134. ui_nodes_ui.ops.theme.ELEMENT_H = config_raw.touch_ui ? (28 + 2) : 28;
  1135. ui_menu_h = category.length * ui_ELEMENT_H(ui_nodes_ui);
  1136. if (is_group_category) {
  1137. ui_menu_h += project_material_groups.length * ui_ELEMENT_H(ui_nodes_ui);
  1138. }
  1139. ui_menu_start(ui_nodes_ui);
  1140. for (let i: i32 = 0; i < category.length; ++i) {
  1141. let n: ui_node_t = category[i];
  1142. if (ui_menu_button(tr(n.name))) {
  1143. ui_nodes_push_undo();
  1144. let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
  1145. let nodes: ui_nodes_t = ui_nodes_get_nodes();
  1146. let node: ui_node_t = ui_nodes_make_node(n, nodes, canvas);
  1147. array_push(canvas.nodes, node);
  1148. nodes.nodes_selected_id = [node.id];
  1149. nodes.nodes_drag = true;
  1150. ///if is_lab
  1151. parser_logic_parse(canvas);
  1152. ///end
  1153. }
  1154. // Next column
  1155. if (ui_nodes_ui._y - ui_nodes_wy + ui_ELEMENT_H(ui_nodes_ui) / 2 > ui_nodes_wh) {
  1156. ui_nodes_ui._x += menuw;
  1157. ui_nodes_ui._y = py;
  1158. }
  1159. }
  1160. if (is_group_category) {
  1161. for (let i: i32 = 0; i < project_material_groups.length; ++i) {
  1162. let g: node_group_t = project_material_groups[i];
  1163. ui_nodes_ui.enabled = ui_nodes_can_place_group(g.canvas.name);
  1164. let row: f32[] = [5 / 6, 1 / 6];
  1165. ui_row(row);
  1166. if (ui_button(config_button_spacing + g.canvas.name, ui_align_t.LEFT)) {
  1167. ui_nodes_push_undo();
  1168. let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
  1169. let nodes: ui_nodes_t = ui_nodes_get_nodes();
  1170. let node: ui_node_t = ui_nodes_make_group_node(g.canvas, nodes, canvas);
  1171. array_push(canvas.nodes, node);
  1172. nodes.nodes_selected_id = [node.id];
  1173. nodes.nodes_drag = true;
  1174. }
  1175. ///if is_paint
  1176. ui_nodes_ui.enabled = !project_is_material_group_in_use(g);
  1177. if (ui_button("x", ui_align_t.CENTER)) {
  1178. history_delete_material_group(g);
  1179. array_remove(project_material_groups, g);
  1180. }
  1181. ///end
  1182. ui_nodes_ui.enabled = true;
  1183. }
  1184. }
  1185. ui_nodes_hide_menu = ui_nodes_ui.combo_selected_handle == null && !ui_nodes_show_menu_first && (ui_nodes_ui.changed || ui_nodes_ui.input_released || ui_nodes_ui.input_released_r || ui_nodes_ui.is_escape_down);
  1186. ui_nodes_show_menu_first = false;
  1187. ui_nodes_ui.ops.theme.FILL_BUTTON_BG = _FILL_BUTTON_BG;
  1188. ui_nodes_ui.ops.theme.ELEMENT_OFFSET = _ELEMENT_OFFSET;
  1189. ui_nodes_ui.ops.theme.ELEMENT_H = _ELEMENT_H;
  1190. ui_menu_end(ui_nodes_ui);
  1191. ui_end_region();
  1192. draw_end();
  1193. }
  1194. if (ui_nodes_hide_menu) {
  1195. ui_nodes_show_menu = false;
  1196. ui_nodes_show_menu_first = true;
  1197. }
  1198. }
  1199. function ui_nodes_contains_node_group_recursive(group: node_group_t, group_name: string): bool {
  1200. if (group.canvas.name == group_name) {
  1201. return true;
  1202. }
  1203. for (let i: i32 = 0; i < group.canvas.nodes.length; ++i) {
  1204. let n: ui_node_t = group.canvas.nodes[i];
  1205. if (n.type == "GROUP") {
  1206. let g: node_group_t = project_get_material_group_by_name(n.name);
  1207. if (g != null && ui_nodes_contains_node_group_recursive(g, group_name)) {
  1208. return true;
  1209. }
  1210. }
  1211. }
  1212. return false;
  1213. }
  1214. function ui_nodes_can_place_group(group_name: string): bool {
  1215. // Prevent Recursive node groups
  1216. // The group to place must not contain the current group or a group that contains the current group
  1217. if (ui_nodes_group_stack.length > 0) {
  1218. for (let i: i32 = 0; i < ui_nodes_group_stack.length; ++i) {
  1219. let g: node_group_t = ui_nodes_group_stack[i];
  1220. if (ui_nodes_contains_node_group_recursive(project_get_material_group_by_name(group_name), g.canvas.name)) return false;
  1221. }
  1222. }
  1223. // Group was deleted / renamed
  1224. let group_exists: bool = false;
  1225. for (let i: i32 = 0; i < project_material_groups.length; ++i) {
  1226. let group: node_group_t = project_material_groups[i];
  1227. if (group_name == group.canvas.name) {
  1228. group_exists = true;
  1229. }
  1230. }
  1231. if (!group_exists) {
  1232. return false;
  1233. }
  1234. return true;
  1235. }
  1236. function ui_nodes_push_undo(last_canvas: ui_node_canvas_t = null) {
  1237. if (last_canvas == null) {
  1238. last_canvas = ui_nodes_get_canvas(true);
  1239. }
  1240. let canvas_group: i32 = -1;
  1241. if (ui_nodes_group_stack.length > 0) {
  1242. canvas_group = array_index_of(project_material_groups, ui_nodes_group_stack[ui_nodes_group_stack.length - 1]);
  1243. }
  1244. ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
  1245. history_edit_nodes(last_canvas, ui_nodes_canvas_type, canvas_group);
  1246. }
  1247. function ui_nodes_accept_asset_drag(index: i32) {
  1248. ui_nodes_push_undo();
  1249. let g: node_group_t = ui_nodes_group_stack.length > 0 ? ui_nodes_group_stack[ui_nodes_group_stack.length - 1] : null;
  1250. ///if is_paint
  1251. let n: ui_node_t =
  1252. ui_nodes_canvas_type == canvas_type_t.MATERIAL ?
  1253. nodes_material_create_node("TEX_IMAGE", g) :
  1254. nodes_brush_create_node("TEX_IMAGE");
  1255. ///end
  1256. ///if is_lab
  1257. let n: ui_node_t = nodes_brush_create_node("image_texture_node");
  1258. ///end
  1259. n.buttons[0].default_value[0] = index;
  1260. ui_nodes_get_nodes().nodes_selected_id = [n.id];
  1261. ///if is_lab
  1262. parser_logic_parse(project_canvas);
  1263. ///end
  1264. }
  1265. function ui_nodes_accept_layer_drag(index: i32) {
  1266. ui_nodes_push_undo();
  1267. if (slot_layer_is_group(project_layers[index])) {
  1268. return;
  1269. }
  1270. let g: node_group_t = ui_nodes_group_stack.length > 0 ? ui_nodes_group_stack[ui_nodes_group_stack.length - 1] : null;
  1271. let n: ui_node_t = nodes_material_create_node(slot_layer_is_mask(context_raw.layer) ? "LAYER_MASK" : "LAYER", g);
  1272. n.buttons[0].default_value[0] = index;
  1273. ui_nodes_get_nodes().nodes_selected_id = [n.id];
  1274. }
  1275. function ui_nodes_accept_material_drag(index: i32) {
  1276. ui_nodes_push_undo();
  1277. let g: node_group_t = ui_nodes_group_stack.length > 0 ? ui_nodes_group_stack[ui_nodes_group_stack.length - 1] : null;
  1278. let n: ui_node_t = nodes_material_create_node("MATERIAL", g);
  1279. n.buttons[0].default_value[0] = index;
  1280. ui_nodes_get_nodes().nodes_selected_id = [n.id];
  1281. }
  1282. function ui_nodes_accept_swatch_drag(swatch: swatch_color_t) {
  1283. ///if is_paint
  1284. ui_nodes_push_undo();
  1285. let g: node_group_t = ui_nodes_group_stack.length > 0 ? ui_nodes_group_stack[ui_nodes_group_stack.length - 1] : null;
  1286. let n: ui_node_t = nodes_material_create_node("RGB", g);
  1287. n.outputs[0].default_value = f32_array_create_xyzw(
  1288. color_get_rb(swatch.base) / 255,
  1289. color_get_gb(swatch.base) / 255,
  1290. color_get_bb(swatch.base) / 255,
  1291. color_get_ab(swatch.base) / 255
  1292. );
  1293. ui_nodes_get_nodes().nodes_selected_id = [n.id];
  1294. ///end
  1295. }
  1296. function ui_nodes_make_node(n: ui_node_t, nodes: ui_nodes_t, canvas: ui_node_canvas_t): ui_node_t {
  1297. let node: ui_node_t = {};
  1298. node.id = ui_next_node_id(canvas.nodes);
  1299. node.name = n.name;
  1300. node.type = n.type;
  1301. node.x = ui_nodes_get_node_x();
  1302. node.y = ui_nodes_get_node_y();
  1303. node.color = n.color;
  1304. node.inputs = [];
  1305. node.outputs = [];
  1306. node.buttons = [];
  1307. node.width = 0;
  1308. let count: i32 = 0;
  1309. for (let i: i32 = 0; i < n.inputs.length; ++i) {
  1310. let soc: ui_node_socket_t = {};
  1311. soc.id = ui_get_socket_id(canvas.nodes) + count;
  1312. count++;
  1313. soc.node_id = node.id;
  1314. soc.name = n.inputs[i].name;
  1315. soc.type = n.inputs[i].type;
  1316. soc.color = n.inputs[i].color;
  1317. soc.default_value = f32_array_create_from_array(n.inputs[i].default_value);
  1318. soc.min = n.inputs[i].min;
  1319. soc.max = n.inputs[i].max;
  1320. soc.precision = n.inputs[i].precision;
  1321. soc.display = n.inputs[i].display;
  1322. array_push(node.inputs, soc);
  1323. }
  1324. for (let i: i32 = 0; i < n.outputs.length; ++i) {
  1325. let soc: ui_node_socket_t = {};
  1326. soc.id = ui_get_socket_id(canvas.nodes) + count;
  1327. count++;
  1328. soc.node_id = node.id;
  1329. soc.name = n.outputs[i].name;
  1330. soc.type = n.outputs[i].type;
  1331. soc.color = n.outputs[i].color;
  1332. soc.default_value = f32_array_create_from_array(n.outputs[i].default_value);
  1333. soc.min = n.outputs[i].min;
  1334. soc.max = n.outputs[i].max;
  1335. soc.precision = n.outputs[i].precision;
  1336. soc.display = n.outputs[i].display;
  1337. array_push(node.outputs, soc);
  1338. }
  1339. for (let i: i32 = 0; i < n.buttons.length; ++i) {
  1340. let but: ui_node_button_t = {};
  1341. but.name = n.buttons[i].name;
  1342. but.type = n.buttons[i].type;
  1343. but.output = n.buttons[i].output;
  1344. but.default_value = f32_array_create_from_array(n.buttons[i].default_value);
  1345. if (n.buttons[i].data != null) {
  1346. but.data = u8_array_create_from_array(n.buttons[i].data);
  1347. }
  1348. but.min = n.buttons[i].min;
  1349. but.max = n.buttons[i].max;
  1350. but.precision = n.buttons[i].precision;
  1351. but.height = n.buttons[i].height;
  1352. array_push(node.buttons, but);
  1353. }
  1354. return node;
  1355. }
  1356. function ui_nodes_make_group_node(group_canvas: ui_node_canvas_t, nodes: ui_nodes_t, canvas: ui_node_canvas_t): ui_node_t {
  1357. let category: ui_node_t[] = nodes_material_list[5];
  1358. let n: ui_node_t = category[0];
  1359. let node: ui_node_t = util_clone_canvas_node(n);
  1360. node.name = group_canvas.name;
  1361. node.id = ui_next_node_id(canvas.nodes);
  1362. node.x = ui_nodes_get_node_x();
  1363. node.y = ui_nodes_get_node_y();
  1364. let group_input: ui_node_t = null;
  1365. let group_output: ui_node_t = null;
  1366. for (let i: i32 = 0; i < project_material_groups.length; ++i) {
  1367. let g: node_group_t = project_material_groups[i];
  1368. let cname: string = g.canvas.name;
  1369. if (cname == node.name) {
  1370. for (let i: i32 = 0; i < g.canvas.nodes.length; ++i) {
  1371. let n: ui_node_t = g.canvas.nodes[i];
  1372. if (n.type == "GROUP_INPUT") {
  1373. group_input = n;
  1374. }
  1375. else if (n.type == "GROUP_OUTPUT") {
  1376. group_output = n;
  1377. }
  1378. }
  1379. break;
  1380. }
  1381. }
  1382. if (group_input != null && group_output != null) {
  1383. for (let i: i32 = 0; i < group_input.outputs.length; ++i) {
  1384. let soc: ui_node_socket_t = group_input.outputs[i];
  1385. array_push(node.inputs, nodes_material_create_socket(nodes, node, soc.name, soc.type, canvas, soc.min, soc.max, soc.default_value));
  1386. }
  1387. for (let i: i32 = 0; i < group_output.inputs.length; ++i) {
  1388. let soc: ui_node_socket_t = group_output.inputs[i];
  1389. array_push(node.outputs, nodes_material_create_socket(nodes, node, soc.name, soc.type, canvas, soc.min, soc.max, soc.default_value));
  1390. }
  1391. }
  1392. return node;
  1393. }
  1394. function ui_nodes_make_node_preview() {
  1395. let ui_nodes: ui_nodes_t = context_raw.material.nodes;
  1396. if (ui_nodes.nodes_selected_id.length == 0) {
  1397. return;
  1398. }
  1399. let nodes: ui_node_t[] = context_raw.material.canvas.nodes;
  1400. let node: ui_node_t = ui_get_node(nodes, ui_nodes.nodes_selected_id[0]);
  1401. context_raw.node_preview_name = node.name;
  1402. if (node.type == "LAYER" ||
  1403. node.type == "LAYER_MASK" ||
  1404. node.type == "MATERIAL" ||
  1405. node.type == "OUTPUT_MATERIAL_PBR") {
  1406. return;
  1407. }
  1408. if (array_index_of(nodes, node) == -1) {
  1409. return;
  1410. }
  1411. if (context_raw.node_preview == null) {
  1412. context_raw.node_preview = gpu_create_render_target(util_render_material_preview_size, util_render_material_preview_size);
  1413. }
  1414. context_raw.node_preview_dirty = false;
  1415. ui_nodes_hwnd.redraws = 2;
  1416. util_render_make_node_preview(context_raw.material.canvas, node, context_raw.node_preview);
  1417. }
  1418. function ui_nodes_has_group(c: ui_node_canvas_t): bool {
  1419. for (let i: i32 = 0; i < c.nodes.length; ++i) {
  1420. let n: ui_node_t = c.nodes[i];
  1421. if (n.type == "GROUP") {
  1422. return true;
  1423. }
  1424. }
  1425. return false;
  1426. }
  1427. function ui_nodes_traverse_group(mgroups: ui_node_canvas_t[], c: ui_node_canvas_t) {
  1428. for (let i: i32 = 0; i < c.nodes.length; ++i) {
  1429. let n: ui_node_t = c.nodes[i];
  1430. if (n.type == "GROUP") {
  1431. if (ui_nodes_get_group(mgroups, n.name) == null) {
  1432. let canvases: ui_node_canvas_t[] = [];
  1433. for (let i: i32 = 0; i < project_material_groups.length; ++i) {
  1434. let g: node_group_t = project_material_groups[i];
  1435. array_push(canvases, g.canvas);
  1436. }
  1437. let group: ui_node_canvas_t = ui_nodes_get_group(canvases, n.name);
  1438. array_push(mgroups, util_clone_canvas(group));
  1439. ui_nodes_traverse_group(mgroups, group);
  1440. }
  1441. }
  1442. }
  1443. }
  1444. function ui_nodes_get_group(canvases: ui_node_canvas_t[], name: string): ui_node_canvas_t {
  1445. for (let i: i32 = 0; i < canvases.length; ++i) {
  1446. let c: ui_node_canvas_t = canvases[i];
  1447. if (c.name == name) {
  1448. return c;
  1449. }
  1450. }
  1451. return null;
  1452. }