Skip to content

Commit 0e98ca0

Browse files
committed
feat: allow alternative control layout in app bundle, port v8
1 parent cbd360a commit 0e98ca0

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

Natives/customcontrols/ControlJoystick.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ - (void)callbackMoveX:(CGFloat)xValue Y:(CGFloat)yValue {
180180
}
181181

182182
- (void)setFrame:(CGRect)frame {
183+
// Keep the frame square
184+
if (self.frame.size.width != frame.size.width) {
185+
frame.size.height = frame.size.width;
186+
} else if (self.frame.size.height != frame.size.height) {
187+
frame.size.width = frame.size.height;
188+
}
189+
183190
[super setFrame:frame];
184191
self.mCenter = CGPointMake(frame.size.width/2, frame.size.height/2);
185192
CGFloat minSize = MIN(frame.size.width, frame.size.height);

Natives/customcontrols/CustomControlsUtils.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ static int computeStrokeWidth(float widthInPercent, float width, float height) {
6464
return (int) ((maxSize / 2) * (widthInPercent / 100));
6565
}
6666

67+
// Normalize the layout to v8 from v6/7
68+
void convertV6_7Layout(NSMutableDictionary* dict) {
69+
for (NSMutableDictionary *data in (NSMutableArray *)dict[@"mJoystickDataList"]) {
70+
if ([data[@"height"] floatValue] > [data[@"width"] floatValue]) {
71+
// Make the size square, adjust the dynamic position related to height
72+
CGFloat ratio = [data[@"height"] floatValue] / [data[@"width"] floatValue];
73+
data[@"dynamicX"] = [data[@"dynamicX"] stringByReplacingOccurrencesOfString:@"${height}" withString:[NSString stringWithFormat:@"(%f * ${height})", ratio]];
74+
data[@"dynamicY"] = [data[@"dynamicY"] stringByReplacingOccurrencesOfString:@"${height}" withString:[NSString stringWithFormat:@"(%f * ${height})", ratio]];
75+
data[@"height"] = data[@"width"];
76+
}
77+
}
78+
79+
dict[@"version"] = @(8);
80+
}
81+
6782
void convertV3_4Layout(NSMutableDictionary* dict) {
6883
// Convert the layout stroke width to the V5 form
6984
for (NSMutableDictionary *button in (NSMutableArray *)dict[@"mControlDataList"]) {
@@ -185,6 +200,8 @@ BOOL convertLayoutIfNecessary(NSMutableDictionary* dict) {
185200
break;
186201
case 6:
187202
case 7:
203+
convertV6_7Layout(dict);
204+
case 8:
188205
break;
189206
default:
190207
showDialog(localize(@"custom_controls.control_menu.save.error.json", nil), [NSString stringWithFormat:localize(@"custom_controls.error.incompatible", nil), version]);
@@ -199,6 +216,13 @@ void generateAndSaveDefaultControl() {
199216
return;
200217
}
201218

219+
// New layout is expected to be placed in the app bundle. If not found, use fallback layout
220+
NSString *builtinPath = [NSBundle.mainBundle pathForResource:@"default" ofType:@"json"];
221+
if (builtinPath) {
222+
[NSFileManager.defaultManager copyItemAtPath:builtinPath toPath:defaultPath error:nil];
223+
return;
224+
}
225+
202226
// Generate a v2.7 control
203227
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
204228
dict[@"version"] = @(5);

0 commit comments

Comments
 (0)