diff --git a/FloatingSpace/CircularMenu.js b/FloatingSpace/CircularMenu.js index 8e286884b..b623c7c34 100644 --- a/FloatingSpace/CircularMenu.js +++ b/FloatingSpace/CircularMenu.js @@ -51,6 +51,7 @@ function newCircularMenu () { let menuItemInitialValue = menuItemsInitialValues[i] menuItem.action = menuItemInitialValue.action + menuItem.actionFunction = menuItemInitialValue.actionFunction menuItem.label = menuItemInitialValue.label menuItem.visible = menuItemInitialValue.visible menuItem.imagePathOn = menuItemInitialValue.imagePathOn diff --git a/FloatingSpace/CircularMenuItem.js b/FloatingSpace/CircularMenuItem.js index cb8a25ea4..b22524258 100644 --- a/FloatingSpace/CircularMenuItem.js +++ b/FloatingSpace/CircularMenuItem.js @@ -7,6 +7,7 @@ function newCircularMenuItem () { iconOn: undefined, iconOff: undefined, action: undefined, + actionFunction: undefined, label: undefined, visible: false, imagePathOn: undefined, @@ -53,6 +54,7 @@ function newCircularMenuItem () { thisObject.iconOn = undefined thisObject.iconOff = undefined thisObject.payload = undefined + thisObject.actionFunction = undefined } function initialize (pPayload) { @@ -116,7 +118,7 @@ function newCircularMenuItem () { } function onMouseClick (event) { - thisObject.payload.onMenuItemClick(thisObject.payload, thisObject.action) + thisObject.actionFunction(thisObject.payload, thisObject.action) } function drawBackground () { diff --git a/FloatingSpace/CodeEditor.js b/FloatingSpace/CodeEditor.js new file mode 100644 index 000000000..a9cf2ed8d --- /dev/null +++ b/FloatingSpace/CodeEditor.js @@ -0,0 +1,215 @@ + +function newCodeEditor () { + const MODULE_NAME = 'Circular Menu Iem' + + let thisObject = { + isDeployed: undefined, + iconOK: undefined, + iconNOT_OK: undefined, + visible: false, + imagePathOK: undefined, + imagePathNOT_OK: undefined, + rawRadius: undefined, + targetRadius: undefined, + currentRadius: undefined, + container: undefined, + payload: undefined, + deactivate: deactivate, + activate: activate, + physics: physics, + drawBackground: drawBackground, + drawForeground: drawForeground, + getContainer: getContainer, + finalize: finalize, + initialize: initialize + } + + thisObject.container = newContainer() + thisObject.container.initialize(MODULE_NAME) + thisObject.container.isClickeable = false + thisObject.container.isDraggeable = false + thisObject.container.detectMouseOver = false + thisObject.container.frame.radius = 0 + thisObject.container.frame.position.x = 0 + thisObject.container.frame.position.y = 0 + thisObject.container.frame.width = 150 + thisObject.container.frame.height = 30 + + let isMouseOver = false + + let selfMouseOverEventSubscriptionId + let selfMouseClickEventSubscriptionId + let selfMouseNotOverEventSubscriptionId + return thisObject + + function finalize () { + thisObject.container.eventHandler.stopListening(selfMouseOverEventSubscriptionId) + thisObject.container.eventHandler.stopListening(selfMouseClickEventSubscriptionId) + thisObject.container.eventHandler.stopListening(selfMouseNotOverEventSubscriptionId) + + thisObject.container.finalize() + thisObject.container = undefined + thisObject.iconOK = undefined + thisObject.iconNOT_OK = undefined + thisObject.payload = undefined + } + + function initialize () { + /* Load Images */ + + thisObject.iconOK = new Image() + thisObject.iconOK.onload = onImageLoad + + function onImageLoad () { + thisObject.iconNOT_OK = new Image() + thisObject.iconNOT_OK.onload = onImageLoad + + function onImageLoad () { + thisObject.canDrawIcon = true + } + thisObject.iconNOT_OK.src = window.canvasApp.urlPrefix + thisObject.imagePathNOT_OK + } + thisObject.iconOK.src = window.canvasApp.urlPrefix + thisObject.imagePathOK + thisObject.icon = thisObject.iconOK // The default value is ON. + + selfMouseOverEventSubscriptionId = thisObject.container.eventHandler.listenToEvent('onMouseOver', onMouseOver) + selfMouseClickEventSubscriptionId = thisObject.container.eventHandler.listenToEvent('onMouseClick', onMouseClick) + selfMouseNotOverEventSubscriptionId = thisObject.container.eventHandler.listenToEvent('onMouseNotOver', onMouseNotOver) + } + + function deactivate () { + if (thisObject.visible === true) { + thisObject.visible = false + let textArea = document.getElementById('textArea') + textArea.style.display = 'none' + thisObject.payload.node.code = textArea.value + } + } + + function activate (payload) { + thisObject.visible = true + thisObject.payload = payload + thisObject.rawRadius = 8 + thisObject.targetRadius = thisObject.container.frame.radius + thisObject.currentRadius = 0 + + const WIDTH = thisObject.container.frame.radius * 2.2 + const HEIGHT = thisObject.container.frame.radius * 1 + + let textArea = document.getElementById('textArea') + textArea.value = payload.node.code + textArea.style = 'resize: none; border: none; outline: none; box-shadow: none; overflow:hidden; font-family: Saira; font-size: 12px; background-color: rgb(204, 88, 53);color:rgb(255, 255, 255); width: ' + WIDTH + 'px; height: ' + HEIGHT + 'px' + textArea.style.display = 'block' + + let textAreaPosition = { + x: 0 - WIDTH / 2, + y: 0 + } + + textAreaPosition = thisObject.container.frame.frameThisPoint(textAreaPosition) + + let textAreaDiv = document.getElementById('textAreaDiv') + textAreaDiv.style = 'position:absolute; top:' + textAreaPosition.y + 'px; left:' + textAreaPosition.x + 'px; z-index:1; ' + } + + function getContainer (point) { + let container + if (thisObject.isDeployed === true) { + if (thisObject.container.frame.isThisPointHere(point, true, false) === true) { + return thisObject.container + } else { + return undefined + } + } + } + + function physics () { + if (Math.abs(thisObject.currentRadius - thisObject.targetRadius) >= 0.5) { + if (thisObject.currentRadius < thisObject.targetRadius) { + thisObject.currentRadius = thisObject.currentRadius + 0.5 + } else { + thisObject.currentRadius = thisObject.currentRadius - 0.5 + } + } + + thisObject.container.frame.position.x = 0 + thisObject.container.frame.position.y = 0 + } + + function onMouseOver (point) { + if (thisObject.container.frame.isThisPointHere(point, true, false) === true) { + isMouseOver = true + } else { + isMouseOver = false + } + } + + function onMouseNotOver (point) { + isMouseOver = false + } + + function onMouseClick (event) { + + } + + function drawBackground () { + if (thisObject.visible === true) { + let position = { + x: 0, + y: 0 + } + + position = thisObject.container.frame.frameThisPoint(position) + + let radius = thisObject.container.frame.radius + + if (radius > 0.5) { + browserCanvasContext.beginPath() + browserCanvasContext.arc(position.x, position.y, radius * 1.5 + 3, 0, Math.PI * 2, true) + browserCanvasContext.closePath() + browserCanvasContext.fillStyle = 'rgba(' + UI_COLOR.WHITE + ', ' + 1 + ')' + browserCanvasContext.fill() + + browserCanvasContext.beginPath() + browserCanvasContext.arc(position.x, position.y, radius * 1.5, 0, Math.PI * 2, true) + browserCanvasContext.closePath() + browserCanvasContext.fillStyle = 'rgba(' + UI_COLOR.RUSTED_RED + ', ' + 1 + ')' + browserCanvasContext.fill() + } + } + } + function drawForeground () { + let iconPosition = { + x: 0, + y: thisObject.currentRadius * 1.5 + } + + iconPosition = thisObject.container.frame.frameThisPoint(iconPosition) + + /* Menu Item */ + + if (thisObject.canDrawIcon === true && thisObject.currentRadius > 1) { + browserCanvasContext.drawImage(thisObject.icon, iconPosition.x - thisObject.currentRadius, iconPosition.y - thisObject.currentRadius, thisObject.currentRadius * 2, thisObject.currentRadius * 2) + + /* Menu Label */ + + let label = 'Code looks good!' + let labelPoint + let fontSize = 10 + + browserCanvasContext.font = fontSize + 'px ' + UI_FONT.PRIMARY + + if (label !== undefined && thisObject.currentRadius >= thisObject.targetRadius) { + labelPoint = { + x: iconPosition.x + thisObject.currentRadius + 10, + y: iconPosition.y + fontSize * FONT_ASPECT_RATIO + } + + browserCanvasContext.font = fontSize + 'px ' + UI_FONT.PRIMARY + browserCanvasContext.fillStyle = 'rgba(' + UI_COLOR.WHITE + ', 1)' + browserCanvasContext.fillText(label, labelPoint.x, labelPoint.y) + } + } + } +} + diff --git a/FloatingSpace/StrategyPart.js b/FloatingSpace/StrategyPart.js index 66639bde1..2dc24d638 100644 --- a/FloatingSpace/StrategyPart.js +++ b/FloatingSpace/StrategyPart.js @@ -12,6 +12,7 @@ function newStrategyPart () { isOnFocus: false, container: undefined, payload: undefined, + codeEditor: undefined, physics: physics, drawBackground: drawBackground, drawMiddleground: drawMiddleground, @@ -51,6 +52,11 @@ function newStrategyPart () { thisObject.menu.finalize() thisObject.menu = undefined + if (thisObject.codeEditor !== undefined) { + thisObject.codeEditor.finalize() + thisObject.codeEditor = undefined + } + floatingLayer = undefined image = undefined imagePath = undefined @@ -67,6 +73,7 @@ function newStrategyPart () { menuItemsInitialValues = [ { action: 'Open Settings', + actionFunction: thisObject.payload.onMenuItemClick, label: 'Settings', visible: false, imagePathOn: 'Images/icons/style-01/tools.png', @@ -78,6 +85,7 @@ function newStrategyPart () { }, { action: 'Delete Strategy', + actionFunction: thisObject.payload.onMenuItemClick, label: 'Delete This Strategy', visible: false, imagePathOn: 'Images/icons/style-01/trash.png', @@ -94,6 +102,7 @@ function newStrategyPart () { menuItemsInitialValues = [ { action: 'Add Situation', + actionFunction: thisObject.payload.onMenuItemClick, label: 'Add Situation', visible: false, imagePathOn: 'Images/icons/style-01/attractive.png', @@ -110,6 +119,7 @@ function newStrategyPart () { menuItemsInitialValues = [ { action: 'Add Situation', + actionFunction: thisObject.payload.onMenuItemClick, label: 'Add Situation', visible: false, imagePathOn: 'Images/icons/style-01/attractive.png', @@ -126,6 +136,7 @@ function newStrategyPart () { menuItemsInitialValues = [ { action: 'Add Situation', + actionFunction: thisObject.payload.onMenuItemClick, label: 'Add Situation', visible: false, imagePathOn: 'Images/icons/style-01/attractive.png', @@ -142,6 +153,7 @@ function newStrategyPart () { menuItemsInitialValues = [ { action: 'Add Phase', + actionFunction: thisObject.payload.onMenuItemClick, label: 'Add Phase', visible: false, imagePathOn: 'Images/icons/style-01/placeholder.png', @@ -158,6 +170,7 @@ function newStrategyPart () { menuItemsInitialValues = [ { action: 'Add Phase', + actionFunction: thisObject.payload.onMenuItemClick, label: 'Add Phase', visible: false, imagePathOn: 'Images/icons/style-01/placeholder.png', @@ -174,6 +187,7 @@ function newStrategyPart () { menuItemsInitialValues = [ { action: 'Edit Code', + actionFunction: thisObject.payload.onMenuItemClick, label: 'Edit Code', visible: false, imagePathOn: 'Images/icons/style-01/html.png', @@ -185,6 +199,7 @@ function newStrategyPart () { }, { action: 'Add Situation', + actionFunction: thisObject.payload.onMenuItemClick, label: 'Add Situation', visible: false, imagePathOn: 'Images/icons/style-01/attractive.png', @@ -196,6 +211,7 @@ function newStrategyPart () { }, { action: 'Delete Phase', + actionFunction: thisObject.payload.onMenuItemClick, label: 'Delete This Phase', visible: false, imagePathOn: 'Images/icons/style-01/trash.png', @@ -212,6 +228,7 @@ function newStrategyPart () { menuItemsInitialValues = [ { action: 'Add Condition', + actionFunction: thisObject.payload.onMenuItemClick, label: 'Add Condition', visible: false, imagePathOn: 'Images/icons/style-01/testing.png', @@ -223,6 +240,7 @@ function newStrategyPart () { }, { action: 'Delete Situation', + actionFunction: thisObject.payload.onMenuItemClick, label: 'Delete This Situation', visible: false, imagePathOn: 'Images/icons/style-01/trash.png', @@ -236,9 +254,13 @@ function newStrategyPart () { } case 'Condition': { imagePath = 'Images/icons/style-01/testing.png' + thisObject.codeEditor = newCodeEditor() + thisObject.codeEditor.initialize() + thisObject.codeEditor.container.connectToParent(thisObject.container, false, false, true, true, false, false, false, false) menuItemsInitialValues = [ { action: 'Edit Code', + actionFunction: thisObject.codeEditor.activate, label: 'Edit Code', visible: false, imagePathOn: 'Images/icons/style-01/html.png', @@ -250,6 +272,7 @@ function newStrategyPart () { }, { action: 'Delete Condition', + actionFunction: thisObject.payload.onMenuItemClick, label: 'Delete This Condition', visible: false, imagePathOn: 'Images/icons/style-01/trash.png', @@ -303,6 +326,10 @@ function newStrategyPart () { function physics () { thisObject.menu.physics() + if (thisObject.codeEditor !== undefined) { + thisObject.codeEditor.physics() + } + if (thisObject.payload.chainParent === undefined) { return } thisObject.payload.targetPosition.x = thisObject.payload.chainParent.payload.position.x @@ -315,6 +342,10 @@ function newStrategyPart () { function onNotFocus () { thisObject.isOnFocus = false + + if (thisObject.codeEditor !== undefined) { + thisObject.codeEditor.deactivate() + } } function onMouseClick (event) { @@ -350,6 +381,11 @@ function newStrategyPart () { drawBodyAndPicture(params) thisObject.menu.drawForeground() + + if (thisObject.codeEditor !== undefined) { + thisObject.codeEditor.drawBackground() + thisObject.codeEditor.drawForeground() + } } } @@ -480,3 +516,4 @@ function newStrategyPart () { } } } +