private void ShowPreviews(DragParameters dragParams) { for (int x = dragParams.StartX; x <= dragParams.EndX; x++) { for (int y = dragParams.StartY; y <= dragParams.EndY; y++) { Tile t = WorldController.Instance.World.GetTileAt(x, y, WorldController.Instance.cameraController.CurrentLayer); if (t != null) { // Display the building hint on top of this tile position. if (bmc.buildMode == BuildMode.FURNITURE) { Furniture proto = PrototypeManager.Furniture.Get(bmc.buildModeType); if (IsPartOfDrag(t, dragParams, proto.DragType)) { ShowFurnitureSpriteAtTile(bmc.buildModeType, t); ShowWorkSpotSpriteAtTile(bmc.buildModeType, t); } } else if (bmc.buildMode == BuildMode.UTILITY) { Utility proto = PrototypeManager.Utility.Get(bmc.buildModeType); if (IsPartOfDrag(t, dragParams, proto.DragType)) { ShowUtilitySpriteAtTile(bmc.buildModeType, t); } } else { ShowGenericVisuals(x, y); } } } } }
private void ShowPreviews(DragParameters dragParams) { for (int x = dragParams.StartX; x <= dragParams.EndX; x++) { for (int y = dragParams.StartY; y <= dragParams.EndY; y++) { Tile t = WorldController.Instance.world.GetTileAt(x, y); if (t != null) { // Display the building hint on top of this tile position. if (bmc.buildMode == BuildMode.FURNITURE) { Furniture proto = World.current.furniturePrototypes[bmc.buildModeObjectType]; if (IsPartOfDrag(t, dragParams, proto.dragType)) { ShowFurnitureSpriteAtTile(bmc.buildModeObjectType, t); } } else { ShowGenericVisuals(x, y); } } } } }
private void UpdateDragging() { CleanUpDragPreviews(); if (currentMode != MouseMode.BUILD) { return; } UpdateIsDragging(); if (isDragging == false || bmc.IsObjectDraggable() == false) { dragStartPosition = currPlacingPosition; } DragParameters dragParams = GetDragParameters(); ShowPreviews(dragParams); // End Drag. if (isDragging && Input.GetMouseButtonUp(0)) { isDragging = false; // If we're over a UI element, then bail out from this. if (EventSystem.current.IsPointerOverGameObject()) { return; } BuildOnDraggedTiles(dragParams); } }
private void BuildOnDraggedTiles(DragParameters dragParams) { for (int x = dragParams.StartX; x <= dragParams.EndX; x++) { for (int y = dragParams.StartY; y <= dragParams.EndY; y++) { Tile t = WorldController.Instance.world.GetTileAt(x, y); if (bmc.buildMode == BuildMode.FURNITURE) { // Check for furniture dragType. Furniture proto = World.current.furniturePrototypes[bmc.buildModeObjectType]; if (IsPartOfDrag(t, dragParams, proto.dragType)) { if (t != null) { // Call BuildModeController::DoBuild(). bmc.DoBuild(t); } } } else { bmc.DoBuild(t); } } } }
private void BuildOnDraggedTiles(DragParameters dragParams) { for (int x = dragParams.StartX; x <= dragParams.EndX; x++) { // Variables for the for-loop over the y-coordinates. // These are used to determine whether the loop should run from highest to lowest values or vice-versa. // The tiles are thus added in a snake or zig-zag pattern, which makes building more efficient. int begin = (x - dragParams.StartX) % 2 == 0 ? dragParams.StartY : dragParams.EndY; int stop = (x - dragParams.StartX) % 2 == 0 ? dragParams.EndY + 1 : dragParams.StartY - 1; int increment = (x - dragParams.StartX) % 2 == 0 ? 1 : -1; for (int y = begin; y != stop; y += increment) { Tile t = WorldController.Instance.World.GetTileAt(x, y, WorldController.Instance.cameraController.CurrentLayer); if (bmc.buildMode == BuildMode.FURNITURE) { // Check for furniture dragType. Furniture proto = PrototypeManager.Furniture.Get(bmc.buildModeType); if (IsPartOfDrag(t, dragParams, proto.DragType)) { if (t != null) { // Call BuildModeController::DoBuild(). bmc.DoBuild(t); } } } else { bmc.DoBuild(t); } } } }
private void ExecuteDrag(object parameter) { DragParameters dragParameters = (DragParameters)parameter; TreeViewExItem tvei = dragParameters.DragItem; dragParameters.DraggedObject = tvei.DataContext; return; }
private void BuildOnDraggedTiles(DragParameters dragParams) { for (int x = dragParams.StartX; x <= dragParams.EndX; x++) { // Variables for the for-loop over the y-coordinates. // These are used to determine whether the loop should run from highest to lowest values or vice-versa. // The tiles are thus added in a snake or zig-zag pattern, which makes building more efficient. int begin = (x - dragParams.StartX) % 2 == 0 ? dragParams.StartY : dragParams.EndY; int stop = (x - dragParams.StartX) % 2 == 0 ? dragParams.EndY + 1 : dragParams.StartY - 1; int increment = (x - dragParams.StartX) % 2 == 0 ? 1 : -1; for (int y = begin; y != stop; y += increment) { Tile tile = WorldController.Instance.World.GetTileAt(x, y, WorldController.Instance.CameraController.CurrentLayer); if (tile == null) { // Trying to build off the map, bail out of this cycle. continue; } tile = tile.GetValidGroundTile(); if (buildModeController.BuildMode == BuildMode.FURNITURE) { // Check for furniture dragType. Furniture proto = PrototypeManager.Furniture.Get(buildModeController.BuildModeType); if (IsPartOfDrag(tile, dragParams, proto.DragType)) { // Call BuildModeController::DoBuild(). buildModeController.DoBuild(tile); } } else if (buildModeController.BuildMode == BuildMode.UTILITY) { // Check for furniture dragType. Utility proto = PrototypeManager.Utility.Get(buildModeController.BuildModeType); if (IsPartOfDrag(tile, dragParams, proto.DragType)) { // Call BuildModeController::DoBuild(). buildModeController.DoBuild(tile); } } else { buildModeController.DoBuild(tile); } } } // In devmode, utilities don't build their network, and one of the utilities built needs UpdateGrid called explicitly after all are built. if (buildModeController.BuildMode == BuildMode.UTILITY && SettingsKeyHolder.DeveloperMode) { Tile firstTile = World.Current.GetTileAt(dragParams.RawStartX, dragParams.RawStartY, WorldController.Instance.CameraController.CurrentLayer); Utility utility = firstTile.Utilities[PrototypeManager.Utility.Get(buildModeController.BuildModeType).Type]; utility.UpdateGrid(utility); } }
private bool CanExecuteDrag(object parameter) { DragParameters dragParameters = (DragParameters)parameter; TreeViewExItem tvei = dragParameters.DragItem; Node node = tvei.DataContext as Node; if (node != null) { return(node.AllowDrag); } return(false); }
private void ExecuteDrag(object parameter) { DragParameters dragParameters = (DragParameters)parameter; var data = new List <Node>(); foreach (var item in dragParameters.Items) { data.Add(item.DataContext as Node); } dragParameters.Data.SetData("Nodes", data); return; }
// Checks whether a tile is valid for the drag type, given the drag parameters // Returns true if tile should be included, false otherwise private bool IsPartOfDrag(Tile tile, DragParameters dragParams, string dragType) { switch (dragType) { case "border": return(tile.X == dragParams.StartX || tile.X == dragParams.EndX || tile.Y == dragParams.StartY || tile.Y == dragParams.EndY); case "path": bool withinXBounds = dragParams.StartX <= tile.X && tile.X <= dragParams.EndX; bool onPath = tile.Y == dragParams.RawStartY || tile.X == dragParams.RawEndX; return(withinXBounds && onPath); default: return(true); } }
/// <summary> /// NOT IMPLEMENTED BY DEFAULT MOUSE HANDLER. Will throw on call. /// NOT MEANT TO BE CALLED. /// </summary> public List <GameObject> HandleDragVisual(DragParameters parameters, Transform parent) { throw new InvalidOperationException("Not supported by this class"); }
/// <summary> /// NOT IMPLEMENTED BY DEFAULT MOUSE HANDLER. Will throw on call. /// NOT MEANT TO BE CALLED. /// </summary> public void HandleDragFinished(DragParameters parameters) { throw new InvalidOperationException("Not supported by this class"); }
/// <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; }