IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
ModelZoomer.cs
1 // Model Zoomer
2 //
3 
4 using System;
5 using UnityEngine;
6 
7 public class ModelZoomer : MonoBehaviour
8 {
9 
10 
11  public float zoomingSpeed = 1;
12  public float maxZoom = 2f;
13  public float minZoom = 0.2f;
14 
15  private Vector3 targetZoom;
16  private Vector3 zoomVelocity;
17 
18  public float autoZoomSpeed = 0.5f;
19  private float scaleTime = 0.3f;
20 
21  private bool zooming = false;
22  private float originalDist = 0;
23  private Vector3 mOriginalZoom;
24 
25  private void Start()
26  {
27  targetZoom = transform.localScale;
28  }
29 
30 
31  private void Update()
32  {
33  if (UI.Core.instance.layoutSystem.activeScreen == UI.Screen.center) {
34 
35  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
36  if (inputDevice.getDeviceType () == InputDeviceManager.InputDeviceType.Mouse) {
37  // Let mouse handle zooming:
38  if (!UI.Core.instance.pointerIsOverPlatformUIObject) {
39  if (Input.GetAxis ("Mouse ScrollWheel") != 0) {
40 
41  float inputScroll = Input.GetAxis ("Mouse ScrollWheel");
42 
43  float zoom = transform.localScale.x + inputScroll / (1 / zoomingSpeed);
44 
45  zoom = Mathf.Clamp (zoom, minZoom, maxZoom);
46 
47  transform.localScale = new Vector3 (zoom, zoom, zoom);
48  targetZoom = transform.localScale;
49  }
50  }
51  } else if (inputDevice.getDeviceType () == InputDeviceManager.InputDeviceType.ViveController) {
52 
53  // Let left Vive controller handle zooming:
54  LeftController lc = InputDeviceManager.instance.leftController;
55  if (lc != null) {
56  UnityEngine.EventSystems.PointerEventData.FramePressState triggerState = lc.triggerButtonState;
57  if (triggerState == UnityEngine.EventSystems.PointerEventData.FramePressState.Pressed && zooming == false) {
58  zooming = true;
59  originalDist = (lc.transform.position - transform.position).magnitude;
60  mOriginalZoom = transform.localScale;
61  } else if (triggerState == UnityEngine.EventSystems.PointerEventData.FramePressState.Released && zooming == true) {
62  zooming = false;
63  }
64 
65  if (zooming) {
66 
67  float dist = (lc.transform.position - transform.position).magnitude;
68 
69  float distDiff = dist - originalDist;
70 
71  Vector3 newScale = mOriginalZoom + mOriginalZoom * distDiff;
72 
73  setTargetZoom (newScale);
74  }
75  }
76 
77  }
78  }
79 
80  // Auto-Zoom to target, if given:
81  transform.localScale = Vector3.SmoothDamp(transform.localScale, targetZoom, ref zoomVelocity, scaleTime);
82  }
83 
84  public void setTargetZoom( Vector3 zoom, float timeForScaling = 0f )
85  {
86  targetZoom = zoom;
87  zoomVelocity = new Vector3 (0, 0, 0);
88  if (timeForScaling == 0) {
89  scaleTime = 1f;
90  transform.localScale = targetZoom;
91  } else {
92  scaleTime = timeForScaling;
93  }
94  }
95 }
96 
97