Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using System.Collections.Generic;
- public class MapEditor : EditorWindow
- {
- public static MapEditor window;
- private List<MapNode> nodes;
- private GUIStyle nodeStyle;
- private GUIStyle exitStyle;
- private Vector2 drag;
- //private Vector2 offset = new Vector2(0,0);
- public const int grid_width = 200;
- public const int grid_height = 200;
- private const int node_size = 150;
- public static Vector2 offset = new Vector2(0, 0);
- public static Vector2 nodeOffset = new Vector2((grid_width - node_size),(grid_height-node_size)) / 2;
- public static ScenarioDefinition scenario = null;
- [MenuItem("Window/Map Editor")]
- private static void OpenWindow()
- {
- window = GetWindow<MapEditor>();
- window.titleContent = new GUIContent("Node Based Editor");
- }
- private void OnEnable()
- {
- offset = new Vector2(0, 0);
- window = GetWindow<MapEditor>();
- nodeStyle = new GUIStyle();
- nodeStyle.normal.background = EditorGUIUtility.Load("builtin skins/darkskin/images/node1.png") as Texture2D;
- exitStyle = new GUIStyle();
- exitStyle.normal.background = EditorGUIUtility.Load("builtin skins/darkskin/images/node1.png") as Texture2D;
- nodeStyle.border = new RectOffset(12, 12, 12, 12);
- nodes = new List<MapNode>();
- //Generate all nodes based on scenario data
- scenario = AssetDatabase.LoadAssetAtPath("Assets/Data/CrossBreed/Scenario.asset", typeof(ScenarioDefinition)) as ScenarioDefinition;
- Debug.Log("Loaded scenario: " + scenario.state);
- //Start pumping out them nodies
- foreach(uint key in scenario.state.roomIDs.keys)
- {
- int x = scenario.state.roomIDs[key].properties.pos_x;
- int y = scenario.state.roomIDs[key].properties.pos_y;
- nodes.Add(new MapNode(new Vector2(x*grid_width,y*grid_height) + nodeOffset, node_size, node_size, nodeStyle, exitStyle, key));
- Repaint();
- }
- }
- private void OnGUI()
- {
- DrawCheckerBoard();
- DrawNodes();
- ProcessNodeEvents(Event.current);
- ProcessEvents(Event.current);
- if (GUI.changed) Repaint();
- }
- private void DrawCheckerBoard()
- {
- Vector2 boxPos = Vector2.zero;
- Rect box = new Rect(0, 0, grid_width, grid_height);
- Color color = Color.grey;
- int height = (int)window.position.height;
- int width = (int)window.position.width;
- int infLoopCatcher = 0;
- //Start drawin' boxes
- for (int y = 0; y < height;)
- {
- for (int x = 0; x < width;)
- {
- //Debug.Log("okay!");
- box.x = x;
- box.y = y;
- box.height = getBottom(box.y, grid_height) - y;
- box.width = getRight(box.x, grid_width) - x;
- bool draw = true;
- if(getColumn(box.x,grid_width) % 2 == 0)
- {
- draw = !draw;
- }
- if (getRow(box.y, grid_height) % 2 == 0)
- {
- draw = !draw;
- }
- if(draw)
- {
- color.b = .5f + (.03f * getColumn(x, grid_width));
- EditorGUI.DrawRect(box, color);
- }
- x = getRight(box.x, grid_width) + 1;
- infLoopCatcher++;
- if (infLoopCatcher > 500)
- {
- break;
- }
- }
- y = getBottom(box.y, grid_height) + 1;
- infLoopCatcher++;
- if (infLoopCatcher > 500)
- {
- break;
- }
- }
- Repaint();
- }
- public int getColumn(float local_x, int width)
- {
- float world_x = local_x - offset.x;
- if (world_x >= 0)
- return (int)(world_x / width);
- else
- return (int)((world_x + 1) / width) - 1;
- }
- private int getColumnDebug(float local_x, int width, float offset_x)
- {
- float world_x = local_x - offset_x;
- Debug.Log(" world_x = " + local_x + " - " + offset_x + " == " + world_x);
- Debug.Log(" " + (world_x+1) + " / " + width + " = " + (world_x+1 / width));
- if (world_x >= 0)
- return (int)(world_x / width);
- else
- return (int)((world_x + 1) / width) - 1;
- }
- public int getRow(float local_y, int height)
- {
- float world_y = local_y - offset.y;
- if (world_y >= 0)
- return (int)(world_y / height);
- else
- return (int)((world_y + 1) / height) - 1;
- }
- private int getRight(float local_x, int width)
- {
- //Translate this point to worldspace
- int column = getColumn(local_x, width);
- //Right point should be
- return (int)((((column + 1) * width)-1) + offset.x);
- }
- private int getRightDebug(float local_x, int width, float offset_x)
- {
- //Translate this point to worldspace
- Debug.Log("GetRightDebug() " + local_x + " " + offset_x);
- int column = getColumnDebug(local_x, width, offset_x);
- Debug.Log("Column is: " + column);
- Debug.Log("Right is: " + (((column + 1) * width) - 1));
- Debug.Log("Local is: " + (int)((((column + 1) * width) - 1) + offset_x));
- //Right point should be
- return (int)((((column + 1) * width) - 1) + offset_x);
- }
- private int getBottom(float local_y, int height)
- {
- int row = getRow(local_y, height);
- //Right point should be
- return (int)((((row + 1) * height) - 1) + offset.y);
- }
- //Draw all of our nodes
- private void DrawNodes()
- {
- if (nodes != null)
- {
- for (int i = 0; i < nodes.Count; i++)
- {
- nodes[i].Draw();
- }
- }
- }
- //Our main event processing function
- private void ProcessEvents(Event e)
- {
- drag = Vector2.zero;
- switch (e.type)
- {
- case EventType.MouseDown:
- if (e.button == 1)
- {
- Debug.Log("Mouse clicked in mapeditor at position: " + e.mousePosition);
- ProcessContextMenu(e.mousePosition);
- }
- else if (e.button == 0)
- {
- Debug.Log(MapEditor.window.getColumn(e.mousePosition.x, MapEditor.grid_width) + "," + MapEditor.window.getRow(e.mousePosition.y, MapEditor.grid_height));
- }
- break;
- case EventType.MouseDrag:
- if (e.button == 2)
- {
- OnDrag(e.delta);
- }
- break;
- }
- }
- private void OnDrag(Vector2 delta)
- {
- drag = delta;
- offset += delta;
- if (nodes != null)
- {
- for (int i = 0; i < nodes.Count; i++)
- {
- nodes[i].Drag(delta);
- }
- }
- GUI.changed = true;
- DrawCheckerBoard();
- }
- //Opens our context menu at mouse position
- private void ProcessContextMenu(Vector2 mousePosition)
- {
- GenericMenu genericMenu = new GenericMenu();
- genericMenu.AddItem(new GUIContent("Add node"), false, () => OnClickAddNode(mousePosition));
- genericMenu.ShowAsContext();
- }
- //Adding a node at the clicked area
- private void OnClickAddNode(Vector2 mousePosition)
- {
- if (nodes == null)
- {
- nodes = new List<MapNode>();
- }
- //nodes.Add(new MapNode(mousePosition, grid_height-30, grid_width-30, nodeStyle));
- Repaint();
- }
- //Propogates events to each node
- private void ProcessNodeEvents(Event e)
- {
- if (nodes != null)
- {
- for (int i = nodes.Count - 1; i >= 0; i--)
- {
- bool guiChanged = nodes[i].ProcessEvents(e);
- if (guiChanged)
- {
- GUI.changed = true;
- }
- }
- }
- }
- }
- public class MapNode
- {
- public uint key;
- public Rect rect;
- public string title;
- public bool isDragged;
- public GUIStyle style;
- public GUIStyle exitStyle;
- NodeExit north;
- NodeExit south;
- NodeExit east;
- NodeExit west;
- public List<NodeExit> exits;
- public MapNode(Vector2 position, float width, float height, GUIStyle nodeStyle, GUIStyle exitStyle, uint key)
- {
- rect = new Rect(position.x, position.y, width, height);
- style = nodeStyle;
- this.key = key;
- /*
- north = new NodeExit("north", rect, rect.width/2, 0, exitStyle);
- south = new NodeExit("south", rect, rect.width/2, rect.height, exitStyle);
- west = new NodeExit("west", rect, 0, rect.height/2, exitStyle);
- east = new NodeExit("east", rect, rect.width, rect.height/2, exitStyle);
- */
- north = new NodeExit("north", rect, rect.width / 2, 0, exitStyle);
- south = new NodeExit("south", rect, rect.width / 2, rect.height, exitStyle);
- west = new NodeExit("west", rect, 0, rect.height / 2, exitStyle);
- east = new NodeExit("east", rect, rect.width, rect.height / 2, exitStyle);
- }
- public void Drag(Vector2 delta)
- {
- rect.position += delta;
- north.Drag(delta);
- south.Drag(delta);
- east.Drag(delta);
- west.Drag(delta);
- }
- public void Drop(Event e)
- {
- int column = MapEditor.window.getColumn(e.mousePosition.x, MapEditor.grid_width);
- int row = MapEditor.window.getRow(e.mousePosition.y , MapEditor.grid_height);
- Vector2 delta = (new Vector2(column * MapEditor.grid_width, row * MapEditor.grid_height) - rect.position) + MapEditor.nodeOffset + MapEditor.offset;
- Debug.Log("x: " + column + " y: " + row);
- Debug.Log(delta);
- this.Drag(delta);
- }
- public void Draw()
- {
- //string label = "";
- GUI.Box(rect, title, style);
- GUILayout.BeginArea(rect);
- if(MapEditor.scenario != null)
- {
- GUILayout.Label(MapEditor.scenario.state.roomIDs[key].name);
- }
- else
- {
- GUILayout.Label("Room");
- }
- GUILayout.EndArea();
- north.Draw();
- south.Draw();
- east.Draw();
- west.Draw();
- }
- public bool ProcessEvents(Event e)
- {
- switch (e.type)
- {
- case EventType.MouseDown:
- if (e.button == 0)
- {
- if (rect.Contains(e.mousePosition))
- {
- isDragged = true;
- GUI.changed = true;
- }
- else
- {
- GUI.changed = true;
- }
- }
- break;
- case EventType.MouseUp:
- if(isDragged)
- {
- Drop(e);
- }
- isDragged = false;
- break;
- case EventType.MouseDrag:
- if (e.button == 0 && isDragged)
- {
- Drag(e.delta);
- e.Use();
- return true;
- }
- break;
- }
- return false;
- }
- }
- public class NodeExit
- {
- public Rect rect;
- string name;
- GUIStyle style;
- public void Drag(Vector2 delta)
- {
- rect.position += delta;
- }
- public void Draw()
- {
- GUI.Box(rect, name, style);
- }
- public NodeExit(string name, Rect parent, float x_offset, float y_offset, GUIStyle exitStyle)
- {
- style = exitStyle;
- this.name = name;
- rect = new Rect((parent.x + x_offset) - 50/2, (parent.y + y_offset) - 50/2, 50, 50);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement