summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/UICommon/XUISimpleList.cs
blob: 24b451375dce5c435b9fde99977e1c6df10b11d7 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using UILib;
using UnityEngine;
using System;
using System.Collections.Generic;

public class XUISimpleList : XUIObject , IXUISimpleList
{
    public enum Sorting
    {
        Horizontal,
        Vertical,
    }

    struct SimpleNode : IComparable<SimpleNode>
    {
        public Transform t;
        public Vector3 pos;

        public int CompareTo(SimpleNode other)
        {
            switch (sSorting)
            {
                case Sorting.Horizontal:
                    return sRevert * pos.x.CompareTo(other.pos.x);
                case Sorting.Vertical:
                    return sRevert * pos.y.CompareTo(other.pos.y);
            }
            return 0;
        }

        public static Sorting sSorting = Sorting.Horizontal;
        public static int sRevert = -1;

    }

    SimpleNode[] m_OriginDatas;

    public Sorting sorting = Sorting.Horizontal;
    public bool IsRevert = false;

    protected override void OnAwake()
    {
        base.OnAwake();

        int count = 0;
        for (int i = 0; i < transform.childCount; ++i)
        {
            Transform child = transform.GetChild(i);
            if (child && child.gameObject)
            {
                ++count;
            }
        }

        m_OriginDatas = new SimpleNode[count];

        for (int i = 0, j = 0; i < transform.childCount; ++i)
        {
            Transform child = transform.GetChild(i);
            if (child && child.gameObject)
            {
                m_OriginDatas[j].pos = child.localPosition;
                m_OriginDatas[j].t = child;
                ++j;
            }
        }

        SimpleNode.sSorting = sorting;
        SimpleNode.sRevert = IsRevert ? -1 : 1;

        Array.Sort(m_OriginDatas);
    }

    public void Refresh()
    {
        int j = 0;
        for (int i = 0; i < m_OriginDatas.Length; ++i)
        {
            if (m_OriginDatas[i].t == null || !m_OriginDatas[i].t.gameObject.activeSelf)
                continue;

            m_OriginDatas[i].t.localPosition = m_OriginDatas[j++].pos;
        }
    }
}