| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace GDX.DataTables.CellValues |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// A <see cref="DataTableBase" /> <see cref="RectInt" /> cell reference. |
| | 12 | | /// </summary> |
| | 13 | | [Serializable] |
| | 14 | | public struct RectIntCellValue |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// The cached struct of the referenced <see cref="DataTable" /> cell. |
| | 18 | | /// </summary> |
| | 19 | | RectInt m_CachedValue; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// The cached <see cref="DataTable" /> version found when last updating <see cref="m_CachedValue" />. |
| | 23 | | /// </summary> |
| | 24 | | ulong m_TableVersion; |
| | 25 | | #pragma warning disable IDE1006 |
| | 26 | | // ReSharper disable InconsistentNaming |
| | 27 | | /// <summary> |
| | 28 | | /// The <see cref="DataTableBase" /> polled for cell data. |
| | 29 | | /// </summary> |
| | 30 | | public DataTableBase DataTable; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// The unique column identifier used when accessing the <see cref="DataTable" />. |
| | 34 | | /// </summary> |
| | 35 | | public int ColumnIdentifier; |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// The unique row identifier used when accessing the <see cref="DataTable" />. |
| | 39 | | /// </summary> |
| | 40 | | public int RowIdentifier; |
| | 41 | | // ReSharper enable InconsistentNaming |
| | 42 | | #pragma warning restore IDE1006 |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Creates a <see cref="RectIntCellValue" />. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="dataTable">The referenced <see cref="DataTableBase" />.</param> |
| | 48 | | /// <param name="rowIdentifier">The unique row identifier to use when polling the <paramref name="dataTable" />. |
| | 49 | | /// <param name="columnIdentifier">The unique column identifier to use when polling the <paramref name="dataTabl |
| | 50 | | public RectIntCellValue(DataTableBase dataTable, int rowIdentifier, int columnIdentifier) |
| 0 | 51 | | { |
| 0 | 52 | | DataTable = dataTable; |
| 0 | 53 | | RowIdentifier = rowIdentifier; |
| 0 | 54 | | ColumnIdentifier = columnIdentifier; |
| 0 | 55 | | m_TableVersion = 0; |
| 0 | 56 | | m_CachedValue = default; |
| 0 | 57 | | Get(); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Get the <see cref="RectInt" /> value referenced from the <see cref="DataTableBase" />. |
| | 62 | | /// </summary> |
| | 63 | | /// <remarks> |
| | 64 | | /// This will evaluate if the version of the table matches the internally cached version, and will update |
| | 65 | | /// the cached reference if necessary. |
| | 66 | | /// </remarks> |
| | 67 | | /// <returns>A <see cref="RectInt" /> struct.</returns> |
| | 68 | | public RectInt Get() |
| 0 | 69 | | { |
| 0 | 70 | | if (m_TableVersion != DataTable.GetDataVersion()) |
| 0 | 71 | | { |
| 0 | 72 | | m_CachedValue = DataTable.GetRectInt(RowIdentifier, ColumnIdentifier); |
| 0 | 73 | | m_TableVersion = DataTable.GetDataVersion(); |
| 0 | 74 | | } |
| | 75 | |
|
| 0 | 76 | | return m_CachedValue; |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Get the internally cached version of the <see cref="DataTableBase" />'s data version. |
| | 81 | | /// </summary> |
| | 82 | | /// <returns>A version number.</returns> |
| | 83 | | public ulong GetDataVersion() |
| 0 | 84 | | { |
| 0 | 85 | | return m_TableVersion; |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Get the cached value without a version check. |
| | 90 | | /// </summary> |
| | 91 | | /// <remarks> |
| | 92 | | /// This can respond with a default value if a <see cref="Get" /> call has not been made yet to populate the |
| | 93 | | /// internally cached value. |
| | 94 | | /// </remarks> |
| | 95 | | /// <returns>A <see cref="RectInt" /> struct.</returns> |
| | 96 | | public RectInt GetUnsafe() |
| 0 | 97 | | { |
| 0 | 98 | | return m_CachedValue; |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Sets the cached value of the struct and by default, updates the associated <see cref="DataTableBase" />. |
| | 103 | | /// </summary> |
| | 104 | | /// <remarks>Updating the <see cref="DataTableBase" /> will update the cached table version.</remarks> |
| | 105 | | /// <param name="newValue">A <see cref="RectInt" /> struct.</param> |
| | 106 | | /// <param name="updateTable">Should the value be pushed back to the referenced <see cref="DataTableBase" /> cel |
| | 107 | | public void Set(RectInt newValue, bool updateTable = true) |
| 0 | 108 | | { |
| 0 | 109 | | m_CachedValue = newValue; |
| 0 | 110 | | if (updateTable) |
| 0 | 111 | | { |
| 0 | 112 | | m_TableVersion = DataTable.SetRectInt(RowIdentifier, ColumnIdentifier, newValue); |
| 0 | 113 | | } |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Get the <see cref="Serializable.SerializableTypes" /> which this struct supports. |
| | 118 | | /// </summary> |
| | 119 | | /// <returns>A <see cref="Serializable.SerializableTypes" />.</returns> |
| | 120 | | public static Serializable.SerializableTypes GetSupportedType() |
| 0 | 121 | | { |
| 0 | 122 | | return Serializable.SerializableTypes.RectInt; |
| 0 | 123 | | } |
| | 124 | | } |
| | 125 | | } |