Click here to Skip to main content
15,883,853 members
Articles / Desktop Programming / Windows Forms

BSEtunes

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
24 Apr 2010CPOL4 min read 64.6K   4.3K   58  
BSEtunes is a MySQL based, full manageable, networkable single or multiuser jukebox application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using BSE.Platten.BO;
using BSE.Platten.Audio;
using System.Drawing.Imaging;
using BSE.Platten.Common;
using System.Globalization;
using BSE.Platten.Tunes.Properties;


namespace BSE.Platten.Tunes
{
    public partial class TrackInfo : UserControl
    {
        #region Delegates
        private delegate void DelegateRefreshTrackInfo();
        #endregion

        #region Events
        public event EventHandler<AlbumSelectEventArgs> AlbumSelecting;
        #endregion

        #region FieldsPrivate

        private Image m_imgTrackInfoBase;
        private Image m_imgOldThumbNail;
        private Image m_imgNewThumbNail;
        private System.Timers.Timer m_tmrFadeInTrackImage;
        private CEnvironment m_environment;
        private BSE.Platten.BO.CTrack m_currentTrack;
        private BSE.Platten.Audio.PlayMode m_playMode;
        private Queue<TrackInfoData> m_trackInfoDataToSelect = new Queue<TrackInfoData>(2);
        private WorkingState m_workingState;
        private Rectangle m_ImageRectangle;
        private float m_fTransparencyOut = 1.0f;
        private float m_fTransparencyCountOut = 1.0f;
        private float m_fTransparencyIn = 1.0f;
        private float m_fTransparencyCountIn;

        #endregion

        public CPlayerManager PlayerManager
        {
            get { return this.m_playerManager; }
            set { this.m_playerManager = value; }
        }

        public CEnvironment Environment
        {
            get { return this.m_environment; }
            set { this.m_environment = value; }
        }

        public TrackInfo()
        {
            InitializeComponent();

            this.m_lblInterpret.Visible = false;
            this.m_lblAlbum.Visible = false;
            this.m_lblTitle.Visible = false;
            this.m_pnlNotes.Visible = false;
            this.m_pnlNotes.IsClickable = false;
            this.m_picCover.Size = CCoverData.ThumbnailSize;
            this.m_imgTrackInfoBase = Properties.Resources.TrackInfo;
            this.m_picCover.Image = this.m_imgTrackInfoBase;

            this.m_tmrFadeInTrackImage = new System.Timers.Timer();
            this.m_tmrFadeInTrackImage.Interval = 10;
            this.m_tmrFadeInTrackImage.AutoReset = true;
            this.m_tmrFadeInTrackImage.Enabled = false;
            this.m_tmrFadeInTrackImage.Elapsed += new System.Timers.ElapsedEventHandler(FadeInTrackImageElapsed);

            this.m_ImageRectangle = new Rectangle(
                0,
                0,
                CCoverData.ThumbnailSize.Width,
                CCoverData.ThumbnailSize.Height);
        }

        #region MethodsProtected

        protected void OnAlbumSelecting(AlbumSelectEventArgs e)
        {
            if (AlbumSelecting != null)
            {
                AlbumSelecting(this, e);
            }
        }

        #endregion

        #region MethodsPrivate

        private void CTrackInfoBaseLoad(object sender, EventArgs e)
        {
            if (this.m_playerManager != null)
            {
                this.m_playerManager.PlayerManagerTrackChanged += new EventHandler<PlayerManagerStatusChangedEventArgs>(PlayerManagerTrackChanged);
            }
        }

        private void PlayerManagerTrackChanged(object sender, BSE.Platten.Audio.PlayerManagerStatusChangedEventArgs e)
        {
            this.m_lblInterpret.Visible = false;
            this.m_lblAlbum.Visible = false;
            this.m_lblTitle.Visible = false;
            this.m_pnlNotes.IsClickable = false;
            this.m_currentTrack = null;
            if (e.CurrentTrack != null)
            {
                this.m_playMode = e.PlayMode;
                if (this.m_picCover.Image != null)
                {
                    this.m_imgOldThumbNail = this.m_picCover.Image;
                }

                SetNotesPanelColors(this.m_playMode);

                TrackInfoData trackInfoData = new TrackInfoData();
                trackInfoData.CurrentTrack = e.CurrentTrack.Clone() as CTrack;
                trackInfoData.Environment = this.m_environment;
                this.m_trackInfoDataToSelect.Enqueue(trackInfoData);
                DequeueTrackInfoData();
            }
        }

        private void FadeInTrackImageElapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            m_fTransparencyOut = this.m_fTransparencyCountOut;
            m_fTransparencyIn = this.m_fTransparencyCountIn;
            if (this.m_fTransparencyCountOut > 0.05)
            {
                this.m_fTransparencyCountOut -= 0.05f;
                this.m_fTransparencyCountIn += 0.05f;
            }
            else
            {
                this.m_fTransparencyCountOut = 1.0f;
                this.m_fTransparencyCountIn = 0.0f;

                m_fTransparencyOut = 0.0f;
                m_fTransparencyIn = 1.0f;

                this.m_tmrFadeInTrackImage.Enabled = false;

                if (this.m_picCover.Image.Equals(this.m_imgNewThumbNail) == false)
                {
                    this.m_picCover.Image = this.m_imgNewThumbNail;
                }
            }
            this.m_picCover.Invalidate();
        }
        
        private void PicCoverPaint(object sender, PaintEventArgs e)
        {
            if (this.m_imgNewThumbNail != null)
            {
                using (ImageAttributes imageAttributes = new ImageAttributes())
                {
                    //Set alpha in the ColorMatrix
                    ColorMatrix colorMatrix = new ColorMatrix();
                    colorMatrix.Matrix33 = m_fTransparencyIn;

                    // Set color matrix of imageAttributes
                    imageAttributes.SetColorMatrix(
                        colorMatrix,
                        ColorMatrixFlag.Default,
                        ColorAdjustType.Bitmap);

                    e.Graphics.DrawImage(
                        this.m_imgNewThumbNail,
                        this.m_ImageRectangle,
                        0,
                        0,
                        CCoverData.ThumbnailSize.Width,
                        CCoverData.ThumbnailSize.Height,
                        GraphicsUnit.Pixel,
                        imageAttributes);
                }
            }

            if (this.m_imgOldThumbNail != null)
            {
                using (ImageAttributes imageAttributes = new ImageAttributes())
                {
                    //Set alpha in the ColorMatrix
                    ColorMatrix colorMatrix = new ColorMatrix();
                    colorMatrix.Matrix33 = m_fTransparencyOut;

                    // Set color matrix of imageAttributes
                    imageAttributes.SetColorMatrix(
                        colorMatrix,
                        ColorMatrixFlag.Default,
                        ColorAdjustType.Bitmap);

                    e.Graphics.DrawImage(
                        this.m_imgOldThumbNail,
                        this.m_ImageRectangle,
                        0,
                        0,
                        CCoverData.ThumbnailSize.Width,
                        CCoverData.ThumbnailSize.Height,
                        GraphicsUnit.Pixel,
                        imageAttributes);
                }
            }
        }
        
        private void ButtonToAlbumClick(object sender, EventArgs e)
        {
            if (this.m_currentTrack != null)
            {
                OnAlbumSelecting(new AlbumSelectEventArgs(this.m_currentTrack.TitelId));
            }
        }

        private void SetNotesPanelColors(PlayMode playMode)
        {
            switch (playMode)
            {
                case PlayMode.CD:
                    this.m_pnlNotes.BorderColor = Color.FromArgb(173,77,13);
                    this.m_pnlNotes.ColorGradientBegin = Color.FromArgb(249,202,164);
                    this.m_pnlNotes.ColorGradientEnd = Color.FromArgb(244,149,86);
                    break;
                case PlayMode.Playlist:
                    this.m_pnlNotes.BorderColor = Color.FromArgb(167,29,35);
                    this.m_pnlNotes.ColorGradientBegin = Color.FromArgb(241,174,175);
                    this.m_pnlNotes.ColorGradientEnd = Color.FromArgb(228,101,108);
                    break;
                case PlayMode.Random:
                    this.m_pnlNotes.BorderColor = Color.FromArgb(40, 81, 142);
                    this.m_pnlNotes.ColorGradientBegin = Color.FromArgb(180, 199, 237);
                    this.m_pnlNotes.ColorGradientEnd = Color.FromArgb(115, 156, 217);
                    break;
                default:
                    this.m_pnlNotes.BorderColor = Color.FromArgb(55, 126, 45);
                    this.m_pnlNotes.ColorGradientBegin = Color.FromArgb(193, 231, 187);
                    this.m_pnlNotes.ColorGradientEnd = Color.FromArgb(132, 209, 123);
                    break;
            }
        }

        private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            TrackInfoData trackInfoData = e.Argument as TrackInfoData;
            if ((trackInfoData != null) && (trackInfoData.Environment != null) && (trackInfoData.CurrentTrack != null))
            {
                CTunesBusinessObject tunesBusinessObject = new CTunesBusinessObject(trackInfoData.Environment.GetConnectionString());
                try
                {
                    e.Result = tunesBusinessObject.GetTrackWithThumbNailById(trackInfoData.CurrentTrack);
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }

        private void BackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                GlobalizedMessageBox.Show(this, e.Error.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                CTrack currentTrack = e.Result as CTrack;
                if (currentTrack != null)
                {
                    if (this.m_pnlNotes.Visible == false)
                    {
                        this.m_pnlNotes.Visible = true;
                    }
                    this.m_currentTrack = currentTrack;
                    this.m_pnlNotes.IsClickable = true;
                    if (String.IsNullOrEmpty(currentTrack.Interpret) == false)
                    {
                        this.m_lblInterpret.Visible = true;
                        this.m_lblInterpret.Text = String.Format(
                            CultureInfo.CurrentUICulture,
                            Resources.IDS_TrackInfoInterpret, currentTrack.Interpret);
                    }
                    if (String.IsNullOrEmpty(currentTrack.Album) == false)
                    {
                        this.m_lblAlbum.Visible = true;
                        this.m_lblAlbum.Text = String.Format(
                            CultureInfo.CurrentUICulture,
                            Resources.IDS_TrackInfoAlbum, currentTrack.Album);
                    }
                    if (this.m_playMode != PlayMode.CD)
                    {
                        if (String.IsNullOrEmpty(currentTrack.Title) == false)
                        {
                            this.m_lblTitle.Visible = true;
                            this.m_lblTitle.Text = String.Format(
                                CultureInfo.CurrentUICulture,
                                Resources.IDS_TrackInfoTrack, currentTrack.Title);
                        }
                    }
                    if (currentTrack.ThumbNail != null)
                    {
                        this.m_imgNewThumbNail = currentTrack.ThumbNail;
                    }
                    else
                    {
                        this.m_imgNewThumbNail = this.m_imgTrackInfoBase;
                    }
                    this.m_tmrFadeInTrackImage.Enabled = true;
                    this.m_workingState = WorkingState.None;
                    DequeueTrackInfoData();
                }
            }
        }

        private void DequeueTrackInfoData()
        {
            if (this.m_trackInfoDataToSelect.Count > 0)
            {
                if (this.m_workingState == WorkingState.Working)
                {
                    return;
                }
                else
                {
                    this.m_workingState = WorkingState.Working;
                    TrackInfoData trackInfoData = this.m_trackInfoDataToSelect.Dequeue();
                    if (trackInfoData != null)
                    {
                        this.m_backgroundWorker.RunWorkerAsync(trackInfoData);
                    }
                }
            }
        }

        internal class TrackInfoData
        {
            #region FieldsPrivate
            private CEnvironment m_environment;
            private CTrack m_currentTrack;
            #endregion

            #region Properties
            public CEnvironment Environment
            {
                get { return this.m_environment; }
                set { this.m_environment = value; }
            }
            public CTrack CurrentTrack
            {
                get { return this.m_currentTrack; }
                set { this.m_currentTrack = value; }
            }
            #endregion
        }

        #endregion
        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions