Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Serializable> Public Structure GoNumber : Implements IXmlSerializable
- <XmlElement(ElementName:="Value")> Public Property Value As Single
- Get
- Return Go
- End Get
- Set(value As Single)
- Go = value
- If Clamped Then
- If Go <= Minimum Then Go = Minimum
- If Go >= Maximum Then Go = Maximum
- End If
- Now = Go
- End Set
- End Property
- Public Property GoName As String
- Get
- Return _name
- End Get
- Set(value As String)
- _name = value
- End Set
- End Property
- Public Property Go As Single
- Get
- Return _Go
- End Get
- Set(Value As Single)
- _Go = Value
- ' SlideFunction = AddressOf DoSlide
- End Set
- End Property
- Private _Go As Single
- Public Now, Last, DefaultValue, Minimum, Maximum, Percentage As Single
- Public Clamped As Boolean
- Public Precision As Single
- Public Change, Percent, Start, InverseDifference As Single
- Public Velocity, Acceleration, Difference As Single
- Public Direction As Integer
- Public SpaceName As String
- Private _name As String
- #Region "Initializers"
- Public Sub New(_Value As Single)
- _name = ""
- Now = _Value : Go = _Value : Last = _Value : DefaultValue = _Value
- Minimum = 0
- Maximum = 1
- Precision = 1
- Difference = 0.0001
- End Sub
- Public Sub New(_Value As Single, Optional _Min As Single = Nothing, Optional _Max As Single = Nothing,
- Optional _Precision As Single = 1)
- Now = _Value : Go = _Value : Last = _Value : DefaultValue = _Value
- Precision = IIf(_Precision = 0, 1, _Precision)
- Difference = 0.0001
- Type = _GoType
- If Type = GoType.ZeroOne Then
- Minimum = 0 : Maximum = 1 : Precision = 0.01
- End If
- If _Min <> Nothing Then Minimum = _Min
- If _Max <> Nothing Then Maximum = _Max
- If Minimum <> Nothing And Maximum <> Nothing Then Clamped = True
- End Sub
- #End Region
- Public Sub [Set](_Value As Single)
- _Go = _Value
- End Sub
- Public Sub GoNow(NewValue As Single)
- Now = NewValue
- Go = NewValue
- End Sub
- Public Sub SlideWays(Up As Single, Down As Single, Optional ByVal Difference As Single = 0.0001)
- Slide(IIf(Now < Go, Up, Down)) ', Difference)
- End Sub
- Public Sub Slide(ByVal Percentage As Single, Optional ByVal Difference As Single = 0.0001)
- Change = Go - Now
- ' Adjust Velocity smoothly
- Velocity += ((Change - Velocity) * 0.5) * Time.Factor100
- 'Move now towards
- Now += ((Velocity) * Percentage) * Time.Factor100
- 'If the new difference is less than specified Difference, force Here to Go
- 'Formerly: If Math.Abs(Changed) < Difference Then Now = Go
- Now = Now * (-(Math.Abs(Change) >= Difference)) +
- Go * (-(Math.Abs(Change) < Difference))
- End Sub
- Public Sub SlideClamp(ByVal Percentage As Single, Optional ByVal Difference As Single = 0.0001)
- Slide(Clamp(Percentage, 0, 1))
- End Sub
- Public Sub SlideZero(Outward As Single, ToZero As Single)
- ' Negate Conditional Expressions to make them 0/1
- Slide(-(Go > 0) * (Outward * -(Go >= Now)) +
- -(Go <= 0) * (ToZero * -(Go <= Now)) +
- (ToZero * -(Go = 0)))
- ' Formerly Before Branchless Optimization:
- 'If Go = 0 Then
- ' Slide(ToZero)
- 'Else
- ' Slide(IIf(Go > 0, IIf(Go >= Now, Outward, ToZero), IIf(Go <= Now, Outward, ToZero)))
- 'End If
- End Sub
- #Region "Glide Functions"
- Public Sub GlideStart(_Now As Single, _Go As Single, _Velocity As Single,
- Optional _Acceleration As Single = 0)
- Now = _Now : Start = _Now : Go = _Go : Difference = Go - Now
- Velocity = _Velocity : Acceleration = _Acceleration
- Direction = Math.Sign(Difference)
- If Difference = 0 Then Difference = 1
- InverseDifference = 1 / Difference
- Change = 0
- End Sub
- Public Sub Glide()
- 'If Go = Now Then Go = Now + 1
- Velocity += Acceleration * Time.Factor
- Change += Velocity * Time.Factor
- Percent = ABS(Change * InverseDifference) ' Divide via multiplying a float
- Now = Start + (Sine(Percent * 90) * Difference)
- If ABS(Go - Now) < 0.1 Then Velocity *= 0.8
- If ABS(Go - Now) < 0.01 Then
- Velocity = 0
- Acceleration = 0
- End If
- 'If Velocity < 0.01 Then Velocity = 0
- End Sub
- #End Region
- #Region "Operators and XML"
- Public Shared Operator =(This As GoNumber, That As Single) As Boolean
- Return This.Go = That
- End Operator
- Public Shared Operator <>(This As GoNumber, That As Single) As Boolean
- Return This.Go <> That
- End Operator
- Public Shared Operator =(This As GoNumber, That As GoNumber) As Boolean
- Return This.Go = That.Go
- End Operator
- Public Shared Operator <>(This As GoNumber, That As GoNumber) As Boolean
- Return This.Go <> That.Go
- End Operator
- Public Sub SetName(NewName As String)
- _name = NewName
- End Sub
- Public Function GetSchema() As XmlSchema Implements IXmlSerializable.GetSchema
- Return Nothing ' This Will Probably Always Be Nothing
- End Function
- Public Sub ReadXml(Reader As XmlReader) Implements IXmlSerializable.ReadXml
- With Reader
- _Go = .GetAttribute("Value")
- 'Minimum = .GetAttribute("Min")
- 'Maximum = .GetAttribute("Max")
- 'Precision = .GetAttribute("Precision")
- End With
- End Sub
- Public Sub WriteXml(Writer As XmlWriter) Implements IXmlSerializable.WriteXml
- 'Writer.WriteAttributeString("Value", Go)
- With Writer
- .WriteStartElement(GoName)
- .WriteAttributeString("Value", _Go)
- '.WriteAttributeString("Min", Minimum)
- '.WriteAttributeString("Max", Maximum)
- '.WriteAttributeString("Precision", Precision)
- .WriteEndElement()
- End With
- End Sub
- Public Overrides Function ToString() As String
- Return $"{_name} = {Math.Round(Now, 2)}{vbTab} -> {Math.Round(Go, 2)} {vbTab}({Math.Round(Velocity, 2)})"
- End Function
- #End Region
- End Structure
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement