input_node.ts 4.7 KB

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