implements custom on screen keyboard button
This commit is contained in:
1 parent
a1a7047370
commit
ab3c551357
11 files changed
+240
-529
No files matched your search
@@ -304,29 +304,7 @@ import UIKit
|
||||
private func sendKeyboardCommand(_ cmd: RemoteCommand) {
|
||||
print("Sending key-value")
|
||||
let keyboardCmdStrings = CommandManager.shared.extractKeyStrings(from: cmd.keyboardCmdString)
|
||||
sendKeyDownEventWithDelay(keyboardCmdStrings: keyboardCmdStrings!)
|
||||
}
|
||||
|
||||
private func sendKeyDownEventWithDelay(keyboardCmdStrings: [String], delay: TimeInterval = 0.05, index: Int = 0) {
|
||||
guard index < keyboardCmdStrings.count else {
|
||||
for keyStr in keyboardCmdStrings {
|
||||
LiSendKeyboardEvent(CommandManager.keyMappings[keyStr]!,Int8(KEY_ACTION_UP), 0)
|
||||
}
|
||||
return
|
||||
} // 如果已经处理完所有键,释放所有按键
|
||||
|
||||
let keyCode = CommandManager.keyMappings[keyboardCmdStrings[index]]
|
||||
guard let keyCode = keyCode else {
|
||||
print("No mapping found for \(keyboardCmdStrings[index])")
|
||||
sendKeyDownEventWithDelay(keyboardCmdStrings: keyboardCmdStrings, delay: delay, index: index+1) // 跳过当前键,继续下一个
|
||||
return
|
||||
}
|
||||
// 发送当前键的键盘按下事件
|
||||
LiSendKeyboardEvent(keyCode,Int8(KEY_ACTION_DOWN), 0)
|
||||
// 使用 asyncAfter 在指定异步延迟后发送下一个键的键盘按下事件
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
|
||||
self.sendKeyDownEventWithDelay(keyboardCmdStrings: keyboardCmdStrings, delay: delay, index: index+1) // 跳过当前键,继续下一个
|
||||
}
|
||||
CommandManager.shared.sendKeyDownEventWithDelay(keyboardCmdStrings: keyboardCmdStrings!)
|
||||
}
|
||||
}
|
||||
|
||||
+58
-44
@@ -35,7 +35,7 @@
|
||||
|
||||
|
||||
- (void) viewWillDisappear:(BOOL)animated{
|
||||
OnScreenKeyView.editMode = false;
|
||||
OnScreenKeyboardButtonView.editMode = false;
|
||||
[super viewWillDisappear:animated];
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:@"OscLayoutCloseNotification" object:self];
|
||||
}
|
||||
@@ -48,7 +48,7 @@
|
||||
|
||||
for (UIView *subview in self.view.subviews) {
|
||||
// 检查子视图是否是特定类型的实例
|
||||
if ([subview isKindOfClass:[OnScreenKeyView class]]) {
|
||||
if ([subview isKindOfClass:[OnScreenKeyboardButtonView class]]) {
|
||||
// 如果是,就添加到将要被移除的数组中
|
||||
[subview removeFromSuperview];
|
||||
}
|
||||
@@ -63,7 +63,7 @@
|
||||
for (NSData *buttonStateEncoded in oscProfile.buttonStates) {
|
||||
OnScreenButtonState* buttonState = [NSKeyedUnarchiver unarchivedObjectOfClass:[OnScreenButtonState class] fromData:buttonStateEncoded error:nil];
|
||||
if(buttonState.buttonType == KeyboardButton){
|
||||
OnScreenKeyView* keyView = [[OnScreenKeyView alloc] initWithKeyString:buttonState.name keyLabel:buttonState.alias]; //reconstruct keyView
|
||||
OnScreenKeyboardButtonView* keyView = [[OnScreenKeyboardButtonView alloc] initWithKeyString:buttonState.name keyLabel:buttonState.alias]; //reconstruct keyView
|
||||
keyView.translatesAutoresizingMaskIntoConstraints = NO; // weird but this is mandatory, or you will find no key views added to the right place
|
||||
keyView.timestamp = buttonState.timestamp; // will be set as key in in the dict.
|
||||
// Add the KeyView to the view controller's view
|
||||
@@ -165,7 +165,7 @@
|
||||
|
||||
|
||||
- (void) viewDidAppear:(BOOL)animated {
|
||||
OnScreenKeyView.editMode = true;
|
||||
OnScreenKeyboardButtonView.editMode = true;
|
||||
[super viewWillAppear:animated];
|
||||
[self profileRefresh];
|
||||
}
|
||||
@@ -282,14 +282,58 @@
|
||||
}
|
||||
|
||||
- (IBAction) addTapped:(id)sender{
|
||||
OnScreenKeyView* keyView = [[OnScreenKeyView alloc] initWithKeyString:@"ALT" keyLabel:@"test"];
|
||||
keyView.translatesAutoresizingMaskIntoConstraints = NO; // weird but this is mandatory, or you will find no key views added to the right place
|
||||
|
||||
keyView.timestamp = CACurrentMediaTime(); // will be set as key in in the dict.
|
||||
[self.onScreenKeyViewsDict setObject:keyView forKey:@(keyView.timestamp)];
|
||||
// Add the KeyView to the view controller's view
|
||||
[self.view addSubview:keyView];
|
||||
[keyView setKeyLocationWithXOffset:50 yOffset:50];
|
||||
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[LocalizationHelper localizedStringForKey:@"New Keyboard Button"]
|
||||
message:[LocalizationHelper localizedStringForKey:@"Enter the command & key label"]
|
||||
preferredStyle:UIAlertControllerStyleAlert];
|
||||
|
||||
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
||||
textField.placeholder = [LocalizationHelper localizedStringForKey:@"Command"];
|
||||
textField.keyboardType = UIKeyboardTypeASCIICapable;
|
||||
textField.autocorrectionType = UITextAutocorrectionTypeNo;
|
||||
textField.spellCheckingType = UITextSpellCheckingTypeNo;
|
||||
}];
|
||||
|
||||
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
|
||||
textField.placeholder = [LocalizationHelper localizedStringForKey:@"Key label (optional)"];
|
||||
textField.keyboardType = UIKeyboardTypeASCIICapable;
|
||||
textField.autocorrectionType = UITextAutocorrectionTypeNo;
|
||||
textField.spellCheckingType = UITextSpellCheckingTypeNo;
|
||||
}];
|
||||
|
||||
|
||||
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:[LocalizationHelper localizedStringForKey:@"Cancel"]
|
||||
style:UIAlertActionStyleCancel
|
||||
handler:nil];
|
||||
|
||||
UIAlertAction *okAction = [UIAlertAction actionWithTitle:[LocalizationHelper localizedStringForKey:@"OK"]
|
||||
style:UIAlertActionStyleDefault
|
||||
handler:^(UIAlertAction *action) {
|
||||
/*
|
||||
alertController.textFields[0].keyboardType = UIKeyboardTypeASCIICapable;
|
||||
alertController.textFields[0].autocorrectionType = UITextAutocorrectionTypeNo;
|
||||
alertController.textFields[0].spellCheckingType = UITextSpellCheckingTypeNo;
|
||||
alertController.textFields[1].keyboardType = UIKeyboardTypeASCIICapable;
|
||||
alertController.textFields[1].autocorrectionType = UITextAutocorrectionTypeNo;
|
||||
alertController.textFields[1].spellCheckingType = UITextSpellCheckingTypeNo;*/
|
||||
|
||||
NSString *keyboardCmdString = [alertController.textFields[0].text uppercaseString]; // convert to uppercase
|
||||
NSString *keyLabel = alertController.textFields[1].text;
|
||||
if([keyLabel isEqualToString:@""]) keyLabel = [[keyboardCmdString lowercaseString] capitalizedString];
|
||||
if([CommandManager.shared extractKeyStringsFrom:keyboardCmdString] == nil) return; // this is a invalid string.
|
||||
|
||||
//saving & present the keyboard button:
|
||||
OnScreenKeyboardButtonView* keyView = [[OnScreenKeyboardButtonView alloc] initWithKeyString:keyboardCmdString keyLabel:keyLabel];
|
||||
keyView.translatesAutoresizingMaskIntoConstraints = NO; // weird but this is mandatory, or you will find no key views added to the right place
|
||||
keyView.timestamp = CACurrentMediaTime(); // will be set as key in in the dict.
|
||||
[self.onScreenKeyViewsDict setObject:keyView forKey:@(keyView.timestamp)];
|
||||
// Add the KeyView to the view controller's view
|
||||
[self.view addSubview:keyView];
|
||||
[keyView setKeyLocationWithXOffset:50 yOffset:50];
|
||||
}];
|
||||
[alertController addAction:cancelAction];
|
||||
[alertController addAction:okAction];
|
||||
[self presentViewController:alertController animated:YES completion:nil];
|
||||
}
|
||||
|
||||
|
||||
@@ -310,36 +354,6 @@
|
||||
}]];
|
||||
[self presentViewController:savedAlertController animated:YES completion:nil];
|
||||
}
|
||||
|
||||
/*
|
||||
if ([self->profilesManager profileNameAlreadyExist:enteredProfileName] == YES) { // if the entered profile name already exists then let the user know. Offer to allow them to overwrite the existing profile
|
||||
UIAlertController * savedAlertController = [UIAlertController alertControllerWithTitle: [NSString stringWithFormat:@""] message: [NSString stringWithFormat:@"Another profile with the name '%@' already exists! Do you want to overwrite it?", enteredProfileName] preferredStyle:UIAlertControllerStyleAlert];
|
||||
|
||||
[savedAlertController addAction:[UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // overwrite existing profile
|
||||
[self->profilesManager saveProfileWithName: enteredProfileName andButtonLayers:self.layoutOSC.OSCButtonLayers];
|
||||
}]];
|
||||
|
||||
[savedAlertController addAction:[UIAlertAction actionWithpinNo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // don't overwrite the existing profile
|
||||
[savedAlertController dismissViewControllerAnimated:NO completion:nil];
|
||||
}]];
|
||||
[self presentViewController:savedAlertController animated:YES completion:nil];
|
||||
}
|
||||
else { // if user entered a valid name that doesn't already exist then save the profile to persistent storage
|
||||
[self->profilesManager saveProfileWithName: enteredProfileName andButtonLayers:self.layoutOSC.OSCButtonLayers];
|
||||
[self->profilesManager setProfileToSelected: enteredProfileName];
|
||||
|
||||
UIAlertController * savedAlertController = [UIAlertController alertControllerWithTitle: [NSString stringWithFormat:@""] message: [NSString stringWithFormat:@"%@ profile saved and set as your active in-game controller profile layout", enteredProfileName] preferredStyle:UIAlertControllerStyleAlert]; // Let user know this profile has been saved and is now the selected controller layout
|
||||
|
||||
[savedAlertController addAction:[UIAlertAction actionWithTitle:[LocalizationHelper localizedStringForKey:@"Ok"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
|
||||
[savedAlertController dismissViewControllerAnimated:NO completion:nil];
|
||||
}]];
|
||||
[self presentViewController:savedAlertController animated:YES completion:nil];
|
||||
}
|
||||
}]];
|
||||
[inputNameAlertController addAction:[UIAlertAction actionWithTitle:[LocalizationHelper localizedStringForKey:@"Cancel"] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // adds a button that allows user to decline the option to save the controller layout they currently see on screen
|
||||
[inputNameAlertController dismissViewControllerAnimated:NO completion:nil];
|
||||
}]];
|
||||
[self presentViewController:inputNameAlertController animated:YES completion:nil]; */
|
||||
}
|
||||
|
||||
|
||||
@@ -471,9 +485,9 @@
|
||||
// removing keyboard buttons objs
|
||||
UITouch *touch = [touches anyObject]; // Get the first touch in the set
|
||||
if([self touchWithinTashcanButton:touch]){
|
||||
[self.onScreenKeyViewsDict[@(OnScreenKeyView.timestampOfButtonBeingDragged)] removeFromSuperview];
|
||||
[self.onScreenKeyViewsDict removeObjectForKey:@(OnScreenKeyView.timestampOfButtonBeingDragged)];
|
||||
OnScreenKeyView.timestampOfButtonBeingDragged = 0; //reset thie timestamp
|
||||
[self.onScreenKeyViewsDict[@(OnScreenKeyboardButtonView.timestampOfButtonBeingDragged)] removeFromSuperview];
|
||||
[self.onScreenKeyViewsDict removeObjectForKey:@(OnScreenKeyboardButtonView.timestampOfButtonBeingDragged)];
|
||||
OnScreenKeyboardButtonView.timestampOfButtonBeingDragged = 0; //reset thie timestamp
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,339 +0,0 @@
|
||||
//
|
||||
// KeyManagerViewController.swift
|
||||
// Moonlight-ZWM
|
||||
//
|
||||
// Created by ZWM on 2024/7/23.
|
||||
// Copyright © 2024 Moonlight Game Streaming Project. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
@objc public class OnScreenKeyManagerViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
|
||||
|
||||
public let tableView = UITableView()
|
||||
private let addButton = UIButton(type: .system)
|
||||
private let deleteButton = UIButton(type: .system)
|
||||
private let editButton = UIButton(type: .system)
|
||||
private let exitButton = UIButton(type: .system)
|
||||
private let pinButton = UIButton(type: .system)
|
||||
private let viewBackgroundColor = UIColor(white: 0.2, alpha: 0.8);
|
||||
private let highlightColor = UIColor(white: 0.39, alpha: 0.8);
|
||||
private let titleLabel = UILabel()
|
||||
|
||||
private var viewPinned: Bool = false
|
||||
private var isEditingMode: Bool = false {
|
||||
didSet {
|
||||
updateEditingMode()
|
||||
}
|
||||
}
|
||||
|
||||
override public func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
setupViews()
|
||||
updateEditingMode()
|
||||
|
||||
tableView.dataSource = self
|
||||
tableView.delegate = self
|
||||
|
||||
// Register the cell class
|
||||
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
|
||||
//pass self to the CommandManager
|
||||
//CommandManager.shared.viewController = self
|
||||
}
|
||||
|
||||
override public func viewDidLayoutSubviews() {
|
||||
super.viewDidLayoutSubviews()
|
||||
setupConstraints()
|
||||
}
|
||||
|
||||
private func setupViews() {
|
||||
|
||||
// Set corner radius
|
||||
view.layer.cornerRadius = 20 // Adjust the corner radius
|
||||
view.layer.masksToBounds = true
|
||||
|
||||
// Set up the title label
|
||||
titleLabel.text = SwiftLocalizationHelper.localizedString(forKey: "Send Commands")
|
||||
titleLabel.font = UIFont.boldSystemFont(ofSize: 20) // Adjust font size as needed
|
||||
titleLabel.textColor = UIColor.white // Adjust color as needed
|
||||
titleLabel.textAlignment = .center
|
||||
view.addSubview(titleLabel)
|
||||
|
||||
view.backgroundColor = viewBackgroundColor
|
||||
tableView.backgroundColor = .clear
|
||||
tableView.rowHeight = 60
|
||||
tableView.separatorColor = highlightColor
|
||||
// Configure buttons
|
||||
addButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Add / Duplicate"), for: .normal)
|
||||
deleteButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Delete"), for: .normal)
|
||||
editButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Edit"), for: .normal)
|
||||
exitButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Exit"), for: .normal)
|
||||
pinButton.setTitle("📌", for: .normal)
|
||||
addButton.titleLabel?.font = UIFont.systemFont(ofSize: 20) // Adjust the size as needed
|
||||
deleteButton.titleLabel?.font = UIFont.systemFont(ofSize: 20)
|
||||
editButton.titleLabel?.font = UIFont.systemFont(ofSize: 20)
|
||||
exitButton.titleLabel?.font = UIFont.systemFont(ofSize: 20)
|
||||
pinButton.titleLabel?.font = UIFont.systemFont(ofSize: 20)
|
||||
|
||||
// Add subviews
|
||||
view.addSubview(tableView)
|
||||
view.addSubview(addButton)
|
||||
view.addSubview(deleteButton)
|
||||
view.addSubview(editButton)
|
||||
view.addSubview(exitButton)
|
||||
view.addSubview(pinButton)
|
||||
|
||||
// Setup button targets
|
||||
addButton.addTarget(self, action: #selector(addButtonTapped), for: .touchUpInside)
|
||||
deleteButton.addTarget(self, action: #selector(deleteButtonTapped), for: .touchUpInside)
|
||||
editButton.addTarget(self, action: #selector(editButtonTapped), for: .touchUpInside)
|
||||
exitButton.addTarget(self, action: #selector(exitButtonTapped), for: .touchUpInside)
|
||||
pinButton.addTarget(self, action: #selector(pinButtonTapped), for: .touchUpInside)
|
||||
pinButton.contentEdgeInsets = UIEdgeInsets(top: 7, left: 20, bottom: 7, right: 20)
|
||||
|
||||
}
|
||||
|
||||
private func setupConstraints() {
|
||||
|
||||
guard let superview = view.superview else {
|
||||
return
|
||||
}
|
||||
|
||||
view.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
tableView.translatesAutoresizingMaskIntoConstraints = false
|
||||
addButton.translatesAutoresizingMaskIntoConstraints = false
|
||||
deleteButton.translatesAutoresizingMaskIntoConstraints = false
|
||||
editButton.translatesAutoresizingMaskIntoConstraints = false
|
||||
exitButton.translatesAutoresizingMaskIntoConstraints = false
|
||||
pinButton.translatesAutoresizingMaskIntoConstraints = false
|
||||
titleLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
NSLayoutConstraint.activate([
|
||||
|
||||
// Set the width and height of the view
|
||||
view.widthAnchor.constraint(equalTo: view.superview!.widthAnchor, multiplier: 1),
|
||||
view.heightAnchor.constraint(equalTo: view.superview!.heightAnchor, multiplier: 1),
|
||||
|
||||
// Center the view horizontally and vertically
|
||||
view.centerXAnchor.constraint(equalTo: view.superview!.centerXAnchor),
|
||||
view.centerYAnchor.constraint(equalTo: view.superview!.centerYAnchor),
|
||||
|
||||
// ViewTitle constrains
|
||||
titleLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 13.5),
|
||||
titleLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
|
||||
titleLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
|
||||
|
||||
|
||||
// TableView constraints
|
||||
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
|
||||
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
|
||||
tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
|
||||
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -60),
|
||||
|
||||
// ExitButton constraints
|
||||
exitButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 25),
|
||||
exitButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
|
||||
|
||||
// EditButton constraints
|
||||
editButton.leadingAnchor.constraint(equalTo: exitButton.trailingAnchor, constant: 50),
|
||||
editButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
|
||||
|
||||
// AddButton constraints
|
||||
addButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -25),
|
||||
addButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
|
||||
|
||||
// DeleteButton constraints
|
||||
deleteButton.trailingAnchor.constraint(equalTo: addButton.leadingAnchor, constant: -50),
|
||||
deleteButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
|
||||
|
||||
// DeleteButton constraints
|
||||
pinButton.trailingAnchor.constraint(equalTo: deleteButton.leadingAnchor, constant: -50),
|
||||
pinButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
|
||||
])
|
||||
}
|
||||
|
||||
// view controller method
|
||||
private func updateEditingMode() {
|
||||
addButton.isEnabled = isEditingMode
|
||||
deleteButton.isEnabled = isEditingMode
|
||||
if(isEditingMode){ editButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Done"), for: .normal) }
|
||||
else{ editButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Edit"), for: .normal) }
|
||||
}
|
||||
|
||||
// view controller method
|
||||
@objc private func pinButtonTapped() {
|
||||
viewPinned = !viewPinned
|
||||
if viewPinned {
|
||||
pinButton.backgroundColor = highlightColor
|
||||
}
|
||||
else{
|
||||
pinButton.backgroundColor = viewBackgroundColor
|
||||
}
|
||||
}
|
||||
|
||||
// tobe modified
|
||||
@objc private func addButtonTapped() {
|
||||
let previouslySelectedIndexPath = tableView.indexPathForSelectedRow //memorize selected indexpath
|
||||
let alert = UIAlertController(title: SwiftLocalizationHelper.localizedString(forKey: "New Command"), message: SwiftLocalizationHelper.localizedString(forKey: "Enter a new command and alias"), preferredStyle: .alert)
|
||||
alert.addTextField { $0.placeholder = SwiftLocalizationHelper.localizedString(forKey:"Command") }
|
||||
alert.addTextField { $0.placeholder = SwiftLocalizationHelper.localizedString(forKey: "Alias (optional)") }
|
||||
alert.textFields?[0].keyboardType = .asciiCapable
|
||||
alert.textFields?[0].autocorrectionType = .no
|
||||
alert.textFields?[0].spellCheckingType = .no
|
||||
alert.textFields?[1].keyboardType = .asciiCapable
|
||||
alert.textFields?[1].autocorrectionType = .no
|
||||
alert.textFields?[1].spellCheckingType = .no
|
||||
|
||||
let submitAction = UIAlertAction(title: SwiftLocalizationHelper.localizedString(forKey: "Add"), style: .default) { [unowned alert] _ in
|
||||
let keyboardCmdString = alert.textFields?[0].text ?? ""
|
||||
let alias = alert.textFields?[1].text ?? keyboardCmdString
|
||||
let newCommand = RemoteCommand(keyboardCmdString: keyboardCmdString, alias: alias)
|
||||
let addCommandSuceeded = CommandManager.shared.addCommand(newCommand)
|
||||
//self.reloadTableView() // don't know why but this reload has to be called from the CommandManager, it doesn't work here.
|
||||
|
||||
//if previouslySelectedIndexPath == nil { return }
|
||||
if addCommandSuceeded {
|
||||
let lastRow = self.tableView.numberOfRows(inSection: 0) - 1 //for now there's only 1 section for the tableview, just use setion 0
|
||||
let newEntryIndexPath = IndexPath(row: lastRow, section: 0)
|
||||
self.tableView.selectRow(at: newEntryIndexPath, animated: true, scrollPosition: .middle) // shift the highlight to the newly added entry
|
||||
}
|
||||
else {
|
||||
self.tableView.selectRow(at: previouslySelectedIndexPath, animated: true, scrollPosition: .middle) // keep the highlight on the previous entry if failed to add command
|
||||
}
|
||||
}
|
||||
|
||||
let cancelAction = UIAlertAction(title: SwiftLocalizationHelper.localizedString(forKey:"Cancel"), style: .cancel)
|
||||
alert.addAction(submitAction)
|
||||
alert.addAction(cancelAction)
|
||||
|
||||
if let selectedIndexPath = self.tableView.indexPathForSelectedRow {
|
||||
let selectedCommand = CommandManager.shared.getAllCommands()[selectedIndexPath.row]
|
||||
alert.textFields?[0].text = selectedCommand.keyboardCmdString // load selected keyboard cmd string
|
||||
//alert.textFields?[1].text = selectedCommand.alias // leave the alias input field empty
|
||||
}
|
||||
|
||||
self.present(alert, animated: true)
|
||||
}
|
||||
|
||||
// tobe modified
|
||||
@objc private func deleteButtonTapped() {
|
||||
guard let selectedIndexPath = tableView.indexPathForSelectedRow else {
|
||||
return
|
||||
}
|
||||
let previouslySelectedIndexPath = selectedIndexPath // memorize selected row before reloading tableview
|
||||
|
||||
CommandManager.shared.deleteCommand(at: selectedIndexPath.row)
|
||||
reloadTableView()
|
||||
// target new entry after deletion
|
||||
var targetRowAfterDel = previouslySelectedIndexPath.row - 1
|
||||
if targetRowAfterDel < 0 { targetRowAfterDel = 0 }
|
||||
let targetIndexPathAfterDel = IndexPath(row: targetRowAfterDel, section: previouslySelectedIndexPath.section)
|
||||
if targetRowAfterDel < tableView.numberOfRows(inSection: previouslySelectedIndexPath.section) {
|
||||
tableView.selectRow(at: targetIndexPathAfterDel, animated: true, scrollPosition: .middle) // shift highlight to the target entry after deletion
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func editButtonTapped() {
|
||||
isEditingMode.toggle()
|
||||
}
|
||||
|
||||
@objc private func exitButtonTapped() {
|
||||
dismiss(animated: true, completion: nil)
|
||||
}
|
||||
|
||||
@objc public func reloadTableView() {
|
||||
tableView.layoutIfNeeded()
|
||||
tableView.reloadData()
|
||||
|
||||
/*
|
||||
let previouslySelectedIndexPath = tableView.indexPathForSelectedRow
|
||||
if let indexPath = previouslySelectedIndexPath {
|
||||
// Make sure the indexPath is still valid and scroll to the selected indexPath
|
||||
if indexPath.row < tableView.numberOfRows(inSection: indexPath.section) {
|
||||
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .middle) // keep the entry of previous index selected.
|
||||
}
|
||||
} */
|
||||
}
|
||||
|
||||
// tobe modified
|
||||
// UITableViewDataSource
|
||||
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
return CommandManager.shared.getAllCommands().count
|
||||
}
|
||||
|
||||
|
||||
// tobe modified
|
||||
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
|
||||
// Configure cell appearance
|
||||
cell.backgroundColor = .clear // Set background color of the cell
|
||||
cell.textLabel?.textColor = UIColor.white // Set font color of the text
|
||||
|
||||
|
||||
// Set selected background view
|
||||
let selectedBackgroundView = UIView()
|
||||
selectedBackgroundView.backgroundColor = highlightColor // Color for the selected state
|
||||
cell.selectedBackgroundView = selectedBackgroundView
|
||||
|
||||
let command = CommandManager.shared.getAllCommands()[indexPath.row]
|
||||
cell.textLabel?.text = command.alias
|
||||
return cell
|
||||
}
|
||||
|
||||
|
||||
// UITableViewDelegate
|
||||
// tobe modified
|
||||
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||
|
||||
if let cell = tableView.cellForRow(at: indexPath) {
|
||||
// Set the cell background color to the flashing color
|
||||
// Animate the flash effect
|
||||
UIView.animate(withDuration: 0.1, animations: {
|
||||
cell.selectedBackgroundView?.backgroundColor = .clear
|
||||
}) { _ in
|
||||
// Reset the cell background color after the animation
|
||||
UIView.animate(withDuration: 0.1) {
|
||||
cell.selectedBackgroundView?.backgroundColor = self.highlightColor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !isEditingMode {
|
||||
// Sending keyboard command
|
||||
let command = CommandManager.shared.getAllCommands()[indexPath.row]
|
||||
sendKeyboardCommand(command)
|
||||
if !viewPinned { dismiss(animated: false, completion: nil) } // dimiss the view in sending mode & the view is not pinned
|
||||
}
|
||||
}
|
||||
|
||||
private func sendKeyboardCommand(_ cmd: RemoteCommand) {
|
||||
print("Sending key-value")
|
||||
let keyboardCmdStrings = CommandManager.shared.extractKeyStrings(from: cmd.keyboardCmdString)
|
||||
sendKeyDownEventWithDelay(keyboardCmdStrings: keyboardCmdStrings!)
|
||||
}
|
||||
|
||||
private func sendKeyDownEventWithDelay(keyboardCmdStrings: [String], delay: TimeInterval = 0.05, index: Int = 0) {
|
||||
guard index < keyboardCmdStrings.count else {
|
||||
for keyStr in keyboardCmdStrings {
|
||||
LiSendKeyboardEvent(CommandManager.keyMappings[keyStr]!,Int8(KEY_ACTION_UP), 0)
|
||||
}
|
||||
return
|
||||
} // 如果已经处理完所有键,释放所有按键
|
||||
|
||||
let keyCode = CommandManager.keyMappings[keyboardCmdStrings[index]]
|
||||
guard let keyCode = keyCode else {
|
||||
print("No mapping found for \(keyboardCmdStrings[index])")
|
||||
sendKeyDownEventWithDelay(keyboardCmdStrings: keyboardCmdStrings, delay: delay, index: index+1) // 跳过当前键,继续下一个
|
||||
return
|
||||
}
|
||||
// 发送当前键的键盘按下事件
|
||||
LiSendKeyboardEvent(keyCode,Int8(KEY_ACTION_DOWN), 0)
|
||||
// 使用 asyncAfter 在指定异步延迟后发送下一个键的键盘按下事件
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
|
||||
self.sendKeyDownEventWithDelay(keyboardCmdStrings: keyboardCmdStrings, delay: delay, index: index+1) // 跳过当前键,继续下一个
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#import "CustomTapGestureRecognizer.h"
|
||||
#import "LocalizationHelper.h"
|
||||
#import "Moonlight-Swift.h"
|
||||
#import "OSCProfilesManager.h"
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
@@ -155,7 +156,7 @@
|
||||
[_streamView setupStreamView:_controllerSupport interactionDelegate:self config:self.streamConfig]; //reinitiate setupStreamView process.
|
||||
[self->_streamView reloadOnScreenControlsRealtimeWith:(ControllerSupport*)_controllerSupport
|
||||
andConfig:(StreamConfiguration*)_streamConfig]; //reload OSC here.
|
||||
|
||||
[self->_streamView reloadOnScreenKeyboardButtons]; //reload keyboard buttons here. the keyboard button view will be added to the streamframe view instead streamview, the highest layer, which saves a lot of reengineering
|
||||
//reconfig statsOverlay
|
||||
self->_statsUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
|
||||
target:self
|
||||
@@ -169,10 +170,12 @@
|
||||
|
||||
|
||||
|
||||
|
||||
- (void)viewDidAppear:(BOOL)animated
|
||||
{
|
||||
[super viewDidAppear:animated];
|
||||
|
||||
//[self layoutOnScreenKeyboardButtons];
|
||||
|
||||
|
||||
// onscreen keyView test
|
||||
/*
|
||||
@@ -188,9 +191,9 @@
|
||||
CGFloat yOffset = 550;
|
||||
CGFloat spacing = 10.0;
|
||||
|
||||
[keyView setKeyLocationWithXOffset:500 yOffset:(CGFloat)550];
|
||||
[keyView setKeyLocationWithXOffset:500 yOffset:(CGFloat)550]; */
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -346,6 +349,7 @@
|
||||
|
||||
- (void)layoutOSC{
|
||||
[self->_streamView disableOnScreenControls];
|
||||
[self->_streamView clearOnScreenKeyboardButtons]; // clear all onScreenKeyboardButtons before entering edit mode
|
||||
[self presentViewController:_layoutOnScreenControlsVC animated:YES completion:nil];
|
||||
}
|
||||
|
||||
@@ -355,6 +359,7 @@
|
||||
[self->_streamView reloadOnScreenControlsWith:(ControllerSupport*)_controllerSupport
|
||||
andConfig:(StreamConfiguration*)_streamConfig];
|
||||
[self->_streamView showOnScreenControls];
|
||||
[self->_streamView reloadOnScreenKeyboardButtons]; //update keyboard buttons here
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user