private static void DrawAtlasesList(OCBlockSet blockSet) { OCAtlas[] list = blockSet.Atlases; GUILayout.BeginVertical(GUI.skin.box, GUILayout.ExpandWidth(true)); { selectedAtlas = BlockEditorUtils.DrawList(selectedAtlas, list); EditorGUILayout.Separator(); GUILayout.BeginHorizontal(); if (GUILayout.Button("Add")) { ArrayUtility.Add <OCAtlas>(ref list, new OCAtlas()); selectedAtlas = list.Length - 1; GUI.changed = true; } if (GUILayout.Button("Remove") && selectedAtlas != -1) { Undo.RecordObject(blockSet, "Remove atlas"); ArrayUtility.RemoveAt <OCAtlas>(ref list, selectedAtlas); selectedAtlas = Mathf.Clamp(selectedAtlas, 0, list.Length - 1); GUI.changed = true; } GUILayout.EndHorizontal(); } GUILayout.EndVertical(); blockSet.Atlases = list; }
public static void DrawBlockEditor(OCBlock block, OCBlockSet blockSet) { UnityEngine.GUILayout.BeginVertical(UnityEngine.GUI.skin.box); { string name = EditorGUILayout.TextField("Name", block.GetName()); block.SetName(FixNameString(name)); if (block is OCGlassBlock) { OCGlassBlock glass = (OCGlassBlock)block; UnityEngine.GameObject interior = (UnityEngine.GameObject)EditorGUILayout.ObjectField("Interior", glass.GetInterior(), typeof(UnityEngine.GameObject), true, null); glass.SetInterior(interior); } int atlas = BlockEditorUtils.Popup("Atlas", block.AtlasID, blockSet.Atlases); block.AtlasID = atlas; int light = EditorGUILayout.IntField("Light", block.GetLight()); block.SetLight(light); } UnityEngine.GUILayout.EndVertical(); UnityEngine.Texture texture = block.GetTexture(); if (texture != null) { FieldInfo field = DrawFacesList(block, texture); int face = (int)field.GetValue(block); DrawFaceEditor(ref face, block.Atlas, ref atlasMatrix); field.SetValue(block, face); } }
private static bool DrawItem(Rect position, OCBlock block, bool selected, int index) { Rect texturePosition = position; texturePosition.height = texturePosition.width; Rect labelPosition = position; labelPosition.yMin += texturePosition.height; if (selected) { BlockEditorUtils.FillRect(labelPosition, new Color(61 / 255f, 128 / 255f, 223 / 255f)); } if (block != null) { block.DrawPreview(texturePosition); GUI.Label(labelPosition, block.GetName()); } else { BlockEditorUtils.FillRect(texturePosition, Color.grey); GUI.Label(labelPosition, "Null"); } if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && position.Contains(Event.current.mousePosition)) { Event.current.Use(); return(true); } return(false); }
public override void OnInspectorGUI() { EditorGUIUtility.LookLikeControls(); Mode oldMode = mode; mode = (Mode)BlockEditorUtils.Toolbar(mode); EditorGUILayout.Separator(); if (mode != oldMode) { EditorGUIUtility.keyboardControl = 0; } if (mode == Mode.AtlasSet) { DrawAtlasesList(blockSet); if (blockSet.GetAtlas(selectedAtlas) != null) { DrawAtlasEditor(blockSet.GetAtlas(selectedAtlas)); } } if (mode == Mode.BlockSet) { DrawBlockSet(blockSet); EditorGUILayout.Separator(); if (blockSet.GetBlock(selectedBlock) != null) { BlockEditor.DrawBlockEditor(blockSet.GetBlock(selectedBlock), blockSet); } } if (mode == Mode.XML) { if (oldMode != mode) { xml = blockSet.Data; } xmlScrollPosition = GUILayout.BeginScrollView(xmlScrollPosition); GUIStyle style = new GUIStyle(GUI.skin.box); style.alignment = TextAnchor.UpperLeft; xml = EditorGUILayout.TextArea(xml, GUILayout.ExpandWidth(true)); blockSet.Data = xml; GUILayout.EndScrollView(); if (GUILayout.Button("Import")) { OCBlockSetImport.Import(blockSet, blockSet.Data); GUI.changed = true; } } if (GUI.changed) { string data = BlockSetExport.Export(blockSet); blockSet.Data = data; EditorUtility.SetDirty(blockSet); } }
public static int SelectionGrid(OCBlockSet blockSet, int index, params GUILayoutOption[] options) { Container <Vector2> scroll = BlockEditorUtils.GetStateObject <Container <Vector2> >(blockSet.GetHashCode()); scroll.value = GUILayout.BeginScrollView(scroll, options); index = SelectionGrid(blockSet.Blocks, index); GUILayout.EndScrollView(); return(index); }
private static void DrawFaceEditor(ref int face, OCAtlas atlas, ref UnityEngine.Matrix4x4 matrix) { UnityEngine.GUILayout.BeginVertical(UnityEngine.GUI.skin.box); UnityEngine.Texture texture = atlas.Texture; UnityEngine.Rect rect = UnityEngine.GUILayoutUtility.GetAspectRect((float)texture.width / texture.height); UnityEngine.GUILayout.EndVertical(); UnityEngine.Matrix4x4 rectMatrix = UnityEngine.Matrix4x4.Scale(new UnityEngine.Vector3(rect.width, rect.height, 0)) * matrix; UnityEngine.Matrix4x4 invRectMatrix = matrix.inverse * UnityEngine.Matrix4x4.Scale(new UnityEngine.Vector3(1 / rect.width, 1 / rect.height, 0)); UnityEngine.Matrix4x4 invertY = UnityEngine.Matrix4x4.TRS(new UnityEngine.Vector2(0, 1), UnityEngine.Quaternion.identity, new UnityEngine.Vector2(1, -1)); bool mouseInRect = rect.Contains(UnityEngine.Event.current.mousePosition); UnityEngine.GUI.BeginGroup(rect); { UnityEngine.Vector2 mouse = invRectMatrix.MultiplyPoint(UnityEngine.Event.current.mousePosition); // local mouse [0..1] if (UnityEngine.Event.current.type == UnityEngine.EventType.Repaint) { UnityEngine.Rect texturePosition = Mul(new UnityEngine.Rect(0, 0, 1, 1), rectMatrix); UnityEngine.Rect faceRet = atlas.ToRect(face); faceRet = Mul(faceRet, rectMatrix * invertY); UnityEngine.GUI.DrawTexture(texturePosition, texture); BlockEditorUtils.DrawRect(faceRet, UnityEngine.Color.green); } if (UnityEngine.Event.current.type == UnityEngine.EventType.MouseDown && UnityEngine.Event.current.button == 0 && mouseInRect) { UnityEngine.Vector2 invMouse = invertY.MultiplyPoint(mouse); if (invMouse.x >= 0 && invMouse.x <= 1 && invMouse.y >= 0 && invMouse.y <= 1) { int posX = UnityEngine.Mathf.FloorToInt(invMouse.x * atlas.Width); int posY = UnityEngine.Mathf.FloorToInt(invMouse.y * atlas.Height); face = posY * atlas.Width + posX; UnityEngine.GUI.changed = true; UnityEngine.Event.current.Use(); } } if (UnityEngine.Event.current.type == UnityEngine.EventType.MouseDrag && UnityEngine.Event.current.button == 1 && mouseInRect) { UnityEngine.Vector3 delta = UnityEngine.Event.current.delta; delta.x /= rect.width; delta.y /= rect.height; UnityEngine.Matrix4x4 offsetMatrix = UnityEngine.Matrix4x4.TRS(delta, UnityEngine.Quaternion.identity, UnityEngine.Vector3.one); matrix = offsetMatrix * matrix; UnityEngine.GUI.changed = true; UnityEngine.Event.current.Use(); } if (UnityEngine.Event.current.type == UnityEngine.EventType.ScrollWheel && mouseInRect) { float s = 0.95f; if (UnityEngine.Event.current.delta.y < 0) { s = 1.0f / s; } UnityEngine.Matrix4x4 offsetMatrix = UnityEngine.Matrix4x4.TRS(mouse, UnityEngine.Quaternion.identity, UnityEngine.Vector3.one); matrix *= offsetMatrix; UnityEngine.Matrix4x4 scaleMatrix = UnityEngine.Matrix4x4.Scale(UnityEngine.Vector3.one * s); matrix *= scaleMatrix; offsetMatrix = UnityEngine.Matrix4x4.TRS(-mouse, UnityEngine.Quaternion.identity, UnityEngine.Vector3.one); matrix *= offsetMatrix; UnityEngine.GUI.changed = true; UnityEngine.Event.current.Use(); } } UnityEngine.GUI.EndGroup(); }
private static int SelectionGrid(IList <OCBlock> items, int index) { Rect rect; int xCount, yCount; index = SelectionGrid(items, index, out rect, out xCount, out yCount); float itemWidth = rect.width / xCount; float itemHeight = rect.height / yCount; GUI.BeginGroup(rect); Vector2 mouse = Event.current.mousePosition; int posX = Mathf.FloorToInt(mouse.x / itemWidth); int posY = Mathf.FloorToInt(mouse.y / itemHeight); int realIndex = -1; // номер элемента под курсором if (posX >= 0 && posX < xCount && posY >= 0 && posY < yCount) { realIndex = posY * xCount + posX; } int dropX = Mathf.Clamp(posX, 0, xCount - 1); int dropY = Mathf.Clamp(posY, 0, yCount - 1); if (dropY == yCount - 1 && items.Count % xCount != 0) { dropX = Mathf.Clamp(dropX, 0, items.Count % xCount); } int dropIndex = dropY * xCount + dropX; // ближайший элемент к курсору if (Event.current.type == EventType.MouseDrag && Event.current.button == 0 && realIndex == index) { #if UNITY_EDITOR DragAndDrop.PrepareStartDrag(); DragAndDrop.objectReferences = new Object[0]; DragAndDrop.paths = new string[0]; DragAndDrop.SetGenericData(DRAG_AND_DROP, new Container <int>(index)); DragAndDrop.StartDrag("DragAndDrop"); #endif Event.current.Use(); } if (Event.current.type == EventType.DragUpdated) { #if UNITY_EDITOR Container <int> data = (Container <int>)DragAndDrop.GetGenericData(DRAG_AND_DROP); if (data != null) { DragAndDrop.visualMode = DragAndDropVisualMode.Link; Event.current.Use(); } #endif } if (Event.current.type == EventType.DragPerform) { #if UNITY_EDITOR Container <int> oldIndex = (Container <int>)DragAndDrop.GetGenericData(DRAG_AND_DROP); if (dropIndex > oldIndex.value) { dropIndex--; } dropIndex = Mathf.Clamp(dropIndex, 0, items.Count - 1); Insert(items, dropIndex, oldIndex); index = dropIndex; DragAndDrop.AcceptDrag(); DragAndDrop.PrepareStartDrag(); #endif Event.current.Use(); } #if UNITY_EDITOR if (Event.current.type == EventType.Repaint && DragAndDrop.visualMode == DragAndDropVisualMode.Link) { Vector2 pos = new Vector2(2 + dropX * itemWidth, 2 + dropY * itemHeight); Rect lineRect = new Rect(pos.x - 2, pos.y, 2, itemWidth - 2); BlockEditorUtils.FillRect(lineRect, Color.red); } #endif GUI.EndGroup(); return(index); }