public ChapterPreview(Rect rect, GUIContent content, GUIStyle style, params GUILayoutOption[] options) : base(rect, content, style, options) { ProjectConfigData.addConsumer(this); PreviewTitle = "Chapter.Preview".Traslate(); noBackground = Controller.ResourceManager.getImage(SpecialAssetPaths.ASSET_EMPTY_BACKGROUND); sceneColors = new Dictionary <string, Color>(); positions = new Positions(); images = new Dictionary <string, Texture2D>(); sizes = new Dictionary <string, Vector2>(); // SceneList sceneList = new DataControlList() { RequestRepaint = Repaint, footerHeight = 10, elementHeight = 20, Columns = new List <ColumnList.Column>() { new ColumnList.Column() { Text = TC.get("Element.Name1"), SizeOptions = new GUILayoutOption[] { GUILayout.ExpandWidth(true) } }, new ColumnList.Column() { Text = "Open", SizeOptions = new GUILayoutOption[] { GUILayout.Width(250) } } }, drawCell = (cellRect, row, column, isActive, isFocused) => { var scene = ((ScenesListDataControl)sceneList.DataControl).getScenes()[row]; switch (column) { case 0: GUI.Label(cellRect, scene.getId()); break; case 1: if (GUI.Button(cellRect, TC.get("GeneralText.Edit"))) { GameRources.GetInstance().selectedSceneIndex = row; } break; } } }; Layout(); EditorApplication.update += UpdatePositions; }
private Rect GetSceneRect(SceneDataControl scene) { if (!this.positions.ContainsKey(scene.getId())) { string id = GetScenePropertyId(Controller.Instance.SelectedChapterDataControl, scene); string X = ProjectConfigData.getProperty(id + ".X"); string Y = ProjectConfigData.getProperty(id + ".Y"); positions.Add(scene.getId(), new Vector2(ExParsers.ParseDefault(X, 0), ExParsers.ParseDefault(Y, 0))); } var background = scene.getPreviewBackground(); if (!sizes.ContainsKey(background)) { Texture2D scenePreview = null; Vector2 previewSize = new Vector2(800, 600); if (!images.ContainsKey(background)) { scenePreview = Controller.ResourceManager.getImage(background) ?? Controller.ResourceManager.getImage(SpecialAssetPaths.ASSET_EMPTY_IMAGE); images[background] = scenePreview; } else { scenePreview = images[background]; } if (scenePreview) { previewSize = new Vector2(scenePreview.width, scenePreview.height); } sizes.Add(background, previewSize); Color color; try { var pixel = scenePreview.GetPixel(scenePreview.width / 2, scenePreview.height / 2); var colorAsVector = ToHSV(new Color(1f - pixel.r, 1f - pixel.g, 1f - pixel.b)); colorAsVector.y *= 2f; colorAsVector.z *= 1.5f; color = FromHSV(colorAsVector); } catch { // Error getting the pixel color = UnityEngine.Random.ColorHSV(0f, 1f, 0.8f, 1f, 0.5f, 1f, 1f, 1f); } sceneColors[background] = color; } return(new Rect(positions[scene.getId()], sizes[background] * SceneScaling)); }
// AUX FUNCTIONS private void DrawScene(SceneDataControl scene) { var rect = AdaptToViewport(GetSceneRect(scene), space); switch (Event.current.type) { case EventType.Repaint: GUI.DrawTexture(rect, images[scene.getPreviewBackground()] ?? noBackground); if (sceneList.index != -1 && Controller.Instance.SelectedChapterDataControl.getScenesList().getScenes()[sceneList.index] == scene) { HandleUtil.DrawPolyLine(rect.ToPoints().ToArray(), true, Color.red); } break; } EditorGUI.DropShadowLabel(new Rect(rect.position - new Vector2(20, 0), rect.size), scene.getId()); var prevHot = GUIUtility.hotControl; EditorGUI.BeginChangeCheck(); rect = HandleUtil.HandleRectMovement(scene.GetHashCode(), rect); if (EditorGUI.EndChangeCheck()) { rect = RevertFromViewport(rect, space); positions[scene.getId()] = rect.position; // Update in the project data var id = GetScenePropertyId(Controller.Instance.SelectedChapterDataControl, scene); ProjectConfigData.setProperty(id + ".X", ((int)positions[scene.getId()].x).ToString()); ProjectConfigData.setProperty(id + ".Y", ((int)positions[scene.getId()].y).ToString()); } if (GUIUtility.hotControl != prevHot) { sceneList.index = Controller.Instance.SelectedChapterDataControl.getScenesList().getScenes().IndexOf(scene); if (Event.current.clickCount == 2) { OnSelectElement(scene); } } }
public Vector2 this[string id] { get { if (!positions.ContainsKey(id)) { var idX = id + X; var idY = id + Y; if (!ProjectConfigData.existsKey(idX)) { ProjectConfigData.setProperty(idX, "0"); } if (!ProjectConfigData.existsKey(idY)) { ProjectConfigData.setProperty(idY, "0"); } var x = ProjectConfigData.getProperty(idX); var y = ProjectConfigData.getProperty(idY); positions.Add(id, new Vector2(ExParsers.ParseDefault(x, 0), ExParsers.ParseDefault(y, 0))); } return(positions[id]); } set { if (value == this[id]) // this[id] forces positions[id] initialization { return; } positions[id] = value; ProjectConfigData.setProperty(id + X, value.x.ToString(CultureInfo.InvariantCulture)); ProjectConfigData.setProperty(id + Y, value.y.ToString(CultureInfo.InvariantCulture)); } }
private void Layout() { try { var scenes = Controller.Instance.ChapterList.getSelectedChapterDataControl().getScenesList(); // Layout algorithm var settings = new Microsoft.Msagl.Layout.Layered.SugiyamaLayoutSettings(); settings.MaxAspectRatioEccentricity = 1.6; settings.NodeSeparation = 10; settings.PackingMethod = PackingMethod.Compact; // Graph GeometryGraph graph = new GeometryGraph(); graph.BoundingBox = new Microsoft.Msagl.Core.Geometry.Rectangle(0, 0, SpaceWidth, SpaceHeight); graph.UpdateBoundingBox(); Dictionary <SceneDataControl, Node> sceneToNode = new Dictionary <SceneDataControl, Node>(); Dictionary <UniRx.Tuple <Node, Node>, bool> present = new Dictionary <UniRx.Tuple <Node, Node>, bool>(); foreach (var scene in scenes.getScenes()) { sizes.Remove(scene.getPreviewBackground()); var rect = GetSceneRect(scene); var node = new Node(CurveFactory.CreateRectangle(rect.width, rect.height, new Point()), scene.getId()); graph.Nodes.Add(node); sceneToNode.Add(scene, node); } foreach (var scene in scenes.getScenes()) { var node = sceneToNode[scene]; foreach (var exit in scene.getExitsList().getExits()) { var index = scenes.getSceneIndexByID(exit.getNextSceneId()); // If the exit points to a cutscene it normally is out of the array if (index < 0 || index >= scenes.getScenes().Count) { continue; } var nextScene = scenes.getScenes()[index]; var t = new UniRx.Tuple <Node, Node>(node, sceneToNode[nextScene]); if (!present.ContainsKey(t)) { present.Add(t, true); graph.Edges.Add(new Edge(node, sceneToNode[nextScene])); var exitOrigin = GetExitArea(scene, exit).ToRect().center; var originRect = GetSceneRect(scene); var pos = exitOrigin - originRect.position; pos.x = Mathf.Clamp01(pos.x / originRect.width); pos.y = Mathf.Clamp01(pos.y / originRect.height); // Positioning constraints if (pos.x < 0.3) { settings.AddLeftRightConstraint(t.Item2, t.Item1); } if (pos.x > 0.7) { settings.AddLeftRightConstraint(t.Item1, t.Item2); } } } } // Do the layouting LayoutHelpers.CalculateLayout(graph, settings, null); // Extract the results var graphRect = new Rect((float)graph.Left, (float)graph.Bottom, (float)graph.Width, (float)graph.Height); var canvasRect = new Rect(0, 0, SpaceWidth, SpaceHeight); foreach (var scene in scenes.getScenes()) { var n = sceneToNode[scene]; positions[scene.getId()] = TransformPoint(new Vector2((float)(n.Center.X - n.Width / 2f), (float)(n.Center.Y + n.Height / 2f)), graphRect, canvasRect, true); // Update in the project data var id = GetScenePropertyId(Controller.Instance.SelectedChapterDataControl, scene); ProjectConfigData.setProperty(id + ".X", ((int)positions[scene.getId()].x).ToString()); ProjectConfigData.setProperty(id + ".Y", ((int)positions[scene.getId()].y).ToString()); } } catch (Exception ex) { Debug.LogError(ex.Message + " : " + ex.StackTrace); } }