Skip to content

Commit

Permalink
CATTY-552 Clone object or actor
Browse files Browse the repository at this point in the history
  • Loading branch information
amelak9 authored and srinner committed Mar 3, 2022
1 parent ff9ffc2 commit bb9ddfb
Show file tree
Hide file tree
Showing 127 changed files with 1,683 additions and 9 deletions.
22 changes: 22 additions & 0 deletions src/Catty.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,11 @@
972622DD25F51A8F00ABCC7A /* ChangeBrightnessByNBrick+CBXMLHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 972622DC25F51A8F00ABCC7A /* ChangeBrightnessByNBrick+CBXMLHandler.swift */; };
972622E225F51B4500ABCC7A /* SetBrightnessBrick+CBXMLHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 972622E125F51B4500ABCC7A /* SetBrightnessBrick+CBXMLHandler.swift */; };
9728AE9E25DEEE5A00708EB6 /* ProjectDetailStoreViewControllerReportExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9728AE9D25DEEE5A00708EB6 /* ProjectDetailStoreViewControllerReportExtension.swift */; };
973F1D3226B1923F0043108A /* CloneBrick+CBXMLHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 973F1D3126B1923F0043108A /* CloneBrick+CBXMLHandler.swift */; };
9740489026B04FAE0047DEBB /* CloneBrick.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9740488F26B04FAE0047DEBB /* CloneBrick.swift */; };
9740489426B052950047DEBB /* CloneBrickCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9740489326B052950047DEBB /* CloneBrickCell.swift */; };
9740489626B0547D0047DEBB /* CloneBrick+Instruction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9740489526B0547D0047DEBB /* CloneBrick+Instruction.swift */; };
9740489E26B097500047DEBB /* CloneBrickTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9740489D26B097500047DEBB /* CloneBrickTests.swift */; };
97417A9B265284400079A2A2 /* SoundsTableViewController+SelectFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97417A9A265284400079A2A2 /* SoundsTableViewController+SelectFile.swift */; };
9742DC5B269A2EC600980DE8 /* TouchesFingerSensorTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9742DC5A269A2EC600980DE8 /* TouchesFingerSensorTest.swift */; };
9767BAFA26668ECD009794E8 /* JoinThreeStringsFunctionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9767BAF926668ECD009794E8 /* JoinThreeStringsFunctionTest.swift */; };
Expand Down Expand Up @@ -3491,6 +3496,11 @@
972622DC25F51A8F00ABCC7A /* ChangeBrightnessByNBrick+CBXMLHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ChangeBrightnessByNBrick+CBXMLHandler.swift"; sourceTree = "<group>"; };
972622E125F51B4500ABCC7A /* SetBrightnessBrick+CBXMLHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SetBrightnessBrick+CBXMLHandler.swift"; sourceTree = "<group>"; };
9728AE9D25DEEE5A00708EB6 /* ProjectDetailStoreViewControllerReportExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProjectDetailStoreViewControllerReportExtension.swift; sourceTree = "<group>"; };
973F1D3126B1923F0043108A /* CloneBrick+CBXMLHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "CloneBrick+CBXMLHandler.swift"; path = "CattyTests/Bricks/CloneBrick+CBXMLHandler.swift"; sourceTree = SOURCE_ROOT; };
9740488F26B04FAE0047DEBB /* CloneBrick.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloneBrick.swift; sourceTree = "<group>"; };
9740489326B052950047DEBB /* CloneBrickCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloneBrickCell.swift; sourceTree = "<group>"; };
9740489526B0547D0047DEBB /* CloneBrick+Instruction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CloneBrick+Instruction.swift"; sourceTree = "<group>"; };
9740489D26B097500047DEBB /* CloneBrickTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloneBrickTests.swift; sourceTree = "<group>"; };
97417A9A265284400079A2A2 /* SoundsTableViewController+SelectFile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SoundsTableViewController+SelectFile.swift"; sourceTree = "<group>"; };
9742DC5A269A2EC600980DE8 /* TouchesFingerSensorTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TouchesFingerSensorTest.swift; sourceTree = "<group>"; };
9767BAF926668ECD009794E8 /* JoinThreeStringsFunctionTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JoinThreeStringsFunctionTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4152,6 +4162,7 @@
C82FC8DC26964F4A008DFBB8 /* TouchesFingerSensor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchesFingerSensor.swift; sourceTree = "<group>"; };
C84B21702714A2690077CF70 /* TouchesEdgeSensor.xml */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = TouchesEdgeSensor.xml; sourceTree = "<group>"; };
C85A5C9F267A218F009BA454 /* Functions_0993.xml */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = Functions_0993.xml; sourceTree = "<group>"; };
C88FFF5D26B830E100D381D8 /* BrickObjectWithOutBackgroundProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BrickObjectWithOutBackgroundProtocol.h; sourceTree = "<group>"; };
C8A0338226064F3C00702911 /* SetTempoToBrick+CBXMLHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "SetTempoToBrick+CBXMLHandler.swift"; path = "Catty/Views/Custom/CollectionViewCells/BrickCells/Sound/SetTempoToBrick+CBXMLHandler.swift"; sourceTree = SOURCE_ROOT; };
C8CD7DD325E63D0A0018C655 /* BrickCategoryOverviewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrickCategoryOverviewControllerTests.swift; sourceTree = "<group>"; };
C8D010C3264BD2B700896DEB /* JoinThreeStringsFunction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JoinThreeStringsFunction.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5970,6 +5981,7 @@
9990BB6C1D89D89A0088357A /* BrickStaticChoiceProtocol.h */,
4C1EB20F1AC19B3D0001F431 /* BrickTextProtocol.h */,
4CC0D51F1B01FB73006193C4 /* BrickVariableProtocol.h */,
C88FFF5D26B830E100D381D8 /* BrickObjectWithOutBackgroundProtocol.h */,
);
path = BrickData;
sourceTree = "<group>";
Expand Down Expand Up @@ -6325,6 +6337,7 @@
AA74EEDA1BC057B900D1E954 /* WaitBrick+CBXMLHandler.m */,
BA987D042194DDCF002DAA05 /* WaitUntilBrick+CBXMLHandler.h */,
BA987D032194DDCF002DAA05 /* WaitUntilBrick+CBXMLHandler.m */,
973F1D3126B1923F0043108A /* CloneBrick+CBXMLHandler.swift */,
);
name = Control;
path = ControlBricks;
Expand Down Expand Up @@ -7169,6 +7182,7 @@
83F8230725EBA9610093DD9A /* SetBackgroundByIndexBrickTests.swift */,
2E8780A72542BCE200816B52 /* WebRequestBrickTests.swift */,
0580514826EF734F00F719E0 /* StartRunningStitchBrickTests.swift */,
9740489D26B097500047DEBB /* CloneBrickTests.swift */,
);
path = Bricks;
sourceTree = "<group>";
Expand Down Expand Up @@ -9655,6 +9669,7 @@
AA74EE1F1BC053FD00D1E954 /* BroadcastWaitBrick+Instruction.swift */,
AA74EE201BC053FD00D1E954 /* WaitBrick+Instruction.swift */,
BA987D0E2194EA1F002DAA05 /* WaitUntilBrick+Instruction.swift */,
9740489526B0547D0047DEBB /* CloneBrick+Instruction.swift */,
);
name = Control;
sourceTree = "<group>";
Expand Down Expand Up @@ -9788,6 +9803,7 @@
AA74EF951BC05B5F00D1E954 /* WaitBrick.m */,
BA987D072194E158002DAA05 /* WaitUntilBrick.h */,
BA987D062194E157002DAA05 /* WaitUntilBrick.m */,
9740488F26B04FAE0047DEBB /* CloneBrick.swift */,
);
path = Control;
sourceTree = "<group>";
Expand Down Expand Up @@ -10028,6 +10044,7 @@
9EDCD22422886FD90040EFE3 /* WaitBrickCell.swift */,
BA987D0A2194E25A002DAA05 /* WaitUntilBrickCell.h */,
BA987D092194E25A002DAA05 /* WaitUntilBrickCell.m */,
9740489326B052950047DEBB /* CloneBrickCell.swift */,
);
path = Control;
sourceTree = "<group>";
Expand Down Expand Up @@ -11950,6 +11967,7 @@
9E24D7032326E8E600608203 /* TurnLeftBrickTests.swift in Sources */,
E57E6D872540414700E775DF /* SetVolumeToBrickTests.swift in Sources */,
4C968C401F00288500355C0D /* BrickTests.swift in Sources */,
9740489E26B097500047DEBB /* CloneBrickTests.swift in Sources */,
E579F10B253D98B0009107C8 /* PhiroPlayToneBrickTests.swift in Sources */,
E579F114253DCA96009107C8 /* ThinkBubbleBrickTests.swift in Sources */,
4C0F9FD9204BD3D500E71B2D /* RepeatUntilBrickTests.swift in Sources */,
Expand Down Expand Up @@ -12293,6 +12311,7 @@
AA74F0BD1BC05FCE00D1E954 /* PlaceAtBrickCell.m in Sources */,
18390A6924D0576100A07DFD /* StampBrick.swift in Sources */,
CA3E72E81B0C00A500D6B184 /* CBStack.swift in Sources */,
9740489426B052950047DEBB /* CloneBrickCell.swift in Sources */,
AA74EFFE1BC05B5F00D1E954 /* ComeToFrontBrick.m in Sources */,
057B182A26DC8B6A00C47E60 /* StartRunningStitchBrick.swift in Sources */,
92FF31051A24DCAA00093DA7 /* WhenScript.m in Sources */,
Expand All @@ -12312,17 +12331,20 @@
2D6E3F3B210A0AB700FB8139 /* ChartProjectsStoreViewController.swift in Sources */,
92FF2EA41A24C7D800093DA7 /* Util.m in Sources */,
AA74EFEC1BC05B5F00D1E954 /* ChangeVariableBrick.m in Sources */,
973F1D3226B1923F0043108A /* CloneBrick+CBXMLHandler.swift in Sources */,
92FF31031A24DCAA00093DA7 /* Script.m in Sources */,
4C822693213FBC4400F3D750 /* MultiFingerTouchedFunction.swift in Sources */,
4420ACB1250929AE00951328 /* AskBrick+CBXMLHandler.swift in Sources */,
1882475924C84D9C00B01653 /* SetPenColorBrickCell.swift in Sources */,
4C0F9F9E204BD2B100E71B2D /* SayBubbleBrickCell.m in Sources */,
4C2EE41E1B555B55006DE9B8 /* CBXMLOpenedNestingBricksStack.m in Sources */,
5EFBD5F92145533B003B3CDC /* ProjectDescriptionViewController.swift in Sources */,
9740489626B0547D0047DEBB /* CloneBrick+Instruction.swift in Sources */,
92FF31571A24DEB300093DA7 /* ObjectTableViewController.m in Sources */,
4CE3D68F2107B68600005629 /* FaceDetectionManagerProtocol.swift in Sources */,
929CC0EF1BC39B8C0027DEC0 /* PhiroMotorStopBrickCell.m in Sources */,
4C0F9F64204BD18600E71B2D /* SayForBubbleBrick+CBXMLHandler.m in Sources */,
9740489026B04FAE0047DEBB /* CloneBrick.swift in Sources */,
92FF32BB1A24E2F400093DA7 /* DarkBlueGradientImageCell.m in Sources */,
92FF314E1A24DEB300093DA7 /* LookImageViewController.m in Sources */,
AA74F0A41BC05FCE00D1E954 /* LoopEndBrickCell.m in Sources */,
Expand Down
110 changes: 110 additions & 0 deletions src/Catty/CloneBrick+Instruction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Copyright (C) 2010-2022 The Catrobat Team
* (http://developer.catrobat.org/credits)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* An additional term exception under section 7 of the GNU Affero
* General Public License, version 3, is available at
* (http://developer.catrobat.org/license_additional_term)
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

@objc extension CloneBrick: CBInstructionProtocol {

@nonobjc func instruction() -> CBInstruction {
.action { context in SKAction.run(self.actionBlock(context.formulaInterpreter)) }
}

func actionBlock(_ formulaInterpreter: FormulaInterpreterProtocol) -> () -> Void {
guard let objectToClone = self.objectToClone
else { fatalError("This should never happen!") }

return {
let object = SpriteObject()
object.name = objectToClone.name + "Clone " + String(CloneBrick.nameCounter)
CloneBrick.nameCounter += 1

guard let scriptList = objectToClone.scriptList as NSMutableArray? as? [Script] else {
//fatalError
debugPrint("!! No script list given in object: \(objectToClone) !!")
return
}

let context = CBMutableCopyContext()
let variables = objectToClone.userData.variables()
let lists = objectToClone.userData.lists()

for variable in variables {
let var1 = UserVariable(name: variable.name)
let label = SKLabelNode(fontNamed: SpriteKitDefines.defaultFont)
var1.value = variable.value
var1.textLabel = label

let value = variable.value as! Int? ?? 0
var1.textLabel?.text = String(value)
var1.textLabel?.fontColor = variable.textLabel?.fontColor ?? UIColor.black
var1.textLabel?.fontSize = variable.textLabel?.fontSize ?? CGFloat(SpriteKitDefines.defaultLabelFontSize)
var1.textLabel?.position = variable.textLabel?.position ?? CGPoint(x: 0, y: 0)
var1.textLabel?.isHidden = variable.textLabel?.isHidden ?? true
object.userData.add(var1)
}

for list in lists {
let list1 = list.mutableCopy(with: context) as! UserList
object.userData.add(list1)
}

for script in scriptList {
let newScript = script.clone(with: object)!
self.setScriptAttributes(script: newScript, oldScript: script)
for brick in script.brickList {
let brickToClone = brick as! Brick
let newBrick = brickToClone.clone(with: newScript)!
self.setBrickAttributes(brick: newBrick, oldBrick: brickToClone)
newScript.brickList.add(newBrick)
}
object.scriptList.add(newScript)
}

object.soundList = objectToClone.soundList.mutableCopy() as? NSMutableArray
object.lookList = objectToClone.lookList.mutableCopy() as? NSMutableArray

objectToClone.scene.add(object: object)
let spriteNode = CBSpriteNode(spriteObject: object)
object.spriteNode = spriteNode

let stage = objectToClone.spriteNode.scene as? Stage
stage?.addChild(object.spriteNode)
object.spriteNode.startAsClone(objectToClone.spriteNode)
stage?.scheduler.registerSpriteNode(object.spriteNode)
stage?.addClonedObjecToProject(spriteObject: object)
}
}

func setBrickAttributes(brick: Brick, oldBrick: Brick) {
brick.isDisabled = oldBrick.isDisabled
brick.isSelected = oldBrick.isSelected
brick.isAnimatedMoveBrick = oldBrick.isAnimatedMoveBrick
brick.isAnimatedInsertBrick = oldBrick.isAnimatedInsertBrick
brick.isAnimated = oldBrick.isAnimated
}

func setScriptAttributes(script: Script, oldScript: Script) {
script.isDisabled = oldScript.isDisabled
script.isSelected = oldScript.isSelected
script.isAnimatedMoveBrick = oldScript.isAnimatedMoveBrick
script.isAnimatedInsertBrick = oldScript.isAnimatedInsertBrick
script.isAnimated = oldScript.isAnimated
}
}
10 changes: 10 additions & 0 deletions src/Catty/DataModel/Bricks/Arduino/ArduinoSendDigitalValueBrick.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ - (BOOL)allowsStringFormula
return NO;
}

- (Brick*)cloneWithScript:(Script *)script
{
ArduinoSendDigitalValueBrick *clone = [[ArduinoSendDigitalValueBrick alloc] init];
clone.script = script;
clone.pin = self.pin;
clone.value = self.value;

return clone;
}

#pragma mark - Description
- (NSString*)description
{
Expand Down
10 changes: 10 additions & 0 deletions src/Catty/DataModel/Bricks/Arduino/ArduinoSendPWMValueBrick.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ - (BOOL)allowsStringFormula
return NO;
}

- (Brick*)cloneWithScript:(Script *)script
{
ArduinoSendPWMValueBrick *clone = [[ArduinoSendPWMValueBrick alloc] init];
clone.script = script;
clone.pin = self.pin;
clone.value = self.value;

return clone;
}

#pragma mark - Description
- (NSString*)description
{
Expand Down
5 changes: 3 additions & 2 deletions src/Catty/DataModel/Bricks/Brick.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
#import "SpriteObject.h"
#import "UIDefines.h"
#import "BrickProtocol.h"

@class Script;
#import "Script.h"

@interface Brick : NSObject

Expand Down Expand Up @@ -72,4 +71,6 @@

- (Class<BrickCellProtocol>)brickCell;

- (Brick*)cloneWithScript:(Script *) script;

@end
9 changes: 9 additions & 0 deletions src/Catty/DataModel/Bricks/Brick.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ - (void)setDefaultValuesForObject:(SpriteObject*)spriteObject
// Override this method in Brick implementation
}

- (Brick*)cloneWithScript:(Script *)script
{
// Override this method in Brick implementation
Brick *clone = [[Brick alloc] init];
clone.script = script;

return clone;
}

#pragma mark - Copy
// This function must be overriden by Bricks with references to other Bricks (e.g. ForeverBrick)
- (id)mutableCopyWithContext:(CBMutableCopyContext*)context
Expand Down
9 changes: 9 additions & 0 deletions src/Catty/DataModel/Bricks/Control/BroadcastBrick.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ - (NSString*)messageForLineNumber:(NSInteger)lineNumber andParameterNumber:(NSIn
return self.broadcastMessage;
}

- (Brick*)cloneWithScript:(Script *)script
{
BroadcastBrick *clone = [[BroadcastBrick alloc] init];
clone.script = script;
clone.broadcastMessage = self.broadcastMessage;

return clone;
}

#pragma mark - Description
- (NSString*)description
{
Expand Down
9 changes: 9 additions & 0 deletions src/Catty/DataModel/Bricks/Control/BroadcastWaitBrick.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ - (NSString*)messageForLineNumber:(NSInteger)lineNumber andParameterNumber:(NSIn
return self.broadcastMessage;
}

- (Brick*)cloneWithScript:(Script *)script
{
BroadcastWaitBrick *clone = [[BroadcastWaitBrick alloc] init];
clone.script = script;
clone.broadcastMessage = self.broadcastMessage;

return clone;
}

#pragma mark - Description
- (NSString*)description
{
Expand Down
Loading

0 comments on commit bb9ddfb

Please sign in to comment.