Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/VB/GaugesDemo.Wpf/Modules/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/WPF/VB/GaugesDemo.Wpf/Modules/CarDashboard.xaml.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Windows
Imports System.Windows.Threading
Imports DevExpress.Xpf.DemoBase
Imports DevExpress.Xpf.Gauges
Imports System.Windows.Data

Namespace GaugesDemo
	Partial Public Class CarDashboard
		Inherits GaugesDemoModule
		Private dataGenerator As New CarDataGenerator()

		Public Overrides ReadOnly Property AllowRtl() As Boolean
			Get
				Return False
			End Get
		End Property

		Public Sub New()
			InitializeComponent()
			Dim binding As New Binding("IsPressed") With {.Source = buttonAccelerate}
			BindingOperations.SetBinding(dataGenerator, CarDataGenerator.IsAcceleratePressedProperty, binding)
			binding = New Binding("IsPressed") With {.Source = buttonBrake}
			BindingOperations.SetBinding(dataGenerator, CarDataGenerator.IsBrakePressedProperty, binding)
			Me.DataContext = dataGenerator
		End Sub
		Private Sub DemoModule_ModuleAppear(ByVal sender As Object, ByVal e As RoutedEventArgs)
			dataGenerator.Start()
		End Sub
	End Class

	Public Class CarDataGenerator
		Inherits DependencyObject
		Public Shared ReadOnly IsAcceleratePressedProperty As DependencyProperty = DependencyProperty.Register("IsAcceleratePressed", GetType(Boolean), GetType(CarDataGenerator), New PropertyMetadata(False))
		Public Shared ReadOnly IsBrakePressedProperty As DependencyProperty = DependencyProperty.Register("IsBrakePressed", GetType(Boolean), GetType(CarDataGenerator), New PropertyMetadata(False))
		Public Shared ReadOnly MaxSpeedProperty As DependencyProperty = DependencyProperty.Register("MaxSpeed", GetType(Double), GetType(CarDataGenerator), New PropertyMetadata(120.0))
		Public Shared ReadOnly SpeedProperty As DependencyProperty = DependencyProperty.Register("Speed", GetType(Double), GetType(CarDataGenerator), New PropertyMetadata(0.0))
		Public Shared ReadOnly NormalEngineTemperatureProperty As DependencyProperty = DependencyProperty.Register("NormalEngineTemperature", GetType(Double), GetType(CarDataGenerator), New PropertyMetadata(85.0))
		Public Shared ReadOnly MaxEngineTemperatureProperty As DependencyProperty = DependencyProperty.Register("MaxEngineTemperature", GetType(Double), GetType(CarDataGenerator), New PropertyMetadata(130.0))
		Public Shared ReadOnly CurrentEngineTemperatureProperty As DependencyProperty = DependencyProperty.Register("CurrentEngineTemperature", GetType(Double), GetType(CarDataGenerator), New PropertyMetadata(20.0))
		Public Shared ReadOnly TachometerMaxValueProperty As DependencyProperty = DependencyProperty.Register("TachometerMaxValue", GetType(Double), GetType(CarDataGenerator), New PropertyMetadata(8000.0))
		Public Shared ReadOnly TachometerValueProperty As DependencyProperty = DependencyProperty.Register("TachometerValue", GetType(Double), GetType(CarDataGenerator), New PropertyMetadata(900.0))
		Public Shared ReadOnly GearCountProperty As DependencyProperty = DependencyProperty.Register("GearCount", GetType(Integer), GetType(CarDataGenerator), New PropertyMetadata(6))
		Public Shared ReadOnly GearProperty As DependencyProperty = DependencyProperty.Register("Gear", GetType(Integer), GetType(CarDataGenerator), New PropertyMetadata(0))
		Public Shared ReadOnly FuelLevelProperty As DependencyProperty = DependencyProperty.Register("FuelLevel", GetType(Double), GetType(CarDataGenerator), New PropertyMetadata(0.75))
		Public Shared ReadOnly CurrentTimeProperty As DependencyProperty = DependencyProperty.Register("CurrentTime", GetType(String), GetType(CarDataGenerator), New PropertyMetadata(""))
		Public Shared ReadOnly CurrentDateProperty As DependencyProperty = DependencyProperty.Register("CurrentDate", GetType(String), GetType(CarDataGenerator), New PropertyMetadata(""))


		Public Property IsAcceleratePressed() As Boolean
			Get
				Return CBool(GetValue(IsAcceleratePressedProperty))
			End Get
			Set(ByVal value As Boolean)
				SetValue(IsAcceleratePressedProperty, value)
			End Set
		End Property
		Public Property IsBrakePressed() As Boolean
			Get
				Return CBool(GetValue(IsBrakePressedProperty))
			End Get
			Set(ByVal value As Boolean)
				SetValue(IsBrakePressedProperty, value)
			End Set
		End Property
		Public Property MaxSpeed() As Double
			Get
				Return CDbl(GetValue(MaxSpeedProperty))
			End Get
			Set(ByVal value As Double)
				SetValue(MaxSpeedProperty, value)
			End Set
		End Property
		Public Property Speed() As Double
			Get
				Return CDbl(GetValue(SpeedProperty))
			End Get
			Set(ByVal value As Double)
				SetValue(SpeedProperty, value)
			End Set
		End Property
		Public Property NormalEngineTemperature() As Double
			Get
				Return CDbl(GetValue(NormalEngineTemperatureProperty))
			End Get
			Set(ByVal value As Double)
				SetValue(NormalEngineTemperatureProperty, value)
			End Set
		End Property
		Public Property MaxEngineTemperature() As Double
			Get
				Return CDbl(GetValue(MaxEngineTemperatureProperty))
			End Get
			Set(ByVal value As Double)
				SetValue(MaxEngineTemperatureProperty, value)
			End Set
		End Property
		Public Property CurrentEngineTemperature() As Double
			Get
				Return CDbl(GetValue(CurrentEngineTemperatureProperty))
			End Get
			Set(ByVal value As Double)
				SetValue(CurrentEngineTemperatureProperty, value)
			End Set
		End Property
		Public Property TachometerMaxValue() As Double
			Get
				Return CDbl(GetValue(TachometerMaxValueProperty))
			End Get
			Set(ByVal value As Double)
				SetValue(TachometerMaxValueProperty, value)
			End Set
		End Property
		Public Property TachometerValue() As Double
			Get
				Return CDbl(GetValue(TachometerValueProperty))
			End Get
			Set(ByVal value As Double)
				SetValue(TachometerValueProperty, value)
			End Set
		End Property
		Public Property GearCount() As Integer
			Get
				Return CInt(Fix(GetValue(GearCountProperty)))
			End Get
			Set(ByVal value As Integer)
				SetValue(GearCountProperty, value)
			End Set
		End Property
		Public Property Gear() As Integer
			Get
				Return CInt(Fix(GetValue(GearProperty)))
			End Get
			Set(ByVal value As Integer)
				SetValue(GearProperty, value)
			End Set
		End Property
		Public Property FuelLevel() As Double
			Get
				Return CDbl(GetValue(FuelLevelProperty))
			End Get
			Set(ByVal value As Double)
				SetValue(FuelLevelProperty, value)
			End Set
		End Property
		Public Property CurrentTime() As String
			Get
				Return CStr(GetValue(CurrentTimeProperty))
			End Get
			Set(ByVal value As String)
				SetValue(CurrentTimeProperty, value)
			End Set
		End Property
		Public Property CurrentDate() As String
			Get
				Return CStr(GetValue(CurrentDateProperty))
			End Get
			Set(ByVal value As String)
				SetValue(CurrentDateProperty, value)
			End Set
		End Property

		Private ReadOnly timer As New DispatcherTimer()
		Private ReadOnly timerInitialAnimation As New DispatcherTimer()
		Private ReadOnly timerUpdateDateTime As New DispatcherTimer()

		Private ReadOnly Property GearInteval() As Double
			Get
				Return 0.8 * (MaxSpeed / GearCount)
			End Get
		End Property

		Public Sub New()
			AddHandler timer.Tick, AddressOf OnTimedEvent
			timer.Interval = TimeSpan.FromMilliseconds(500)
			AddHandler timerInitialAnimation.Tick, AddressOf OnTimedEventInitialAnimation
			timerInitialAnimation.Interval = TimeSpan.FromMilliseconds(800)
			CurrentTime = DateTime.Now.ToShortTimeString()
			CurrentDate = DateTime.Now.ToShortDateString()
			timerUpdateDateTime.Interval = New TimeSpan(0, 0, 1)
			AddHandler timerUpdateDateTime.Tick, AddressOf updateTimerAndDate
			timerUpdateDateTime.Start()
		End Sub
		Private Sub OnTimedEventInitialAnimation(ByVal source As Object, ByVal e As EventArgs)
			timerInitialAnimation.Stop()
			Speed = 0
			TachometerValue = 0
			timer.Start()
		End Sub
		Private Sub OnTimedEvent(ByVal source As Object, ByVal e As EventArgs)
			Update(timer.Interval.TotalSeconds)
		End Sub
		Private Sub Update(ByVal deltaTime As Double)
			UpdateSpeed(10 * deltaTime)
			Gear = CInt(Fix(Math.Min(GearCount, Math.Ceiling((Speed / GearInteval)))))
			TachometerValue = If(Gear > 0, TachometerMaxValue * (Speed - GearInteval * (Gear - 1)) / GearInteval, 0)
			TachometerValue = Math.Max(0, Math.Min(TachometerMaxValue, TachometerValue))
			FuelLevel -= TachometerValue / TachometerMaxValue / 1000
			If ((TachometerMaxValue / 2) < TachometerValue) OrElse (CurrentEngineTemperature < NormalEngineTemperature) Then
				CurrentEngineTemperature += TachometerValue / TachometerMaxValue / 2.5
			Else
				CurrentEngineTemperature -= (TachometerMaxValue - TachometerValue) / TachometerMaxValue / 2.5
			End If
			CurrentEngineTemperature = Math.Min(MaxEngineTemperature, CurrentEngineTemperature)
		End Sub
		Private Sub UpdateSpeed(ByVal delta As Double)
			If IsAcceleratePressed Then
				Speed += delta
			Else
				If IsBrakePressed Then
					Speed -= delta
				End If
			End If
			Speed = Math.Max(0, Math.Min(MaxSpeed, Speed))
		End Sub
		Public Sub Start()
			timer.Stop()
			Speed = MaxSpeed
			TachometerValue = TachometerMaxValue
			timerInitialAnimation.Start()
		End Sub
		Private Sub updateTimerAndDate(ByVal source As Object, ByVal e As EventArgs)
			CurrentTime = DateTime.Now.ToShortTimeString()
			CurrentDate = DateTime.Now.ToShortDateString()
		End Sub
	End Class
End Namespace