少年修仙传客户端基础资源
dabaoji
2025-06-09 8ee0256378cbf5dbc9d76ed10b60b65a844ef4dd
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using UnityEditor;
using UnityEngine;
using System;
using qtools.qhierarchy.pdata;
using System.Text;
 
namespace qtools.qhierarchy.phelper
{
    public class QComponentsOrderList
    {
        // PRIVATE
        private EditorWindow window;
        private Texture2D dragButton;
        private bool dragAndDrop = false;
        private float dragOffset;
        private int originalDragIndex;
        private Color backgroundColor;
 
        // CONSTRUCTOR
        public QComponentsOrderList (EditorWindow window)
        {            
            this.window = window;
            dragButton = QResources.getInstance().getTexture(QTexture.QDragButton);
            backgroundColor = QResources.getInstance().getColor(QColor.BackgroundDark);
        }
        
        // PUBLIC
        public void draw(Rect rect, string[] componentIds)
        {
            Event currentEvent = Event.current;
 
            int currentMouseIndex = Mathf.Clamp(Mathf.RoundToInt((currentEvent.mousePosition.y - dragOffset - rect.y) / 18), 0, componentIds.Length - 1);
 
            if (dragAndDrop && currentEvent.type == EventType.MouseUp)      
            {
                dragAndDrop = false;
                window.Repaint();
 
                if (currentMouseIndex != originalDragIndex)
                {
                    string newIconOrder = "";
                    for (int j = 0; j < componentIds.Length; j++)
                    {
                        if (j == currentMouseIndex) 
                        {
                            if (j > originalDragIndex)
                            {
                                newIconOrder += componentIds[j] + ";";
                                newIconOrder += componentIds[originalDragIndex] + ";";
                            }
                            else
                            {
                                newIconOrder += componentIds[originalDragIndex] + ";";
                                newIconOrder += componentIds[j] + ";";
                            }
                        }
                        else if (j != originalDragIndex) 
                        {
                            newIconOrder += componentIds[j] + ";";
                        }
                    }
                    newIconOrder = newIconOrder.TrimEnd(';');
                    QSettings.getInstance().set(QSetting.ComponentsOrder, newIconOrder);
                    componentIds = newIconOrder.Split(';');
                }
            }
            else if (dragAndDrop && currentEvent.type == EventType.MouseDrag)
            {
                window.Repaint();
            }
 
            for (int i = 0; i < componentIds.Length; i++)
            {
                QHierarchyComponentEnum type = (QHierarchyComponentEnum)int.Parse(componentIds[i]);
                
                Rect curRect = new Rect(rect.x, rect.y + 18 * i, rect.width, 16);
                
                if (!dragAndDrop && currentEvent.type == EventType.MouseDown && curRect.Contains(currentEvent.mousePosition))
                {
                    dragAndDrop = true;
                    originalDragIndex = i;
                    dragOffset = currentEvent.mousePosition.y - curRect.y;
                    Event.current.Use();
                }
 
                if (dragAndDrop)
                {
                    if (originalDragIndex != i)
                    {
                             if (i < originalDragIndex && currentMouseIndex <= i) curRect.y += 18;
                        else if (i > originalDragIndex && currentMouseIndex >= i) curRect.y -= 18;
 
                        drawComponentLabel(curRect, type);                
                    }
                }
                else
                {
                    drawComponentLabel(curRect, type);                    
                }
            }
 
            if (dragAndDrop)
            {
                float curY = currentEvent.mousePosition.y - dragOffset;
                curY = Mathf.Clamp(curY, rect.y, rect.y + rect.height - 16);
                drawComponentLabel(new Rect(rect.x, curY, rect.width, rect.height), (QHierarchyComponentEnum)int.Parse(componentIds[originalDragIndex]), true);
            }
        }
        
        // PRIVATE
        private void drawComponentLabel(Rect rect, QHierarchyComponentEnum type, bool withBackground = false)
        {
            if (withBackground)
            {
                EditorGUI.DrawRect(new Rect(rect.x, rect.y - 2, rect.width, 20), backgroundColor);
            }
            GUI.DrawTexture(new Rect(rect.x, rect.y - 2, 20, 20), dragButton);
            Rect labelRect = new Rect(rect.x + 31, rect.y, rect.width - 20, 16);
            labelRect.y -= (EditorGUIUtility.singleLineHeight - labelRect.height) * 0.5f;
            EditorGUI.LabelField(labelRect, getTextWithSpaces(type.ToString()));
        }
        
        private string getTextWithSpaces(string text)
        {
            StringBuilder newText = new StringBuilder(text.Length * 2);
            newText.Append(text[0]);
            for (int i = 1; i < text.Length; i++)
            {
                if (char.IsUpper(text[i]) && text[i - 1] != ' ')                
                    newText.Append(' ');                
                newText.Append(text[i]);                
            }
            newText.Replace(" Component", "");
            return newText.ToString();
        }
    }
}