-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathTilePainter.cs
372 lines (322 loc) · 9.94 KB
/
TilePainter.cs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using System;
using System.IO;
using System.Collections;
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
[RequireComponent(typeof(BoxCollider))]
public class TilePainter : MonoBehaviour{
public int gridsize = 1;
public int width = 20;
public int height = 20;
public GameObject tiles;
private bool _changed = true;
public Vector3 cursor;
public bool focused = false;
public GameObject[,] tileobs;
int colidx = 0;
public List<UnityEngine.Object> palette = new List<UnityEngine.Object>();
public UnityEngine.Object color = null;
Quaternion color_rotation;
#if UNITY_EDITOR
private static bool IsAssetAFolder(UnityEngine.Object obj){
string path = "";
if (obj == null){return false;}
path = AssetDatabase.GetAssetPath(obj.GetInstanceID());
if (path.Length > 0){
if (Directory.Exists(path)){
return true;
}else{
return false;}
}
return false;
}
public void Encode(){
}
static GameObject CreatePrefab(UnityEngine.Object fab, Vector3 pos, Quaternion rot) {
GameObject o = PrefabUtility.InstantiatePrefab(fab as GameObject) as GameObject;
if (o == null){
Debug.Log(IsAssetAFolder(fab));
return o;}
o.transform.position = pos;
o.transform.rotation = rot;
return o;
}
public void Restore(){
Transform palt = transform.Find("palette");
if (palt != null){GameObject.DestroyImmediate(palt.gameObject);}
GameObject pal = new GameObject("palette");
pal.hideFlags = HideFlags.HideInHierarchy;
BoxCollider bc = pal.AddComponent<BoxCollider>();
bc.size = new Vector3(palette.Count*gridsize, gridsize, 0f);
bc.center = new Vector3((palette.Count-1f)*gridsize*0.5f, 0f, 0f);
pal.transform.parent = this.gameObject.transform;
pal.transform.localPosition = new Vector3(0f, -gridsize*2, 0f);
pal.transform.rotation = transform.rotation;
int palette_folder = -1;
for (int i = 0; i < palette.Count; i++){
UnityEngine.Object o = palette[i];
if (IsAssetAFolder(o)){
palette_folder = i;
}
else {
if (o != null){
GameObject g = CreatePrefab(o, new Vector3() , transform.rotation);
g.transform.parent = pal.transform;
g.transform.localPosition = new Vector3(i*gridsize, 0f, 0f);
}
}
}
if (palette_folder != -1){
string path = AssetDatabase.GetAssetPath(palette[palette_folder].GetInstanceID());
path = path.Trim().Replace("Assets/Resources/", "");
palette.RemoveAt(palette_folder);
UnityEngine.Object[] contents = (UnityEngine.Object[]) Resources.LoadAll(path);
foreach (UnityEngine.Object o in contents){
if (!palette.Contains(o)){palette.Add(o);}
}
Restore();
}
tileobs = new GameObject[width, height];
if (tiles == null){
tiles = new GameObject("tiles");
tiles.transform.parent = this.gameObject.transform;
tiles.transform.localPosition = new Vector3();
}
int cnt = tiles.transform.childCount;
List<GameObject> trash = new List<GameObject>();
for (int i = 0; i < cnt; i++){
GameObject tile = tiles.transform.GetChild(i).gameObject;
Vector3 tilepos = tile.transform.localPosition;
int X = (int)(tilepos.x / gridsize);
int Y = (int)(tilepos.y / gridsize);
if (ValidCoords(X, Y)){
tileobs[X, Y] = tile;
} else {
trash.Add(tile);
}
}
for (int i = 0; i < trash.Count; i++){
if (Application.isPlaying){Destroy(trash[i]);} else {DestroyImmediate(trash[i]);}}
if (color == null){
if (palette.Count > 0){
color = palette[0];
}
}
}
public void Resize(){
transform.localScale = new Vector3(1,1,1);
if (_changed){
_changed = false;
Restore();
}
}
public void Awake(){
Restore();
}
public void OnEnable(){
Restore();
}
void OnValidate(){
_changed = true;
BoxCollider bounds = this.GetComponent<BoxCollider>();
bounds.center = new Vector3((width*gridsize)*0.5f-gridsize*0.5f, (height*gridsize)*0.5f-gridsize*0.5f, 0f);
bounds.size = new Vector3(width*gridsize, (height*gridsize), 0f);
}
public Vector3 GridV3(Vector3 pos){
Vector3 p = transform.InverseTransformPoint(pos) + new Vector3(gridsize*0.5f,gridsize*0.5f, 0f);
return new Vector3((int)(p.x/gridsize), (int)(p.y/gridsize), 0);
}
public bool ValidCoords(int x, int y){
if (tileobs == null) {return false;}
return (x >= 0 && y >= 0 && x < tileobs.GetLength(0) && y < tileobs.GetLength(1));
}
public void CycleColor(){
colidx += 1;
if (colidx >= palette.Count){
colidx = 0;
}
color = (UnityEngine.Object)palette[colidx];
}
public void Turn(){
if (this.ValidCoords((int)cursor.x, (int)cursor.y)){
GameObject o = tileobs[(int)cursor.x, (int)cursor.y];
if (o != null){
o.transform.Rotate(0f, 0f, 90f);
}
}
}
public Vector3 Local(Vector3 p){
return this.transform.TransformPoint(p);
}
public UnityEngine.Object PrefabSource(GameObject o){
if (o == null)
{
return null;
}
UnityEngine.Object fab = PrefabUtility.GetCorrespondingObjectFromSource(o);
if (fab == null)
{
fab = Resources.Load(o.name);
}
if (fab == null)
{
fab = palette[0];
}
return fab;
}
public void Drag(Vector3 mouse, TileLayerEditor.TileOperation op){
Resize();
if (tileobs == null){Restore();}
if (this.ValidCoords((int)cursor.x, (int)cursor.y)){
if (op == TileLayerEditor.TileOperation.Sampling){
UnityEngine.Object s = PrefabSource(tileobs[(int)cursor.x, (int)cursor.y]);
Debug.Log(s);
if (s != null){
color = s;
color_rotation = tileobs[(int)cursor.x, (int)cursor.y].transform.localRotation;
}
} else {
DestroyImmediate(tileobs[(int)cursor.x, (int)cursor.y]);
if (op == TileLayerEditor.TileOperation.Drawing){
if (color == null){return;}
GameObject o = CreatePrefab(color, new Vector3() , color_rotation);
o.transform.parent = tiles.transform;
o.transform.localPosition = (cursor*gridsize);
o.transform.localRotation = color_rotation;
tileobs[(int)cursor.x, (int)cursor.y] = o;
}
}
} else {
if (op == TileLayerEditor.TileOperation.Sampling){
if (cursor.y == -1 && cursor.x >= 0 && cursor.x < palette.Count){
color = palette[(int)cursor.x];
color_rotation = Quaternion.identity;
}
}
}
}
public void Clear(){
tileobs = new GameObject[width, height];
DestroyImmediate(tiles);
tiles = new GameObject("tiles");
tiles.transform.parent = gameObject.transform;
tiles.transform.localPosition = new Vector3();
}
public void OnDrawGizmos(){
Gizmos.color = Color.white;
Gizmos.matrix = transform.localToWorldMatrix;
if (focused){
Gizmos.color = new Color(1f,0f,0f,0.6f);
Gizmos.DrawRay((cursor*gridsize)+Vector3.forward*-49999f, Vector3.forward*99999f);
Gizmos.DrawRay((cursor*gridsize)+Vector3.right*-49999f, Vector3.right*99999f);
Gizmos.DrawRay((cursor*gridsize)+Vector3.up*-49999f, Vector3.up*99999f);
Gizmos.color = Color.yellow;
}
Gizmos.DrawWireCube( new Vector3((width*gridsize)*0.5f-gridsize*0.5f, (height*gridsize)*0.5f-gridsize*0.5f, 0f),
new Vector3(width*gridsize, (height*gridsize), 0f));
}
#endif
}
#if UNITY_EDITOR
[CustomEditor(typeof(TilePainter))]
public class TileLayerEditor : Editor{
public enum TileOperation {None, Drawing, Erasing, Sampling};
private TileOperation operation;
public override void OnInspectorGUI () {
TilePainter me = (TilePainter)target;
GUILayout.Label("Assign a prefab to the color property");
GUILayout.Label("or the pallete array.");
GUILayout.Label("drag : paint tiles");
GUILayout.Label("[s]+click : sample tile color");
GUILayout.Label("[x]+drag : erase tiles");
GUILayout.Label("[space] : rotate tile");
GUILayout.Label("[b] : cycle color");
if(GUILayout.Button("CLEAR")){
me.Clear();}
DrawDefaultInspector();}
private bool AmHovering(Event e){
TilePainter me = (TilePainter)target;
RaycastHit hit;
if (Physics.Raycast(HandleUtility.GUIPointToWorldRay(Event.current.mousePosition), out hit, Mathf.Infinity) &&
hit.collider.GetComponentInParent<TilePainter>() == me)
{
me.cursor = me.GridV3(hit.point);
me.focused = true;
Renderer rend = me.gameObject.GetComponentInChildren<Renderer>( );
if( rend ) EditorUtility.SetSelectedRenderState( rend, EditorSelectedRenderState.Wireframe );
return true;
}
me.focused = false;
return false;
}
public void ProcessEvents(){
TilePainter me = (TilePainter)target;
int controlID = GUIUtility.GetControlID(1778, FocusType.Passive);
EditorWindow currentWindow = EditorWindow.mouseOverWindow;
if(currentWindow && AmHovering(Event.current)){
Event current = Event.current;
bool leftbutton = (current.button == 0);
switch(current.type){
case EventType.KeyDown:
if (current.keyCode == KeyCode.S) operation = TileOperation.Sampling;
if (current.keyCode == KeyCode.X) operation = TileOperation.Erasing;
current.Use();
return;
case EventType.KeyUp:
operation = TileOperation.None;
if (current.keyCode == KeyCode.Space) me.Turn();
if (current.keyCode == KeyCode.B) me.CycleColor();
current.Use();
return;
case EventType.MouseDown:
if (leftbutton)
{
if (operation == TileOperation.None){
operation = TileOperation.Drawing;
}
me.Drag(current.mousePosition, operation);
current.Use();
return;
}
break;
case EventType.MouseDrag:
if (leftbutton)
{
if (operation != TileOperation.None){
me.Drag(current.mousePosition, operation);
current.Use();
}
return;
}
break;
case EventType.MouseUp:
if (leftbutton)
{
operation = TileOperation.None;
current.Use();
return;
}
break;
case EventType.MouseMove:
me.Resize();
current.Use();
break;
case EventType.Repaint:
break;
case EventType.Layout:
HandleUtility.AddDefaultControl(controlID);
break;
}
}
}
void OnSceneGUI (){
ProcessEvents();
}
void DrawEvents(){
Handles.BeginGUI();
Handles.EndGUI();
}}
#endif