ui.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * REALLY Small framework for creating/manipulating/deleting gui elements in a browser.
  3. */
  4. ['svg'].forEach(
  5. function (dependency) {
  6. if (this[dependency] === undefined) throw 'Missing dependency: ' + dependency;
  7. });
  8. userInterface = {};
  9. userInterface.mainWindow = window;
  10. userInterface.nextWindowId = 0;
  11. userInterface.EVENTS = {
  12. KEY_PRESS: 'keydown',
  13. MOUSE_CLICK: 'click',
  14. MOUSE_MOVE: 'mousemove',
  15. MOUSE_PRESS: 'mousedown',
  16. MOUSE_RELEASE: 'mouseup',
  17. MOUSE_RIGHT_CLICK: 'contextmenu',
  18. WINDOW_CLOSE: 'unload'
  19. };
  20. userInterface.MOUSE_BUTTONS = {
  21. LEFT: 0,
  22. MIDDLE: 1,
  23. RIGHT: 2
  24. };
  25. userInterface.KEYCODES = {
  26. DELETE: 46
  27. };
  28. userInterface.appendButton = function (targetWindow, text) {
  29. let button = targetWindow.document.createElement('button');
  30. button.innerHTML = text;
  31. targetWindow.document.body.appendChild(button);
  32. return userInterface.wrapElement(button);
  33. };
  34. userInterface.appendCanvas = function (targetWindow, width, height, style) {
  35. let canvas = targetWindow.document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  36. canvas.setAttribute('width', width);
  37. canvas.setAttribute('height', height);
  38. for (let key in style) {
  39. canvas.style[key] = style[key];
  40. }
  41. targetWindow.document.body.appendChild(canvas);
  42. return new svg.canvasWrapper(canvas);
  43. };
  44. userInterface.bindEvent = function (source, event, controller, raiseName, port, timeOffset) {
  45. if (port === undefined) {port = 'userInterface';}
  46. if (timeOffset === undefined) {timeOffset = 0.0;}
  47. var args = [];
  48. switch (event) {
  49. case userInterface.EVENTS.KEY_PRESS:
  50. args = function (e) {
  51. return [e.keyCode, e.view]
  52. };
  53. source = source.document;
  54. break;
  55. case userInterface.EVENTS.MOUSE_CLICK:
  56. case userInterface.EVENTS.MOUSE_MOVE:
  57. case userInterface.EVENTS.MOUSE_PRESS:
  58. case userInterface.EVENTS.MOUSE_RELEASE:
  59. case userInterface.EVENTS.MOUSE_RIGHT_CLICK:
  60. args = function (e) {
  61. return [e.clientX, e.clientY, e.button]
  62. };
  63. if (source instanceof svg.elementWrapper)
  64. source = source.element;
  65. break;
  66. case userInterface.EVENTS.WINDOW_CLOSE:
  67. args = function () {
  68. return [source]
  69. };
  70. break;
  71. default:
  72. throw 'Unsupported event';
  73. }
  74. source.addEventListener(
  75. event,
  76. (function (controller, raiseName, port, timeOffset) {
  77. return function (e) {
  78. e.preventDefault();
  79. e.stopPropagation();
  80. controller.addInput(new Event(raiseName, port, args(e)), timeOffset);
  81. };
  82. }
  83. )(controller, raiseName, port, timeOffset));
  84. };
  85. userInterface.closeWindow = function (targetWindow) {
  86. targetWindow.close();
  87. };
  88. userInterface.log = function (value) {
  89. if (typeof (value) == 'object')
  90. console.log(JSON.stringify(value));
  91. else
  92. console.log(value);
  93. };
  94. userInterface.newWindow = function (width, height) {
  95. return window.open('', userInterface.nextWindowId++, 'width=' + width + ',height=' + height);
  96. };
  97. userInterface.println = function (value, target) {
  98. if (target === undefined) {target = window.document.body;}
  99. if (typeof (value) == 'object') {
  100. target.innerHTML += JSON.stringify(value);
  101. } else {
  102. target.innerHTML += value;
  103. }
  104. target.innerHTML += '<br/>'
  105. };
  106. userInterface.wrapElement = function (element) {
  107. return {element: element};
  108. };