Skip to content
Merged
60 changes: 47 additions & 13 deletions PlayTools/Controls/MenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
.landscapeLeft, .portrait, .landscapeRight, .portraitUpsideDown]
static var orientationTraverser = 0

static func rotate() {
orientationTraverser += 1
static func rotate(deviceOrientation: Int) {
orientationTraverser = deviceOrientation
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
Expand Down Expand Up @@ -58,7 +58,10 @@
guard let windowScene = scene as? UIWindowScene else { continue }
for window in windowScene.windows {
guard let rootViewController = window.rootViewController else { continue }
rootViewController.rotateView(sender)
if let dict = sender.propertyList as? [String: Any],
let index = dict["rotationIndex"] as? Int {
rootViewController.rotateView(sender, deviceOrientation: index)
}
}
}

Expand Down Expand Up @@ -114,8 +117,9 @@

extension UIViewController {
@objc
func rotateView(_ sender: AnyObject) {
RotateViewController.rotate()
func rotateView(_ sender: AnyObject, deviceOrientation: Int) {
RotateViewController.rotate(deviceOrientation: deviceOrientation)
RotateViewController.orientationTraverser %= RotateViewController.orientationList.count
let viewController = RotateViewController(nibName: nil, bundle: nil)
self.present(viewController, animated: true)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
Expand All @@ -137,8 +141,6 @@
value: "Upsize selected element", comment: ""),
NSLocalizedString("menu.keymapping.downsizeElement", tableName: "Playtools",
value: "Downsize selected element", comment: ""),
NSLocalizedString("menu.keymapping.rotateDisplay", tableName: "Playtools",
value: "Rotate display area", comment: ""),
NSLocalizedString("menu.keymapping.toggleDebug", tableName: "Playtools",
value: "Toggle Debug Overlay", comment: ""),
NSLocalizedString("menu.keymapping.hide.pointer", tableName: "Playtools",
Expand Down Expand Up @@ -216,13 +218,12 @@
children: arrowKeyChildrenCommands)])
}

class func keymappingMenu() -> UIMenu {

Check failure on line 221 in PlayTools/Controls/MenuController.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Function Body Length Violation: Function body should span 50 lines or less excluding comments and whitespace: currently spans 57 lines (function_body_length)
let keyCommands = [
"K", // Toggle keymap editor
UIKeyCommand.inputDelete, // Remove keymap element
UIKeyCommand.inputUpArrow, // Increase keymap element size
UIKeyCommand.inputDownArrow, // Decrease keymap element size
"R", // Rotate display
"D", // Toggle debug overlay
".", // Hide cursor until move
"[", // Previous keymap
Expand All @@ -238,11 +239,43 @@
)
}

let arrowKeysGroup = UIMenu(title: "",
image: nil,
identifier: .keymappingOptionsMenu,
options: .displayInline,
children: arrowKeyChildrenCommands)
let rotationTitles = [
Comment thread
TheMoonThatRises marked this conversation as resolved.
Outdated
"Landscape Left (0°)",
"Portrait (90°)",
"Landscape Right (180°)",
"Portrait Upside Down (270°)"
]
let rotationInputs = ["1", "2", "3", "4"]

// Every rotation item calls the *same selector*, passing its index in propertyList
let rotationMenuItems = rotationTitles.enumerated().map { (index, title) in
UIKeyCommand(
title: title,
image: nil,
action: #selector(UIApplication.rotateView(_:)),
input: rotationInputs[index],
modifierFlags: [.command, .shift], // ⌘⇧1–4
propertyList: ["rotationIndex": index]
)
}

let rotationMenu = UIMenu(
title: NSLocalizedString("menu.keymapping.rotateDisplay", tableName: "Playtools",
value: "Rotate display area", comment: ""),
image: nil,
identifier: .rotationOptionsMenu,
options: [],
children: rotationMenuItems
)

// Combine all menus
let arrowKeysGroup = UIMenu(
title: "",
image: nil,
identifier: .keymappingOptionsMenu,
options: .displayInline,
children: arrowKeyChildrenCommands + [rotationMenu]
)

return UIMenu(title: NSLocalizedString("menu.keymapping", tableName: "Playtools",
value: "Keymapping", comment: ""),
Expand All @@ -258,4 +291,5 @@
static var keymappingOptionsMenu: UIMenu.Identifier { UIMenu.Identifier("io.playcover.PlayTools.menus.keymapping") }
static var debuggingMenu: UIMenu.Identifier { UIMenu.Identifier("io.playcover.PlayTools.menus.debug") }
static var debuggingOptionsMenu: UIMenu.Identifier { UIMenu.Identifier("io.playcover.PlayTools.menus.debugging") }
static var rotationOptionsMenu: UIMenu.Identifier { UIMenu.Identifier("io.playcover.PlayTools.menus.rotation") }
}
19 changes: 19 additions & 0 deletions PlayTools/PlayCover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ public class PlayCover: NSObject {
// Change the working directory to / just like iOS
FileManager.default.changeCurrentDirectoryPath("/")
}

if PlaySettings.shared.displayRotation != 0 {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5, execute: {
let rotateCommand = UIKeyCommand(
title: "Keep Rotation Command",
image: nil,
action: #selector(UIApplication.rotateView(_:)),
input: "",
modifierFlags: [],
propertyList: ["rotationIndex": PlaySettings.shared.displayRotation]
)
UIApplication.shared.sendAction(
#selector(UIApplication.rotateView(_:)),
to: UIApplication.shared,
from: rotateCommand,
for: nil
)
})
}
}

@objc static public func initMenu(menu: NSObject) {
Expand Down
3 changes: 3 additions & 0 deletions PlayTools/PlaySettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ let settings = PlaySettings.shared

@objc lazy var floatingWindow = settingsData.floatingWindow

@objc lazy var displayRotation = settingsData.displayRotation

@objc lazy var checkMicPermissionSync = settingsData.checkMicPermissionSync

@objc lazy var limitMotionUpdateFrequency = settingsData.limitMotionUpdateFrequency
Expand All @@ -104,6 +106,7 @@ struct AppSettingsData: Codable {
var customScaler = 2.0
var resolution = 2
var aspectRatio = 1
var displayRotation = 0
var notch = false
var bypass = false
var discordActivity = DiscordActivity()
Expand Down