| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- /**
- * REALLY Small framework for creating/manipulating/deleting gui elements in a browser.
- */
- ['svg'].forEach(
- function (dependency) {
- if (this[dependency] === undefined) throw 'Missing dependency: ' + dependency;
- });
- userInterface = {};
- userInterface.mainWindow = window;
- userInterface.nextWindowId = 0;
- userInterface.EVENTS = {
- KEY_PRESS: 'keydown',
- MOUSE_CLICK: 'click',
- MOUSE_MOVE: 'mousemove',
- MOUSE_PRESS: 'mousedown',
- MOUSE_RELEASE: 'mouseup',
- MOUSE_RIGHT_CLICK: 'contextmenu',
- WINDOW_CLOSE: 'unload'
- };
- userInterface.MOUSE_BUTTONS = {
- LEFT: 0,
- MIDDLE: 1,
- RIGHT: 2
- };
- userInterface.KEYCODES = {
- DELETE: 46
- };
- userInterface.appendButton = function (targetWindow, text) {
- let button = targetWindow.document.createElement('button');
- button.innerHTML = text;
- targetWindow.document.body.appendChild(button);
- return userInterface.wrapElement(button);
- };
- userInterface.appendCanvas = function (targetWindow, width, height, style) {
- let canvas = targetWindow.document.createElementNS('http://www.w3.org/2000/svg', 'svg');
- canvas.setAttribute('width', width);
- canvas.setAttribute('height', height);
- for (let key in style) {
- canvas.style[key] = style[key];
- }
- targetWindow.document.body.appendChild(canvas);
- return new svg.canvasWrapper(canvas);
- };
- userInterface.bindEvent = function (source, event, controller, raiseName, port, timeOffset) {
- if (port === undefined) {port = 'userInterface';}
- if (timeOffset === undefined) {timeOffset = 0.0;}
- var args = [];
- switch (event) {
- case userInterface.EVENTS.KEY_PRESS:
- args = function (e) {
- return [e.keyCode, e.view]
- };
- source = source.document;
- break;
- case userInterface.EVENTS.MOUSE_CLICK:
- case userInterface.EVENTS.MOUSE_MOVE:
- case userInterface.EVENTS.MOUSE_PRESS:
- case userInterface.EVENTS.MOUSE_RELEASE:
- case userInterface.EVENTS.MOUSE_RIGHT_CLICK:
- args = function (e) {
- return [e.clientX, e.clientY, e.button]
- };
- if (source instanceof svg.elementWrapper)
- source = source.element;
- break;
- case userInterface.EVENTS.WINDOW_CLOSE:
- args = function () {
- return [source]
- };
- break;
- default:
- throw 'Unsupported event';
- }
- source.addEventListener(
- event,
- (function (controller, raiseName, port, timeOffset) {
- return function (e) {
- e.preventDefault();
- e.stopPropagation();
- controller.addInput(new Event(raiseName, port, args(e)), timeOffset);
- };
- }
- )(controller, raiseName, port, timeOffset));
- };
- userInterface.closeWindow = function (targetWindow) {
- targetWindow.close();
- };
- userInterface.log = function (value) {
- if (typeof (value) == 'object')
- console.log(JSON.stringify(value));
- else
- console.log(value);
- };
- userInterface.newWindow = function (width, height) {
- return window.open('', userInterface.nextWindowId++, 'width=' + width + ',height=' + height);
- };
- userInterface.println = function (value, target) {
- if (target === undefined) {target = window.document.body;}
- if (typeof (value) == 'object') {
- target.innerHTML += JSON.stringify(value);
- } else {
- target.innerHTML += value;
- }
- target.innerHTML += '<br/>'
- };
- userInterface.wrapElement = function (element) {
- return {element: element};
- };
|