Skip to content
Merged
57 changes: 40 additions & 17 deletions PlayTools/Controls/MenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class RotateViewController: UIViewController {
.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 @@ extension UIApplication {
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 UIApplication {

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 @@ var keymapping = [
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 @@ -233,32 +235,52 @@ class MenuController {
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
"]" // Next keymap
]
let arrowKeyChildrenCommands = zip(zip(keyCommands, keymapping), iconsSelctor).map { (arg0, image) in
let (command, btn) = arg0
return UIKeyCommand(title: btn,
image: image,
return UIKeyCommand(title: btn, image: image,
action: keymappingSelectors[keymapping.firstIndex(of: btn)!],
input: command,
modifierFlags: .command,
propertyList: [CommandsList.KeymappingToolbox: btn]
)
}

let arrowKeysGroup = UIMenu(title: "",
image: nil,
identifier: .keymappingOptionsMenu,
options: .displayInline,
children: arrowKeyChildrenCommands)
// Every rotation item calls the *same selector*, passing its index in propertyList
let rotationMenuItems = [
"Landscape Left (0°)", "Portrait (90°)", "Landscape Right (180°)", "Portrait Upside Down (270°)"
].enumerated().map { (index, title) in
UIKeyCommand(
title: title, image: nil,
action: #selector(UIApplication.rotateView(_:)),
input: "\(index + 1)",
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: ""),
image: nil,
value: "Keymapping", comment: ""), image: nil,
identifier: .keymappingMenu,
options: [],
children: [arrowKeysGroup])
Expand All @@ -270,4 +292,5 @@ extension UIMenu.Identifier {
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 @@ -22,6 +22,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 @@ -88,6 +88,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 @@ -108,6 +110,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
Loading