Skip to content

Commit

Permalink
Merge pull request #27 from XuYicong/fix/editor-not-displayed
Browse files Browse the repository at this point in the history
Fix: editor UI not shown in some games
  • Loading branch information
Depal1 authored Sep 3, 2022
2 parents d0aad4c + 461efc0 commit fad9beb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 34 deletions.
10 changes: 5 additions & 5 deletions PlayTools/Controls/MenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import UIKit

extension UIViewController {
extension UIWindow {
@objc
func switchEditorMode(_ sender: AnyObject) {
EditorController.shared.switchMode()
Expand Down Expand Up @@ -37,10 +37,10 @@ var keymapping = ["Open/Close Keymapping Editor",
"Delete selected element",
"Upsize selected element",
"Downsize selected element"]
var keymappingSelectors = [#selector(UIViewController.switchEditorMode(_:)),
#selector(UIViewController.removeElement(_:)),
#selector(UIViewController.upscaleElement(_:)),
#selector(UIViewController.downscaleElement(_:))]
var keymappingSelectors = [#selector(UIWindow.switchEditorMode(_:)),
#selector(UIWindow.removeElement(_:)),
#selector(UIWindow.upscaleElement(_:)),
#selector(UIWindow.downscaleElement(_:))]

class MenuController {
init(with builder: UIMenuBuilder) {
Expand Down
6 changes: 1 addition & 5 deletions PlayTools/Controls/Toucher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ class Toucher {
static func touchcam(point: CGPoint, phase: UITouch.Phase, tid: Int) {
touchQueue.async {
if keyWindow == nil {
keyWindow = UIApplication
.shared
.connectedScenes
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
.first { $0.isKeyWindow }
keyWindow = screen.keyWindow
}
PTFakeMetaTouch.fakeTouchId(tid, at: point, with: phase, in: keyWindow)
}
Expand Down
2 changes: 1 addition & 1 deletion PlayTools/Keymap/DragElementsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class KeymapHolder: CircleMenuDelegate {
menu?.center = location
menu?.backgroundColor = UIColor.gray
menu?.layer.cornerRadius = menu!.frame.size.width / 2
screen.window?.addSubview(menu!)
screen.keyWindow?.addSubview(menu!)
menu?.backgroundColor = UIColor.black
menu?.onTap()
}
Expand Down
41 changes: 26 additions & 15 deletions PlayTools/Keymap/EditorController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import SwiftUI

let editor = EditorController.shared

class EditorViewController: UIViewController {
override func loadView() {
view = EditorView()
}
}

final class EditorController: NSObject {

static let shared = EditorController()
Expand All @@ -11,8 +17,17 @@ final class EditorController: NSObject {

var focusedControl: ControlModel?

lazy var editorWindow: UIWindow = initWindow()
var previousWindow: UIWindow?
var controls: [ControlModel] = []
lazy var view: EditorView = EditorView()
lazy var viewController = EditorViewController(nibName: nil, bundle: nil)
lazy var view: EditorView! = viewController.view as? EditorView

private func initWindow() -> UIWindow {
let window = UIWindow(windowScene: screen.windowScene!)
window.rootViewController = viewController
return window
}

private func addControlToView(control: ControlModel) {
controls.append(control)
Expand All @@ -35,32 +50,31 @@ final class EditorController: NSObject {

public func switchMode() {
lock.lock()
if EditorController.shared.editorMode {
Toast.showOver(msg: "Keymapping saved")
} else {
Toast.showOver(msg: "Click to start keymmaping edit")
}

if editorMode {
KeymapHolder.shared.hide()
saveButtons()
view.removeFromSuperview()
editorMode = false
// editorWindow.windowScene = nil
previousWindow?.makeKeyAndVisible()
mode.show(false)
focusedControl = nil
Toast.showOver(msg: "Keymapping saved")
} else {
mode.show(true)
previousWindow = screen.keyWindow
// editorWindow.windowScene = screen.windowScene
editorMode = true
editorWindow.makeKeyAndVisible()
showButtons()
screen.window?.addSubview(view)
view.becomeFirstResponder()
Toast.showOver(msg: "Click to start keymmaping edit")
}
lock.unlock()
}

var editorMode: Bool {
get { view.isUserInteractionEnabled }
set { view.isUserInteractionEnabled = newValue}
get { !editorWindow.isHidden }
set { editorWindow.isHidden = !newValue}
}

public func setKeyCode(_ key: Int) {
Expand Down Expand Up @@ -174,7 +188,6 @@ extension UIResponder {

class EditorView: UIView {
override var preferredFocusEnvironments: [UIFocusEnvironment] {
if !isUserInteractionEnabled { return [self] }
if let btn = editor.focusedControl?.button {
return [btn]
}
Expand All @@ -192,7 +205,7 @@ class EditorView: UIView {
init() {
super.init(frame: .zero)
self.frame = screen.screenRect
self.isUserInteractionEnabled = false
self.isUserInteractionEnabled = true
let single = UITapGestureRecognizer(target: self, action: #selector(self.doubleClick(sender:)))
single.numberOfTapsRequired = 1
self.addGestureRecognizer(single)
Expand All @@ -208,7 +221,6 @@ class EditorView: UIView {
var label: UILabel?

@objc func pressed(sender: UIButton!) {
if !isUserInteractionEnabled { return }
if let button = sender as? Element {
if editor.focusedControl?.button == nil || editor.focusedControl?.button != button {
editor.updateFocus(button: sender)
Expand All @@ -217,7 +229,6 @@ class EditorView: UIView {
}

@objc func dragged(_ sender: UIPanGestureRecognizer) {
if !isUserInteractionEnabled { return }
if let ele = sender.view as? Element {
if editor.focusedControl?.button == nil || editor.focusedControl?.button != ele {
editor.updateFocus(button: ele)
Expand Down
12 changes: 12 additions & 0 deletions PlayTools/PlayScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ public final class PlayScreen: NSObject {
max / 100.0
}

var keyWindow: UIWindow? {
return UIApplication
.shared
.connectedScenes
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
.first { $0.isKeyWindow }
}

var windowScene: UIWindowScene? {
window?.windowScene
}

var window: UIWindow? {
return UIApplication.shared.windows.first
}
Expand Down
16 changes: 8 additions & 8 deletions PlayTools/Utils/Toast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import UIKit

class Toast {
public static func showOver(msg: String) {
if let controller = PlayInput.shared.root {
Toast.show(message: msg, controller: controller)
if let parent = screen.keyWindow {
Toast.show(message: msg, parent: parent)
}
}

// swiftlint:disable function_body_length

private static func show(message: String, controller: UIViewController) {
private static func show(message: String, parent: UIView) {
let toastContainer = UIView(frame: CGRect())
toastContainer.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastContainer.alpha = 0.0
Expand All @@ -32,7 +32,7 @@ class Toast {
toastLabel.numberOfLines = 0

toastContainer.addSubview(toastLabel)
controller.view.addSubview(toastContainer)
parent.addSubview(toastContainer)

toastLabel.translatesAutoresizingMaskIntoConstraints = false
toastContainer.translatesAutoresizingMaskIntoConstraints = false
Expand Down Expand Up @@ -70,25 +70,25 @@ class Toast {
let controllerConstraint1 = NSLayoutConstraint(item: toastContainer,
attribute: .leading,
relatedBy: .equal,
toItem: controller.view,
toItem: parent,
attribute: .leading,
multiplier: 1,
constant: 65)
let controllerConstraint2 = NSLayoutConstraint(item: toastContainer,
attribute: .trailing,
relatedBy: .equal,
toItem: controller.view,
toItem: parent,
attribute: .trailing,
multiplier: 1,
constant: -65)
let controllerConstraint3 = NSLayoutConstraint(item: toastContainer,
attribute: .bottom,
relatedBy: .equal,
toItem: controller.view,
toItem: parent,
attribute: .bottom,
multiplier: 1,
constant: -75)
controller.view.addConstraints([controllerConstraint1, controllerConstraint2, controllerConstraint3])
parent.addConstraints([controllerConstraint1, controllerConstraint2, controllerConstraint3])

UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseIn, animations: {
toastContainer.alpha = 1.0
Expand Down

0 comments on commit fad9beb

Please sign in to comment.