public void OnSceneGUI() { mouseDoubleClick.Update(); if (tileMap == null || tileMap.tileSet == null) { return; } Vector3 saved_pos = tileMap.transform.position; tileMap.transform.position += (Vector3)(Vector2.Scale(Camera.current.transform.position, (Vector2.one - tileMap.parallax_factor))); //apply parallax is_tileSetBrush_visible = edit_mode == TileMapEditorEditMode.Paint; if (edit_mode == TileMapEditorEditMode.Paint) { OnSceneGUI_DoPaint(); if (GetTileSetBrushMode() == TileMapEditorBrushMode.Fill && Event.current.type == EventType.Repaint) { Color fill_preview_color = new Color(1f, 1f, 1f, 0.2f); Color empty_color = new Color(0f, 0f, 0f, 0f); Rect tile_rect = new Rect(Vector2.zero, tileMap.cell_size); foreach (Vector2 tile_pos in fill_preview_tile_pos_list) { tile_rect.position = tile_pos; DrawUtil.HandlesDrawSolidRectangleWithOutline(tile_rect, fill_preview_color, empty_color, tileMap.transform); } } // else if (_edit_mode == TileMapEditorEditMode.Map) // DoMapSceneGUI(); // else if (_edit_mode == TileMapEditorEditMode.Collider) // DoColliderSceneGUI(); TileSetBrushBehaviour.SetVisible(is_tileSetBrush_visible); tileMap.transform.position = saved_pos; // restore position } }
private void OnTileSetBrushSelected(TileSet tileSet, int prevBrushId, int newBrushId) { Tools.current = Tool.Rect; ResetTileSetBrushMode(); TileSetBrushBehaviour tileSetBrushBehaviour = TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap); tileSetBrushBehaviour.tileMap.ClearMap(); tileSetBrushBehaviour.tileMap.SetTileData(0, 0, (uint)(newBrushId << 16) | TileSetConst.TileDataMask_TileId); tileSetBrushBehaviour.tileMap.UpdateMesh(); tileSetBrushBehaviour.offset = Vector2.zero; }
private void OnTileSelectionChanged(TileSet tileSet) { Tools.current = Tool.Rect; ResetTileSetBrushMode(); TileSetBrushBehaviour tileSetBrushBehaviour = TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap); tileSetBrushBehaviour.tileMap.ClearMap(); if (tileSet.tileSelection != null) { for (int i = 0; i < tileSet.tileSelection.selection_tileData_list.Count; ++i) { int gx = i % tileSet.tileSelection.column_count; int gy = i / tileSet.tileSelection.column_count; tileSetBrushBehaviour.tileMap.SetTileData(gx, gy, (uint)tileSet.tileSelection.selection_tileData_list[i]); } } tileSetBrushBehaviour.tileMap.UpdateMesh(); tileSetBrushBehaviour.offset = Vector2.zero; }
public static ReorderableList CreateTileViewReorderableList(TileSet tileSet) { ReorderableList tileView_reorderableList = new ReorderableList(tileSet.tileView_list, typeof(TileView), true, true, true, true); tileView_reorderableList.onAddDropdownCallback = (Rect buttonRect, ReorderableList reorderableList) => { GenericMenu menu = new GenericMenu(); GenericMenu.MenuFunction add_tileSelection_func = () => { TileSelection tileSelection = tileSet.tileSelection.Clone(); tileSelection.FlipVertical(); // flip vertical to fit the tileset coordinate system ( from top to bottom ) tileSet.AddTileView("new TileView", tileSelection); EditorUtility.SetDirty(tileSet); }; GenericMenu.MenuFunction add_tileSetBrushSelection_func = () => { TileSelection tileSelection = TileSetBrushBehaviour.CreateTileSelection(); tileSet.AddTileView("new TileView", tileSelection); EditorUtility.SetDirty(tileSet); }; GenericMenu.MenuFunction remove_all_tileViews_func = () => { if (EditorUtility.DisplayDialog("警告!", "是否删除全部的TileViews?", "Yes", "No")) { tileSet.RemoveAllTileViewList(); EditorUtility.SetDirty(tileSet); } }; if (tileSet.tileSelection != null) { menu.AddItem(new GUIContent("添加TileSelection"), false, add_tileSelection_func); } else { menu.AddDisabledItem(new GUIContent("添加TileSelection到TileView")); } if (TileSetBrushBehaviour.GetTileSetBrushTileSet() == tileSet && TileSetBrushBehaviour.CreateTileSelection() != null) { menu.AddItem(new GUIContent("Add TileSetBrush Selection"), false, add_tileSetBrushSelection_func); } menu.AddSeparator(""); menu.AddItem(new GUIContent("删除全部TileViews"), false, remove_all_tileViews_func); menu.AddSeparator(""); menu.AddItem(new GUIContent("按Name排序"), false, tileSet.SortTileViewListByName); menu.ShowAsContext(); }; tileView_reorderableList.onRemoveCallback = (ReorderableList reorderableList) => { if (EditorUtility.DisplayDialog("警告!", "是否确定删除这个TileView?", "Yes", "No")) { ReorderableList.defaultBehaviours.DoRemoveButton(reorderableList); EditorUtility.SetDirty(tileSet); } }; tileView_reorderableList.drawHeaderCallback = (Rect rect) => { EditorGUI.LabelField(rect, "TileViews", EditorStyles.boldLabel); Texture2D button_texture = tileView_reorderableList.elementHeight == 0f ? EditorGUIUtility.FindTexture("winbtn_win_max_h") : EditorGUIUtility.FindTexture("winbtn_win_min_h"); if (GUI.Button(new Rect(rect.width - rect.height, rect.y, rect.height, rect.height), button_texture, EditorStyles.label)) { tileView_reorderableList.elementHeight = tileView_reorderableList.elementHeight == 0f ? EditorGUIUtility.singleLineHeight : 0f; tileView_reorderableList.draggable = tileView_reorderableList.elementHeight > 0f; } }; tileView_reorderableList.drawElementCallback = (Rect rect, int index, bool is_active, bool is_focused) => { if (tileView_reorderableList.elementHeight == 0f) { return; } Rect label_rect = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight); TileView tileView = tileView_reorderableList.list[index] as TileView; if (index == tileView_reorderableList.index) { string new_name = EditorGUI.TextField(label_rect, tileView.name); if (new_name != tileView.name) { tileSet.RenameTileView(tileView.name, new_name); } } else { EditorGUI.LabelField(label_rect, tileView.name); } }; return(tileView_reorderableList); }
public void OnSceneGUI_DoPaint() { Event e = Event.current; DrawToolbar(); if (DragAndDrop.objectReferences.Length > 0 || // hide brush when user is dragging a prefab into the scene EditorWindow.mouseOverWindow != SceneView.currentDrawingSceneView || // hide brush when it's not over the scene view (Tools.current != Tool.Rect && Tools.current != Tool.None)) { is_tileSetBrush_visible = false; SceneView.RepaintAll(); return; } int control_id = GUIUtility.GetControlID(FocusType.Passive); HandleUtility.AddDefaultControl(control_id); EventType current_event_type = Event.current.GetTypeForControl(control_id); bool is_skip = false; int save_control = GUIUtility.hotControl; try { if (current_event_type == EventType.Layout) { is_skip = true; } else if (current_event_type == EventType.ScrollWheel) { is_skip = true; } if (tileMap.tileSet == null) { return; } if (!is_skip) { if (e.type == EventType.KeyDown) { switch (e.keyCode) { case TileToolbarConst.Key_FlipH: TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap).FlipH(!e.shift); e.Use(); // Use key event break; case TileToolbarConst.Key_FlipV: TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap).FlipV(!e.shift); e.Use(); // Use key event break; case TileToolbarConst.Key_Rot90: TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap).Rot90(!e.shift); e.Use(); // Use key event break; case TileToolbarConst.Key_Rot90Back: TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap).Rot90Back(!e.shift); e.Use(); // Use key event break; case TileToolbarConst.Key_PencilTool: case TileToolbarConst.Key_LineTool: case TileToolbarConst.Key_RectTool: case TileToolbarConst.Key_EllipseTool: switch (e.keyCode) { case TileToolbarConst.Key_PencilTool: TileSetBrushBehaviour.instance.paint_mode = TileSetBrushPaintMode.Pencil; break; case TileToolbarConst.Key_LineTool: TileSetBrushBehaviour.instance.paint_mode = TileSetBrushPaintMode.Line; break; case TileToolbarConst.Key_RectTool: TileSetBrushBehaviour.instance.paint_mode = TileSetBrushBehaviour.instance.paint_mode == TileSetBrushPaintMode.Rect ? TileSetBrushPaintMode.FilledRect : TileSetBrushPaintMode.Rect;//在filledRect和Rect之间切换 break; case TileToolbarConst.Key_EllipseTool: TileSetBrushBehaviour.instance.paint_mode = TileSetBrushBehaviour.instance.paint_mode == TileSetBrushPaintMode.Ellipse ? TileSetBrushPaintMode.FilledEllipse : TileSetBrushPaintMode.Ellipse;//在FilledEllipse和Ellipse之间切换 break; } tileSetBrush_mode = TileMapEditorBrushMode.Paint; TileToolbar.instance.tileSetBrushToolbar.TriggerButton((int)tileSetBrush_mode); TileToolbar.instance.tileSetBrushPaintToolbar.TriggerButton((int)TileSetBrushBehaviour.instance .paint_mode); e.Use(); break; } } EditorGUIUtility.AddCursorRect(new Rect(0f, 0f, (float)Screen.width, (float)Screen.height), MouseCursor.Arrow); GUIUtility.hotControl = control_id; { Plane tileMapChunk_plane = new Plane(tileMap.transform.forward, tileMap.transform.position); Vector2 mouse_pos = Event.current.mousePosition; mouse_pos.y = Screen.height - mouse_pos.y; Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); float distance; if (tileMapChunk_plane.Raycast(ray, out distance)) { Rect tile_rect = new Rect(0, 0, tileMap.cell_size.x, tileMap.cell_size.y); tile_rect.position = tileMap.transform.InverseTransformPoint(ray.GetPoint(distance)); Vector2 tile_pos = tile_rect.position; if (tile_pos.x < 0) { tile_pos.x -= tileMap.cell_size.x;//因为负数是从-1开始的,正数是从0开始的 } if (tile_pos.y < 0) { tile_pos.y -= tileMap.cell_size.y; //因为负数是从-1开始的,正数是从0开始的 } tile_pos.x -= tile_pos.x % tileMap.cell_size.x; //取整 tile_pos.y -= tile_pos.y % tileMap.cell_size.y; //取整 tile_rect.position = tile_pos; Vector2 start_pos = new Vector2(Mathf.Min(start_dragging_pos.x, end_dragging_pos.x), Mathf.Min(start_dragging_pos.y, end_dragging_pos.y)); Vector2 end_pos = new Vector2(Mathf.Max(start_dragging_pos.x, end_dragging_pos.x), Mathf.Max(start_dragging_pos.y, end_dragging_pos.y)); Vector2 selection_snapped_pos = TileSetBrushUtil.GetSnappedPosition(start_pos, tileMap.cell_size); Vector2 selection_size = TileSetBrushUtil.GetSnappedPosition(end_pos, tileMap.cell_size) - selection_snapped_pos + tileMap.cell_size; TileSetBrushBehaviour tileSetBrushBehaviour = TileSetBrushBehaviour.instance.GetOrCreateTileSetBrushBehaviour(tileMap); // Update brush transform local_tileSetBrushBehaviour_pos = (Vector2)tileMap.transform.InverseTransformPoint(ray.GetPoint(distance));//设置tileSetBrushBehaviour的position Vector2 tileSetBrushBehaviour_snapped_pos = TileSetBrushUtil.GetSnappedPosition((tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos), tileMap.cell_size); tileSetBrushBehaviour.transform.rotation = tileMap.transform.rotation; tileSetBrushBehaviour.transform.localScale = tileMap.transform.lossyScale; if (!TileSetBrushBehaviour.instance.is_dragging)//不是拖动的时候 { tileSetBrushBehaviour.transform.position = tileMap.transform.TransformPoint(new Vector3(tileSetBrushBehaviour_snapped_pos.x, tileSetBrushBehaviour_snapped_pos.y, -0.01f)); } int pre_mouse_grid_x = mouse_grid_x; int pre_mouse_grid_y = mouse_grid_y; if (e.isMouse) { mouse_grid_x = TileSetBrushUtil.GetGridX(local_tileSetBrushBehaviour_pos, tileMap.cell_size); mouse_grid_y = TileSetBrushUtil.GetGridY(local_tileSetBrushBehaviour_pos, tileMap.cell_size); } bool is_mouse_grid_changed = pre_mouse_grid_x != mouse_grid_x || pre_mouse_grid_y != mouse_grid_y; //Update Fill Preview if (GetTileSetBrushMode() == TileMapEditorBrushMode.Fill && is_mouse_grid_changed) { fill_preview_tile_pos_list.Clear(); TileMapDrawingUtil.FloodFillPreview(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos, tileSetBrushBehaviour.tileMap.GetTileData(0, 0), fill_preview_tile_pos_list); } bool is_modifiers_changed = false; if (e.isKey) { EventModifiers filtered_modifiers = e.modifiers & (EventModifiers.Control | EventModifiers.Alt); is_modifiers_changed = filtered_modifiers != m_prevEventModifiers; m_prevEventModifiers = filtered_modifiers; } if ( (EditorWindow.focusedWindow == EditorWindow.mouseOverWindow ) && // fix painting tiles when closing another window popup over the SceneView like GameObject Selection window (e.type == EventType.MouseDown || e.type == EventType.MouseDrag && is_mouse_grid_changed || e.type == EventType.MouseUp || is_modifiers_changed) ) { if (e.button == 0) { if (mouseDoubleClick.IsDoubleClick && tileSetBrushBehaviour.tileMap.GridWidth == 1 && tileSetBrushBehaviour.tileMap.GridHeight == 1) { // Restore previous tiledata modified by Paint, because before the double click, a single click is done before tileMap.SetTileData(tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos, flood_fill_restored_tileData); tileSetBrushBehaviour.FloodFill(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos); } // Do a brush paint action else { switch (GetTileSetBrushMode()) { case TileMapEditorBrushMode.Paint: if (e.type == EventType.MouseDown) { flood_fill_restored_tileData = tileMap.GetTileData(mouse_grid_x, mouse_grid_y); } if (e.type == EventType.MouseDown) { tileSetBrushBehaviour.DoPaintPressed(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos, e.modifiers); } else if (e.type == EventType.MouseDrag || is_modifiers_changed && TileSetBrushBehaviour.instance.paint_mode != TileSetBrushPaintMode.Pencil) { tileSetBrushBehaviour.DoPaintDragged(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos, e.modifiers); } else if (e.type == EventType.MouseUp) { tileSetBrushBehaviour.DoPaintReleased(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos, e.modifiers); } break; case TileMapEditorBrushMode.Erase: tileSetBrushBehaviour.Erase(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos); break; case TileMapEditorBrushMode.Fill: tileSetBrushBehaviour.FloodFill(tileMap, tileSetBrushBehaviour.offset + local_tileSetBrushBehaviour_pos); break; } } } else if (e.button == 1) { if (e.type == EventType.MouseDown) { is_dragging = true; tileSetBrushBehaviour.tileMap.ClearMap(); start_dragging_pos = end_dragging_pos = local_tileSetBrushBehaviour_pos; } else { end_dragging_pos = local_tileSetBrushBehaviour_pos; } } } if (e.type == EventType.MouseUp) { if (e.button == 1) // right mouse button { is_dragging = false; ResetTileSetBrushMode(); // Copy one tile if (selection_size.x <= tileMap.cell_size.x && selection_size.y <= tileMap.cell_size.y) { uint tileData = tileMap.GetTileData(local_tileSetBrushBehaviour_pos); //Select the first tile not null if any and select the tilemap if (e.control && tileMap.parent_tileMapGroup) { for (int i = tileMap.parent_tileMapGroup.tileMap_list.Count - 1; i >= 0; --i) { var tileMap = this.tileMap.parent_tileMapGroup.tileMap_list[i]; tileData = tileMap.GetTileData(local_tileSetBrushBehaviour_pos); if (tileData != TileSetConst.TileData_Empty) { tileMap.parent_tileMapGroup.selected_tileMap = tileMap; if (Selection.activeGameObject == this.tileMap.gameObject) { Selection.activeGameObject = tileMap.gameObject; } break; } } } if (tileData == TileSetConst.TileData_Empty) { tileMap.tileSet.selected_tileId = TileSetConst.TileId_Empty; tileSetBrushBehaviour.tileMap.SetTileData(0, 0, TileSetConst.TileData_Empty); } else { int brushId = TileSetUtil.GetTileSetBrushIdFromTileData(tileData); int tileId = TileSetUtil.GetTileIdFromTileData(tileData); // Select the copied tile in the tileset if (brushId > 0 && !e.alt) //NOTE: if Alt is held, the tile is selected instead { tileMap.tileSet.selected_tileSetBrushId = brushId; } else { tileMap.tileSet.selected_tileId = tileId; tileSetBrushBehaviour.tileMap.SetTileData(0, 0, tileData & ~TileSetConst.TileDataMask_TileSetBrushId); // keep tile flags } } // Cut tile if key shift is pressed if (e.shift) { int startGridX = TileSetBrushUtil.GetGridX(start_pos, tileMap.cell_size); int startGridY = TileSetBrushUtil.GetGridY(start_pos, tileMap.cell_size); tileSetBrushBehaviour.CutRect(tileMap, startGridX, startGridY, startGridX, startGridY); } tileSetBrushBehaviour.tileMap.UpdateMesh(); tileSetBrushBehaviour.offset = Vector2.zero; } // copy a rect of tiles else { int startGridX = TileSetBrushUtil.GetGridX(start_pos, tileMap.cell_size); int startGridY = TileSetBrushUtil.GetGridY(start_pos, tileMap.cell_size); int endGridX = TileSetBrushUtil.GetGridX(end_pos, tileMap.cell_size); int endGridY = TileSetBrushUtil.GetGridY(end_pos, tileMap.cell_size); // Cut tile if key shift is pressed if (e.shift) { tileSetBrushBehaviour.CutRect(tileMap, startGridX, startGridY, endGridX, endGridY); } else { tileSetBrushBehaviour.CopyRect(tileMap, startGridX, startGridY, endGridX, endGridY); } tileSetBrushBehaviour.offset.x = end_dragging_pos.x > start_dragging_pos.x ? -(endGridX - startGridX) * tileMap.cell_size.x : 0f; tileSetBrushBehaviour.offset.y = end_dragging_pos.y > start_dragging_pos.y ? -(endGridY - startGridY) * tileMap.cell_size.y : 0f; } } } if (is_dragging) { Rect rGizmo = new Rect(selection_snapped_pos, selection_size); DrawUtil.HandlesDrawSolidRectangleWithOutline(rGizmo, new Color(), Color.white, tileMap.transform); } else // Draw brush border { Rect rBound = new Rect(tileSetBrushBehaviour.tileMap.tileMapBounds.min, tileSetBrushBehaviour.tileMap.tileMapBounds.size); Color fillColor; switch (GetTileSetBrushMode()) { case TileMapEditorBrushMode.Paint: fillColor = new Color(0, 0, 0, 0); break; case TileMapEditorBrushMode.Erase: fillColor = new Color(1f, 0f, 0f, 0.1f); break; case TileMapEditorBrushMode.Fill: fillColor = new Color(1f, 1f, 0f, 0.2f); break; default: fillColor = new Color(0, 0, 0, 0); break; } DrawUtil.HandlesDrawSolidRectangleWithOutline(rBound, fillColor, new Color(1, 1, 1, 0.2f), tileSetBrushBehaviour.transform); } } } if (current_event_type == EventType.MouseDrag && Event.current.button < 2) // 2 is for central mouse button { // avoid dragging the map Event.current.Use(); } } } catch (Exception exception) { Debug.LogException(exception); } SceneView.RepaintAll(); GUIUtility.hotControl = save_control; }
void OnDisable() { UnregisterTilesetEvents(tileMap_tileSet); TileSetBrushBehaviour.SetVisible(false); }