Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/PojavLauncher_iOS
Path: blob/main/Natives/CustomControlsViewController+UndoManager.m
589 views
1
#import "CustomControlsViewController.h"
2
#import "customcontrols/ControlDrawer.h"
3
#import "customcontrols/ControlJoystick.h"
4
#import "customcontrols/ControlLayout.h"
5
#import "utils.h"
6
7
@implementation CustomControlsViewController(UndoManager)
8
9
- (void)doAddButton:(ControlButton *)button atIndex:(NSNumber *)index {
10
NSUndoManager *undo = self.undoManager;
11
12
if ([button isKindOfClass:ControlSubButton.class]) {
13
undo.actionName = localize(@"custom_controls.button_menu.add_subbutton", nil);
14
15
[self.ctrlView addSubview:button];
16
ControlDrawer *drawer = ((ControlSubButton *)button).parentDrawer;
17
[drawer.buttons insertObject:button atIndex:index.intValue];
18
[drawer.drawerData[@"buttonProperties"] insertObject:button.properties atIndex:index.intValue];
19
[drawer syncButtons];
20
} else if ([button isKindOfClass:ControlDrawer.class]) {
21
undo.actionName = localize(@"custom_controls.control_menu.add_drawer", nil);
22
23
for (ControlSubButton *subButton in ((ControlDrawer *)button).buttons) {
24
[self.ctrlView addSubview:subButton];
25
}
26
[self.ctrlView.layoutDictionary[@"mDrawerDataList"] insertObject:((ControlDrawer *)button).drawerData atIndex:index.intValue];
27
[self.ctrlView addSubview:button];
28
} else if ([button isKindOfClass:ControlJoystick.class]) {
29
undo.actionName = localize(@"custom_controls.control_menu.add_joystick", nil);
30
[self.ctrlView.layoutDictionary[@"mJoystickDataList"] insertObject:button.properties atIndex:index.intValue];
31
[self.ctrlView addSubview:button];
32
} else {
33
undo.actionName = localize(@"custom_controls.control_menu.add_button", nil);
34
[self.ctrlView.layoutDictionary[@"mControlDataList"] insertObject:button.properties atIndex:index.intValue];
35
[self.ctrlView addSubview:button];
36
}
37
38
[[undo prepareWithInvocationTarget:self] doRemoveButton:button];
39
if (undo.isUndoing) {
40
undo.actionName = localize(@"Remove", nil);
41
}
42
}
43
44
- (void)doRemoveButton:(ControlButton *)button {
45
NSNumber *index;
46
if ([button isKindOfClass:ControlSubButton.class]) {
47
ControlDrawer *parent = ((ControlSubButton *)button).parentDrawer;
48
index = @([parent.drawerData[@"buttonProperties"] indexOfObject:button.properties]);
49
[parent.buttons removeObject:button];
50
[parent.drawerData[@"buttonProperties"] removeObject:button.properties];
51
[parent syncButtons];
52
} else if ([button isKindOfClass:ControlDrawer.class]) {
53
ControlDrawer *drawer = (ControlDrawer *)button;
54
index = @([self.ctrlView.layoutDictionary[@"mDrawerDataList"] indexOfObject:drawer.drawerData]);
55
[drawer.buttons makeObjectsPerformSelector:@selector(removeFromSuperview)];
56
[self.ctrlView.layoutDictionary[@"mDrawerDataList"] removeObject:drawer.drawerData];
57
} else if ([button isKindOfClass:ControlJoystick.class]) {
58
index = @([self.ctrlView.layoutDictionary[@"mJoystickDataList"] indexOfObject:button.properties]);
59
[self.ctrlView.layoutDictionary[@"mJoystickDataList"] removeObject:button.properties];
60
} else {
61
index = @([self.ctrlView.layoutDictionary[@"mControlDataList"] indexOfObject:button.properties]);
62
[self.ctrlView.layoutDictionary[@"mControlDataList"] removeObject:button.properties];
63
}
64
[button removeFromSuperview];
65
self.resizeView.hidden = YES;
66
67
NSUndoManager *undo = self.undoManager;
68
[[undo prepareWithInvocationTarget:self] undoRemoveButton:button atIndex:index];
69
if (!undo.isUndoing) {
70
undo.actionName = localize(@"Remove", nil);
71
}
72
}
73
74
- (void)undoRemoveButton:(ControlButton *)button atIndex:(NSNumber *)index {
75
NSUndoManager *undo = self.undoManager;
76
if (!undo.isUndoing) {
77
undo.actionName = localize(@"Remove", nil);
78
}
79
[self doAddButton:button atIndex:index];
80
}
81
82
- (void)doMoveOrResizeButton:(ControlButton *)button from:(CGRect)from to:(CGRect)to
83
{
84
NSUndoManager *undo = self.undoManager;
85
[[undo prepareWithInvocationTarget:self] doMoveOrResizeButton:button from:to to:from];
86
if (!undo.isUndoing) {
87
if (CGSizeEqualToSize(from.size, to.size)) {
88
undo.actionName = localize(@"Move", nil);
89
} else {
90
undo.actionName = localize(@"Resize", nil);
91
}
92
}
93
94
[button snapAndAlignX:to.origin.x Y:to.origin.y];
95
button.properties[@"width"] = @(to.size.width);
96
button.properties[@"height"] = @(to.size.height);
97
[button update];
98
self.resizeView.frame = CGRectMake(CGRectGetMaxX(button.frame), CGRectGetMaxY(button.frame), self.resizeView.frame.size.width, self.resizeView.frame.size.height);
99
}
100
101
- (void)doUpdateButton:(ControlButton *)button from:(NSMutableDictionary *)from to:(NSMutableDictionary *)to
102
{
103
NSUndoManager *undo = self.undoManager;
104
[[undo prepareWithInvocationTarget:self] doUpdateButton:button from:to to:from];
105
if (!undo.isUndoing) {
106
undo.actionName = localize(@"Edit", nil);
107
}
108
109
for (NSString *key in to) {
110
button.properties[key] = to[key];
111
}
112
113
@try {
114
[button update];
115
} @catch (NSException *exception) {
116
button.properties[@"dynamicX"] = to[@"dynamicX"] = from[@"dynamicX"];
117
button.properties[@"dynamicY"] = to[@"dynamicY"] = from[@"dynamicY"];
118
@throw exception;
119
}
120
}
121
122
@end
123
124