TilemapProperties.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Microsoft.Xna.Framework;
  5. namespace MonoGame.Extended.Tilemaps
  6. {
  7. /// <summary>
  8. /// Collection of custom properties for tilemap elements.
  9. /// </summary>
  10. public class TilemapProperties : IEnumerable<KeyValuePair<string, TilemapPropertyValue>>
  11. {
  12. private readonly Dictionary<string, TilemapPropertyValue> _properties = new Dictionary<string, TilemapPropertyValue>();
  13. /// <summary>
  14. /// Gets or sets the property value for the specified key.
  15. /// </summary>
  16. /// <param name="key">The property key.</param>
  17. /// <returns>The property value.</returns>
  18. public TilemapPropertyValue this[string key]
  19. {
  20. get => _properties[key];
  21. set => _properties[key] = value;
  22. }
  23. /// <summary>
  24. /// Gets the number of properties in this collection.
  25. /// </summary>
  26. public int Count => _properties.Count;
  27. /// <summary>
  28. /// Attempts to get the value associated with the specified key.
  29. /// </summary>
  30. /// <param name="key">The key of the value to get.</param>
  31. /// <param name="value">
  32. /// When this method returns, contains the value associated with the specified key, if found;
  33. /// otherwise, the default value.
  34. /// </param>
  35. /// <returns><see langword="true"/> if the property was found; otherwise, <see langword="false"/>.</returns>
  36. public bool TryGetValue(string key, out TilemapPropertyValue value)
  37. {
  38. return _properties.TryGetValue(key, out value);
  39. }
  40. /// <summary>
  41. /// Gets a string property value.
  42. /// </summary>
  43. /// <param name="key">The property key.</param>
  44. /// <param name="defaultValue">The default value to return if the key is not found or has a different type.</param>
  45. /// <returns>The property value, or the default value if not found or type mismatch.</returns>
  46. public string GetString(string key, string defaultValue = "")
  47. {
  48. if (!_properties.TryGetValue(key, out var value))
  49. return defaultValue;
  50. if (value.Type != TilemapPropertyType.String)
  51. return defaultValue;
  52. return value.AsString();
  53. }
  54. /// <summary>
  55. /// Gets an integer property value.
  56. /// </summary>
  57. /// <param name="key">The property key.</param>
  58. /// <param name="defaultValue">The default value to return if the key is not found or has a different type.</param>
  59. /// <returns>The property value, or the default value if not found or type mismatch.</returns>
  60. public int GetInt(string key, int defaultValue = 0)
  61. {
  62. if (!_properties.TryGetValue(key, out var value))
  63. return defaultValue;
  64. if (value.Type != TilemapPropertyType.Int)
  65. return defaultValue;
  66. return value.AsInt();
  67. }
  68. /// <summary>
  69. /// Gets a float property value.
  70. /// </summary>
  71. /// <param name="key">The property key.</param>
  72. /// <param name="defaultValue">The default value to return if the key is not found or has a different type.</param>
  73. /// <returns>The property value, or the default value if not found or type mismatch.</returns>
  74. public float GetFloat(string key, float defaultValue = 0.0f)
  75. {
  76. if (!_properties.TryGetValue(key, out var value))
  77. return defaultValue;
  78. if (value.Type != TilemapPropertyType.Float)
  79. return defaultValue;
  80. return value.AsFloat();
  81. }
  82. /// <summary>
  83. /// Gets a bool property value.
  84. /// </summary>
  85. /// <param name="key">The property key.</param>
  86. /// <param name="defaultValue">The default value to return if the key is not found or has a different type.</param>
  87. /// <returns>The property value, or the default value if not found or type mismatch.</returns>
  88. public bool GetBool(string key, bool defaultValue = false)
  89. {
  90. if (!_properties.TryGetValue(key, out var value))
  91. return defaultValue;
  92. if (value.Type != TilemapPropertyType.Bool)
  93. return defaultValue;
  94. return value.AsBool();
  95. }
  96. /// <summary>
  97. /// Gets a color property value.
  98. /// </summary>
  99. /// <param name="key">The property key.</param>
  100. /// <param name="defaultValue">The default value to return if the key is not found or has a different type.</param>
  101. /// <returns>The property value, or the default value if not found or type mismatch.</returns>
  102. public Color GetColor(string key, Color? defaultValue = null)
  103. {
  104. var effectiveDefault = defaultValue ?? Color.White;
  105. if (!_properties.TryGetValue(key, out var value))
  106. return effectiveDefault;
  107. if (value.Type != TilemapPropertyType.Color)
  108. return effectiveDefault;
  109. return value.AsColor();
  110. }
  111. /// <summary>
  112. /// Sets a string property value.
  113. /// </summary>
  114. /// <param name="key">The property key.</param>
  115. /// <param name="value">The value to set.</param>
  116. public void SetString(string key, string value)
  117. {
  118. _properties[key] = TilemapPropertyValue.CreateString(value);
  119. }
  120. /// <summary>
  121. /// Sets an integer property value.
  122. /// </summary>
  123. /// <param name="key">The property key.</param>
  124. /// <param name="value">The value to set.</param>
  125. public void SetInt(string key, int value)
  126. {
  127. _properties[key] = TilemapPropertyValue.CreateInt(value);
  128. }
  129. /// <summary>
  130. /// Sets a float property value.
  131. /// </summary>
  132. /// <param name="key">The property key.</param>
  133. /// <param name="value">The value to set.</param>
  134. public void SetFloat(string key, float value)
  135. {
  136. _properties[key] = TilemapPropertyValue.CreateFloat(value);
  137. }
  138. /// <summary>
  139. /// Sets a bool property value.
  140. /// </summary>
  141. /// <param name="key">The property key.</param>
  142. /// <param name="value">The value to set.</param>
  143. public void SetBool(string key, bool value)
  144. {
  145. _properties[key] = TilemapPropertyValue.CreateBool(value);
  146. }
  147. /// <summary>
  148. /// Sets a color property value.
  149. /// </summary>
  150. /// <param name="key">The property key.</param>
  151. /// <param name="value">The value to set.</param>
  152. public void SetColor(string key, Color value)
  153. {
  154. _properties[key] = TilemapPropertyValue.CreateColor(value);
  155. }
  156. public IEnumerator<KeyValuePair<string, TilemapPropertyValue>> GetEnumerator()
  157. {
  158. return _properties.GetEnumerator();
  159. }
  160. IEnumerator IEnumerable.GetEnumerator()
  161. {
  162. return GetEnumerator();
  163. }
  164. }
  165. }