more refactoring

remove code duplication
remove magic constant
add new constant
add LICENSE.meta
This commit is contained in:
aliaksei.kalosha
2025-03-18 12:12:39 +01:00
parent e2a9c13fe5
commit f89ed71bd4
4 changed files with 61 additions and 58 deletions

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using SplashEdit.RuntimeCode; using SplashEdit.RuntimeCode;
using Unity.Collections; using Unity.Collections;
using UnityEditor; using UnityEditor;
@@ -13,6 +14,7 @@ namespace SplashEdit.EditorCode
{ {
private const int VramWidth = 1024; private const int VramWidth = 1024;
private const int VramHeight = 512; private const int VramHeight = 512;
private static readonly Vector2 MinSize = new Vector2(800, 600);
private List<ProhibitedArea> prohibitedAreas = new List<ProhibitedArea>(); private List<ProhibitedArea> prohibitedAreas = new List<ProhibitedArea>();
private Vector2 scrollPosition; private Vector2 scrollPosition;
private Texture2D vramImage; private Texture2D vramImage;
@@ -22,8 +24,6 @@ namespace SplashEdit.EditorCode
private Color bufferColor1 = new Color(1, 0, 0, 0.5f); private Color bufferColor1 = new Color(1, 0, 0, 0.5f);
private Color bufferColor2 = new Color(0, 1, 0, 0.5f); private Color bufferColor2 = new Color(0, 1, 0, 0.5f);
private Color prohibitedColor = new Color(1, 0, 0, 0.3f); private Color prohibitedColor = new Color(1, 0, 0, 0.3f);
private static string _psxDataPath = "Assets/PSXData.asset";
private PSXData _psxData; private PSXData _psxData;
private static readonly Vector2[] resolutions = private static readonly Vector2[] resolutions =
@@ -33,14 +33,15 @@ namespace SplashEdit.EditorCode
new Vector2(368, 240), new Vector2(368, 480), new Vector2(368, 240), new Vector2(368, 480),
new Vector2(512, 240), new Vector2(512, 480), new Vector2(512, 240), new Vector2(512, 480),
new Vector2(640, 240), new Vector2(640, 480) new Vector2(640, 240), new Vector2(640, 480)
}; };
private static string[] resolutionsStrings => resolutions.Select(c => $"{c.x}x{c.y}").ToArray();
[MenuItem("Window/VRAM Editor")] [MenuItem("Window/VRAM Editor")]
public static void ShowWindow() public static void ShowWindow()
{ {
VRAMEditorWindow window = GetWindow<VRAMEditorWindow>("VRAM Editor"); VRAMEditorWindow window = GetWindow<VRAMEditorWindow>("VRAM Editor");
// Set minimum window dimensions. // Set minimum window dimensions.
window.minSize = new Vector2(1600, 600); window.minSize = MinSize;
} }
private void OnEnable() private void OnEnable()
@@ -53,9 +54,9 @@ namespace SplashEdit.EditorCode
blackPixels.Dispose(); blackPixels.Dispose();
// Ensure minimum window size is applied. // Ensure minimum window size is applied.
this.minSize = new Vector2(800, 600); this.minSize = MinSize;
LoadData(); _psxData = Utils.LoadData(out selectedResolution, out dualBuffering, out verticalLayout, out prohibitedAreas);
} }
/// <summary> /// <summary>
@@ -117,9 +118,7 @@ namespace SplashEdit.EditorCode
} }
// Define framebuffer regions based on selected resolution and layout. // Define framebuffer regions based on selected resolution and layout.
Rect buffer1 = new Rect(0, 0, selectedResolution.x, selectedResolution.y); (Rect buffer1, Rect buffer2) = Utils.BufferForResolution(selectedResolution, verticalLayout);
Rect buffer2 = verticalLayout ? new Rect(0, 256, selectedResolution.x, selectedResolution.y)
: new Rect(selectedResolution.x, 0, selectedResolution.x, selectedResolution.y);
List<Rect> framebuffers = new List<Rect> { buffer1 }; List<Rect> framebuffers = new List<Rect> { buffer1 };
if (dualBuffering) if (dualBuffering)
@@ -162,12 +161,11 @@ namespace SplashEdit.EditorCode
GUILayout.Label("VRAM Editor", EditorStyles.boldLabel); GUILayout.Label("VRAM Editor", EditorStyles.boldLabel);
// Dropdown for resolution selection. // Dropdown for resolution selection.
selectedResolution = resolutions[EditorGUILayout.Popup("Resolution", System.Array.IndexOf(resolutions, selectedResolution), selectedResolution = resolutions[EditorGUILayout.Popup("Resolution", System.Array.IndexOf(resolutions, selectedResolution), resolutionsStrings)];
new string[] { "256x240", "256x480", "320x240", "320x480", "368x240", "368x480", "512x240", "512x480", "640x240", "640x480" })];
// Check resolution constraints for dual buffering. // Check resolution constraints for dual buffering.
bool canDBHorizontal = selectedResolution[0] * 2 <= 1024; bool canDBHorizontal = selectedResolution.x * 2 <= VramWidth;
bool canDBVertical = selectedResolution[1] * 2 <= 512; bool canDBVertical = selectedResolution.y * 2 <= VramHeight;
if (canDBHorizontal || canDBVertical) if (canDBHorizontal || canDBVertical)
{ {
@@ -240,9 +238,7 @@ namespace SplashEdit.EditorCode
EditorGUI.DrawPreviewTexture(vramRect, vramImage, null, ScaleMode.ScaleToFit, 0, 0, ColorWriteMask.All); EditorGUI.DrawPreviewTexture(vramRect, vramImage, null, ScaleMode.ScaleToFit, 0, 0, ColorWriteMask.All);
// Draw framebuffer overlays. // Draw framebuffer overlays.
Rect buffer1 = new Rect(vramRect.x, vramRect.y, selectedResolution.x, selectedResolution.y); (Rect buffer1, Rect buffer2) = Utils.BufferForResolution(selectedResolution, verticalLayout, vramRect.min);
Rect buffer2 = verticalLayout ? new Rect(vramRect.x, 256, selectedResolution.x, selectedResolution.y)
: new Rect(vramRect.x + selectedResolution.x, vramRect.y, selectedResolution.x, selectedResolution.y);
EditorGUI.DrawRect(buffer1, bufferColor1); EditorGUI.DrawRect(buffer1, bufferColor1);
GUI.Label(new Rect(buffer1.center.x - 40, buffer1.center.y - 10, 120, 20), "Framebuffer A", EditorStyles.boldLabel); GUI.Label(new Rect(buffer1.center.x - 40, buffer1.center.y - 10, 120, 20), "Framebuffer A", EditorStyles.boldLabel);
@@ -263,25 +259,6 @@ namespace SplashEdit.EditorCode
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
} }
/// <summary>
/// Loads stored PSX data from the asset.
/// </summary>
private void LoadData()
{
_psxData = AssetDatabase.LoadAssetAtPath<PSXData>(_psxDataPath);
if (!_psxData)
{
_psxData = CreateInstance<PSXData>();
AssetDatabase.CreateAsset(_psxData, _psxDataPath);
AssetDatabase.SaveAssets();
}
selectedResolution = _psxData.OutputResolution;
dualBuffering = _psxData.DualBuffering;
verticalLayout = _psxData.VerticalBuffering;
prohibitedAreas = _psxData.ProhibitedAreas;
}
/// <summary> /// <summary>
/// Stores current configuration to the PSX data asset. /// Stores current configuration to the PSX data asset.
/// This is now triggered manually via the "Save Settings" button. /// This is now triggered manually via the "Save Settings" button.

7
LICENSE.meta Normal file
View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: c1679c9d58898f14494d614dfe5f76a6
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -26,7 +26,7 @@ namespace SplashEdit.RuntimeCode
public void Export() public void Export()
{ {
LoadData(); _psxData = Utils.LoadData(out selectedResolution, out dualBuffering, out verticalLayout, out prohibitedAreas);
_exporters = FindObjectsByType<PSXObjectExporter>(FindObjectsSortMode.None); _exporters = FindObjectsByType<PSXObjectExporter>(FindObjectsSortMode.None);
foreach (PSXObjectExporter exp in _exporters) foreach (PSXObjectExporter exp in _exporters)
{ {
@@ -39,10 +39,7 @@ namespace SplashEdit.RuntimeCode
void PackTextures() void PackTextures()
{ {
(Rect buffer1, Rect buffer2) = Utils.BufferForResolution(selectedResolution, verticalLayout);
Rect buffer1 = new Rect(0, 0, selectedResolution.x, selectedResolution.y);
Rect buffer2 = verticalLayout ? new Rect(0, 256, selectedResolution.x, selectedResolution.y)
: new Rect(selectedResolution.x, 0, selectedResolution.x, selectedResolution.y);
List<Rect> framebuffers = new List<Rect> { buffer1 }; List<Rect> framebuffers = new List<Rect> { buffer1 };
if (dualBuffering) if (dualBuffering)
@@ -111,24 +108,6 @@ namespace SplashEdit.RuntimeCode
Debug.Log(totalFaces); Debug.Log(totalFaces);
} }
public void LoadData()
{
_psxData = AssetDatabase.LoadAssetAtPath<PSXData>(_psxDataPath);
if (!_psxData)
{
_psxData = ScriptableObject.CreateInstance<PSXData>();
AssetDatabase.CreateAsset(_psxData, _psxDataPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
selectedResolution = _psxData.OutputResolution;
dualBuffering = _psxData.DualBuffering;
verticalLayout = _psxData.VerticalBuffering;
prohibitedAreas = _psxData.ProhibitedAreas;
}
void OnDrawGizmos() void OnDrawGizmos()
{ {
Gizmos.DrawIcon(transform.position, "Packages/net.psxsplash.splashedit/Icons/PSXSceneExporter.png", true); Gizmos.DrawIcon(transform.position, "Packages/net.psxsplash.splashedit/Icons/PSXSceneExporter.png", true);

View File

@@ -1,3 +1,5 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine; using UnityEngine;
namespace SplashEdit.RuntimeCode namespace SplashEdit.RuntimeCode
@@ -41,4 +43,42 @@ namespace SplashEdit.RuntimeCode
return new Rect(X, Y, Width, Height); return new Rect(X, Y, Width, Height);
} }
} }
public static class Utils
{
private static string _psxDataPath = "Assets/PSXData.asset";
public static (Rect, Rect) BufferForResolution(Vector2 selectedResolution, bool verticalLayout, Vector2 offset = default)
{
if (offset == default)
{
offset = Vector2.zero;
}
Rect buffer1 = new Rect(offset.x, offset.y, selectedResolution.x, selectedResolution.y);
Rect buffer2 = verticalLayout ? new Rect(offset.x, 256, selectedResolution.x, selectedResolution.y)
: new Rect(offset.x + selectedResolution.x, offset.y, selectedResolution.x, selectedResolution.y);
return (buffer1, buffer2);
}
/// <summary>
/// Loads stored PSX data from the asset.
/// </summary>
public static PSXData LoadData(out Vector2 selectedResolution, out bool dualBuffering, out bool verticalLayout, out List<ProhibitedArea> prohibitedAreas)
{
var _psxData = AssetDatabase.LoadAssetAtPath<PSXData>(_psxDataPath);
if (!_psxData)
{
_psxData = ScriptableObject.CreateInstance<PSXData>();
AssetDatabase.CreateAsset(_psxData, _psxDataPath);
AssetDatabase.SaveAssets();
}
selectedResolution = _psxData.OutputResolution;
dualBuffering = _psxData.DualBuffering;
verticalLayout = _psxData.VerticalBuffering;
prohibitedAreas = _psxData.ProhibitedAreas;
return _psxData;
}
}
} }