Skip to content

Commit

Permalink
Virtualized grid view (#9)
Browse files Browse the repository at this point in the history
* Added VirtualGridLayoutGroup
* Display app version
  • Loading branch information
rubenwe authored Apr 18, 2020
1 parent d89c4ca commit c178bae
Show file tree
Hide file tree
Showing 55 changed files with 1,106 additions and 842 deletions.
955 changes: 566 additions & 389 deletions Assets/Scenes/MainScene.unity

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions Assets/Scripts/Config/ApplicationSettings.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using StlVault.Util;
using StlVault.Util.Logging;
using StlVault.Util.Logging;

namespace StlVault.Config
{
internal class ApplicationSettings
{
public ushort UiScalePercent { get; set; } = 100;
public LogLevel LogLevel { get; set; } = LogLevel.Info;
public ushort ImportParallelism { get; set; } = 2;
public LogLevel LogLevel { get; set; } = LogLevel.Trace;
public ushort ImportParallelism { get; set; } = 1;
public ushort PreviewJpegQuality { get; set; } = 70;
public ushort PreviewResolution { get; set; } = 10;
public ushort ScrollSensitivity { get; set; } = 150;
Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/Config/ImportFolderConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using StlVault.Services;

namespace StlVault.Config
{
Expand Down
4 changes: 0 additions & 4 deletions Assets/Scripts/Config/PreviewInfo.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using JetBrains.Annotations;
using Newtonsoft.Json;
using StlVault.Services;
using UnityEngine.Scripting;

namespace StlVault.Config
{
Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/Services/AppDataPreviewImageStore.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Threading.Tasks;
using StlVault.Config;
using StlVault.Util.Logging;

namespace StlVault.Services
Expand Down
5 changes: 1 addition & 4 deletions Assets/Scripts/Services/IKnownItemStore.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace StlVault.Services
namespace StlVault.Services
{

}
3 changes: 0 additions & 3 deletions Assets/Scripts/Services/ILibrary.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using StlVault.Config;
using StlVault.Util;
using StlVault.Util.Collections;
using StlVault.ViewModels;
using UnityEngine;

Expand Down
3 changes: 1 addition & 2 deletions Assets/Scripts/Services/IPreviewBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading.Tasks;
using UnityEngine;

namespace StlVault.Services
Expand Down
3 changes: 1 addition & 2 deletions Assets/Scripts/Services/IPreviewImageStore.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;

namespace StlVault.Services
{
Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/Services/IPreviewList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using StlVault.Config;
using StlVault.Util.Collections;
using StlVault.ViewModels;

Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/Services/ITagIndex.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;

namespace StlVault.Services
{
Expand Down
2 changes: 0 additions & 2 deletions Assets/Scripts/Services/PreviewList.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using StlVault.Config;
using StlVault.Util.Collections;
using StlVault.ViewModels;

Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/Util/Collections/IReadOnlyObservableList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using StlVault.Config;

namespace StlVault.Util.Collections
{
Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/Util/Collections/ObservableList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using static System.Collections.Specialized.NotifyCollectionChangedAction;
using NotifyCollectionChangedAction = System.Collections.Specialized.NotifyCollectionChangedAction;
Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/Util/Collections/TrackingCollection.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using JetBrains.Annotations;
Expand Down
20 changes: 17 additions & 3 deletions Assets/Scripts/Util/Stl/BinaryStl.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using StlVault.Util.Logging;
using ILogger = StlVault.Util.Logging.ILogger;

namespace StlVault.Util.Stl
{
internal static class BinaryStl
{
private static readonly ILogger Logger = UnityLogger.Instance;

/// <summary>
/// Determine whether this file is a binary stl format or not.
/// </summary>
Expand Down Expand Up @@ -49,16 +54,25 @@ public static bool IsBinary(byte[] fileBytes)
public static Facet[] FromBytes(byte[] fileBytes)
{
// Discard header
var facetCount = BitConverter.ToUInt32(fileBytes, 80);
var facets = new Facet[facetCount];
var dataFacetCount = BitConverter.ToUInt32(fileBytes, 80);
var calculatedCount = (uint) ((fileBytes.LongLength - 84) / 50);

if (dataFacetCount > calculatedCount)
throw new InvalidDataException("The facet count specified in the STL file is too big for the file!");

if (dataFacetCount != calculatedCount)
Logger.Warn("Calculated facet count and the one in STL file don't match up!");

var usedCount = Math.Min(dataFacetCount, calculatedCount);
var facets = new Facet[usedCount];

unsafe
{
fixed (byte* fileStart = fileBytes)
fixed (Facet* destination = facets)
{
var source = fileStart + 84;
Unsafe.CopyBlockUnaligned(destination, source, facetCount * 50);
Unsafe.CopyBlockUnaligned(destination, source, usedCount * 50);
}
}

Expand Down
32 changes: 23 additions & 9 deletions Assets/Scripts/Util/Stl/StlImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Security.Cryptography;
using System.Threading.Tasks;
using StlVault.Util.Unity;
using Unity.Collections;
using UnityEngine;
using UnityEngine.Rendering;

Expand Down Expand Up @@ -64,20 +65,34 @@ private static async Task<Mesh> CreateMeshFromFacetsAsync(Facet[] facets, bool c
var mesh = new Mesh
{
name = fileName,
indexFormat = IndexFormat.UInt32,
vertices = vertices,
normals = normals,
triangles = triangles,
hideFlags = HideFlags.HideAndDontSave
};
var vertexCount = facets.Length * 3;
mesh.SetVertexBufferParams(
vertexCount,
new VertexAttributeDescriptor(VertexAttribute.Position, stream: 0),
new VertexAttributeDescriptor(VertexAttribute.Normal, stream: 1)
);
mesh.SetVertexBufferData(vertices, 0, 0, vertexCount, stream:0);
mesh.SetVertexBufferData(normals, 0, 0, vertexCount, stream:1);
mesh.SetIndexBufferParams(vertexCount, IndexFormat.UInt32);
mesh.SetIndexBufferData(triangles, 0, 0, vertexCount);
mesh.SetSubMesh(0, new SubMeshDescriptor(0, vertexCount));
mesh.RecalculateBounds();
if (centerVertices)
{
var currentCenter = mesh.bounds.center;
await Task.Run(() => CenterVertices(vertices, currentCenter))
.Timed("Centering vertices of {0}", fileName);
mesh.vertices = vertices;
mesh.SetVertexBufferData(vertices, 0, 0, vertexCount, stream:0);
mesh.RecalculateBounds();
}
Expand Down Expand Up @@ -105,11 +120,10 @@ private static void CenterVertices(Vector3[] vertices, Vector3 correction)
private static (Vector3[] vertices, Vector3[] normals, int[] triangles) BuildMesh(Facet[] facets)
{
var meshSize = facets.Length * 3;

var triangles = new int[meshSize];
var vertices = new Vector3[meshSize];
var normals = new Vector3[meshSize];

var triangles = new int[meshSize];

void WriteFacet(int currentFacet)
{
var i = currentFacet * 3;
Expand Down
7 changes: 4 additions & 3 deletions Assets/Scripts/Util/Unity/AdaptiveTopPadding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

namespace StlVault.Util.Unity
{
[RequireComponent(typeof(GridLayoutGroup))]
[RequireComponent(typeof(RectTransform))]
public class AdaptiveTopPadding : MonoBehaviour
{
[SerializeField] private int _basePadding;
[SerializeField] private RectTransform _paddingDriver;
private GridLayoutGroup _layoutGroup;

private LayoutGroup _layoutGroup;
private RectTransform _rectTransform;

private void Start()
{
_layoutGroup = GetComponent<GridLayoutGroup>();
_layoutGroup = GetComponent<LayoutGroup>();
_rectTransform = GetComponent<RectTransform>();
}

Expand All @@ -25,6 +25,7 @@ public void Update()
var newValue = _basePadding + (int) _paddingDriver.rect.height;
if (_layoutGroup.padding.top == newValue) return;
_layoutGroup.padding.top = newValue;

LayoutRebuilder.MarkLayoutForRebuild(_rectTransform);
}
}
Expand Down
17 changes: 17 additions & 0 deletions Assets/Scripts/Util/Unity/AppVersionDisplay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using TMPro;
using UnityEngine;

#pragma warning disable 0649

namespace StlVault.Util.Unity
{
public class AppVersionDisplay : MonoBehaviour
{
[SerializeField] private TMP_Text _text;

private void Start()
{
_text.text = "v" + Application.version;
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Util/Unity/AppVersionDisplay.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 0 additions & 34 deletions Assets/Scripts/Util/Unity/GridItemScaler.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/Scripts/Util/Unity/GridItemScaler.cs.meta

This file was deleted.

2 changes: 0 additions & 2 deletions Assets/Scripts/Util/Unity/GuiCallbackQueue.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
using UnityEngine;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using StlVault.Util.Collections;
using StlVault.Util.Collections;
using StlVault.ViewModels;
using StlVault.Views;
using UnityEngine;

namespace StlVault.Views
namespace StlVault.Util.Unity
{
internal class ItemsView : ContainerView<ItemsModel, ItemView, ItemPreviewModel>
internal class LibraryView : VirtualGridLayoutGroup<ItemsModel, ItemView, ItemPreviewModel>
{
protected override IReadOnlyObservableList<ItemPreviewModel> ChildModels => ViewModel.Items;

private void Update()
protected override IReadOnlyObservableList<ItemPreviewModel> ChildModels => ViewModel?.Items;
protected override void Update()
{
if (ViewModel == null) return;

var ctrl = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
if (ctrl && Input.GetKeyDown(KeyCode.A))
{
Expand All @@ -23,6 +26,8 @@ private void Update()

var shift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
ViewModel.SelectRange = shift;

base.Update();
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Util/Unity/LibraryView.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c178bae

Please sign in to comment.