private static void OpenWindow() { AtomToolWindow window = GetWindow <AtomToolWindow>(); window.titleContent = new GUIContent("Atom Placement Tool"); window.maxSize = new Vector2(500, 150); window.minSize = new Vector2(300, 150); window.Show(); }
public void OnSceneGUI(SceneView sceneView) { Event e = Event.current; Vector2 mousePos = AtomToolWindow.EditorToWorldPoint(e.mousePosition); if (toBePlaced == null) { if (e.type == EventType.MouseDown && e.button == 0) { string prefabName = (type == IonType.Negative) ? "Ion.prefab" : "PositiveIon.prefab"; toBePlaced = Instantiate(AssetDatabase.LoadAssetAtPath <Ion>("Assets/Prefabs/" + prefabName)).GetComponent <Ion>(); toBePlaced.Init(behaviour, radius * baseRadiusUnit); //Debug.Log("PREVIEW"); } else if (e.type == EventType.MouseDrag && e.button == 1) { foreach (Transform t in ionMap) { if (((Vector2)t.position - mousePos).sqrMagnitude < 1) { DestroyImmediate(t.gameObject); } } } //if toBePlaced != null } else { //Debug.Log("TO BE PLACED"); if (e.type == EventType.MouseDown && e.button == 1) { DestroyImmediate(toBePlaced.gameObject); toBePlaced = null; e.Use(); //Debug.Log("CANCEL"); return; } Atom anchor = null; Vector2 placePoint = mousePos; // <-- goal is to obtain this switch (behaviour) { case IonBehaviour.Orbit: if (atomMap.transform.childCount > 0) { Atom closest = null; int closestLevel = 0; float closestDist = float.MaxValue; foreach (Transform t in atomMap) { Atom a = t.GetComponent <Atom>(); for (int l = 0; l < a.radii.Length; l++) { float dist = Mathf.Abs(((Vector2)a.transform.position - mousePos).sqrMagnitude - a.radii[l] * a.radii[l]); if (dist < closestDist) { closest = a; closestLevel = l; closestDist = dist; } } } if (closestDist < anchorSnapThresh) { anchor = closest; Vector2 snapDir = AtomToolWindow.GetSnapDir(anchor, mousePos, subdiv, subdivOffset * Mathf.Deg2Rad); placePoint = (Vector2)anchor.transform.position + snapDir * (anchor.radii[closestLevel]); toBePlaced.transform.eulerAngles = new Vector3(0, 0, Util.VectorAngle(snapDir) * Mathf.Rad2Deg); toBePlaced.parent = anchor; toBePlaced.initAngle = Util.VectorAngle(snapDir); toBePlaced.orbitLevel = closestLevel; toBePlaced.orbitFreq = orbitFreq; if (resetTrigger != null) { toBePlaced.resetTrigger = resetTrigger; } } } break; default: break; } toBePlaced.transform.position = (Vector3)placePoint; if (e.type == EventType.MouseDown && e.button == 0) { toBePlaced.transform.SetParent(ionMap); toBePlaced.initPos = toBePlaced.transform.position; toBePlaced = null; //Debug.Log("PLACED"); } } if ((e.type == EventType.MouseDown || e.type == EventType.MouseUp) && e.button != 2) { e.Use(); } }