From 1920b49e47e0df3e1ef008e28b76906b4eec5532 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 2 Feb 2016 00:21:21 +0600 Subject: [PATCH] undo-redo for create tiled map and property holder --- Assembly-CSharp.csproj | 1 + .../Internal/Editor/IsoObjectEditor.cs | 8 +- Assets/IsoTools/Scripts/IsoObject.cs | 34 ++-- Assets/IsoTools/Scripts/IsoWorld.cs | 42 ++--- .../Tiled/Examples/Scenes/TiledScene.unity | 2 +- .../Internal/Editor/TiledMapAssetEditor.cs | 1 + Assets/IsoTools/Tiled/TiledMap.cs | 11 +- Assets/IsoTools/Tiled/TiledMapProperties.cs | 171 ++++++++++++++++++ .../IsoTools/Tiled/TiledMapProperties.cs.meta | 12 ++ 9 files changed, 236 insertions(+), 46 deletions(-) create mode 100644 Assets/IsoTools/Tiled/TiledMapProperties.cs create mode 100644 Assets/IsoTools/Tiled/TiledMapProperties.cs.meta diff --git a/Assembly-CSharp.csproj b/Assembly-CSharp.csproj index aa1724f..7041f62 100644 --- a/Assembly-CSharp.csproj +++ b/Assembly-CSharp.csproj @@ -68,6 +68,7 @@ + /Applications/Unity/Unity.app/Contents/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll diff --git a/Assets/IsoTools/Scripts/Internal/Editor/IsoObjectEditor.cs b/Assets/IsoTools/Scripts/Internal/Editor/IsoObjectEditor.cs index ae38b40..5536eda 100644 --- a/Assets/IsoTools/Scripts/Internal/Editor/IsoObjectEditor.cs +++ b/Assets/IsoTools/Scripts/Internal/Editor/IsoObjectEditor.cs @@ -61,7 +61,9 @@ namespace IsoTools.Internal { } float ZMoveIsoObjects(float delta) { - Undo.RecordObjects(_iso_zpositions.Keys.ToArray(), "Move"); + Undo.RecordObjects( + _iso_zpositions.Keys.ToArray(), + _iso_zpositions.Count > 1 ? "Move IsoObjects" : "Move IsoObject"); var is_any_alignment = isAnyAlignment; return _iso_zpositions.Aggregate(0.0f, (AccIn, pair) => { var iso_object = pair.Key; @@ -77,7 +79,9 @@ namespace IsoTools.Internal { } Vector3 XYMoveIsoObjects(Vector3 delta) { - Undo.RecordObjects(_positions.Keys.ToArray(), "Move"); + Undo.RecordObjects( + _positions.Keys.ToArray(), + _positions.Count > 1 ? "Move IsoObjects" : "Move IsoObject"); var is_any_alignment = isAnyAlignment; return _positions.Aggregate(Vector3.zero, (AccIn, pair) => { var iso_object = pair.Key; diff --git a/Assets/IsoTools/Scripts/IsoObject.cs b/Assets/IsoTools/Scripts/IsoObject.cs index f5d1feb..55f171b 100644 --- a/Assets/IsoTools/Scripts/IsoObject.cs +++ b/Assets/IsoTools/Scripts/IsoObject.cs @@ -10,11 +10,11 @@ namespace IsoTools { [ExecuteInEditMode, DisallowMultipleComponent] public class IsoObject : MonoBehaviour { - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // Mode // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- public enum Mode { Mode2d, @@ -32,11 +32,11 @@ namespace IsoTools { } } - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // Size // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- [SerializeField] Vector3 _size = Vector3.one; @@ -76,11 +76,11 @@ namespace IsoTools { get { return new Vector2(sizeX, sizeZ); } } - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // Position // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- [SerializeField] Vector3 _position = Vector3.zero; @@ -120,11 +120,11 @@ namespace IsoTools { get { return new Vector2(positionX, positionZ); } } - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // TilePosition // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- public Vector3 tilePosition { get { return IsoUtils.Vec3Round(position); } @@ -158,11 +158,11 @@ namespace IsoTools { get { return new Vector2(tilePositionX, tilePositionZ); } } - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // Internal // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- public class InternalState { public bool Dirty = false; @@ -178,11 +178,11 @@ namespace IsoTools { public InternalState Internal = new InternalState(); - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // For editor // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- #if UNITY_EDITOR Vector3 _lastSize = Vector3.zero; @@ -203,11 +203,11 @@ namespace IsoTools { } #endif - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // Functions // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- IsoWorld _isoWorld = null; public IsoWorld isoWorld { @@ -270,11 +270,11 @@ namespace IsoTools { #endif } - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // Messages // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- void Awake() { Internal.SelfDepends = new HashSet(new IsoObject[47]); @@ -328,4 +328,4 @@ namespace IsoTools { } #endif } -} // namespace IsoTools \ No newline at end of file +} // namespace IsoTools diff --git a/Assets/IsoTools/Scripts/IsoWorld.cs b/Assets/IsoTools/Scripts/IsoWorld.cs index 9adfb3b..dfcf9f8 100644 --- a/Assets/IsoTools/Scripts/IsoWorld.cs +++ b/Assets/IsoTools/Scripts/IsoWorld.cs @@ -33,11 +33,11 @@ namespace IsoTools { Vector2 _sectorsMaxNumPos = Vector2.zero; Vector2 _sectorsNumPosCount = Vector2.zero; - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // Constants // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- public static readonly float DefTileSize = 32.0f; public static readonly float MinTileSize = Mathf.Epsilon; @@ -63,11 +63,11 @@ namespace IsoTools { public static readonly float MinStartDepth = float.MinValue; public static readonly float MaxStartDepth = float.MaxValue; - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // Sorting properties // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- [SerializeField] public float _tileSize = DefTileSize; @@ -129,11 +129,11 @@ namespace IsoTools { } } - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // IsoToScreen/ScreenToIso // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- public Vector2 IsoToScreen(Vector3 iso_pnt) { if ( _dirtyMat ) { @@ -160,11 +160,11 @@ namespace IsoTools { iso_z); } - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // TouchIsoPosition // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- public Vector3 TouchIsoPosition(int finger_id) { return TouchIsoPosition(finger_id, 0.0f); @@ -199,11 +199,11 @@ namespace IsoTools { return Vector3.zero; } - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // TouchIsoTilePosition // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- public Vector3 TouchIsoTilePosition(int finger_id) { return IsoUtils.Vec3Floor(TouchIsoPosition(finger_id)); @@ -221,11 +221,11 @@ namespace IsoTools { return IsoUtils.Vec3Floor(TouchIsoPosition(finger_id, camera, iso_z)); } - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // MouseIsoPosition // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- public Vector3 MouseIsoPosition() { return MouseIsoPosition(0.0f); @@ -253,11 +253,11 @@ namespace IsoTools { iso_z); } - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // MouseIsoTilePosition // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- public Vector3 MouseIsoTilePosition() { return IsoUtils.Vec3Floor(MouseIsoPosition()); @@ -275,11 +275,11 @@ namespace IsoTools { return IsoUtils.Vec3Floor(MouseIsoPosition(camera, iso_z)); } - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // Internal // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- public void MarkDirty() { if ( !_dirty ) { @@ -308,11 +308,11 @@ namespace IsoTools { _oldVisibles.Remove(iso_object); } - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // Private // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- void MarkDirtyIsoMatrix() { _dirtyMat = true; @@ -634,11 +634,11 @@ namespace IsoTools { trans.position = IsoUtils.Vec3ChangeZ(trans.position, depth); } - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // Messages // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- void Start() { ChangeSortingProperty(); @@ -689,4 +689,4 @@ namespace IsoTools { } #endif } -} // namespace IsoTools \ No newline at end of file +} // namespace IsoTools diff --git a/Assets/IsoTools/Tiled/Examples/Scenes/TiledScene.unity b/Assets/IsoTools/Tiled/Examples/Scenes/TiledScene.unity index 624515f..c6339f3 100644 --- a/Assets/IsoTools/Tiled/Examples/Scenes/TiledScene.unity +++ b/Assets/IsoTools/Tiled/Examples/Scenes/TiledScene.unity @@ -146,7 +146,7 @@ Camera: far clip plane: 1000 field of view: 60 orthographic: 1 - orthographic size: 3 + orthographic size: 6 m_Depth: -1 m_CullingMask: serializedVersion: 2 diff --git a/Assets/IsoTools/Tiled/Internal/Editor/TiledMapAssetEditor.cs b/Assets/IsoTools/Tiled/Internal/Editor/TiledMapAssetEditor.cs index 0b2fe83..1feea5f 100644 --- a/Assets/IsoTools/Tiled/Internal/Editor/TiledMapAssetEditor.cs +++ b/Assets/IsoTools/Tiled/Internal/Editor/TiledMapAssetEditor.cs @@ -132,6 +132,7 @@ namespace IsoTools.Tiled.Internal { Debug.LogErrorFormat("Create tiled map error: {0}", e.Message); DestroyImmediate(map_go, true); } + Undo.RegisterCreatedObjectUndo(map_go, "Create TiledMap"); } } } diff --git a/Assets/IsoTools/Tiled/TiledMap.cs b/Assets/IsoTools/Tiled/TiledMap.cs index 38fe12a..c5c1cca 100644 --- a/Assets/IsoTools/Tiled/TiledMap.cs +++ b/Assets/IsoTools/Tiled/TiledMap.cs @@ -13,17 +13,18 @@ namespace IsoTools.Tiled { public TiledMapAsset Asset = null; - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- // // Functions // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- - // ------------------------------------------------------------------------ + + // --------------------------------------------------------------------- // // Messages // - // ------------------------------------------------------------------------ + // --------------------------------------------------------------------- void Awake() { } @@ -55,4 +56,4 @@ namespace IsoTools.Tiled { } #endif } -} // namespace IsoTools.Tiled \ No newline at end of file +} // namespace IsoTools.Tiled diff --git a/Assets/IsoTools/Tiled/TiledMapProperties.cs b/Assets/IsoTools/Tiled/TiledMapProperties.cs new file mode 100644 index 0000000..94c184a --- /dev/null +++ b/Assets/IsoTools/Tiled/TiledMapProperties.cs @@ -0,0 +1,171 @@ +using UnityEngine; +using System.Globalization; +using System.Collections.Generic; + +namespace IsoTools.Tiled { + public class TiledMapProperties { + List _properties = null; + + // ----------------------------- + // Functions + // ----------------------------- + + public TiledMapProperties(List properties) { + _properties = properties; + } + + public bool Has(string property_name) { + if ( _properties != null ) { + for ( var i = 0; i < _properties.Count / 2; ++i ) { + if ( _properties[i * 2] == property_name ) { + return true; + } + } + } + return false; + } + + // ----------------------------- + // GetAsX + // ----------------------------- + + public bool GetAsBool(string property_name) { + bool value; + if ( TryGetAsBool(property_name, out value) ) { + return value; + } + throw new UnityException("find or parse parameter error"); + } + + public short GetAsShort(string property_name) { + short value; + if ( TryGetAsShort(property_name, out value) ) { + return value; + } + throw new UnityException("find or parse parameter error"); + } + + public int GetAsInt(string property_name) { + int value; + if ( TryGetAsInt(property_name, out value) ) { + return value; + } + throw new UnityException("find or parse parameter error"); + } + + public long GetAsLong(string property_name) { + long value; + if ( TryGetAsLong(property_name, out value) ) { + return value; + } + throw new UnityException("find or parse parameter error"); + } + + public float GetAsFloat(string property_name) { + float value; + if ( TryGetAsFloat(property_name, out value) ) { + return value; + } + throw new UnityException("find or parse parameter error"); + } + + public double GetAsDouble(string property_name) { + double value; + if ( TryGetAsDouble(property_name, out value) ) { + return value; + } + throw new UnityException("find or parse parameter error"); + } + + public string GetAsString(string property_name) { + string value; + if ( TryGetAsString(property_name, out value) ) { + return value; + } + throw new UnityException("find or parse parameter error"); + } + + // ----------------------------- + // TryGetAsX + // ----------------------------- + + public bool TryGetAsBool(string property_name, out bool value) { + string property_value; + if ( TryGetAsString(property_name, out property_value) ) { + if ( bool.TryParse(property_value, out value) ) { + return true; + } + } + value = false; + return false; + } + + public bool TryGetAsShort(string property_name, out short value) { + string property_value; + if ( TryGetAsString(property_name, out property_value) ) { + if ( short.TryParse(property_value, NumberStyles.Any, CultureInfo.InvariantCulture, out value) ) { + return true; + } + } + value = 0; + return false; + } + + public bool TryGetAsInt(string property_name, out int value) { + string property_value; + if ( TryGetAsString(property_name, out property_value) ) { + if ( int.TryParse(property_value, NumberStyles.Any, CultureInfo.InvariantCulture, out value) ) { + return true; + } + } + value = 0; + return false; + } + + public bool TryGetAsLong(string property_name, out long value) { + string property_value; + if ( TryGetAsString(property_name, out property_value) ) { + if ( long.TryParse(property_value, NumberStyles.Any, CultureInfo.InvariantCulture, out value) ) { + return true; + } + } + value = 0; + return false; + } + + public bool TryGetAsFloat(string property_name, out float value) { + string property_value; + if ( TryGetAsString(property_name, out property_value) ) { + if ( float.TryParse(property_value, NumberStyles.Any, CultureInfo.InvariantCulture, out value) ) { + return true; + } + } + value = 0; + return false; + } + + public bool TryGetAsDouble(string property_name, out double value) { + string property_value; + if ( TryGetAsString(property_name, out property_value) ) { + if ( double.TryParse(property_value, NumberStyles.Any, CultureInfo.InvariantCulture, out value) ) { + return true; + } + } + value = 0; + return false; + } + + public bool TryGetAsString(string property_name, out string value) { + if ( _properties != null ) { + for ( var i = 0; i < _properties.Count / 2; ++i ) { + if ( _properties[i * 2] == property_name ) { + value = _properties[i * 2 + 1]; + return true; + } + } + } + value = string.Empty; + return false; + } + } +} // namespace IsoTools.Tiled diff --git a/Assets/IsoTools/Tiled/TiledMapProperties.cs.meta b/Assets/IsoTools/Tiled/TiledMapProperties.cs.meta new file mode 100644 index 0000000..dea460e --- /dev/null +++ b/Assets/IsoTools/Tiled/TiledMapProperties.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: fa7b674b7f7ae4c11a2df9172b4020ba +timeCreated: 1454346480 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: