pixelgon/Assets/Scripts/AssetSpace.cs

297 lines
No EOL
7.4 KiB
C#

using UnityEngine;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.Collections.Generic;
using SerializableTypes;
[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)
{
string destination = folderPath + "/" + name + ".pixelgon";
FileStream file;
if (File.Exists(destination)) file = File.OpenWrite(destination);
else file = File.Create(destination);
BinaryFormatter bf = new BinaryFormatter();
FPixelgon fPixelgon = new FPixelgon();
fPixelgon.vectors = new SVector3[pixelgon.vectors.Count];
for (int i = 0; i < pixelgon.vectors.Count; i++)
{
SVector3 vec3 = pixelgon.vectors[i];
fPixelgon.vectors[i] = vec3;
}
bf.Serialize(file, fPixelgon);
file.Close();
}
public Pixelgon Load()
{
string destination = folderPath + "/" + name + ".pixelgon";
FileStream file;
if (File.Exists(destination)) file = File.OpenRead(destination);
else
{
Debug.LogError("File not found");
return null;
}
BinaryFormatter bf = new BinaryFormatter();
FPixelgon fPixelgon = (FPixelgon)bf.Deserialize(file);
Pixelgon pixelgon = new Pixelgon();
for (int i = 0; i < fPixelgon.vectors.Length; i++)
{
Vector3 vec3 = fPixelgon.vectors[i];
pixelgon.vectors.Add(vec3);
}
file.Close();
return pixelgon;
}
}
[Serializable]
public class Asset
{
public Mesh mesh;
public Vector3 pos;
public Quaternion rot;
}
[Serializable]
public class FAssetSpace
{
// public class Placement
// {
// public SerializableVector3 pos
// }
}
[Serializable]
public class FPixelgon
{
public SVector3[] vectors;
}
namespace SerializableTypes
{
/// <summary> Serializable version of UnityEngine.Vector3. </summary>
[Serializable]
public struct SVector3
{
public float x;
public float y;
public float z;
public SVector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public override string ToString()
=> $"[x, y, z]";
public static implicit operator Vector3(SVector3 s)
=> new Vector3(s.x, s.y, s.z);
public static implicit operator SVector3(Vector3 v)
=> new SVector3(v.x, v.y, v.z);
public static SVector3 operator +(SVector3 a, SVector3 b)
=> new SVector3(a.x + b.x, a.y + b.y, a.z + b.z);
public static SVector3 operator -(SVector3 a, SVector3 b)
=> new SVector3(a.x - b.x, a.y - b.y, a.z - b.z);
public static SVector3 operator -(SVector3 a)
=> new SVector3(-a.x, -a.y, -a.z);
public static SVector3 operator *(SVector3 a, float m)
=> new SVector3(a.x * m, a.y * m, a.z * m);
public static SVector3 operator *(float m, SVector3 a)
=> new SVector3(a.x * m, a.y * m, a.z * m);
public static SVector3 operator /(SVector3 a, float d)
=> new SVector3(a.x / d, a.y / d, a.z / d);
}
/// <summary> Serializable version of UnityEngine.Quaternion. </summary>
[Serializable]
public struct SQuaternion
{
public float x;
public float y;
public float z;
public float w;
public SQuaternion(float x, float y, float z, float w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public override string ToString()
=> $"[{x}, {y}, {z}, {w}]";
public static implicit operator Quaternion(SQuaternion s)
=> new Quaternion(s.x, s.y, s.z, s.w);
public static implicit operator SQuaternion(Quaternion q)
=> new SQuaternion(q.x, q.y, q.z, q.w);
}
/// <summary> Serializable version of UnityEngine.Color32 without transparency. </summary>
[Serializable]
public struct SColor32
{
public byte r;
public byte g;
public byte b;
public SColor32(byte r, byte g, byte b)
{
this.r = r;
this.g = g;
this.b = b;
}
public SColor32(Color32 c)
{
r = c.r;
g = c.g;
b = c.b;
}
public override string ToString()
=> $"[{r}, {g}, {b}]";
public static implicit operator Color32(SColor32 rValue)
=> new Color32(rValue.r, rValue.g, rValue.b, a: byte.MaxValue);
public static implicit operator SColor32(Color32 rValue)
=> new SColor32(rValue.r, rValue.g, rValue.b);
}
}