Path: blob/main/website/GAUSS/js/project.js
2941 views
var resolution = { width : 1024, height : 768 };12var newProject = function()3{4$('#room-accordion').empty();5for(var room in editorMapIdRoom)6{7deleteEditorRoom(room);8view_UpdateDataLists('delete', 'rooms', room);9editorRoomsCount = 0;10editorItemsCount = 0;11}12$('#character-accordion').empty();13for(var character in editorMapIdCharacter)14{15deleteEditorCharacter(character);16}17$('#anims-accordion').empty();18for(var anim in editorMapIdAnim)19{20deleteEditorAnim(anim);21view_UpdateDataLists('delete', 'anims', anim);22}23$('#inventory-accordion').empty();24for(var invItem in editorMapIdInvItem)25{26deleteInvItem(invItem);27view_UpdateDataLists('delete', 'inventory-items', invItem);28editorInvItemCount = 0;29}30$('#dialog-accordion').empty();31for(var dialog in editorMapIdDialog)32{33deleteDialog(dialog);34view_UpdateDataLists('delete', 'dialogs', dialog);35editorDialogsCount = 0;36}37$('#audio-container').empty();38for(var audio in editorMapIdAudio)39{40deleteAudioContent(audio)41view_UpdateDataLists('delete', 'audio', audio);42}43for(var script in editorScriptList)44{45view_UpdateDataLists('delete', 'scripts', script);46}47editorScriptList = {};48};4950var saveProject = function()51{52var backupRooms = owl.deepCopy(editorRoomsList);53var backupCharacters = owl.deepCopy(editorCharactersList);54var backupAnims = owl.deepCopy(editorAnimsList);5556for(var i in backupRooms)57{58eliminateCycles(backupRooms[i], 'room');59delete backupRooms[i].zOrderMap;60}61for(var i = 0; i < backupAnims.length; i++)62eliminateCycles(backupAnims[i], 'anim');6364view_SaveGameVariables();65var project = { resolution : JSON.stringify(resolution), rooms : JSON.stringify(backupRooms), actions: JSON.stringify(editorActionsList), characters: JSON.stringify(backupCharacters), scripts: JSON.stringify(editorScriptList), vars : JSON.stringify(editorGameVars), anims: JSON.stringify(backupAnims), inv_items : JSON.stringify(editorInvItemList), dialogs : JSON.stringify(editorMapIdDialog), audio : JSON.stringify(editorMapIdAudio) };66download('project.json', JSON.stringify(project));67};6869var loadProject = function(event)70{71newProject();72var fileReader = new FileReader();73fileReader.onload = function(event)74{75var s = event.target.result;76var project = JSON.parse(s);77if(project.resolution)78resolution = JSON.parse(project.resolution);79editorRoomsList = JSON.parse(project.rooms);80editorMapIdRoom = {};81editorMapIdItem = {};82editorCurrentRoom = null;83editorCurrentItem = null;84editorRoomsCount = 0;85editorItemsCount = 0;86editorInvItemList = JSON.parse(project.inv_items);87editorInvItemCount = 0;88editorCharactersList = JSON.parse(project.characters);89editorMapIdCharacter = {};90editorAnimsList = JSON.parse(project.anims);91editorMapIdAnim = {};92editorAnimsCount = 0;93editorScriptList = JSON.parse(project.scripts);94if(project.dialogs)95editorMapIdDialog = JSON.parse(project.dialogs);96if(project.audio)97editorMapIdAudio = JSON.parse(project.audio);9899if(project.vars) editorGameVars = JSON.parse(project.vars);100view_ClearRoomPanels();101102for(var i = 0; i < editorAnimsList.length; i++)103{104editorAnimsList[i].addFrame = addAnimFrame;105editorAnimsList[i].removeFrame = removeAnimFrame;106editorAnimsList[i].incrCurrIdx = incrCurrFrame;107editorAnimsList[i].play = startRollingFrames;108editorAnimsList[i].stop = stopRollingFrames;109110for(var j = 0; j < editorAnimsList[i].frames.length; j++)111{112var frame = editorAnimsList[i].frames[j];113if(frame)114{115var img = new Image();116img.src = frame.img;117//var newFrame = new EditorSprite(img);118//newFrame.setPosition(frame.boundingBox[1], frame.boundingBox[2]);119//newFrame.setSize(frame.boundingBox[3], frame.boundingBox[4]);120//editorAnimsList[i].frames[j] = newFrame;121editorAnimsList[i].frames[j] = {img: img};122}123}124editorMapIdAnim[editorAnimsList[i].id] = editorAnimsList[i];125view_CreateNewAnimPanel(editorAnimsList[i].id);126editorAnimsCount++;127}128129for(var i = 0; i < editorCharactersList.length; i++)130{131var currentCharacter = editorCharactersList[i];132currentCharacter.getCurrentFrame = getItemCurrentFrame;133currentCharacter.setLayer = setItemLayer;134for(var j in currentCharacter.sprites)135for(var k in currentCharacter.sprites[j])136{137var src = currentCharacter.sprites[j][k].img;138var img = new Image();139img.src = src;140//var sprite = new EditorSprite(img);141//sprite.setPosition(currentCharacter.sprites[j][k].boundingBox[1], currentCharacter.sprites[j][k].boundingBox[2]);142//sprite.setSize(currentCharacter.sprites[j][k].boundingBox[3], currentCharacter.sprites[j][k].boundingBox[4]);143//currentCharacter.sprites[j][k] = sprite;144currentCharacter.sprites[j][k] = {img: img};145};146147var hotspot = currentCharacter.hotspot;148149if(hotspot != null)150{151var hotspotCopy = new Polygon();152for (var k = 0; k < hotspot.points.length; k++)153hotspotCopy.points[k] = new paper.Point(hotspot.points[k][1], hotspot.points[k][2]);154if(hotspot.closed)155hotspotCopy.close();156editorCharactersList[i].hotspot = hotspotCopy;157}158editorMapIdCharacter[editorCharactersList[i].id] = editorCharactersList[i];159view_CreateNewCharacterPanel(editorCharactersList[i].id);160}161162for(var i in editorRoomsList)163{164editorRoomsList[i].setId = setId;165editorRoomsList[i].zOrderMap = {};166if(editorRoomsList[i].walkBehinds == undefined) // Project backward compatibility167editorRoomsList[i].walkBehinds = [];168editorMapIdRoom[editorRoomsList[i].id] = editorRoomsList[i];169editorRoomsCount++;170171view_CreateNewRoomPanel(editorRoomsList[i].id);172173if(editorRoomsList[i].walkablePath != null)174{175var path = editorRoomsList[i].walkablePath;176var pathCopy = new Polygon();177178for(var j = 0; j < path.points.length; j++)179pathCopy.points[j] = new paper.Point(path.points[j][1], path.points[j][2]);180if(path.closed)181pathCopy.close();182183if(path.holes.length)184{185var holesCopy = [];186187for(var j = 0; j < path.holes.length; j++)188{189var hole = path.holes[j];190var newHole = new Polygon();191192for(var k = 0; k < hole.points.length; k++)193{194newHole.points[k] = new paper.Point(hole.points[k][1], hole.points[k][2]);195}196holesCopy[j] = newHole;197if(hole.closed)198newHole.close();199}200201pathCopy.holes = holesCopy;202}203editorRoomsList[i].walkablePath = pathCopy;204}205var bg = editorRoomsList[i].items[0];206if(bg)207{208var src = bg.img;209var img = new Image();210img.src = src;211editorRoomsList[i].items[0] = {img : img, boundingBox : new paper.Rectangle(bg.boundingBox[1], bg.boundingBox[2], bg.boundingBox[3], bg.boundingBox[4])};212}213for(var j = 1; j < editorRoomsList[i].items.length; j++)214{215editorRoomsList[i].items[j].setLayer = setItemLayer;216editorRoomsList[i].items[j].getCurrentFrame = getItemCurrentFrame;217for(var key in editorRoomsList[i].items[j].defaultAnims)218219//if(animsDefined)220//{221if (editorRoomsList[i].zOrderMap[editorRoomsList[i].items[j].layer] == undefined)222editorRoomsList[i].zOrderMap[editorRoomsList[i].items[j].layer] = [];223editorRoomsList[i].zOrderMap[editorRoomsList[i].items[j].layer].push(editorRoomsList[i].items[j].id);224//}225editorMapIdItem[editorRoomsList[i].items[j].id] = editorRoomsList[i].items[j];226view_CreateEditorItemPanel(editorRoomsList[i].items[j].id, editorRoomsList[i].id);227editorItemsCount++;228229var hotspot = editorRoomsList[i].items[j].hotspot;230231if(hotspot != null)232{233var hotspotCopy = new Polygon();234for (var k = 0; k < hotspot.points.length; k++)235hotspotCopy.points[k] = new paper.Point(hotspot.points[k][1], hotspot.points[k][2]);236if(hotspot.closed)237hotspotCopy.close();238editorRoomsList[i].items[j].hotspot = hotspotCopy;239}240}241for(var j = 0; j < editorRoomsList[i].walkBehinds.length; j++)242{243var wb = editorRoomsList[i].walkBehinds[j];244editorMapIdWb[wb.id] = wb;245for(var k = 0; k < wb.poly.points.length; k++)246wb.poly.points[k] = new paper.Point(wb.poly.points[k][1], wb.poly.points[k][2]);247view_CreateEditorWBPanel(wb.id, editorRoomsList[i].id);248editorWbCount++;249}250for(var j in editorRoomsList[i].walkBoxes)251{252var wbox = editorRoomsList[i].walkBoxes[j];253var polyCopy = new Polygon();254for(var k = 0; k < wbox.polygon.points.length; k++)255polyCopy.points[k] = new paper.Point(wbox.polygon.points[k][1], wbox.polygon.points[k][2]);256if(wbox.polygon.closed)257polyCopy.close();258/*for(var k = 0; k < wbox.polygon.edges.length; k++)259{260polyCopy.edges[k] = [ new paper.Point(wbox.polygon.edges[k][0][1], wbox.polygon.edges[k][0][2]),261new paper.Point(wbox.polygon.edges[k][1][1], wbox.polygon.edges[k][1][2])];262}*/263editorRoomsList[i].walkBoxes[j].polygon = polyCopy;264view_CreateEditorWalkBoxPanel(wbox.id, editorRoomsList[i].id);265//editorWbCount++;266}267}268269for(var i = 0; i < editorInvItemList.length; i++)270{271editorMapIdInvItem[editorInvItemList[i].id] = editorInvItemList[i];272view_CreateNewInvItemPanel(editorInvItemList[i].id);273editorInvItemCount++;274}275276for(var key in editorScriptList)277{278//$('#datalist-game-scripts').append($(document.createElement('option')).append(key));279view_UpdateDataLists('add', 'scripts', key);280}281282for(var key in editorMapIdDialog)283{284var d = new Dialog();285d.copy(editorMapIdDialog[key]);286editorMapIdDialog[key] = d;287editorDialogsCount++;288view_CreateNewDialogPanel(d.id);289}290291view_LoadGameVariables();292293$('#game-resolution')[0].value = resolution.width + 'x' + resolution.height;294$('#game-resolution').change();295296for(var key in editorMapIdAudio)297{298view_AddNewSound(editorMapIdAudio[key].audioData, editorMapIdAudio[key].id);299}300};301302fileReader.readAsText(event.target.files[0]);303};304305var eliminateCycles = function(data, dataType)306{307switch(dataType)308{309case 'room':310if(data.items[0] != null)311data.items[0].img = data.items[0].img.src;312break;313case 'character':314if(data.sprite != null)315data.sprite.img = data.sprite.img.src;316for(var i in data.sprites)317for(var j in data.sprites[i])318if(data.sprites[i][j])319data.sprites[i][j].img = data.sprites[i][j].img.src;320else delete data.sprites[i][j];321break;322case 'anim':323for(var i = 0; i < data.frames.length; i++)324if(data.frames[i])325data.frames[i].img = data.frames[i].img.src;326break;327}328};329330var download = function (filename, text)331{332var blob = new Blob([text], {type : 'text/html'});333var pom = document.createElement('a');334//pom.setAttribute('href', 'data:application/json;charset=utf-8,' + encodeURIComponent(text));335pom.setAttribute('href', URL.createObjectURL(blob));336pom.setAttribute('download', filename);337pom.click();338};339340