Skip to content

Commit

Permalink
CATTY-369 Implement place visually feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoeg authored and wallisch committed Feb 2, 2023
1 parent f9736bd commit 1174623
Show file tree
Hide file tree
Showing 55 changed files with 1,051 additions and 566 deletions.
52 changes: 28 additions & 24 deletions src/Catty.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Catty/DataModel/Bricks/Brick.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

- (BOOL)isFormulaBrick;

- (BOOL)isVisualPlacementBrick;

- (BOOL)isIfLogicBrick;

- (BOOL)isLoopBrick;
Expand Down
6 changes: 6 additions & 0 deletions src/Catty/DataModel/Bricks/Brick.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#import "BroadcastScript.h"
#import "CBMutableCopyContext.h"
#import "Util.h"
#import "Pocket_Code-Swift.h"

@implementation Brick

Expand All @@ -52,6 +53,11 @@ - (BOOL)isFormulaBrick
return ([self conformsToProtocol:@protocol(BrickFormulaProtocol)]);
}

- (BOOL)isVisualPlacementBrick
{
return ([self conformsToProtocol:@protocol(BrickVisualPlacementProtocol)]);
}

- (BOOL)isIfLogicBrick
{
return NO;
Expand Down
60 changes: 60 additions & 0 deletions src/Catty/DataModel/Bricks/Motion/GlideToBrick+CBXMLHandler.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/.
*/

extension GlideToBrick: CBXMLNodeProtocol {
static func parse(from xmlElement: GDataXMLElement!, with context: CBXMLParserContext!) -> Self! {
CBXMLParserHelper.validate(xmlElement, forNumberOfChildNodes: 1, andFormulaListWithTotalNumberOfFormulas: 3)

let glideToBrick = self.init()
if let formulaDuration = CBXMLParserHelper.formula(in: xmlElement, forCategoryName: "DURATION_IN_SECONDS", with: context) {
glideToBrick.durationInSeconds = formulaDuration
}
if let formulaXDestination = CBXMLParserHelper.formula(in: xmlElement, forCategoryName: "X_DESTINATION", with: context) {
glideToBrick.xPosition = formulaXDestination
}
if let formulaYDestination = CBXMLParserHelper.formula(in: xmlElement, forCategoryName: "Y_DESTINATION", with: context) {
glideToBrick.yPosition = formulaYDestination
}

return glideToBrick
}

func xmlElement(with context: CBXMLSerializerContext!) -> GDataXMLElement! {
let brick = super.xmlElement(for: "GlideToBrick", with: context)
let formulaList = GDataXMLElement(name: "formulaList", context: context)

var formula = self.durationInSeconds.xmlElement(with: context)
formula?.addAttribute(GDataXMLElement.attribute(withName: "category", escapedStringValue: "DURATION_IN_SECONDS") as? GDataXMLNode)
formulaList?.addChild(formula, context: context)

formula = self.yPosition.xmlElement(with: context)
formula?.addAttribute(GDataXMLElement.attribute(withName: "category", escapedStringValue: "Y_DESTINATION") as? GDataXMLNode)
formulaList?.addChild(formula, context: context)

formula = self.xPosition.xmlElement(with: context)
formula?.addAttribute(GDataXMLElement.attribute(withName: "category", escapedStringValue: "X_DESTINATION") as? GDataXMLNode)
formulaList?.addChild(formula, context: context)

brick?.addChild(formulaList, context: context)
return brick
}
}
35 changes: 0 additions & 35 deletions src/Catty/DataModel/Bricks/Motion/GlideToBrick.h

This file was deleted.

104 changes: 0 additions & 104 deletions src/Catty/DataModel/Bricks/Motion/GlideToBrick.m

This file was deleted.

102 changes: 102 additions & 0 deletions src/Catty/DataModel/Bricks/Motion/GlideToBrick.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* 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(GlideToBrick)
@objcMembers class GlideToBrick: Brick, BrickVisualPlacementProtocol {
var durationInSeconds: Formula
var xPosition: Formula
var yPosition: Formula

override required init() {
self.durationInSeconds = Formula(integer: 1)
self.xPosition = Formula(integer: 100)
self.yPosition = Formula(integer: 200)
super.init()
}

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

func formula(forLineNumber lineNumber: Int, andParameterNumber paramNumber: Int) -> Formula? {
if lineNumber == 0 && paramNumber == 0 {
return self.durationInSeconds
} else if lineNumber == 1 && paramNumber == 0 {
return self.xPosition
} else if lineNumber == 1 && paramNumber == 1 {
return self.yPosition
}

return nil
}

func setFormula(_ formula: Formula, forLineNumber lineNumber: Int, andParameterNumber paramNumber: Int) {
if lineNumber == 0 && paramNumber == 0 {
self.durationInSeconds = formula
} else if lineNumber == 1 && paramNumber == 0 {
self.xPosition = formula
} else if lineNumber == 1 && paramNumber == 1 {
self.yPosition = formula
}
}

func getFormulas() -> [Formula] {
[self.durationInSeconds, self.xPosition, self.yPosition]
}

func allowsStringFormula() -> Bool {
false
}

override func setDefaultValuesFor(_ spriteObject: SpriteObject!) {
self.durationInSeconds = Formula(integer: 1)
self.xPosition = Formula(integer: 100)
self.yPosition = Formula(integer: 200)
}

override func description() -> String! {
"GlideToBrick"
}

override func isEqual(to brick: Brick!) -> Bool {
if brick is GlideToBrick {
let glideToBrick = brick as! GlideToBrick
return self.durationInSeconds.isEqual(to: glideToBrick.durationInSeconds) &&
self.xPosition.isEqual(to: glideToBrick.xPosition) &&
self.yPosition.isEqual(to: glideToBrick.yPosition)
}
return false
}

func isVisualPlacementFormula(_ formula: Formula) -> Bool {
(formula.isEqual(to: xPosition) || formula.isEqual(to: yPosition)) &&
doVisualPlacementBrickCellsContainOnlyValues()
}

func doVisualPlacementBrickCellsContainOnlyValues() -> Bool {
xPosition.formulaTree.isSingleNumberFormula() && yPosition.formulaTree.isSingleNumberFormula()
}

override func getRequiredResources() -> Int {
durationInSeconds.getRequiredResources() | xPosition.getRequiredResources() | yPosition.getRequiredResources()
}
}
54 changes: 54 additions & 0 deletions src/Catty/DataModel/Bricks/Motion/PlaceAtBrick+CBXMLHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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 PlaceAtBrick: CBXMLNodeProtocol {
static func parse(from xmlElement: GDataXMLElement!, with context: CBXMLParserContext!) -> Self! {

CBXMLParserHelper.validate(xmlElement, forNumberOfChildNodes: 1, andFormulaListWithTotalNumberOfFormulas: 2)

let placeAtBrick = self.init()
if let formulaXPosition = CBXMLParserHelper.formula(in: xmlElement, forCategoryName: "X_POSITION", with: context) {
placeAtBrick.xPosition = formulaXPosition
}
if let formulaYPostion = CBXMLParserHelper.formula(in: xmlElement, forCategoryName: "Y_POSITION", with: context) {
placeAtBrick.yPosition = formulaYPostion
}
return placeAtBrick
}

func xmlElement(with context: CBXMLSerializerContext) -> GDataXMLElement? {
let brick = super.xmlElement(for: "PlaceAtBrick", with: context)
let formulaList = GDataXMLElement(name: "formulaList", context: context)

var formula = self.yPosition.xmlElement(with: context)
formula?.addAttribute(GDataXMLElement.attribute(withName: "category", escapedStringValue: "Y_POSITION") as? GDataXMLNode)
formulaList?.addChild(formula, context: context)

formula = self.xPosition.xmlElement(with: context)
formula?.addAttribute(GDataXMLElement.attribute(withName: "category", escapedStringValue: "X_POSITION") as? GDataXMLNode)
formulaList?.addChild(formula, context: context)

brick?.addChild(formulaList, context: context)
return brick
}

}
Loading

0 comments on commit 1174623

Please sign in to comment.