diff --git a/Limelight/Images.xcassets/ArrowBackward.imageset/Contents.json b/Limelight/Images.xcassets/ArrowBackward.imageset/Contents.json new file mode 100644 index 00000000..2f5e927f --- /dev/null +++ b/Limelight/Images.xcassets/ArrowBackward.imageset/Contents.json @@ -0,0 +1,25 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "filename" : "arrow_uturn_backward_square-80x80@2x.png", + "idiom" : "universal", + "scale" : "2x" + }, + { + "filename" : "arrow_uturn_backward_square-120x120@3x.png", + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + }, + "properties" : { + "template-rendering-intent" : "template" + } +} diff --git a/Limelight/Images.xcassets/ArrowBackward.imageset/arrow_uturn_backward_square-120x120@3x.png b/Limelight/Images.xcassets/ArrowBackward.imageset/arrow_uturn_backward_square-120x120@3x.png new file mode 100644 index 00000000..00ce8865 Binary files /dev/null and b/Limelight/Images.xcassets/ArrowBackward.imageset/arrow_uturn_backward_square-120x120@3x.png differ diff --git a/Limelight/Images.xcassets/ArrowBackward.imageset/arrow_uturn_backward_square-80x80@2x.png b/Limelight/Images.xcassets/ArrowBackward.imageset/arrow_uturn_backward_square-80x80@2x.png new file mode 100644 index 00000000..71ea3c22 Binary files /dev/null and b/Limelight/Images.xcassets/ArrowBackward.imageset/arrow_uturn_backward_square-80x80@2x.png differ diff --git a/Limelight/Images.xcassets/ArrowBackwardFilled.imageset/Contents.json b/Limelight/Images.xcassets/ArrowBackwardFilled.imageset/Contents.json new file mode 100644 index 00000000..b926c753 --- /dev/null +++ b/Limelight/Images.xcassets/ArrowBackwardFilled.imageset/Contents.json @@ -0,0 +1,25 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "filename" : "arrow_uturn_backward_square_fill-80x80@2x.png", + "idiom" : "universal", + "scale" : "2x" + }, + { + "filename" : "arrow_uturn_backward_square_fill-120x120.png", + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + }, + "properties" : { + "template-rendering-intent" : "template" + } +} diff --git a/Limelight/Images.xcassets/ArrowBackwardFilled.imageset/arrow_uturn_backward_square_fill-120x120.png b/Limelight/Images.xcassets/ArrowBackwardFilled.imageset/arrow_uturn_backward_square_fill-120x120.png new file mode 100644 index 00000000..e5e5a9e4 Binary files /dev/null and b/Limelight/Images.xcassets/ArrowBackwardFilled.imageset/arrow_uturn_backward_square_fill-120x120.png differ diff --git a/Limelight/Images.xcassets/ArrowBackwardFilled.imageset/arrow_uturn_backward_square_fill-80x80@2x.png b/Limelight/Images.xcassets/ArrowBackwardFilled.imageset/arrow_uturn_backward_square_fill-80x80@2x.png new file mode 100644 index 00000000..412403db Binary files /dev/null and b/Limelight/Images.xcassets/ArrowBackwardFilled.imageset/arrow_uturn_backward_square_fill-80x80@2x.png differ diff --git a/Limelight/Images.xcassets/ChevronCompactDown.imageset/Contents.json b/Limelight/Images.xcassets/ChevronCompactDown.imageset/Contents.json new file mode 100644 index 00000000..af6bc379 --- /dev/null +++ b/Limelight/Images.xcassets/ChevronCompactDown.imageset/Contents.json @@ -0,0 +1,25 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "filename" : "chevron_compact_down@2x.png", + "idiom" : "universal", + "scale" : "2x" + }, + { + "filename" : "chevron_compact_down@3x.png", + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + }, + "properties" : { + "template-rendering-intent" : "template" + } +} diff --git a/Limelight/Images.xcassets/ChevronCompactDown.imageset/chevron_compact_down@2x.png b/Limelight/Images.xcassets/ChevronCompactDown.imageset/chevron_compact_down@2x.png new file mode 100644 index 00000000..8e9bba51 Binary files /dev/null and b/Limelight/Images.xcassets/ChevronCompactDown.imageset/chevron_compact_down@2x.png differ diff --git a/Limelight/Images.xcassets/ChevronCompactDown.imageset/chevron_compact_down@3x.png b/Limelight/Images.xcassets/ChevronCompactDown.imageset/chevron_compact_down@3x.png new file mode 100644 index 00000000..faf03285 Binary files /dev/null and b/Limelight/Images.xcassets/ChevronCompactDown.imageset/chevron_compact_down@3x.png differ diff --git a/Limelight/Images.xcassets/ChevronCompactUp.imageset/Contents.json b/Limelight/Images.xcassets/ChevronCompactUp.imageset/Contents.json new file mode 100644 index 00000000..2e6fba06 --- /dev/null +++ b/Limelight/Images.xcassets/ChevronCompactUp.imageset/Contents.json @@ -0,0 +1,25 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "filename" : "chevron_compact_up@2x.png", + "idiom" : "universal", + "scale" : "2x" + }, + { + "filename" : "chevron_compact_up@3x.png", + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + }, + "properties" : { + "template-rendering-intent" : "template" + } +} diff --git a/Limelight/Images.xcassets/ChevronCompactUp.imageset/chevron_compact_up@2x.png b/Limelight/Images.xcassets/ChevronCompactUp.imageset/chevron_compact_up@2x.png new file mode 100644 index 00000000..1ecebbe4 Binary files /dev/null and b/Limelight/Images.xcassets/ChevronCompactUp.imageset/chevron_compact_up@2x.png differ diff --git a/Limelight/Images.xcassets/ChevronCompactUp.imageset/chevron_compact_up@3x.png b/Limelight/Images.xcassets/ChevronCompactUp.imageset/chevron_compact_up@3x.png new file mode 100644 index 00000000..fc287c47 Binary files /dev/null and b/Limelight/Images.xcassets/ChevronCompactUp.imageset/chevron_compact_up@3x.png differ diff --git a/Limelight/Images.xcassets/Contents.json b/Limelight/Images.xcassets/Contents.json index da4a164c..73c00596 100644 --- a/Limelight/Images.xcassets/Contents.json +++ b/Limelight/Images.xcassets/Contents.json @@ -1,6 +1,6 @@ { "info" : { - "version" : 1, - "author" : "xcode" + "author" : "xcode", + "version" : 1 } -} \ No newline at end of file +} diff --git a/Limelight/Images.xcassets/SquareAndArrowDown.imageset/Contents.json b/Limelight/Images.xcassets/SquareAndArrowDown.imageset/Contents.json new file mode 100644 index 00000000..d28b8ee0 --- /dev/null +++ b/Limelight/Images.xcassets/SquareAndArrowDown.imageset/Contents.json @@ -0,0 +1,25 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "filename" : "square_and_arrow_down-80x80@2x.png", + "idiom" : "universal", + "scale" : "2x" + }, + { + "filename" : "square_and_arrow_down-120x120@3x.png", + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + }, + "properties" : { + "template-rendering-intent" : "template" + } +} diff --git a/Limelight/Images.xcassets/SquareAndArrowDown.imageset/square_and_arrow_down-120x120@3x.png b/Limelight/Images.xcassets/SquareAndArrowDown.imageset/square_and_arrow_down-120x120@3x.png new file mode 100644 index 00000000..75f85523 Binary files /dev/null and b/Limelight/Images.xcassets/SquareAndArrowDown.imageset/square_and_arrow_down-120x120@3x.png differ diff --git a/Limelight/Images.xcassets/SquareAndArrowDown.imageset/square_and_arrow_down-80x80@2x.png b/Limelight/Images.xcassets/SquareAndArrowDown.imageset/square_and_arrow_down-80x80@2x.png new file mode 100644 index 00000000..aa21d1b8 Binary files /dev/null and b/Limelight/Images.xcassets/SquareAndArrowDown.imageset/square_and_arrow_down-80x80@2x.png differ diff --git a/Limelight/Images.xcassets/SquaresArrowUp.imageset/Contents.json b/Limelight/Images.xcassets/SquaresArrowUp.imageset/Contents.json new file mode 100644 index 00000000..0ad5a001 --- /dev/null +++ b/Limelight/Images.xcassets/SquaresArrowUp.imageset/Contents.json @@ -0,0 +1,25 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "filename" : "square_and_arrow_up_on_square-80x80@2x.png", + "idiom" : "universal", + "scale" : "2x" + }, + { + "filename" : "square_and_arrow_up_on_square-120x120@3x.png", + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + }, + "properties" : { + "template-rendering-intent" : "template" + } +} diff --git a/Limelight/Images.xcassets/SquaresArrowUp.imageset/square_and_arrow_up_on_square-120x120@3x.png b/Limelight/Images.xcassets/SquaresArrowUp.imageset/square_and_arrow_up_on_square-120x120@3x.png new file mode 100644 index 00000000..de7aa00c Binary files /dev/null and b/Limelight/Images.xcassets/SquaresArrowUp.imageset/square_and_arrow_up_on_square-120x120@3x.png differ diff --git a/Limelight/Images.xcassets/SquaresArrowUp.imageset/square_and_arrow_up_on_square-80x80@2x.png b/Limelight/Images.xcassets/SquaresArrowUp.imageset/square_and_arrow_up_on_square-80x80@2x.png new file mode 100644 index 00000000..85e092db Binary files /dev/null and b/Limelight/Images.xcassets/SquaresArrowUp.imageset/square_and_arrow_up_on_square-80x80@2x.png differ diff --git a/Limelight/Images.xcassets/Trash.imageset/Contents.json b/Limelight/Images.xcassets/Trash.imageset/Contents.json new file mode 100644 index 00000000..67e1d21a --- /dev/null +++ b/Limelight/Images.xcassets/Trash.imageset/Contents.json @@ -0,0 +1,25 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "filename" : "trash_square-80x80@2x.png", + "idiom" : "universal", + "scale" : "2x" + }, + { + "filename" : "trash_square-120x120@3x.png", + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + }, + "properties" : { + "template-rendering-intent" : "template" + } +} diff --git a/Limelight/Images.xcassets/Trash.imageset/trash_square-120x120@3x.png b/Limelight/Images.xcassets/Trash.imageset/trash_square-120x120@3x.png new file mode 100644 index 00000000..6e26af82 Binary files /dev/null and b/Limelight/Images.xcassets/Trash.imageset/trash_square-120x120@3x.png differ diff --git a/Limelight/Images.xcassets/Trash.imageset/trash_square-80x80@2x.png b/Limelight/Images.xcassets/Trash.imageset/trash_square-80x80@2x.png new file mode 100644 index 00000000..f91ed9b3 Binary files /dev/null and b/Limelight/Images.xcassets/Trash.imageset/trash_square-80x80@2x.png differ diff --git a/Limelight/Images.xcassets/TrashFilled.imageset/Contents.json b/Limelight/Images.xcassets/TrashFilled.imageset/Contents.json new file mode 100644 index 00000000..d442915b --- /dev/null +++ b/Limelight/Images.xcassets/TrashFilled.imageset/Contents.json @@ -0,0 +1,22 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "filename" : "trash_square_fill-80x80@2x.png", + "idiom" : "universal", + "scale" : "2x" + }, + { + "filename" : "trash_square_fill-120x120@3x.png", + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Limelight/Images.xcassets/TrashFilled.imageset/trash_square_fill-120x120@3x.png b/Limelight/Images.xcassets/TrashFilled.imageset/trash_square_fill-120x120@3x.png new file mode 100644 index 00000000..ab7ec37f Binary files /dev/null and b/Limelight/Images.xcassets/TrashFilled.imageset/trash_square_fill-120x120@3x.png differ diff --git a/Limelight/Images.xcassets/TrashFilled.imageset/trash_square_fill-80x80@2x.png b/Limelight/Images.xcassets/TrashFilled.imageset/trash_square_fill-80x80@2x.png new file mode 100644 index 00000000..be3b2707 Binary files /dev/null and b/Limelight/Images.xcassets/TrashFilled.imageset/trash_square_fill-80x80@2x.png differ diff --git a/Limelight/Input/Custom OnScreen Controls/LayoutOnScreenControls.h b/Limelight/Input/Custom OnScreen Controls/LayoutOnScreenControls.h new file mode 100644 index 00000000..85f36da6 --- /dev/null +++ b/Limelight/Input/Custom OnScreen Controls/LayoutOnScreenControls.h @@ -0,0 +1,35 @@ +// +// LayoutOnScreenControls.h +// Moonlight +// +// Created by Long Le on 9/26/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import +#import "OnScreenControls.h" +NS_ASSUME_NONNULL_BEGIN + +/** + This object is a subclass of 'OnScreenControls' and adds additional properties and functions which allow the user to drag and drop each of the 19 on screen controller buttons in order to change their positions on screen. The object is used in the 'LayoutOnScreenControlsViewController' thus allowing the user to drag, drop, hide, unhide, on screen controller buttons. + Note that this is in contrast to the game stream view which displays an 'OnScreenControls' object on screen that allows the app to register taps on each button as controller input. It does not (and naturally should not) allow the user to move the buttons around the screen. + */ +@interface LayoutOnScreenControls : OnScreenControls + +@property UIView* _view; +@property NSMutableArray *layoutChanges; +@property CALayer *layerBeingDragged; + +- (id) initWithView:(UIView*)view controllerSup:(ControllerSupport*)controllerSupport + streamConfig:(StreamConfiguration*)streamConfig oscLevel:(int)oscLevel; + +- (CALayer*) buttonLayerFromName:(NSString*)name; +- (BOOL) isLayer:(CALayer*)layer hoveringOverButton:(UIButton*)button; + +- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; +- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; +- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Limelight/Input/Custom OnScreen Controls/LayoutOnScreenControls.m b/Limelight/Input/Custom OnScreen Controls/LayoutOnScreenControls.m new file mode 100644 index 00000000..ffe66a18 --- /dev/null +++ b/Limelight/Input/Custom OnScreen Controls/LayoutOnScreenControls.m @@ -0,0 +1,239 @@ +// +// LayoutOnScreenControls.m +// Moonlight +// +// Created by Long Le on 9/26/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import "LayoutOnScreenControls.h" +#import "OSCProfilesTableViewController.h" +#import "OnScreenButtonState.h" +#import "OSCProfilesManager.h" + + +@interface LayoutOnScreenControls () +@end + +@implementation LayoutOnScreenControls { + + UIButton *trashCanButton; + UIView *horizontalGuideline; + UIView *verticalGuideline; +} + +@synthesize layerBeingDragged; +@synthesize _view; +@synthesize layoutChanges; + +- (id) initWithView:(UIView*)view controllerSup:(ControllerSupport*)controllerSupport streamConfig:(StreamConfiguration*)streamConfig oscLevel:(int)oscLevel { + _view = view; + _view.multipleTouchEnabled = false; + + self = [super initWithView:view controllerSup:controllerSupport streamConfig:streamConfig]; + self._level = oscLevel; + + layoutChanges = [[NSMutableArray alloc] init]; // will contain OSC button layout changes the user has made for this profile + + [self drawButtons]; + [self drawGuidelines]; // add the blue guidelines that appear when button is being tapped and dragged + + return self; +} + +#pragma mark - Drawing + +/** + * This method overrides the superclass's drawButtons method. The purpose of this method is to create a dPad parent layer, and add the four dPad buttons to it so that the user can drag the entire dPad around, directional buttons and all, as one unit as is the expected behavior. Note that we do not want the four dPad buttons to be child layers of a CALayer parent layer on the game stream view since the touch logic implemented for the four dPad buttons on the game stream view is written assuming the dPad buttons are not children of another parent CALayer + */ +- (void) drawButtons { + [super setDPadCenter]; // Set custom position for D-Pad here + [super setAnalogStickPositions]; // Set custom position for analog sticks here + [super drawButtons]; + + UIImage* downButtonImage = [UIImage imageNamed:@"DownButton"]; + UIImage* rightButtonImage = [UIImage imageNamed:@"RightButton"]; + UIImage* upButtonImage = [UIImage imageNamed:@"UpButton"]; + UIImage* leftButtonImage = [UIImage imageNamed:@"LeftButton"]; + + // create dPad background layer + self._dPadBackground = [CALayer layer]; + self._dPadBackground.name = @"dPad"; + self._dPadBackground.frame = CGRectMake(self.D_PAD_CENTER_X, + self.D_PAD_CENTER_Y, + self._leftButton.frame.size.width * 2 + BUTTON_DIST, + self._leftButton.frame.size.width * 2 + BUTTON_DIST); + self._dPadBackground.position = CGPointMake(self.D_PAD_CENTER_X, self.D_PAD_CENTER_Y); // since dPadBackground's dimensions have change after settings its width and height you need to reset its position again here + [self.OSCButtonLayers addObject:self._dPadBackground]; + [_view.layer addSublayer:self._dPadBackground]; + + /* add dPad buttons to parent layer */ + [self._dPadBackground addSublayer:self._downButton]; + [self._dPadBackground addSublayer:self._rightButton]; + [self._dPadBackground addSublayer:self._upButton]; + [self._dPadBackground addSublayer:self._leftButton]; + + /* reposition each dPad button within their parent dPadBackground layer */ + self._downButton.frame = CGRectMake(self._dPadBackground.frame.size.width/3, self._dPadBackground.frame.size.height/2 + D_PAD_DIST, downButtonImage.size.width, downButtonImage.size.height); + self._rightButton.frame = CGRectMake(self._dPadBackground.frame.size.width/2 + D_PAD_DIST, self._dPadBackground.frame.size.height/3, rightButtonImage.size.width, rightButtonImage.size.height); + self._upButton.frame = CGRectMake(self._dPadBackground.frame.size.width/3, 0, upButtonImage.size.width, upButtonImage.size.height); + self._leftButton.frame = CGRectMake(0, self._dPadBackground.frame.size.height/3, leftButtonImage.size.width, leftButtonImage.size.height); +} + +/** + * draws a horizontal and vertical line that is made visible and positioned over whichever button the user is dragging around the screen + */ +- (void) drawGuidelines { + horizontalGuideline = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self._view.frame.size.width * 2, 2)]; + horizontalGuideline.backgroundColor = [UIColor blueColor]; + horizontalGuideline.hidden = YES; + [self._view addSubview: horizontalGuideline]; + + verticalGuideline = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2, self._view.frame.size.height * 2)]; + verticalGuideline.backgroundColor = [UIColor blueColor]; + verticalGuideline.hidden = YES; + [self._view addSubview: verticalGuideline]; +} + + +#pragma mark - Queries + +/* used to determine whether user is dragging an OSC button (of type CALayer) over the trash can with the intent of hiding that button */ +- (BOOL) isLayer:(CALayer *)layer hoveringOverButton:(UIButton *)button { + CGRect buttonConvertedRect = [self._view convertRect:button.imageView.frame fromView:button.superview]; + + if (CGRectIntersectsRect(layer.frame, buttonConvertedRect)) { + return YES; + } + else { + return NO; + } +} + +/* returns reference to button layer object given the button's name*/ +- (CALayer*)buttonLayerFromName: (NSString*)name { + for (CALayer *buttonLayer in self.OSCButtonLayers) { + + if ([buttonLayer.name isEqualToString:name]) { + return buttonLayer; + } + } + return nil; +} + +#pragma mark - Touch + +- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { + /* Reset variables related to on screen controller button drag and drop routine. These variables should be reset in 'touchesCancelled' or 'touchesEnded' but these may not be called in unaccounted-for edge cases such as when the user opens various OS-level control center related views by dragging down from the top of the screen, or dragging up from the bottom of the screen. Since Apple likes to add new control centers and new ways of opening them (i.e. Dynamic Island on iPhone 14 Pro) it's best to reset these variables here when the user is beginning a new on screen controller button drag routine */ + layerBeingDragged = nil; + horizontalGuideline.backgroundColor = [UIColor blueColor]; + verticalGuideline.backgroundColor = [UIColor blueColor]; + + for (UITouch* touch in touches) { // Process touch + + CGPoint touchLocation = [touch locationInView:_view]; + touchLocation = [[touch view] convertPoint:touchLocation toView:nil]; + CALayer *layer = [_view.layer hitTest:touchLocation]; + + /* Don't let user drag and move anything other than on screen controller buttons, which are CALayer types. The reason is that 'LayoutOnScreenControls' should only be responsible for managing and letting users move on screen controller buttons. Since this class's view is currently set to be set equal to the 'LayoutOnScreenControlsViewController' view it belongs to, we need to make sure touches on the VC's objects don't propagate down to 'LayoutOnScreenControls'. Weird stuff can happen to the UI buttons that belong to that VC (trash can button, undo button, save button, etc), such as them being dragged around the screen with the user's touches */ + for (UIView *subview in self._view.subviews) { + + if (CGRectContainsPoint(subview.frame, touchLocation)) { + if (![subview isKindOfClass:[CALayer class]]) { + return; + } + } + } + + if (layer == self._upButton || + layer == self._downButton || + layer == self._leftButton || + layer == self._rightButton) { // don't let user drag individual dPad buttons + layerBeingDragged = self._dPadBackground; + } + else if (layer == self._rightStick) { // only let user drag right stick's background, not the inner analog stick itself + layerBeingDragged = self._rightStickBackground; + } + else if (layer == self._leftStick) { // only let user drag left stick's background, not the inner analog stick itself + layerBeingDragged = self._leftStickBackground; + } + else { // let user drag whatever other valid button they're touching + layerBeingDragged = layer; + } + + /* 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 isHidden:layerBeingDragged.isHidden andPosition:layerBeingDragged.position]; + [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 + + /* make guide lines visible and position them over the button the user is touching */ + horizontalGuideline.center = layerBeingDragged.position; + horizontalGuideline.hidden = NO; + verticalGuideline.center = layerBeingDragged.position; + verticalGuideline.hidden = NO; + } +} + +- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { + UITouch *touch = [touches anyObject]; + CGPoint touchLocation = [touch locationInView:_view]; + + layerBeingDragged.position = touchLocation; // move object to touch location + + /* have guidelines follow wherever the user is touching on the screen */ + horizontalGuideline.center = layerBeingDragged.position; + verticalGuideline.center = layerBeingDragged.position; + + /* + Telegraph to the user whether the horizontal and/or vertical guidelines line up with one or more of the buttons on screen by doing the following: + -Change horizontal guideline color to white if its y-position is almost equal to that of one of the buttons on screen. + -Change vertical guideline color to white if its x-position is almost equal to that of one of the buttons on screen. + */ + for (CALayer *button in self.OSCButtonLayers) { // horizontal guideline position check + + if ((layerBeingDragged != button) && !button.isHidden) { + if ((horizontalGuideline.center.y < button.position.y + 1) && + (horizontalGuideline.center.y > button.position.y - 1)) { + horizontalGuideline.backgroundColor = [UIColor whiteColor]; + break; + } + } + + horizontalGuideline.backgroundColor = [UIColor blueColor]; // change horizontal guideline back to blue if it doesn't line up with one of the on screen buttons + } + for (CALayer *button in self.OSCButtonLayers) { // vertical guideline position check + + if ((layerBeingDragged != button) && !button.isHidden) { + if ((verticalGuideline.center.x < button.position.x + 1) && + (verticalGuideline.center.x > button.position.x - 1)) { + verticalGuideline.backgroundColor = [UIColor whiteColor]; + break; + } + } + + verticalGuideline.backgroundColor = [UIColor blueColor]; // change vertical guideline back to blue if it doesn't line up with one of the on screen buttons + } +} + +- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { + layerBeingDragged = nil; + + horizontalGuideline.hidden = YES; + verticalGuideline.hidden = YES; + horizontalGuideline.backgroundColor = [UIColor blueColor]; + verticalGuideline.backgroundColor = [UIColor blueColor]; +} + +- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { + layerBeingDragged = nil; + + horizontalGuideline.hidden = YES; + verticalGuideline.hidden = YES; + horizontalGuideline.backgroundColor = [UIColor blueColor]; + verticalGuideline.backgroundColor = [UIColor blueColor]; +} + + + + +@end diff --git a/Limelight/Input/Custom OnScreen Controls/OSCProfile.h b/Limelight/Input/Custom OnScreen Controls/OSCProfile.h new file mode 100644 index 00000000..1161b12f --- /dev/null +++ b/Limelight/Input/Custom OnScreen Controls/OSCProfile.h @@ -0,0 +1,32 @@ +// +// OSCProfile.h +// Moonlight +// +// Created by Long Le on 12/22/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import +#import "OnScreenButtonState.h" + +NS_ASSUME_NONNULL_BEGIN + +/** + This object contains information pertaining to any of the user created, custom on screen controller layout configurations, or 'profiles.' The object contains a 'name' property for easy reference, as well as an 'isSelected' property which is used to determine whether this particular custom OSC layout should show on screen during game stream view. Only one 'OSCProfile' is set to 'isSelected' at any given time. The object also contains an array of 'OnScreenButtonStates' which provides information that allows us to move and hide/unhide each of the 19 on screen buttons. Note that the 'buttonStates' property should contain an NSMutableArray of ENCODED 'OnScreenButtonState' objects. This allows us to save the 'OSCProfile' object to NSUserDefaults. + Additionally the 'OSCProfile' object adopts encoding and decoding protocols so that we can encode the object before saving it to NSUserDefaults. By saving this object to NSUserDefaults we allow the user to save and load their custom on screen controller button layouts between app launches + */ +@interface OSCProfile : NSObject + +@property NSString *name; +@property NSMutableArray *buttonStates; +@property BOOL isSelected; + +- (id) initWithName:(NSString*)name buttonStates:(NSMutableArray*)buttonStates isSelected:(BOOL)isSelected; + ++ (BOOL) supportsSecureCoding; +- (id) initWithCoder:(NSCoder*)decoder; +- (void) encodeWithCoder:(NSCoder*)encoder; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Limelight/Input/Custom OnScreen Controls/OSCProfile.m b/Limelight/Input/Custom OnScreen Controls/OSCProfile.m new file mode 100644 index 00000000..43612647 --- /dev/null +++ b/Limelight/Input/Custom OnScreen Controls/OSCProfile.m @@ -0,0 +1,43 @@ +// +// OSCProfile.m +// Moonlight +// +// Created by Long Le on 12/22/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import "OSCProfile.h" + +@implementation OSCProfile + +- (id) initWithName:(NSString*)name buttonStates:(NSMutableArray*)buttonStates isSelected:(BOOL)isSelected { + if ((self = [self init])) { + self.name = name; + self.buttonStates = buttonStates; + self.isSelected = isSelected; + } + + return self; +} + ++ (BOOL) supportsSecureCoding { + return YES; +} + +- (void) encodeWithCoder:(NSCoder*)encoder { + [encoder encodeObject:self.name forKey:@"name"]; + [encoder encodeObject:self.buttonStates forKey:@"buttonStates"]; + [encoder encodeBool:self.isSelected forKey:@"isSelected"]; +} + +- (id) initWithCoder:(NSCoder*)decoder { + if (self = [super init]) { + self.name = [decoder decodeObjectForKey:@"name"]; + self.buttonStates = [decoder decodeObjectForKey:@"buttonStates"]; + self.isSelected = [decoder decodeBoolForKey:@"isSelected"]; + } + + return self; +} + +@end diff --git a/Limelight/Input/Custom OnScreen Controls/OSCProfilesManager.h b/Limelight/Input/Custom OnScreen Controls/OSCProfilesManager.h new file mode 100644 index 00000000..6a2c83bd --- /dev/null +++ b/Limelight/Input/Custom OnScreen Controls/OSCProfilesManager.h @@ -0,0 +1,63 @@ +// +// OSCProfilesManager.h +// Moonlight +// +// Created by Long Le on 1/1/23. +// Copyright © 2023 Moonlight Game Streaming Project. All rights reserved. +// + +#import +#import "OSCProfile.h" + +NS_ASSUME_NONNULL_BEGIN + +/** + This singleton object can be accessed from any class and provides methods to get and set on screen controller profile related data. + Note that the implementation file contains a number of 'Helper' methods. These helper methods are only used in this class's implementation file and help to reduce re-writing large blocks of code that are called multiple times throughout the file + */ +@interface OSCProfilesManager : NSObject + + ++ (OSCProfilesManager *) sharedManager; + +#pragma mark - Getters +/** + * Returns an array of decoded profile objects + */ +- (NSMutableArray *) getAllProfiles; + +/** + * Returns the OSC Profile that is currently selected to be displayed on screen during game streaming + */ +- (OSCProfile *) getSelectedProfile; + +/** + * Returns the index of the 'selected' profile within the array it's in + */ +- (NSInteger) getIndexOfSelectedProfile; + + + + +#pragma mark - Setters +/** + * Sets the profile object with the particular 'name' as the selected profile to be displayed on screen during game streaming + */ +- (void) setProfileToSelected:(NSString *)name; + +/** + * Saves a profile object with a particular 'name' and an array of button layers (the CALayer button layers are the objects currently visible on screen) to persistent storage + */ +- (void) saveProfileWithName:(NSString*)name andButtonLayers:(NSMutableArray *)buttonLayers; + + +#pragma mark - Queries +/** + * Lets the caller of this method know whether a profile with a given name already exists in persistent storage + */ +- (BOOL) profileNameAlreadyExist:(NSString*)name; + + +@end + +NS_ASSUME_NONNULL_END diff --git a/Limelight/Input/Custom OnScreen Controls/OSCProfilesManager.m b/Limelight/Input/Custom OnScreen Controls/OSCProfilesManager.m new file mode 100644 index 00000000..f24f619d --- /dev/null +++ b/Limelight/Input/Custom OnScreen Controls/OSCProfilesManager.m @@ -0,0 +1,215 @@ +// +// OSCProfilesManager.m +// Moonlight +// +// Created by Long Le on 1/1/23. +// Copyright © 2023 Moonlight Game Streaming Project. All rights reserved. +// + +#import "OSCProfilesManager.h" + +@implementation OSCProfilesManager + +#pragma mark - Initializer + ++ (OSCProfilesManager *) sharedManager { + static OSCProfilesManager *_sharedManager = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + _sharedManager = [[self alloc] init]; + }); + return _sharedManager; +} + + +#pragma mark - Class Helper Methods + +/** + * Returns the profile whose 'name' property has a value that is equal to the 'name' passed into the method + */ +- (OSCProfile *) OSCProfileWithName:(NSString*)name { + NSMutableArray *profiles = [self getAllProfiles]; + + for (OSCProfile *profile in profiles) { + + if ([profile.name isEqualToString:name]) { + return profile; + } + } + + return nil; +} + +/** + * Returns an array of encoded 'OSCProfile' objects from the array of decoded 'OSCProfile' objects passed into this method + */ +- (NSMutableArray *) encodedProfilesFromArray:(NSMutableArray *)profiles { + + NSMutableArray *profilesEncoded = [[NSMutableArray alloc] init]; + /* encode each profile and add them back into an array */ + for (OSCProfile *profile in profiles) { // + + NSData *profileEncoded = [NSKeyedArchiver archivedDataWithRootObject:profile requiringSecureCoding:YES error:nil]; + [profilesEncoded addObject:profileEncoded]; + } + + return profilesEncoded; +} + +/** + * Replaces one 'OSCProfile' object for another in the 'OSCProfile' objects array stored in persistent storage + */ +- (void) replaceProfile:(OSCProfile*)oldProfile withProfile:(OSCProfile*)newProfile { + 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; + } + + /* Set the new profile as the selected one. The reasoning behind this is that this method is currently being used when the user saves over an existing profile with another profile that has the same name. The expected behavior is that the newly saved profile becomes the selected profile which will show on screen when they launch the game stream view */ + newProfile.isSelected = YES; + + /* Remove the old profile from the array and insert the new profile into its place */ + int index = 0; + for (int i = 0; i < profiles.count; i++) { + + if ([[profiles[i] name] isEqualToString: oldProfile.name]) { + index = i; + } + } + [profiles removeObjectAtIndex:index]; + [profiles insertObject:newProfile atIndex:index]; + + NSMutableArray *profilesEncoded = [self encodedProfilesFromArray:profiles]; // encode each 'profile' object in the array and add them to a new array + + /* Encode the array itself, NOT the objects in the array, which are already encoded. Save array to persistent storage */ + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profilesEncoded requiringSecureCoding:YES error:nil]; + [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"OSCProfiles"]; + [[NSUserDefaults standardUserDefaults] synchronize]; +} + + +#pragma mark - Globally Accessible Methods + +#pragma mark - Getters + +- (NSMutableArray *) getAllProfiles { + + NSData *profilesArrayEncoded = [[NSUserDefaults standardUserDefaults] objectForKey: @"OSCProfiles"]; // Get the encoded array of encoded OSC profiles from persistent storage + NSSet *classes = [NSSet setWithObjects:[NSString class], [NSMutableData class], [NSMutableArray class], [OSCProfile class], [OnScreenButtonState class], nil]; + + NSMutableArray *profilesEncoded = [NSKeyedUnarchiver unarchivedObjectOfClasses:classes fromData:profilesArrayEncoded error:nil]; // Decode the encoded array itself, NOT the objects contained in the array + + /* Decode each of the encoded profiles, place them into an array, then return the array */ + NSMutableArray *profilesDecoded = [[NSMutableArray alloc] init]; + OSCProfile *profileDecoded; + for (NSData *profileEncoded in profilesEncoded) { + + profileDecoded = [NSKeyedUnarchiver unarchivedObjectOfClasses: classes fromData:profileEncoded error: nil]; + [profilesDecoded addObject: profileDecoded]; + } + return profilesDecoded; +} + +- (OSCProfile *) getSelectedProfile { + NSMutableArray *profiles = [self getAllProfiles]; + + for (OSCProfile *profile in profiles) { + + if (profile.isSelected) { + return profile; + } + } + return nil; +} + +- (NSInteger) getIndexOfSelectedProfile { + NSMutableArray *profiles = [self getAllProfiles]; + for (OSCProfile *profile in profiles) { + + if (profile.isSelected == YES) { + return [profiles indexOfObject:profile]; + } + } + return 0; // if none of the profiles in the array have their 'isSelected' property set to YES (which should not be possible) return the 'Default' profile as the 'selected' profile +} + + +#pragma mark - Setters + +- (void) setProfileToSelected:(NSString *)name { + NSMutableArray *profiles = [self getAllProfiles]; + + /* Iterate through each profile. If its name equals the value of the 'name' parameter passed into this method then set the profile's 'isSelected' property to YES, otherwise set the value to NO */ + for (OSCProfile *profile in profiles) { + + if ([profile.name isEqualToString:name]) { + profile.isSelected = YES; + } + else { + profile.isSelected = NO; + } + } + + NSMutableArray *profilesEncoded = [self encodedProfilesFromArray:profiles]; // encode each 'profile' object in the array and add them to a new array + + /* Encode the array itself, NOT the objects inside the array, which have already been encoded by this point */ + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profilesEncoded requiringSecureCoding:YES error:nil]; + [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"OSCProfiles"]; + [[NSUserDefaults standardUserDefaults] synchronize]; +} + +- (void) saveProfileWithName:(NSString*)name andButtonLayers:(NSMutableArray *)buttonLayers { + /* 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 */ + NSMutableArray *buttonStatesEncoded = [[NSMutableArray alloc] init]; + for (CALayer *buttonLayer in buttonLayers) { + + OnScreenButtonState *buttonState = [[OnScreenButtonState alloc] initWithButtonName:buttonLayer.name isHidden:buttonLayer.isHidden andPosition:buttonLayer.position]; + NSData *buttonStateEncoded = [NSKeyedArchiver archivedDataWithRootObject:buttonState requiringSecureCoding:YES error:nil]; + [buttonStatesEncoded addObject: buttonStateEncoded]; + } + + OSCProfile *newProfile = [[OSCProfile alloc] initWithName: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 + + + /* set all saved OSCProfiles 'isSelected' property to NO since the new profile you're adding will be set as the selected profile */ + NSMutableArray *profiles = [self getAllProfiles]; + for (OSCProfile *profile in profiles) { + + profile.isSelected = NO; + } + + if ([self profileNameAlreadyExist:name]) { // if a saved profile with the same 'name' already exists in persistent storage then overwrite it and save the change to persistent storage + [self replaceProfile:[self OSCProfileWithName:name] withProfile:newProfile]; + } + else { // otherwise encode then add the new profile to the end of the OSCProfiles array + NSData *newProfileEncoded = [NSKeyedArchiver archivedDataWithRootObject:newProfile requiringSecureCoding:YES error:nil]; + NSMutableArray *profilesEncoded = [self encodedProfilesFromArray:profiles]; + [profilesEncoded addObject:newProfileEncoded]; + + /* Encode the 'profilesEncoded' array itself, NOT the objects in the 'profilesEncoded' array, all of which are already encoded by this point */ + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profilesEncoded requiringSecureCoding:YES error:nil]; + [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"OSCProfiles"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + } +} + +#pragma mark - Queries + +- (BOOL) profileNameAlreadyExist:(NSString*)name { + NSMutableArray *profiles = [self getAllProfiles]; + + /* Iterate through the decoded profiles and return 'YES' if one of the profiles' 'name' properties equals the 'name' passed into this method */ + for (OSCProfile *profile in profiles) { + + if ([profile.name isEqualToString:name]) { + return YES; + } + } + + return NO; +} + +@end diff --git a/Limelight/Input/Custom OnScreen Controls/OnScreenButtonState.h b/Limelight/Input/Custom OnScreen Controls/OnScreenButtonState.h new file mode 100644 index 00000000..a08402ba --- /dev/null +++ b/Limelight/Input/Custom OnScreen Controls/OnScreenButtonState.h @@ -0,0 +1,32 @@ +// +// OnScreenButtonState.h +// Moonlight +// +// Created by Long Le on 10/20/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +/** + This object is used to save positional and visibility information for any particular on screen virtual controller button. + We are able to associate this 'OnScreenButtonState' object and its corresponding CALayer onscreen controller button by setting their 'name' properties equal; in our particular case we give them descriptive names such as 'aButton', 'upButton', 'leftStick', etc. By keeping references to the 19 on screen controller buttons (CALayers) in an array, and creating 19 'OnScreenButtonState' objects with names corresponding to these 19 CALayers and keeping them in an array, we can iterate through both arrays to find OSC buttons (CALayers) with the same name as one of the 'OnScreenButtonStateObjects' and then set the CALayer on screen control button's 'position' and 'hidden' property according to the value of the 'OnScreenButtonState' objects 'position' and 'isHidden' properties. + Naturally we would like the user to be able to save their controller layout configurations so that they can load them between app launches, so we adopt encoding/decoding related protocols so we encode these 'OnScreenButtonState' objects and save them to NSUserDefaults + */ +@interface OnScreenButtonState : NSObject + +@property NSString *name; +@property CGPoint position; +@property BOOL isHidden; + +- (id) initWithButtonName:(NSString*)name isHidden:(BOOL)isHidden andPosition:(CGPoint)position; + ++ (BOOL) supportsSecureCoding; +- (void) encodeWithCoder:(NSCoder*)encoder; +- (id) initWithCoder:(NSCoder*)decoder; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Limelight/Input/Custom OnScreen Controls/OnScreenButtonState.m b/Limelight/Input/Custom OnScreen Controls/OnScreenButtonState.m new file mode 100644 index 00000000..a6dca53c --- /dev/null +++ b/Limelight/Input/Custom OnScreen Controls/OnScreenButtonState.m @@ -0,0 +1,43 @@ +// +// OnScreenButtonState.m +// Moonlight +// +// Created by Long Le on 10/20/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import "OnScreenButtonState.h" + +@implementation OnScreenButtonState + +- (id) initWithButtonName:(NSString*)name isHidden:(BOOL)isHidden andPosition:(CGPoint)position { + if ((self = [self init])) { + self.name = name; + self.isHidden = isHidden; + self.position = position; + } + + return self; +} + ++ (BOOL) supportsSecureCoding { + return YES; +} + +- (void) encodeWithCoder:(NSCoder*)encoder { + [encoder encodeObject:self.name forKey:@"name"]; + [encoder encodeBool:self.isHidden forKey:@"isHidden"]; + [encoder encodeCGPoint:self.position forKey:@"position"]; +} + +- (id) initWithCoder:(NSCoder*)decoder { + if (self = [super init]) { + self.name = [decoder decodeObjectForKey:@"name"]; + self.isHidden = [decoder decodeBoolForKey:@"isHidden"]; + self.position = [decoder decodeCGPointForKey:@"position"]; + } + + return self; +} + +@end diff --git a/Limelight/Input/OnScreenControls.h b/Limelight/Input/OnScreenControls.h index 29997de3..af33862e 100644 --- a/Limelight/Input/OnScreenControls.h +++ b/Limelight/Input/OnScreenControls.h @@ -7,10 +7,15 @@ // #import +#import "ControllerSupport.h" +#import "OSCProfile.h" @class ControllerSupport; @class StreamConfiguration; +static const float D_PAD_DIST = 10; +static const float BUTTON_DIST = 20; + @interface OnScreenControls : NSObject typedef NS_ENUM(NSInteger, OnScreenControlsLevel) { @@ -18,6 +23,7 @@ typedef NS_ENUM(NSInteger, OnScreenControlsLevel) { OnScreenControlsLevelAuto, OnScreenControlsLevelSimple, OnScreenControlsLevelFull, + OnScreenControlsCustom, // Internal levels selected by ControllerSupport OnScreenControlsLevelAutoGCGamepad, @@ -25,12 +31,47 @@ typedef NS_ENUM(NSInteger, OnScreenControlsLevel) { OnScreenControlsLevelAutoGCExtendedGamepadWithStickButtons }; +@property CALayer* _aButton; +@property CALayer* _bButton; +@property CALayer* _xButton; +@property CALayer* _yButton; +@property CALayer* _startButton; +@property CALayer* _selectButton; +@property CALayer* _r1Button; +@property CALayer* _r2Button; +@property CALayer* _r3Button; +@property CALayer* _l1Button; +@property CALayer* _l2Button; +@property CALayer* _l3Button; +@property CALayer* _upButton; +@property CALayer* _downButton; +@property CALayer* _leftButton; +@property CALayer* _rightButton; +@property CALayer* _leftStickBackground; +@property CALayer* _leftStick; +@property CALayer* _rightStickBackground; +@property CALayer* _rightStick; +@property CALayer *_dPadBackground; // parent layer that contains each individual dPad button so user can drag them around the screen together + +@property float D_PAD_CENTER_X; +@property float D_PAD_CENTER_Y; + +@property OnScreenControlsLevel _level; + +@property NSMutableArray *OSCButtonLayers; + - (id) initWithView:(UIView*)view controllerSup:(ControllerSupport*)controllerSupport streamConfig:(StreamConfiguration*)streamConfig; - (BOOL) handleTouchDownEvent:(NSSet*)touches; - (BOOL) handleTouchUpEvent:(NSSet*)touches; - (BOOL) handleTouchMovedEvent:(NSSet*)touches; - (void) setLevel:(OnScreenControlsLevel)level; -- (OnScreenControlsLevel) getLevel; - (void) show; +- (void) setupComplexControls; +- (void) drawButtons; +- (void) updateControls; +- (OnScreenControlsLevel) getLevel; +- (void) setDPadCenter; +- (void) setAnalogStickPositions; +- (void) positionOSCButtons; @end diff --git a/Limelight/Input/OnScreenControls.m b/Limelight/Input/OnScreenControls.m index 667de2d6..cdfcdefb 100644 --- a/Limelight/Input/OnScreenControls.m +++ b/Limelight/Input/OnScreenControls.m @@ -8,34 +8,17 @@ #import "OnScreenControls.h" #import "StreamView.h" -#import "ControllerSupport.h" #import "Controller.h" #include "Limelight.h" +#import "OnScreenButtonState.h" +#import "OSCProfilesManager.h" + #define UPDATE_BUTTON(x, y) (buttonFlags = \ (y) ? (buttonFlags | (x)) : (buttonFlags & ~(x))) @implementation OnScreenControls { - CALayer* _aButton; - CALayer* _bButton; - CALayer* _xButton; - CALayer* _yButton; - CALayer* _upButton; - CALayer* _downButton; - CALayer* _leftButton; - CALayer* _rightButton; - CALayer* _leftStickBackground; - CALayer* _leftStick; - CALayer* _rightStickBackground; - CALayer* _rightStick; - CALayer* _startButton; - CALayer* _selectButton; - CALayer* _r1Button; - CALayer* _r2Button; - CALayer* _r3Button; - CALayer* _l1Button; - CALayer* _l2Button; - CALayer* _l3Button; + UITouch* _aTouch; UITouch* _bTouch; @@ -62,25 +45,47 @@ @implementation OnScreenControls { BOOL _iPad; CGRect _controlArea; UIView* _view; - OnScreenControlsLevel _level; BOOL _visible; ControllerSupport *_controllerSupport; Controller *_controller; NSMutableArray* _deadTouches; BOOL _swapABXY; + OSCProfilesManager *profilesManager; } +@synthesize D_PAD_CENTER_X; +@synthesize D_PAD_CENTER_Y; +@synthesize _leftStickBackground; +@synthesize _leftStick; +@synthesize _rightStickBackground; +@synthesize _rightStick; +@synthesize _upButton; +@synthesize _downButton; +@synthesize _leftButton; +@synthesize _rightButton; +@synthesize _aButton; +@synthesize _bButton; +@synthesize _xButton; +@synthesize _yButton; +@synthesize _startButton; +@synthesize _selectButton; +@synthesize _r1Button; +@synthesize _r2Button; +@synthesize _r3Button; +@synthesize _l1Button; +@synthesize _l2Button; +@synthesize _l3Button; +@synthesize _level; +@synthesize OSCButtonLayers; +@synthesize _dPadBackground; + static const float EDGE_WIDTH = .05; //static const float BUTTON_SIZE = 50; -static const float BUTTON_DIST = 20; static float BUTTON_CENTER_X; static float BUTTON_CENTER_Y; -static const float D_PAD_DIST = 10; -static float D_PAD_CENTER_X; -static float D_PAD_CENTER_Y; static const float DEAD_ZONE_PADDING = 15; @@ -115,10 +120,19 @@ @implementation OnScreenControls { - (id) initWithView:(UIView*)view controllerSup:(ControllerSupport*)controllerSupport streamConfig:(StreamConfiguration*)streamConfig { self = [self init]; _view = view; - _controllerSupport = controllerSupport; + + profilesManager = [OSCProfilesManager sharedManager]; + + self.OSCButtonLayers = [[NSMutableArray alloc] init]; + + if (controllerSupport) { + _controllerSupport = controllerSupport; + } _controller = [controllerSupport getOscController]; _deadTouches = [[NSMutableArray alloc] init]; - _swapABXY = streamConfig.swapABXYButtons; + if (streamConfig) { + _swapABXY = streamConfig.swapABXYButtons; + } _iPad = ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad); _controlArea = CGRectMake(0, 0, _view.frame.size.width, _view.frame.size.height); @@ -133,7 +147,7 @@ - (id) initWithView:(UIView*)view controllerSup:(ControllerSupport*)controllerSu _controlArea.origin.x = _controlArea.size.width * EDGE_WIDTH; _controlArea.size.width -= _controlArea.origin.x * 2; } - + _aButton = [CALayer layer]; _bButton = [CALayer layer]; _xButton = [CALayer layer]; @@ -154,6 +168,49 @@ - (id) initWithView:(UIView*)view controllerSup:(ControllerSupport*)controllerSu _rightStickBackground = [CALayer layer]; _leftStick = [CALayer layer]; _rightStick = [CALayer layer]; + + [self.OSCButtonLayers addObject:_aButton]; + [self.OSCButtonLayers addObject:_bButton]; + [self.OSCButtonLayers addObject:_xButton]; + [self.OSCButtonLayers addObject:_yButton]; + [self.OSCButtonLayers addObject:_startButton]; + [self.OSCButtonLayers addObject:_selectButton]; + [self.OSCButtonLayers addObject:_r1Button]; + [self.OSCButtonLayers addObject:_r2Button]; + [self.OSCButtonLayers addObject:_r3Button]; + [self.OSCButtonLayers addObject:_l1Button]; + [self.OSCButtonLayers addObject:_l2Button]; + [self.OSCButtonLayers addObject:_l3Button]; + [self.OSCButtonLayers addObject:_upButton]; + [self.OSCButtonLayers addObject:_downButton]; + [self.OSCButtonLayers addObject:_leftButton]; + [self.OSCButtonLayers addObject:_rightButton]; + [self.OSCButtonLayers addObject:_leftStickBackground]; + [self.OSCButtonLayers addObject:_rightStickBackground]; + [self.OSCButtonLayers addObject:_leftStick]; + [self.OSCButtonLayers addObject:_rightStick]; + + /* Name button layers to allow us to more easily associate them with 'OnScreenButtonState' objects by comparing their name properties */ + _leftStickBackground.name = @"leftStickBackground"; + _rightStickBackground.name = @"rightStickBackground"; + _leftStick.name = @"leftStick"; + _rightStick.name = @"rightStick"; + _aButton.name = @"aButton"; + _bButton.name = @"bButton"; + _xButton.name = @"xButton"; + _yButton.name = @"yButton"; + _startButton.name = @"startButton"; + _selectButton.name = @"selectButton"; + _r1Button.name = @"r1Button"; + _r2Button.name = @"r2Button"; + _r3Button.name = @"r3Button"; + _l1Button.name = @"l1Button"; + _l2Button.name = @"l2Button"; + _l3Button.name = @"l3Button"; + _upButton.name = @"upButton"; + _rightButton.name = @"rightButton"; + _downButton.name = @"downButton"; + _leftButton.name = @"leftButton"; return self; } @@ -179,7 +236,7 @@ - (OnScreenControlsLevel) getLevel { } - (void) updateControls { - switch (_level) { + switch (self._level) { case OnScreenControlsLevelOff: [self hideButtons]; [self hideBumpers]; @@ -224,8 +281,8 @@ - (void) updateControls { [self hideSticks]; break; case OnScreenControlsLevelSimple: + [self setupSimpleControls]; - [self hideTriggers]; [self hideL3R3]; [self hideBumpers]; @@ -234,14 +291,27 @@ - (void) updateControls { [self drawButtons]; break; case OnScreenControlsLevelFull: - [self setupComplexControls]; + [self setupComplexControls]; [self drawButtons]; [self drawStartSelect]; [self drawBumpers]; [self drawTriggers]; [self drawSticks]; [self hideL3R3]; // Full controls don't need these they have the sticks + break; + case OnScreenControlsCustom: + + [self setupComplexControls]; // Default postion for D-Pad set here + [self setDPadCenter]; // Custom position for D-Pad set here + [self setAnalogStickPositions]; // Custom position for analog sticks set here + [self drawButtons]; + [self drawStartSelect]; + [self drawBumpers]; + [self drawTriggers]; + [self drawSticks]; + [self positionOSCButtons]; + break; default: Log(LOG_W, @"Unknown on-screen controls level: %d", (int)_level); @@ -408,27 +478,96 @@ - (void) drawButtons { UIImage* downButtonImage = [UIImage imageNamed:@"DownButton"]; _downButton.frame = CGRectMake(D_PAD_CENTER_X - downButtonImage.size.width / 2, D_PAD_CENTER_Y + D_PAD_DIST, downButtonImage.size.width, downButtonImage.size.height); _downButton.contents = (id) downButtonImage.CGImage; + _downButton.frame = CGRectMake(D_PAD_CENTER_X - downButtonImage.size.width / 2, D_PAD_CENTER_Y + D_PAD_DIST, downButtonImage.size.width, downButtonImage.size.height); [_view.layer addSublayer:_downButton]; // create Right button UIImage* rightButtonImage = [UIImage imageNamed:@"RightButton"]; _rightButton.frame = CGRectMake(D_PAD_CENTER_X + D_PAD_DIST, D_PAD_CENTER_Y - rightButtonImage.size.height / 2, rightButtonImage.size.width, rightButtonImage.size.height); _rightButton.contents = (id) rightButtonImage.CGImage; + _rightButton.frame = CGRectMake(D_PAD_CENTER_X + D_PAD_DIST, D_PAD_CENTER_Y - rightButtonImage.size.height / 2, rightButtonImage.size.width, rightButtonImage.size.height); [_view.layer addSublayer:_rightButton]; // create Up button UIImage* upButtonImage = [UIImage imageNamed:@"UpButton"]; _upButton.frame = CGRectMake(D_PAD_CENTER_X - upButtonImage.size.width / 2, D_PAD_CENTER_Y - D_PAD_DIST - upButtonImage.size.height, upButtonImage.size.width, upButtonImage.size.height); _upButton.contents = (id) upButtonImage.CGImage; + _upButton.frame = CGRectMake(D_PAD_CENTER_X - upButtonImage.size.width / 2, D_PAD_CENTER_Y - D_PAD_DIST - upButtonImage.size.height, upButtonImage.size.width, upButtonImage.size.height); [_view.layer addSublayer:_upButton]; // create Left button UIImage* leftButtonImage = [UIImage imageNamed:@"LeftButton"]; _leftButton.frame = CGRectMake(D_PAD_CENTER_X - D_PAD_DIST - leftButtonImage.size.width, D_PAD_CENTER_Y - leftButtonImage.size.height / 2, leftButtonImage.size.width, leftButtonImage.size.height); _leftButton.contents = (id) leftButtonImage.CGImage; + _leftButton.frame = CGRectMake(D_PAD_CENTER_X - D_PAD_DIST - leftButtonImage.size.width, D_PAD_CENTER_Y - leftButtonImage.size.height / 2, leftButtonImage.size.width, leftButtonImage.size.height); [_view.layer addSublayer:_leftButton]; } +/** + * Sets D-Pad position for class const var + */ +- (void) setDPadCenter { + 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.name isEqualToString:@"dPad"]) { + D_PAD_CENTER_X = buttonState.position.x; + D_PAD_CENTER_Y = buttonState.position.y; + } + } +} + +/** + * Sets analog stick positions for class const var + */ +- (void) setAnalogStickPositions { + 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.name isEqualToString:@"leftStickBackground"]) { + LS_CENTER_X = buttonState.position.x; + LS_CENTER_Y = buttonState.position.y; + } + if ([buttonState.name isEqualToString:@"rightStickBackground"]) { + RS_CENTER_X = buttonState.position.x; + RS_CENTER_Y = buttonState.position.y; + } + } +} + +/** + * Loads the selected OSC profile and lays out each button associated with that profile onto the screen + */ +- (void) positionOSCButtons { + OSCProfile *oscProfile = [profilesManager getSelectedProfile]; + + for (NSData *buttonStateEncoded in oscProfile.buttonStates) { + + OnScreenButtonState *buttonStateDecoded = [NSKeyedUnarchiver unarchivedObjectOfClass:[OnScreenButtonState class] fromData:buttonStateEncoded error:nil]; + + for (CALayer *buttonLayer in self.OSCButtonLayers) { // iterate through each button layer on screen and position and hide/unhide each according to the instructions of its associated 'buttonState' + + if ([buttonLayer.name isEqualToString:buttonStateDecoded.name]) { + + if ([buttonLayer.name isEqualToString:@"upButton"] == NO && + [buttonLayer.name isEqualToString:@"rightButton"] == NO && + [buttonLayer.name isEqualToString:@"downButton"] == NO && + [buttonLayer.name isEqualToString:@"leftButton"] == NO && + [buttonLayer.name isEqualToString:@"leftStick"] == NO && + [buttonLayer.name isEqualToString:@"rightStick"] == NO) { // Don't move these buttons since they've already been positioned correctly in the 'drawButtons' and 'drawSticks' methods called before this method is called. The 'buttonStateDecoded' object associated with these buttons contains positions relative to a parent CALayer which only exists on 'LayoutOnScreenControls'. These positions relative to the parent layers would translate incorrectly when placed on the game stream VC's 'view' layer + + buttonLayer.position = buttonStateDecoded.position; + } + buttonLayer.hidden = buttonStateDecoded.isHidden; + } + } + } +} + - (void) drawStartSelect { // create Start button UIImage* startButtonImage = [UIImage imageNamed:@"StartButton"]; @@ -982,4 +1121,5 @@ - (BOOL) isDeadZone:(UITouch*) touch startX:(float)deadZoneStartX startY:(float) } + @end diff --git a/Limelight/ViewControllers/Custom OnScreen Controls/Cells/ProfileTableViewCell.h b/Limelight/ViewControllers/Custom OnScreen Controls/Cells/ProfileTableViewCell.h new file mode 100644 index 00000000..ee758a07 --- /dev/null +++ b/Limelight/ViewControllers/Custom OnScreen Controls/Cells/ProfileTableViewCell.h @@ -0,0 +1,19 @@ +// +// ProfileTableViewCell.h +// Moonlight +// +// Created by Long Le on 12/11/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface ProfileTableViewCell : UITableViewCell + +@property (weak, nonatomic) IBOutlet UILabel *name; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Limelight/ViewControllers/Custom OnScreen Controls/Cells/ProfileTableViewCell.m b/Limelight/ViewControllers/Custom OnScreen Controls/Cells/ProfileTableViewCell.m new file mode 100644 index 00000000..f1388f47 --- /dev/null +++ b/Limelight/ViewControllers/Custom OnScreen Controls/Cells/ProfileTableViewCell.m @@ -0,0 +1,21 @@ +// +// ProfileTableViewCell.m +// Moonlight +// +// Created by Long Le on 12/11/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import "ProfileTableViewCell.h" + +@implementation ProfileTableViewCell + +- (void)awakeFromNib { + [super awakeFromNib]; +} + +- (void)setSelected:(BOOL)selected animated:(BOOL)animated { + [super setSelected:selected animated:animated]; +} + +@end diff --git a/Limelight/ViewControllers/Custom OnScreen Controls/Cells/ProfileTableViewCell.xib b/Limelight/ViewControllers/Custom OnScreen Controls/Cells/ProfileTableViewCell.xib new file mode 100644 index 00000000..f32dd8f4 --- /dev/null +++ b/Limelight/ViewControllers/Custom OnScreen Controls/Cells/ProfileTableViewCell.xib @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Limelight/ViewControllers/Custom OnScreen Controls/LayoutOnScreenControlsViewController.h b/Limelight/ViewControllers/Custom OnScreen Controls/LayoutOnScreenControlsViewController.h new file mode 100644 index 00000000..acbaa5d3 --- /dev/null +++ b/Limelight/ViewControllers/Custom OnScreen Controls/LayoutOnScreenControlsViewController.h @@ -0,0 +1,36 @@ +// +// LayoutOnScreenControlsViewController.h +// Moonlight +// +// Created by Long Le on 9/27/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import +#import "LayoutOnScreenControls.h" +#import "ToolBarContainerView.h" + +NS_ASSUME_NONNULL_BEGIN + +/** + This view controller provides the user interface which allows the user to position on screen controller buttons anywhere they'd like on the screen. It also provides the user with the abilities to undo a change, save the on screen controller layout for later retrieval, and load previously saved controller layouts + */ +@interface LayoutOnScreenControlsViewController : UIViewController + + +@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 int OSCSegmentSelected; + +@property (weak, nonatomic) IBOutlet UIButton *trashCanButton; +@property (weak, nonatomic) IBOutlet UIButton *undoButton; + +@property (weak, nonatomic) IBOutlet ToolBarContainerView *toolbarRootView; +@property (weak, nonatomic) IBOutlet UIView *chevronView; +@property (weak, nonatomic) IBOutlet UIImageView *chevronImageView; +@property (weak, nonatomic) IBOutlet UIStackView *toolbarStackView; + + + +@end + +NS_ASSUME_NONNULL_END diff --git a/Limelight/ViewControllers/Custom OnScreen Controls/LayoutOnScreenControlsViewController.m b/Limelight/ViewControllers/Custom OnScreen Controls/LayoutOnScreenControlsViewController.m new file mode 100644 index 00000000..da7dcb7f --- /dev/null +++ b/Limelight/ViewControllers/Custom OnScreen Controls/LayoutOnScreenControlsViewController.m @@ -0,0 +1,358 @@ +// +// LayoutOnScreenControlsViewController.m +// Moonlight +// +// Created by Long Le on 9/27/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import "LayoutOnScreenControlsViewController.h" +#import "OSCProfilesTableViewController.h" +#import "OnScreenButtonState.h" +#import "OnScreenControls.h" +#import "OSCProfilesManager.h" + +@interface LayoutOnScreenControlsViewController () + +@end + + +@implementation LayoutOnScreenControlsViewController { + BOOL isToolbarHidden; + OSCProfilesManager *profilesManager; + __weak IBOutlet NSLayoutConstraint *toolbarTopConstraintiPhone; + __weak IBOutlet NSLayoutConstraint *toolbarTopConstraintiPad; +} + +@synthesize trashCanButton; +@synthesize undoButton; +@synthesize OSCSegmentSelected; +@synthesize toolbarRootView; +@synthesize chevronView; +@synthesize chevronImageView; + +- (void) viewDidLoad { + [super viewDidLoad]; + + profilesManager = [OSCProfilesManager sharedManager]; + + 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 + + /* add curve to bottom of chevron tab view */ + UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.chevronView.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)]; + CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; + maskLayer.frame = self.view.bounds; + maskLayer.path = maskPath.CGPath; + self.chevronView.layer.mask = maskLayer; + + /* Add swipe gesture to toolbar to allow user to swipe it up and off screen */ + UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(moveToolbar:)]; + swipeUp.direction = UISwipeGestureRecognizerDirectionUp; + [self.toolbarRootView addGestureRecognizer:swipeUp]; + + /* Add tap gesture to toolbar's chevron to allow user to tap it in order to move the toolbar on and off screen */ + UITapGestureRecognizer *singleFingerTap = + [[UITapGestureRecognizer alloc] initWithTarget:self + action:@selector(moveToolbar:)]; + [self.chevronView addGestureRecognizer:singleFingerTap]; + + self.layoutOSC = [[LayoutOnScreenControls alloc] initWithView:self.view controllerSup:nil streamConfig:nil oscLevel:OSCSegmentSelected]; + self.layoutOSC._level = 4; + [self.layoutOSC show]; // draw on screen controls + + [self addInnerAnalogSticksToOuterAnalogLayers]; // allows inner and analog sticks to be dragged together around the screen together as one unit which is the expected behavior + + self.undoButton.alpha = 0.3; // no changes to undo yet, so fade out the undo button a bit + + if ([[profilesManager getAllProfiles] count] == 0) { // if no saved OSC profiles exist yet then create one called 'Default' and associate it with Moonlight's legacy 'Full' OSC layout that's already been laid out on the screen at this point + [profilesManager saveProfileWithName:@"Default" andButtonLayers:self.layoutOSC.OSCButtonLayers]; + } + + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OSCLayoutChanged) name:@"OSCLayoutChanged" object:nil]; // used to notifiy this view controller that the user made a change to the OSC layout so that the VC can either fade in or out its 'Undo button' which will signify to the user whether there are any OSC layout changes to undo + + /* This will animate the toolbar with a subtle up and down motion intended to telegraph to the user that they can hide the toolbar if they wish*/ + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + + [UIView animateWithDuration:0.3 + delay:0.25 + usingSpringWithDamping:0.8 + initialSpringVelocity:0.5 + options:UIViewAnimationOptionCurveEaseInOut animations:^{ // Animate toolbar up a a very small distance. Note the 0.35 time delay is necessary to avoid a bug that keeps animations from playing if the animation is presented immediately on a modally presented VC + self.toolbarRootView.frame = CGRectMake(self.toolbarRootView.frame.origin.x, self.toolbarRootView.frame.origin.y - 25, self.toolbarRootView.frame.size.width, self.toolbarRootView.frame.size.height); + } + completion:^(BOOL finished) { + [UIView animateWithDuration:0.3 + delay:0 + usingSpringWithDamping:0.7 + initialSpringVelocity:1.0 + options:UIViewAnimationOptionCurveEaseIn animations:^{ // Animate the toolbar back down that same distance + self.toolbarRootView.frame = CGRectMake(self.toolbarRootView.frame.origin.x, self.toolbarRootView.frame.origin.y + 25, self.toolbarRootView.frame.size.width, self.toolbarRootView.frame.size.height); + } + completion:^(BOOL finished) { + NSLog (@"done"); + }]; + }]; + }); +} + + +#pragma mark - Class Helper Functions + +/* fades the 'Undo Button' in or out depending on whether the user has any OSC layout changes to undo */ +- (void) OSCLayoutChanged { + if ([self.layoutOSC.layoutChanges count] > 0) { + self.undoButton.alpha = 1.0; + } + else { + self.undoButton.alpha = 0.3; + } +} + +/* animates the toolbar up and off the screen or back down onto the screen */ +- (void) moveToolbar:(UISwipeGestureRecognizer *)sender { + BOOL isPad = [[UIDevice currentDevice].model hasPrefix:@"iPad"]; + NSLayoutConstraint *toolbarTopConstraint = isPad ? self->toolbarTopConstraintiPad : self->toolbarTopConstraintiPhone; + if (isToolbarHidden == NO) { + [UIView animateWithDuration:0.2 animations:^{ // animates toolbar up and off screen + toolbarTopConstraint.constant -= self.toolbarRootView.frame.size.height; + [self.view layoutIfNeeded]; + + } + completion:^(BOOL finished) { + if (finished) { + self->isToolbarHidden = YES; + self.chevronImageView.image = [UIImage imageNamed:@"ChevronCompactDown"]; + } + }]; + } + else { + [UIView animateWithDuration:0.2 animations:^{ // animates the toolbar back down into the screen + toolbarTopConstraint.constant += self.toolbarRootView.frame.size.height; + [self.view layoutIfNeeded]; + } + completion:^(BOOL finished) { + if (finished) { + self->isToolbarHidden = NO; + self.chevronImageView.image = [UIImage imageNamed:@"ChevronCompactUp"]; + } + }]; + } +} + +/** + * Makes the inner analog stick layers a child layer of its corresponding outer analog stick layers so that both the inner and its corresponding outer layers move together when the user drags them around the screen as is the expected behavior when laying out OSC. Note that this is NOT expected behavior on the game stream view where the inner analog sticks move to follow toward the user's touch and their corresponding outer analog stick layers do not move + */ +- (void)addInnerAnalogSticksToOuterAnalogLayers { + // right stick + [self.layoutOSC._rightStickBackground addSublayer: self.layoutOSC._rightStick]; + self.layoutOSC._rightStick.position = CGPointMake(self.layoutOSC._rightStickBackground.frame.size.width / 2, self.layoutOSC._rightStickBackground.frame.size.height / 2); + + // left stick + [self.layoutOSC._leftStickBackground addSublayer: self.layoutOSC._leftStick]; + self.layoutOSC._leftStick.position = CGPointMake(self.layoutOSC._leftStickBackground.frame.size.width / 2, self.layoutOSC._leftStickBackground.frame.size.height / 2); +} + + +#pragma mark - UIButton Actions + +- (IBAction) closeTapped:(id)sender { + [self dismissViewControllerAnimated:YES completion:nil]; +} + +- (IBAction) trashCanTapped:(id)sender { + UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Delete Buttons Here" message:@"Drag and drop buttons onto this trash can to remove them from the interface" preferredStyle:UIAlertControllerStyleAlert]; + + UIAlertAction *ok = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:nil]; + [alert addAction:ok]; + [self presentViewController:alert animated:YES completion:nil]; +} + +- (IBAction) undoTapped:(id)sender { + if ([self.layoutOSC.layoutChanges count] > 0) { // check if there are layout changes to roll back to + OnScreenButtonState *buttonState = [self.layoutOSC.layoutChanges lastObject]; // Get the 'OnScreenButtonState' object that contains the name, position, and visiblity state of the button the user last moved + + CALayer *buttonLayer = [self.layoutOSC buttonLayerFromName:buttonState.name]; // get the on screen button layer that corresponds with the 'OnScreenButtonState' object that we retrieved above + + /* Set the button's position and visiblity to what it was before the user last moved it */ + buttonLayer.position = buttonState.position; + buttonLayer.hidden = buttonState.isHidden; + + /* if user is showing or hiding dPad, then show or hide all four dPad button child layers as well since setting the 'hidden' property on the parent CALayer is not automatically setting the individual dPad child CALayers */ + if ([buttonLayer.name isEqualToString:@"dPad"]) { + self.layoutOSC._upButton.hidden = buttonState.isHidden; + self.layoutOSC._rightButton.hidden = buttonState.isHidden; + self.layoutOSC._downButton.hidden = buttonState.isHidden; + self.layoutOSC._leftButton.hidden = buttonState.isHidden; + } + + /* if user is showing or hiding the left or right analog sticks, then show or hide their corresponding inner analog stick child layers as well since setting the 'hidden' property on the parent analog stick doesn't automatically hide its child inner analog stick CALayer */ + if ([buttonLayer.name isEqualToString:@"leftStickBackground"]) { + self.layoutOSC._leftStick.hidden = buttonState.isHidden; + } + if ([buttonLayer.name isEqualToString:@"rightStickBackground"]) { + self.layoutOSC._rightStick.hidden = buttonState.isHidden; + } + + [self.layoutOSC.layoutChanges removeLastObject]; + + [self OSCLayoutChanged]; // will fade the undo button in or out depending on whether there are any further changes to undo + } + else { // there are no changes to undo. let user know there are no changes to undo + UIAlertController * savedAlertController = [UIAlertController alertControllerWithTitle: [NSString stringWithFormat:@"Nothing to Undo"] message: @"There are no changes to undo" preferredStyle:UIAlertControllerStyleAlert]; + [savedAlertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { + [savedAlertController dismissViewControllerAnimated:NO completion:nil]; + }]]; + [self presentViewController:savedAlertController animated:YES completion:nil]; + } +} + +/* show pop up notification that lets users choose to save the current OSC layout configuration as a profile they can load when they want. User can also choose to cancel out of this pop up */ +- (IBAction) saveTapped:(id)sender { + UIAlertController * inputNameAlertController = [UIAlertController alertControllerWithTitle: @"Enter the name you want to save this controller profile as" message: @"" preferredStyle:UIAlertControllerStyleAlert]; + [inputNameAlertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { // pop up notification with text field where user can enter the text they wish to name their OSC layout profile + textField.placeholder = @"name"; + textField.textColor = [UIColor lightGrayColor]; + textField.clearButtonMode = UITextFieldViewModeWhileEditing; + textField.borderStyle = UITextBorderStyleNone; + }]; + [inputNameAlertController addAction:[UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // add save button to allow user to save the on screen controller configuration + NSArray *textFields = inputNameAlertController.textFields; + UITextField *nameField = textFields[0]; + NSString *enteredProfileName = nameField.text; + + if ([enteredProfileName isEqualToString:@"Default"]) { // don't let user user overwrite the 'Default' profile + UIAlertController *alertController = [UIAlertController alertControllerWithTitle: [NSString stringWithFormat:@""] message: [NSString stringWithFormat:@"Saving over the 'Default' profile is not allowed"] preferredStyle:UIAlertControllerStyleAlert]; + + [alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { + [alertController dismissViewControllerAnimated:NO completion:^{ + [self presentViewController:inputNameAlertController animated:YES completion:nil]; + }]; + }]]; + [self presentViewController:alertController animated:YES completion:nil]; + } + else if ([enteredProfileName length] == 0) { // if user entered no text and taps the 'Save' button let them know they can't do that + UIAlertController * savedAlertController = [UIAlertController alertControllerWithTitle: [NSString stringWithFormat:@""] message: [NSString stringWithFormat:@"Profile name cannot be blank!"] preferredStyle:UIAlertControllerStyleAlert]; + + [savedAlertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // show pop up notification letting user know they must enter a name in the text field if they wish to save the controller profile + + [savedAlertController dismissViewControllerAnimated:NO completion:^{ + [self presentViewController:inputNameAlertController animated:YES completion:nil]; + }]; + }]]; + [self presentViewController:savedAlertController animated:YES completion:nil]; + } + else 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 actionWithTitle:@"No" 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:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { + [savedAlertController dismissViewControllerAnimated:NO completion:nil]; + }]]; + [self presentViewController:savedAlertController animated:YES completion:nil]; + } + }]]; + [inputNameAlertController addAction:[UIAlertAction actionWithTitle:@"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]; +} + +/* Presents the view controller that lists all OSC profiles the user can choose from */ +- (IBAction) loadTapped:(id)sender { + UIStoryboard *storyboard; + BOOL isIPhone = ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone); + if (isIPhone) { + storyboard = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil]; + } + else { + storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil]; + } + + OSCProfilesTableViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"OSCProfilesTableViewController"] ; + + vc.didDismissOSCProfilesTVC = ^() { // a block that will be called when the modally presented 'OSCProfilesTableViewController' VC is dismissed. By the time the 'OSCProfilesTableViewController' VC is dismissed the user would have potentially selected a different OSC profile with a different layout and they want to see this layout on this 'LayoutOnScreenControlsViewController.' This block of code will load the profile and then hide/show and move each OSC button to their appropriate position + [self.layoutOSC updateControls]; // creates and saves a 'Default' OSC profile or loads the one the user selected on the previous screen + + [self addInnerAnalogSticksToOuterAnalogLayers]; + + [self.layoutOSC.layoutChanges removeAllObjects]; // since a new OSC profile is being loaded, this will remove all previous layout changes made from the array + + [self OSCLayoutChanged]; // fades the 'Undo Button' out + }; + [self presentViewController:vc animated:YES completion:nil]; +} + + +#pragma mark - Touch + +- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { + for (UITouch* touch in touches) { + + CGPoint touchLocation = [touch locationInView:self.view]; + touchLocation = [[touch view] convertPoint:touchLocation toView:nil]; + CALayer *layer = [self.view.layer hitTest:touchLocation]; + + if (layer == self.toolbarRootView.layer || + layer == self.chevronView.layer || + layer == self.chevronImageView.layer || + layer == self.toolbarStackView.layer || + layer == self.view.layer) { // don't let user move toolbar or toolbar UI buttons, toolbar's chevron 'pull tab', or the layer associated with this VC's view + return; + } + } + [self.layoutOSC touchesBegan:touches withEvent:event]; +} + +- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { + [self.layoutOSC touchesMoved:touches withEvent:event]; + + if ([self.layoutOSC isLayer:self.layoutOSC.layerBeingDragged + hoveringOverButton:trashCanButton]) { // check if user is dragging around a button and hovering it over the trash can button + trashCanButton.tintColor = [UIColor redColor]; + } + else { + trashCanButton.tintColor = [UIColor colorWithRed:171.0/255.0 green:157.0/255.0 blue:255.0/255.0 alpha:1]; + } +} + +- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { + if (self.layoutOSC.layerBeingDragged != nil && + [self.layoutOSC isLayer:self.layoutOSC.layerBeingDragged hoveringOverButton:trashCanButton]) { // check if user wants to throw OSC button into the trash can + + self.layoutOSC.layerBeingDragged.hidden = YES; + + if ([self.layoutOSC.layerBeingDragged.name isEqualToString:@"dPad"]) { // if user is hiding dPad, then hide all four dPad button child layers as well since setting the 'hidden' property on the parent dPad CALayer doesn't automatically hide the four child CALayer dPad buttons + self.layoutOSC._upButton.hidden = YES; + self.layoutOSC._rightButton.hidden = YES; + self.layoutOSC._downButton.hidden = YES; + self.layoutOSC._leftButton.hidden = YES; + } + + /* if user is hiding left or right analog sticks, then hide their corresponding inner analog stick child layers as well since setting the 'hidden' property on the parent analog stick doesn't automatically hide its child inner analog stick CALayer */ + if ([self.layoutOSC.layerBeingDragged.name isEqualToString:@"leftStickBackground"]) { + self.layoutOSC._leftStick.hidden = YES; + } + if ([self.layoutOSC.layerBeingDragged.name isEqualToString:@"rightStickBackground"]) { + self.layoutOSC._rightStick.hidden = YES; + } + + trashCanButton.tintColor = [UIColor colorWithRed:171.0/255.0 green:157.0/255.0 blue:255.0/255.0 alpha:1]; + } + [self.layoutOSC touchesEnded:touches withEvent:event]; +} + +@end diff --git a/Limelight/ViewControllers/Custom OnScreen Controls/OSCProfilesTableViewController.h b/Limelight/ViewControllers/Custom OnScreen Controls/OSCProfilesTableViewController.h new file mode 100644 index 00000000..448d0f87 --- /dev/null +++ b/Limelight/ViewControllers/Custom OnScreen Controls/OSCProfilesTableViewController.h @@ -0,0 +1,24 @@ +// +// OSCProfilesTableViewController.h +// Moonlight +// +// Created by Long Le on 11/28/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +/** + This view displays a list of on screen controller profiles and gives the user the ability to select any of the profiles to be the 'Selected' profile whose on screen controller layout configuration will be shown on the game stream view, or in the on screen controller layout view. This view also allows the user to swipe and delete any of the listed profiles. + */ +@interface OSCProfilesTableViewController : UIViewController + +@property (weak, nonatomic) IBOutlet UITableView *tableView; +@property (nonatomic, copy) void (^didDismissOSCProfilesTVC)(void); + + +@end + +NS_ASSUME_NONNULL_END diff --git a/Limelight/ViewControllers/Custom OnScreen Controls/OSCProfilesTableViewController.m b/Limelight/ViewControllers/Custom OnScreen Controls/OSCProfilesTableViewController.m new file mode 100644 index 00000000..6d1c09a8 --- /dev/null +++ b/Limelight/ViewControllers/Custom OnScreen Controls/OSCProfilesTableViewController.m @@ -0,0 +1,158 @@ +// +// OSCProfilesTableViewController.m +// Moonlight +// +// Created by Long Le on 11/28/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import "OSCProfilesTableViewController.h" +#import "LayoutOnScreenControlsViewController.h" +#import "ProfileTableViewCell.h" +#import "OSCProfile.h" +#import "OnScreenButtonState.h" +#import "OSCProfilesManager.h" + +const double NAV_BAR_HEIGHT = 50; + +@interface OSCProfilesTableViewController () + +@end + +@implementation OSCProfilesTableViewController { + OSCProfilesManager *profilesManager; +} + +@synthesize tableView; + +- (void) viewDidLoad { + [super viewDidLoad]; + + profilesManager = [OSCProfilesManager sharedManager]; + + self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, NAV_BAR_HEIGHT)]; + + self.tableView.delegate = self; + self.tableView.dataSource = self; + + [self.tableView registerNib:[UINib nibWithNibName:@"ProfileTableViewCell" bundle:nil] + forCellReuseIdentifier:@"Cell"]; // Register the custom cell nib file with the table view + +} + +- (void) viewDidAppear:(BOOL)animated { + [super viewDidAppear: animated]; + + if ([[profilesManager getAllProfiles] count] > 0) { // scroll to selected profile if user has any saved profiles + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[profilesManager getIndexOfSelectedProfile] inSection:0]; + [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; + } +} + + +#pragma mark - UIButton Actions + +/* Loads the OSC profile that user selected, dismisses this VC, then tells the presenting view controller to lay out the on screen buttons according to the selected profile's instructions */ +- (IBAction) loadTapped:(id)sender { + [self dismissViewControllerAnimated:YES completion:nil]; + + if (self.didDismissOSCProfilesTVC) { // tells the presenting view controller to lay out the on screen buttons according to the selected profile's instructions + self.didDismissOSCProfilesTVC(); + } +} + +- (IBAction) cancelTapped:(id)sender { + [self dismissViewControllerAnimated:YES completion:nil]; +} + + +#pragma mark - TableView DataSource + +- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { + return [[profilesManager getAllProfiles] count]; +} + +- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { + ProfileTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; + OSCProfile *profile = [[profilesManager getAllProfiles] objectAtIndex: indexPath.row]; + cell.name.text = profile.name; + + if ([profile.name isEqualToString: [profilesManager getSelectedProfile].name]) { // if this cell contains the name of the currently selected OSC profile then add a checkmark to the right side of the cell + cell.accessoryType = UITableViewCellAccessoryCheckmark; + } + else { + cell.accessoryType = UITableViewCellAccessoryNone; + } + return cell; +} + +- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { + return YES; +} + +- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { + NSMutableArray *profiles = [profilesManager getAllProfiles]; + + if ([[[profiles objectAtIndex:indexPath.row] name] isEqualToString:@"Default"]) { // if user is attempting to delete the 'Default' profile then show a pop up telling user they can't do that and return out of this method + UIAlertController *alertController = [UIAlertController alertControllerWithTitle: [NSString stringWithFormat:@""] message: @"Deleting the 'Default' profile is not allowed" preferredStyle:UIAlertControllerStyleAlert]; + + [alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { + [alertController dismissViewControllerAnimated:NO completion:nil]; + }]]; + [self presentViewController:alertController animated:YES completion:nil]; + + return; + } + + if (editingStyle == UITableViewCellEditingStyleDelete) { + OSCProfile *profile = [profiles objectAtIndex:indexPath.row]; + if (profile.isSelected) { // if user is deleting the currently selected OSC profile then make the profile at its previous index the currently selected profile + if (indexPath.row > 0) { // check that row is greater than zero to avoid an out of bounds crash, although that should not be possible right now since the 'Default' profile is always at row 0 and they're not allowed to delete it + OSCProfile *profile = [profiles objectAtIndex:indexPath.row - 1]; + profile.isSelected = YES; + } + } + + [profiles removeObjectAtIndex:indexPath.row]; + + /* save OSC profiles array to persistent storage */ + NSMutableArray *profilesEncoded = [[NSMutableArray alloc] init]; + for (OSCProfile *profileDecoded in profiles) { // encode each OSC profile object and add them to an array + + NSData *profileEncoded = [NSKeyedArchiver archivedDataWithRootObject:profileDecoded requiringSecureCoding:YES error:nil]; + [profilesEncoded addObject:profileEncoded]; + } + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profilesEncoded + requiringSecureCoding:YES error:nil]; // encode the array itself, NOT the objects in the array + [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"OSCProfiles"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + + [tableView reloadData]; + } +} + + +#pragma mark - TableView Delegate + +/* When user taps a cell it moves the checkmark to that cell indicating to the user the profile associated with that cell is now the selected profile. It also sets that cell's associated OSCProfile object's 'isSelected' property to YES */ +- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath +{ + NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; + NSIndexPath *lastSelectedIndexPath = [NSIndexPath indexPathForRow:[profilesManager getIndexOfSelectedProfile] inSection:0]; + + if (selectedIndexPath != lastSelectedIndexPath) { + /* Place checkmark on selected cell and set profile associated with cell as selected profile */ + UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath: selectedIndexPath]; + selectedCell.accessoryType = UITableViewCellAccessoryCheckmark; // add checkmark to the cell the user tapped + OSCProfile *profile = [[profilesManager getAllProfiles] objectAtIndex:indexPath.row]; + [profilesManager setProfileToSelected: profile.name]; // set the profile associated with this cell's 'isSelected' property to YES + + /* Remove checkmark on the previously selected cell */ + UITableViewCell *lastSelectedCell = [tableView cellForRowAtIndexPath: lastSelectedIndexPath]; + lastSelectedCell.accessoryType = UITableViewCellAccessoryNone; + [tableView deselectRowAtIndexPath:lastSelectedIndexPath animated:YES]; + } +} + + +@end diff --git a/Limelight/ViewControllers/Custom OnScreen Controls/ToolBarContainerView.h b/Limelight/ViewControllers/Custom OnScreen Controls/ToolBarContainerView.h new file mode 100644 index 00000000..a6063db2 --- /dev/null +++ b/Limelight/ViewControllers/Custom OnScreen Controls/ToolBarContainerView.h @@ -0,0 +1,18 @@ +// +// ToolBarContainerView.h +// Moonlight +// +// Created by Long Le on 12/10/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +/* Allows the ToolBar in the 'LayoutOnScreenControlsViewController' to accept touches on the small UIView 'tab' protruding from its bottom center*/ +@interface ToolBarContainerView : UIView + +@end + +NS_ASSUME_NONNULL_END diff --git a/Limelight/ViewControllers/Custom OnScreen Controls/ToolBarContainerView.m b/Limelight/ViewControllers/Custom OnScreen Controls/ToolBarContainerView.m new file mode 100644 index 00000000..c08fc758 --- /dev/null +++ b/Limelight/ViewControllers/Custom OnScreen Controls/ToolBarContainerView.m @@ -0,0 +1,26 @@ +// +// ToolBarContainerView.m +// Moonlight +// +// Created by Long Le on 12/10/22. +// Copyright © 2022 Moonlight Game Streaming Project. All rights reserved. +// + +#import "ToolBarContainerView.h" + +@implementation ToolBarContainerView + +- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event { + + for (UIView *view in self.subviews) { + + if (!view.hidden && view.alpha > 0 && + view.userInteractionEnabled && + [view pointInside:[self convertPoint:point toView:view] withEvent:event]) + return YES; + } + + return NO; +} + +@end diff --git a/Limelight/ViewControllers/SettingsViewController.h b/Limelight/ViewControllers/SettingsViewController.h index af3caff0..f3859b28 100644 --- a/Limelight/ViewControllers/SettingsViewController.h +++ b/Limelight/ViewControllers/SettingsViewController.h @@ -8,6 +8,7 @@ #import #import "AppDelegate.h" +#import "LayoutOnScreenControlsViewController.h" @interface SettingsViewController : UIViewController @property (strong, nonatomic) IBOutlet UILabel *bitrateLabel; @@ -26,6 +27,7 @@ @property (strong, nonatomic) IBOutlet UISegmentedControl *btMouseSelector; @property (strong, nonatomic) IBOutlet UISegmentedControl *statsOverlaySelector; @property (strong, nonatomic) IBOutlet UIScrollView *scrollView; +@property (strong, nonatomic) LayoutOnScreenControlsViewController *layoutOnScreenControlsVC; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunguarded-availability" diff --git a/Limelight/ViewControllers/SettingsViewController.m b/Limelight/ViewControllers/SettingsViewController.m index 49be8632..1928acb5 100644 --- a/Limelight/ViewControllers/SettingsViewController.m +++ b/Limelight/ViewControllers/SettingsViewController.m @@ -13,9 +13,12 @@ #import #import + @implementation SettingsViewController { NSInteger _bitrate; NSInteger _lastSelectedResolutionIndex; + NSInteger previouslySelectedSegmentIndex; + UITapGestureRecognizer *tapGesture; } @dynamic overrideUserInterfaceStyle; @@ -81,11 +84,12 @@ -(void)viewDidLayoutSubviews { // indicators. Ignore any views we don't recognize. if (![view isKindOfClass:[UILabel class]] && ![view isKindOfClass:[UISegmentedControl class]] && - ![view isKindOfClass:[UISlider class]]) { + ![view isKindOfClass:[UISlider class]] && + ![view isKindOfClass:[UIButton class]]) { continue; } - CGFloat currentViewY = view.frame.origin.y + view.frame.size.height; + CGFloat currentViewY = view.frame.origin.y; if (currentViewY > highestViewY) { highestViewY = currentViewY; } @@ -258,6 +262,22 @@ - (void)viewDidLoad { [self.bitrateSlider addTarget:self action:@selector(bitrateSliderMoved) forControlEvents:UIControlEventValueChanged]; [self updateBitrateText]; [self updateCustomResolutionText]; + + tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OSCSegmentControlReselected:)]; // detects when OSC segmented control button is re-selected + [self.onscreenControlSelector addGestureRecognizer:tapGesture]; + + /* sets a reference to the correct 'LayoutOnScreenControlsViewController' depending on whether the user is on an iPhone or iPad */ + self.layoutOnScreenControlsVC = [[LayoutOnScreenControlsViewController alloc] init]; + BOOL isIPhone = ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone); + if (isIPhone) { + UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil]; + self.layoutOnScreenControlsVC = [storyboard instantiateViewControllerWithIdentifier:@"LayoutOnScreenControlsViewController"]; + } + else { + UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil]; + self.layoutOnScreenControlsVC = [storyboard instantiateViewControllerWithIdentifier:@"LayoutOnScreenControlsViewController"]; + self.layoutOnScreenControlsVC.modalPresentationStyle = UIModalPresentationFullScreen; + } } - (void) touchModeChanged { @@ -497,9 +517,60 @@ - (void)didReceiveMemoryWarning { #pragma mark - Navigation +/* detects a tap on the 'Custom' segment of the OnScreen Controls 'UISegmentControl' object. It will load the appropriate VC if the 'Custom' segment is tapped */ +- (IBAction)OSCSegmentedControlsTapped:(id)sender { + UISegmentedControl *segmentedControl = (UISegmentedControl*) sender; + + LayoutOnScreenControlsViewController *vc = [[LayoutOnScreenControlsViewController alloc] init]; + BOOL isIPhone = ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone); + if (isIPhone) { + UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil]; + vc = [storyboard instantiateViewControllerWithIdentifier:@"LayoutOnScreenControlsViewController"]; + } + else { + UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil]; + vc = [storyboard instantiateViewControllerWithIdentifier:@"LayoutOnScreenControlsViewController"]; + vc.modalPresentationStyle = UIModalPresentationFullScreen; + } -- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { + switch ([segmentedControl selectedSegmentIndex]) { + case 0: + break; + case 1: + break; + case 2: + break; + case 3: + break; + case 4: + if (self.layoutOnScreenControlsVC.isBeingPresented == NO) { + [self presentViewController:self.layoutOnScreenControlsVC animated:YES completion:nil]; + } + break; + } } +/* Used to detect when user taps on an on screen control UISegmentedControl button, even if that button is already selected */ +- (void)OSCSegmentControlReselected:(id)sender { + + CGPoint point = [tapGesture locationInView:self.onscreenControlSelector]; + NSUInteger segmentSize = self.onscreenControlSelector.bounds.size.width / self.onscreenControlSelector.numberOfSegments; + + + NSUInteger touchedSegment = point.x / segmentSize; // Warning: If you are using segments not equally sized, you have to adapt the code here + + if (self.onscreenControlSelector.selectedSegmentIndex != touchedSegment) { // normal behavior, previously unselected button is selected + self.onscreenControlSelector.selectedSegmentIndex = touchedSegment; + + } else { // already selected button is tapped again + if (self.onscreenControlSelector.selectedSegmentIndex == 4) { + if (self.layoutOnScreenControlsVC.isBeingPresented == NO) { + [self presentViewController:self.layoutOnScreenControlsVC animated:YES completion:nil]; + } + } + } + + [self OSCSegmentedControlsTapped:self.onscreenControlSelector]; // must call selector because the UIControlEventValueChanged can't work together with UITapGestureRecognizer +} @end diff --git a/iPad.storyboard b/Limelight/iPad.storyboard similarity index 67% rename from iPad.storyboard rename to Limelight/iPad.storyboard index d0f87e2d..b563e4e3 100644 --- a/iPad.storyboard +++ b/Limelight/iPad.storyboard @@ -1,9 +1,9 @@ - + - + @@ -141,16 +141,20 @@ - + + + + + + + + + + + + + + + + diff --git a/iPhone.storyboard b/Limelight/iPhone.storyboard similarity index 64% rename from iPhone.storyboard rename to Limelight/iPhone.storyboard index 312542e6..24a35654 100644 --- a/iPhone.storyboard +++ b/Limelight/iPhone.storyboard @@ -1,9 +1,10 @@ - + - + + @@ -12,7 +13,7 @@ - + @@ -82,7 +83,7 @@ - + - + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -189,14 +221,14 @@ - + @@ -206,14 +238,14 @@ - + @@ -223,14 +255,14 @@ - + @@ -240,14 +272,14 @@ - + @@ -257,14 +289,14 @@ - + @@ -274,14 +306,14 @@ - + @@ -291,42 +323,16 @@ - - - - - - - - - - - - - - - - - - - - - + @@ -348,14 +354,220 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -406,7 +618,7 @@ - + @@ -418,7 +630,20 @@ - + + + + + + + + + + + + + +