summaryrefslogtreecommitdiff
path: root/Assets/Scripts/UIFlip.cs
blob: 2cb705540ab2747a19eb5457724158a167fa8d60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Coffee.UIEffects
{
    [DisallowMultipleComponent]
    [AddComponentMenu("UI/UIEffects/UIFlip", 102)]
    public class UIFlip : BaseMeshEffect
    {
        [Tooltip("Flip horizontally.")] [SerializeField]
        private bool m_Horizontal = false;

        [Tooltip("Flip vertically.")] [SerializeField]
        private bool m_Veritical = false;

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Coffee.UIEffects.UIFlip"/> should be flipped horizontally.
        /// </summary>
        /// <value><c>true</c> if be flipped horizontally; otherwise, <c>false</c>.</value>
        public bool horizontal
        {
            get { return m_Horizontal; }
            set
            {
                if (m_Horizontal == value) return;
                m_Horizontal = value;
                SetEffectParamsDirty();
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Coffee.UIEffects.UIFlip"/> should be flipped vertically.
        /// </summary>
        /// <value><c>true</c> if be flipped horizontally; otherwise, <c>false</c>.</value>
        public bool vertical
        {
            get { return m_Veritical; }
            set
            {
                if (m_Veritical == value) return;
                m_Veritical = value;
                SetEffectParamsDirty();
            }
        }

        /// <summary>
        /// Call used to modify mesh.
        /// </summary>
        /// <param name="vh">VertexHelper.</param>
        public override void ModifyMesh(VertexHelper vh, Graphic graphic)
        {
            if (!isActiveAndEnabled) return;

            var vt = default(UIVertex);
            for (var i = 0; i < vh.currentVertCount; i++)
            {
                vh.PopulateUIVertex(ref vt, i);
                var pos = vt.position;
                vt.position = new Vector3(
                    m_Horizontal ? -pos.x : pos.x,
                    m_Veritical ? -pos.y : pos.y
                );
                vh.SetUIVertex(vt, i);
            }
        }
    }
}