awesome-inc / FontAwesome.Sharp

A library for using Font Awesome in WPF & Windows Forms applications
Apache License 2.0
385 stars 89 forks source link

How to rotate icon? #4

Closed VoidVolker closed 6 years ago

VoidVolker commented 7 years ago

How to rotate icon? I'm using IconPictureBox

VoidVolker commented 7 years ago

Founded next solution:

    /// <summary>
    /// FontAwesome icon
    /// </summary>
    public class FAIcon : IconPictureBox
    {
        /// <summary>
        /// Flip flags
        /// </summary>
        [Flags]
        public enum IconFlip : byte
        {
            /// <summary>
            /// Flip off
            /// </summary>
            None = 0x0,
            /// <summary>
            /// Horizontal flip
            /// </summary>
            Horizontal = 0x1,
            /// <summary>
            /// Vertical flip
            /// </summary>
            Vertical = 0x2,
            /// <summary>
            /// Full flip
            /// </summary>
            Full = 0x4
        }

        private IconFlip _Flip;

        /// <summary>
        /// Icon flip
        /// </summary>
        [Category("Transform"), Description("Flip options")]
        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
        [DefaultValue(IconFlip.None)]
        public IconFlip Flip
        {
            get { return _Flip; }
            set
            {
                if (_Flip == value) { return; }
                _Flip = value;
                Invalidate();
            }
        }

        private int _Rotation = 0;

        /// <summary>
        /// Rotation angle in degrees, ±360°
        /// </summary>
        [Category("Transform"), Description("Rotation angle in degrees, ±360°")]
        [EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
        [DefaultValue(0)]
        public int Rotation
        {
            get { return _Rotation; }
            set {
                if (_Rotation == value) { return; }
                _Rotation = value % 360;
                Invalidate();
            }
        }

        /// <summary>
        /// FontAwesome icon
        /// </summary>
        public FAIcon() : base()
        {

        }

        /// <summary>
        /// OnPaint
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            switch (_Flip)
            {
                case (IconFlip.Horizontal):
                    // Flip the X-Axis
                    e.Graphics.ScaleTransform(-1.0F, 1.0F);
                    // Translate the drawing area accordingly
                    e.Graphics.TranslateTransform(-(float)Width, 0.0F);
                    break;

                case (IconFlip.Vertical):
                    // Flip the Y-Axis
                    e.Graphics.ScaleTransform(1.0F, -1.0F);
                    // Translate the drawing area accordingly
                    e.Graphics.TranslateTransform(0.0F, -(float)Height);
                    break;

                case (IconFlip.Full):
                    e.Graphics.ScaleTransform(-1.0F, -1.0F);
                    e.Graphics.TranslateTransform(-(float)Width, -(float)Height);
                    break;
            }

            if(_Rotation != 0)
            {
                float mx = Width / 2
                    , my = Height / 2
                ;
                e.Graphics.TranslateTransform( mx, my );
                e.Graphics.RotateTransform( _Rotation );
                e.Graphics.TranslateTransform( -mx, -my );
            }

            base.OnPaint(e);
        }
    }
mkoertgen commented 7 years ago

Right now, we have no rotation/flip support for Windows Forms.

Yet the solution you provided looks good. Would it be ok for you to have me adding this to the code base? Or would you prefer to open a merge request?

VoidVolker commented 7 years ago

Yeah, feel free to do it. I think you can better integrate this code. Also, what about full documentation with more examples? It was a bit difficult to find right code for using in my apo. Also, a bit later I'll add code for free rotation of icon.

VoidVolker commented 7 years ago

Done - I'm updated code with rotation option for any angle.

mkoertgen commented 7 years ago

Thx, i should find time integrating the rotation/flip code during the next few days.

And, yes, concerning the docs you are right that especially the Windows Forms part lacks a lot of examples and documentation.

mkoertgen commented 6 years ago

@VoidVolker Cleaned up the cache. ReSharper makes refactoring boilerplate code like e.g. IEquatable<> quite safe.

Added coding style conventions (Visual Studio defaults), for consistency with other IDEs like VSCode).

Adding flip/rotate support for the other controls right now...

VoidVolker commented 6 years ago

Ok, thanks! Was very busy last few weeks.

mkoertgen commented 6 years ago

We all are, right? ;-)

VoidVolker commented 6 years ago

Yeah :)

mkoertgen commented 6 years ago

Added rotate & flip for all 6 windows forms control. Fixed designer serialization along the way.

Should be done with 2450d8297bb4f0447dadd23433722e2aaa530ed (version 4.7.1).