Skip to content

Commit 62ffdb5

Browse files
authored
Add different units to scale the world to (#60)
* Add different units to scale the world to This defines how to interpret 1 unity unit. * renamed regionScaleValue to regionScaledValue * Address comments
1 parent 70b1d0e commit 62ffdb5

File tree

4 files changed

+124
-2
lines changed

4 files changed

+124
-2
lines changed

Assets/Editor/MapzenMapEditor.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class MapzenMapEditor : Editor
99
{
1010
private MapzenMap mapzenMap;
1111
private bool showTileDataFoldout = false;
12+
private bool showRegionScaleRatioFoldout = false;
1213

1314
void OnEnable()
1415
{
@@ -21,6 +22,8 @@ public override void OnInspectorGUI()
2122

2223
TileDataFoldout();
2324

25+
RegionScaleRatioFoldout();
26+
2427
base.OnInspectorGUI();
2528

2629
bool valid = IsValid();
@@ -83,16 +86,50 @@ private void TileDataFoldout()
8386
EditorGUI.indentLevel--;
8487
}
8588

89+
private void UnitScaleToggle(MapzenMap mapzenMap, RegionScaleUnits.Units unit)
90+
{
91+
bool isSet = RegionScaleUnits.Test(unit, mapzenMap.RegionScaleUnit);
92+
isSet = EditorGUILayout.Toggle(unit.ToString(), isSet);
93+
94+
mapzenMap.RegionScaleUnit = isSet ? unit : mapzenMap.RegionScaleUnit;
95+
}
96+
97+
private void RegionScaleRatioFoldout()
98+
{
99+
showRegionScaleRatioFoldout = EditorGUILayout.Foldout(showRegionScaleRatioFoldout, "Region Scale Ratio");
100+
if (!showRegionScaleRatioFoldout)
101+
{
102+
return;
103+
}
104+
105+
mapzenMap.RegionScaleRatio = EditorGUILayout.FloatField("Scale: ", mapzenMap.RegionScaleRatio);
106+
GUILayout.Label("Choose world scale units: ");
107+
EditorGUI.indentLevel++;
108+
109+
EditorGUILayout.BeginHorizontal();
110+
UnitScaleToggle(mapzenMap, RegionScaleUnits.Units.Meters);
111+
UnitScaleToggle(mapzenMap, RegionScaleUnits.Units.KiloMeters);
112+
EditorGUILayout.EndHorizontal();
113+
EditorGUILayout.BeginHorizontal();
114+
UnitScaleToggle(mapzenMap, RegionScaleUnits.Units.Miles);
115+
UnitScaleToggle(mapzenMap, RegionScaleUnits.Units.Feet);
116+
EditorGUILayout.EndHorizontal();
117+
118+
EditorGUI.indentLevel--;
119+
}
120+
86121
private void LoadPreferences()
87122
{
88123
string key = typeof(MapzenMapEditor).Name;
89124
showTileDataFoldout = EditorPrefs.GetBool(key + ".showTileDataFoldout");
125+
showRegionScaleRatioFoldout = EditorPrefs.GetBool(key + ".showRegionScaleRatioFoldout");
90126
}
91127

92128
private void SavePreferences()
93129
{
94130
string key = typeof(MapzenMapEditor).Name;
95131
EditorPrefs.SetBool(key + ".showTileDataFoldout", showTileDataFoldout);
132+
EditorPrefs.SetBool(key + ".showRegionScaleRatioFoldout", showRegionScaleRatioFoldout);
96133
}
97134

98135
private bool IsValid()

Assets/Mapzen/RegionScaleUnits.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
3+
namespace Mapzen
4+
{
5+
public class RegionScaleUnits
6+
{
7+
[Flags]
8+
public enum Units
9+
{
10+
Meters = 1 << 0,
11+
KiloMeters = 1 << 1,
12+
Miles = 1 << 2,
13+
Feet = 1 << 3,
14+
}
15+
16+
// A name identifer
17+
public string name;
18+
public Units unit;
19+
20+
/// <summary>
21+
/// Tests whether this unit is enabled
22+
/// </summary>
23+
/// <param name="unit">The unit to check.</param>
24+
/// <param name="regionUnit">Unit defined for the region.</param>
25+
public static bool Test(Units unit, Units regionUnit)
26+
{
27+
return ((int)unit & (int)regionUnit) == (int)unit;
28+
}
29+
30+
public RegionScaleUnits(Units u, string n)
31+
{
32+
this.unit = u;
33+
this.name = n;
34+
}
35+
}
36+
}
37+

Assets/Mapzen/RegionScaleUnits.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/MapzenMap.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class MapzenMap : MonoBehaviour
1313
{
1414
public GameObjectOptions gameObjectOptions;
1515

16-
public float regionScaleRatio = 1.0f;
16+
private float regionScaleRatio = 1.0f;
1717

1818
public string ApiKey = "vector-tiles-tyHL4AY";
1919

@@ -35,6 +35,10 @@ public class MapzenMap : MonoBehaviour
3535
[SerializeField]
3636
private SceneGroup.Type groupOptions;
3737

38+
[HideInInspector]
39+
[SerializeField]
40+
private RegionScaleUnits.Units regionScaleUnit = RegionScaleUnits.Units.Meters;
41+
3842
private List<TileTask> tasks = new List<TileTask>();
3943

4044
private int nTasksForArea = 0;
@@ -78,7 +82,28 @@ public void DownloadTiles()
7882
float offsetX = (tileAddress.x - bounds.min.x);
7983
float offsetY = (-tileAddress.y + bounds.min.y);
8084

81-
TileTask task = new TileTask(tileAddress, groupOptions, response.data, offsetX, offsetY, regionScaleRatio);
85+
86+
float unitConverter = 1.0f;
87+
switch (regionScaleUnit)
88+
{
89+
case RegionScaleUnits.Units.Meters:
90+
unitConverter = 1.0f;
91+
break;
92+
case RegionScaleUnits.Units.KiloMeters:
93+
unitConverter = 0.001f;
94+
break;
95+
case RegionScaleUnits.Units.Miles:
96+
unitConverter = 0.00063f;
97+
break;
98+
case RegionScaleUnits.Units.Feet:
99+
unitConverter = 3.28f;
100+
break;
101+
default:
102+
unitConverter = 0.0f;
103+
break;
104+
}
105+
106+
TileTask task = new TileTask(tileAddress, groupOptions, response.data, offsetX, offsetY, regionScaleRatio * unitConverter);
82107

83108
task.Start(featureStyling, regionMap);
84109

@@ -112,9 +137,20 @@ public List<FeatureStyle> FeatureStyling
112137
get { return featureStyling; }
113138
}
114139

140+
public float RegionScaleRatio {
141+
get { return regionScaleRatio; }
142+
set { regionScaleRatio = value; }
143+
}
144+
115145
public SceneGroup.Type GroupOptions
116146
{
117147
get { return groupOptions; }
118148
set { groupOptions = value; }
119149
}
150+
151+
public RegionScaleUnits.Units RegionScaleUnit
152+
{
153+
get { return regionScaleUnit; }
154+
set { regionScaleUnit = value; }
155+
}
120156
}

0 commit comments

Comments
 (0)