Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.00 KB | None | 0 0
  1. (
  2. var imgui_prev;
  3. var imgui = ();
  4.  
  5. var ui_begin = {
  6. ui_clear.value; // TODO: perhaps possible to fix via property?
  7.  
  8. imgui.inside_item = nil;
  9. Pen.font = imgui.font;
  10.  
  11. Pen.fillColor = Color.black;
  12. Pen.strokeColor = Color.black;
  13. if (imgui_prev.notNil) {
  14. if (imgui.mouse_is_down and: imgui_prev.mouse_is_down.not) {
  15. imgui.mouse_was_pressed = true;
  16. } {
  17. imgui.mouse_was_pressed = false;
  18. };
  19. if (imgui.mouse_is_down.not and: imgui_prev.mouse_is_down) {
  20. imgui.mouse_was_released = true;
  21. } {
  22. imgui.mouse_was_released = false;
  23. };
  24. };
  25. };
  26.  
  27. var ui_end = {
  28. imgui.mousedeltax = 0;
  29. imgui.mousedeltay = 0;
  30. imgui.keys_pressed = [];
  31. imgui.keys_released = [];
  32.  
  33. imgui_prev = imgui.copy;
  34. };
  35.  
  36. var ui_clear = {
  37. Pen.fillColor = Color.white; // Color.new(0xff/0xff, 0xa5/0xff, 0);
  38. Pen.addRect(Rect(0, 0, v.bounds.width, v.bounds.height));
  39. Pen.fill;
  40. };
  41.  
  42. var ui_clip_rect = { |x, y, width, height|
  43. var left = x;
  44. var top = y;
  45. var right = x+width;
  46. var bottom = y+height;
  47. Pen.moveTo(left@top);
  48. Pen.lineTo(right@top);
  49. Pen.lineTo(right@bottom);
  50. Pen.lineTo(left@bottom);
  51. Pen.lineTo(left@top);
  52. Pen.clip;
  53. imgui.active_clip_area = (left: left, top: top, right: right, bottom: bottom);
  54. };
  55.  
  56. var do_text = { |string, x, y, width, height|
  57. var id = ['text', string, x, y];
  58. var result = false;
  59.  
  60. width = width ? (imgui.button_xpad * 2) + get_text_width.value(string);
  61. height = height ? imgui.button_ypad * 2 + imgui.fontsize + 5;
  62.  
  63. draw_text_widget.value(id, string, x, y, width, height);
  64. };
  65.  
  66. var do_button = { |string, x, y, width, height|
  67. var id = ['button', string, x, y];
  68.  
  69. var result = false;
  70. var is_inside, is_active, is_hot;
  71.  
  72. width = width ? (imgui.button_xpad * 2) + get_text_width.value(string);
  73. height = height ? imgui.button_ypad * 2 + imgui.fontsize + 5;
  74.  
  75. is_inside = is_inside_clipped_rect.value(x, y, x+width, y+height, imgui.mousex, imgui.mousey);
  76. is_active = imgui.active_item == id;
  77. is_hot = imgui.hot_item == id;
  78.  
  79. if (is_active) {
  80. if (imgui.mouse_was_released) {
  81. if (is_hot) {
  82. postln("pressed " ++ id ++ ": " ++ string);
  83. result = true;
  84. };
  85. imgui.active_item = nil;
  86. }
  87. } {
  88. if (is_hot) {
  89. if (imgui.mouse_was_pressed) {
  90. imgui.active_item = id;
  91. };
  92. };
  93. };
  94.  
  95. if (is_inside and: imgui.inside_item.isNil) {
  96. imgui.inside_item = id;
  97. if (imgui.active_item.isNil) {
  98. imgui.hot_item = id;
  99. }
  100. } {
  101. if (is_hot) {
  102. imgui.hot_item = nil;
  103. };
  104. };
  105.  
  106. draw_button.value(id, string, x, y, width, height);
  107.  
  108. result;
  109. };
  110.  
  111. var do_checkbox = { |id, string, container, x, y, width, height|
  112. var is_inside, is_active, is_hot;
  113.  
  114. width = width ? imgui.button_xpad + imgui.checkbox_size + imgui.button_xpad + get_text_width.value(string) + imgui.button_xpad;
  115. height = height ? imgui.button_ypad * 2 + imgui.fontsize + 5;
  116.  
  117. is_inside = is_inside_clipped_rect.value(x, y, x+width, y+height, imgui.mousex, imgui.mousey);
  118.  
  119. is_active = imgui.active_item == id;
  120. is_hot = imgui.hot_item == id;
  121.  
  122. if (is_active) {
  123. if (imgui.mouse_was_released) {
  124. if (is_hot) {
  125. postln("pressed " ++ id ++ ": " ++ string);
  126. container.ref = container.ref.not;
  127. };
  128. imgui.active_item = nil;
  129. }
  130. } {
  131. if (is_hot) {
  132. if (imgui.mouse_was_pressed) {
  133. imgui.active_item = id;
  134. };
  135. };
  136. };
  137.  
  138. if (is_inside and: imgui.inside_item.isNil) {
  139. imgui.inside_item = id;
  140. if (imgui.active_item.isNil) {
  141. imgui.hot_item = id;
  142. }
  143. } {
  144. if (is_hot) {
  145. imgui.hot_item = nil;
  146. };
  147. };
  148.  
  149. draw_checkbox.value(id, string, container, x, y, width, height);
  150. };
  151.  
  152. var do_modules = { |modules|
  153. modules.do { |module, i|
  154. do_module.value(module);
  155. };
  156. };
  157.  
  158. var do_module = { |module|
  159. var id = ['module', module];
  160.  
  161. var result = false;
  162.  
  163. var pos = module.pos;
  164. var string = module.name;
  165. var x = pos.x;
  166. var y = pos.y;
  167.  
  168. var width = get_module_width.value(module);
  169. var height = get_module_height.value(module);
  170.  
  171. var is_inside = is_inside_clipped_rect.value(x+1, y+1, x+width-2, y+height-2, imgui.mousex, imgui.mousey);
  172.  
  173. var is_active = imgui.active_item == id;
  174. var is_hot = imgui.hot_item == id;
  175.  
  176. if (is_active) {
  177. if (imgui.mouse_was_released) {
  178. if (is_hot) {
  179. postln("pressed " ++ id ++ ": " ++ string);
  180. result = true;
  181. };
  182. imgui.active_item = nil;
  183. } {
  184. if ((imgui.mousedeltax != 0) or: (imgui.mousedeltay != 0)) {
  185. pos.x = pos.x+imgui.mousedeltax;
  186. pos.y = pos.y+imgui.mousedeltay;
  187. };
  188. }
  189. } {
  190. if (is_hot) {
  191. if (imgui.mouse_was_pressed) {
  192. imgui.active_item = id;
  193. };
  194. };
  195. };
  196.  
  197. if (is_inside and: imgui.inside_item.isNil) {
  198. imgui.inside_item = id;
  199. if (imgui.active_item.isNil) {
  200. imgui.hot_item = id;
  201. }
  202. } {
  203. if (is_hot) {
  204. imgui.hot_item = nil;
  205. };
  206. };
  207.  
  208. draw_module.value(id, module, x, y);
  209.  
  210. result;
  211. };
  212.  
  213. var do_module_in_and_outlets = { |modules|
  214. modules.do { |module|
  215. module.inlets.do { |index|
  216. do_inlet.value(module, index);
  217. };
  218. module.outlets.do { |index|
  219. do_outlet.value(module, index);
  220. };
  221. };
  222. };
  223.  
  224. var do_inlet = { |module, index|
  225. var id = ['inlet', module, index];
  226.  
  227. var pos = get_module_inlet_pos.value(module, index);
  228. var bounds = Rect(pos.x - (imgui.inoutlet_width/2), pos.y, imgui.inoutlet_width, 3);
  229. var detect_within_bounds = bounds.insetBy(-5);
  230.  
  231. var is_inside = is_inside_clipped_rect2.value(detect_within_bounds, imgui.mousex, imgui.mousey);
  232. var is_active = imgui.active_item == id;
  233. var is_hot = imgui.hot_item == id;
  234.  
  235. if (is_active) {
  236. if (imgui.mouse_was_released) {
  237. if (is_hot) {
  238. postln("pressed" + id);
  239. // result = true;
  240. };
  241. imgui.active_item = nil;
  242. }
  243. } {
  244. if (is_hot) {
  245. if (imgui.mouse_was_pressed) {
  246. imgui.active_item = id;
  247. };
  248. } {
  249. if (is_inside) {
  250. if (imgui.mouse_was_released) {
  251. if (imgui.possible_connection.notNil) {
  252. var from = imgui.possible_connection.key;
  253. var to = imgui.possible_connection.value;
  254.  
  255. if (outlet_is_connected_to_inlet.value(from, to).not) {
  256. var outlet_module = from[1];
  257. var outlet_index = from[2];
  258. var inlet_module = to[1];
  259. var inlet_index = to[2];
  260. outlet_module['connections'] = outlet_module['connections'].add([outlet_index, [inlet_module.name, inlet_index]]);
  261. };
  262. }
  263. }
  264. }
  265. }
  266. };
  267.  
  268. if (is_inside and: imgui.inside_item.isNil) {
  269. imgui.inside_item = id;
  270. if (imgui.active_item.isNil) {
  271. imgui.hot_item = id;
  272. }
  273. } {
  274. if (is_hot) {
  275. imgui.hot_item = nil;
  276. };
  277. };
  278.  
  279. draw_inlet.value(id, bounds);
  280. };
  281.  
  282. var do_outlet = { |module, index|
  283. var id = ['outlet', module, index];
  284.  
  285. var pos = get_module_outlet_pos.value(module, index);
  286. var bounds = Rect(pos.x - (imgui.inoutlet_width/2), pos.y-3, imgui.inoutlet_width, 3);
  287. var detect_within_bounds = bounds.insetBy(-5);
  288.  
  289. var is_inside = is_inside_clipped_rect2.value(detect_within_bounds, imgui.mousex, imgui.mousey);
  290. var is_active = imgui.active_item == id;
  291. var is_hot = imgui.hot_item == id;
  292.  
  293. if (is_active) {
  294. if (imgui.mouse_was_released) {
  295. if (is_hot) {
  296. postln("pressed" + id);
  297. // result = true;
  298. };
  299. imgui.active_item = nil;
  300. }
  301. } {
  302. if (is_hot) {
  303. if (imgui.mouse_was_pressed) {
  304. imgui.active_item = id;
  305. };
  306. } {
  307. if (is_inside) {
  308. if (imgui.mouse_was_released) {
  309. if (imgui.possible_connection.notNil) {
  310. var from = imgui.possible_connection.key;
  311. var to = imgui.possible_connection.value;
  312.  
  313. if (outlet_is_connected_to_inlet.value(from, to).not) {
  314. var outlet_module = from[1];
  315. var outlet_index = from[2];
  316. var inlet_module = to[1];
  317. var inlet_index = to[2];
  318. outlet_module['connections'] = outlet_module['connections'].add([outlet_index, [inlet_module.name, inlet_index]]);
  319. };
  320. }
  321. }
  322. }
  323. }
  324. };
  325.  
  326. if (is_inside and: imgui.inside_item.isNil) {
  327. imgui.inside_item = id;
  328. if (imgui.active_item.isNil) {
  329. imgui.hot_item = id;
  330. }
  331. } {
  332. if (is_hot) {
  333. imgui.hot_item = nil;
  334. };
  335. };
  336.  
  337. draw_outlet.value(id, bounds);
  338. };
  339.  
  340. var do_connections = { |modules|
  341. var connections = modules.collect { |module, i|
  342. module.connections.collect { |arr|
  343. var dest = arr[1];
  344. [[module, arr[0]], [modules.detect { |module| module.name == dest[0] }, dest[1]]]
  345. };
  346. }.flatten.reject { |conn| conn.isNil };
  347.  
  348. connections.do { |conn|
  349. do_connection.value(conn[0][0], conn[0][1], conn[1][0], conn[1][1]);
  350. };
  351. };
  352.  
  353. var do_connection = { |src_module, src_index, dest_module, dest_index|
  354. var id = ['connection', src_module, src_index, dest_module, dest_index];
  355.  
  356. var from = get_module_outlet_pos.value(src_module, src_index);
  357. var to = get_module_inlet_pos.value(dest_module, dest_index);
  358.  
  359. var is_inside = is_inside_connection.value(from, to, imgui.mousex, imgui.mousey);
  360. var is_active = imgui.active_item == id;
  361. var is_hot = imgui.hot_item == id;
  362.  
  363. if (is_active) {
  364. if (imgui.mouse_was_released) {
  365. imgui.selection = id;
  366. /*
  367. if (is_hot) {
  368. postln("pressed " ++ id ++ ": " ++ string);
  369. result = true;
  370. };
  371. */
  372. imgui.active_item = nil;
  373. }
  374. } {
  375. if (is_hot) {
  376. if (imgui.mouse_was_pressed) {
  377. imgui.active_item = id;
  378. };
  379. };
  380. };
  381.  
  382. if (is_inside and: imgui.inside_item.isNil) {
  383. imgui.inside_item = id;
  384. if (imgui.active_item.isNil) {
  385. imgui.hot_item = id;
  386. }
  387. } {
  388. if (is_hot) {
  389. imgui.hot_item = nil;
  390. };
  391. };
  392.  
  393. draw_connection.value(id, from, to);
  394. };
  395.  
  396. var do_tips = { |modules|
  397. var positions = all_in_and_outlet_positions.value(modules);
  398. var pos_to_inlet;
  399. if ((pos_to_inlet = positions.detect { |position| ((imgui.mousex@imgui.mousey) dist: position.key) < 10 }).notNil) {
  400. var pos = pos_to_inlet.key;
  401. var module_inlet = pos_to_inlet.value;
  402. var type = module_inlet[0];
  403. var module = module_inlet[1];
  404. var inlet = module_inlet[2];
  405.  
  406. if (type == 'inlet') {
  407. do_text.value(1234124, "Inlet " ++ inlet, pos.x-(imgui.inoutlet_width/2), pos.y-imgui.fontsize-10);
  408. } {
  409. do_text.value(1234124, "Outlet " ++ inlet, pos.x-(imgui.inoutlet_width/2), pos.y+(imgui.fontsize/2));
  410. };
  411. };
  412. };
  413.  
  414. var draw_text_widget = { |id, string, x, y, width, height|
  415. draw_text.value(string, x, y);
  416. };
  417.  
  418. var draw_button = { |id, string, x, y, width, height|
  419. if (imgui.active_item == id) {
  420. Pen.width = 3;
  421. } {
  422. Pen.width = 2;
  423. };
  424. draw_text.value(string, x+imgui.button_xpad, y+imgui.button_ypad);
  425. draw_rect.value(x, y, width, height);
  426. };
  427.  
  428. var draw_module = { |id, module, x, y|
  429. if (imgui.active_item == id) {
  430. Pen.width = 3;
  431. } {
  432. if (imgui.hot_item == id) {
  433. Pen.width = 1;
  434. } {
  435. Pen.width = 2;
  436. }
  437. };
  438. //fill_rect.value(x, y, get_module_width.value(module), get_module_height.value(module), Color.yellow);
  439. draw_rect.value(x, y, get_module_width.value(module), get_module_height.value(module));
  440. Pen.fillColor = Color.black;
  441. draw_text.value(module.name, x+imgui.button_xpad, y+imgui.button_ypad);
  442. };
  443.  
  444. var draw_checkbox = { |id, string, container, x, y, width, height|
  445. if (imgui.active_item == id) {
  446. Pen.width = 3;
  447. } {
  448. Pen.width = 2;
  449. };
  450.  
  451. if (container.ref) {
  452. fill_rect.value(x+imgui.button_xpad, y+imgui.button_ypad, imgui.checkbox_size, imgui.checkbox_size);
  453. } {
  454. draw_rect.value(x+imgui.button_xpad, y+imgui.button_ypad, imgui.checkbox_size, imgui.checkbox_size);
  455. };
  456.  
  457. draw_text.value(string, x+imgui.button_xpad+imgui.checkbox_size+imgui.button_xpad, y+imgui.button_ypad);
  458. draw_rect.value(x, y, width, height);
  459. };
  460.  
  461. var draw_text = { |string, x, y|
  462. Pen.stringAtPoint(string, x@y);
  463. };
  464.  
  465. var draw_rect = { |x, y, width, height|
  466. Pen.strokeColor = Color.black;
  467. Pen.strokeRect(Rect(x, y, width, height));
  468. };
  469.  
  470. var draw_inlet = { |id, bounds|
  471. var is_inside = imgui.inside_item == id;
  472. var focused = case
  473. { imgui.active_item.notNil and: is_inside } {
  474. if (imgui.active_item.first == 'outlet') {
  475. outlet_is_connected_to_inlet.value(imgui.active_item, id).not
  476. }
  477. }
  478. { is_inside } { true } ? false;
  479.  
  480. draw_in_and_outlet.value(id, bounds, focused);
  481. };
  482.  
  483. var draw_outlet = { |id, bounds|
  484. var is_inside = imgui.inside_item == id;
  485. var focused = case
  486. { imgui.active_item.notNil and: is_inside } {
  487. if (imgui.active_item.first == 'inlet') {
  488. outlet_is_connected_to_inlet.value(id, imgui.active_item).not
  489. }
  490. }
  491. { is_inside } { true } ? false;
  492.  
  493. draw_in_and_outlet.value(id, bounds, focused);
  494. };
  495.  
  496. var draw_in_and_outlet = { |id, bounds, focused|
  497. var is_inside = imgui.inside_item == id;
  498.  
  499. fill_rect.value(bounds.left, bounds.top, bounds.width, bounds.height, Color.black);
  500.  
  501. if (focused) {
  502. fill_oval.value(bounds.insetBy(-5));
  503. };
  504. };
  505.  
  506. var draw_connection = { |id, from, to|
  507. var is_selected = imgui.selection == id;
  508.  
  509. Pen.strokeColor = case
  510. { is_selected } {Color.grey}
  511. { true } {Color.black};
  512. Pen.width = case
  513. { is_selected } {6}
  514. { imgui.active_item == id } {6}
  515. { imgui.hot_item == id } {4}
  516. { true } {2};
  517. Pen.moveTo(from);
  518. Pen.lineTo(to);
  519. Pen.stroke;
  520. };
  521.  
  522. var outlet_is_connected_to_inlet = { |outlet, inlet|
  523. var outlet_module = outlet[1];
  524. var outlet_index = outlet[2];
  525. var inlet_module = inlet[1];
  526. var inlet_index = inlet[2];
  527.  
  528. var conn = [outlet_index, [inlet_module.name, inlet_index]];
  529. // conn.debug(\conn);
  530.  
  531. outlet_module['connections'].any { |actual_conn|
  532. actual_conn/* .debug(\actual) */ == conn
  533. };
  534. };
  535.  
  536. var is_inside_clipped_rect2 = { |bounds, x, y|
  537. is_inside_clipped_rect.value(bounds.left, bounds.top, bounds.right, bounds.bottom, x, y);
  538. };
  539.  
  540. var is_inside_clipped_rect = { |left, top, right, bottom, x, y|
  541. var clip = imgui.active_clip_area;
  542.  
  543. if (clip.notNil) {
  544. (clip.left <= x) and: (x <= clip.right) and: (clip.top <= y) and: (y <= clip.bottom) and: (left <= x) and: (x <= right) and: (top <= y) and: (y <= bottom)
  545. } {
  546. (left <= x) and: (x <= right) and: (top <= y) and: (y <= bottom)
  547. };
  548. };
  549.  
  550. var get_module_inlet_pos = { |module, index|
  551. var span = get_module_width.value(module) - imgui.inoutlet_width;
  552. var div = span / ((module.inlets-1) max: 1);
  553. ( module.pos.x+(imgui.inoutlet_width/2)+(div*index) ) @ (module.pos.y);
  554. };
  555.  
  556. var get_module_outlet_pos = { |module, index|
  557. var span = get_module_width.value(module) - imgui.inoutlet_width;
  558. var div = span / ((module.outlets-1) max: 1);
  559. ( module.pos.x+(imgui.inoutlet_width/2)+(div*index) ) @ (module.pos.y + get_module_height.value(module));
  560. };
  561.  
  562. var get_module_width = { |module|
  563. (imgui.button_xpad * 2) + get_text_width.value(module.name);
  564. };
  565.  
  566. var get_module_height = { |module|
  567. (imgui.button_ypad * 2) + imgui.fontsize + 4;
  568. };
  569.  
  570. var fill_oval = { |bounds|
  571. Pen.strokeColor = Color.grey;
  572. Pen.strokeOval(bounds);
  573. };
  574.  
  575. var fill_rect = { |x, y, width, height, colour|
  576. Pen.fillColor = colour ? Color.red;
  577. Pen.fillRect(Rect(x, y, width, height));
  578. };
  579.  
  580. var get_text_width = { |text|
  581. text.bounds(imgui.font).width+3;
  582. };
  583.  
  584. var all_in_and_outlet_positions = { |modules|
  585. modules.collect { |module, i|
  586. module.inlets.collect { |inlet|
  587. get_module_inlet_pos.value(module, inlet) -> ['inlet', module, inlet]
  588. } ++
  589. module.outlets.collect { |inlet|
  590. get_module_outlet_pos.value(module, inlet) -> ['outlet', module, inlet]
  591. };
  592. }.flatten;
  593. };
  594.  
  595. // TODO: does not respect clip
  596. var is_inside_connection = { |from, to, mousex, mousey, sensitivity = 5|
  597. var mouse_pos = mousex@mousey;
  598. var diff = to-from;
  599. var delta = diff/from.dist(to);
  600.  
  601. var points = from.dist(to).round.asInteger;
  602.  
  603. points.collect { |index, i|
  604. (from+(delta*index)) dist: mouse_pos
  605. }.minItem < sensitivity;
  606. };
  607.  
  608. var dump = { |id|
  609. switch (id.first)
  610. { 'inlet' } {
  611. "Inlet:" ++ id[1].name ++ "/" ++ id[2] + get_module_inlet_pos.value(id[1], id[2])
  612. }
  613. { 'outlet' } {
  614. "Outlet:" ++ id[1].name ++ "/" ++ id[2] + get_module_outlet_pos.value(id[1], id[2])
  615. } ? id
  616. };
  617.  
  618. imgui.window_width = 640;
  619. imgui.window_height = 480;
  620. imgui.hot_item = nil;
  621. imgui.active_item = nil;
  622. imgui.inoutlet_width = 18;
  623. imgui.button_xpad = 10;
  624. imgui.button_ypad = 5;
  625. imgui.fontsize = 18;
  626. imgui.checkbox_size = imgui.fontsize;
  627. imgui.font = Font("Consolas", imgui.fontsize);
  628. //imgui.font = Font("Akkurat-Mono", imgui.fontsize);
  629. imgui.mousex = 0;
  630. imgui.mousey = 0;
  631. imgui.mouse_is_down = false;
  632. imgui.mouse_was_pressed = false;
  633. imgui.mouse_was_released = false;
  634. imgui.mousedeltax = 0;
  635. imgui.mousedeltay = 0;
  636. imgui.keys_pressed = [];
  637. imgui.keys_released = [];
  638.  
  639. imgui.selection = nil;
  640.  
  641. {
  642. var x = 300;
  643. var y = 50;
  644. var col_width = 0;
  645. var row_height = 45;
  646.  
  647. ~modules = [
  648. (
  649. name: "FreqGate",
  650. pos: Point(x+(col_width*0), y+(row_height*0)),
  651. inlets: 0,
  652. outlets: 1,
  653. connections: [
  654. //[0, ["Osc", 0]],
  655. //[0, ["Envelope", 0]],
  656. ]
  657. ),
  658. (
  659. name: "Osc",
  660. pos: Point(x+(col_width*0), y+(row_height*1)),
  661. inlets: 1,
  662. outlets: 1,
  663. connections: [
  664. //[0, ["Filter", 0]],
  665. ]
  666. ),
  667. (
  668. name: "Filter",
  669. pos: Point(x+(col_width*1), y+(row_height*2)),
  670. inlets: 3,
  671. outlets: 1,
  672. connections: [
  673. //[0, ["Amp", 0]],
  674. ]
  675. ),
  676. (
  677. name: "Envelope",
  678. pos: Point(x+110+(col_width*2), y+(row_height*1)),
  679. inlets: 1,
  680. outlets: 1,
  681. connections: [
  682. //[0, ["Amp", 1]],
  683. //[0, ["Filter", 1]],
  684. ]
  685. ),
  686. (
  687. name: "Amp",
  688. pos: Point(x+(col_width*2), y+(row_height*3)),
  689. inlets: 2,
  690. outlets: 1,
  691. connections: [
  692. //[0, ["Pan", 0]],
  693. ]
  694. ),
  695. (
  696. name: "Pan",
  697. pos: Point(x+(col_width*3), y+(row_height*4)),
  698. inlets: 1,
  699. outlets: 2,
  700. connections: [
  701. //[0, ["Out", 0]],
  702. //[1, ["Out", 1]],
  703. ]
  704. ),
  705. (
  706. name: "Out",
  707. pos: Point(x+(col_width*3), y+(row_height*5)),
  708. inlets: 2,
  709. outlets: 0,
  710. )
  711. ];
  712. }.value;
  713.  
  714. w = Window.new("", Rect(100, 100, imgui.window_width, imgui.window_height)).front;
  715.  
  716. v = UserView(w, Rect(0, 0, imgui.window_width, imgui.window_height));
  717.  
  718. v.resize = 5;
  719.  
  720. v.drawFunc = {
  721. ui_begin.value;
  722.  
  723. do_module_in_and_outlets.value(~modules);
  724. do_connections.value(~modules);
  725. do_modules.value(~modules);
  726.  
  727. if (imgui.active_item.notNil) {
  728. var type = imgui.active_item.first;
  729. Pen.width = 2;
  730.  
  731. switch (type)
  732. { 'outlet' } {
  733. var module = imgui.active_item[1];
  734. var index = imgui.active_item[2];
  735. var pos = get_module_outlet_pos.value(module, index);
  736. Pen.moveTo(pos);
  737.  
  738. if (imgui.inside_item.notNil) {
  739. if (imgui.inside_item.first == 'inlet') {
  740. if (outlet_is_connected_to_inlet.value(imgui.active_item, imgui.inside_item).not) {
  741. Pen.lineTo(get_module_inlet_pos.value(imgui.inside_item[1], imgui.inside_item[2]));
  742. imgui.possible_connection = imgui.active_item -> imgui.inside_item;
  743. }
  744. };
  745. } ?? {
  746. Pen.lineTo(imgui.mousex@imgui.mousey);
  747. imgui.possible_connection = nil;
  748. };
  749. }
  750. { 'inlet' } {
  751. var module = imgui.active_item[1];
  752. var index = imgui.active_item[2];
  753. var pos = get_module_inlet_pos.value(module, index);
  754. Pen.moveTo(pos);
  755.  
  756. if (imgui.inside_item.notNil) {
  757. if (imgui.inside_item.first == 'outlet') {
  758. if (outlet_is_connected_to_inlet.value(imgui.inside_item, imgui.active_item).not) {
  759. Pen.lineTo(get_module_outlet_pos.value(imgui.inside_item[1], imgui.inside_item[2]));
  760. imgui.possible_connection = imgui.inside_item -> imgui.active_item;
  761. };
  762. };
  763. } ?? {
  764. Pen.lineTo(imgui.mousex@imgui.mousey);
  765. imgui.possible_connection = nil;
  766. };
  767.  
  768. };
  769. Pen.stroke;
  770. };
  771.  
  772. if (imgui.active_item.isNil and: imgui.mouse_was_pressed) {
  773. imgui.selection = nil;
  774. };
  775.  
  776. //do_tips.value(~modules);
  777.  
  778. do_text.value("Mouse: "++(imgui.mousex@imgui.mousey), 10, 340);
  779. do_text.value("Inside Item: "++(dump.value(imgui.inside_item) ? "None"), 10, 360);
  780. do_text.value("Hot Item: "++(dump.value(imgui.hot_item) ? "None"), 10, 380);
  781. do_text.value("Active Item: "++(dump.value(imgui.active_item) ? "None"), 10, 400);
  782.  
  783. if (imgui.selection.notNil) {
  784. if (imgui.keys_pressed.includes($s)) {
  785. imgui.selection = nil;
  786. };
  787.  
  788. if (imgui.keys_pressed.includes($d)) {
  789. var outlet_module = imgui.selection[1];
  790. var outlet_index = imgui.selection[2];
  791. var inlet_module = imgui.selection[3];
  792. var inlet_index = imgui.selection[4];
  793.  
  794. outlet_module['connections'] = outlet_module['connections'].reject { |conn| conn == [outlet_index, [inlet_module.name, inlet_index]] };
  795.  
  796. imgui.selection = nil;
  797. };
  798. };
  799.  
  800. ui_end.value;
  801. };
  802.  
  803. w.acceptsMouseOver = true;
  804. v.mouseOverAction = { |view, x, y|
  805. imgui.mousedeltax = x-imgui.mousex;
  806. imgui.mousedeltay = y-imgui.mousey;
  807. imgui.mousex = x;
  808. imgui.mousey = y;
  809. v.refresh;
  810. };
  811. v.mouseMoveAction = v.mouseOverAction;
  812. v.mouseDownAction = { |...args|
  813. imgui.mouse_is_down = true;
  814. v.refresh;
  815. };
  816. v.mouseUpAction = { |...args|
  817. imgui.mouse_is_down = false;
  818. v.refresh;
  819. };
  820. v.keyDownAction = { |view, char, mod, unicode, keycode, key|
  821. imgui.keys_pressed = imgui.keys_pressed.add(char);
  822. };
  823. v.keyUpAction = { |view, char, mod, unicode, keycode, key|
  824. imgui.keys_released = imgui.keys_released.add(char);
  825. };
  826.  
  827. v.animate = true; // TODO: invalidate on request instead
  828. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement