Mini Kabibi Habibi

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

using System;
using System.Windows;
using System.Windows.Threading;
using DevExpress.Xpf.DemoBase;
using DevExpress.Xpf.Gauges;
using System.Windows.Data;

namespace GaugesDemo {
    public partial class CarDashboard : GaugesDemoModule {
        CarDataGenerator dataGenerator = new CarDataGenerator();

        public override bool AllowRtl { get { return false; } }

        public CarDashboard() {
            InitializeComponent();
            Binding binding = new Binding("IsPressed") { Source = buttonAccelerate };
            BindingOperations.SetBinding(dataGenerator, CarDataGenerator.IsAcceleratePressedProperty, binding);
            binding = new Binding("IsPressed") { Source = buttonBrake };
            BindingOperations.SetBinding(dataGenerator, CarDataGenerator.IsBrakePressedProperty, binding);
            this.DataContext = dataGenerator;
        }
        void DemoModule_ModuleAppear(object sender, RoutedEventArgs e) {
            dataGenerator.Start();
        }
    }

    public class CarDataGenerator : DependencyObject {
        public static readonly DependencyProperty IsAcceleratePressedProperty = DependencyProperty.Register("IsAcceleratePressed",
            typeof(bool), typeof(CarDataGenerator), new PropertyMetadata(false));
        public static readonly DependencyProperty IsBrakePressedProperty = DependencyProperty.Register("IsBrakePressed",
            typeof(bool), typeof(CarDataGenerator), new PropertyMetadata(false));
        public static readonly DependencyProperty MaxSpeedProperty = DependencyProperty.Register("MaxSpeed",
            typeof(double), typeof(CarDataGenerator), new PropertyMetadata(120.0));
        public static readonly DependencyProperty SpeedProperty = DependencyProperty.Register("Speed",
            typeof(double), typeof(CarDataGenerator), new PropertyMetadata(0.0));
        public static readonly DependencyProperty NormalEngineTemperatureProperty = DependencyProperty.Register("NormalEngineTemperature",
            typeof(double), typeof(CarDataGenerator), new PropertyMetadata(85.0));
        public static readonly DependencyProperty MaxEngineTemperatureProperty = DependencyProperty.Register("MaxEngineTemperature",
            typeof(double), typeof(CarDataGenerator), new PropertyMetadata(130.0));
        public static readonly DependencyProperty CurrentEngineTemperatureProperty = DependencyProperty.Register("CurrentEngineTemperature",
            typeof(double), typeof(CarDataGenerator), new PropertyMetadata(20.0));
        public static readonly DependencyProperty TachometerMaxValueProperty = DependencyProperty.Register("TachometerMaxValue",
            typeof(double), typeof(CarDataGenerator), new PropertyMetadata(8000.0));
        public static readonly DependencyProperty TachometerValueProperty = DependencyProperty.Register("TachometerValue",
            typeof(double), typeof(CarDataGenerator), new PropertyMetadata(900.0));
        public static readonly DependencyProperty GearCountProperty = DependencyProperty.Register("GearCount",
            typeof(int), typeof(CarDataGenerator), new PropertyMetadata(6));
        public static readonly DependencyProperty GearProperty = DependencyProperty.Register("Gear",
            typeof(int), typeof(CarDataGenerator), new PropertyMetadata(0));
        public static readonly DependencyProperty FuelLevelProperty = DependencyProperty.Register("FuelLevel",
            typeof(double), typeof(CarDataGenerator), new PropertyMetadata(0.75));
        public static readonly DependencyProperty CurrentTimeProperty = DependencyProperty.Register("CurrentTime",
            typeof(string), typeof(CarDataGenerator), new PropertyMetadata(""));
        public static readonly DependencyProperty CurrentDateProperty = DependencyProperty.Register("CurrentDate",
            typeof(string), typeof(CarDataGenerator), new PropertyMetadata(""));


        public bool IsAcceleratePressed {
            get { return (bool)GetValue(IsAcceleratePressedProperty); }
            set { SetValue(IsAcceleratePressedProperty, value); }
        }
        public bool IsBrakePressed {
            get { return (bool)GetValue(IsBrakePressedProperty); }
            set { SetValue(IsBrakePressedProperty, value); }
        }
        public double MaxSpeed {
            get { return (double)GetValue(MaxSpeedProperty); }
            set { SetValue(MaxSpeedProperty, value); }
        }
        public double Speed {
            get { return (double)GetValue(SpeedProperty); }
            set { SetValue(SpeedProperty, value); }
        }
        public double NormalEngineTemperature {
            get { return (double)GetValue(NormalEngineTemperatureProperty); }
            set { SetValue(NormalEngineTemperatureProperty, value); }
        }
        public double MaxEngineTemperature {
            get { return (double)GetValue(MaxEngineTemperatureProperty); }
            set { SetValue(MaxEngineTemperatureProperty, value); }
        }
        public double CurrentEngineTemperature {
            get { return (double)GetValue(CurrentEngineTemperatureProperty); }
            set { SetValue(CurrentEngineTemperatureProperty, value); }
        }
        public double TachometerMaxValue {
            get { return (double)GetValue(TachometerMaxValueProperty); }
            set { SetValue(TachometerMaxValueProperty, value); }
        }
        public double TachometerValue {
            get { return (double)GetValue(TachometerValueProperty); }
            set { SetValue(TachometerValueProperty, value); }
        }
        public int GearCount {
            get { return (int)GetValue(GearCountProperty); }
            set { SetValue(GearCountProperty, value); }
        }
        public int Gear {
            get { return (int)GetValue(GearProperty); }
            set { SetValue(GearProperty, value); }
        }
        public double FuelLevel {
            get { return (double)GetValue(FuelLevelProperty); }
            set { SetValue(FuelLevelProperty, value); }
        }
        public string CurrentTime {
            get { return (string)GetValue(CurrentTimeProperty); }
            set { SetValue(CurrentTimeProperty, value); }
        }
        public string CurrentDate {
            get { return (string)GetValue(CurrentDateProperty); }
            set { SetValue(CurrentDateProperty, value); }
        }

        readonly DispatcherTimer timer = new DispatcherTimer();
        readonly DispatcherTimer timerInitialAnimation = new DispatcherTimer();
        readonly DispatcherTimer timerUpdateDateTime = new DispatcherTimer();

        double GearInteval { get { return 0.8 * (MaxSpeed / GearCount); } }

        public CarDataGenerator() {
            timer.Tick += new EventHandler(OnTimedEvent);
            timer.Interval = TimeSpan.FromMilliseconds(500);
            timerInitialAnimation.Tick += new EventHandler(OnTimedEventInitialAnimation);
            timerInitialAnimation.Interval = TimeSpan.FromMilliseconds(800);
            CurrentTime = DateTime.Now.ToShortTimeString();
            CurrentDate = DateTime.Now.ToShortDateString();
            timerUpdateDateTime.Interval = new TimeSpan(0, 0, 1);
            timerUpdateDateTime.Tick += new EventHandler(updateTimerAndDate);
            timerUpdateDateTime.Start();
        }
        void OnTimedEventInitialAnimation(object source, EventArgs e) {
            timerInitialAnimation.Stop();
            Speed = 0;
            TachometerValue = 0;
            timer.Start();
        }
        void OnTimedEvent(object source, EventArgs e) {
            Update(timer.Interval.TotalSeconds);
        }
        void Update(double deltaTime) {
            UpdateSpeed(10 * deltaTime);
            Gear = (int)Math.Min(GearCount, Math.Ceiling((Speed / GearInteval)));
            TachometerValue = 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) || (CurrentEngineTemperature < NormalEngineTemperature))
                CurrentEngineTemperature += TachometerValue / TachometerMaxValue / 2.5;
            else
                CurrentEngineTemperature -= (TachometerMaxValue - TachometerValue) / TachometerMaxValue / 2.5;
            CurrentEngineTemperature = Math.Min(MaxEngineTemperature, CurrentEngineTemperature);
        }
        void UpdateSpeed(double delta) {
            if(IsAcceleratePressed)
                Speed += delta;
            else
                if(IsBrakePressed)
                    Speed -= delta;
            Speed = Math.Max(0, Math.Min(MaxSpeed, Speed));
        }
        public void Start() {
            timer.Stop();
            Speed = MaxSpeed;
            TachometerValue = TachometerMaxValue;
            timerInitialAnimation.Start();
        }
        void updateTimerAndDate(object source, EventArgs e) {
            CurrentTime = DateTime.Now.ToShortTimeString();
            CurrentDate = DateTime.Now.ToShortDateString();
        }
    }
}