add mouse button function to commandManager & OnScreenButton

This commit is contained in:
True Zhuanjia committed 2024-08-24 16:48:17 +08:00
1 parent 747997f46c
commit 1d1d4cbddf
10 files changed
+83 -99

No files matched your search

+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];
}
}
}