Path: blob/main/website/GAUSS/js/canvas-manager.js
2941 views
var $canvas, canvas, canvas_Char, ctx, ctx_Char;12/*var canvas_cMenu =3{4'spritePositioning' :5[{6'text' : 'Place object here',7'action' : function() { console.log('Elt')}8}]9}10;*/1112paper.Point.getDistance = function(p)13{14return Math.sqrt(Math.pow(this.x - p.x, 2) - Math.pow(this.y - p.y, 2));15};1617var drawPolygon = function(polygon, fillColor, strokeColor, ctx, drawHandles)18{19ctx.strokeStyle = 'black';20ctx.lineWidth = 1;2122if(polygon.points.length == 0)23return;24if (polygon.points.length == 1)25{26ctx.fillRect(polygon.points[0].x - 5, polygon.points[0].y - 5, 10, 10);27return;28}29else30{31var shiftX = 0, shiftY = 0;32if(ctx === gameCtx)33{34shiftX = viewport.left;35shiftY = viewport.top;36}37ctx.beginPath();38ctx.moveTo(polygon.points[0].x - shiftX, polygon.points[0].y - shiftY);39for (var i = 0; i < polygon.points.length; i++) {40if (polygon.points[i + 1] != undefined) {41if (polygon.points[i + 1].x == polygon.points[i].x && polygon.points[i + 1].y == polygon.points[i].y)42continue;43ctx.lineTo(polygon.points[i + 1].x + 0.5 - shiftX, polygon.points[i + 1].y - shiftY);44}45else if (polygon.closed) {46ctx.closePath();47if(fillColor)48{49ctx.fillStyle = fillColor;50ctx.fill();51break;52}5354}55}5657if(strokeColor)58{59ctx.lineWidth = 3;60ctx.strokeStyle = strokeColor;61}62ctx.stroke();63ctx.strokeStyle = 'black';6465ctx.fillStyle = 'black';6667/*for(var i = 0; i < polygon.edges.length; i++)68{69if(polygon.edges[i].highlight)70{71//alert('Close to edge!');72ctx.fillRect(0, 0, 50, 50);73ctx.strokeStyle = 'blue';74ctx.lineTo(polygon.edges[i][0] + 0.5, polygon.edges[i][1]);75break;76}77}78*/79if (!polygon.closed || drawHandles === true)80for(var i = 0; i < polygon.points.length; i++)81{82if(polygon.points[i].highlight)83ctx.fillStyle = 'red';84ctx.fillRect(polygon.points[i].x - 5, polygon.points[i].y - 5, 10, 10);85ctx.fillStyle = 'black';86}878889}90};919293/*=======================================================================================================*/94var getItemWithSpriteContaining = function(point)95{96/* The point must be sought starting from the highest layers down to the lowest.97This allows the user to select an item whose bounding box is completely contained inside another object's bounding box */9899var ks = new Array();100for(var layer in editorCurrentRoom.zOrderMap)101ks.push(layer);102ks.sort(); // Since you can't order an object by keys in Javascript, order its keys103104for(var i = ks.length - 1; i >= 0; i--)105{106for(var j = 0; j < editorCurrentRoom.zOrderMap[ks[i]].length; j++)107{108var currentItem = editorMapIdItem[editorCurrentRoom.zOrderMap[ks[i]][j]];109if (currentItem.defaultAnims.default == null)110continue;111var anim = editorMapIdAnim[currentItem.defaultAnims.default];112if(!anim)113continue;114var frame = anim.frames[anim.start_idx];115var bb = new paper.Rectangle(currentItem.position.x - frame.img.width / 2, currentItem.position.y - frame.img.height, frame.img.width, frame.img.height);116if (bb.contains(point))117return currentItem;118}119}120return null;121};122123var getCharacterWithSpriteContaining = function(point)124{125var room = editorMapIdCharacter[editorCurrentCharacter.id];126var characters = [];127128for(var key in editorMapIdCharacter)129if(editorMapIdCharacter[key].id == room.id)130characters.push(editorMapIdCharacter[key]);131132for(var i = 0; i < characters.length; i++)133{134if (characters[i].defaultAnims.stand.FL == null)135continue;136var anim = editorMapIdAnim[characters[i].defaultAnims.stand.FL];137if(!anim)138continue;139var frame = anim.frames[anim.start_idx];140var bb = new paper.Rectangle(characters[i].position.x - frame.img.width / 2, characters[i].position.y - frame.img.height, frame.img.width, frame.img.height);141if (bb.contains(point))142return characters[i];143}144return null;145};146147var drawItemBoundingBox = function(item, ctx)148{149if(item == null)150return;151var frame = getItemPlaceHolder(item);152ctx.beginPath();153ctx.lineWidth = '2';154ctx.setLineDash([2]);155ctx.strokeStyle = '#2200ff';156ctx.rect(item.position.x - frame.img.width / 2, item.position.y - frame.img.height, frame.img.width, frame.img.height);157ctx.stroke();158};159160var updateCanvas = function(room, which)161{162if(!room)163{164$('.container.' + which).hide();165return;166}167168var _canvas;169var _context;170var _selectedData;171var _drawRoomChunks;172173switch(which)174{175case 'room':176_canvas = canvas;177_context = ctx;178_selectedData = editorCurrentItem;179_drawRoomChunks = function()180{181for(var key in room.zOrderMap)182{183var itemsKeyList = room.zOrderMap[key];184for (var i = 0; i < itemsKeyList.length; i++)185{186var item = editorMapIdItem[itemsKeyList[i]];187if(item.hideFromCanvas === true)188continue;189var frame = getItemPlaceHolder(item);190if (!frame)191continue;192if (item.position.x == null || item.position.y == null)193continue;194_context.drawImage(frame.img, item.position.x - frame.img.width / 2, item.position.y - frame.img.height);195}196}197};198break;199case 'character':200_canvas = canvas_Char;201_context = ctx_Char;202_selectedData = editorCurrentCharacter;203_drawRoomChunks = function()204{205for (var key in editorMapIdCharacter)206{207var character = editorMapIdCharacter[key];208if (character.parentRoomId != room.id)209continue;210var frame = getItemPlaceHolder(character);211if (!frame)212continue;213if (character.position.x == null || character.position.y == null)214continue;215_context.drawImage(frame.img, character.position.x - frame.img.width / 2, character.position.y - frame.img.height);216}217};218break;219}220221_context.clearRect(0, 0, _canvas.width, _canvas.height);222223if(room.items.length == 0)224{225$('.container.' + which).hide();226//$('.canvas.room').hide();227//$('.toolbar.room').hide();228//$('.item').hide();229return;230}231232//$(_canvas).css({ 'width' : room.items[0].boundingBox.width, 'height' : room.items[0].boundingBox.height });233_canvas.width = room.items[0].boundingBox.width;234//_canvas.width = resolution.width;235_canvas.height = room.items[0].boundingBox.height;236//_canvas.height = resolution.height;237238_context.drawImage(room.items[0].img, room.items[0].boundingBox.left, room.items[0].boundingBox.top);239240_drawRoomChunks();241switch(_canvas.state)242{243case 'sprite':244drawItemBoundingBox(_canvas.selected, _context);245break;246case 'hotspot':247if(_selectedData != null && _selectedData.hotspot != null)248drawPolygon(_selectedData.hotspot, '', _selectedData.hotspot['closed'] ? 'red' : '', _context);249break;250case 'pathfinding':251if(keys(editorCurrentRoom.walkBoxes).length > 0)252{253for(var i in editorCurrentRoom.walkBoxes)254{255var fillColor = 'rgba(255, 255, 0, 0.5)';256var lineColor = 'orange';257if(editorCurrentWalkBox === editorCurrentRoom.walkBoxes[i].id)258{259fillColor = 'rgba(255, 0, 0, 0.5)';260lineColor = 'purple';261}262drawPolygon(editorCurrentRoom.walkBoxes[i].polygon, fillColor, lineColor, ctx, true);263}264}265break;266case 'crop-image':267if(cropPoly != null)268{269drawPolygon(cropPoly, '', cropPoly['closed'] ? 'red' : '', ctx);270if(cropPoly['closed'] === true)271{272var data = cropImage(cropPoly);273var croppedImage = new Image();274croppedImage.src = data.src;275$(croppedImage).css('border', '1px solid black');276var croppedImageDiv = $('#cropped-image');277croppedImageDiv.empty();278croppedImageDiv.append(croppedImage);279$('#cropped-image-modal').modal('show');280cropPoly = null;281}282}283break;284case 'walk-behind':285if(editorCurrentItem != null && editorCurrentItem.centralPerspectiveWalkBehind != null)286{287ctx.strokeStyle = 'black';288ctx.lineWidth = 1;289290ctx.beginPath();291ctx.moveTo(0, editorCurrentItem.centralPerspectiveWalkBehind);292ctx.lineTo(canvas.width, editorCurrentItem.centralPerspectiveWalkBehind);293ctx.closePath();294ctx.stroke();295}296break;297default:298break;299}300$('.container.' + which).show(); // Show the right part of the page301//$('.canvas.room').show();302$('.toolbar.' + which).width($('.canvas.' + which).width());303$('.item.' + which).show();304};305306