Skip to content

Commit

Permalink
Merge pull request #84 from tangrams/dynamic-reloading-rb
Browse files Browse the repository at this point in the history
Dynamic reloading
  • Loading branch information
matteblair authored Dec 6, 2017
2 parents e29d2af + 3c37408 commit 8e257e2
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 111 deletions.
49 changes: 48 additions & 1 deletion Assets/Mapzen/Unity/Editor/MapStyleEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Mapzen.Unity.Editor
[CustomEditor(typeof(MapStyle))]
public class MapStyleEditor : UnityEditor.Editor
{
[SerializeField]
TreeViewState layerTreeViewState;

FeatureLayerTreeView layerTreeView;
Expand Down Expand Up @@ -41,6 +40,13 @@ public override void OnInspectorGUI()
return;
}

GUILayout.Label("Editing options", labelBoldStyle);

var liveUpdateProperty = serializedObject.FindProperty("liveUpdateEnabled");
EditorGUILayout.PropertyField(liveUpdateProperty, new GUIContent { text = "Update RegionMap while editing" });

GUILayout.Space(EditorGUIUtility.singleLineHeight);

GUILayout.Label("Layers", labelBoldStyle);

layerTreeView.Layers = mapStyle.Layers;
Expand Down Expand Up @@ -91,6 +97,47 @@ public override void OnInspectorGUI()
}

serializedObject.ApplyModifiedProperties();

if (liveUpdateProperty.boolValue)
{
// Find the regionMap containing the style mapStyle
var regionMaps = GameObject.FindObjectsOfType<RegionMap>();
RegionMap map = null;
foreach (var regionMap in regionMaps)
{
var style = regionMap.Styles.Find(s => s == mapStyle);
if (style != null)
{
map = regionMap;
break;
}
}

if (map != null)
{
if (GUI.changed)
{
map.LogWarnings();
if (map.IsValid())
{
map.DownloadTilesAsync();
}
else
{
map.LogErrors();
}
}

if (map.HasPendingTasks())
{
Repaint();
if (map.FinishedRunningTasks())
{
map.GenerateSceneGraph();
}
}
}
}
}

void DrawSelectedLayer(SerializedProperty layerProperty)
Expand Down
53 changes: 4 additions & 49 deletions Assets/Mapzen/Unity/Editor/RegionMapEditor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using UnityEditor;

Expand Down Expand Up @@ -41,23 +40,23 @@ public override void OnInspectorGUI()

EditorGUILayout.PropertyField(serializedObject.FindProperty("Styles"), true);

bool valid = IsValid();
bool valid = map.IsValid();

EditorConfig.SetColor(valid ?
EditorConfig.DownloadButtonEnabledColor :
EditorConfig.DownloadButtonDisabledColor);

if (GUILayout.Button("Download"))
{
map.LogWarnings();

if (valid)
{
LogWarnings();

map.DownloadTilesAsync();
}
else
{
LogErrors();
map.LogErrors();
}
}

Expand All @@ -76,49 +75,5 @@ public override void OnInspectorGUI()

serializedObject.ApplyModifiedProperties();
}

private bool IsValid()
{
bool hasStyle = map.Styles.Any(style => style != null);
return map.RegionName.Length > 0 && hasStyle;
}

private void LogWarnings()
{
foreach (var style in map.Styles)
{
if (style == null)
{
Debug.LogWarning("'Null' style provided in feature styling collection");
continue;
}

if (style.Layers.Count == 0)
{
Debug.LogWarning("The style " + style.name + " has no filter");
}

foreach (var filterStyle in style.Layers)
{
if (filterStyle.GetFilter().CollectionNameSet.Count == 0)
{
Debug.LogWarning("The style " + style.name + " has a filter selecting no layer");
}
}
}
}

private void LogErrors()
{
if (map.RegionName.Length == 0)
{
Debug.LogError("Make sure to give a region name");
}

if (!map.Styles.Any(style => style != null))
{
Debug.LogError("Make sure to create at least one style");
}
}
}
}
3 changes: 3 additions & 0 deletions Assets/Mapzen/Unity/MapStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class MapStyle : ScriptableObject
{
public List<FeatureLayer> Layers;

[SerializeField]
private bool liveUpdateEnabled;

public MapStyle()
{
this.Layers = new List<FeatureLayer>();
Expand Down
10 changes: 5 additions & 5 deletions Assets/Mapzen/Unity/SceneGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class SceneGraph
// Merged mesh data for each game object group
private Dictionary<GameObject, MeshData> gameObjectMeshData;
// The root game object
private GameObject mapRegion;
private GameObject regionMap;
// Group options for grouping
private SceneGroupType groupOptions;
// The leaf group of the hierarchy
Expand All @@ -21,10 +21,10 @@ public class SceneGraph
// The list of feature mesh generated by the tile task
private List<FeatureMesh> features;

public SceneGraph(GameObject mapRegion, SceneGroupType groupOptions, GameObjectOptions gameObjectOptions, List<FeatureMesh> features)
public SceneGraph(GameObject regionMap, SceneGroupType groupOptions, GameObjectOptions gameObjectOptions, List<FeatureMesh> features)
{
this.gameObjectMeshData = new Dictionary<GameObject, MeshData>();
this.mapRegion = mapRegion;
this.regionMap = regionMap;
this.groupOptions = groupOptions;
this.gameObjectOptions = gameObjectOptions;
this.features = features;
Expand Down Expand Up @@ -83,7 +83,7 @@ public void Generate()
// Merge every game object created to the 'root' element being the map region
foreach (var featureMesh in features)
{
MergeMeshData(mapRegion, featureMesh);
MergeMeshData(regionMap, featureMesh);
}
}
else
Expand All @@ -93,7 +93,7 @@ public void Generate()
// Generate all game object with the appropriate hiarchy
foreach (var featureMesh in features)
{
currentGroup = mapRegion;
currentGroup = regionMap;

foreach (SceneGroupType group in Enum.GetValues(typeof(SceneGroupType)))
{
Expand Down
39 changes: 15 additions & 24 deletions Assets/Mapzen/Unity/TileTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
using Mapzen;
using Mapzen.Unity;
using Mapzen.VectorData;
using Mapzen.VectorData.Formats;
using Mapzen.VectorData.Filters;
using UnityEngine;

public class TileTask
{
// The tile address this task is working on
private TileAddress address;
private byte[] tileData;
private bool ready;
private SceneGroupType groupOptions;
private float inverseTileScale;
// The transform applied to the geometry built the tile task builders
private Matrix4x4 transform;
// The generation of this tile task
private int generation;
// The resulting data of the tile task is stored in this container
private List<FeatureMesh> data;
// The map styling this tile task is working on
private List<MapStyle> featureStyling;
private int generation;

public int Generation
{
Expand All @@ -28,30 +28,23 @@ public List<FeatureMesh> Data
get { return data; }
}

public bool Ready
{
get { return ready; }
}

public TileTask(List<MapStyle> featureStyling, TileAddress address, Matrix4x4 transform, byte[] tileData, int generation)
public TileTask(List<MapStyle> featureStyling, TileAddress address, Matrix4x4 transform, int generation)
{
this.data = new List<FeatureMesh>();
this.address = address;
this.tileData = tileData;
this.transform = transform;
this.ready = false;
this.featureStyling = featureStyling;
this.generation = generation;
this.featureStyling = featureStyling;
}

public void Start()
/// <summary>
/// Runs the tile task, resulting data will be stored in Data.
/// </summary>
/// <param name="featureCollections">The feature collections this tile task will be building.</param>
public void Start(IEnumerable<FeatureCollection> featureCollections)
{
float inverseTileScale = 1.0f / (float)address.GetSizeMercatorMeters();

// TODO: Reuse tile parsing data
// var tileData = new GeoJsonTile(address, response);
var mvtTile = new MvtTile(address, tileData);

foreach (var style in featureStyling)
{
if (style == null)
Expand All @@ -61,9 +54,8 @@ public void Start()

foreach (var styleLayer in style.Layers)
{
foreach (var collection in mvtTile.FeatureCollections)
foreach (var collection in featureCollections)
{

foreach (var feature in styleLayer.GetFilter().Filter(collection))
{
var layerStyle = styleLayer.Style;
Expand All @@ -75,6 +67,7 @@ public void Start()
featureName += identifier.ToString();
}

// Resulting data for this feature.
FeatureMesh featureMesh = new FeatureMesh(address.ToString(), collection.Name, styleLayer.Name, featureName);

IGeometryHandler handler = null;
Expand Down Expand Up @@ -108,7 +101,5 @@ public void Start()
}
}
}

ready = true;
}
}
Loading

0 comments on commit 8e257e2

Please sign in to comment.