onscreen keyboard profile test

This commit is contained in:
True Zhuanjia
2024-08-14 22:49:35 +08:00
parent d90bf747fc
commit fd768db70b
14 changed files with 277 additions and 65 deletions
@@ -162,7 +162,10 @@
}
/* save the name, position, and visibility of button being touched in array in case user wants to undo the change later */
OnScreenButtonState *onScreenButtonState = [[OnScreenButtonState alloc] initWithButtonName:layerBeingDragged.name isKeyboardButton:NO isHidden:layerBeingDragged.isHidden andPosition:layerBeingDragged.position];
OnScreenButtonState *onScreenButtonState = [[OnScreenButtonState alloc] initWithButtonName:layerBeingDragged.name buttonType:GameControllerButton andPosition:layerBeingDragged.position];
// add hidden attr here
onScreenButtonState.isHidden = layerBeingDragged.isHidden;
[layoutChanges addObject:onScreenButtonState];
[[NSNotificationCenter defaultCenter] postNotificationName:@"OSCLayoutChanged" object:self]; // lets the view controller know whether to fade the undo button in or out depending on whether there are any further OSC layout changes the user is allowed to undo
@@ -19,6 +19,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (OSCProfilesManager *) sharedManager;
+ (void) setOnScreenKeyViewsDict:(NSMutableDictionary* )dict;
#pragma mark - Getters
/**
@@ -50,7 +51,7 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void) saveProfileWithName:(NSString*)name andButtonLayers:(NSMutableArray *)buttonLayers;
- (bool) updateSelectedProfileSucceedWithButtonLayers:(NSMutableArray *)buttonLayers;
- (bool) updateSelectedProfile:(NSMutableArray *) oscButtonLayers;
/**
* Delete current selected profile.
*/
+31 -6
View File
@@ -7,10 +7,13 @@
//
#import "OSCProfilesManager.h"
#import "OnScreenButtonState.h"
#import "Moonlight-Swift.h"
#import "LayoutOnScreenControlsViewController.h"
@implementation OSCProfilesManager
static NSMutableDictionary *onScreenKeyViewsDict;
#pragma mark - Initializer
@@ -27,6 +30,11 @@
#pragma mark - Class Helper Methods
+ (void) setOnScreenKeyViewsDict:(NSMutableDictionary* )dict{
onScreenKeyViewsDict = dict;
}
/**
* Returns the profile whose 'name' property has a value that is equal to the 'name' passed into the method
*/
@@ -94,7 +102,6 @@
NSMutableArray *profiles = [self getAllProfiles];
for (OSCProfile *profile in profiles) { // set all profiles' 'isSelected' property to NO since the new profile we're saving over the old profile with will be the 'selected' profile
profile.isSelected = NO;
}
@@ -192,7 +199,7 @@
}
- (bool) updateSelectedProfileSucceedWithButtonLayers:(NSMutableArray *)buttonLayers {
- (bool) updateSelectedProfile:(NSMutableArray *) oscButtonLayers {
/* iterate through each OSC button the user sees on screen, create an 'OnScreenButtonState' object from each button, encode each object, and then add each object to an array */
/*
NSSet *validPositionButtonNames = [NSSet setWithObjects:
@@ -211,9 +218,12 @@
@"startButton",
nil]; */
NSMutableArray *buttonStatesEncoded = [[NSMutableArray alloc] init];
for (CALayer *buttonLayer in buttonLayers) {
for (CALayer *oscButtonLayer in oscButtonLayers) {
OnScreenButtonState *buttonState = [[OnScreenButtonState alloc] initWithButtonName:oscButtonLayer.name buttonType:GameControllerButton andPosition:oscButtonLayer.position];
// add hidden attr here
buttonState.isHidden = oscButtonLayer.isHidden;
OnScreenButtonState *buttonState = [[OnScreenButtonState alloc] initWithButtonName:buttonLayer.name isKeyboardButton:NO isHidden:buttonLayer.isHidden andPosition:buttonLayer.position];
/* //-------------------------------------
NSLog(@"buttonName to be saved: %@", buttonState.name);
if([validPositionButtonNames containsObject:buttonState.name]) buttonState.hasValidPosition = true; // this is for setting up DZ for custom OSC buttons.
@@ -222,6 +232,19 @@
NSData *buttonStateEncoded = [NSKeyedArchiver archivedDataWithRootObject:buttonState requiringSecureCoding:YES error:nil];
[buttonStatesEncoded addObject: buttonStateEncoded];
}
// save keyboard button as buttonstate
[onScreenKeyViewsDict enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) { // this dict is passed from the layout tool VC, as a static obj in this class.
OnScreenKeyView *keyView = value;
OnScreenButtonState *buttonState = [[OnScreenButtonState alloc] initWithButtonName:keyView.keyString buttonType:KeyboardButton andPosition:keyView.frame.origin];
buttonState.alias = keyView.keyLabel;
buttonState.timestamp = keyView.timestamp;
NSData *buttonStateEncoded = [NSKeyedArchiver archivedDataWithRootObject:buttonState requiringSecureCoding:YES error:nil];
[buttonStatesEncoded addObject: buttonStateEncoded];
}];
if([self getIndexOfSelectedProfile]==0) return false;
OSCProfile *newProfile = [[OSCProfile alloc] initWithName:[self getSelectedProfile].name
buttonStates:buttonStatesEncoded isSelected:YES]; // create a new 'OSCProfile'. Set the array of encoded button states created above to the 'buttonStates' property of the new profile, along with a 'name'. Set 'isSelected' argument to YES which will set this saved profile as the one that will show up in the game stream view
@@ -258,7 +281,9 @@
nil]; */
NSMutableArray *buttonStatesEncoded = [[NSMutableArray alloc] init];
for (CALayer *buttonLayer in buttonLayers) {
OnScreenButtonState *buttonState = [[OnScreenButtonState alloc] initWithButtonName:buttonLayer.name isKeyboardButton:NO isHidden:buttonLayer.isHidden andPosition:buttonLayer.position];
OnScreenButtonState *buttonState = [[OnScreenButtonState alloc] initWithButtonName:buttonLayer.name buttonType:GameControllerButton andPosition:buttonLayer.position];
// add hidden attr here
buttonState.isHidden = buttonLayer.isHidden;
/* //-------------------------------------
NSLog(@"buttonName to be saved: %@", buttonState.name);
if([validPositionButtonNames containsObject:buttonState.name]) buttonState.hasValidPosition = true; // this is for setting up DZ for custom OSC buttons.
@@ -20,12 +20,19 @@ NS_ASSUME_NONNULL_BEGIN
@interface OnScreenButtonState : NSObject <NSCoding, NSSecureCoding>
@property NSString *name;
@property NSString *alias;
@property CGPoint position;
@property (nonatomic, assign) BOOL isHidden;
@property (nonatomic, assign) BOOL isKeyboardButton;
@property (nonatomic, assign) uint8_t buttonType;
@property (nonatomic, assign) NSTimeInterval timestamp;
// @property (nonatomic, assign) BOOL hasValidPosition;
- (id) initWithButtonName:(NSString*)name isKeyboardButton:(BOOL)isKeyboardButton isHidden:(BOOL)isHidden andPosition:(CGPoint)position;
typedef NS_ENUM(NSInteger, OnScreenButtonType) {
GameControllerButton,
KeyboardButton
};
- (id) initWithButtonName:(NSString*)name buttonType:(uint8_t)buttonType andPosition:(CGPoint)position;
+ (BOOL) supportsSecureCoding;
- (void) encodeWithCoder:(NSCoder*)encoder;
@@ -10,12 +10,13 @@
@implementation OnScreenButtonState
- (id) initWithButtonName:(NSString*)name isKeyboardButton:(BOOL)isKeyboardButton isHidden:(BOOL)isHidden andPosition:(CGPoint)position {
- (id) initWithButtonName:(NSString*)name buttonType:(uint8_t)buttonType andPosition:(CGPoint)position {
if ((self = [self init])) {
self.name = name;
self.isHidden = isHidden;
//self.isHidden = isHidden;
self.position = position;
self.isKeyboardButton = isKeyboardButton;
self.buttonType = buttonType;
}
return self;
@@ -27,19 +28,21 @@
- (void) encodeWithCoder:(NSCoder*)encoder {
[encoder encodeObject:self.name forKey:@"name"];
[encoder encodeBool:self.isKeyboardButton forKey:@"isKeyboardButton"];
[encoder encodeBool:self.isHidden forKey:@"isHidden"];
// [encoder encodeBool:self.hasValidPosition forKey:@"hasValidPosition"];
[encoder encodeObject:self.alias forKey:@"alias"];
[encoder encodeDouble:self.timestamp forKey:@"timestamp"];
[encoder encodeInt:self.buttonType forKey:@"buttonType"];
[encoder encodeCGPoint:self.position forKey:@"position"];
[encoder encodeBool:self.isHidden forKey:@"isHidden"];
}
- (id) initWithCoder:(NSCoder*)decoder {
if (self = [super init]) {
self.name = [decoder decodeObjectForKey:@"name"];
self.isKeyboardButton = [decoder decodeBoolForKey:@"isKeyboardButton"];
self.isHidden = [decoder decodeBoolForKey:@"isHidden"];
// self.hasValidPosition = [decoder decodeBoolForKey:@"hasValidPosition"];
self.alias = [decoder decodeObjectForKey:@"alias"];
self.timestamp = [decoder decodeDoubleForKey:@"timestamp"];
self.buttonType = [decoder decodeIntForKey:@"buttonType"];
self.position = [decoder decodeCGPointForKey:@"position"];
self.isHidden = [decoder decodeBoolForKey:@"isHidden"];
}
return self;
+2 -1
View File
@@ -272,7 +272,8 @@ static float L3_Y;
NSLog(@"_activeCustomOscButtonPositionDict update: STARTOVER");
for (NSData *buttonStateEncoded in oscProfile.buttonStates) {
OnScreenButtonState *buttonState = [NSKeyedUnarchiver unarchivedObjectOfClass:[OnScreenButtonState class] fromData:buttonStateEncoded error:nil];
if(!buttonState.isHidden && [validPositionButtonNames containsObject:buttonState.name] ){
if(!buttonState.isHidden && [validPositionButtonNames containsObject:buttonState.name] && (buttonState.buttonType == GameControllerButton || [profilesManager getIndexOfSelectedProfile] == 0 ) ){
[_activeCustomOscButtonPositionDict setObject:[NSValue valueWithCGPoint:buttonState.position] forKey:buttonState.name]; // we got a buttonname -> position dict here
NSLog(@"_activeCustomOscButtonPositionDict update, button name:%@, position: %f, %f", buttonState.name, buttonState.position.x, buttonState.position.y);
}
+46 -14
View File
@@ -8,10 +8,13 @@
import UIKit
@objc class OnScreenKeyView: UIView {
@objc static var relocationMode: Bool = false
@objc var keyLabel: String
@objc var keyString: String
@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
@@ -22,7 +25,7 @@ import UIKit
self.keyLabel = keyLabel
self.label = UILabel()
self.originalBackgroundColor = UIColor(white: 0.2, alpha: 0.7)
self.timestamp = 0;
super.init(frame: .zero)
setupView()
}
@@ -39,16 +42,17 @@ import UIKit
}
@objc public func enableRelocationMode(enabled: Bool){
OnScreenKeyView.relocationMode = enabled
OnScreenKeyView.editMode = enabled
}
private func setupView() {
label.text = self.keyLabel
label.font = UIFont.systemFont(ofSize: 30)
label.font = UIFont.systemFont(ofSize: 22)
label.textColor = UIColor(white: 1.0, alpha: 0.8)
label.shadowColor = .black
label.translatesAutoresizingMaskIntoConstraints = false
self.translatesAutoresizingMaskIntoConstraints = true // this is mandatory to prevent unexpected key view location change
self.layer.borderColor = UIColor.black.cgColor
self.layer.borderWidth = 2
self.layer.cornerRadius = 8
@@ -64,21 +68,24 @@ import UIKit
])
NSLayoutConstraint.activate([
self.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.088),
self.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height * 0.1),
])
//self.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.088),
//self.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height * 0.1),
self.widthAnchor.constraint(equalToConstant: 85),
self.heightAnchor.constraint(equalToConstant: 65),
])
}
// Touch event handling
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
//self.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 1.0, alpha: 0.7)
self.backgroundColor = UIColor.systemBlue.withAlphaComponent(0.7)
if !OnScreenKeyView.relocationMode { LiSendKeyboardEvent(CommandManager.keyMappings[self.keyString]!,Int8(KEY_ACTION_DOWN), 0) }
if !OnScreenKeyView.editMode {
self.backgroundColor = UIColor.systemBlue.withAlphaComponent(0.7)
LiSendKeyboardEvent(CommandManager.keyMappings[self.keyString]!,Int8(KEY_ACTION_DOWN), 0)
}
else {
// Store the initial center point of the key
if let touch = touches.first {
let touchLocation = touch.location(in: superview)
storedLocation = touchLocation
@@ -89,15 +96,17 @@ import UIKit
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
OnScreenKeyView.timestampOfButtonBeingDragged = self.timestamp
// Move the KeyView based on touch movement in relocation mode
if OnScreenKeyView.relocationMode {
if OnScreenKeyView.editMode {
if let touch = touches.first {
let currentLocation = touch.location(in: superview)
let offsetX = currentLocation.x - storedLocation.x
let offsetY = currentLocation.y - storedLocation.y
center = CGPoint(x: center.x + offsetX, y: center.y + offsetY)
//NSLog("x coord: %f, y coord: %f", self.frame.origin.x, self.frame.origin.y)
storedLocation = currentLocation // Update initial center for next movement
}
}
@@ -106,7 +115,30 @@ import UIKit
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
if !OnScreenKeyView.editMode {
LiSendKeyboardEvent(CommandManager.keyMappings[self.keyString]!,Int8(KEY_ACTION_UP), 0)
return
}
self.backgroundColor = self.originalBackgroundColor
if !OnScreenKeyView.relocationMode { LiSendKeyboardEvent(CommandManager.keyMappings[self.keyString]!,Int8(KEY_ACTION_UP), 0) }
guard let superview = superview else { return }
// Deactivate existing constraints if necessary
NSLayoutConstraint.deactivate(self.constraints)
// Add new constraints based on the current center position
translatesAutoresizingMaskIntoConstraints = true
// Create new constraints
let newLeadingConstraint = self.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: self.frame.origin.x)
let newTopConstraint = self.topAnchor.constraint(equalTo: superview.topAnchor, constant: self.frame.origin.y)
// Activate the new location constraints
NSLayoutConstraint.activate([newLeadingConstraint, newTopConstraint])
// Trigger layout update
superview.layoutIfNeeded()
setupView(); //re-setup keyview style
}
}