Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/WinForms/CS/GaugesMainDemo/Modules/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/WinForms/CS/GaugesMainDemo/Modules/Styles.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraGauges.Base;
using DevExpress.XtraGauges.Core.Model;
using DevExpress.XtraGauges.Win;
using DevExpress.XtraGauges.Win.Base;
using DevExpress.XtraGauges.Win.Gauges.Digital;
using DevExpress.XtraTab;

namespace DevExpress.XtraGauges.Demos {
    public partial class GaugesStyles : TutorialControl {
        int animationLockCounterCore = 0;
        Timer timer;
        Random random = new Random(DateTime.Now.Millisecond);

        bool IsAnimationLocked { get { return animationLockCounterCore > 0; } }

        public GaugesStyles() {
            InitializeComponent();
            this.Load += new EventHandler(OnLoad);
        }
        void OnLoad(object sender, EventArgs e) {
            timer = new Timer();
            timer.Interval = 166;
            timer.Tick += new EventHandler(OnTimerTick);
            timer.Start();
        }
        void OnTimerTick(object sender, EventArgs e) {
            if(IsAnimationLocked) return;
            XtraTabPage activePage = xtraTabControl1.SelectedTabPage;
            if(!xtraTabControl1.Visible || activePage == null) return;
            LockAnimation();
            List<GaugeControl> gauges = CollectChildGauges(activePage);
            foreach(GaugeControl gauge in gauges) DoAnimation(gauge);
            UnlockAnimation();
        }
        void UnlockAnimation() {
            animationLockCounterCore--;
        }
        void LockAnimation() {
            animationLockCounterCore++;
        }
        void DoAnimation(GaugeControl gauge) {
            foreach(IGauge gb in gauge.Gauges) {
                ICircularGauge cGauge = gb as ICircularGauge;
                if(cGauge!=null) {
                    foreach(IScale scale in cGauge.Scales) scale.Value = AnimateScaleValue(scale);
                }
                ILinearGauge lGauge = gb as ILinearGauge;
                if(lGauge!=null) {
                    foreach(IScale scale in lGauge.Scales) scale.Value = AnimateScaleValue(scale);
                }
                DigitalGauge dGauge = gb as DigitalGauge;
                if(dGauge != null) {
                    dGauge.Text = AnimateStringValue(dGauge);
                }
            }
        }
        List<GaugeControl> CollectChildGauges(Control parentControl) {
            List<GaugeControl> result = new List<GaugeControl>();
            foreach(Control control in parentControl.Controls) {
                if(control is GaugeControl) result.Add(control as GaugeControl);
                else if(control.Controls.Count > 0) {
                    result.AddRange(CollectChildGauges(control));
                }
            }
            return result;
        }
        float AnimateScaleValue(IScale scale) {
            float deviation = ((float)random.NextDouble() - (scale as IConvertibleScale).Percent);
            return scale.Value + (scale.ScaleLength * 0.025f) * deviation;
        }
        string AnimateStringValue(DigitalGauge dGauge) {
            int value = DateTime.Now.Second * 1000 + DateTime.Now.Millisecond;
            return value.ToString();
        }
    }
}