//! \cond // ---------------- public void ApplyUnclampedVec(Vector2 v) //, JoystickConfig.ClampMode sourceClampMode) { if (this.config.clampMode == JoystickConfig.ClampMode.Circle) { v = CFUtils.ClampInsideUnitCircle(v); } else { v = CFUtils.ClampInsideUnitSquare(v); } this.nextFramePos.x = CFUtils.ApplyDeltaInput(this.nextFramePos.x, v.x); this.nextFramePos.y = CFUtils.ApplyDeltaInput(this.nextFramePos.y, v.y); }
// ------------------- public void ApplyClampedVec(Vector2 v, JoystickConfig.ClampMode clampMode) { if (clampMode != this.config.clampMode) { if (this.config.clampMode == JoystickConfig.ClampMode.Circle) { v = CFUtils.SquareToCircularJoystickVec(v); } else { v = CFUtils.CircularToSquareJoystickVec(v); } } this.nextFramePos.x = CFUtils.ApplyDeltaInput(this.nextFramePos.x, v.x); this.nextFramePos.y = CFUtils.ApplyDeltaInput(this.nextFramePos.y, v.y); }
// ------------------ public void Update() { // Combine digital and analog input... Dir digiInputDir = CFUtils.DigitalToDir(this.nextFrameDigiU, this.nextFrameDigiR, this.nextFrameDigiD, this.nextFrameDigiL); this.digiInputAnalogVecCur = this.config.AnimateDirToAnalogVec(this.digiInputAnalogVecCur, digiInputDir); this.posRaw.x = CFUtils.ApplyDeltaInput(this.nextFramePos.x, this.digiInputAnalogVecCur.x); this.posRaw.y = CFUtils.ApplyDeltaInput(this.nextFramePos.y, this.digiInputAnalogVecCur.y); this.nextFramePos = Vector2.zero; this.nextFrameDigiU = false; this.nextFrameDigiR = false; this.nextFrameDigiD = false; this.nextFrameDigiL = false; // Process input... if (this.config.blockX) { this.posRaw.x = 0; } if (this.config.blockY) { this.posRaw.y = 0; } Vector2 unclampedPos = this.posRaw; this.posRaw = this.config.ClampNormalizedPos(this.posRaw); float tiltRaw = this.posRaw.magnitude; float angleRaw = this.safeAngle; if (this.posRaw.sqrMagnitude < MIN_SAFE_TILT_SQ) { tiltRaw = 0; this.tilt = 0; } else { this.normDir = this.posRaw.normalized; this.tilt = tiltRaw; angleRaw = Mathf.Atan2(this.normDir.x, this.normDir.y) * Mathf.Rad2Deg; angleRaw = CFUtils.NormalizeAnglePositive(angleRaw); } // Get digital state... Dir curDir4 = this.GetDir4(), curDir8 = this.GetDir8(); float dist4 = tiltRaw, dist8 = tiltRaw; // Use different tilt calculation mode... if (this.config.digitalDetectionMode == JoystickConfig.DigitalDetectionMode.Touch) { dist4 = Mathf.Max(Mathf.Abs(unclampedPos.x), Mathf.Abs(unclampedPos.y)); dist8 = dist4; dist8 = Mathf.Max(dist8, Mathf.Abs(((CFUtils.OneOverSqrtOf2 * unclampedPos.x) + (CFUtils.OneOverSqrtOf2 * unclampedPos.y)))); dist8 = Mathf.Max(dist8, Mathf.Abs(((CFUtils.OneOverSqrtOf2 * unclampedPos.x) - (CFUtils.OneOverSqrtOf2 * unclampedPos.y)))); } if (tiltRaw < 0.001f) { this.dirLastNonNeutral4 = this.dirLastNonNeutral8 = Dir.N; } curDir4 = (dist4 > this.config.digitalEnterThresh) ? CFUtils.DirFromAngleEx(angleRaw, false, this.dirLastNonNeutral4, this.config.angularMagnet) : (dist4 > this.config.digitalLeaveThresh) ? curDir4 : Dir.N; curDir8 = (dist8 > this.config.digitalEnterThresh) ? CFUtils.DirFromAngleEx(angleRaw, true, this.dirLastNonNeutral8, this.config.angularMagnet) : (dist8 > this.config.digitalLeaveThresh) ? curDir8 : Dir.N; if (curDir4 != Dir.N) { this.dirLastNonNeutral4 = curDir4; } if (curDir8 != Dir.N) { this.dirLastNonNeutral8 = curDir8; } this.dirState.BeginFrame(); this.dirState4.BeginFrame(); this.dirState8.BeginFrame(); this.dirState4.SetDir(curDir4, this.config.originalDirResetMode); this.dirState8.SetDir(curDir8, this.config.originalDirResetMode); this.dirState.SetDir((this.config.stickMode == JoystickConfig.StickMode.Digital4) ? this.GetDir4() : this.GetDir8(), this.config.originalDirResetMode); switch (this.config.stickMode) { case JoystickConfig.StickMode.Analog: this.angle = angleRaw; if (this.config.perAxisDeadzones) { this.pos.x = this.config.GetAnalogVal(this.posRaw.x); this.pos.y = this.config.GetAnalogVal(this.posRaw.y); this.tilt = this.pos.magnitude; } else { this.tilt = this.config.GetAnalogVal(tiltRaw); this.pos = this.normDir * this.tilt; if (this.config.clampMode == JoystickConfig.ClampMode.Square) { this.pos = CFUtils.CircularToSquareJoystickVec(this.pos); } } break; case JoystickConfig.StickMode.Digital4: case JoystickConfig.StickMode.Digital8: this.pos = CFUtils.DirToVector(this.GetDir(), (this.config.clampMode == JoystickConfig.ClampMode.Circle)); this.angle = CFUtils.DirToAngle(this.GetDir()); this.tilt = (this.GetDir() == Dir.N) ? 0 : 1; if (this.GetDir() != Dir.N) { this.normDir = this.pos.normalized; } break; } this.safeAngle = this.angle; }