<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Infinity Code Forum — Drag with two fingers]]></title>
		<link>https://forum.infinity-code.com/viewtopic.php?id=1310</link>
		<atom:link href="https://forum.infinity-code.com/extern.php?action=feed&amp;tid=1310&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in Drag with two fingers.]]></description>
		<lastBuildDate>Thu, 01 Aug 2019 20:49:13 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Drag with two fingers]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=5393#p5393</link>
			<description><![CDATA[<p>Something like that:<br /></p><div class="codebox"><pre><code>using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace InfinityCode.OnlineMapsExamples
{
    public class DragAndZoomInertia : MonoBehaviour
    {
        /// &lt;summary&gt;
        /// Deceleration rate (0 - 1).
        /// &lt;/summary&gt;
        public float friction = 0.9f;

        private bool isInteract;
        private List&lt;double&gt; speedX;
        private List&lt;double&gt; speedY;
        private List&lt;float&gt; speedZ;
        private double rsX;
        private double rsY;
        private float rsZ;
        private double ptx;
        private double pty;
        private float pz;
        private const int maxSamples = 5;

        private OnlineMaps map;
        private OnlineMapsControlBase control;

        private void FixedUpdate()
        {
            if (isInteract &amp;&amp; control.GetTouchCount() == 0) isInteract = false;

            // If there is interaction with the map.
            if (isInteract)
            {
                // Calculates speeds.
                double tx, ty;
                map.GetTilePosition(out tx, out ty, 20);

                double cSpeedX = tx - ptx;
                double cSpeedY = ty - pty;
                float cSpeedZ = map.floatZoom - pz;

                int halfMax = 1 &lt;&lt; 19;
                int max = 1 &lt;&lt; 20;
                if (cSpeedX &gt; halfMax) cSpeedX -= max;
                else if (cSpeedX &lt; -halfMax) cSpeedX += max;

                while (speedX.Count &gt;= maxSamples) speedX.RemoveAt(0);
                while (speedY.Count &gt;= maxSamples) speedY.RemoveAt(0);
                while (speedZ.Count &gt;= maxSamples) speedZ.RemoveAt(0);

                speedX.Add(cSpeedX);
                speedY.Add(cSpeedY);
                speedZ.Add(cSpeedZ);

                ptx = tx;
                pty = ty;
                pz = map.floatZoom;
            }
            // If no interaction with the map.
            else if (rsX * rsX + rsY * rsY &gt; 0.001 || rsZ &gt; 0.001)
            {
                // Continue to move the map with the current speed.
                double tx, ty;
                map.GetTilePosition(out tx, out ty, 20);

                tx += rsX;
                ty += rsY;

                int max = 1 &lt;&lt; 20;
                if (tx &gt;= max) tx -= max;
                else if (tx &lt; 0) tx += max;

                map.SetTilePosition(tx, ty, 20);
                //control.ZoomOnPoint(rsZ, lastScreenPoint);
                map.floatZoom += rsZ;

                // Reduces the current speed.
                rsX *= friction;
                rsY *= friction;
                rsZ *= friction;
            }
        }

        /// &lt;summary&gt;
        /// This method is called when you press on the map.
        /// &lt;/summary&gt;
        private void OnMapPress()
        {
            // Get tile coordinates of map
            map.GetTilePosition(out ptx, out pty, 20);
            pz = map.floatZoom;

            // Is marked, that is the interaction with the map.
            isInteract = true;
        }

        /// &lt;summary&gt;
        /// This method is called when you release on the map.
        /// &lt;/summary&gt;
        private void OnMapRelease()
        {
            if (control.GetTouchCount() != 0) return;

            // Is marked, that ended the interaction with the map.
            isInteract = false;

            // Calculates the average speed.
            rsX = speedX.Count &gt; 0 ? speedX.Average() : 0;
            rsY = speedY.Count &gt; 0 ? speedY.Average() : 0;
            rsZ = speedZ.Count &gt; 0 ? speedZ.Average() : 0;

            speedX.Clear();
            speedY.Clear();
            speedZ.Clear();
        }


        private void Start()
        {
            map = OnlineMaps.instance; ;
            control = OnlineMapsControlBase.instance;

            // Subscribe to map events
            control.OnMapPress += OnMapPress;
            control.OnMapRelease += OnMapRelease;

            // Initialize arrays of speed
            speedX = new List&lt;double&gt;(maxSamples);
            speedY = new List&lt;double&gt;(maxSamples);
            speedZ = new List&lt;float&gt;(maxSamples);
        }
    }
}</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Alex Vertax)]]></author>
			<pubDate>Thu, 01 Aug 2019 20:49:13 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=5393#p5393</guid>
		</item>
		<item>
			<title><![CDATA[Re: Drag with two fingers]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=5378#p5378</link>
			<description><![CDATA[<p>&quot;smooth damping&quot; - do you mean inertia?<br />Yes, of course it is possible to implement.<br />It will take a couple of days (due to the release of Unity 2019.2).</p>]]></description>
			<author><![CDATA[null@example.com (Alex Vertax)]]></author>
			<pubDate>Tue, 30 Jul 2019 17:42:35 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=5378#p5378</guid>
		</item>
		<item>
			<title><![CDATA[Re: Drag with two fingers]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=5373#p5373</link>
			<description><![CDATA[<p>Works like a charm now!<br />Thank you very much! <img src="https://forum.infinity-code.com/img/smilies/smile.png" width="15" height="15" alt="smile" /></p><p>Now for me there is just one more thing that reduces the usability of the touch controls: Google maps has a smooth damping when zooming. This makes it much easier and faster to zoom in/out very quickly (and also adds a natural feel to it). Do you think it&#039;s also possible to implement this in Online Maps? (Should I have opened a new topic for this?)</p>]]></description>
			<author><![CDATA[null@example.com (MartinTinu)]]></author>
			<pubDate>Tue, 30 Jul 2019 09:30:41 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=5373#p5373</guid>
		</item>
		<item>
			<title><![CDATA[Re: Drag with two fingers]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=5371#p5371</link>
			<description><![CDATA[<p>Updated script in post #2 to not produce a million versions of the same script.<br />lastInputPosition contains the position of a mouse or touch, and not geographical coordinates.</p>]]></description>
			<author><![CDATA[null@example.com (Alex Vertax)]]></author>
			<pubDate>Mon, 29 Jul 2019 16:46:26 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=5371#p5371</guid>
		</item>
		<item>
			<title><![CDATA[Re: Drag with two fingers]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=5370#p5370</link>
			<description><![CDATA[<p>Cool, this almost works. <img src="https://forum.infinity-code.com/img/smilies/big_smile.png" width="15" height="15" alt="big_smile" /></p><p>But it jumps when starting the zoom/drag. My guess is that it takes the last inputPosition and centers the pinch to it, which leads to the jump. Any idea how to solve this?</p><p>Thank you!</p><br /><p>EDIT: Ooops, the following code doesn&#039;t work. <br /><em>Additionally I added the following lines. Otherwise when starting not at {0,0}, it jumps to the african atlantic.</em><br /></p><div class="codebox"><pre><code>if(lastInputPosition == Vector2.zero)
            lastInputPosition = OnlineMaps.instance.position;</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (MartinTinu)]]></author>
			<pubDate>Mon, 29 Jul 2019 16:32:10 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=5370#p5370</guid>
		</item>
		<item>
			<title><![CDATA[Re: Drag with two fingers]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=5369#p5369</link>
			<description><![CDATA[<p>Hello.</p><p>Something like:<br /></p><div class="codebox"><pre><code>using System;
using System.Reflection;
using UnityEngine;

public class DragOnSmoothZoom : MonoBehaviour
{
    private Vector2 lastInputPosition;
    private Vector2 inputPosition;
    private OnlineMapsControlBase control;
    private MethodInfo updatePosition;
    private FieldInfo lastInputField;
    private bool sendWrongTouchCount;

    private int OnGetTouchCount()
    {
        if (sendWrongTouchCount) return 1;

        if (Input.touchSupported)
        {
            if (Input.touchCount &gt; 0) return Input.touchCount;
        }
        return Input.GetMouseButton(0) ? 1 : 0;
    }

    private void OnSmoothZoomBegin()
    {
        lastInputPosition = control.GetInputPosition();
        control.OnUpdateBefore += OnUpdateBefore;

        lastInputField.SetValue(control, control.GetInputPosition());
        control.UpdateLastPosition();
    }

    private void OnSmoothZoomFinish()
    {
        control.OnUpdateBefore -= OnUpdateBefore;

        lastInputField.SetValue(control, control.GetInputPosition());
        control.UpdateLastPosition();
    }

    private void OnUpdateBefore()
    {
        if (OnGetTouchCount() &lt;= 1) return;

        inputPosition = control.GetInputPosition();
        if (lastInputPosition == inputPosition) return;

        lastInputField.SetValue(control, lastInputPosition);
        sendWrongTouchCount = true;
        updatePosition.Invoke(control, null);
        sendWrongTouchCount = false;

        lastInputPosition = inputPosition;
    }

    private void Start()
    {
        control = OnlineMapsControlBase.instance;
        control.OnSmoothZoomBegin += OnSmoothZoomBegin;
        control.OnSmoothZoomFinish += OnSmoothZoomFinish;
        control.OnGetTouchCount += OnGetTouchCount;

        Type controlType = typeof(OnlineMapsControlBase);
        updatePosition = controlType.GetMethod(&quot;UpdatePosition&quot;, BindingFlags.Instance | BindingFlags.NonPublic);
        lastInputField = controlType.GetField(&quot;lastInputPosition&quot;, BindingFlags.Instance | BindingFlags.NonPublic);
    }
}</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Alex Vertax)]]></author>
			<pubDate>Mon, 29 Jul 2019 11:26:43 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=5369#p5369</guid>
		</item>
		<item>
			<title><![CDATA[Drag with two fingers]]></title>
			<link>https://forum.infinity-code.com/viewtopic.php?pid=5368#p5368</link>
			<description><![CDATA[<p>Hello there!</p><p>I want to drag the map while zooming with two fingers, like Google maps does it. Is there an option for it?<br />If not, I can&#039;t find where you handle the touch input (when not connected to Fingers or EasyTouch), to add this option manually. In which script is the touch input handled? </p><p>Best,<br />Martin</p>]]></description>
			<author><![CDATA[null@example.com (MartinTinu)]]></author>
			<pubDate>Mon, 29 Jul 2019 08:52:33 +0000</pubDate>
			<guid>https://forum.infinity-code.com/viewtopic.php?pid=5368#p5368</guid>
		</item>
	</channel>
</rss>
