Skip to content

Added an indipendent joystick to handle view rotation #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions Quake-iOS/GameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class GameViewController: GLKViewController, GLKViewControllerDelegate
menuPressRecognizer.addTarget(self, action: #selector(GameViewController.menuButtonAction))
menuPressRecognizer.allowedPressTypes = [NSNumber(value: UIPress.PressType.menu.rawValue)]

self.view.addGestureRecognizer(menuPressRecognizer)
self.view.addGestureRecognizer(menuPressRecognizer)

#endif

Expand Down Expand Up @@ -117,10 +117,13 @@ class GameViewController: GLKViewController, GLKViewControllerDelegate
if !joysticksInitialized {

let rect = view.frame
let size = CGSize(width: 100.0, height: 100.0)
let joystickSize = CGSize(width: 100.0, height: 100.0)
let joystick1Frame = CGRect(origin: CGPoint(x: 50.0,
y: (rect.height - size.height - 50.0)),
size: size)
y: (rect.height - joystickSize.height - 50.0)),
size: joystickSize)
let joystick2Frame = CGRect(origin: CGPoint(x: rect.width - joystickSize.width - 50.0,
y: (rect.height - joystickSize.height - 50.0)),
size: joystickSize)
let joystick1 = JoyStickView(frame: joystick1Frame)
joystick1.delegate = self

Expand All @@ -130,8 +133,20 @@ class GameViewController: GLKViewController, GLKViewControllerDelegate
joystick1.alpha = 0.5
joystick1.baseAlpha = 0.5 // let the background bleed thru the base
joystick1.handleTintColor = UIColor.darkGray // Colorize the handle
joystick1.joystickTag = .movePitch

let fireButton = UIButton(frame: CGRect(x: rect.width - 155, y: rect.height - 90, width: 75, height: 75))
let joystick2 = JoyStickView(frame: joystick2Frame)
joystick2.delegate = self

view.addSubview(joystick2)

joystick2.movable = false
joystick2.alpha = 0.5
joystick2.baseAlpha = 0.5 // let the background bleed thru the base
joystick2.handleTintColor = UIColor.darkGray // Colorize the handle
joystick2.joystickTag = .viewPitch

let fireButton = UIButton(frame: CGRect(x: rect.width - 155 - joystickSize.width, y: rect.height - 90 - joystickSize.height, width: 75, height: 75))
fireButton.setTitle("FIRE", for: .normal)
fireButton.setBackgroundImage(UIImage(named: "JoyStickBase")!, for: .normal)
fireButton.addTarget(self, action: #selector(firePressed), for: .touchDown)
Expand All @@ -140,7 +155,7 @@ class GameViewController: GLKViewController, GLKViewControllerDelegate

view.addSubview(fireButton)

let jumpButton = UIButton(frame: CGRect(x: rect.width - 90, y: rect.height - 135, width: 75, height: 75))
let jumpButton = UIButton(frame: CGRect(x: rect.width - 90 - joystickSize.width, y: rect.height - 135 - joystickSize.height, width: 75, height: 75))
jumpButton.setTitle("JUMP", for: .normal)
jumpButton.setBackgroundImage(UIImage(named: "JoyStickBase")!, for: .normal)
jumpButton.addTarget(self, action: #selector(jumpPressed), for: .touchDown)
Expand Down Expand Up @@ -271,8 +286,7 @@ class GameViewController: GLKViewController, GLKViewControllerDelegate

Sys_Init(resourcesDir, documentsDir, commandLine!, selectedGameString)

if (host_initialized != qboolean(0))
{
if (host_initialized != qboolean(0)) {
let server = UserDefaults.standard.string(forKey: "lanConfig_joinname")

if server != nil && !server!.isEmpty
Expand Down Expand Up @@ -303,8 +317,7 @@ class GameViewController: GLKViewController, GLKViewControllerDelegate
}
}

func setupEndingScreen()
{
func setupEndingScreen() {
}

@objc func firePressed(sender: UIButton!) {
Expand Down Expand Up @@ -402,12 +415,19 @@ class GameViewController: GLKViewController, GLKViewControllerDelegate

extension GameViewController: JoystickDelegate {

func handleJoyStickPosition(x: CGFloat, y: CGFloat) {
in_sidestepmove = Float(y) // misnamed but whatever
in_rollangle = Float(x)
func handleJoyStickPosition(tag: JoystickTag, x: CGFloat, y: CGFloat) {
switch tag {
case .viewPitch:
in_rollangle = Float(x)
in_pitchangle = Float(-y)
case .movePitch:
in_sidestepmove = Float(y)
in_forwardmove = Float(x)
}
// misnamed but whatever
}

func handleJoyStick(angle: CGFloat, displacement: CGFloat) {
func handleJoyStick(tag: JoystickTag, angle: CGFloat, displacement: CGFloat) {
// print("angle: \(angle) displacement: \(displacement)")
}

Expand Down
26 changes: 18 additions & 8 deletions Quake-iOS/JoyStickView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import UIKit
import CoreGraphics


protocol JoystickDelegate: class {
protocol JoystickDelegate: AnyObject {

func handleJoyStick(angle: CGFloat, displacement: CGFloat)
func handleJoyStickPosition(x: CGFloat, y: CGFloat)
func handleJoyStick(tag: JoystickTag, angle: CGFloat, displacement: CGFloat)
func handleJoyStickPosition(tag: JoystickTag, x: CGFloat, y: CGFloat)

}

Expand All @@ -20,6 +20,12 @@ protocol JoystickDelegate: class {
*/
public typealias JoyStickViewMonitor = (_ angle: CGFloat, _ displacement: CGFloat) -> ()


public enum JoystickTag: Int {
case viewPitch
case movePitch
}

/**
A simple implementation of a joystick interface like those found on classic arcade games. This implementation detects
and reports two values when the joystick moves:
Expand Down Expand Up @@ -96,6 +102,9 @@ public final class JoyStickView: UIView {
/// The original location of the joystick. Used to restore its position when user double-taps on it.
private var originalCenter: CGPoint?

/// The original location of the joystick. Used to restore its position when user double-taps on it.
public var joystickTag: JoystickTag = .movePitch

/// Tap gesture recognizer for double-taps which will reset the joystick position
private var tapGestureRecognizer: UITapGestureRecognizer!

Expand Down Expand Up @@ -206,9 +215,10 @@ public final class JoyStickView: UIView {
Reset our position.
*/
@objc public func resetFrame() {
if displacement < 0.5 && originalCenter != nil {
center = originalCenter!
originalCenter = nil
if displacement < 0.5,
let originalCenter = originalCenter {
center = originalCenter
self.originalCenter = nil
}
}

Expand Down Expand Up @@ -309,15 +319,15 @@ public final class JoyStickView: UIView {
//
self.angle = newClampedDisplacement != 0.0 ? CGFloat(180.0 - newAngleRadians * 180.0 / Float.pi) : 0.0
// monitor?(angle, displacement)
self.delegate?.handleJoyStick(angle: angle, displacement: displacement)
self.delegate?.handleJoyStick(tag: joystickTag, angle: angle, displacement: displacement)


// print("delta x: \(delta.dx) delta y: \(delta.dy)")

let new_x = (delta.dx / (radius * 2))
let new_y = (delta.dy / (radius * 2)) * -1
// print("new_x: \(new_x) new_y: \(new_y)")
self.delegate?.handleJoyStickPosition(x: new_x, y: new_y)
self.delegate?.handleJoyStickPosition(tag: joystickTag, x: new_x, y: new_y)
}
}
}
Expand Down