SurfaceControl.cs :  » Game » SDL » SdlDotNet » Windows » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » SDL 
SDL » SdlDotNet » Windows » SurfaceControl.cs
#region License
/*
 * $RCSfile$
 * Copyright (C) 2005 David Hudson (jendave@yahoo.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#endregion License

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics.CodeAnalysis;

using SdlDotNet.Core;
using SdlDotNet.Graphics;
using SdlDotNet.Input;

namespace SdlDotNet.Windows{
    #region Class Documentation
    /// <summary>
    ///     Provides a simple Sdl Surface control allowing 
    ///     quick development of Windows Forms-based
    ///     Sdl Surface applications.
    /// </summary>
    #endregion Class Documentation
    [DefaultProperty("Image")]
    [ToolboxBitmap(typeof(Bitmap), "SurfaceControl.bmp")]
    public partial class SurfaceControl : System.Windows.Forms.PictureBox
    {
        #region Constructors

        /// <summary>
        /// Constructor
        /// </summary>
        public SurfaceControl()
        {
            InitializeComponent();
            BlitRequestHandler = Blit;
        }

        #endregion

        #region Private fields

        int lastX;
        int lastY;

        #endregion

        #region Public Methods

        /// <summary>
        /// Copies surface to this surface
        /// </summary>
        /// <param name="surface">surface to copy onto control</param>
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Justification = "Correct Spelling")]
        public void Blit(Surface surface)
        {
            if (surface == null)
            {
                throw new ArgumentNullException("surface");
            }
            try
            {
                if (this.InvokeRequired)
                {
                    Object[] args = { surface };
                    this.Invoke(BlitRequestHandler, args);
                }
                else
                {
                    this.Image = surface.Bitmap;
                }
            }
            catch (InvalidOperationException e)
            {
                e.ToString();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="surface"></param>
        public delegate void BlitEvent(Surface surface);
        private BlitEvent BlitRequestHandler;

        /// <summary>
        /// Copies surface onto this surface at a set position.
        /// </summary>
        /// <param name="sourceSurface"></param>
        /// <param name="destinationPosition"></param>
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Justification = "Correct Spelling")]
        public void Blit(Surface sourceSurface, System.Drawing.Point destinationPosition)
        {
            Surface destinationSurface = new Surface((Bitmap)this.Image);
            destinationSurface.Blit(sourceSurface, destinationPosition);
            this.Image = destinationSurface.Bitmap;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        public void KeyPressed(KeyEventArgs e)
        {
            this.OnKeyDown(e);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        public void KeyReleased(KeyEventArgs e)
        {
            this.OnKeyUp(e);
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// Raises the OnResize event
        /// </summary>
        /// <param name="e">Contains the event data</param>
        protected override void OnResize(EventArgs e)
        {
            if (!this.DesignMode)
            {
                SdlDotNet.Core.Events.Add(new VideoResizeEventArgs(this.Width, this.Height));
            }
            base.OnResize(e);
        }

        /// <summary>
        /// Raises the SizeChanged event
        /// </summary>
        /// <param name="e">Contains the event data</param>
        protected override void OnSizeChanged(EventArgs e)
        {
            if (!this.DesignMode)
            {
                SdlDotNet.Core.Events.Add(new VideoResizeEventArgs(this.Width, this.Height));
            }
            base.OnSizeChanged(e);
        }

        /// <summary>
        /// Raises the MouseDown event
        /// </summary>
        /// <param name="e">Contains the event data</param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (!this.DesignMode)
            {
                SdlDotNet.Core.Events.Add(new MouseButtonEventArgs(SurfaceControl.ConvertMouseButtons(e), true, (short)e.X, (short)e.Y));
            }
            base.OnMouseDown(e);
        }

        /// <summary>
        /// Raises the MouseUp event
        /// </summary>
        /// <param name="e">Contains the event data</param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (!this.DesignMode)
            {
                SdlDotNet.Core.Events.Add(new MouseButtonEventArgs(SurfaceControl.ConvertMouseButtons(e), false, (short)e.X, (short)e.Y));
            }
            base.OnMouseUp(e);
        }

        /// <summary>
        /// Raises the MouseWheel Event
        /// </summary>
        /// <param name="e">Contains the event data</param>
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            if (!this.DesignMode)
            {
                SdlDotNet.Core.Events.Add(new MouseButtonEventArgs(SurfaceControl.ConvertMouseButtons(e), false, (short)e.X, (short)e.Y));
            }
            base.OnMouseWheel(e);
        }

        /// <summary>
        /// Raises the MouseMove event
        /// </summary>
        /// <param name="e">Contains the event data</param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (!this.DesignMode)
            {
                //if (e.Button != MouseButtons.None)
                //{
                SdlDotNet.Core.Events.Add(new MouseMotionEventArgs(true, SurfaceControl.ConvertMouseButtons(e), (short)e.X, (short)e.Y, (short)(e.X - lastX), (short)(e.Y - lastY)));
                //}
                lastX = e.X;
                lastY = e.Y;
            }
            base.OnMouseMove(e);
        }

        /// <summary>
        /// Raises the 
        /// <see cref="E:System.Windows.Forms.Control.KeyDown"/> event.
        /// </summary>
        /// <param name="e">A 
        /// <see cref="T:System.Windows.Forms.KeyEventArgs"/> 
        /// that contains the event data.</param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            try
            {
                if (!this.DesignMode)
                {
                    SdlDotNet.Core.Events.Add(new KeyboardEventArgs((SdlDotNet.Input.Key)Enum.Parse(typeof(SdlDotNet.Input.Key), e.KeyCode.ToString()), (ModifierKeys)e.Modifiers, true));
                }
            }
            catch (ArgumentException ex)
            {
                ex.ToString();
            }
            base.OnKeyDown(e);
        }

        /// <summary>
        /// Raises the 
        /// <see cref="E:System.Windows.Forms.Control.KeyUp"/> 
        /// event.
        /// </summary>
        /// <param name="e">A 
        /// <see cref="T:System.Windows.Forms.KeyEventArgs"/> 
        /// that contains the event data.</param>
        protected override void OnKeyUp(KeyEventArgs e)
        {
            try
            {
                if (!this.DesignMode)
                {
                    SdlDotNet.Core.Events.Add(new KeyboardEventArgs((SdlDotNet.Input.Key)Enum.Parse(typeof(SdlDotNet.Input.Key), e.KeyCode.ToString()), (ModifierKeys)e.Modifiers, false));
                }
            }
            catch (ArgumentException ex)
            {
                ex.ToString();
            }
            base.OnKeyUp(e);
        }

        #endregion

        #region Private Methods

        private static MouseButton ConvertMouseButtons(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                return MouseButton.PrimaryButton;
            }
            else if (e.Button == MouseButtons.Right)
            {
                return MouseButton.SecondaryButton;
            }
            else if (e.Button == MouseButtons.Middle)
            {
                return MouseButton.MiddleButton;
            }
            else if (e.Button == MouseButtons.XButton1)
            {
                return MouseButton.WheelDown;
            }
            else if (e.Button == MouseButtons.XButton2)
            {
                return MouseButton.WheelUp;
            }
            else
            {
                return MouseButton.None;
            }
        }

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.