add mouse button function to commandManager & OnScreenButton

This commit is contained in:
True Zhuanjia
2024-08-24 16:48:17 +08:00
parent 747997f46c
commit 1d1d4cbddf
10 changed files with 83 additions and 99 deletions
+6
View File
@@ -51,6 +51,12 @@ import UIKit
@objc public class CommandManager: NSObject {
@objc public static let shared = CommandManager()
@objc public static let mouseButtonMappings: [String: Int32] = ["M_LEFT" : BUTTON_LEFT,
"M_MIDDLE" : BUTTON_RIGHT,
"M_RIGHT" : BUTTON_RIGHT,
"M_X1" : BUTTON_X1,
"M_X2" : BUTTON_X2]
static let keyMappings: [String: Int16] = [
// Windows Key Codes
"CTRL": 0x11, // VK_CONTROL
@@ -19,7 +19,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (OSCProfilesManager *) sharedManager;
+ (void) setOnScreenKeyViewsDict:(NSMutableDictionary* )dict;
+ (void) setOnScreenButtonViewsDict:(NSMutableDictionary* )dict;
#pragma mark - Getters
/**
@@ -13,7 +13,7 @@
@implementation OSCProfilesManager
static NSMutableDictionary *onScreenKeyViewsDict;
static NSMutableDictionary *onScreenButtonViewsDict;
#pragma mark - Initializer
@@ -31,8 +31,8 @@ static NSMutableDictionary *onScreenKeyViewsDict;
#pragma mark - Class Helper Methods
+ (void) setOnScreenKeyViewsDict:(NSMutableDictionary* )dict{
onScreenKeyViewsDict = dict;
+ (void) setOnScreenButtonViewsDict:(NSMutableDictionary* )dict{
onScreenButtonViewsDict = dict;
}
/**
@@ -272,11 +272,11 @@ static NSMutableDictionary *onScreenKeyViewsDict;
}
// save keyboard button as buttonstate, this is for the new onscreen keyboard buttons:
[onScreenKeyViewsDict enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) { // this dict is passed from the layout tool VC, as a static obj in this class.
OnScreenKeyboardButtonView *keyView = value;
OnScreenButtonState *buttonState = [[OnScreenButtonState alloc] initWithButtonName:keyView.keyString buttonType:KeyboardButton andPosition:keyView.frame.origin];
buttonState.alias = keyView.keyLabel;
buttonState.timestamp = keyView.timestamp;
[onScreenButtonViewsDict enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) { // this dict is passed from the layout tool VC, as a static obj in this class.
OnScreenButtonView *buttonView = value;
OnScreenButtonState *buttonState = [[OnScreenButtonState alloc] initWithButtonName:buttonView.keyString buttonType:KeyboardOrMouseButton andPosition:buttonView.frame.origin];
buttonState.alias = buttonView.keyLabel;
buttonState.timestamp = buttonView.timestamp;
NSData *buttonStateEncoded = [NSKeyedArchiver archivedDataWithRootObject:buttonState requiringSecureCoding:YES error:nil];
[buttonStatesEncoded addObject: buttonStateEncoded];
}];
@@ -29,7 +29,7 @@ NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, OnScreenButtonType) {
GameControllerButton,
KeyboardButton
KeyboardOrMouseButton
};
- (id) initWithButtonName:(NSString*)name buttonType:(uint8_t)buttonType andPosition:(CGPoint)position;
@@ -9,12 +9,14 @@
import UIKit
@objc class OnScreenKeyboardButtonView: UIView {
@objc class OnScreenButtonView: UIView {
@objc static public var editMode: Bool = false
@objc static public var timestampOfButtonBeingDragged: TimeInterval = 0
@objc public var keyLabel: String
@objc public var keyString: String
@objc public var timestamp: TimeInterval
private let label: UILabel
private let originalBackgroundColor: UIColor
private var storedLocation: CGPoint = .zero
@@ -42,7 +44,7 @@ import UIKit
}
@objc public func enableRelocationMode(enabled: Bool){
OnScreenKeyboardButtonView.editMode = enabled
OnScreenButtonView.editMode = enabled
}
private func setupView() {
@@ -105,7 +107,7 @@ import UIKit
super.touchesBegan(touches, with: event)
//self.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 1.0, alpha: 0.7)
if !OnScreenKeyboardButtonView.editMode {
if !OnScreenButtonView.editMode {
// self.backgroundColor = UIColor.systemBlue.withAlphaComponent(0.7)
self.buttonDownVisualEffect()
self.layer.borderWidth = 0
@@ -122,7 +124,12 @@ import UIKit
}
// if there's no "+" in the keystring, treat it as a regular button:
else{
LiSendKeyboardEvent(CommandManager.keyMappings[self.keyString]!,Int8(KEY_ACTION_DOWN), 0)
if(CommandManager.mouseButtonMappings.keys.contains(self.keyString)){
LiSendMouseButtonEvent(CChar(BUTTON_ACTION_PRESS), Int32(CommandManager.mouseButtonMappings[self.keyString]!))
}
else{
LiSendKeyboardEvent(CommandManager.keyMappings[self.keyString]!,Int8(KEY_ACTION_DOWN), 0)
}
}
}
else {
@@ -136,10 +143,10 @@ import UIKit
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
OnScreenKeyboardButtonView.timestampOfButtonBeingDragged = self.timestamp
OnScreenButtonView.timestampOfButtonBeingDragged = self.timestamp
// Move the KeyView based on touch movement in relocation mode
if OnScreenKeyboardButtonView.editMode {
// Move the buttonView based on touch movement in relocation mode
if OnScreenButtonView.editMode {
if let touch = touches.first {
let currentLocation = touch.location(in: superview)
let offsetX = currentLocation.x - storedLocation.x
@@ -155,8 +162,15 @@ import UIKit
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
if !OnScreenKeyboardButtonView.editMode && !self.keyString.contains("+") { // if the command(keystring contains "+", it's a multi-key command rather than a single key button
LiSendKeyboardEvent(CommandManager.keyMappings[self.keyString]!,Int8(KEY_ACTION_UP), 0)
if !OnScreenButtonView.editMode && !self.keyString.contains("+") { // if the command(keystring contains "+", it's a multi-key command rather than a single key button
if(CommandManager.mouseButtonMappings.keys.contains(self.keyString)){
LiSendMouseButtonEvent(CChar(BUTTON_ACTION_RELEASE), Int32(CommandManager.mouseButtonMappings[self.keyString]!))
}
else{
LiSendKeyboardEvent(CommandManager.keyMappings[self.keyString]!,Int8(KEY_ACTION_UP), 0)
}
// self.backgroundColor = self.originalBackgroundColor
self.layer.shadowColor = UIColor.clear.cgColor
self.layer.borderWidth = 1
@@ -184,6 +198,6 @@ import UIKit
// Trigger layout update
superview.layoutIfNeeded()
setupView(); //re-setup keyview style
setupView(); //re-setup buttonView style
}
}
+8 -8
View File
@@ -343,7 +343,7 @@ static const double X1_MOUSE_SPEED_DIVISOR = 2.5;
- (void) clearOnScreenKeyboardButtons{
for (UIView *subview in self.superview.subviews) {
// 检查子视图是否是特定类型的实例
if ([subview isKindOfClass:[OnScreenKeyboardButtonView class]]) {
if ([subview isKindOfClass:[OnScreenButtonView class]]) {
// 如果是,就添加到将要被移除的数组中
[subview removeFromSuperview];
}
@@ -364,15 +364,15 @@ static const double X1_MOUSE_SPEED_DIVISOR = 2.5;
OSCProfilesManager* profilesManager = [OSCProfilesManager sharedManager];
OSCProfile *oscProfile = [profilesManager getSelectedProfile]; //returns the currently selected OSCProfile
if(!OnScreenKeyboardButtonView.editMode){ // in edit mode, keyboard button view will be updated within layoutool view controller.
if(!OnScreenButtonView.editMode){ // in edit mode, keyboard button view will be updated within layoutool view controller.
for (NSData *buttonStateEncoded in oscProfile.buttonStates) {
OnScreenButtonState* buttonState = [NSKeyedUnarchiver unarchivedObjectOfClass:[OnScreenButtonState class] fromData:buttonStateEncoded error:nil];
if(buttonState.buttonType == KeyboardButton){
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
// Add the KeyView to the view controller's view
[self.superview addSubview:keyView]; // add keyboard button to the stream frame view. must add it to the target view before setting location.
[keyView setKeyLocationWithXOffset:buttonState.position.x yOffset:buttonState.position.y];
if(buttonState.buttonType == KeyboardOrMouseButton){
OnScreenButtonView* buttonView = [[OnScreenButtonView alloc] initWithKeyString:buttonState.name keyLabel:buttonState.alias]; //reconstruct buttonView
buttonView.translatesAutoresizingMaskIntoConstraints = NO; // weird but this is mandatory, or you will find no key views added to the right place
// Add the buttonView to the view controller's view
[self.superview addSubview:buttonView]; // add keyboard button to the stream frame view. must add it to the target view before setting location.
[buttonView setKeyLocationWithXOffset:buttonState.position.x yOffset:buttonState.position.y];
}
}
}
@@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)reloadOnScreenKeyboardButtons;
@property LayoutOnScreenControls *layoutOSC; // object that contains a view which contains the on screen controller buttons that allows the user to drag and positions each button on the screen using touch
@property (nonatomic) NSMutableDictionary* onScreenKeyViewsDict;
@property (nonatomic) NSMutableDictionary* onScreenButtonViewsDict;
@property int OSCSegmentSelected;
@@ -35,26 +35,21 @@
- (void) viewWillDisappear:(BOOL)animated{
OnScreenKeyboardButtonView.editMode = false;
OnScreenButtonView.editMode = false;
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] postNotificationName:@"OscLayoutCloseNotification" object:self];
}
- (void) reloadOnScreenKeyboardButtons {
/*
[self.onScreenKeyViewsDict enumerateKeysAndObjectsUsingBlock:^(id timeIntervalKey, id keyView, BOOL *stop) {
[keyView removeFromSuperview];
}];*/
for (UIView *subview in self.view.subviews) {
// 检查子视图是否是特定类型的实例
if ([subview isKindOfClass:[OnScreenKeyboardButtonView class]]) {
if ([subview isKindOfClass:[OnScreenButtonView class]]) {
// 如果是,就添加到将要被移除的数组中
[subview removeFromSuperview];
}
}
[self.onScreenKeyViewsDict removeAllObjects];
[self.onScreenButtonViewsDict removeAllObjects];
NSLog(@"reload os Key here");
@@ -62,32 +57,25 @@
OSCProfile *oscProfile = [profilesManager getSelectedProfile]; //returns the currently selected OSCProfile
for (NSData *buttonStateEncoded in oscProfile.buttonStates) {
OnScreenButtonState* buttonState = [NSKeyedUnarchiver unarchivedObjectOfClass:[OnScreenButtonState class] fromData:buttonStateEncoded error:nil];
if(buttonState.buttonType == KeyboardButton){
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
[self.view addSubview:keyView];
[keyView setKeyLocationWithXOffset:buttonState.position.x yOffset:buttonState.position.y];
if(buttonState.buttonType == KeyboardOrMouseButton){
OnScreenButtonView* buttonView = [[OnScreenButtonView alloc] initWithKeyString:buttonState.name keyLabel:buttonState.alias]; //reconstruct buttonView
buttonView.translatesAutoresizingMaskIntoConstraints = NO; // weird but this is mandatory, or you will find no key views added to the right place
buttonView.timestamp = buttonState.timestamp; // will be set as key in in the dict.
// Add the buttonView to the view controller's view
[self.view addSubview:buttonView];
[buttonView setKeyLocationWithXOffset:buttonState.position.x yOffset:buttonState.position.y];
[self.onScreenKeyViewsDict setObject:keyView forKey:@(keyView.timestamp)];
[self.onScreenButtonViewsDict setObject:buttonView forKey:@(buttonView.timestamp)];
}
}
//////////////////
/*
[self.onScreenKeyViewsDict enumerateKeysAndObjectsUsingBlock:^(id timeIntervalKey, id keyView, BOOL *stop) {
[self.view addSubview:keyView];
}];*/
}
- (void) viewDidLoad {
[super viewDidLoad];
profilesManager = [OSCProfilesManager sharedManager];
self.onScreenKeyViewsDict = [[NSMutableDictionary alloc] init]; // will be revised to read persisted data , somewhere else
[OSCProfilesManager setOnScreenKeyViewsDict:self.onScreenKeyViewsDict]; // pass the keyboard button dict to profiles manager
self.onScreenButtonViewsDict = [[NSMutableDictionary alloc] init]; // will be revised to read persisted data , somewhere else
[OSCProfilesManager setOnScreenButtonViewsDict:self.onScreenButtonViewsDict]; // pass the keyboard button dict to profiles manager
isToolbarHidden = NO; // keeps track if the toolbar is hidden up above the screen so that we know whether to hide or show it when the user taps the toolbar's hide/show button
@@ -165,7 +153,7 @@
- (void) viewDidAppear:(BOOL)animated {
OnScreenKeyboardButtonView.editMode = true;
OnScreenButtonView.editMode = true;
[super viewWillAppear:animated];
[self profileRefresh];
}
@@ -317,19 +305,21 @@
alertController.textFields[1].autocorrectionType = UITextAutocorrectionTypeNo;
alertController.textFields[1].spellCheckingType = UITextSpellCheckingTypeNo;*/
NSString *keyboardCmdString = [alertController.textFields[0].text uppercaseString]; // convert to uppercase
NSString *cmdString = [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.
if([keyLabel isEqualToString:@""]) keyLabel = [[cmdString lowercaseString] capitalizedString];
bool noValidKeyboardString = [CommandManager.shared extractKeyStringsFrom:cmdString] == nil; // this is a invalid string.
bool noVliadMouseButtongString = ![CommandManager.mouseButtonMappings.allKeys containsObject:cmdString];
if(noValidKeyboardString && noVliadMouseButtongString) return;
//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];
OnScreenButtonView* buttonView = [[OnScreenButtonView alloc] initWithKeyString:cmdString keyLabel:keyLabel];
buttonView.translatesAutoresizingMaskIntoConstraints = NO; // weird but this is mandatory, or you will find no key views added to the right place
buttonView.timestamp = CACurrentMediaTime(); // will be set as key in in the dict.
[self.onScreenButtonViewsDict setObject:buttonView forKey:@(buttonView.timestamp)];
// Add the buttonView to the view controller's view
[self.view addSubview:buttonView];
[buttonView setKeyLocationWithXOffset:50 yOffset:50];
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
@@ -485,9 +475,9 @@
// removing keyboard buttons objs
UITouch *touch = [touches anyObject]; // Get the first touch in the set
if([self touchWithinTashcanButton:touch]){
[self.onScreenKeyViewsDict[@(OnScreenKeyboardButtonView.timestampOfButtonBeingDragged)] removeFromSuperview];
[self.onScreenKeyViewsDict removeObjectForKey:@(OnScreenKeyboardButtonView.timestampOfButtonBeingDragged)];
OnScreenKeyboardButtonView.timestampOfButtonBeingDragged = 0; //reset thie timestamp
[self.onScreenButtonViewsDict[@(OnScreenButtonView.timestampOfButtonBeingDragged)] removeFromSuperview];
[self.onScreenButtonViewsDict removeObjectForKey:@(OnScreenButtonView.timestampOfButtonBeingDragged)];
OnScreenButtonView.timestampOfButtonBeingDragged = 0; //reset thie timestamp
}
@@ -174,33 +174,7 @@
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//[self layoutOnScreenKeyboardButtons];
// onscreen keyView test
/*
OnScreenKeyView *keyView = [[OnScreenKeyView alloc] initWithKeyString:@"WIN" keyLabel:@"WIN"];
keyView.translatesAutoresizingMaskIntoConstraints = NO;
// Add the KeyView to the view controller's view
[self.view addSubview:keyView];
// Configure the KeyView's layout
CGFloat xOffset = 50;
CGFloat yOffset = 550;
CGFloat spacing = 10.0;
[keyView setKeyLocationWithXOffset:500 yOffset:(CGFloat)550]; */
#if !TARGET_OS_TV
[[self revealViewController] setPrimaryViewController:self];
[[NSNotificationCenter defaultCenter] addObserver:self
+4 -4
View File
@@ -8,7 +8,7 @@
/* Begin PBXBuildFile section */
840751B32BF49C050096B657 /* CustomTapGestureRecognizer.m in Sources */ = {isa = PBXBuildFile; fileRef = 840751B22BF49C050096B657 /* CustomTapGestureRecognizer.m */; };
84203F232C5F24C00032EEED /* OnScreenKeyboardButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84203F222C5F24C00032EEED /* OnScreenKeyboardButtonView.swift */; };
84203F232C5F24C00032EEED /* OnScreenButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84203F222C5F24C00032EEED /* OnScreenButtonView.swift */; };
84351B0E2C29ACCB00A77989 /* LayoutOnScreenControls.m in Sources */ = {isa = PBXBuildFile; fileRef = 84351B072C29ACCA00A77989 /* LayoutOnScreenControls.m */; };
84351B0F2C29ACCB00A77989 /* OnScreenButtonState.m in Sources */ = {isa = PBXBuildFile; fileRef = 84351B092C29ACCA00A77989 /* OnScreenButtonState.m */; };
84351B102C29ACCB00A77989 /* OSCProfile.m in Sources */ = {isa = PBXBuildFile; fileRef = 84351B0A2C29ACCA00A77989 /* OSCProfile.m */; };
@@ -126,7 +126,7 @@
693B3A9A218638CD00982F7B /* Settings.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = Settings.bundle; sourceTree = "<group>"; };
840751B12BF49B5A0096B657 /* CustomTapGestureRecognizer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CustomTapGestureRecognizer.h; sourceTree = "<group>"; };
840751B22BF49C050096B657 /* CustomTapGestureRecognizer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CustomTapGestureRecognizer.m; sourceTree = "<group>"; };
84203F222C5F24C00032EEED /* OnScreenKeyboardButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnScreenKeyboardButtonView.swift; sourceTree = "<group>"; };
84203F222C5F24C00032EEED /* OnScreenButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnScreenButtonView.swift; sourceTree = "<group>"; };
84351B062C29ACCA00A77989 /* OnScreenButtonState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OnScreenButtonState.h; sourceTree = "<group>"; };
84351B072C29ACCA00A77989 /* LayoutOnScreenControls.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LayoutOnScreenControls.m; sourceTree = "<group>"; };
84351B082C29ACCA00A77989 /* OSCProfilesManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OSCProfilesManager.h; sourceTree = "<group>"; };
@@ -625,7 +625,7 @@
988FCD3F293B091B003050E2 /* KeyboardInputField.h */,
988FCD40293B091B003050E2 /* KeyboardInputField.m */,
84E1CDEB2C4F67D300547904 /* CommandManager.swift */,
84203F222C5F24C00032EEED /* OnScreenKeyboardButtonView.swift */,
84203F222C5F24C00032EEED /* OnScreenButtonView.swift */,
);
path = Input;
sourceTree = "<group>";
@@ -1031,7 +1031,7 @@
FBDE86E019F7A837001C18A8 /* UIComputerView.m in Sources */,
FB89463019F646E200339C8A /* StreamConfiguration.m in Sources */,
FBD3495319FF36FB002D2A60 /* SWRevealViewController.m in Sources */,
84203F232C5F24C00032EEED /* OnScreenKeyboardButtonView.swift in Sources */,
84203F232C5F24C00032EEED /* OnScreenButtonView.swift in Sources */,
84FD0B512C31338D006B5BE5 /* LocalizationHelper.m in Sources */,
FB1D59971BBCCB6400F482CA /* ComputerScrollView.m in Sources */,
FBD3495019FF2174002D2A60 /* SettingsViewController.m in Sources */,