Esempio n. 1
0
        public void CalculatorDoesBasicCalculations()
        {
            InputCalculator calculator = new InputCalculator("2 3");

            Assert.AreEqual(calculator.Calculate(), 5);

            calculator = new InputCalculator("* 2 3");
            Assert.AreEqual(calculator.Calculate(), 6);

            calculator = new InputCalculator("- 2 3");
            Assert.AreEqual(calculator.Calculate(), -5);

            calculator = new InputCalculator("/ 6 2");
            Assert.AreEqual(calculator.Calculate(), 3);
        }
Esempio n. 2
0
        public void OperatorStateChangesBasedOnInput()
        {
            InputCalculator calculator = new InputCalculator("-");

            calculator.Calculate();
            Assert.AreEqual(new Subtract().GetType(), calculator.currentState.GetType());
            calculator = new InputCalculator("+");
            calculator.Calculate();
            Assert.AreEqual(new Add().GetType(), calculator.currentState.GetType());
            calculator = new InputCalculator("*");
            calculator.Calculate();
            Assert.AreEqual(new Multiply().GetType(), calculator.currentState.GetType());
            calculator = new InputCalculator("/");
            calculator.Calculate();
            Assert.AreEqual(new Divide().GetType(), calculator.currentState.GetType());
        }
Esempio n. 3
0
    void GetMultiTouchInput()
    {
        for (int i = 0; i < Input.touchCount; i++)
        {
            if (Input.GetTouch(i).phase == TouchPhase.Began)
            {
                //if the last press time is less than allowed, reset the dictionary.
                if (Mathf.Abs(multiFinger.lastPressTime - Time.time) < multiTouchWindow)
                {
                    multiFinger.numFinger++;
                }
                else
                {
                    multiTouchDictionary.Clear();
                    fingerIDs.Clear();
                    multiFinger.numFinger     = 1;
                    multiFinger.lastPressTime = Time.time;
                }
                //Add the finger to the dictionary,
                multiTouchDictionary.Add(Input.GetTouch(i).fingerId, new Finger(Input.GetTouch(i), i));
                fingerIDs.Add(Input.GetTouch(i).fingerId);
                //Clear the action stack because a new press is detected.
                actionList.Clear();
            }

            if (Input.GetTouch(i).phase == TouchPhase.Ended || Input.GetTouch(i).phase == TouchPhase.Canceled)
            {
                if (multiTouchDictionary.TryGetValue(Input.GetTouch(i).fingerId, out tempFinger))
                {
                    //Before we remove it, calculate the current swipe value for each finger and save it onto the stack.
                    actionList.Add(CalculateSwipe(tempFinger.DeltaPosition));
                    //Debug text.
                    //Check if it exists in dictionary, and remove if so.
                    multiTouchDictionary.Remove(Input.GetTouch(i).fingerId);
                    fingerIDs.Remove(Input.GetTouch(i).fingerId);
                }

                //Compare the last tap time.
                if (Mathf.Abs(multiFinger.lastFingerLeaveTime - Time.time) < multiTouchWindow)
                {
                    multiFinger.numFingerLeft++;
                }
                else
                {
                    multiFinger.numFingerLeft       = 1;
                    multiFinger.lastFingerLeaveTime = Time.time;
                }
            }
        }

        if (multiTouchDictionary.Count == 0)
        {
            //The number of fingers presse is equal to the number of fingers left. It also has to have more than 1 finger so that it is a multi touch.
            if (multiFinger.numFinger == multiFinger.numFingerLeft && multiFinger.numFinger > 1)
            {
                //Check if the inputs are swipes.
                InputAction curInput     = actionList[0];
                bool        isSameAction = true;
                for (int i = 1; i < actionList.Count; i++)
                {
                    if (curInput != actionList[i])
                    {
                        isSameAction = false;
                    }
                }

                if (!isSameAction)                   //One of the finger had different from the rest of fingers.
                {
                    multiFinger.ResetFinger();

                    /*}else if (multiFinger.lastPressTime + dragThreshold + multiTouchWindow < Time.time) { Uncomment this if you want to have drag threshold on multi finger.
                     *      //Will need to be a drag, not a swipe.
                     *      multiFinger.ResetFinger();*/
                }
                else
                {
                    if (curInput == InputAction.Click)
                    {
                        //It is a successful tap. Now we check if the number of fingers previously is same as the currently saved.
                        if (multiFinger.numTap > 0)
                        {
                            bool isSameNumFinger = multiFinger.numFinger == multiFinger.numFingerTapped;
                            if (!isSameNumFinger)
                            {
                                multiFinger.numTap = 0;                                 //Reset the number of tap because the number of fingers tapped is now different.
                            }
                        }
                        else                           //This is the first tap with this number of fingers.
                        {
                            multiFinger.numFingerTapped = multiFinger.numFinger;
                        }
                        multiFinger.lastTapTime = Time.time;
                        multiFinger.numTap++;
                        multiFinger.ResetFinger();
                    }
                    else                       //It is a swipe.
                    {
                        currentInput = InputCalculator.Calculate(multiFinger.numFinger, curInput);
                    }
                }
            }

            if (multiFinger.lastTapTime + doubleClickThreshold < Time.time)
            {
                //Only count as tap IF the last finger pressed time is less than the drag threshold.
                if (multiFinger.lastPressTime + dragThreshold + multiTouchWindow > Time.time && multiFinger.lastPressTime != 0)
                {
                    //Calculate the tap and reset the value.
                    if (multiFinger.numTap == 1)
                    {
                        currentInput = InputCalculator.Calculate(multiFinger.numFingerTapped, InputAction.Click);
                    }
                    else if (multiFinger.numTap == 2)
                    {
                        currentInput = InputCalculator.Calculate(multiFinger.numFingerTapped, InputAction.DoubleClick);
                    }                     //We don't do beyond double tap.
                }

                //Reset the tap.
                multiFinger.Reset();
            }
        }
        else
        {
            //Check if any of the finger left the screen OR if there are more than 5 fingers. We don't calculate beyond 5 fingers.
            if (multiFinger.numFingerLeft > 0 || multiFinger.numFinger > 5)
            {
                currentInput = InputAction.Null;
            }

            //Check if the finger has been pressed long enough to count as a drag.
            if (multiFinger.lastPressTime + multiTouchWindow < Time.time && multiFinger.lastPressTime != 0)
            {
                //Calculate the drag.
                multiTouchDictionary[fingerIDs[0]].action = CalculateDrag(multiTouchDictionary[fingerIDs[0]].DeltaPosition);
                bool isSameAction = true;
                for (int i = 1; i < multiTouchDictionary.Count; i++)
                {
                    multiTouchDictionary[fingerIDs[i]].action = CalculateDrag(multiTouchDictionary[fingerIDs[i]].DeltaPosition);
                    if (multiTouchDictionary[fingerIDs[0]].action != multiTouchDictionary[fingerIDs[i]].action)
                    {
                        isSameAction = false;
                    }
                }
                if (isSameAction)
                {
                    //It becomes a drag.
                    currentInput = InputCalculator.Calculate(Input.touchCount, multiTouchDictionary[fingerIDs[0]].action);
                }
                else
                {
                    //The input isnt the same. Now we check if the input is pinch in/out.
                    if (multiFinger.numFinger == 2)
                    {
                        currentInput = CalculatePinch();
                    }
                }
            }
        }
    }