| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635 |
- ///if is_paint
- let ui_nodes_show: bool = false;
- ///end
- ///if is_lab
- let ui_nodes_show: bool = true;
- ///end
- let ui_nodes_wx: i32;
- let ui_nodes_wy: i32;
- let ui_nodes_ww: i32;
- let ui_nodes_wh: i32;
- let ui_nodes_ui: ui_t;
- let ui_nodes_canvas_type: canvas_type_t = canvas_type_t.MATERIAL;
- let ui_nodes_show_menu: bool = false;
- let ui_nodes_show_menu_first: bool = true;
- let ui_nodes_hide_menu: bool = false;
- let ui_nodes_menu_category: i32 = 0;
- let ui_nodes_popup_x: f32 = 0.0;
- let ui_nodes_popup_y: f32 = 0.0;
- let ui_nodes_node_search_x: i32;
- let ui_nodes_node_search_y: i32;
- let ui_nodes_uichanged_last: bool = false;
- let ui_nodes_recompile_mat: bool = false; // Mat preview
- let ui_nodes_recompile_mat_final: bool = false;
- let ui_nodes_node_search_spawn: ui_node_t = null;
- let ui_nodes_node_search_offset: i32 = 0;
- let ui_nodes_last_canvas: ui_node_canvas_t = null;
- let ui_nodes_last_node_selected_id: i32 = -1;
- let ui_nodes_release_link: bool = false;
- let ui_nodes_is_node_menu_op: bool = false;
- let ui_nodes_grid: gpu_texture_t = null;
- let ui_nodes_grid_redraw: bool = true;
- let ui_nodes_grid_cell_w: i32 = 200;
- let ui_nodes_grid_small_cell_w: i32 = 40;
- let ui_nodes_hwnd: ui_handle_t = ui_handle_create();
- let ui_nodes_group_stack: node_group_t[] = [];
- let ui_nodes_controls_down: bool = false;
- let ui_nodes_tabs: slot_material_t[] = null;
- let ui_nodes_htab: ui_handle_t = ui_handle_create();
- let _ui_nodes_on_link_drag_link_drag: ui_node_link_t;
- let _ui_nodes_on_link_drag_node: ui_node_t;
- let _ui_nodes_on_socket_released_socket: ui_node_socket_t;
- let _ui_nodes_on_socket_released_node: ui_node_t;
- let _ui_nodes_htype: ui_handle_t = ui_handle_create();
- let _ui_nodes_hname: ui_handle_t = ui_handle_create();
- let _ui_nodes_hmin: ui_handle_t = ui_handle_create();
- let _ui_nodes_hmax: ui_handle_t = ui_handle_create();
- let _ui_nodes_hval0: ui_handle_t = ui_handle_create();
- let _ui_nodes_hval1: ui_handle_t = ui_handle_create();
- let _ui_nodes_hval2: ui_handle_t = ui_handle_create();
- let _ui_nodes_hval3: ui_handle_t = ui_handle_create();
- let _ui_nodes_on_canvas_released_selected: ui_node_t;
- let _ui_nodes_node_search_first: bool;
- let _ui_nodes_node_search_done: ()=>void;
- function ui_viewnodes_init() {
- ui_nodes_on_link_drag = ui_viewnodes_on_link_drag;
- ui_nodes_on_socket_released = ui_viewnodes_on_socket_released;
- ui_nodes_on_canvas_released = ui_viewnodes_on_canvas_released;
- ui_nodes_on_canvas_control = ui_viewnodes_on_canvas_control;
- ui_nodes_grid_snap = config_raw.grid_snap;
- nodes_material_init();
- let scale: f32 = config_raw.window_scale;
- let ops: ui_options_t = {
- theme: base_theme,
- font: base_font,
- color_wheel: base_color_wheel,
- black_white_gradient: base_color_wheel_gradient,
- scale_factor: scale
- };
- ui_nodes_ui = ui_create(ops);
- ui_nodes_ui.scroll_enabled = false;
- }
- function ui_viewnodes_on_link_drag(link_drag_id: i32, is_new_link: bool) {
- if (!is_new_link) {
- return;
- }
- let ui_nodes: ui_nodes_t = ui_nodes_get_nodes();
- let links: ui_node_link_t[] = ui_nodes_get_canvas(true).links;
- let link_drag: ui_node_link_t = ui_get_link(links, link_drag_id);
- let nodes: ui_node_t[] = ui_nodes_get_canvas(true).nodes;
- let node: ui_node_t = ui_get_node(nodes, link_drag.from_id > -1 ? link_drag.from_id : link_drag.to_id);
- let link_x: i32 = ui_nodes_ui._window_x + UI_NODE_X(node);
- let link_y: i32 = ui_nodes_ui._window_y + UI_NODE_Y(node);
- if (link_drag.from_id > -1) {
- link_x += UI_NODE_W(node);
- link_y += UI_OUTPUT_Y(node.outputs.length, link_drag.from_socket);
- }
- else {
- link_y += ui_nodes_INPUT_Y(ui_nodes_get_canvas(true), node.inputs, link_drag.to_socket) +
- UI_OUTPUTS_H(node.outputs.length) + UI_BUTTONS_H(node);
- }
- if (math_abs(mouse_x - link_x) > 5 || math_abs(mouse_y - link_y) > 5) { // Link length
- _ui_nodes_on_link_drag_link_drag = link_drag;
- _ui_nodes_on_link_drag_node = node;
- ui_nodes_node_search(-1, -1, function () {
- let ui_nodes: ui_nodes_t = ui_nodes_get_nodes();
- let node_selected_id: i32 = _ui_nodes_on_link_drag_node.id;
- if (ui_nodes.nodes_selected_id.length > 0) {
- node_selected_id = ui_nodes.nodes_selected_id[0];
- }
- let link_drag: ui_node_link_t = _ui_nodes_on_link_drag_link_drag;
- let nodes: ui_node_t[] = ui_nodes_get_canvas(true).nodes;
- let n: ui_node_t = ui_get_node(nodes, node_selected_id);
- if (link_drag.to_id == -1 && n.inputs.length > 0) {
- link_drag.to_id = n.id;
- let from_type: string = _ui_nodes_on_link_drag_node.outputs[link_drag.from_socket].type;
- // Connect to the first socket
- link_drag.to_socket = 0;
- // Try to find the first type-matching socket and use it if present
- for (let i: i32 = 0; i < n.inputs.length; ++i) {
- let socket: ui_node_socket_t = n.inputs[i];
- if (socket.type == from_type) {
- link_drag.to_socket = array_index_of(n.inputs, socket);
- break;
- }
- }
- array_push(ui_nodes_get_canvas(true).links, link_drag);
- }
- else if (link_drag.from_id == -1 && n.outputs.length > 0) {
- link_drag.from_id = n.id;
- link_drag.from_socket = 0;
- array_push(ui_nodes_get_canvas(true).links, link_drag);
- }
- ///if is_lab
- parser_logic_parse(ui_nodes_get_canvas(true));
- context_raw.rdirty = 5;
- ///end
- });
- }
- // Selecting which node socket to preview
- else if (ui_nodes.nodes_selected_id.length > 0 && node.id == ui_nodes.nodes_selected_id[0]) {
- context_raw.node_preview_socket = link_drag.from_id > -1 ? link_drag.from_socket : 0;
- ///if is_paint
- context_raw.node_preview_dirty = true;
- ///end
- }
- }
- function ui_viewnodes_on_socket_released(socket_id: i32) {
- let nodes: ui_nodes_t = ui_nodes_get_nodes();
- let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
- let socket: ui_node_socket_t = ui_get_socket(canvas.nodes, socket_id);
- let node: ui_node_t = ui_get_node(canvas.nodes, socket.node_id);
- if (ui_nodes_ui.input_released_r) {
- if (node.type == "GROUP_INPUT" || node.type == "GROUP_OUTPUT") {
- _ui_nodes_on_socket_released_socket = socket;
- _ui_nodes_on_socket_released_node = node;
- sys_notify_on_next_frame(function () {
- ui_menu_draw(function (ui: ui_t) {
- let socket: ui_node_socket_t = _ui_nodes_on_socket_released_socket;
- let node: ui_node_t = _ui_nodes_on_socket_released_node;
- if (ui_menu_button(tr("Edit"))) {
- _ui_nodes_htype.position = socket.type == "RGBA" ? 0 : socket.type == "VECTOR" ? 1 : 2;
- _ui_nodes_hname.text = socket.name;
- _ui_nodes_hmin.value = socket.min;
- _ui_nodes_hmax.value = socket.max;
- if (socket.type == "RGBA" || socket.type == "VECTOR") {
- _ui_nodes_hval0.value = socket.default_value[0];
- _ui_nodes_hval1.value = socket.default_value[1];
- _ui_nodes_hval2.value = socket.default_value[2];
- if (socket.type == "RGBA") {
- _ui_nodes_hval3.value = socket.default_value[3];
- }
- }
- else {
- _ui_nodes_hval0.value = socket.default_value[0];
- }
- sys_notify_on_next_frame(function () {
- ui_end_input();
- ui_box_show_custom(function (ui: ui_t) {
- let socket: ui_node_socket_t = _ui_nodes_on_socket_released_socket;
- let node: ui_node_t = _ui_nodes_on_socket_released_node;
- if (ui_tab(ui_handle(__ID__), tr("Socket"))) {
- let type_combo: string[] = [tr("Color"), tr("Vector"), tr("Value")];
- let type: i32 = ui_combo(_ui_nodes_htype, type_combo, tr("Type"), true);
- if (_ui_nodes_htype.changed) {
- _ui_nodes_hname.text = type == 0 ? tr("Color") : type == 1 ? tr("Vector") : tr("Value");
- }
- let name: string = ui_text_input(_ui_nodes_hname, tr("Name"));
- let min: f32 = ui_float_input(_ui_nodes_hmin, tr("Min"));
- let max: f32 = ui_float_input(_ui_nodes_hmax, tr("Max"));
- let default_value: f32_array_t = null;
- if (type == 0) {
- ui_row4();
- ui_float_input(_ui_nodes_hval0, tr("R"));
- ui_float_input(_ui_nodes_hval1, tr("G"));
- ui_float_input(_ui_nodes_hval2, tr("B"));
- ui_float_input(_ui_nodes_hval3, tr("A"));
- default_value = f32_array_create_xyzw(
- _ui_nodes_hval0.value, _ui_nodes_hval1.value, _ui_nodes_hval2.value, _ui_nodes_hval3.value);
- }
- else if (type == 1) {
- ui_row3();
- _ui_nodes_hval0.value = ui_float_input(_ui_nodes_hval0, tr("X"));
- _ui_nodes_hval1.value = ui_float_input(_ui_nodes_hval1, tr("Y"));
- _ui_nodes_hval2.value = ui_float_input(_ui_nodes_hval2, tr("Z"));
- default_value = f32_array_create_xyz(
- _ui_nodes_hval0.value, _ui_nodes_hval1.value, _ui_nodes_hval2.value);
- }
- else {
- let f: f32 = ui_float_input(_ui_nodes_hval0, tr("default_value"));
- default_value = f32_array_create_x(f);
- }
- if (ui_button(tr("OK"))) { // || ui.isReturnDown
- socket.name = name;
- socket.type = type == 0 ? "RGBA" : type == 1 ? "VECTOR" : "VALUE";
- socket.color = nodes_material_get_socket_color(socket.type);
- socket.min = min;
- socket.max = max;
- socket.default_value = default_value;
- ui_box_hide();
- nodes_material_sync_sockets(node);
- ui_nodes_hwnd.redraws = 2;
- }
- }
- }, 400, 250);
- });
- }
- if (ui_menu_button(tr("Delete"))) {
- let i: i32 = 0;
- let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
- // Remove links connected to the socket
- while (i < canvas.links.length) {
- let l: ui_node_link_t = canvas.links[i];
- if ((l.from_id == node.id && l.from_socket == array_index_of(node.outputs, socket)) ||
- (l.to_id == node.id && l.to_socket == array_index_of(node.inputs, socket))) {
- array_splice(canvas.links, i, 1);
- }
- else {
- i++;
- }
- }
- // Remove socket
- array_remove(node.inputs, socket);
- array_remove(node.outputs, socket);
- nodes_material_sync_sockets(node);
- }
- });
- });
- }
- else ui_viewnodes_on_canvas_released();
- }
- // Selecting which node socket to preview
- else if (nodes.nodes_selected_id.length > 0 && node.id == nodes.nodes_selected_id[0]) {
- let i: i32 = array_index_of(node.outputs, socket);
- if (i > -1) {
- context_raw.node_preview_socket = i;
- ///if is_paint
- context_raw.node_preview_dirty = true;
- ///end
- }
- }
- }
- function ui_viewnodes_on_canvas_released() {
- if (ui_nodes_ui.input_released_r &&
- context_in_nodes() &&
- math_abs(ui_nodes_ui.input_x - ui_nodes_ui.input_started_x) < 2 &&
- math_abs(ui_nodes_ui.input_y - ui_nodes_ui.input_started_y) < 2) {
- // Node selection
- let nodes: ui_nodes_t = ui_nodes_get_nodes();
- let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
- let selected: ui_node_t = null;
- for (let i: i32 = 0; i < canvas.nodes.length; ++i) {
- let node: ui_node_t = canvas.nodes[i];
- 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))) {
- selected = node;
- break;
- }
- }
- if (selected == null) {
- nodes.nodes_selected_id = [];
- }
- else if (array_index_of(nodes.nodes_selected_id, selected.id) == -1) {
- nodes.nodes_selected_id = [selected.id];
- }
- // Node context menu
- if (!ui_nodes_socket_released) {
- _ui_nodes_on_canvas_released_selected = selected;
- ui_menu_draw(function (ui_menu: ui_t) {
- let selected: ui_node_t = _ui_nodes_on_canvas_released_selected;
- ui_menu._y += 1;
- let is_protected: bool = selected == null ||
- ///if is_paint
- selected.type == "OUTPUT_MATERIAL_PBR" ||
- ///end
- selected.type == "GROUP_INPUT" ||
- selected.type == "GROUP_OUTPUT" ||
- selected.type == "brush_output_node";
- ui_menu.enabled = !is_protected;
- if (ui_menu_button(tr("Cut"), "ctrl+x")) {
- sys_notify_on_next_frame(function () {
- ui_nodes_hwnd.redraws = 2;
- ui_is_copy = true;
- ui_is_cut = true;
- ui_nodes_is_node_menu_op = true;
- });
- }
- if (ui_menu_button(tr("Copy"), "ctrl+c")) {
- sys_notify_on_next_frame(function () {
- ui_is_copy = true;
- ui_nodes_is_node_menu_op = true;
- });
- }
- ui_menu.enabled = ui_clipboard != "";
- if (ui_menu_button(tr("Paste"), "ctrl+v")) {
- sys_notify_on_next_frame(function () {
- ui_nodes_hwnd.redraws = 2;
- ui_is_paste = true;
- ui_nodes_is_node_menu_op = true;
- });
- }
- ui_menu.enabled = !is_protected;
- if (ui_menu_button(tr("Delete"), "delete")) {
- sys_notify_on_next_frame(function () {
- ui_nodes_hwnd.redraws = 2;
- ui_nodes_ui.is_delete_down = true;
- ui_nodes_is_node_menu_op = true;
- });
- }
- if (ui_menu_button(tr("Duplicate"))) {
- sys_notify_on_next_frame(function () {
- ui_nodes_hwnd.redraws = 2;
- ui_is_copy = true;
- ui_is_paste = true;
- ui_nodes_is_node_menu_op = true;
- });
- }
- if (selected != null && selected.type == "RGB") {
- if (ui_menu_button(tr("Add Swatch"))) {
- let color: f32_array_t = selected.outputs[0].default_value;
- let new_swatch: swatch_color_t = make_swatch(color_from_floats(color[0], color[1], color[2], color[3]));
- context_set_swatch(new_swatch);
- array_push(project_raw.swatches, new_swatch);
- ui_base_hwnds[tab_area_t.STATUS].redraws = 1;
- }
- }
- if (ui_nodes_canvas_type == canvas_type_t.MATERIAL) {
- ui_menu_separator(ui_menu);
- if (ui_menu_button(tr("2D View"))) {
- ui_base_show_2d_view(view_2d_type_t.NODE);
- }
- }
- ui_menu.enabled = true;
- });
- }
- }
- if (ui_nodes_ui.input_released) {
- let nodes: ui_nodes_t = ui_nodes_get_nodes();
- let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
- for (let i: i32 = 0; i < canvas.nodes.length; ++i) {
- let node: ui_node_t = canvas.nodes[i];
- 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))) {
- if (nodes.nodes_selected_id.length > 0 && node.id == nodes.nodes_selected_id[0]) {
- ui_view2d_hwnd.redraws = 2;
- if (sys_time() - context_raw.select_time < 0.25) ui_base_show_2d_view(view_2d_type_t.NODE);
- context_raw.select_time = sys_time();
- }
- break;
- }
- }
- }
- }
- function ui_viewnodes_on_canvas_control(): ui_canvas_control_t {
- let control: ui_canvas_control_t = ui_nodes_get_canvas_control(ui_nodes_ui, ui_nodes_controls_down);
- ui_nodes_controls_down = control.controls_down;
- return control;
- }
- function ui_nodes_get_canvas_control(ui: ui_t, controls_down: bool): ui_canvas_control_t {
- if (config_raw.wrap_mouse && controls_down) {
- if (ui.input_x < ui._window_x) {
- ui.input_x = ui._window_x + ui._window_w;
- iron_mouse_set_position(math_floor(ui.input_x), math_floor(ui.input_y));
- }
- else if (ui.input_x > ui._window_x + ui._window_w) {
- ui.input_x = ui._window_x;
- iron_mouse_set_position(math_floor(ui.input_x), math_floor(ui.input_y));
- }
- else if (ui.input_y < ui._window_y) {
- ui.input_y = ui._window_y + ui._window_h;
- iron_mouse_set_position(math_floor(ui.input_x), math_floor(ui.input_y));
- }
- else if (ui.input_y > ui._window_y + ui._window_h) {
- ui.input_y = ui._window_y;
- iron_mouse_set_position(math_floor(ui.input_x), math_floor(ui.input_y));
- }
- }
- if (operator_shortcut(map_get(config_keymap, "action_pan"), shortcut_type_t.STARTED) ||
- operator_shortcut(map_get(config_keymap, "action_zoom"), shortcut_type_t.STARTED) ||
- ui.input_started_r ||
- ui.input_wheel_delta != 0.0) {
- controls_down = true;
- }
- else if (!operator_shortcut(map_get(config_keymap, "action_pan"), shortcut_type_t.DOWN) &&
- !operator_shortcut(map_get(config_keymap, "action_zoom"), shortcut_type_t.DOWN) &&
- !ui.input_down_r &&
- ui.input_wheel_delta == 0.0) {
- controls_down = false;
- }
- if (!controls_down) {
- let cc: ui_canvas_control_t = {
- pan_x: 0,
- pan_y: 0,
- zoom: 0,
- controls_down: controls_down
- };
- return cc;
- }
- let pan: bool = ui.input_down_r || operator_shortcut(map_get(config_keymap, "action_pan"), shortcut_type_t.DOWN);
- 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;
- let control: ui_canvas_control_t = {
- pan_x: pan ? ui.input_dx : 0.0,
- pan_y: pan ? ui.input_dy : 0.0,
- zoom: ui.input_wheel_delta != 0.0 ? -ui.input_wheel_delta / 10 : zoom_delta,
- controls_down: controls_down
- };
- if (base_is_combo_selected()) {
- control.zoom = 0.0;
- }
- if (control.zoom != 0.0) {
- ui_nodes_grid_redraw = true;
- }
- return control;
- }
- function ui_nodes_get_zoom_delta(ui: ui_t): f32 {
- return config_raw.zoom_direction == zoom_direction_t.VERTICAL ? -ui.input_dy :
- config_raw.zoom_direction == zoom_direction_t.VERTICAL_INVERTED ? -ui.input_dy :
- config_raw.zoom_direction == zoom_direction_t.HORIZONTAL ? ui.input_dx :
- config_raw.zoom_direction == zoom_direction_t.HORIZONTAL_INVERTED ? ui.input_dx :
- -(ui.input_dy - ui.input_dx);
- }
- function ui_nodes_is_tab_selected(): bool {
- return ui_nodes_htab.position > 0 &&
- ui_nodes_htab.position % 2 == 1 && // [tab0, tab1, x, tab2, x, +]
- ui_nodes_tabs.length >= ui_nodes_htab.position / 2;
- }
- function ui_nodes_tab_index(): i32 {
- return (int)(ui_nodes_htab.position / 2);
- }
- function ui_nodes_get_canvas(groups: bool = false): ui_node_canvas_t {
- ///if is_paint
- if (ui_nodes_canvas_type == canvas_type_t.MATERIAL) {
- if (groups && ui_nodes_group_stack.length > 0) {
- return ui_nodes_group_stack[ui_nodes_group_stack.length - 1].canvas;
- }
- else if (ui_nodes_is_tab_selected()) {
- return ui_nodes_tabs[ui_nodes_tab_index()].canvas;
- }
- else {
- return ui_nodes_get_canvas_material();
- }
- }
- else {
- return context_raw.brush.canvas;
- }
- ///end
- ///if is_lab
- return project_canvas;
- ///end
- }
- function ui_nodes_get_canvas_material(): ui_node_canvas_t {
- return context_raw.material.canvas;
- }
- function ui_nodes_get_nodes(): ui_nodes_t {
- ///if is_paint
- if (ui_nodes_canvas_type == canvas_type_t.MATERIAL) {
- if (ui_nodes_group_stack.length > 0) {
- return ui_nodes_group_stack[ui_nodes_group_stack.length - 1].nodes;
- }
- else if (ui_nodes_is_tab_selected()) {
- return ui_nodes_tabs[ui_nodes_tab_index()].nodes;
- }
- else {
- return context_raw.material.nodes;
- }
- }
- else {
- return context_raw.brush.nodes;
- }
- ///end
- ///if is_lab
- if (ui_nodes_group_stack.length > 0) {
- return ui_nodes_group_stack[ui_nodes_group_stack.length - 1].nodes;
- }
- else {
- return project_nodes;
- }
- ///end
- }
- function ui_nodes_update() {
- if (!ui_nodes_show || !base_ui_enabled) {
- return;
- }
- ///if is_paint
- ui_nodes_wx = math_floor(sys_w()) + ui_toolbar_w(true);
- ///end
- ///if is_lab
- ui_nodes_wx = math_floor(sys_w());
- ///end
- ui_nodes_wy = ui_header_h * 2;
- if (ui_view2d_show) {
- ui_nodes_wy += sys_h() - config_raw.layout[layout_size_t.NODES_H];
- }
- let ww: i32 = config_raw.layout[layout_size_t.NODES_W];
- if (!ui_base_show) {
- ///if is_paint
- ww += config_raw.layout[layout_size_t.SIDEBAR_W] + ui_toolbar_w(true);
- ui_nodes_wx -= ui_toolbar_w(true);
- ///end
- ui_nodes_wy = 0;
- }
- let mx: i32 = mouse_x;
- let my: i32 = mouse_y;
- if (mx < ui_nodes_wx || mx > ui_nodes_wx + ww || my < ui_nodes_wy) {
- return;
- }
- if (ui_nodes_ui.is_typing || !ui_nodes_ui.input_enabled) {
- return;
- }
- let nodes: ui_nodes_t = ui_nodes_get_nodes();
- if (nodes.nodes_selected_id.length > 0 && ui_nodes_ui.is_key_pressed) {
- if (ui_nodes_ui.key_code == key_code_t.LEFT) {
- for (let i: i32 = 0; i < nodes.nodes_selected_id.length; ++i) {
- let n: i32 = nodes.nodes_selected_id[i];
- let _nodes: ui_node_t[] = ui_nodes_get_canvas(true).nodes;
- ui_get_node(_nodes, n).x -= 1;
- }
- }
- else if (ui_nodes_ui.key_code == key_code_t.RIGHT) {
- for (let i: i32 = 0; i < nodes.nodes_selected_id.length; ++i) {
- let n: i32 = nodes.nodes_selected_id[i];
- let _nodes: ui_node_t[] = ui_nodes_get_canvas(true).nodes;
- ui_get_node(_nodes, n).x += 1;
- }
- }
- if (ui_nodes_ui.key_code == key_code_t.UP) {
- for (let i: i32 = 0; i < nodes.nodes_selected_id.length; ++i) {
- let n: i32 = nodes.nodes_selected_id[i];
- let _nodes: ui_node_t[] = ui_nodes_get_canvas(true).nodes;
- ui_get_node(_nodes, n).y -= 1;
- }
- }
- else if (ui_nodes_ui.key_code == key_code_t.DOWN) {
- for (let i: i32 = 0; i < nodes.nodes_selected_id.length; ++i) {
- let n: i32 = nodes.nodes_selected_id[i];
- let _nodes: ui_node_t[] = ui_nodes_get_canvas(true).nodes;
- ui_get_node(_nodes, n).y += 1;
- }
- }
- }
- // Node search popup
- if (operator_shortcut(map_get(config_keymap, "node_search"))) {
- ui_nodes_node_search();
- }
- if (ui_nodes_node_search_spawn != null) {
- ui_nodes_ui.input_x = mouse_x; // Fix inputDX after popup removal
- ui_nodes_ui.input_y = mouse_y;
- ui_nodes_node_search_spawn = null;
- }
- if (operator_shortcut(map_get(config_keymap, "view_reset"))) {
- nodes.pan_x = 0.0;
- nodes.pan_y = 0.0;
- nodes.zoom = 1.0;
- }
- }
- function ui_nodes_canvas_changed() {
- ui_nodes_recompile_mat = true;
- ui_nodes_recompile_mat_final = true;
- }
- function ui_nodes_node_search(x: i32 = -1, y: i32 = -1, done: ()=>void = null) {
- _ui_nodes_node_search_first = true;
- _ui_nodes_node_search_done = done;
- ui_menu_draw(function (ui: ui_t) {
- ui_menu_h = ui_ELEMENT_H(ui) * 8;
- let search_handle: ui_handle_t = ui_handle(__ID__);
- let search: string = to_lower_case(ui_text_input(search_handle, "", ui_align_t.LEFT, true, true));
- ui.changed = false;
- if (_ui_nodes_node_search_first) {
- _ui_nodes_node_search_first = false;
- search_handle.text = "";
- ui_start_text_edit(search_handle); // Focus search bar
- }
- if (search_handle.changed) {
- ui_nodes_node_search_offset = 0;
- }
- if (ui.is_key_pressed) { // Move selection
- if (ui.key_code == key_code_t.DOWN && ui_nodes_node_search_offset < 6) {
- ui_nodes_node_search_offset++;
- }
- if (ui.key_code == key_code_t.UP && ui_nodes_node_search_offset > 0) {
- ui_nodes_node_search_offset--;
- }
- }
- let enter: bool = keyboard_down("enter");
- let count: i32 = 0;
- let BUTTON_COL: i32 = ui.ops.theme.BUTTON_COL;
- let FILL_BUTTON_BG: bool = ui.ops.theme.FILL_BUTTON_BG;
- ui.ops.theme.FILL_BUTTON_BG = true;
- ///if is_paint
- let node_list: node_list_t[] = ui_nodes_canvas_type == canvas_type_t.MATERIAL ? nodes_material_list : nodes_brush_list;
- ///end
- ///if is_lab
- let node_list: node_list_t[] = nodes_brush_list;
- ///end
- for (let i: i32 = 0; i < node_list.length; ++i) {
- let list: ui_node_t[] = node_list[i];
- for (let i: i32 = 0; i < list.length; ++i) {
- let n: ui_node_t = list[i];
- if (string_index_of(to_lower_case(tr(n.name)), search) >= 0) {
- ui.ops.theme.BUTTON_COL = count == ui_nodes_node_search_offset ? ui.ops.theme.HIGHLIGHT_COL : ui.ops.theme.SEPARATOR_COL;
- if (ui_button(tr(n.name), ui_align_t.LEFT) || (enter && count == ui_nodes_node_search_offset)) {
- ui_nodes_push_undo();
- let nodes: ui_nodes_t = ui_nodes_get_nodes();
- let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
- ui_nodes_node_search_spawn = ui_nodes_make_node(n, nodes, canvas); // Spawn selected node
- array_push(canvas.nodes, ui_nodes_node_search_spawn);
- nodes.nodes_selected_id = [ui_nodes_node_search_spawn.id];
- nodes.nodes_drag = true;
- ///if is_lab
- parser_logic_parse(canvas);
- ///end
- ui_nodes_hwnd.redraws = 2;
- if (enter) {
- ui.changed = true;
- count = 6; // Trigger break
- }
- if (_ui_nodes_node_search_done != null) {
- _ui_nodes_node_search_done();
- }
- }
- if (++count > 6) {
- break;
- }
- }
- }
- if (count > 6) {
- break;
- }
- }
- if (enter && count == 0) { // Hide popup on enter when node is not found
- ui.changed = true;
- search_handle.text = "";
- }
- ui.ops.theme.BUTTON_COL = BUTTON_COL;
- ui.ops.theme.FILL_BUTTON_BG = FILL_BUTTON_BG;
- }, x, y);
- }
- function ui_nodes_get_node_x(): i32 {
- return math_floor((mouse_x - ui_nodes_wx - UI_NODES_PAN_X()) / UI_NODES_SCALE());
- }
- function ui_nodes_get_node_y(): i32 {
- return math_floor((mouse_y - ui_nodes_wy - UI_NODES_PAN_Y()) / UI_NODES_SCALE());
- }
- function ui_nodes_draw_grid(zoom: f32): gpu_texture_t {
- let ww: i32 = config_raw.layout[layout_size_t.NODES_W];
- ///if is_paint
- if (!ui_base_show) {
- ww += config_raw.layout[layout_size_t.SIDEBAR_W] + ui_toolbar_w(true);
- }
- ///end
- let wh: i32 = sys_h();
- let step: f32 = ui_nodes_grid_cell_w * zoom;
- let mult: i32 = 5 * ui_SCALE(ui_nodes_ui);
- let w: i32 = math_floor(ww + step * mult);
- let h: i32 = math_floor(wh + step * mult);
- if (w < 1) {
- w = 1;
- }
- if (h < 1) {
- h = 1;
- }
- let grid: gpu_texture_t = gpu_create_render_target(w, h);
- draw_begin(grid, true, ui_nodes_ui.ops.theme.SEPARATOR_COL);
- let sep_col: i32 = ui_nodes_ui.ops.theme.SEPARATOR_COL;
- let line_primary: i32 = sep_col - 0x00050505;
- if (line_primary < 0xff000000) {
- line_primary = sep_col + 0x00050505;
- }
- let line_secondary: i32 = sep_col - 0x00090909;
- if (line_secondary < 0xff000000) {
- line_secondary = sep_col + 0x00090909;
- }
- draw_set_color(line_primary);
- step = ui_nodes_grid_small_cell_w * zoom;
- for (let i: i32 = 0; i < math_floor(h / step) + 1; ++i) {
- draw_line(0, i * step, w, i * step);
- }
- for (let i: i32 = 0; i < math_floor(w / step) + 1; ++i) {
- draw_line(i * step, 0, i * step, h);
- }
- draw_set_color(line_secondary);
- step = ui_nodes_grid_cell_w * zoom;
- for (let i: i32 = 0; i < math_floor(h / step) + 1; ++i) {
- draw_line(0, i * step, w, i * step);
- }
- for (let i: i32 = 0; i < math_floor(w / step) + 1; ++i) {
- draw_line(i * step, 0, i * step, h);
- }
- draw_end();
- return grid;
- }
- let _ui_nodes_render_tmp: (col: i32)=>void;
- function ui_nodes_render() {
- if (ui_nodes_recompile_mat) {
- ///if is_paint
- if (ui_nodes_canvas_type == canvas_type_t.BRUSH) {
- make_material_parse_brush();
- util_render_make_brush_preview();
- ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
- }
- else {
- let _material: slot_material_t = context_raw.material;
- if (ui_nodes_is_tab_selected()) {
- context_raw.material = ui_nodes_tabs[ui_nodes_tab_index()];
- }
- layers_is_fill_material() ?
- layers_update_fill_layers() :
- util_render_make_material_preview();
- context_raw.material = _material;
- if (ui_view2d_show && ui_view2d_type == view_2d_type_t.NODE) {
- ui_view2d_hwnd.redraws = 2;
- }
- }
- ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
- if (context_raw.split_view) {
- context_raw.ddirty = 2;
- }
- ///end
- ///if is_lab
- parser_logic_parse(project_canvas);
- ///end
- ui_nodes_recompile_mat = false;
- }
- else if (ui_nodes_recompile_mat_final) {
- ///if is_paint
- make_material_parse_paint_material();
- if (ui_nodes_canvas_type == canvas_type_t.MATERIAL && layers_is_fill_material()) {
- layers_update_fill_layers();
- util_render_make_material_preview();
- }
- let decal: bool = context_is_decal();
- if (decal) {
- util_render_make_decal_preview();
- }
- ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
- context_raw.node_preview_dirty = true;
- ///end
- ui_nodes_recompile_mat_final = false;
- }
- let nodes: ui_nodes_t = ui_nodes_get_nodes();
- if (nodes.nodes_selected_id.length > 0 && nodes.nodes_selected_id[0] != ui_nodes_last_node_selected_id) {
- ui_nodes_last_node_selected_id = nodes.nodes_selected_id[0];
- ///if is_paint
- context_raw.node_preview_dirty = true;
- ///end
- ///if is_lab
- context_raw.ddirty = 2; // Show selected node texture in viewport
- ui_header_handle.redraws = 2;
- ///end
- context_raw.node_preview_socket = 0;
- }
- // Remove dragged link when mouse is released out of the node viewport
- let c: ui_node_canvas_t = ui_nodes_get_canvas(true);
- if (ui_nodes_release_link && nodes.link_drag_id != -1) {
- array_remove(c.links, ui_get_link(c.links, nodes.link_drag_id));
- nodes.link_drag_id = -1;
- }
- ui_nodes_release_link = ui_nodes_ui.input_released;
- if (!ui_nodes_show || iron_window_width() == 0 || iron_window_height() == 0) {
- return;
- }
- ui_nodes_ui.input_enabled = base_ui_enabled;
- if (ui_nodes_grid_redraw) {
- if (ui_nodes_grid != null) {
- iron_delete_texture(ui_nodes_grid);
- }
- let ui_nodes: ui_nodes_t = ui_nodes_get_nodes();
- ui_nodes_grid = ui_nodes_draw_grid(ui_nodes.zoom);
- ui_nodes_grid_redraw = false;
- }
- ///if is_paint
- if (config_raw.node_preview && context_raw.node_preview_dirty) {
- ui_nodes_make_node_preview();
- }
- ///end
- // Start with UI
- ui_begin(ui_nodes_ui);
- // Make window
- ui_nodes_ww = config_raw.layout[layout_size_t.NODES_W];
- ///if is_paint
- ui_nodes_wx = math_floor(sys_w()) + ui_toolbar_w(true);
- ///end
- ///if is_lab
- ui_nodes_wx = math_floor(sys_w());
- ///end
- ui_nodes_wy = 0;
- ///if is_paint
- if (!ui_base_show) {
- ui_nodes_ww += config_raw.layout[layout_size_t.SIDEBAR_W] + ui_toolbar_w(true);
- ui_nodes_wx -= ui_toolbar_w(true);
- }
- ///end
- let ew: i32 = math_floor(ui_ELEMENT_W(ui_nodes_ui) * 0.7);
- ui_nodes_wh = sys_h();
- if (config_raw.layout[layout_size_t.HEADER] == 1) {
- ui_nodes_wh += ui_header_h * 2;
- }
- if (ui_view2d_show) {
- ui_nodes_wh = config_raw.layout[layout_size_t.NODES_H];
- ui_nodes_wy = sys_h() - config_raw.layout[layout_size_t.NODES_H] + ui_header_h;
- if (config_raw.layout[layout_size_t.HEADER] == 1) {
- ui_nodes_wy += ui_header_h;
- }
- if (!ui_base_show) {
- ui_nodes_wy -= ui_header_h * 2;
- }
- }
- if (ui_window(ui_nodes_hwnd, ui_nodes_wx, ui_nodes_wy, ui_nodes_ww, ui_nodes_wh)) {
- ui_tab(ui_nodes_htab, tr("Nodes"));
- // Additional tabs
- if (ui_nodes_canvas_type == canvas_type_t.MATERIAL) {
- if (ui_nodes_tabs == null) {
- ui_nodes_tabs = [];
- }
- for (let i: i32 = 0; i < ui_nodes_tabs.length; ++i) {
- ui_tab(ui_nodes_htab, ui_nodes_tabs[i].canvas.name);
- if (ui_tab(ui_nodes_htab, tr("x"))) {
- array_splice(ui_nodes_tabs, i, 1);
- ui_nodes_htab.position = 0;
- }
- }
- ///if is_paint
- if (ui_tab(ui_nodes_htab, tr("+"))) {
- array_push(ui_nodes_tabs, context_raw.material);
- }
- ///end
- }
- // Grid
- draw_set_color(0xffffffff);
- let step: f32 = ui_nodes_grid_cell_w * nodes.zoom;
- let x: f32 = math_fmod(UI_NODES_PAN_X(), step) - step;
- let y: f32 = math_fmod(UI_NODES_PAN_Y(), step) - step;
- draw_image(ui_nodes_grid, x, y);
- // Undo
- if (ui_nodes_ui.input_started || ui_nodes_ui.is_key_pressed) {
- ui_nodes_last_canvas = util_clone_canvas(ui_nodes_get_canvas(true));
- }
- // Nodes
- let _input_enabled: bool = ui_nodes_ui.input_enabled;
- ui_nodes_ui.input_enabled = _input_enabled && !ui_nodes_show_menu;
- ///if is_paint
- ui_nodes_ui.window_border_right = config_raw.layout[layout_size_t.SIDEBAR_W];
- ///end
- ui_nodes_ui.window_border_top = ui_header_h * 2;
- ui_nodes_ui.window_border_bottom = config_raw.layout[layout_size_t.STATUS_H];
- ui_node_canvas(nodes, c);
- ui_nodes_ui.input_enabled = _input_enabled;
- if (nodes.color_picker_callback != null) {
- context_raw.color_picker_previous_tool = context_raw.tool;
- context_select_tool(workspace_tool_t.PICKER);
- _ui_nodes_render_tmp = nodes.color_picker_callback;
- context_raw.color_picker_callback = function (color: swatch_color_t) {
- _ui_nodes_render_tmp(color.base);
- ui_nodes_hwnd.redraws = 2;
- ///if is_paint
- let material_live: bool = config_raw.material_live;
- ///end
- ///if is_lab
- let material_live: bool = true;
- ///end
- if (material_live) {
- ui_nodes_canvas_changed();
- }
- };
- nodes.color_picker_callback = null;
- }
- // Remove nodes with unknown id for this canvas type
- if (ui_is_paste) {
- ///if is_paint
- let node_list: node_list_t[] = ui_nodes_canvas_type == canvas_type_t.MATERIAL ? nodes_material_list : nodes_brush_list;
- ///end
- ///if is_lab
- let node_list: node_list_t[] = nodes_brush_list;
- ///end
- let i: i32 = 0;
- while (i++ < c.nodes.length) {
- let canvas_node: ui_node_t = c.nodes[i - 1];
- if (array_index_of(ui_nodes_exclude_remove, canvas_node.type) >= 0) {
- continue;
- }
- let found: bool = false;
- for (let i: i32 = 0; i < node_list.length; ++i) {
- let list: ui_node_t[] = node_list[i];
- for (let i: i32 = 0; i < list.length; ++i) {
- let list_node: ui_node_t = list[i];
- if (canvas_node.type == list_node.type) {
- found = true;
- break;
- }
- }
- if (found) {
- break;
- }
- }
- if (canvas_node.type == "GROUP" && !ui_nodes_can_place_group(canvas_node.name)) {
- found = false;
- }
- if (!found) {
- ui_remove_node(canvas_node, c);
- array_remove(nodes.nodes_selected_id, canvas_node.id);
- i--;
- }
- }
- }
- if (ui_nodes_is_node_menu_op) {
- ui_is_copy = false;
- ui_is_cut = false;
- ui_is_paste = false;
- ui_nodes_ui.is_delete_down = false;
- }
- // Recompile material on change
- if (ui_nodes_ui.changed) {
- ///if is_paint
- 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
- ///end
- ///if is_lab
- ui_nodes_recompile_mat = (ui_nodes_ui.input_dx != 0 || ui_nodes_ui.input_dy != 0 || !ui_nodes_uichanged_last); // Instant preview
- ///end
- }
- else if (ui_nodes_uichanged_last) {
- ui_nodes_canvas_changed();
- ui_nodes_push_undo(ui_nodes_last_canvas);
- }
- ui_nodes_uichanged_last = ui_nodes_ui.changed;
- // Node previews
- if (config_raw.node_preview && nodes.nodes_selected_id.length > 0) {
- let img: gpu_texture_t = null;
- let sel: ui_node_t = ui_get_node(c.nodes, nodes.nodes_selected_id[0]);
- ///if is_paint
- let single_channel: bool = sel.type == "LAYER_MASK";
- if (sel.type == "LAYER" || sel.type == "LAYER_MASK") {
- let id: i32 = sel.buttons[0].default_value[0];
- if (id < project_layers.length) {
- ///if is_paint
- img = project_layers[id].texpaint_preview;
- ///end
- }
- }
- else if (sel.type == "MATERIAL") {
- let id: i32 = sel.buttons[0].default_value[0];
- if (id < project_materials.length) {
- img = project_materials[id].image;
- }
- }
- else if (sel.type == "OUTPUT_MATERIAL_PBR") {
- img = context_raw.material.image;
- }
- else if (sel.type == "brush_output_node") {
- img = context_raw.brush.image;
- }
- else if (ui_nodes_canvas_type == canvas_type_t.MATERIAL) {
- img = context_raw.node_preview;
- }
- ///else
- let brush_node: logic_node_ext_t = parser_logic_get_logic_node(sel);
- if (brush_node != null) {
- img = logic_node_get_cached_image(brush_node.base);
- }
- ///end
- if (img != null) {
- let tw: f32 = 128 * ui_SCALE(ui_nodes_ui);
- let th: f32 = tw * (img.height / img.width);
- let tx: f32 = ui_nodes_ww - tw - 8 * ui_SCALE(ui_nodes_ui);
- let ty: f32 = ui_nodes_wh - th - 8 * ui_SCALE(ui_nodes_ui);
- let invert_y: bool = false;
- ///if is_paint
- if (single_channel) {
- draw_set_pipeline(ui_view2d_pipe);
- gpu_set_int(ui_view2d_channel_loc, 1);
- }
- ///end
- draw_set_color(0xffffffff);
- invert_y ?
- draw_scaled_image(img, tx, ty + th, tw, -th) :
- draw_scaled_image(img, tx, ty, tw, th);
- ///if is_paint
- if (single_channel) {
- draw_set_pipeline(null);
- }
- ///end
- }
- }
- // Menu
- draw_set_color(ui_nodes_ui.ops.theme.SEPARATOR_COL);
- 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);
- draw_set_color(0xffffffff);
- let start_y: i32 = ui_ELEMENT_H(ui_nodes_ui) + ui_ELEMENT_OFFSET(ui_nodes_ui);
- ui_nodes_ui._x = 0;
- ui_nodes_ui._y = 2 + start_y;
- ui_nodes_ui._w = ew;
- ///if is_paint
- // Editable canvas name
- let h: ui_handle_t = ui_handle(__ID__);
- h.text = c.name;
- 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)));
- let new_name: string = ui_text_input(h, "");
- ui_nodes_ui._x += ui_nodes_ui._w + 3;
- ui_nodes_ui._y = 2 + start_y;
- ui_nodes_ui._w = ew;
- if (h.changed) { // Check whether renaming is possible and update group links
- if (ui_nodes_group_stack.length > 0) {
- let can_rename: bool = true;
- for (let i: i32 = 0; i < project_material_groups.length; ++i) {
- let m: node_group_t = project_material_groups[i];
- if (m.canvas.name == new_name) {
- can_rename = false; // Name already used
- }
- }
- if (can_rename) {
- let old_name: string = c.name;
- c.name = new_name;
- let canvases: ui_node_canvas_t[] = [];
- for (let i: i32 = 0; i < project_materials.length; ++i) {
- let m: slot_material_t = project_materials[i];
- array_push(canvases, m.canvas);
- }
- for (let i: i32 = 0; i < project_material_groups.length; ++i) {
- let m: node_group_t = project_material_groups[i];
- array_push(canvases, m.canvas);
- }
- for (let i: i32 = 0; i < canvases.length; ++i) {
- let canvas: ui_node_canvas_t = canvases[i];
- for (let i: i32 = 0; i < canvas.nodes.length; ++i) {
- let n: ui_node_t = canvas.nodes[i];
- if (n.type == "GROUP" && n.name == old_name) {
- n.name = c.name;
- }
- }
- }
- }
- }
- else {
- c.name = new_name;
- }
- }
- ///end
- ///if is_lab
- ui_nodes_ui.window_border_top = 0;
- ui_nodes_ext_draw_buttons(ew, start_y);
- ///end
- let _BUTTON_COL: i32 = ui_nodes_ui.ops.theme.BUTTON_COL;
- ui_nodes_ui.ops.theme.BUTTON_COL = ui_nodes_ui.ops.theme.SEPARATOR_COL;
- ///if is_paint
- let cats: string[] = ui_nodes_canvas_type == canvas_type_t.MATERIAL ? nodes_material_categories : nodes_brush_categories;
- ///end
- ///if is_lab
- let cats: string[] = nodes_brush_categories;
- ///end
- for (let i: i32 = 0; i < cats.length; ++i) {
- if ((_ui_menu_button(tr(cats[i]))) || (ui_nodes_ui.is_hovered && ui_nodes_show_menu)) {
- ui_nodes_show_menu = true;
- ui_nodes_menu_category = i;
- ui_nodes_popup_x = ui_nodes_wx + ui_nodes_ui._x;
- ui_nodes_popup_y = ui_nodes_wy + ui_nodes_ui._y;
- if (config_raw.touch_ui) {
- ui_nodes_show_menu_first = true;
- let menuw: i32 = math_floor(ew * 2.3);
- ui_nodes_popup_x -= menuw / 2;
- ui_nodes_popup_x += ui_nodes_ui._w / 2;
- }
- }
- ui_nodes_ui._x += ui_nodes_ui._w + 3;
- ui_nodes_ui._y = 2 + start_y;
- }
- if (config_raw.touch_ui) {
- let _w: i32 = ui_nodes_ui._w;
- ui_nodes_ui._w = math_floor(36 * ui_SCALE(ui_nodes_ui));
- ui_nodes_ui._y = 4 * ui_SCALE(ui_nodes_ui) + start_y;
- if (ui_menubar_icon_button(ui_nodes_ui, 2, 3)) {
- 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));
- }
- ui_nodes_ui._w = _w;
- }
- else {
- if (_ui_menu_button(tr("Search"))) {
- ui_nodes_node_search_x = ui_nodes_ui._window_x + ui_nodes_ui._x;
- ui_nodes_node_search_y = ui_nodes_ui._window_y + ui_nodes_ui._y;
- // Allow for node menu to be closed first
- sys_notify_on_init(function() {
- ui_nodes_node_search(math_floor(ui_nodes_node_search_x), math_floor(ui_nodes_node_search_y));
- });
- }
- }
- if (ui_nodes_ui.is_hovered) {
- ui_tooltip(tr("Search for nodes") + " (" + map_get(config_keymap, "node_search") + ")");
- }
- ui_nodes_ui._x += ui_nodes_ui._w + 3;
- ui_nodes_ui._y = 2 + start_y;
- ui_nodes_ui.ops.theme.BUTTON_COL = _BUTTON_COL;
- // Close node group
- if (ui_nodes_group_stack.length > 0 && _ui_menu_button(tr("Close"))) {
- array_pop(ui_nodes_group_stack);
- }
- }
- ui_end(!ui_nodes_show_menu);
- if (ui_nodes_show_menu) {
- ///if is_paint
- let list: node_list_t[] = ui_nodes_canvas_type == canvas_type_t.MATERIAL ? nodes_material_list : nodes_brush_list;
- ///end
- ///if is_lab
- let list: node_list_t[] = nodes_brush_list;
- ///end
- let category: ui_node_t[] = list[ui_nodes_menu_category];
- let num_nodes: i32 = category.length;
- ///if is_paint
- let is_group_category: bool = ui_nodes_canvas_type == canvas_type_t.MATERIAL && nodes_material_categories[ui_nodes_menu_category] == "Group";
- ///end
- ///if is_lab
- let is_group_category: bool = nodes_material_categories[ui_nodes_menu_category] == "Group";
- ///end
- if (is_group_category) {
- num_nodes += project_material_groups.length;
- }
- let py: i32 = ui_nodes_popup_y;
- let menuw: i32 = math_floor(ew * 2.3);
- draw_begin(null);
- ui_begin_region(ui_nodes_ui, math_floor(ui_nodes_popup_x), math_floor(py), menuw);
- let _FILL_BUTTON_BG: i32 = ui_nodes_ui.ops.theme.FILL_BUTTON_BG;
- ui_nodes_ui.ops.theme.FILL_BUTTON_BG = false;
- let _ELEMENT_OFFSET: i32 = ui_nodes_ui.ops.theme.ELEMENT_OFFSET;
- ui_nodes_ui.ops.theme.ELEMENT_OFFSET = 0;
- let _ELEMENT_H: i32 = ui_nodes_ui.ops.theme.ELEMENT_H;
- ui_nodes_ui.ops.theme.ELEMENT_H = config_raw.touch_ui ? (28 + 2) : 28;
- ui_menu_h = category.length * ui_ELEMENT_H(ui_nodes_ui);
- if (is_group_category) {
- ui_menu_h += project_material_groups.length * ui_ELEMENT_H(ui_nodes_ui);
- }
- ui_menu_start(ui_nodes_ui);
- for (let i: i32 = 0; i < category.length; ++i) {
- let n: ui_node_t = category[i];
- if (ui_menu_button(tr(n.name))) {
- ui_nodes_push_undo();
- let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
- let nodes: ui_nodes_t = ui_nodes_get_nodes();
- let node: ui_node_t = ui_nodes_make_node(n, nodes, canvas);
- array_push(canvas.nodes, node);
- nodes.nodes_selected_id = [node.id];
- nodes.nodes_drag = true;
- ///if is_lab
- parser_logic_parse(canvas);
- ///end
- }
- // Next column
- if (ui_nodes_ui._y - ui_nodes_wy + ui_ELEMENT_H(ui_nodes_ui) / 2 > ui_nodes_wh) {
- ui_nodes_ui._x += menuw;
- ui_nodes_ui._y = py;
- }
- }
- if (is_group_category) {
- for (let i: i32 = 0; i < project_material_groups.length; ++i) {
- let g: node_group_t = project_material_groups[i];
- ui_nodes_ui.enabled = ui_nodes_can_place_group(g.canvas.name);
- let row: f32[] = [5 / 6, 1 / 6];
- ui_row(row);
- if (ui_button(config_button_spacing + g.canvas.name, ui_align_t.LEFT)) {
- ui_nodes_push_undo();
- let canvas: ui_node_canvas_t = ui_nodes_get_canvas(true);
- let nodes: ui_nodes_t = ui_nodes_get_nodes();
- let node: ui_node_t = ui_nodes_make_group_node(g.canvas, nodes, canvas);
- array_push(canvas.nodes, node);
- nodes.nodes_selected_id = [node.id];
- nodes.nodes_drag = true;
- }
- ///if is_paint
- ui_nodes_ui.enabled = !project_is_material_group_in_use(g);
- if (ui_button("x", ui_align_t.CENTER)) {
- history_delete_material_group(g);
- array_remove(project_material_groups, g);
- }
- ///end
- ui_nodes_ui.enabled = true;
- }
- }
- 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);
- ui_nodes_show_menu_first = false;
- ui_nodes_ui.ops.theme.FILL_BUTTON_BG = _FILL_BUTTON_BG;
- ui_nodes_ui.ops.theme.ELEMENT_OFFSET = _ELEMENT_OFFSET;
- ui_nodes_ui.ops.theme.ELEMENT_H = _ELEMENT_H;
- ui_menu_end(ui_nodes_ui);
- ui_end_region();
- draw_end();
- }
- if (ui_nodes_hide_menu) {
- ui_nodes_show_menu = false;
- ui_nodes_show_menu_first = true;
- }
- }
- function ui_nodes_contains_node_group_recursive(group: node_group_t, group_name: string): bool {
- if (group.canvas.name == group_name) {
- return true;
- }
- for (let i: i32 = 0; i < group.canvas.nodes.length; ++i) {
- let n: ui_node_t = group.canvas.nodes[i];
- if (n.type == "GROUP") {
- let g: node_group_t = project_get_material_group_by_name(n.name);
- if (g != null && ui_nodes_contains_node_group_recursive(g, group_name)) {
- return true;
- }
- }
- }
- return false;
- }
- function ui_nodes_can_place_group(group_name: string): bool {
- // Prevent Recursive node groups
- // The group to place must not contain the current group or a group that contains the current group
- if (ui_nodes_group_stack.length > 0) {
- for (let i: i32 = 0; i < ui_nodes_group_stack.length; ++i) {
- let g: node_group_t = ui_nodes_group_stack[i];
- if (ui_nodes_contains_node_group_recursive(project_get_material_group_by_name(group_name), g.canvas.name)) return false;
- }
- }
- // Group was deleted / renamed
- let group_exists: bool = false;
- for (let i: i32 = 0; i < project_material_groups.length; ++i) {
- let group: node_group_t = project_material_groups[i];
- if (group_name == group.canvas.name) {
- group_exists = true;
- }
- }
- if (!group_exists) {
- return false;
- }
- return true;
- }
- function ui_nodes_push_undo(last_canvas: ui_node_canvas_t = null) {
- if (last_canvas == null) {
- last_canvas = ui_nodes_get_canvas(true);
- }
- let canvas_group: i32 = -1;
- if (ui_nodes_group_stack.length > 0) {
- canvas_group = array_index_of(project_material_groups, ui_nodes_group_stack[ui_nodes_group_stack.length - 1]);
- }
- ui_base_hwnds[tab_area_t.SIDEBAR0].redraws = 2;
- history_edit_nodes(last_canvas, ui_nodes_canvas_type, canvas_group);
- }
- function ui_nodes_accept_asset_drag(index: i32) {
- ui_nodes_push_undo();
- let g: node_group_t = ui_nodes_group_stack.length > 0 ? ui_nodes_group_stack[ui_nodes_group_stack.length - 1] : null;
- ///if is_paint
- let n: ui_node_t =
- ui_nodes_canvas_type == canvas_type_t.MATERIAL ?
- nodes_material_create_node("TEX_IMAGE", g) :
- nodes_brush_create_node("TEX_IMAGE");
- ///end
- ///if is_lab
- let n: ui_node_t = nodes_brush_create_node("image_texture_node");
- ///end
- n.buttons[0].default_value[0] = index;
- ui_nodes_get_nodes().nodes_selected_id = [n.id];
- ///if is_lab
- parser_logic_parse(project_canvas);
- ///end
- }
- function ui_nodes_accept_layer_drag(index: i32) {
- ui_nodes_push_undo();
- if (slot_layer_is_group(project_layers[index])) {
- return;
- }
- let g: node_group_t = ui_nodes_group_stack.length > 0 ? ui_nodes_group_stack[ui_nodes_group_stack.length - 1] : null;
- let n: ui_node_t = nodes_material_create_node(slot_layer_is_mask(context_raw.layer) ? "LAYER_MASK" : "LAYER", g);
- n.buttons[0].default_value[0] = index;
- ui_nodes_get_nodes().nodes_selected_id = [n.id];
- }
- function ui_nodes_accept_material_drag(index: i32) {
- ui_nodes_push_undo();
- let g: node_group_t = ui_nodes_group_stack.length > 0 ? ui_nodes_group_stack[ui_nodes_group_stack.length - 1] : null;
- let n: ui_node_t = nodes_material_create_node("MATERIAL", g);
- n.buttons[0].default_value[0] = index;
- ui_nodes_get_nodes().nodes_selected_id = [n.id];
- }
- function ui_nodes_accept_swatch_drag(swatch: swatch_color_t) {
- ///if is_paint
- ui_nodes_push_undo();
- let g: node_group_t = ui_nodes_group_stack.length > 0 ? ui_nodes_group_stack[ui_nodes_group_stack.length - 1] : null;
- let n: ui_node_t = nodes_material_create_node("RGB", g);
- n.outputs[0].default_value = f32_array_create_xyzw(
- color_get_rb(swatch.base) / 255,
- color_get_gb(swatch.base) / 255,
- color_get_bb(swatch.base) / 255,
- color_get_ab(swatch.base) / 255
- );
- ui_nodes_get_nodes().nodes_selected_id = [n.id];
- ///end
- }
- function ui_nodes_make_node(n: ui_node_t, nodes: ui_nodes_t, canvas: ui_node_canvas_t): ui_node_t {
- let node: ui_node_t = {};
- node.id = ui_next_node_id(canvas.nodes);
- node.name = n.name;
- node.type = n.type;
- node.x = ui_nodes_get_node_x();
- node.y = ui_nodes_get_node_y();
- node.color = n.color;
- node.inputs = [];
- node.outputs = [];
- node.buttons = [];
- node.width = 0;
- let count: i32 = 0;
- for (let i: i32 = 0; i < n.inputs.length; ++i) {
- let soc: ui_node_socket_t = {};
- soc.id = ui_get_socket_id(canvas.nodes) + count;
- count++;
- soc.node_id = node.id;
- soc.name = n.inputs[i].name;
- soc.type = n.inputs[i].type;
- soc.color = n.inputs[i].color;
- soc.default_value = f32_array_create_from_array(n.inputs[i].default_value);
- soc.min = n.inputs[i].min;
- soc.max = n.inputs[i].max;
- soc.precision = n.inputs[i].precision;
- soc.display = n.inputs[i].display;
- array_push(node.inputs, soc);
- }
- for (let i: i32 = 0; i < n.outputs.length; ++i) {
- let soc: ui_node_socket_t = {};
- soc.id = ui_get_socket_id(canvas.nodes) + count;
- count++;
- soc.node_id = node.id;
- soc.name = n.outputs[i].name;
- soc.type = n.outputs[i].type;
- soc.color = n.outputs[i].color;
- soc.default_value = f32_array_create_from_array(n.outputs[i].default_value);
- soc.min = n.outputs[i].min;
- soc.max = n.outputs[i].max;
- soc.precision = n.outputs[i].precision;
- soc.display = n.outputs[i].display;
- array_push(node.outputs, soc);
- }
- for (let i: i32 = 0; i < n.buttons.length; ++i) {
- let but: ui_node_button_t = {};
- but.name = n.buttons[i].name;
- but.type = n.buttons[i].type;
- but.output = n.buttons[i].output;
- but.default_value = f32_array_create_from_array(n.buttons[i].default_value);
- if (n.buttons[i].data != null) {
- but.data = u8_array_create_from_array(n.buttons[i].data);
- }
- but.min = n.buttons[i].min;
- but.max = n.buttons[i].max;
- but.precision = n.buttons[i].precision;
- but.height = n.buttons[i].height;
- array_push(node.buttons, but);
- }
- return node;
- }
- function ui_nodes_make_group_node(group_canvas: ui_node_canvas_t, nodes: ui_nodes_t, canvas: ui_node_canvas_t): ui_node_t {
- let category: ui_node_t[] = nodes_material_list[5];
- let n: ui_node_t = category[0];
- let node: ui_node_t = util_clone_canvas_node(n);
- node.name = group_canvas.name;
- node.id = ui_next_node_id(canvas.nodes);
- node.x = ui_nodes_get_node_x();
- node.y = ui_nodes_get_node_y();
- let group_input: ui_node_t = null;
- let group_output: ui_node_t = null;
- for (let i: i32 = 0; i < project_material_groups.length; ++i) {
- let g: node_group_t = project_material_groups[i];
- let cname: string = g.canvas.name;
- if (cname == node.name) {
- for (let i: i32 = 0; i < g.canvas.nodes.length; ++i) {
- let n: ui_node_t = g.canvas.nodes[i];
- if (n.type == "GROUP_INPUT") {
- group_input = n;
- }
- else if (n.type == "GROUP_OUTPUT") {
- group_output = n;
- }
- }
- break;
- }
- }
- if (group_input != null && group_output != null) {
- for (let i: i32 = 0; i < group_input.outputs.length; ++i) {
- let soc: ui_node_socket_t = group_input.outputs[i];
- array_push(node.inputs, nodes_material_create_socket(nodes, node, soc.name, soc.type, canvas, soc.min, soc.max, soc.default_value));
- }
- for (let i: i32 = 0; i < group_output.inputs.length; ++i) {
- let soc: ui_node_socket_t = group_output.inputs[i];
- array_push(node.outputs, nodes_material_create_socket(nodes, node, soc.name, soc.type, canvas, soc.min, soc.max, soc.default_value));
- }
- }
- return node;
- }
- function ui_nodes_make_node_preview() {
- let ui_nodes: ui_nodes_t = context_raw.material.nodes;
- if (ui_nodes.nodes_selected_id.length == 0) {
- return;
- }
- let nodes: ui_node_t[] = context_raw.material.canvas.nodes;
- let node: ui_node_t = ui_get_node(nodes, ui_nodes.nodes_selected_id[0]);
- context_raw.node_preview_name = node.name;
- if (node.type == "LAYER" ||
- node.type == "LAYER_MASK" ||
- node.type == "MATERIAL" ||
- node.type == "OUTPUT_MATERIAL_PBR") {
- return;
- }
- if (array_index_of(nodes, node) == -1) {
- return;
- }
- if (context_raw.node_preview == null) {
- context_raw.node_preview = gpu_create_render_target(util_render_material_preview_size, util_render_material_preview_size);
- }
- context_raw.node_preview_dirty = false;
- ui_nodes_hwnd.redraws = 2;
- util_render_make_node_preview(context_raw.material.canvas, node, context_raw.node_preview);
- }
- function ui_nodes_has_group(c: ui_node_canvas_t): bool {
- for (let i: i32 = 0; i < c.nodes.length; ++i) {
- let n: ui_node_t = c.nodes[i];
- if (n.type == "GROUP") {
- return true;
- }
- }
- return false;
- }
- function ui_nodes_traverse_group(mgroups: ui_node_canvas_t[], c: ui_node_canvas_t) {
- for (let i: i32 = 0; i < c.nodes.length; ++i) {
- let n: ui_node_t = c.nodes[i];
- if (n.type == "GROUP") {
- if (ui_nodes_get_group(mgroups, n.name) == null) {
- let canvases: ui_node_canvas_t[] = [];
- for (let i: i32 = 0; i < project_material_groups.length; ++i) {
- let g: node_group_t = project_material_groups[i];
- array_push(canvases, g.canvas);
- }
- let group: ui_node_canvas_t = ui_nodes_get_group(canvases, n.name);
- array_push(mgroups, util_clone_canvas(group));
- ui_nodes_traverse_group(mgroups, group);
- }
- }
- }
- }
- function ui_nodes_get_group(canvases: ui_node_canvas_t[], name: string): ui_node_canvas_t {
- for (let i: i32 = 0; i < canvases.length; ++i) {
- let c: ui_node_canvas_t = canvases[i];
- if (c.name == name) {
- return c;
- }
- }
- return null;
- }
|