Path: blob/main/Natives/CustomControlsViewController+UndoManager.m
589 views
#import "CustomControlsViewController.h"1#import "customcontrols/ControlDrawer.h"2#import "customcontrols/ControlJoystick.h"3#import "customcontrols/ControlLayout.h"4#import "utils.h"56@implementation CustomControlsViewController(UndoManager)78- (void)doAddButton:(ControlButton *)button atIndex:(NSNumber *)index {9NSUndoManager *undo = self.undoManager;1011if ([button isKindOfClass:ControlSubButton.class]) {12undo.actionName = localize(@"custom_controls.button_menu.add_subbutton", nil);1314[self.ctrlView addSubview:button];15ControlDrawer *drawer = ((ControlSubButton *)button).parentDrawer;16[drawer.buttons insertObject:button atIndex:index.intValue];17[drawer.drawerData[@"buttonProperties"] insertObject:button.properties atIndex:index.intValue];18[drawer syncButtons];19} else if ([button isKindOfClass:ControlDrawer.class]) {20undo.actionName = localize(@"custom_controls.control_menu.add_drawer", nil);2122for (ControlSubButton *subButton in ((ControlDrawer *)button).buttons) {23[self.ctrlView addSubview:subButton];24}25[self.ctrlView.layoutDictionary[@"mDrawerDataList"] insertObject:((ControlDrawer *)button).drawerData atIndex:index.intValue];26[self.ctrlView addSubview:button];27} else if ([button isKindOfClass:ControlJoystick.class]) {28undo.actionName = localize(@"custom_controls.control_menu.add_joystick", nil);29[self.ctrlView.layoutDictionary[@"mJoystickDataList"] insertObject:button.properties atIndex:index.intValue];30[self.ctrlView addSubview:button];31} else {32undo.actionName = localize(@"custom_controls.control_menu.add_button", nil);33[self.ctrlView.layoutDictionary[@"mControlDataList"] insertObject:button.properties atIndex:index.intValue];34[self.ctrlView addSubview:button];35}3637[[undo prepareWithInvocationTarget:self] doRemoveButton:button];38if (undo.isUndoing) {39undo.actionName = localize(@"Remove", nil);40}41}4243- (void)doRemoveButton:(ControlButton *)button {44NSNumber *index;45if ([button isKindOfClass:ControlSubButton.class]) {46ControlDrawer *parent = ((ControlSubButton *)button).parentDrawer;47index = @([parent.drawerData[@"buttonProperties"] indexOfObject:button.properties]);48[parent.buttons removeObject:button];49[parent.drawerData[@"buttonProperties"] removeObject:button.properties];50[parent syncButtons];51} else if ([button isKindOfClass:ControlDrawer.class]) {52ControlDrawer *drawer = (ControlDrawer *)button;53index = @([self.ctrlView.layoutDictionary[@"mDrawerDataList"] indexOfObject:drawer.drawerData]);54[drawer.buttons makeObjectsPerformSelector:@selector(removeFromSuperview)];55[self.ctrlView.layoutDictionary[@"mDrawerDataList"] removeObject:drawer.drawerData];56} else if ([button isKindOfClass:ControlJoystick.class]) {57index = @([self.ctrlView.layoutDictionary[@"mJoystickDataList"] indexOfObject:button.properties]);58[self.ctrlView.layoutDictionary[@"mJoystickDataList"] removeObject:button.properties];59} else {60index = @([self.ctrlView.layoutDictionary[@"mControlDataList"] indexOfObject:button.properties]);61[self.ctrlView.layoutDictionary[@"mControlDataList"] removeObject:button.properties];62}63[button removeFromSuperview];64self.resizeView.hidden = YES;6566NSUndoManager *undo = self.undoManager;67[[undo prepareWithInvocationTarget:self] undoRemoveButton:button atIndex:index];68if (!undo.isUndoing) {69undo.actionName = localize(@"Remove", nil);70}71}7273- (void)undoRemoveButton:(ControlButton *)button atIndex:(NSNumber *)index {74NSUndoManager *undo = self.undoManager;75if (!undo.isUndoing) {76undo.actionName = localize(@"Remove", nil);77}78[self doAddButton:button atIndex:index];79}8081- (void)doMoveOrResizeButton:(ControlButton *)button from:(CGRect)from to:(CGRect)to82{83NSUndoManager *undo = self.undoManager;84[[undo prepareWithInvocationTarget:self] doMoveOrResizeButton:button from:to to:from];85if (!undo.isUndoing) {86if (CGSizeEqualToSize(from.size, to.size)) {87undo.actionName = localize(@"Move", nil);88} else {89undo.actionName = localize(@"Resize", nil);90}91}9293[button snapAndAlignX:to.origin.x Y:to.origin.y];94button.properties[@"width"] = @(to.size.width);95button.properties[@"height"] = @(to.size.height);96[button update];97self.resizeView.frame = CGRectMake(CGRectGetMaxX(button.frame), CGRectGetMaxY(button.frame), self.resizeView.frame.size.width, self.resizeView.frame.size.height);98}99100- (void)doUpdateButton:(ControlButton *)button from:(NSMutableDictionary *)from to:(NSMutableDictionary *)to101{102NSUndoManager *undo = self.undoManager;103[[undo prepareWithInvocationTarget:self] doUpdateButton:button from:to to:from];104if (!undo.isUndoing) {105undo.actionName = localize(@"Edit", nil);106}107108for (NSString *key in to) {109button.properties[key] = to[key];110}111112@try {113[button update];114} @catch (NSException *exception) {115button.properties[@"dynamicX"] = to[@"dynamicX"] = from[@"dynamicX"];116button.properties[@"dynamicY"] = to[@"dynamicY"] = from[@"dynamicY"];117@throw exception;118}119}120121@end122123124