Esempio n. 1
0
    void OnTouchOff(Touch touch)
    {
        int fingerId = touch.fingerId;

        if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
        {
            GameObject selectedObject = _objectSelector.DetermineSelection(touch.position);

            if (_currentSelection.ContainsKey(fingerId))
            {
                if (_currentSelection[fingerId])
                {
                    var controlOption = _currentSelection[fingerId].GetComponent <ITouchControlOptions>().TouchOption;
                    if (controlOption)
                    {
                        if (controlOption.enableDiselectOnlyOnTouchOff)
                        {
                            //_selectionResponse.IsSelected(_currentSelection[fingerId], touch.position);
                            if (_currentSelection[fingerId])
                            {
                                selectedObject = _currentSelection[fingerId];
                            }
                        }
                    }
                    else
                    {
                        if (enableDiselectOnlyOnTouchOff)
                        {
                            //_selectionResponse.IsSelected(_currentSelection[fingerId], touch.position);
                            if (_currentSelection[fingerId])
                            {
                                selectedObject = _currentSelection[fingerId];
                            }
                        }
                    }
                }
            }
            else
            {
                if (enableDiselectOnlyOnTouchOff)
                {
                    //_selectionResponse.IsSelected(_currentSelection[fingerId], touch.position);
                    selectedObject = null;
                }
            }



            if (selectedObject)
            {
                _touchControlOptions = selectedObject.GetComponent <ITouchControlOptions>();
            }
            else
            {
                if (_currentSelection.ContainsKey(fingerId))
                {
                    if (_currentSelection[fingerId])
                    {
                        _touchControlOptions = _currentSelection[fingerId].GetComponent <ITouchControlOptions>();
                    }
                }
            }

            if (!_selectedOnTouchOff.ContainsKey(fingerId))
            {
                _selectedOnTouchOff.Add(fingerId, selectedObject);
            }
            else
            {
                _selectedOnTouchOff[fingerId] = selectedObject;
            }

            //check it exist - touch off might happen before you could move - like taps
            if (_selectedOnTouchMove.ContainsKey(fingerId))
            {
                if (_selectedOnTouchMove[fingerId] != null)
                {
                    //clean was selected responses to deselected responses
                    if (_selectedOnTouchMove[fingerId].Count > 0)
                    {
                        foreach (var gameObject in _selectedOnTouchMove[fingerId])
                        {
                            //on deselect
                            if (gameObject)
                            {
                                _selectionResponse.Deselected(gameObject, touch.position);
                            }
                        }
                    }
                }
            }
            else
            {
                _selectedOnTouchMove.Add(fingerId, new List <GameObject>());
            }

            //Determine response to touch ended && touch phase cancelled
            if (_selectedOnTouchOff[fingerId])
            {
                if (_touchControlOptions == null)
                {
                    if (enableLastTouchConfirm && !enableDiselectOnlyOnTouchOff)
                    {
                        if (_selectedOnTouchMove[fingerId].Count > 0)
                        {
                            _selectionResponse.OnSelectionConfirm(_selectedOnTouchOff[fingerId], touch.position, _selectedOnTouchMove[fingerId]);
                        }
                        else
                        {
                            _selectionResponse.OnSelectionConfirm(_selectedOnTouchOff[fingerId], touch.position);
                        }
                    }
                    else
                    {
                        _selectionResponse.Deselected(_selectedOnTouchOff[fingerId], touch.position);
                    }
                }
                else
                {
                    if (_touchControlOptions.enableLastTouchConfirm && !_touchControlOptions.enableDiselectOnlyOnTouchOff)
                    {
                        if (_selectedOnTouchMove[fingerId].Count > 0)
                        {
                            _selectionResponse.OnSelectionConfirm(_selectedOnTouchOff[fingerId], touch.position, _selectedOnTouchMove[fingerId]);
                        }
                        else
                        {
                            _selectionResponse.OnSelectionConfirm(_selectedOnTouchOff[fingerId], touch.position);
                        }
                    }
                    else
                    {
                        _selectionResponse.Deselected(_selectedOnTouchOff[fingerId], touch.position);
                    }
                }
            }
            else
            {
                if (_currentSelection.ContainsKey(fingerId))
                {
                    if (_currentSelection[fingerId])
                    {
                        if (_touchControlOptions == null)
                        {
                            if (enableLastTouchConfirm && !enableDiselectOnlyOnTouchOff)
                            {
                                if (_selectedOnTouchMove[fingerId].Count > 0)
                                {
                                    _selectionResponse.OnSelectionConfirm(_currentSelection[fingerId], touch.position, _selectedOnTouchMove[fingerId]);
                                }
                                else
                                {
                                    _selectionResponse.OnSelectionConfirm(_currentSelection[fingerId], touch.position);
                                }
                            }
                            else
                            {
                                _selectionResponse.Deselected(_currentSelection[fingerId], touch.position);
                            }
                        }
                        else
                        {
                            if (_touchControlOptions.enableLastTouchConfirm && !enableDiselectOnlyOnTouchOff)
                            {
                                if (_selectedOnTouchMove[fingerId].Count > 0)
                                {
                                    _selectionResponse.OnSelectionConfirm(_currentSelection[fingerId], touch.position, _selectedOnTouchMove[fingerId]);
                                }
                                else
                                {
                                    _selectionResponse.OnSelectionConfirm(_currentSelection[fingerId], touch.position);
                                }
                            }
                            else
                            {
                                _selectionResponse.Deselected(_currentSelection[fingerId], touch.position);
                            }
                        }
                    }
                }
            }

            //determine touch end response confirmation
            if (_selectedOnTouchBegan.ContainsKey(fingerId) && _selectedOnTouchOff.ContainsKey(fingerId))
            {
                if (_selectedOnTouchBegan[fingerId] && _selectedOnTouchBegan[fingerId] == _selectedOnTouchOff[fingerId])
                {
                    if (_selectedOnTouchMove[fingerId].Count > 0)
                    {
                        _selectionResponse.OnSelectionConfirm(_selectedOnTouchOff[fingerId], touch.position, _selectedOnTouchMove[fingerId]);
                    }
                    else
                    {
                        _selectionResponse.OnSelectionConfirm(_selectedOnTouchOff[fingerId], touch.position);
                    }
                }
            }

            if (_touchControlOptions == null)
            {
                if (enableUnniqueSelection)
                {
                    if (_selectedOnTouchBegan[fingerId] && _selectedOnTouchBegan[fingerId] == _selectedOnTouchOff[fingerId] && _selectedOnTouchMove[fingerId].Count <= 1)
                    {
                        _selectionResponse.IsSelectedUnique(_selectedOnTouchBegan[fingerId], touch.position);
                    }
                }
            }
            else
            {
                if (_touchControlOptions.enableUnniqueSelection)
                {
                    if (_selectedOnTouchBegan.ContainsKey(fingerId) && _selectedOnTouchOff.ContainsKey(fingerId) && _selectedOnTouchMove.ContainsKey(fingerId))
                    {
                        if (_selectedOnTouchBegan[fingerId] && _selectedOnTouchBegan[fingerId] == _selectedOnTouchOff[fingerId] && _selectedOnTouchMove[fingerId].Count <= 1)
                        {
                            _selectionResponse.IsSelectedUnique(_selectedOnTouchBegan[fingerId], touch.position);
                        }
                    }
                }
            }

            //reset on touch off
            //clear all previously assigned touches.
            _currentSelection[fingerId]          = null;
            _selectedOnTouchBegan[fingerId]      = null;
            _selectedOnTouchStationary[fingerId] = null;
            _selectedOnTouchMove[fingerId].Clear();
            _selectedOnTouchOff[fingerId] = null;
        }
    }
Esempio n. 2
0
    void OnTouchMove(Touch touch)
    {
        int fingerId = touch.fingerId;

        if (touch.phase == TouchPhase.Moved)
        {
            GameObject selectedObject = _objectSelector.DetermineSelection(touch.position);

            ////TODO
            //if (_currentSelection.ContainsKey(fingerId))
            //{
            //    if (_currentSelection[fingerId])
            //    {
            //        var controlOption = _currentSelection[fingerId].GetComponent<ITouchControlOptions>().TouchOption;
            //        if (controlOption)
            //        {
            //            if (controlOption.enableDiselectOnlyOnTouchOff)
            //            {
            //                //_selectionResponse.IsSelected(_currentSelection[fingerId], touch.position);
            //                selectedObject = null;
            //                selectedObject = _currentSelection[fingerId];
            //            }
            //            else
            //            {
            //                selectedObject= _objectSelector.DetermineSelection(touch.position);
            //            }
            //        }
            //        else
            //        {
            //            if (enableDiselectOnlyOnTouchOff)
            //            {
            //                //_selectionResponse.IsSelected(_currentSelection[fingerId], touch.position);
            //                selectedObject = null;
            //                selectedObject = _currentSelection[fingerId];
            //            }
            //            else
            //            {
            //                selectedObject= _objectSelector.DetermineSelection(touch.position);
            //            }
            //        }
            //    }
            //    else
            //    {
            //        selectedObject = _objectSelector.DetermineSelection(touch.position);
            //    }
            //}
            //else
            //{
            //    selectedObject = _objectSelector.DetermineSelection(touch.position);
            //}

            //determine if touch control option exist
            if (selectedObject)
            {
                if (_currentSelection[fingerId])
                {
                    bool useSelectedObjectOption = true;
                    var  option = _currentSelection[fingerId].GetComponent <ITouchControlOptions>();

                    if (option != null)
                    {
                        if (option.enableDiselectOnlyOnTouchOff)
                        {
                            useSelectedObjectOption = false;
                        }
                    }
                    else
                    {
                        if (enableDiselectOnlyOnTouchOff)
                        {
                            useSelectedObjectOption = false;
                        }
                    }

                    if (useSelectedObjectOption)
                    {
                        _touchControlOptions = selectedObject.GetComponent <ITouchControlOptions>();
                    }
                    else
                    {
                        if (option != null)
                        {
                            _touchControlOptions = option;
                        }
                    }
                }
                else
                {
                    _touchControlOptions = selectedObject.GetComponent <ITouchControlOptions>();
                }
            }
            else
            {
                if (_currentSelection[fingerId])
                {
                    _touchControlOptions = _currentSelection[fingerId].GetComponent <ITouchControlOptions>();
                }
            }

            //predetermine if keys already exists
            if (!_selectedOnTouchMove.ContainsKey(fingerId))
            {
                _selectedOnTouchMove.Add(fingerId, new List <GameObject>());
            }
            if (!_currentSelection.ContainsKey(fingerId))
            {
                _currentSelection.Add(fingerId, null);
            }

            //determine response on touch move
            if (selectedObject)
            {
                if (_currentSelection[fingerId])
                {
                    if (_currentSelection[fingerId] != selectedObject)
                    {
                        if (_touchControlOptions == null)
                        {
                            if (enablePassiveSelection)
                            {
                                _selectionResponse.WasSelected(_currentSelection[fingerId], touch.position);
                            }
                            else
                            {
                                if (!enableDiselectOnlyOnTouchOff)
                                {
                                    _selectionResponse.Deselected(_currentSelection[fingerId], touch.position);
                                }
                            }
                        }
                        else
                        {
                            if (_touchControlOptions.enablePassiveSelection)
                            {
                                _selectionResponse.WasSelected(_currentSelection[fingerId], touch.position);
                            }
                            else
                            {
                                if (!_touchControlOptions.enableDiselectOnlyOnTouchOff)
                                {
                                    _selectionResponse.Deselected(_currentSelection[fingerId], touch.position);
                                }
                            }
                        }

                        //add current to touch move
                        if (_currentSelection[fingerId] && !_selectedOnTouchMove[fingerId].Contains(_currentSelection[fingerId]))
                        {
                            _selectedOnTouchMove[fingerId].Add(_currentSelection[fingerId]);
                        }

                        //assign selected as the new current object
                        if (_touchControlOptions == null)
                        {
                            if (!enableDiselectOnlyOnTouchOff)
                            {
                                _currentSelection[fingerId] = selectedObject;
                            }
                        }
                        else
                        {
                            if (!_touchControlOptions.enableDiselectOnlyOnTouchOff)
                            {
                                _currentSelection[fingerId] = selectedObject;
                            }
                        }
                    }

                    //touch response
                    _selectionResponse.IsSelected(_currentSelection[fingerId], touch.position);

                    //add selected object to current
                    if (!_selectedOnTouchMove[fingerId].Contains(selectedObject))
                    {
                        _selectedOnTouchMove[fingerId].Add(selectedObject);
                    }
                }
                else
                {
                    _selectionResponse.IsSelected(selectedObject, touch.position);
                    _currentSelection[fingerId] = selectedObject;
                }

                // add current to touch move
                if (_currentSelection[fingerId] && !_selectedOnTouchMove[fingerId].Contains(_currentSelection[fingerId]))
                {
                    _selectedOnTouchMove[fingerId].Add(_currentSelection[fingerId]);
                }
            }
            else
            {
                if (_touchControlOptions == null)
                {
                    if (enableDiselectOnlyOnTouchOff)
                    {
                        //look for most recent item selected and make it current
                        //if (!_currentSelection[fingerId])
                        //{
                        //    int selectedOnTouchMoveCount = _selectedOnTouchMove[fingerId].Count;
                        //    if (selectedOnTouchMoveCount > 0)
                        //    {
                        //        _currentSelection[fingerId] = _selectedOnTouchMove[fingerId][selectedOnTouchMoveCount - 1];
                        //    }
                        //}

                        if (enablePassiveSelection)
                        {
                            if (_currentSelection[fingerId])
                            {
                                _selectionResponse.WasSelected(_currentSelection[fingerId], touch.position);
                            }
                        }
                        else
                        {
                            if (_currentSelection[fingerId])
                            {
                                _selectionResponse.IsSelected(_currentSelection[fingerId], touch.position);
                            }
                        }
                    }
                    else
                    {
                        if (enablePassiveSelection)
                        {
                            if (_currentSelection[fingerId])
                            {
                                _selectionResponse.WasSelected(_currentSelection[fingerId], touch.position);
                            }
                        }
                        else
                        {
                            if (_currentSelection[fingerId])
                            {
                                _selectionResponse.Deselected(_currentSelection[fingerId], touch.position);
                            }
                        }

                        //Allow null current selection
                        _currentSelection[fingerId] = selectedObject;
                    }
                }
                else
                {
                    if (_touchControlOptions.enableDiselectOnlyOnTouchOff)
                    {
                        if (_touchControlOptions.enablePassiveSelection)
                        {
                            if (_currentSelection[fingerId])
                            {
                                _selectionResponse.WasSelected(_currentSelection[fingerId], touch.position);
                            }
                        }
                        else
                        {
                            if (_currentSelection[fingerId])
                            {
                                _selectionResponse.IsSelected(_currentSelection[fingerId], touch.position);
                            }
                        }
                    }
                    else
                    {
                        if (_touchControlOptions.enablePassiveSelection)
                        {
                            if (_currentSelection[fingerId])
                            {
                                _selectionResponse.WasSelected(_currentSelection[fingerId], touch.position);
                            }
                        }
                        else
                        {
                            if (_currentSelection[fingerId])
                            {
                                _selectionResponse.Deselected(_currentSelection[fingerId], touch.position);
                            }
                        }

                        //Allow null current selection
                        _currentSelection[fingerId] = selectedObject;
                    }
                }

                //add current to touch move
                if (_currentSelection[fingerId] && !_selectedOnTouchMove[fingerId].Contains(_currentSelection[fingerId]))
                {
                    _selectedOnTouchMove[fingerId].Add(_currentSelection[fingerId]);
                }
            }
        }
    }