input_node.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. type input_node_t = {
  2. base?: logic_node_t;
  3. };
  4. let input_node_coords: vec4_t = vec4_create();
  5. let input_node_start_x: f32 = 0.0;
  6. let input_node_start_y: f32 = 0.0;
  7. // Brush ruler
  8. let input_node_lock_begin: bool = false;
  9. let input_node_lock_x: bool = false;
  10. let input_node_lock_y: bool = false;
  11. let input_node_lock_start_x: f32 = 0.0;
  12. let input_node_lock_start_y: f32 = 0.0;
  13. let input_node_registered: bool = false;
  14. function input_node_create(arg: any): input_node_t {
  15. let n: float_node_t = {};
  16. n.base = logic_node_create();
  17. n.base.get = input_node_get;
  18. if (!input_node_registered) {
  19. input_node_registered = true;
  20. app_notify_on_update(input_node_update, n);
  21. }
  22. return n;
  23. }
  24. function input_node_update(self: float_node_t) {
  25. if (context_raw.split_view) {
  26. context_raw.view_index = mouse_view_x() > base_w() / 2 ? 1 : 0;
  27. }
  28. let decal: bool = context_raw.tool == workspace_tool_t.DECAL || context_raw.tool == workspace_tool_t.TEXT;
  29. let decal_mask: bool = decal && operator_shortcut(map_get(config_keymap, "decal_mask") + "+" + map_get(config_keymap, "action_paint"), shortcut_type_t.DOWN);
  30. let lazy_paint: bool = context_raw.brush_lazy_radius > 0 &&
  31. (operator_shortcut(map_get(config_keymap, "action_paint"), shortcut_type_t.DOWN) ||
  32. operator_shortcut(map_get(config_keymap, "brush_ruler") + "+" + map_get(config_keymap, "action_paint"), shortcut_type_t.DOWN) ||
  33. decal_mask);
  34. let paint_x: f32 = mouse_view_x() / app_w();
  35. let paint_y: f32 = mouse_view_y() / app_h();
  36. if (mouse_started()) {
  37. input_node_start_x = mouse_view_x() / app_w();
  38. input_node_start_y = mouse_view_y() / app_h();
  39. }
  40. if (pen_down()) {
  41. paint_x = pen_view_x() / app_w();
  42. paint_y = pen_view_y() / app_h();
  43. }
  44. if (pen_started()) {
  45. input_node_start_x = pen_view_x() / app_w();
  46. input_node_start_y = pen_view_y() / app_h();
  47. }
  48. if (operator_shortcut(map_get(config_keymap, "brush_ruler") + "+" + map_get(config_keymap, "action_paint"), shortcut_type_t.DOWN)) {
  49. if (input_node_lock_x) {
  50. paint_x = input_node_start_x;
  51. }
  52. if (input_node_lock_y) {
  53. paint_y = input_node_start_y;
  54. }
  55. }
  56. if (context_raw.brush_lazy_radius > 0) {
  57. context_raw.brush_lazy_x = paint_x;
  58. context_raw.brush_lazy_y = paint_y;
  59. }
  60. if (!lazy_paint) {
  61. input_node_coords.x = paint_x;
  62. input_node_coords.y = paint_y;
  63. }
  64. if (context_raw.split_view) {
  65. context_raw.view_index = -1;
  66. }
  67. if (input_node_lock_begin) {
  68. let dx: f32 = math_abs(input_node_lock_start_x - mouse_view_x());
  69. let dy: f32 = math_abs(input_node_lock_start_y - mouse_view_y());
  70. if (dx > 1 || dy > 1) {
  71. input_node_lock_begin = false;
  72. dx > dy ? input_node_lock_y = true : input_node_lock_x = true;
  73. }
  74. }
  75. if (keyboard_started(map_get(config_keymap, "brush_ruler"))) {
  76. input_node_lock_start_x = mouse_view_x();
  77. input_node_lock_start_y = mouse_view_y();
  78. input_node_lock_begin = true;
  79. }
  80. else if (keyboard_released(map_get(config_keymap, "brush_ruler"))) {
  81. input_node_lock_x = input_node_lock_y = input_node_lock_begin = false;
  82. }
  83. if (context_raw.brush_lazy_radius > 0) {
  84. let v1: vec4_t = vec4_create(context_raw.brush_lazy_x * app_w(), context_raw.brush_lazy_y * app_h(), 0.0);
  85. let v2: vec4_t = vec4_create(input_node_coords.x * app_w(), input_node_coords.y * app_h(), 0.0);
  86. let d: f32 = vec4_dist(v1, v2);
  87. let r: f32 = context_raw.brush_lazy_radius * 85;
  88. if (d > r) {
  89. let v3: vec4_t = vec4_create();
  90. vec4_sub_vecs(v3, v2, v1);
  91. vec4_normalize(v3);
  92. vec4_mult(v3, 1.0 - context_raw.brush_lazy_step);
  93. vec4_mult(v3, r);
  94. vec4_add_vecs(v2, v1, v3);
  95. input_node_coords.x = v2.x / app_w();
  96. input_node_coords.y = v2.y / app_h();
  97. // Parse brush inputs once on next draw
  98. context_raw.painted = -1;
  99. }
  100. context_raw.last_paint_x = -1;
  101. context_raw.last_paint_y = -1;
  102. }
  103. context_raw.parse_brush_inputs(context_raw.brush_output_node_inst);
  104. }
  105. function input_node_get(self: input_node_t, from: i32): logic_node_value_t {
  106. context_raw.brush_lazy_radius = logic_node_input_get(self.base.inputs[0])._f32;
  107. context_raw.brush_lazy_step = logic_node_input_get(self.base.inputs[1])._f32;
  108. let v: logic_node_value_t = { _any: input_node_coords };
  109. return v;
  110. }
  111. let input_node_def: zui_node_t = {
  112. id: 0,
  113. name: _tr("Input"),
  114. type: "input_node",
  115. x: 0,
  116. y: 0,
  117. color: 0xff4982a0,
  118. inputs: [
  119. {
  120. id: 0,
  121. node_id: 0,
  122. name: _tr("Lazy Radius"),
  123. type: "VALUE",
  124. color: 0xffa1a1a1,
  125. default_value: f32_array_create_x(0.0),
  126. min: 0.0,
  127. max: 1.0,
  128. precision: 100,
  129. display: 0
  130. },
  131. {
  132. id: 0,
  133. node_id: 0,
  134. name: _tr("Lazy Step"),
  135. type: "VALUE",
  136. color: 0xffa1a1a1,
  137. default_value: f32_array_create_x(0.0),
  138. min: 0.0,
  139. max: 1.0,
  140. precision: 100,
  141. display: 0
  142. }
  143. ],
  144. outputs: [
  145. {
  146. id: 0,
  147. node_id: 0,
  148. name: _tr("Position"),
  149. type: "VECTOR",
  150. color: 0xff63c763,
  151. default_value: null,
  152. min: 0.0,
  153. max: 1.0,
  154. precision: 100,
  155. display: 0
  156. }
  157. ],
  158. buttons: [],
  159. width: 0
  160. };