Skip to content

Commit d88e71a

Browse files
committed
Calculate swipe by percentage of the page space.
1 parent cfe746d commit d88e71a

File tree

2 files changed

+10
-53
lines changed

2 files changed

+10
-53
lines changed

Assets/JCSUnity/Scripts/GUI/JCS_SlideScreenPanelHolder.cs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -90,48 +90,6 @@ public void DeltaMove(Vector3 deltaPos)
9090
sp.transform.localPosition += deltaPos;
9191
}
9292

93-
/// <summary>
94-
/// Difference between target position and current position.
95-
/// </summary>
96-
/// <returns></returns>
97-
public Vector3 PositionDiff()
98-
{
99-
// SOURCE: https://docs.unity3d.com/2018.1/Documentation/ScriptReference/UI.GraphicRaycaster.Raycast.html
100-
PointerEventData pointerEventData = null;
101-
Canvas canvas = JCS_Canvas.instance.GetCanvas();
102-
GraphicRaycaster raycaster = canvas.GetComponent<GraphicRaycaster>();
103-
104-
// Set up the new Pointer Event
105-
pointerEventData = new PointerEventData(EventSystem.current);
106-
// Set the Pointer Event Position to that of the mouse position
107-
pointerEventData.position = Input.mousePosition;
108-
109-
// Create a list of Raycast Results
110-
List<RaycastResult> results = new List<RaycastResult>();
111-
112-
// Raycast using the Graphics Raycaster and mouse click position
113-
raycaster.Raycast(pointerEventData, results);
114-
115-
JCS_SlidePanel sp = null;
116-
117-
// For every result returned, output the name of the GameObject on the Canvas hit by the Ray
118-
foreach (RaycastResult result in results)
119-
{
120-
sp = result.gameObject.GetComponent<JCS_SlidePanel>();
121-
if (sp != null)
122-
break;
123-
}
124-
125-
if (sp != null)
126-
{
127-
Vector3 curPos = sp.transform.position;
128-
Vector3 targPos = sp.GetTargetPosition();
129-
return JCS_Mathf.AbsoluteValue(curPos - targPos);
130-
}
131-
132-
return Vector3.zero;
133-
}
134-
13593
/// <summary>
13694
/// Add Force to the panel.
13795
/// </summary>

Assets/JCSUnity/Scripts/GameObject/2D/2DCamera/JCS_2DSlideScreenCamera.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,9 @@ public class JCS_2DSlideScreenCamera
6464

6565
[Header("## Mobile")]
6666

67-
[Tooltip("Distance to slide over next scene on x axis.")]
68-
[Range(0.0f, 5000.0f)]
67+
[Tooltip("Area space to swipe for previous/next page.")]
6968
[SerializeField]
70-
private float mSwipeDistanceX = 5.0f;
71-
72-
[Tooltip("Distance to slide over next scene on y axis.")]
73-
[Range(0.0f, 5000)]
74-
[SerializeField]
75-
private float mSwipeDistanceY = 5.0f;
69+
private Vector2 mSwipeArea = new Vector2(0.5f, 0.5f);
7670

7771
[Tooltip("Freeze the x axis sliding action.")]
7872
[SerializeField]
@@ -115,6 +109,7 @@ public class JCS_2DSlideScreenCamera
115109
public JCS_SlideScreenPanelHolder PanelHolder { get { return this.mPanelHolder; } set { this.mPanelHolder = value; } }
116110
public void SetJCS2DCamera(JCS_2DCamera cam) { this.mJCS_2DCamera = cam; }
117111
public JCS_UnityGUIType UnityGUIType { get { return this.mUnityGUIType; } set { this.mUnityGUIType = value; } }
112+
public Vector2 SwipeArea { get { return this.mSwipeArea; } set { this.mSwipeArea = value; } }
118113
public bool FreezeX { get { return this.mFreezeX; } set { this.mFreezeX = value; } }
119114
public bool FreezeY { get { return this.mFreezeY; } set { this.mFreezeY = value; } }
120115
public AudioClip SwitchSceneSound { get { return this.mSwitchSceneSound; } set { this.mSwitchSceneSound = value; } }
@@ -351,17 +346,21 @@ private void DoMobileSwipe()
351346

352347
if (JCS_Input.GetMouseButtonUp(JCS_MouseButton.LEFT))
353348
{
354-
Vector3 posDiff = mPanelHolder.PositionDiff();
349+
Vector3 posDiff = si.DragDistance;
350+
JCS_ScreenSizef vs = JCS_ScreenSettings.instance.VisibleScreenSize();
351+
JCS_ScreenSizef target_vs = new JCS_ScreenSizef(vs.width * mSwipeArea.x, vs.height * mSwipeArea.y);
355352

356-
if (!mFreezeX && posDiff.x > this.mSwipeDistanceX)
353+
if (!mFreezeX && posDiff.x > target_vs.width)
357354
{
355+
print(posDiff);
356+
358357
if (JCS_Mathf.IsPositive(si.DragDisplacement.x))
359358
SwitchScene(JCS_2D4Direction.LEFT);
360359
else
361360
SwitchScene(JCS_2D4Direction.RIGHT);
362361
}
363362

364-
if (!mFreezeY && posDiff.y > this.mSwipeDistanceY)
363+
if (!mFreezeY && posDiff.y > target_vs.height)
365364
{
366365
if (JCS_Mathf.IsPositive(si.DragDisplacement.y))
367366
SwitchScene(JCS_2D4Direction.BOTTOM);

0 commit comments

Comments
 (0)