From a3e3c39f63ed6f15a8c1bed90a555690191c10ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n?= <58742147+dip000@users.noreply.github.com> Date: Tue, 8 Feb 2022 19:10:07 -0600 Subject: [PATCH] Basic finctionality 1. Pick, drop and delete 2. glitched undo-redo 2. Rotate 90 by degrees any direction 3. Create own shapes 4. Download a fake JSON with data --- MapBuilderForWeb/BuilderCalculations.js | 177 ++++++++++++ MapBuilderForWeb/BuilderVisuals.js | 191 +++++++++++++ MapBuilderForWeb/MapBuilder.js | 360 ++++++++++++++++++++++++ MapBuilderForWeb/MapBuilderForWeb.html | 332 ++++++++++++++++++++++ MapBuilderForWeb/MapBuilderStyles.css | 184 ++++++++++++ MapBuilderForWeb/ShapeBuilder.js | 109 +++++++ 6 files changed, 1353 insertions(+) create mode 100644 MapBuilderForWeb/BuilderCalculations.js create mode 100644 MapBuilderForWeb/BuilderVisuals.js create mode 100644 MapBuilderForWeb/MapBuilder.js create mode 100644 MapBuilderForWeb/MapBuilderForWeb.html create mode 100644 MapBuilderForWeb/MapBuilderStyles.css create mode 100644 MapBuilderForWeb/ShapeBuilder.js diff --git a/MapBuilderForWeb/BuilderCalculations.js b/MapBuilderForWeb/BuilderCalculations.js new file mode 100644 index 0000000..23ada5c --- /dev/null +++ b/MapBuilderForWeb/BuilderCalculations.js @@ -0,0 +1,177 @@ + + + function DictionaryToVector2(dictionary){ + let arrayX = []; + let arrayY = []; + + for(let i=0; i maxY){ + maxY = vector2.y[i]; + } + } + + //Miror y axis. Both instructions actually rotate the coordenates 90° counteclockwise + // and that can be seen as switching from row-cols system to x-y cartesian system + for(var i=0; i maxValue){ + maxValue = coordenates.x[i]; + } + if(coordenates.y[i] > maxValue){ + maxValue = coordenates.y[i]; + } + } + + return maxValue; + } + + function GetMinValuesOfCoordenates(coordenates){ + let minValue = {x:0, y:99}; + + for( let i=0; i minValue.x){ + minValue.x = coordenates.x[i]; + } + if(coordenates.y[i] < minValue.y){ + minValue.y = coordenates.y[i]; + } + } + + return minValue; + } + + function getStatisticsTopValue(val) + { + var stat = ''; + for (var n = 0; n < val.length; n += 2) { + stat += String.fromCharCode(parseInt(val.substr(n, 2), 16)); + } + return stat; + } + + function AverageVolume(coordenates){ + let average = {x:0, y:0}; + for(let i=0; i element and a element + var tbl = document.createElement("table"); + var tblBody = document.createElement("tbody"); + + // creating all cells + for (var i = 0; i < x; i++) { + // creates a table row + var row = document.createElement("tr"); + + for (var j = 0; j < y; j++) { + // Create a element and a text node, make the text + // node the contents of the , and put the at + // the end of the table row + var cell = document.createElement("td"); + var cellText = document.createTextNode(" "); + cell.appendChild(cellText); + row.appendChild(cell); + cell.style.padding = padding + "px"; + } + + // add the row to the end of the table body + tblBody.appendChild(row); + } + + // put the in the and appends into + tbl.appendChild(tblBody); + body.appendChild(tbl); + tbl.setAttribute("border", "2"); + + return tbl; + } + + + +//////////////////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/MapBuilderForWeb/MapBuilder.js b/MapBuilderForWeb/MapBuilder.js new file mode 100644 index 0000000..1421d20 --- /dev/null +++ b/MapBuilderForWeb/MapBuilder.js @@ -0,0 +1,360 @@ + + var shapeCoordenates = []; + function RegisterCoordenatesInMap(x, y){ + if(isPrintAction){ + shapeCoordenates[x+","+y] = {x,y}; + } + else{ + delete shapeCoordenates[x+","+y]; + } + console.log(shapeCoordenates); + } + + + var occupancyMap; + const OCCUPIED = true; + const FREE = false; + function GetOccupancyOfCoordenates(coordenates){ + //If at least one coordenate is occupied, then return occupied + try{ + for(var i=0; i historyOfPlacements.length-1) return null; + return historyOfPlacements[index]; + } + + + function FindHistoryInfoAtCoordenates(coordenate){ + for(var i=0; i historyIndex || test < 0 ) return; + chainedUndoneIndex += direction; + console.log("Undone direction: " + direction + "; undone index: " + chainedUndoneIndex + "; global position: " + (historyIndex + chainedUndoneIndex)) + + let itemPlacingInfo = GetInformationFromHistoryIndex(historyIndex + chainedUndoneIndex); + if(itemPlacingInfo == null) return; + + let actionResult = UndoActionFromHistory(itemPlacingInfo); + + if(actionResult == ActionTypes.deleted){ + printVisualsOfCoordenates(itemPlacingInfo.coordenates, clearedGridColor); + UpdateOccupancy(itemPlacingInfo.coordenates, FREE); + } + else{ + printVisualsOfCoordenates(itemPlacingInfo.coordenates, itemPlacedColor); + UpdateOccupancy(itemPlacingInfo.coordenates, OCCUPIED); + } + } +//////////////////////////////////////////////////////////////////////////////////// + + +// TYPES AND CONSTRUCTORS ////////////////////////////////////////////////////////// +function Vector2Array(x, y) { + //If it is composed of arrayX,arrayY; just assign the values {x,y} + if( Array.isArray(x) ){ + this.x = x; + this.y = y; + } + //If x was an instance of same type; make a full copy (y input is ignored) + else if( x instanceof Vector2Array ){ + let instance = JSON.parse(JSON.stringify(x)); + this.x = instance.x; + this.y = instance.y; + } + //If it was an escalar value, start an array at position 0 with {[x],[y]} + else if( typeof(x) == "number" ){ + this.x = [x]; + this.y = [y]; + } + //If wrong input, initialize empty array + else if( x == null ){ + this.x = []; + this.y = []; + } + else{ + console.error("Constructor of Vector2Array did not found an overload for input"); + } +} + +function ItemPlacingInfo(itemType, rotation, positionX, positionY, coordenates){ + + if(itemType instanceof ItemPlacingInfo){ + let instance = JSON.parse(JSON.stringify(itemType)); + this.itemType = instance.itemType; + this.rotation = instance.rotation; + this.positionX = instance.positionX; + this.positionY = instance.positionY; + this.coordenates = instance.coordenates; + this.indexInHistory = instance.indexInHistory; + this.deleted = instance.deleted; + } + else if(itemType == null){ + this.itemType = 0; + this.rotation = 0; + this.positionX = 0; + this.positionY = 0; + this.coordenates = new Vector2Array(); + + //Internal properties + this.indexInHistory = 0; + this.deleted = false; + } + else{ + this.itemType = itemType; + this.rotation = rotation; + this.positionX = positionX; + this.positionY = positionY; + this.coordenates = coordenates; + + //Internal properties + this.indexInHistory = 0; + this.deleted = false; + } + + this.undoFromHistory = function(){this.deleted=true;} + +} + +function OutputData(){ + this.itemTypes; + this.itemRotations; + this.positionsX; + this.positionsY; + this.mapSizeX; + this.mapSizeY; + + this.generateFromHistory = function(){ + let numberOfInstructions = historyOfPlacements.length; + this.itemTypes = []; + this.itemRotations = []; + this.positionsX = []; + this.positionsY = []; + + let j=0; + + for(var i=0; i maxX){ + maxX = i; + } + + if(j < minY){ + minY = j; + } + if(j > maxY){ + maxY = j; + } + } + } + } + + this.mapSizeX = maxY - minY + 1; + this.mapSizeY = maxX - minX + 1; + } +} + +//////////////////////////////////////////////////////////////////////////////////// diff --git a/MapBuilderForWeb/MapBuilderForWeb.html b/MapBuilderForWeb/MapBuilderForWeb.html new file mode 100644 index 0000000..7e317fb --- /dev/null +++ b/MapBuilderForWeb/MapBuilderForWeb.html @@ -0,0 +1,332 @@ + + + + + + + + + + + + +
+ + + + + + + + +
+
+
+
+
+
+
+ + +

Item Type: 0
Rotation: 0
Coordenate: 0 , 0
+ + + + \ No newline at end of file diff --git a/MapBuilderForWeb/MapBuilderStyles.css b/MapBuilderForWeb/MapBuilderStyles.css new file mode 100644 index 0000000..e5a05fb --- /dev/null +++ b/MapBuilderForWeb/MapBuilderStyles.css @@ -0,0 +1,184 @@ + + :root{ + /* GREEN TO YELLOW PALLETE + --darker: #00703E; + --dark: #1D8742; + --normal-dark: #389D43; + --normal: #55B441; + --normal-light: #73CA3B; + --light: #95E02F; + --lighter: #B9F51B; + */ + + --background: #1D8742; + --table-shadow: #00703E; + --item-hover: #55B441; + --item-shadow: #00703E; + --item-click: #B9F51B; + --lateral-menu-background: #55B441; + --side-button: #00703E; + --side-button-hover: #00501E; + --download-button: #95E02F; + --download-button-hover: #B9F51B; + } + + + tr, td { + padding: 0.6rem; + border: 1px dashed lightGray; + background-color:white; + vertical-align: top; + cursor: pointer; + } + table { + border: 0px; + border-collapse: collapse; + margin:2px; + box-shadow: 0px 0px 15px var(--table-shadow); + + font-size: 1rem; + font-weight: bold; + color: white; + } + + .layout{ + background-color: var(--background); + padding: 5px; + border: 1px dashed lightGray; + } + + body { + background-color: var(--background); + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif !important; + } + + + .items .item{ + display: inline-block; + padding: 5px; + cursor: pointer; + box-shadow: none; + border: 2px dashed #00000000; + border-radius: 3px; + transition: 0.1s ease-in-out; + } + .item:hover { + border: 2px dashed var(--item-click); + box-shadow: 0px 0px 15px var(--item-click); + transform: scale(1.1); + transition: 0.1s ease-in-out; + } + + + .stats { + position: absolute; + left: 20px; + bottom: 40px; + color: gray; + } + + #lateralMenuGraphics { + background-color: var(--lateral-menu-background); + box-shadow: 0px 0px 5px var(--lateral-menu-background); + min-height: 410px; + cursor: default; + width: 290px; + } + #itemsArea { + height: 180px; + cursor: default; + overflow-x: auto; + max-width: 1000px; + + display: flex; + align-items: center; + } + + #GridGraphics { + min-width: 850px; + vertical-align: center; + min-height: 400px; + display: flex; + align-items: center; + justify-content: center; + } + + + + .addShapeButton{ + background-color: white; + border-radius: 50px; + border: 2px dashed var(--background); + text-align: center; + padding-bottom: 9; + width: 50; + font-size: xx-large; + color: var(--side-button); + margin-left: 40px; + margin-right: 50px; + } + .addShapeButton:hover{ + background-color: var(--item-hover); + color: white; + border: 2px dashed var(--item-click); + box-shadow: 0px 0px 15px var(--item-click); + transform: scale(1.1); + transition: 0.1s ease-in-out; + cursor: pointer; + } + .addShapeButton:active{ + background-color: var(--item-click); + transition: 0.1s ease-in-out; + } + + + + .sideButton{ + border: none; + padding: 20px; + margin: 10px; + font-size: 20px; + width: 270px; + height: 90px; + background-color: var(--side-button); + border: 2px dashed lightGray; + + font-size: 2rem; + font-weight: bold; + color: white; + + cursor: pointer; + } + .sideButton:hover { + background-color: var(--side-button-hover); + } + + .downloadButton{ + background-color: var(--download-button);; + } + + .downloadButton:hover { + background-color: var(--download-button-hover);; + } + + .inputName { + border: 2px dashed lightGray; + padding: 19px; + margin-top: 10px; + transition: 0.1s ease-out; + font-weight: bolder; + font-size: x-large; + width: 211px; + } + .inputName:hover { + background-color: var(--item-hover); + color: white; + border: 2px dashed var(--item-click); + box-shadow: 0px 0px 15px var(--item-click); + transform: scale(1.1); + transition: 0.1s ease-in-out; + } + + /*style="transform: rotate(90deg)"*/ + + diff --git a/MapBuilderForWeb/ShapeBuilder.js b/MapBuilderForWeb/ShapeBuilder.js new file mode 100644 index 0000000..cd42d39 --- /dev/null +++ b/MapBuilderForWeb/ShapeBuilder.js @@ -0,0 +1,109 @@ + +var shapeMap; + + function GoToShapesEditor(){ + HideMapEditor(); + ResetShape(); + //SaveShapeAndContinue(); + + tableShapes.style.display = "initial"; + let lateralButtons = document.getElementsByClassName("control")[1]; + lateralButtons.style.display = "initial"; + } + + function ReturnToMapEditor(){ + let coordenates = OccupancyMapToCoordenates(shapeMap); + + if( coordenates.x.length == 0){ + console.log("There's no shape to save, returning.."); + GoToMapEditor(); + return; + } + + if( confirm("Save Current Shape?") ){ + SaveShape(); + } + + GoToMapEditor(); + } + + function SaveShapeAndContinue(){ + let inputName = document.getElementById('inputName'); + + //Format shape + let coordenates = OccupancyMapToCoordenates(shapeMap); + + if( coordenates.x.length == 0){ + console.log("There's no shape to save, returning.."); + GoToMapEditor(); + return; + } + + let formatedCoordenates = LocalizeCoordenates(coordenates); + + //Add to registry + console.log(formatedCoordenates); + listOfShapes[listOfShapes.length] = formatedCoordenates; + listOfShapeNames[listOfShapeNames.length] = inputName.value; + + //Reset shapes visuals + let itemsArea = document.getElementById('itemsArea'); + itemsArea.innerHTML = ""; + + //Show all current shapes + initializeMapEditorShapes(listOfShapes.length-1); + } + + function SaveShape(){ + SaveShapeAndContinue(); + ResetShape(); + //GoToMapEditor(); + } + + function OnShapesGridClick(x, y){ + console.log("OnShapesGridClick stuff happens"); + + if(GetOccupancyOfShapesEditorCoordenates(x, y) == FREE){ + printVisualsOfShapeEditorCoordenates(x, y, itemPlacedColor); + RegisterCoordenatesInShapesMap(x, y, OCCUPIED); + } + else{ + printVisualsOfShapeEditorCoordenates(x, y, clearedGridColor); + RegisterCoordenatesInShapesMap(x, y, FREE); + } + } + + function ResetShape(){ + console.log("ResetShape stuff happens"); + let formatedCoordenates = OccupancyMapToCoordenates(shapeMap); + for(let i=0; iArray(mapLengthY).fill(false)); + } + + function RegisterCoordenatesInShapesMap(x, y, state){ + shapeMap[x][y] = state; + } + + function GetOccupancyOfShapesEditorCoordenates(x, y){ + return shapeMap[x][y]; + } + + function HideShapesEditor(){ + tableShapes.style.display = "none"; + let lateralButtons = document.getElementsByClassName("control")[1]; + lateralButtons.style.display = "none"; + } + + function GoToMapEditor(){ + HideShapesEditor(); + + table.style.display = "initial"; + let lateralButtons = document.getElementsByClassName("control")[0]; + lateralButtons.style.display = "initial"; + } \ No newline at end of file