コード例 #1
0
        /// <summary>
        /// Updates all the little components of mouse controller.
        /// </summary>
        private void Update()
        {
            IMouseHandler handler              = mouseHandlers[(int)currentMode];
            Vector3       mousePos             = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            bool          mouseButton0Up       = Input.GetMouseButtonUp(0);
            bool          mouseButton1Up       = Input.GetMouseButtonUp(1);
            bool          mouseButton2Up       = Input.GetMouseButtonUp(2);
            bool          performDragThisFrame = false;

            CurrentFramePosition = new Vector3(mousePos.x, mousePos.y, WorldController.Instance.CameraController.CurrentLayer);

            // The mode is enabled
            if (statusOfModes[(int)currentMode])
            {
                if ((handler.CallbacksEnabled & MouseHandlerCallbacks.HANDLE_PLACING_POSITION) == MouseHandlerCallbacks.HANDLE_PLACING_POSITION)
                {
                    CurrentPlacingPosition = handler.HandlePlacingPosition(CurrentFramePosition);
                }
                else
                {
                    CurrentPlacingPosition = CurrentFramePosition;
                }

                if ((mouseButton0Up || mouseButton1Up) && IsDragging)
                {
                    if (IsPanning == false)
                    {
                        uiTooltip          = null;
                        mouseCursor.UIMode = false;
                    }

                    IsDragging = false;
                    Selection  = null;

                    if (mouseButton0Up)
                    {
                        // If we are over a UI element then don't perform the drag this frame
                        // Also only perform the drag if we are actually ending the drag with left click
                        // else its a cancel drag action
                        performDragThisFrame = !EventSystem.current.IsPointerOverGameObject();
                    }
                }
                else if (IsDragging == false && Input.GetMouseButtonDown(0))
                {
                    IsDragging        = true;
                    dragStartPosition = CurrentPlacingPosition;
                }
            }

            mouseCursor.UpdateCursor(statusOfModes[(int)currentMode]);

            // HANDLE DRAG
            // Clear all the drag objects
            for (int i = 0; i < DragPreviewGameObjects.Count; i++)
            {
                SimplePool.Despawn(DragPreviewGameObjects[i]);
            }

            DragPreviewGameObjects.Clear();

            if (statusOfModes[(int)currentMode])
            {
                // If callback for handling drag is enabled then handle the drag
                bool dragEnabled       = (handler.CallbacksEnabled & MouseHandlerCallbacks.HANDLE_DRAG_FINISHED) == MouseHandlerCallbacks.HANDLE_DRAG_FINISHED;
                bool dragVisualEnabled = (handler.CallbacksEnabled & MouseHandlerCallbacks.HANDLE_DRAG_VISUAL) == MouseHandlerCallbacks.HANDLE_DRAG_VISUAL;

                if (dragEnabled || dragVisualEnabled)
                {
                    DragParameters dragParams = handler.DisableDragging || (IsDragging == false && performDragThisFrame == false) ? new DragParameters(CurrentPlacingPosition) : new DragParameters(dragStartPosition, CurrentPlacingPosition);

                    if (dragVisualEnabled)
                    {
                        // HANDLE VISUAL
                        DragPreviewGameObjects = handler.HandleDragVisual(dragParams, mouseCursor.CursorCanvasGameObject.transform);
                    }

                    // If we have dragEnabled and we are to perform it on our next frame (which is this frame) perform it
                    if (dragEnabled && performDragThisFrame)
                    {
                        // HANDLE DRAG
                        handler.HandleDragFinished(dragParams);
                    }
                }
            }

            UpdateCameraMovement();

            if (statusOfModes[(int)currentMode])
            {
                // Tooltip handling
                if ((handler.CallbacksEnabled & MouseHandlerCallbacks.HANDLE_TOOLTIP) == MouseHandlerCallbacks.HANDLE_TOOLTIP)
                {
                    handler.HandleTooltip(CurrentFramePosition, mouseCursor, IsDragging);
                }

                // Could include drag clicks
                // Should handle any 'building' that requires dragging in the HandleDrag callback
                if ((handler.CallbacksEnabled & MouseHandlerCallbacks.HANDLE_CLICK) == MouseHandlerCallbacks.HANDLE_CLICK && EventSystem.current.IsPointerOverGameObject() == false)
                {
                    if (mouseButton0Up)
                    {
                        handler.HandleClick(CurrentFramePosition, 0);
                    }

                    if (mouseButton1Up)
                    {
                        handler.HandleClick(CurrentFramePosition, 1);
                    }

                    if (mouseButton2Up)
                    {
                        handler.HandleClick(CurrentFramePosition, 2);
                    }
                }
            }

            // Save the mouse position from this frame.
            // We don't use currentFramePosition because we may have moved the camera.
            mousePos            = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            lastFramePosition.x = mousePos.x;
            lastFramePosition.y = mousePos.y;
            lastFramePosition.z = WorldController.Instance.CameraController.CurrentLayer;
        }