Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.24 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4.  
  5. public class MapEditor : EditorWindow
  6. {
  7.     public static MapEditor window;
  8.     private List<MapNode> nodes;
  9.     private GUIStyle nodeStyle;
  10.     private GUIStyle exitStyle;
  11.  
  12.     private Vector2 drag;
  13.     //private Vector2 offset = new Vector2(0,0);
  14.     public const int grid_width = 200;
  15.     public const int grid_height = 200;
  16.  
  17.     private const int node_size = 150;
  18.     public static Vector2 offset = new Vector2(0, 0);
  19.     public static Vector2 nodeOffset = new Vector2((grid_width - node_size),(grid_height-node_size)) / 2;
  20.     public static ScenarioDefinition scenario = null;
  21.  
  22.  
  23.     [MenuItem("Window/Map Editor")]
  24.     private static void OpenWindow()
  25.     {
  26.         window = GetWindow<MapEditor>();
  27.         window.titleContent = new GUIContent("Node Based Editor");
  28.     }
  29.  
  30.     private void OnEnable()
  31.     {
  32.         offset = new Vector2(0, 0);
  33.         window = GetWindow<MapEditor>();
  34.         nodeStyle = new GUIStyle();
  35.         nodeStyle.normal.background = EditorGUIUtility.Load("builtin skins/darkskin/images/node1.png") as Texture2D;
  36.         exitStyle = new GUIStyle();
  37.         exitStyle.normal.background = EditorGUIUtility.Load("builtin skins/darkskin/images/node1.png") as Texture2D;
  38.         nodeStyle.border = new RectOffset(12, 12, 12, 12);
  39.  
  40.         nodes = new List<MapNode>();
  41.  
  42.         //Generate all nodes based on scenario data
  43.         scenario = AssetDatabase.LoadAssetAtPath("Assets/Data/CrossBreed/Scenario.asset", typeof(ScenarioDefinition)) as ScenarioDefinition;
  44.         Debug.Log("Loaded scenario: " + scenario.state);
  45.  
  46.         //Start pumping out them nodies
  47.  
  48.         foreach(uint key in scenario.state.roomIDs.keys)
  49.         {
  50.             int x = scenario.state.roomIDs[key].properties.pos_x;
  51.             int y = scenario.state.roomIDs[key].properties.pos_y;
  52.  
  53.             nodes.Add(new MapNode(new Vector2(x*grid_width,y*grid_height) + nodeOffset, node_size, node_size, nodeStyle, exitStyle, key));
  54.             Repaint();
  55.         }
  56.     }
  57.  
  58.  
  59.     private void OnGUI()
  60.     {
  61.  
  62.         DrawCheckerBoard();
  63.         DrawNodes();
  64.  
  65.         ProcessNodeEvents(Event.current);
  66.         ProcessEvents(Event.current);
  67.        
  68.         if (GUI.changed) Repaint();
  69.     }
  70.  
  71.     private void DrawCheckerBoard()
  72.     {
  73.         Vector2 boxPos = Vector2.zero;
  74.         Rect box = new Rect(0, 0, grid_width, grid_height);
  75.         Color color = Color.grey;
  76.        
  77.         int height = (int)window.position.height;
  78.         int width = (int)window.position.width;
  79.  
  80.         int infLoopCatcher = 0;
  81.  
  82.  
  83.         //Start drawin' boxes
  84.  
  85.  
  86.         for (int y = 0; y < height;)
  87.         {
  88.             for (int x = 0; x < width;)
  89.             {
  90.                 //Debug.Log("okay!");
  91.                 box.x = x;
  92.                 box.y = y;
  93.  
  94.                 box.height = getBottom(box.y, grid_height) - y;
  95.                 box.width = getRight(box.x, grid_width) - x;
  96.  
  97.                
  98.                 bool draw = true;
  99.  
  100.                 if(getColumn(box.x,grid_width) % 2 == 0)
  101.                 {
  102.                     draw = !draw;
  103.                 }
  104.                
  105.                 if (getRow(box.y, grid_height) % 2 == 0)
  106.                 {
  107.                     draw = !draw;
  108.                 }
  109.                
  110.  
  111.                 if(draw)
  112.                 {
  113.                     color.b = .5f + (.03f * getColumn(x, grid_width));
  114.                     EditorGUI.DrawRect(box, color);
  115.                    
  116.                 }
  117.                
  118.  
  119.                 x = getRight(box.x, grid_width) + 1;
  120.  
  121.  
  122.                 infLoopCatcher++;
  123.                 if (infLoopCatcher > 500)
  124.                 {
  125.                     break;
  126.                 }
  127.             }
  128.             y = getBottom(box.y, grid_height) + 1;
  129.             infLoopCatcher++;
  130.             if (infLoopCatcher > 500)
  131.             {
  132.                 break;
  133.             }
  134.         }
  135.  
  136.         Repaint();
  137.        
  138.     }
  139.  
  140.     public int getColumn(float local_x, int width)
  141.     {
  142.         float world_x = local_x - offset.x;
  143.         if (world_x >= 0)
  144.             return (int)(world_x / width);
  145.         else
  146.             return (int)((world_x + 1) / width) - 1;
  147.     }
  148.  
  149.  
  150.     private int getColumnDebug(float local_x, int width, float offset_x)
  151.     {
  152.         float world_x = local_x - offset_x;
  153.         Debug.Log("    world_x = " + local_x + " - " + offset_x + " == " +  world_x);
  154.         Debug.Log("    " + (world_x+1) + " / " + width + " = " + (world_x+1 / width));
  155.  
  156.         if (world_x >= 0)
  157.             return (int)(world_x / width);
  158.         else
  159.             return (int)((world_x + 1) / width) - 1;
  160.     }
  161.  
  162.  
  163.     public int getRow(float local_y, int height)
  164.     {
  165.         float world_y = local_y - offset.y;
  166.         if (world_y >= 0)
  167.             return (int)(world_y / height);
  168.         else
  169.             return (int)((world_y + 1) / height) - 1;
  170.     }
  171.  
  172.  
  173.     private int getRight(float local_x, int width)
  174.     {
  175.         //Translate this point to worldspace
  176.  
  177.         int column = getColumn(local_x, width);
  178.  
  179.         //Right point should be
  180.  
  181.         return (int)((((column + 1) * width)-1) + offset.x);
  182.     }
  183.  
  184.  
  185.     private int getRightDebug(float local_x, int width, float offset_x)
  186.     {
  187.         //Translate this point to worldspace
  188.         Debug.Log("GetRightDebug() " + local_x + " " + offset_x);
  189.  
  190.         int column = getColumnDebug(local_x, width, offset_x);
  191.  
  192.         Debug.Log("Column is: " + column);
  193.         Debug.Log("Right is: " + (((column + 1) * width) - 1));
  194.         Debug.Log("Local is: " + (int)((((column + 1) * width) - 1) + offset_x));
  195.         //Right point should be
  196.  
  197.         return (int)((((column + 1) * width) - 1) + offset_x);
  198.     }
  199.  
  200.     private int getBottom(float local_y, int height)
  201.     {
  202.  
  203.         int row = getRow(local_y, height);
  204.  
  205.         //Right point should be
  206.         return (int)((((row + 1) * height) - 1) + offset.y);
  207.     }
  208.  
  209.     //Draw all of our nodes
  210.     private void DrawNodes()
  211.     {
  212.         if (nodes != null)
  213.         {
  214.             for (int i = 0; i < nodes.Count; i++)
  215.             {
  216.                 nodes[i].Draw();
  217.             }
  218.         }
  219.     }
  220.  
  221.     //Our main event processing function
  222.     private void ProcessEvents(Event e)
  223.     {
  224.         drag = Vector2.zero;
  225.  
  226.         switch (e.type)
  227.         {
  228.             case EventType.MouseDown:
  229.                 if (e.button == 1)
  230.                 {
  231.                     Debug.Log("Mouse clicked in mapeditor at position: " + e.mousePosition);
  232.                     ProcessContextMenu(e.mousePosition);
  233.                 }
  234.                 else if (e.button == 0)
  235.                 {
  236.                     Debug.Log(MapEditor.window.getColumn(e.mousePosition.x, MapEditor.grid_width) + "," + MapEditor.window.getRow(e.mousePosition.y, MapEditor.grid_height));
  237.                 }
  238.                 break;
  239.             case EventType.MouseDrag:
  240.                 if (e.button == 2)
  241.                 {
  242.                     OnDrag(e.delta);
  243.                 }
  244.                 break;
  245.         }
  246.     }
  247.  
  248.     private void OnDrag(Vector2 delta)
  249.     {
  250.         drag = delta;
  251.  
  252.         offset += delta;
  253.  
  254.         if (nodes != null)
  255.         {
  256.             for (int i = 0; i < nodes.Count; i++)
  257.             {
  258.                 nodes[i].Drag(delta);
  259.             }
  260.         }
  261.  
  262.         GUI.changed = true;
  263.         DrawCheckerBoard();
  264.     }
  265.  
  266.     //Opens our context menu at mouse position
  267.     private void ProcessContextMenu(Vector2 mousePosition)
  268.     {
  269.         GenericMenu genericMenu = new GenericMenu();
  270.         genericMenu.AddItem(new GUIContent("Add node"), false, () => OnClickAddNode(mousePosition));
  271.         genericMenu.ShowAsContext();
  272.     }
  273.  
  274.     //Adding a node at the clicked area
  275.     private void OnClickAddNode(Vector2 mousePosition)
  276.     {
  277.         if (nodes == null)
  278.         {
  279.             nodes = new List<MapNode>();
  280.         }
  281.  
  282.         //nodes.Add(new MapNode(mousePosition, grid_height-30, grid_width-30, nodeStyle));
  283.         Repaint();
  284.     }
  285.  
  286.     //Propogates events to each node
  287.     private void ProcessNodeEvents(Event e)
  288.     {
  289.         if (nodes != null)
  290.         {
  291.             for (int i = nodes.Count - 1; i >= 0; i--)
  292.             {
  293.                 bool guiChanged = nodes[i].ProcessEvents(e);
  294.  
  295.                 if (guiChanged)
  296.                 {
  297.                     GUI.changed = true;
  298.                 }
  299.             }
  300.         }
  301.     }
  302. }
  303.  
  304.  
  305. public class MapNode
  306. {
  307.     public uint key;
  308.     public Rect rect;
  309.     public string title;
  310.     public bool isDragged;
  311.     public GUIStyle style;
  312.     public GUIStyle exitStyle;
  313.  
  314.     NodeExit north;
  315.     NodeExit south;
  316.     NodeExit east;
  317.     NodeExit west;
  318.  
  319.     public List<NodeExit> exits;
  320.  
  321.     public MapNode(Vector2 position, float width, float height, GUIStyle nodeStyle, GUIStyle exitStyle, uint key)
  322.     {
  323.         rect = new Rect(position.x, position.y, width, height);
  324.         style = nodeStyle;
  325.         this.key = key;
  326.  
  327.         /*
  328.         north = new NodeExit("north", rect, rect.width/2,  0, exitStyle);
  329.         south = new NodeExit("south", rect, rect.width/2,  rect.height, exitStyle);
  330.         west = new NodeExit("west", rect,   0,             rect.height/2, exitStyle);
  331.         east = new NodeExit("east", rect,   rect.width,    rect.height/2, exitStyle);
  332.         */
  333.         north = new NodeExit("north", rect, rect.width / 2, 0, exitStyle);
  334.         south = new NodeExit("south", rect, rect.width / 2, rect.height, exitStyle);
  335.         west = new NodeExit("west", rect, 0, rect.height / 2, exitStyle);
  336.         east = new NodeExit("east", rect, rect.width, rect.height / 2, exitStyle);
  337.  
  338.     }
  339.  
  340.     public void Drag(Vector2 delta)
  341.     {
  342.         rect.position += delta;
  343.  
  344.         north.Drag(delta);
  345.         south.Drag(delta);
  346.         east.Drag(delta);
  347.         west.Drag(delta);
  348.     }
  349.  
  350.     public void Drop(Event e)
  351.     {
  352.         int column = MapEditor.window.getColumn(e.mousePosition.x, MapEditor.grid_width);
  353.         int row = MapEditor.window.getRow(e.mousePosition.y , MapEditor.grid_height);
  354.  
  355.         Vector2 delta = (new Vector2(column * MapEditor.grid_width, row * MapEditor.grid_height) - rect.position) + MapEditor.nodeOffset + MapEditor.offset;
  356.  
  357.         Debug.Log("x: " + column + " y: " + row);
  358.         Debug.Log(delta);
  359.  
  360.         this.Drag(delta);
  361.     }
  362.  
  363.     public void Draw()
  364.     {
  365.         //string label = "";
  366.         GUI.Box(rect, title, style);
  367.         GUILayout.BeginArea(rect);
  368.         if(MapEditor.scenario != null)
  369.         {
  370.             GUILayout.Label(MapEditor.scenario.state.roomIDs[key].name);
  371.         }
  372.         else
  373.         {
  374.             GUILayout.Label("Room");
  375.         }      
  376.         GUILayout.EndArea();
  377.         north.Draw();
  378.         south.Draw();
  379.         east.Draw();
  380.         west.Draw();
  381.     }
  382.  
  383.     public bool ProcessEvents(Event e)
  384.     {
  385.         switch (e.type)
  386.         {
  387.             case EventType.MouseDown:
  388.                 if (e.button == 0)
  389.                 {
  390.                     if (rect.Contains(e.mousePosition))
  391.                     {
  392.                         isDragged = true;
  393.                         GUI.changed = true;
  394.                     }
  395.                     else
  396.                     {
  397.                         GUI.changed = true;
  398.                     }
  399.                 }
  400.                 break;
  401.  
  402.             case EventType.MouseUp:
  403.                 if(isDragged)
  404.                 {
  405.                     Drop(e);
  406.                 }
  407.                 isDragged = false;
  408.                 break;
  409.  
  410.             case EventType.MouseDrag:
  411.                 if (e.button == 0 && isDragged)
  412.                 {
  413.                     Drag(e.delta);
  414.                     e.Use();
  415.                     return true;
  416.                 }
  417.                 break;
  418.         }
  419.  
  420.         return false;
  421.     }
  422. }
  423.  
  424. public class NodeExit
  425. {
  426.     public Rect rect;
  427.     string name;
  428.     GUIStyle style;
  429.  
  430.     public void Drag(Vector2 delta)
  431.     {
  432.         rect.position += delta;
  433.     }
  434.  
  435.     public void Draw()
  436.     {
  437.         GUI.Box(rect, name, style);
  438.     }
  439.  
  440.     public NodeExit(string name, Rect parent, float x_offset, float y_offset, GUIStyle exitStyle)
  441.     {
  442.         style = exitStyle;
  443.         this.name = name;
  444.         rect = new Rect((parent.x + x_offset) - 50/2, (parent.y + y_offset) - 50/2, 50, 50);
  445.     }
  446. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement