Skip to content

Commit

Permalink
CATTY-438 Implement StopScriptBrick
Browse files Browse the repository at this point in the history
  • Loading branch information
oetzlinger committed Dec 10, 2022
1 parent 757c7c5 commit c5d2db6
Show file tree
Hide file tree
Showing 16 changed files with 384 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/Catty.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,11 @@
F4E6E5C4210E242D00D86FE6 /* FloorFunction.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4E6E5C3210E242D00D86FE6 /* FloorFunction.swift */; };
F4E6E5C6210E24F900D86FE6 /* TrueFunction.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4E6E5C5210E24F900D86FE6 /* TrueFunction.swift */; };
F4E6E5C8210E255D00D86FE6 /* FalseFunction.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4E6E5C7210E255D00D86FE6 /* FalseFunction.swift */; };
FE4134F8293B418A0001DA11 /* StopScriptBrick.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE4134F7293B418A0001DA11 /* StopScriptBrick.swift */; };
FE4134FA293B44E00001DA11 /* StopScriptBrickCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE4134F9293B44E00001DA11 /* StopScriptBrickCell.swift */; };
FE4134FF293BA41A0001DA11 /* StopScriptBrick+Instruction.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE4134FE293BA41A0001DA11 /* StopScriptBrick+Instruction.swift */; };
FE413501293BACEC0001DA11 /* StopStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE413500293BACEC0001DA11 /* StopStyle.swift */; };
FEDC355B29407F5D00256FAB /* StopScriptBrick+CBXMLHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEDC355A29407F5D00256FAB /* StopScriptBrick+CBXMLHandler.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -4730,6 +4735,11 @@
F4E6E5C7210E255D00D86FE6 /* FalseFunction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FalseFunction.swift; sourceTree = "<group>"; };
F511044FACC2C8E38A678BB4 /* chr */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.strings; name = chr; path = chr.lproj/Localizable.strings; sourceTree = "<group>"; };
FB40BD936AE254501FBA2E03 /* kn */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.strings; name = kn; path = kn.lproj/Localizable.strings; sourceTree = "<group>"; };
FE4134F7293B418A0001DA11 /* StopScriptBrick.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StopScriptBrick.swift; sourceTree = "<group>"; };
FE4134F9293B44E00001DA11 /* StopScriptBrickCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StopScriptBrickCell.swift; sourceTree = "<group>"; };
FE4134FE293BA41A0001DA11 /* StopScriptBrick+Instruction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "StopScriptBrick+Instruction.swift"; sourceTree = "<group>"; };
FE413500293BACEC0001DA11 /* StopStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StopStyle.swift; sourceTree = "<group>"; };
FEDC355A29407F5D00256FAB /* StopScriptBrick+CBXMLHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "StopScriptBrick+CBXMLHandler.swift"; path = "Catty/DataModel/SpriteObject/StopScriptBrick+CBXMLHandler.swift"; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -6794,6 +6804,7 @@
AA74EEDA1BC057B900D1E954 /* WaitBrick+CBXMLHandler.m */,
BA987D042194DDCF002DAA05 /* WaitUntilBrick+CBXMLHandler.h */,
BA987D032194DDCF002DAA05 /* WaitUntilBrick+CBXMLHandler.m */,
FEDC355A29407F5D00256FAB /* StopScriptBrick+CBXMLHandler.swift */,
);
name = Control;
path = ControlBricks;
Expand Down Expand Up @@ -9050,6 +9061,7 @@
children = (
181CC10424B750AE004A783E /* CBPosition.swift */,
4C0D2A81254AA2C100A8637F /* RotationStyle.swift */,
FE413500293BACEC0001DA11 /* StopStyle.swift */,
92FF30BF1A24DCAA00093DA7 /* SpriteObject.h */,
92FF30C01A24DCAA00093DA7 /* SpriteObject.m */,
);
Expand Down Expand Up @@ -10126,6 +10138,7 @@
AA74EE1F1BC053FD00D1E954 /* BroadcastWaitBrick+Instruction.swift */,
AA74EE201BC053FD00D1E954 /* WaitBrick+Instruction.swift */,
BA987D0E2194EA1F002DAA05 /* WaitUntilBrick+Instruction.swift */,
FE4134FE293BA41A0001DA11 /* StopScriptBrick+Instruction.swift */,
);
name = Control;
sourceTree = "<group>";
Expand Down Expand Up @@ -10255,6 +10268,7 @@
AA74EF931BC05B5F00D1E954 /* RepeatBrick.m */,
4C0F9F27204ADBDF00E71B2D /* RepeatUntilBrick.h */,
4C0F9F26204ADBDF00E71B2D /* RepeatUntilBrick.m */,
FE4134F7293B418A0001DA11 /* StopScriptBrick.swift */,
AA74EF941BC05B5F00D1E954 /* WaitBrick.h */,
AA74EF951BC05B5F00D1E954 /* WaitBrick.m */,
BA987D072194E158002DAA05 /* WaitUntilBrick.h */,
Expand Down Expand Up @@ -10499,6 +10513,7 @@
9EDCD22422886FD90040EFE3 /* WaitBrickCell.swift */,
BA987D0A2194E25A002DAA05 /* WaitUntilBrickCell.h */,
BA987D092194E25A002DAA05 /* WaitUntilBrickCell.m */,
FE4134F9293B44E00001DA11 /* StopScriptBrickCell.swift */,
);
path = Control;
sourceTree = "<group>";
Expand Down Expand Up @@ -13192,6 +13207,7 @@
46F0D30E25E593F5002252E6 /* SetLookBrick+CBXMLHandler.swift in Sources */,
4CB2CFA61D7AD7E7009F3034 /* SetColorBrick+CBXMLHandler.m in Sources */,
AA74EFEB1BC05B5F00D1E954 /* WaitBrick.m in Sources */,
FEDC355B29407F5D00256FAB /* StopScriptBrick+CBXMLHandler.swift in Sources */,
4C0F9FA5204BD2C200E71B2D /* ThinkForBubbleBrickCell.m in Sources */,
4CDA808521366AEA0052FA24 /* FormulaManagerProtocol.swift in Sources */,
49B55F9327E88702000A95B4 /* RightEyebrowInnerXSensor.swift in Sources */,
Expand Down Expand Up @@ -13453,8 +13469,10 @@
186E99132488F3FF00627E36 /* PenUpBrick.swift in Sources */,
BA9CAB601EC383E800796056 /* AddItemToUserListBrickCell.m in Sources */,
9E2AEA50239887A600CDF58F /* PlaySoundAndWaitBrickCell.swift in Sources */,
FE413501293BACEC0001DA11 /* StopStyle.swift in Sources */,
9218B2051CC4AB75007B4C60 /* LinkedListStack.m in Sources */,
F4E6E584210DFD4300D86FE6 /* TanFunction.swift in Sources */,
FE4134F8293B418A0001DA11 /* StopScriptBrick.swift in Sources */,
92B46F931BC7B3E5004980C1 /* ArduinoSendPWMValueBrick.m in Sources */,
F4E6E596210E03B700D86FE6 /* AtanFunction.swift in Sources */,
F4E6E588210DFE8800D86FE6 /* LogFunction.swift in Sources */,
Expand Down Expand Up @@ -13609,6 +13627,7 @@
AAF6D9D81BC0BAF300686849 /* ShowBrick+Instruction.swift in Sources */,
593E4DDE1FE11D410016DCB9 /* CatrobatReorderableCollectionViewFlowLayout.m in Sources */,
BB9EC0E327D62CBA009FF8CE /* FormCheckTableViewCell.swift in Sources */,
FE4134FA293B44E00001DA11 /* StopScriptBrickCell.swift in Sources */,
4CEFFAD32105DE4800DC5CFE /* ObjectSensor.swift in Sources */,
921D467B1BDF61350086AD20 /* ChangeXByNBrick+Instruction.swift in Sources */,
4420ACB3250929DA00951328 /* AskBrickCell.swift in Sources */,
Expand Down Expand Up @@ -13643,6 +13662,7 @@
5E7B5B0D21EC0FCF002E4B57 /* HelpWebViewController.swift in Sources */,
9218B2111CC4AB75007B4C60 /* PointerTool.m in Sources */,
AA74F0CE1BC05FCE00D1E954 /* WhenScriptCell.m in Sources */,
FE4134FF293BA41A0001DA11 /* StopScriptBrick+Instruction.swift in Sources */,
F4E6E580210DE61900D86FE6 /* PowerFunction.swift in Sources */,
92FF32B91A24E2F400093DA7 /* DarkBlueGradientCell.m in Sources */,
498C158528070C6C00B81C8E /* RightKneeYSensor.swift in Sources */,
Expand Down
60 changes: 60 additions & 0 deletions src/Catty/DataModel/Bricks/Control/StopScriptBrick.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* 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(StopScriptBrick) class StopScriptBrick: Brick, BrickProtocol, BrickStaticChoiceProtocol {
static let defaultSelection = StopStyle.thisScript
var question: Formula?
var selection: StopStyle

override required init() {
self.selection = type(of: self).defaultSelection
super.init()
}

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

override func description() -> String {
"StopScriptBrick"
}

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

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

func choice(forLineNumber lineNumber: Int, andParameterNumber paramNumber: Int) -> String {
selection.localizedString()
}

func setChoice(_ message: String, forLineNumber lineNumber: Int, andParameterNumber paramNumber: Int) {
self.selection = StopStyle.from(localizedString: message) ?? type(of: self).defaultSelection
}

func possibleChoices(forLineNumber lineNumber: Int, andParameterNumber paramNumber: Int) -> [String] {
StopStyle.allCases.map { $0.localizedString() }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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/.
*/

extension StopScriptBrick: CBXMLNodeProtocol {
static func parse(from xmlElement: GDataXMLElement!, with context: CBXMLParserContext!) -> Self! {
CBXMLParserHelper.validate(xmlElement, forNumberOfChildNodes: 1)
let brick = self.init()

let brickType = xmlElement.attribute(forName: "type")

if brickType?.stringValue() == "StopScriptBrick" {
if let selection: GDataXMLElement = xmlElement.child(withElementName: "selection") {
if let choiceInt = Int(selection.stringValue()), let selection = StopStyle.from(rawValue: choiceInt) {
brick.selection = selection
} else {
fatalError("No or invalid selectionChoice given...")
}
} else {
fatalError("StopStyleBrick element does not contain a selection child element!")
}
} else {
fatalError("StopStyleBrick is faulty!")
}

return brick
}

func xmlElement(with context: CBXMLSerializerContext) -> GDataXMLElement? {
let brick = super.xmlElement(for: "StopScriptBrick", with: context)
let numberString = String(format: "%i", arguments: [self.selection.rawValue])
let selection = GDataXMLElement(name: "selection", stringValue: numberString, context: context)

brick?.addChild(selection, context: context)
return brick
}
}
53 changes: 53 additions & 0 deletions src/Catty/DataModel/SpriteObject/StopStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* 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/.
*/

enum StopStyle: Int, CaseIterable {
case thisScript = 0
case allScripts = 1
case otherScripts = 2

//TODO use kLocalizedLeftRight
func localizedString() -> String {
switch self {
case .thisScript:
return "this script"
case .allScripts:
return "all scripts"
case .otherScripts:
return "other scripts of this actor or object"
}
}

static func from(rawValue: Int) -> StopStyle? {
for style in StopStyle.allCases where style.rawValue == rawValue {
return style
}
return nil
}

static func from(localizedString: String) -> StopStyle? {
for style in StopStyle.allCases where style.localizedString() == localizedString {
return style
}
return nil
}
}
1 change: 1 addition & 0 deletions src/Catty/Defines/LanguageTranslationDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@
#define kLocalizedEndOfLoop NSLocalizedString(@"End of Loop", nil)
#define kLocalizedWhen NSLocalizedString(@"When", nil)
#define kLocalizedBecomesTrue NSLocalizedString(@"becomes true", nil)
#define kLocalizedStop NSLocalizedString(@"Stop", nil)

// motion bricks
#define kLocalizedPlaceAt NSLocalizedString(@"Place at ", nil)
Expand Down
1 change: 1 addition & 0 deletions src/Catty/Defines/LanguageTranslationDefinesSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ let kLocalizedTimes = NSLocalizedString("times", comment: "")
let kLocalizedEndOfLoop = NSLocalizedString("End of Loop", comment: "")
let kLocalizedWhen = NSLocalizedString("When", comment: "")
let kLocalizedBecomesTrue = NSLocalizedString("becomes true", comment: "")
let kLocalizedStop = NSLocalizedString("Stop", comment: "")

// motion bricks
let kLocalizedPlaceAt = NSLocalizedString("Place at ", comment: "")
Expand Down
2 changes: 2 additions & 0 deletions src/Catty/PlayerEngine/Protocols/CBSchedulerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ protocol CBSchedulerProtocol: AnyObject {
func runNextInstructionsGroup()
func scheduleContext(_ context: CBScriptContextProtocol)
func stopContext(_ context: CBScriptContextProtocol, continueWaitingBroadcastSenders: Bool)
func stopAllScripts()
func stopAllOtherScripts(_ context: CBScriptContextProtocol)

// timers
func registerTimer(_ timer: ExtendedTimer)
Expand Down
19 changes: 18 additions & 1 deletion src/Catty/PlayerEngine/Scheduler/CBScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,31 @@ final class CBScheduler: CBSchedulerProtocol {
if context.state == .running || context.state == .waiting {
_broadcastHandler.terminateAllCalledBroadcastContextsAndRemoveWaitingContext(context)
}

scheduleContext(context)
}

//runNextInstructionsGroup()

}

func stopAllScripts() {
_scheduledContexts.orderedValues.forEach {
$0.forEach {
stopContext($0, continueWaitingBroadcastSenders: false)
}
}
}

func stopAllOtherScripts(_ context: CBScriptContextProtocol) {
_scheduledContexts.orderedValues.forEach {
$0.forEach {
if $0.spriteNode.name == context.spriteNode.name && $0.id != context.id {
stopContext($0, continueWaitingBroadcastSenders: false)
}
}
}
}

func stopContext(_ context: CBScriptContextProtocol, continueWaitingBroadcastSenders: Bool) {
guard let spriteName = context.spriteNode.name else { fatalError("Sprite node has no name!") }
// assert(!_broadcastHandler.isWaitingForCalledBroadcastContexts(context))
Expand Down
3 changes: 3 additions & 0 deletions src/Catty/Resources/Localization/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,9 @@
/* No comment provided by engineer. */
"Stitch" = "Stitch";

/* No comment provided by engineer. */
"Stop" = "Stop";

/* No comment provided by engineer. */
"Stop all sounds" = "Stop all sounds";

Expand Down
1 change: 1 addition & 0 deletions src/Catty/Setup/CatrobatSetup+Bricks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
NoteBrick(),
WaitUntilBrick(),
LoopEndBrick(),
StopScriptBrick(),
// motion bricks
PlaceAtBrick(),
GlideToBrick(),
Expand Down
38 changes: 38 additions & 0 deletions src/Catty/StopScriptBrick+Instruction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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 StopScriptBrick: CBInstructionProtocol {

@nonobjc func instruction() -> CBInstruction {
CBInstruction.execClosure { context, scheduler in
switch self.selection {
case .thisScript:
scheduler.stopContext(context, continueWaitingBroadcastSenders: false)
case .allScripts:
scheduler.stopAllScripts()
case .otherScripts:
scheduler.stopAllOtherScripts(context)
scheduler.runNextInstructionOfContext(context)
}
}
}
}
Loading

0 comments on commit c5d2db6

Please sign in to comment.