pixelgon/Assets/Scripts/AssetSpace.cs
2020-07-14 16:06:31 -07:00

189 lines
No EOL
4.7 KiB
C#

using UnityEngine;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using SimpleJSON;
[Serializable]
public class AssetSpace
{
public string folderPath = "C:/dofdev/Pixelgon/Assets/AssetSpace";
public string name = "draft";
public List<string> loadedNames = new List<string>();
public Dictionary<string, Asset> assets = new Dictionary<string, Asset>();
public void Init(PolyVector polyVector)
{
// Update Mesh Dictionary
// loop through files in folder
loadedNames.Clear();
assets.Clear();
string[] filePaths = Directory.GetFiles(folderPath, "*.pixelgon", SearchOption.TopDirectoryOnly);
for (int i = 0; i < filePaths.Length; i++)
{
name = Path.GetFileNameWithoutExtension(filePaths[i]);
loadedNames.Add(name);
Pixelgon pgon = Load();
Mesh mesh = new Mesh();
mesh = polyVector.ToMesh(pgon.vectors);
Asset asset = new Asset();
asset.pos = Vector3.zero;
asset.rot = Quaternion.identity;
asset.mesh = mesh;
assets.Add(name, asset);
}
// load draft.space file
}
string held = null;
Vector3 grabOffset;
// Vector3 oldCursorPos;
Quaternion oldOffConRot;
public void Frame(Input input, Render render)
{
Vector3 cursorPos = input.stretchCursor;
// instead of a rigid uniform list
// make it a space where you can lay things out
float closest = 1000f;
for (int i = 0; i < loadedNames.Count; i++)
{
Asset asset = assets[loadedNames[i]];
Graphics.DrawMesh(asset.mesh,
asset.pos, asset.rot,
render.polyVector.matVector, 0
);
if (input.mainCon.trigger.down)
{
float reachDist = Vector3.Distance(asset.pos, cursorPos);
if (reachDist < closest)
{
held = loadedNames[i];
closest = reachDist;
grabOffset = asset.pos - cursorPos;
}
}
}
if (input.mainCon.trigger.held)
{
if (held != null)
{
if (input.mainCon.one.up)
{
grabOffset = assets[held].pos - cursorPos;
}
if (input.mainCon.one.held)
{
Quaternion rotDelta = input.offCon.physical.Rot() * Quaternion.Inverse(oldOffConRot);
assets[held].rot = rotDelta * assets[held].rot;
// assets[held].rot *= Quaternion.LookRotation(cursorPos - assets[held].pos) *
// Quaternion.Inverse(Quaternion.LookRotation(oldCursorPos - assets[held].pos));
}
else
{
assets[held].pos = cursorPos + grabOffset;
}
}
}
else
{
held = null;
}
oldOffConRot = input.offCon.physical.Rot();
// oldCursorPos = cursorPos;
// where to store persistent position and rotation?
// needs to be saved in the .pixelgon file then
// can assetspace have its own save file instead?
// store the name pos and rotation ++animationPath
// Later you can have multiple persistent spaces
// stream lining features *just manually do it in the unity inspector for now
// and switch to editing a certain object
// buttons in the oriel?
// set name
// asset names (no renaming just save new or over, manually delete the deprecated)
// save
// load
// recordable motion paths
// control over time
}
public void Save(Pixelgon pixelgon)
{
// Cleanup Empty Vectors and re-index quads
int dup = 0;
for (int i = pixelgon.vectors.Count - 1; i >= 0; i--)
{
if (pixelgon.vectors[i] == Vector3.zero)
{
if (dup++ > 0)
{
for (int j = 0; j < pixelgon.quads.Count; j++)
{
if (pixelgon.quads[j] >= i)
{
pixelgon.quads[j]--;
}
}
pixelgon.vectors.RemoveAt(i);
}
}
else
{
dup = 0;
}
}
string destination = folderPath + "/" + name + ".json";
File.WriteAllText(destination, JsonUtility.ToJson(pixelgon));
}
public Pixelgon Load()
{
string destination = folderPath + "/" + name + ".json";
if (!File.Exists(destination))
{
Debug.LogError("File not found");
return null;
}
JSONNode N = JSON.Parse(File.ReadAllText(destination));
Pixelgon pixelgon = new Pixelgon();
pixelgon.vectors.Clear();
for (int i = 0; i < N["vectors"].Count; i++)
{
pixelgon.vectors.Add(
new Vector3(N["vectors"][i][0].AsFloat, N["vectors"][i][1].AsFloat, N["vectors"][i][2].AsFloat)
);
}
pixelgon.quads.Clear();
for (int i = 0; i < N["quads"].Count; i++)
{
pixelgon.quads.Add(N["quads"][i].AsInt);
}
return pixelgon;
}
}
[Serializable]
public class Asset
{
public Mesh mesh;
public Vector3 pos;
public Quaternion rot;
}