| 1 |
<DebuggerDisplay("Name: {Description}, Type: {Type}")> |
| 2 |
Public Class clsProperty |
| 3 |
Inherits clsItem |
| 4 |
|
| 5 |
Public Enum PropertyOfEnum |
| 6 |
Locations = 0 |
| 7 |
Objects = 1 |
| 8 |
Characters = 2 |
| 9 |
AnyItem = 3 |
| 10 |
End Enum |
| 11 |
|
| 12 |
Public Enum PropertyTypeEnum |
| 13 |
SelectionOnly |
| 14 |
[Integer] |
| 15 |
Text |
| 16 |
ObjectKey |
| 17 |
StateList |
| 18 |
CharacterKey |
| 19 |
LocationKey |
| 20 |
LocationGroupKey |
| 21 |
ValueList |
| 22 |
End Enum |
| 23 |
|
| 24 |
|
| 25 |
Private eType As PropertyTypeEnum |
| 26 |
Private sDependentKey As String |
| 27 |
|
| 28 |
Friend arlStates As New StringArrayList |
| 29 |
Friend ValueList As New Dictionary(Of String, Integer)(System.StringComparer.OrdinalIgnoreCase) |
| 30 |
|
| 31 |
Friend Property PropertyOf As PropertyOfEnum |
| 32 |
Public Property PrivateTo As String = "" |
| 33 |
Public Property Mandatory() As Boolean |
| 34 |
Public Property FromGroup() As Boolean |
| 35 |
Public Property Description() As String |
| 36 |
Public Property AppendToProperty() As String |
| 37 |
Public Property GroupOnly As Boolean |
| 38 |
Public Property IntData() As Integer |
| 39 |
Friend Property StringData() As Description |
| 40 |
Public Property Selected() As Boolean |
| 41 |
Public Property Indent As Integer = 0 |
| 42 |
Public Property DependentValue() As String |
| 43 |
Public Property RestrictProperty() As String |
| 44 |
Public Property RestrictValue() As String |
| 45 |
Public Property PopupDescription As String |
| 46 |
|
| 47 |
|
| 48 |
Friend Property Type() As PropertyTypeEnum |
| 49 |
Get |
| 50 |
Return eType |
| 51 |
End Get |
| 52 |
Set(ByVal Value As PropertyTypeEnum) |
| 53 |
eType = Value |
| 54 |
Select Case eType |
| 55 |
Case PropertyTypeEnum.StateList |
| 56 |
If Not arlStates.Contains(StringData.ToString) AndAlso arlStates.Count > 0 Then |
| 57 |
StringData = New Description(arlStates(0)) ' Default the value to the first state |
| 58 |
End If |
| 59 |
Case PropertyTypeEnum.ValueList |
| 60 |
If Not ValueList.ContainsValue(IntData) AndAlso ValueList.Count > 0 Then |
| 61 |
For Each iValue As Integer In ValueList.Values |
| 62 |
IntData = iValue |
| 63 |
Exit For |
| 64 |
Next |
| 65 |
End If |
| 66 |
End Select |
| 67 |
End Set |
| 68 |
End Property |
| 69 |
|
| 70 |
Public Overrides ReadOnly Property Clone() As clsItem |
| 71 |
Get |
| 72 |
Return Copy() |
| 73 |
End Get |
| 74 |
End Property |
| 75 |
|
| 76 |
Public Property DependentKey() As String |
| 77 |
Get |
| 78 |
Return sDependentKey |
| 79 |
End Get |
| 80 |
Set(ByVal Value As String) |
| 81 |
If iLoading > 0 OrElse Value Is Nothing OrElse KeyExists(Value) Then |
| 82 |
sDependentKey = Value |
| 83 |
Else |
| 84 |
Throw New Exception("Key " & Value & " doesn't exist") |
| 85 |
End If |
| 86 |
End Set |
| 87 |
End Property |
| 88 |
|
| 89 |
Friend ReadOnly Property PossibleValues(Optional ByVal CurrentProperties As PropertyHashTable = Nothing) As StringArrayList |
| 90 |
Get |
| 91 |
Dim sValues As New StringArrayList |
| 92 |
|
| 93 |
Select Case Type |
| 94 |
Case clsProperty.PropertyTypeEnum.CharacterKey |
| 95 |
For Each ch As clsCharacter In Adventure.htblCharacters.Values |
| 96 |
If (RestrictProperty Is Nothing OrElse ch.HasProperty(RestrictProperty)) Then |
| 97 |
If RestrictValue Is Nothing OrElse ch.GetPropertyValue(RestrictProperty) = RestrictValue Then |
| 98 |
sValues.Add(ch.Key) |
| 99 |
End If |
| 100 |
End If |
| 101 |
Next |
| 102 |
Case clsProperty.PropertyTypeEnum.Integer |
| 103 |
TODO("Integer values") |
| 104 |
Case clsProperty.PropertyTypeEnum.LocationGroupKey |
| 105 |
TODO("Location Group values") |
| 106 |
Case clsProperty.PropertyTypeEnum.LocationKey |
| 107 |
For Each loc As clsLocation In Adventure.htblLocations.Values |
| 108 |
If (RestrictProperty Is Nothing OrElse loc.HasProperty(RestrictProperty)) Then |
| 109 |
If RestrictValue Is Nothing OrElse loc.GetPropertyValue(RestrictProperty) = RestrictValue Then |
| 110 |
sValues.Add(loc.Key) |
| 111 |
End If |
| 112 |
End If |
| 113 |
Next |
| 114 |
Case clsProperty.PropertyTypeEnum.ObjectKey |
| 115 |
For Each ob As clsObject In Adventure.htblObjects.Values |
| 116 |
If (RestrictProperty Is Nothing OrElse ob.HasProperty(RestrictProperty)) Then |
| 117 |
If RestrictValue Is Nothing OrElse ob.GetPropertyValue(RestrictProperty) = RestrictValue Then |
| 118 |
sValues.Add(ob.Key) |
| 119 |
End If |
| 120 |
End If |
| 121 |
Next |
| 122 |
Case clsProperty.PropertyTypeEnum.SelectionOnly |
| 123 |
sValues.Add(sSELECTED) |
| 124 |
sValues.Add(sUNSELECTED) ' Not strictly a property value, but only way for action to remove the property |
| 125 |
Case clsProperty.PropertyTypeEnum.StateList |
| 126 |
For Each sState As String In arlStates |
| 127 |
sValues.Add(sState) |
| 128 |
Next |
| 129 |
If CurrentProperties Is Nothing Then CurrentProperties = Adventure.htblAllProperties |
| 130 |
For Each prop As clsProperty In CurrentProperties.Values |
| 131 |
If (CurrentProperties Is Adventure.htblAllProperties OrElse prop.Selected) AndAlso prop.Type = PropertyTypeEnum.StateList AndAlso prop.AppendToProperty = Me.Key Then |
| 132 |
For Each sState As String In prop.arlStates |
| 133 |
sValues.Add(sState) |
| 134 |
Next |
| 135 |
End If |
| 136 |
Next |
| 137 |
Case clsProperty.PropertyTypeEnum.Text |
| 138 |
TODO("Text values") |
| 139 |
End Select |
| 140 |
|
| 141 |
Return sValues |
| 142 |
End Get |
| 143 |
End Property |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
Public Property Value(Optional ByVal bTesting As Boolean = False) As String |
| 148 |
Get |
| 149 |
Select Case eType |
| 150 |
Case PropertyTypeEnum.StateList, PropertyTypeEnum.ObjectKey, PropertyTypeEnum.CharacterKey, PropertyTypeEnum.LocationKey, PropertyTypeEnum.LocationGroupKey |
| 151 |
Return StringData.ToString |
| 152 |
Case PropertyTypeEnum.ValueList |
| 153 |
Return IntData.ToString |
| 154 |
Case PropertyTypeEnum.Text |
| 155 |
Return StringData.ToString(bTesting) |
| 156 |
Case PropertyTypeEnum.Integer |
| 157 |
Return IntData.ToString |
| 158 |
Case PropertyTypeEnum.SelectionOnly |
| 159 |
Return True.ToString |
| 160 |
End Select |
| 161 |
Return Nothing |
| 162 |
End Get |
| 163 |
Set(ByVal Value As String) |
| 164 |
Try |
| 165 |
Select Case eType |
| 166 |
Case PropertyTypeEnum.StateList |
| 167 |
If iLoading > 0 OrElse Value Is Nothing OrElse PossibleValues.Contains(Value) Then |
| 168 |
StringData = New Description(Value) |
| 169 |
ElseIf PossibleValues.Count > 0 Then |
| 170 |
' Perhaps it's an expression that resolves to a valid state... |
| 171 |
Dim v As New clsVariable |
| 172 |
v.Type = clsVariable.VariableTypeEnum.Text |
| 173 |
v.SetToExpression(Value) |
| 174 |
If v.StringValue IsNot Nothing AndAlso PossibleValues.Contains(v.StringValue) Then |
| 175 |
StringData = New Description(v.StringValue) |
| 176 |
Else |
| 177 |
Throw New Exception("'" & Value & "' is not a valid state.") |
| 178 |
End If |
| 179 |
End If |
| 180 |
Case PropertyTypeEnum.ValueList |
| 181 |
If iLoading > 0 OrElse Value Is Nothing OrElse ValueList.ContainsKey(Value) OrElse ValueList.ContainsValue(SafeInt(Value)) Then |
| 182 |
If ValueList.ContainsKey(Value) Then |
| 183 |
IntData = ValueList(Value) |
| 184 |
Else |
| 185 |
IntData = SafeInt(Value) |
| 186 |
End If |
| 187 |
ElseIf ValueList.Count > 0 Then |
| 188 |
' Perhaps it's an expression that resolves to a valid state... |
| 189 |
Dim v As New clsVariable |
| 190 |
v.Type = clsVariable.VariableTypeEnum.Text |
| 191 |
v.SetToExpression(Value) |
| 192 |
If v.StringValue IsNot Nothing AndAlso ValueList.ContainsKey(v.StringValue) Then |
| 193 |
IntData = ValueList(v.StringValue) |
| 194 |
Else |
| 195 |
Throw New Exception("'" & Value & "' is not a valid state.") |
| 196 |
End If |
| 197 |
End If |
| 198 |
Case PropertyTypeEnum.ObjectKey, PropertyTypeEnum.CharacterKey, PropertyTypeEnum.LocationKey, PropertyTypeEnum.LocationGroupKey |
| 199 |
StringData = New Description(Value) |
| 200 |
Case PropertyTypeEnum.Text |
| 201 |
If StringData.Count < 2 Then |
| 202 |
' Simple text stored |
| 203 |
If Not (StringData.Count = 1 AndAlso StringData(0).Description = Value) Then StringData = New Description(Value) |
| 204 |
Else |
| 205 |
' We have a complex description here, probably with restrictions etc - if we set the property, we'll lose it |
| 206 |
End If |
| 207 |
Case PropertyTypeEnum.Integer |
| 208 |
If IsNumeric(Value) Then |
| 209 |
IntData = SafeInt(Value) |
| 210 |
Else |
| 211 |
Dim var As New clsVariable |
| 212 |
var.Type = clsVariable.VariableTypeEnum.Numeric |
| 213 |
var.SetToExpression(Value) |
| 214 |
IntData = var.IntValue |
| 215 |
End If |
| 216 |
End Select |
| 217 |
Catch ex As Exception |
| 218 |
ErrMsg("Error Setting Property " & Description & " to """ & Value & """", ex) |
| 219 |
End Try |
| 220 |
End Set |
| 221 |
End Property |
| 222 |
|
| 223 |
Public Function Copy() As clsProperty |
| 224 |
|
| 225 |
Dim p As New clsProperty |
| 226 |
|
| 227 |
With p |
| 228 |
.arlStates = Me.arlStates.Clone |
| 229 |
.ValueList.Clear() |
| 230 |
For Each sLabel As String In Me.ValueList.Keys |
| 231 |
.ValueList.Add(sLabel, Me.ValueList(sLabel)) |
| 232 |
Next |
| 233 |
.DependentKey = Me.DependentKey |
| 234 |
.DependentValue = Me.DependentValue |
| 235 |
.AppendToProperty = Me.AppendToProperty |
| 236 |
.RestrictProperty = Me.RestrictProperty |
| 237 |
.RestrictValue = Me.RestrictValue |
| 238 |
.Description = Me.Description |
| 239 |
.IntData = Me.IntData |
| 240 |
.Key = Me.Key |
| 241 |
.Mandatory = Me.Mandatory |
| 242 |
.Selected = Me.Selected |
| 243 |
.Type = Me.Type ' Needs to be set before StringData, otherwise an appended StateList won't be allowed as a valid value (e.g. Locked will be reset to Open) |
| 244 |
.StringData = Me.StringData.Copy |
| 245 |
.PropertyOf = Me.PropertyOf |
| 246 |
.GroupOnly = Me.GroupOnly |
| 247 |
.FromGroup = Me.FromGroup |
| 248 |
.PrivateTo = Me.PrivateTo |
| 249 |
.PopupDescription = Me.PopupDescription |
| 250 |
|
| 251 |
Select Case .Type |
| 252 |
Case PropertyTypeEnum.ObjectKey, PropertyTypeEnum.CharacterKey, PropertyTypeEnum.LocationKey, PropertyTypeEnum.LocationGroupKey, PropertyTypeEnum.Text, PropertyTypeEnum.ValueList, PropertyTypeEnum.StateList |
| 253 |
' These will all re-write StringData, so we can ignore |
| 254 |
Case Else |
| 255 |
.Value = Me.Value |
| 256 |
End Select |
| 257 |
End With |
| 258 |
|
| 259 |
Return p |
| 260 |
|
| 261 |
End Function |
| 262 |
|
| 263 |
Public Shadows Function ToString() As String |
| 264 |
Return Me.Description & " (" & Me.Value & ")" |
| 265 |
End Function |
| 266 |
|
| 267 |
Public Sub New() |
| 268 |
Me.StringData = New Description |
| 269 |
Me.PropertyOf = PropertyOfEnum.Objects |
| 270 |
End Sub |
| 271 |
|
| 272 |
Public Overrides ReadOnly Property CommonName() As String |
| 273 |
Get |
| 274 |
Return Description |
| 275 |
End Get |
| 276 |
End Property |
| 277 |
|
| 278 |
Friend Overrides ReadOnly Property AllDescriptions() As System.Collections.Generic.List(Of SharedModule.Description) |
| 279 |
Get |
| 280 |
Dim all As New Generic.List(Of Description) |
| 281 |
all.Add(Me.StringData) |
| 282 |
Return all |
| 283 |
End Get |
| 284 |
End Property |
| 285 |
|
| 286 |
Friend Overrides Function FindStringLocal(sSearchString As String, Optional sReplace As String = Nothing, Optional bFindAll As Boolean = True, Optional ByRef iReplacements As Integer = 0) As Object |
| 287 |
Dim iCount As Integer = iReplacements |
| 288 |
iReplacements += MyBase.FindStringInStringProperty(Description, sSearchString, sReplace, bFindAll) |
| 289 |
Return iReplacements - iCount |
| 290 |
End Function |
| 291 |
|
| 292 |
Public Overrides Function ReferencesKey(ByVal sKey As String) As Integer |
| 293 |
Dim iCount As Integer = 0 |
| 294 |
If Me.DependentKey = sKey Then iCount += 1 |
| 295 |
If Me.RestrictProperty = sKey Then iCount += 1 |
| 296 |
Return iCount |
| 297 |
End Function |
| 298 |
Public Overrides Function DeleteKey(ByVal sKey As String) As Boolean |
| 299 |
If Me.DependentKey = sKey Then Me.DependentKey = Nothing |
| 300 |
If Me.RestrictProperty = sKey Then Me.RestrictProperty = Nothing |
| 301 |
Return True |
| 302 |
End Function |
| 303 |
|
| 304 |
End Class |