protected override void Execute(List <InputEntity> entities) { InputEntity dragSelectionDataEntity = _context.dragSelectionDataEntity; foreach (InputEntity entity in entities) { if (entity.isLeftMouseButtonDown) { dragSelectionDataEntity.dragSelectionData.mouseDownScreenPoint = entity.screenPoint.value; } else if (entity.isLeftMouseButtonHeld) { dragSelectionDataEntity.dragSelectionData.mouseHeldScreenPoint = entity.screenPoint.value; } else if (entity.isLeftMouseButtonUp) { dragSelectionDataEntity.dragSelectionData.mouseUpScreenPoint = entity.screenPoint.value; } } dragSelectionDataEntity.ReplaceComponent(InputComponentsLookup.DragSelectionData, dragSelectionDataEntity.dragSelectionData); DragSelectionDataComponent dragSelectionDataComponent = dragSelectionDataEntity.dragSelectionData; if ( dragSelectionDataComponent.mouseUpScreenPoint == Vector2.zero || ((dragSelectionDataComponent.mouseHeldScreenPoint - dragSelectionDataComponent.mouseDownScreenPoint).magnitude < _gameConfig.inputConfig.dragSelectionDeadZone) ) { return; } InputEntity addToSelectionKeyEvent = _keyEventGroup.GetEntities().SingleOrDefault(e => e.keyEvent.value.keyCode == KeyCode.LeftShift); bool isAddToSelectionKeyHeld = addToSelectionKeyEvent != null && addToSelectionKeyEvent.isKeyHeld; if (!isAddToSelectionKeyHeld) { foreach (var gameEntity in _selectedEntitiesGroup.GetEntities()) { gameEntity.isSelected = false; } } Vector2 mouseDownViewport = Camera.main.ScreenToViewportPoint(dragSelectionDataComponent.mouseDownScreenPoint); Vector2 mouseUpViewport = Camera.main.ScreenToViewportPoint(dragSelectionDataComponent.mouseUpScreenPoint); Rect selectionRect = new Rect(mouseDownViewport.x, mouseDownViewport.y, mouseUpViewport.x - mouseDownViewport.x, mouseUpViewport.y - mouseDownViewport.y); GameEntity[] selectableEntities = _contexts.game.GetGroup(GameMatcher.Selectable).GetEntities(); Camera mainCamera = Camera.main; foreach (var selectableEntity in selectableEntities) { GameObject selectableGo = selectableEntity.view.gameObject; if (selectionRect.Contains(mainCamera.WorldToViewportPoint(selectableGo.transform.position), true)) { selectableEntity.isSelected = true; } } }
public void Cleanup() { DragSelectionDataComponent dragSelectionDataComponent = _context.dragSelectionDataEntity.dragSelectionData; if (dragSelectionDataComponent.mouseUpScreenPoint != Vector2.zero) { _context.ReplaceDragSelectionData(Vector2.zero, Vector2.zero, Vector2.zero); } }
protected override void Execute(List <InputEntity> entities) { DragSelectionDataComponent dragSelectionDataComponent = _context.dragSelectionDataEntity.dragSelectionData; UiEntity dragSelectionImageEntity = _contexts.ui.dragSelectionImageUiEntity; RectTransform dragSelectionImageTransform = (RectTransform)dragSelectionImageEntity.view.gameObject.transform; Vector2 selectionCenter = (dragSelectionDataComponent.mouseDownScreenPoint + dragSelectionDataComponent.mouseHeldScreenPoint) / 2f; float selectionWidth = Mathf.Abs(dragSelectionDataComponent.mouseHeldScreenPoint.x - dragSelectionDataComponent.mouseDownScreenPoint.x); float selectionHeight = Mathf.Abs(dragSelectionDataComponent.mouseHeldScreenPoint.y - dragSelectionDataComponent.mouseDownScreenPoint.y); dragSelectionImageTransform.gameObject.SetActive(true); dragSelectionImageTransform.position = selectionCenter; dragSelectionImageTransform.sizeDelta = new Vector2(selectionWidth, selectionHeight); if (dragSelectionDataComponent.mouseUpScreenPoint != Vector2.zero) { dragSelectionImageTransform.gameObject.SetActive(false); } }