public void OnSceneGUI() { // Always draw the outline DrawOutline(); if (Application.isPlaying || !tileMap.AllowEdit) { return; } if (editorData.editMode != tk2dTileMapEditorData.EditMode.Paint && editorData.editMode != tk2dTileMapEditorData.EditMode.Color) { return; } if (editorData.editMode == tk2dTileMapEditorData.EditMode.Color && !tileMap.HasColorChannel()) { return; } int controlID = GUIUtility.GetControlID(tileMapHashCode, FocusType.Passive); EventType controlEventType = Event.current.GetTypeForControl(controlID); switch (controlEventType) { case EventType.MouseDown: case EventType.MouseDrag: if ((controlEventType == EventType.MouseDrag && GUIUtility.hotControl != controlID) || Event.current.button != 0) { return; } if (Event.current.type == EventType.MouseDown) { if (IsCursorInside() && !Event.current.shift) { bool pickupKeyDown = (Application.platform == RuntimePlatform.OSXEditor)?Event.current.control:Event.current.alt; bool eraseKeyDown = false; if (Application.platform == RuntimePlatform.OSXEditor) { if (Event.current.command && !Event.current.alt) { eraseKeyDown = true; } } else { eraseKeyDown = Event.current.control; } if (pickupKeyDown) { pickup = true; GUIUtility.hotControl = controlID; } else if (eraseKeyDown) { Undo.RegisterUndo(tileMap, "Erased tile map"); erase = true; GUIUtility.hotControl = controlID; } else if (!Event.current.command && !Event.current.alt && !Event.current.control) { GUIUtility.hotControl = controlID; randomSeed = Random.Range(0, int.MaxValue); Undo.RegisterUndo(tileMap, "Painted tile map"); Splat(); } } } if (Event.current.type == EventType.MouseDrag && GUIUtility.hotControl == controlID) { UpdateCursorPosition(); if (!pickup && !erase) { Splat(); } } break; case EventType.MouseUp: if (Event.current.button == 0 && GUIUtility.hotControl == controlID) { GUIUtility.hotControl = 0; if (pickup) { PickUp(); pickup = false; } else if (erase) { Erase(); erase = false; } else { CommitSplats(); } cursorX0 = cursorX; cursorY0 = cursorY; HandleUtility.Repaint(); } break; case EventType.Layout: //HandleUtility.AddDefaultControl(controlID); break; case EventType.MouseMove: UpdateCursorPosition(); cursorX0 = cursorX; cursorY0 = cursorY; break; } tk2dTileMapEditorBrush activeBrush = editorData.activeBrush; switch (editorData.editMode) { case tk2dTileMapEditorData.EditMode.Paint: DrawTileCursor(activeBrush); break; case tk2dTileMapEditorData.EditMode.Color: DrawPaintCursor(); break; } }
public void OnSceneGUI() { // Always draw the outline DrawOutline(); if (Application.isPlaying || !tileMap.AllowEdit) { return; } if (editorData.editMode == tk2dTileMapEditorData.EditMode.Settings) { return; } if (editorData.editMode != tk2dTileMapEditorData.EditMode.Paint && editorData.editMode != tk2dTileMapEditorData.EditMode.Color) { return; } if (editorData.editMode == tk2dTileMapEditorData.EditMode.Color && !tileMap.HasColorChannel()) { return; } if (Event.current.type == EventType.MouseMove || Event.current.type == EventType.MouseDrag) { tooltipPos = Event.current.mousePosition; } UpdateScratchpadTileSizes(); // Scratchpad tilesort scratchpadGUI.SetTileSort(tileMapData.sortMethod == tk2dTileMapData.SortMethod.BottomLeft || tileMapData.sortMethod == tk2dTileMapData.SortMethod.TopLeft, tileMapData.sortMethod == tk2dTileMapData.SortMethod.BottomLeft || tileMapData.sortMethod == tk2dTileMapData.SortMethod.BottomRight); // Spritedef vars if (tileMap != null) { var spriteDef = tileMap.SpriteCollectionInst.FirstValidDefinition; if (spriteDef != null) { curSpriteDefTexelSize = spriteDef.texelSize; } } // Working brush / tile or paint cursor (behind scratchpad) switch (editorData.editMode) { case tk2dTileMapEditorData.EditMode.Paint: if (!scratchpadGUI.workingHere) { if (Event.current.type == EventType.Repaint) { Matrix4x4 matrix = tileMap.transform.localToWorldMatrix; // Brush mesh is offset so origin is at bottom left. Do the reverse here... // Also add layer depth float layerDepth = GetLayerDepth(editorData.layer); matrix *= Matrix4x4.TRS(tileMapData.tileOrigin + new Vector3(0, 0, layerDepth), Quaternion.identity, Vector3.one); BrushRenderer.DrawBrushInScene(matrix, WorkingBrush, 1000000); } DrawTileCursor(); } break; case tk2dTileMapEditorData.EditMode.Color: DrawPaintCursor(); break; } // Toolbar and scratchpad if (editorData.editMode == tk2dTileMapEditorData.EditMode.Paint) { Handles.BeginGUI(); GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height)); Event ev = Event.current; bool mouseOverToolbar = false; bool mouseOverScratchpad = false; GUILayout.BeginVertical(tk2dEditorSkin.GetStyle("TilemapToolbarBG"), GUILayout.Width(20), GUILayout.Height(34)); tk2dTileMapToolbar.ToolbarWindow(); GUILayout.EndVertical(); mouseOverToolbar = GUILayoutUtility.GetLastRect().Contains(ev.mousePosition); if (tk2dTileMapToolbar.scratchpadOpen) { GUILayout.BeginVertical(tk2dEditorSkin.GetStyle("TilemapToolbarBG"), GUILayout.Width(20), GUILayout.Height(Screen.height - 74)); scratchpadGUI.DrawGUI(); GUILayout.EndVertical(); mouseOverScratchpad = GUILayoutUtility.GetLastRect().Contains(ev.mousePosition); } if (ev.type == EventType.MouseMove) { scratchpadGUI.workingHere = mouseOverToolbar || mouseOverScratchpad; if (scratchpadGUI.workingHere) { ev.Use(); } } GUILayout.EndArea(); Handles.EndGUI(); } else { scratchpadGUI.workingHere = false; } // Draw Tooltip string curTooltip = ""; if (tk2dTileMapToolbar.tooltip.Length > 0) { curTooltip = tk2dTileMapToolbar.tooltip; } if (scratchpadGUI.tooltip.Length > 0) { curTooltip = scratchpadGUI.tooltip; } if (curTooltip.Length > 0 && scratchpadGUI.workingHere) { Handles.BeginGUI(); GUI.contentColor = Color.white; GUIContent tooltipContent = new GUIContent(curTooltip); Vector2 size = GUI.skin.GetStyle("label").CalcSize(tooltipContent); GUI.Label(new Rect(tooltipPos.x, tooltipPos.y + 20, size.x + 10, size.y + 5), curTooltip, "textarea"); Handles.EndGUI(); } if (tk2dTileMapToolbar.scratchpadOpen && !lastScratchpadOpen) { scratchpadGUI.FocusOnSearchFilter(openedScratchpadWithTab); } if (!tk2dTileMapToolbar.scratchpadOpen && lastScratchpadOpen) { openedScratchpadWithTab = false; } lastScratchpadOpen = tk2dTileMapToolbar.scratchpadOpen; int controlID = tileMapHashCode; if (tk2dTileMapToolbar.scratchpadOpen && scratchpadGUI.workingHere) { if (scratchpadGUI.doMouseDown) { GUIUtility.hotControl = controlID; Undo.RegisterUndo(editorData, "Edit scratchpad"); randomSeed = Random.Range(0, int.MaxValue); cursorX0 = cursorX; cursorY0 = cursorY; PencilDrag(); RectangleDragBegin(); } if (scratchpadGUI.doMouseDrag) { UpdateCursorPosition(); UpdateWorkingBrush(); PencilDrag(); } if (scratchpadGUI.doMouseUp) { RectangleDragEnd(); cursorX0 = cursorX; cursorY0 = cursorY; UpdateWorkingBrush(); GUIUtility.hotControl = 0; } if (scratchpadGUI.doMouseMove) { UpdateCursorPosition(); cursorX0 = cursorX; cursorY0 = cursorY; UpdateWorkingBrush(); } } else { EventType controlEventType = Event.current.GetTypeForControl(controlID); switch (controlEventType) { case EventType.MouseDown: case EventType.MouseDrag: if ((controlEventType == EventType.MouseDrag && GUIUtility.hotControl != controlID) || (Event.current.button != 0 && Event.current.button != 1)) { return; } // make sure we don't use up reserved combinations bool inhibitMouseDown = false; if (Application.platform == RuntimePlatform.OSXEditor) { if (Event.current.command && Event.current.alt) // pan combination on mac { inhibitMouseDown = true; } } if (Event.current.type == EventType.MouseDown && !inhibitMouseDown) { CheckVisible(editorData.layer); if (IsCursorInside() && !Event.current.shift) { if (editorData.editMode == tk2dTileMapEditorData.EditMode.Paint) { GUIUtility.hotControl = controlID; Undo.RegisterUndo(tileMap, "Edit tile map"); randomSeed = Random.Range(0, int.MaxValue); PencilDrag(); RectangleDragBegin(); } if (editorData.editMode == tk2dTileMapEditorData.EditMode.Color) { GUIUtility.hotControl = controlID; Undo.RegisterUndo(tileMap, "Paint tile map"); if (tk2dTileMapToolbar.colorBlendMode != tk2dTileMapToolbar.ColorBlendMode.Eyedropper) { PaintColorBrush((float)vertexCursorX, (float)vertexCursorY); host.BuildIncremental(); } } } } if (Event.current.type == EventType.MouseDrag && GUIUtility.hotControl == controlID) { UpdateCursorPosition(); UpdateWorkingBrush(); if (editorData.editMode == tk2dTileMapEditorData.EditMode.Paint) { PencilDrag(); } if (editorData.editMode == tk2dTileMapEditorData.EditMode.Color) { if (tk2dTileMapToolbar.colorBlendMode != tk2dTileMapToolbar.ColorBlendMode.Eyedropper) { PaintColorBrush((float)vertexCursorX, (float)vertexCursorY); host.BuildIncremental(); } } } break; case EventType.MouseUp: if ((Event.current.button == 0 || Event.current.button == 1) && GUIUtility.hotControl == controlID) { GUIUtility.hotControl = 0; if (editorData.editMode == tk2dTileMapEditorData.EditMode.Paint) { RectangleDragEnd(); } if (editorData.editMode == tk2dTileMapEditorData.EditMode.Color) { if (tk2dTileMapToolbar.colorBlendMode == tk2dTileMapToolbar.ColorBlendMode.Eyedropper) { PickUpColor(); } } cursorX0 = cursorX; cursorY0 = cursorY; UpdateWorkingBrush(); HandleUtility.Repaint(); } break; case EventType.Layout: //HandleUtility.AddDefaultControl(controlID); break; case EventType.MouseMove: UpdateCursorPosition(); cursorX0 = cursorX; cursorY0 = cursorY; UpdateWorkingBrush(); break; } } // Set cursor color based on toolbar main mode bool updateCursorColor = false; if (currentModeForBrushColor != (int)tk2dTileMapToolbar.mainMode) { currentModeForBrushColor = (int)tk2dTileMapToolbar.mainMode; updateCursorColor = true; } if (tk2dPreferencesEditor.CheckTilemapCursorColorUpdate()) { updateCursorColor = true; } if (updateCursorColor) { switch (tk2dTileMapToolbar.mainMode) { case tk2dTileMapToolbar.MainMode.Brush: tileSelectionFillColor = tk2dPreferences.inst.tileMapToolColor_brush; break; case tk2dTileMapToolbar.MainMode.BrushRandom: tileSelectionFillColor = tk2dPreferences.inst.tileMapToolColor_brushRandom; break; case tk2dTileMapToolbar.MainMode.Erase: tileSelectionFillColor = tk2dPreferences.inst.tileMapToolColor_erase; break; case tk2dTileMapToolbar.MainMode.Eyedropper: tileSelectionFillColor = tk2dPreferences.inst.tileMapToolColor_eyedropper; break; case tk2dTileMapToolbar.MainMode.Cut: tileSelectionFillColor = tk2dPreferences.inst.tileMapToolColor_cut; break; } tileSelectionOutlineColor = (tileSelectionFillColor + Color.white) * new Color(0.5f, 0.5f, 0.5f, 1.0f); } // Hotkeys switch the static toolbar mode { bool pickupKeyDown = (Application.platform == RuntimePlatform.OSXEditor)?Event.current.control:Event.current.alt; if (Event.current.button == 1) { pickupKeyDown = true; } bool eraseKeyDown = false; if (Application.platform == RuntimePlatform.OSXEditor) { if (Event.current.command && !Event.current.alt) { eraseKeyDown = true; } } else { eraseKeyDown = Event.current.control; } bool hotkeysPressed = pickupKeyDown || eraseKeyDown; if (editorData.editMode == tk2dTileMapEditorData.EditMode.Paint) { if (!pencilDragActive) { if (hotkeysPressed) { if (!hotkeyModeSwitchActive) { // Push mode pushedToolbarMainMode = tk2dTileMapToolbar.mainMode; } if (pickupKeyDown) { if (eraseKeyDown) { pendingModeChange = delegate(int i) { tk2dTileMapToolbar.mainMode = tk2dTileMapToolbar.MainMode.Cut; hotkeyModeSwitchActive = true; }; } else { pendingModeChange = delegate(int i) { tk2dTileMapToolbar.mainMode = tk2dTileMapToolbar.MainMode.Eyedropper; hotkeyModeSwitchActive = true; }; } } else if (eraseKeyDown) { pendingModeChange = delegate(int i) { tk2dTileMapToolbar.mainMode = tk2dTileMapToolbar.MainMode.Erase; hotkeyModeSwitchActive = true; }; } } else { if (hotkeyModeSwitchActive) { // Pop mode pendingModeChange = delegate(int i) { tk2dTileMapToolbar.mainMode = pushedToolbarMainMode; hotkeyModeSwitchActive = false; }; } } } } if (editorData.editMode == tk2dTileMapEditorData.EditMode.Color) { if (hotkeysPressed) { if (!hotkeyModeSwitchActive) { // Push mode pushedToolbarColorBlendMode = tk2dTileMapToolbar.colorBlendMode; if (pickupKeyDown) { tk2dTileMapToolbar.colorBlendMode = tk2dTileMapToolbar.ColorBlendMode.Eyedropper; hotkeyModeSwitchActive = true; } else if (eraseKeyDown) { switch (tk2dTileMapToolbar.colorBlendMode) { case tk2dTileMapToolbar.ColorBlendMode.Addition: tk2dTileMapToolbar.colorBlendMode = tk2dTileMapToolbar.ColorBlendMode.Subtraction; break; case tk2dTileMapToolbar.ColorBlendMode.Subtraction: tk2dTileMapToolbar.colorBlendMode = tk2dTileMapToolbar.ColorBlendMode.Addition; break; } hotkeyModeSwitchActive = true; } } } else { if (hotkeyModeSwitchActive) { tk2dTileMapToolbar.colorBlendMode = pushedToolbarColorBlendMode; hotkeyModeSwitchActive = false; } } } } // Hotkeys toggle flipping, scratchpad, paint { Event ev = Event.current; if (ev.type == EventType.KeyDown) { if (ev.keyCode == KeyCode.Tab) { if (!tk2dTileMapToolbar.scratchpadOpen) { pendingModeChange = delegate(int i) { tk2dTileMapToolbar.scratchpadOpen = !tk2dTileMapToolbar.scratchpadOpen; if (tk2dTileMapToolbar.scratchpadOpen) { openedScratchpadWithTab = true; } }; } } switch (ev.character) { case 'h': ev.Use(); tk2dTileMapToolbar.workBrushFlipX = !tk2dTileMapToolbar.workBrushFlipX; UpdateWorkingBrush(); break; case 'j': ev.Use(); tk2dTileMapToolbar.workBrushFlipY = !tk2dTileMapToolbar.workBrushFlipY; UpdateWorkingBrush(); break; case '[': ev.Use(); tk2dTileMapToolbar.colorBrushRadius -= 0.5f; if (tk2dTileMapToolbar.colorBrushRadius < 1.0f) { tk2dTileMapToolbar.colorBrushRadius = 1.0f; } break; case ']': ev.Use(); tk2dTileMapToolbar.colorBrushRadius += 0.5f; break; case '-': case '_': ev.Use(); tk2dTileMapToolbar.colorBrushIntensity -= 0.01f; break; case '=': case '+': ev.Use(); tk2dTileMapToolbar.colorBrushIntensity += 0.01f; break; } } if (scratchpadGUI.requestClose) { scratchpadGUI.requestClose = false; pendingModeChange = delegate(int i) { tk2dTileMapToolbar.scratchpadOpen = false; }; } } // Hotkey (enter) selects scratchpad tiles if (scratchpadGUI.requestSelectAllTiles) { // fake mouse coords scratchpadGUI.GetTilesCropRect(out cursorX, out cursorY, out cursorX0, out cursorY0); PickUpBrush(editorData.activeBrush, false); tk2dTileMapToolbar.workBrushFlipX = false; tk2dTileMapToolbar.workBrushFlipY = false; scratchpadGUI.requestSelectAllTiles = false; } if (pendingModeChange != null && Event.current.type == EventType.Repaint) { pendingModeChange(0); pendingModeChange = null; UpdateWorkingBrush(); HandleUtility.Repaint(); } }