Ejemplo n.º 1
0
    private void DuringSceneGui(SceneView sceneView)
    {
        var currentEvent      = Event.current;
        var isPaintKeyDown    = currentEvent.control;
        var isEraseKeyDown    = currentEvent.shift;
        var isUseBrushKeyHold = isPaintKeyDown || isEraseKeyDown;

        if (isUseBrushKeyHold)
        {
            HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));

            if (TryGetMouseHit(currentEvent, out var hit))
            {
                Handles.color = Color.green;
                var brushSize      = 2f;
                var normalRotation = Quaternion.LookRotation(hit.normal);
                var snappedPoint   = _grid.CellToWorld(_grid.WorldToCell(hit.point));
                Handles.ArrowHandleCap(2, snappedPoint, normalRotation, brushSize, EventType.Repaint);
                Handles.CircleHandleCap(3, snappedPoint, normalRotation, brushSize, EventType.Repaint);

                var gridPosition = GetGridPosition(hit.point);
                Handles.BeginGUI();
                GUILayout.Label($"({gridPosition.x},{gridPosition.y})");
                Handles.EndGUI();
            }
        }

        if (isUseBrushKeyHold && currentEvent.type == EventType.MouseDown)
        {
            if (_isDrawing == false)
            {
                OnStartDrawing();
            }
            _isDrawing = true;
        }

        if (_isDrawing && (!isUseBrushKeyHold || currentEvent.type == EventType.MouseUp))
        {
            _isDrawing = false;
        }

        if (_isDrawing)
        {
            if (TryGetMouseHit(currentEvent, out var hit))
            {
                var cellPosition = GetGridPosition(hit.point);
                var isGround     = IsGround(cellPosition);
                if (isPaintKeyDown)
                {
                    if (_currentBrushCellConfig != null && (_levelConfig.IsCellFree(cellPosition) || isGround))
                    {
                        var isFree = _levelConfig.IsCellFree(cellPosition);
                        var isBrushOfModifierType = _currentBrushCellConfig.CellConfigMin.CellType == CellType.Modifier;

                        var cellDataMin = new CellDataMin(cellPosition, _currentBrushCellConfig.CellConfigMin);

                        if (isBrushOfModifierType && isGround)
                        {
                            EraseModifier(cellPosition);
                            _levelConfig.AddModifier(cellDataMin);
                            DrawModifier(cellDataMin);
                        }
                        else
                        {
                            EraseCell(cellPosition);
                            _levelConfig.AddCell(cellDataMin);
                            DrawCell(cellDataMin);
                        }
                    }
                }
                else
                {
                    if (!_levelConfig.IsCellFree(cellPosition) && !isGround)
                    {
                        EraseCell(cellPosition);

                        var cellConfig  = _configsProvider.CellConfigProvider.GetConfig(CellType.Ground);
                        var cellDataMin = new CellDataMin(cellPosition, cellConfig.CellConfigMin);
                        _levelConfig.AddCell(cellDataMin);

                        DrawCell(cellDataMin);
                    }
                    else if (isGround)
                    {
                        EraseModifier(cellPosition);
                    }
                }
            }
        }
    }