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 Nov 26, 2021
1 parent 8829853 commit c460c7f
Show file tree
Hide file tree
Showing 24 changed files with 728 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/Catty.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,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 */; };
9767BAFA26668ECD009794E8 /* JoinThreeStringsFunctionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9767BAF926668ECD009794E8 /* JoinThreeStringsFunctionTest.swift */; };
97770B8825E5A88C00F51EFA /* SetBrightnessBrick.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97770B8725E5A88C00F51EFA /* SetBrightnessBrick.swift */; };
Expand Down Expand Up @@ -3454,6 +3459,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>"; };
9767BAF926668ECD009794E8 /* JoinThreeStringsFunctionTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JoinThreeStringsFunctionTest.swift; sourceTree = "<group>"; };
97770B8725E5A88C00F51EFA /* SetBrightnessBrick.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetBrightnessBrick.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4110,6 +4120,7 @@
C286B2695528549DC9B2916D /* af */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.strings; name = af; path = af.lproj/Localizable.strings; sourceTree = "<group>"; };
C42D117BCEADFF7368CA3159 /* bn */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.strings; name = bn; path = bn.lproj/Localizable.strings; 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 @@ -5911,6 +5922,7 @@
9990BB6C1D89D89A0088357A /* BrickStaticChoiceProtocol.h */,
4C1EB20F1AC19B3D0001F431 /* BrickTextProtocol.h */,
4CC0D51F1B01FB73006193C4 /* BrickVariableProtocol.h */,
C88FFF5D26B830E100D381D8 /* BrickObjectWithOutBackgroundProtocol.h */,
);
path = BrickData;
sourceTree = "<group>";
Expand Down Expand Up @@ -6266,6 +6278,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 @@ -7106,6 +7119,7 @@
83F8230725EBA9610093DD9A /* SetBackgroundByIndexBrickTests.swift */,
2E8780A72542BCE200816B52 /* WebRequestBrickTests.swift */,
0580514826EF734F00F719E0 /* StartRunningStitchBrickTests.swift */,
9740489D26B097500047DEBB /* CloneBrickTests.swift */,
);
path = Bricks;
sourceTree = "<group>";
Expand Down Expand Up @@ -9586,6 +9600,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 @@ -9719,6 +9734,7 @@
AA74EF951BC05B5F00D1E954 /* WaitBrick.m */,
BA987D072194E158002DAA05 /* WaitUntilBrick.h */,
BA987D062194E157002DAA05 /* WaitUntilBrick.m */,
9740488F26B04FAE0047DEBB /* CloneBrick.swift */,
);
path = Control;
sourceTree = "<group>";
Expand Down Expand Up @@ -9959,6 +9975,7 @@
9EDCD22422886FD90040EFE3 /* WaitBrickCell.swift */,
BA987D0A2194E25A002DAA05 /* WaitUntilBrickCell.h */,
BA987D092194E25A002DAA05 /* WaitUntilBrickCell.m */,
9740489326B052950047DEBB /* CloneBrickCell.swift */,
);
path = Control;
sourceTree = "<group>";
Expand Down Expand Up @@ -11874,6 +11891,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 @@ -12213,6 +12231,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 @@ -12232,17 +12251,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
131 changes: 131 additions & 0 deletions src/Catty/CloneBrick+Instruction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* Copyright (C) 2010-2021 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 + "Copy " + 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
}

for script in scriptList {
switch script {
case let startScript as StartScript:
let newStartScript = StartScript()
newStartScript.object = object
newStartScript.brickList = startScript.brickList.mutableCopy() as? NSMutableArray
for brick in newStartScript.brickList {
(brick as! Brick).script = newStartScript as Script
}
object.scriptList.add(newStartScript)

case let whenScript as WhenScript:
let newWhenScript = WhenScript()
newWhenScript.object = object
newWhenScript.brickList = whenScript.brickList.mutableCopy() as? NSMutableArray
for brick in newWhenScript.brickList {
(brick as! Brick).script = newWhenScript as Script
}
object.scriptList.add(newWhenScript)

case let whenTouchDownScript as WhenTouchDownScript:
let newWhenTouchDownScript = WhenTouchDownScript()
newWhenTouchDownScript.object = object
newWhenTouchDownScript.brickList = whenTouchDownScript.brickList.mutableCopy() as? NSMutableArray
for brick in newWhenTouchDownScript.brickList {
(brick as! Brick).script = newWhenTouchDownScript as Script
}
object.scriptList.add(newWhenTouchDownScript)

case let whenBackgroundChangesScript as WhenBackgroundChangesScript:
let newWhenBackgroundChangesScript = WhenBackgroundChangesScript()
newWhenBackgroundChangesScript.object = object
newWhenBackgroundChangesScript.brickList = whenBackgroundChangesScript.brickList.mutableCopy() as? NSMutableArray
for brick in newWhenBackgroundChangesScript.brickList {
(brick as! Brick).script = newWhenBackgroundChangesScript as Script
}
object.scriptList.add(newWhenBackgroundChangesScript)

case let bcScript as BroadcastScript:
let newBcScript = BroadcastScript()
newBcScript.object = object
newBcScript.brickList = bcScript.brickList.mutableCopy() as? NSMutableArray
for brick in newBcScript.brickList {
(brick as! Brick).script = newBcScript as Script
}
object.scriptList.add(newBcScript)

case let whenConditionScript as WhenConditionScript:
let newWhenConditionScript = WhenConditionScript()
newWhenConditionScript.object = object
newWhenConditionScript.brickList = whenConditionScript.brickList.mutableCopy() as? NSMutableArray
for brick in newWhenConditionScript.brickList {
(brick as! Brick).script = newWhenConditionScript as Script
}
object.scriptList.add(newWhenConditionScript)

default:
break
}
}

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

let variables = UserDataContainer.objectVariables(for: objectToClone)
let lists = UserDataContainer.objectLists(for: objectToClone)

for variable in variables {
let var1 = variable as UserVariable
object.userData.add(var1)
}
for list in lists {
let list1 = list as UserList
object.userData.add(list1)
}

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.startAsCopy(objectToClone.spriteNode)
stage?.scheduler.registerSpriteNode(object.spriteNode)
stage?.addCopiedObjecToProject(spriteObject: object)
}
}
}
79 changes: 79 additions & 0 deletions src/Catty/DataModel/Bricks/Control/CloneBrick.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (C) 2010-2021 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(CloneBrick)
@objcMembers class CloneBrick: Brick, BrickObjectWithOutBackgroundProtocol {

var objectToClone: SpriteObject?
static var nameCounter: Int = 1

override required init() {
super.init()
}

func category() -> kBrickCategoryType {
kBrickCategoryType.eventBrick
}

override class func description() -> String {
"CloneBrick"
}

override func getRequiredResources() -> Int {
ResourceType.noResources.rawValue
}

override func brickCell() -> BrickCellProtocol.Type! {
CloneBrickCell.self as BrickCellProtocol.Type
}

func setObject(_ object: SpriteObject?, forLineNumber lineNumber: Int, andParameterNumber paramNumber: Int) {
if let object = object {
objectToClone = object
}
}

func object(forLineNumber lineNumber: Int, andParameterNumber paramNumber: Int) -> SpriteObject? {
self.objectToClone
}

override func setDefaultValuesFor(_ spriteObject: SpriteObject!) {
if spriteObject != nil {
objectToClone = spriteObject
} else {
objectToClone = self.script.object
}
}

override func isDisabledForBackground() -> Bool {
true
}

@objc(mutableCopyWithContext:)
override func mutableCopy(with context: CBMutableCopyContext) -> Any {
let brick = CloneBrick()
if self.objectToClone != nil {
brick.objectToClone = self.objectToClone
}
return brick
}
}
1 change: 1 addition & 0 deletions src/Catty/DataModel/Bricks/Motion/PointToBrick.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#import "Brick.h"
#import "BrickObjectProtocol.h"
#import "BrickObjectWithOutBackgroundProtocol.h"

@class SpriteObject;

Expand Down
5 changes: 5 additions & 0 deletions src/Catty/DataModel/Scene/Scene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
self._objects
}

func objectsWithoutBackground() -> [SpriteObject] {
let backgroundObjects = self.numberOfBackgroundObjects()
return [SpriteObject](self._objects[backgroundObjects...])
}

@objc(objectAtIndex:)
func object(at index: Int) -> SpriteObject? {
if index >= 0 && index < self._objects.count {
Expand Down
1 change: 1 addition & 0 deletions src/Catty/Defines/LanguageTranslationDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@
#define kLocalizedEndOfLoop NSLocalizedString(@"End of Loop", nil)
#define kLocalizedWhen NSLocalizedString(@"When", nil)
#define kLocalizedBecomesTrue NSLocalizedString(@"becomes true", nil)
#define kLocalizedCreateCloneOf NSLocalizedString(@"Create clone of", nil)

// motion bricks
#define kLocalizedPlaceAt NSLocalizedString(@"Place at ", nil)
Expand Down
Loading

0 comments on commit c460c7f

Please sign in to comment.