// Update the slider's position based on the mouse. void UpdateDrag(PointerEventData eventData, Camera cam) { RectTransform clickRect = m_HandleContainerRect; if (clickRect != null && clickRect.rect.size[0] > 0) { Vector2 localCursor; if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(clickRect, eventData.position, cam, out localCursor)) { return; } //localCursor -= clickRect.rect.position; float val = Mathf.Clamp01(Mathf.Rad2Deg * (Mathf.Atan2(localCursor.x, localCursor.y) - m_Offset + (Mathf.PI)) / 360f); val = (reverseValue ? 1f - val : val); prevNormalizedValue = normalizedValue; //Loop Around if (prevNormalizedValue > 0.8f && val < 0.2f) { wholeRotationValue += valuePerRotation; } if (prevNormalizedValue < 0.2f && val > 0.8f) { wholeRotationValue -= valuePerRotation; } //Apply Max/Min Constraint float testval = (wholeRotationValue + (val * valuePerRotation)); if (testval > maxValue) { normalizedValue = ((maxValue - wholeRotationValue) / valuePerRotation); if (!endHit) { m_EndHit.Invoke(m_Value); endHit = true; } } else if (testval < minValue) { normalizedValue = ((minValue - wholeRotationValue) / valuePerRotation); if (!endHit) { m_EndHit.Invoke(m_Value); endHit = true; } } else { endHit = false; normalizedValue = val; } //Set the main Value Set((wholeRotationValue + (normalizedValue * valuePerRotation))); //Show the world UpdateVisuals(); } }
public override void FocusUpdate(Selector cursor) { if (isSelected) { Quaternion currentRotation = transform.localRotation; float dialValue = 0; Vector3 currentPoint = transform.InverseTransformPoint(cursor.Cursor.localPosition); currentPoint.z = 0; Quaternion deltaRotation = Quaternion.FromToRotation(interactPoint, currentPoint); currentRotation *= deltaRotation; transform.localRotation = currentRotation; // get dialValue Vector3 controlNorm = transform.localRotation * Vector3.up; controlNorm.Normalize(); float angle = Vector3.Dot(controlNorm, Vector3.up); angle = Mathf.Acos(angle); angle *= Mathf.Rad2Deg; if (Vector3.Dot(controlNorm, Vector3.right) > 0f) { angle = 360f - angle; } dialValue = angle; print(angle); degrees.Invoke(dialValue); } }
protected virtual void Set(float input, bool sendCallback) { // Clamp the input float newValue = ClampValue(input); // If the stepped value doesn't match the last one, it's time to update if (m_Value == newValue) { return; } m_Value = newValue; UpdateVisuals(); if (sendCallback) { m_OnValueChanged.Invoke(newValue); } }
// The selector calls FocusUpdate every Update that a vodget has its focus. public override void FocusUpdate(Selector selector) { if (!isGrabbing) { return; } Vector3 currCursor = transform.InverseTransformPoint(selector.Cursor.localPosition); currCursor.z = 0; // degrees = radians * 180 / pi; // for later if I need it. Quaternion deltaRot = Quaternion.FromToRotation(StoredVector, currCursor); // sin() of theta over 2 transform.localRotation = transform.localRotation * deltaRot; // Vector3 tempVec3ParentSpace = transform.localRotation * gameObject.GetComponentInParent<Transform>().up; float Radians = Mathf.Acos(Vector3.Dot(ParentVector, transform.up)); float degrees = Radians * 180 / Mathf.PI; if (Vector3.Dot(this.transform.parent.transform.right, transform.up) > 0) { degrees = 360 - degrees; } float ratio = degrees / 360.0f; dial_changed.Invoke(degrees); // Convert the controllers current position into the local frame, calculate the change in rotation // about the knobs spin axis and apply the offset to transform.localRotatoin to rotate the dial. // Compute the dial "notch" value visible at the top of the knob from 0f to 360f as it rotates // away from the zero mark visible at the top of the backplate. // Invoke the dial changed event with the dial value that you created as a class variable. // Note: If you don't see your value printed in the console you need to make certain that you have // Added and properly configured an EventValuePrinter component as demonstrated with the Slider homework. }
private void Start() { spin_dir.Normalize(); notch_dir.Normalize(); dial_changed.Invoke(ComputeAngle()); }