Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CATTY-727 Implement minimal functionallity for SVG Plot creation #1838

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions src/Catty.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions src/Catty/DataModel/Bricks/Plot/SavePlotSVGBrick.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright (C) 2010-2024 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(SavePlotSVGBrick)
@objcMembers class SavePlotSVGBrick: Brick, BrickProtocol, BrickFormulaProtocol {

var filename: Formula?

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

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

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

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

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

func formula(forLineNumber lineNumber: Int, andParameterNumber paramNumber: Int) -> Formula! {
self.filename
}

func setFormula(_ formula: Formula!, forLineNumber lineNumber: Int, andParameterNumber paramNumber: Int) {
self.filename = formula
}

func getFormulas() -> [Formula]! {
[filename!]
}

override func setDefaultValuesFor(_ spriteObject: SpriteObject!) {
self.filename = Formula(string: spriteObject.name! + ".svg") ?? Formula(string: kLocalizedSavePlotDefaultFile)
}

func allowsStringFormula() -> Bool {
true
}

override func isDisabledForBackground() -> Bool {
false
}
}
50 changes: 50 additions & 0 deletions src/Catty/DataModel/Bricks/Plot/StartPlotBrick.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (C) 2010-2024 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(StartPlotBrick)
@objcMembers class StartPlotBrick: Brick, BrickProtocol {

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

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

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

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

}

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

override func isDisabledForBackground() -> Bool {
true
}
}
49 changes: 49 additions & 0 deletions src/Catty/DataModel/Bricks/Plot/StopPlotBrick.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (C) 2010-2024 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(StopPlotBrick)
@objcMembers class StopPlotBrick: Brick, BrickProtocol {

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

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

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

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

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

override func isDisabledForBackground() -> Bool {
true
}
}
7 changes: 7 additions & 0 deletions src/Catty/DataModel/Configuration/PenConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

struct PenConfiguration {
var penDown = false
var isCut = false

static let sizeConversionFactor = CGFloat(0.634)

private(set) var size: CGFloat
Expand All @@ -38,6 +40,11 @@ struct PenConfiguration {

var color = SpriteKitDefines.defaultPenColor
var previousPositions = SynchronizedArray<CGPoint>()
var previousPositionLines = SynchronizedArray<SynchronizedArray<CGPoint>>()

var previousCutPositions = SynchronizedArray<CGPoint>()
var drawnCutPoints = 0
var previousCutPositionLines = SynchronizedArray<SynchronizedArray<CGPoint>>()

init(projectWidth: CGFloat?, projectHeight: CGFloat?) {

Expand Down
2 changes: 2 additions & 0 deletions src/Catty/Defines/KeychainUserDefaultsDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define kUseArduinoBricks @"useArduinoBricks"
#define kUseEmbroideryBricks @"useEmbroideryBricks"
#define kUseWebRequestBrick @"useWebRequestBrick"
#define kUsePlotBricks @"usePlotBricks"

#define kUserPrivacyPolicyHasBeenShown @"privacyPolicyHasBeenShown"
#define kUserShowPrivacyPolicyOnEveryLaunch @"showPrivacyPolicyOnEveryLaunch"
Expand All @@ -37,3 +38,4 @@
#define kEmbroideryActivated 1
#define kFirebaseSendCrashReportsDefault 1
#define kWebRequestBrickActivated 0
#define kPlotActivated 1
7 changes: 7 additions & 0 deletions src/Catty/Defines/LanguageTranslationDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,12 @@
#define kLocalizedBlue NSLocalizedString(@"blue", nil)
#define kLocalizedStamp NSLocalizedString(@"Stamp", nil)

// plot bricks
#define kLocalizedStartPlot NSLocalizedString(@"Start to plot", nil)
#define kLocalizedStopPlot NSLocalizedString(@"Stop to plot", nil)
#define kLocalizedSavePlot NSLocalizedString(@"Save plot as SVG", nil)
#define kLocalizedSavePlotDefaultFile NSLocalizedString(@"plot.svg", nil)

// sound bricks
#define kLocalizedSound NSLocalizedString(@"Sound", nil)
#define kLocalizedPlaySound NSLocalizedString(@"Start sound", nil)
Expand Down Expand Up @@ -913,6 +919,7 @@
#define kLocalizedCategoryPhiro NSLocalizedString(@"Phiro", nil)
#define kLocalizedCategoryPen NSLocalizedString(@"Pen", nil)
#define kLocalizedCategoryEmbroidery NSLocalizedString(@"Embroidery", nil)
#define kLocalizedCategoryPlot NSLocalizedString(@"Plot", nil)

//************************************************************************************************************
//************************************ PhiroDefines ********************************************
Expand Down
7 changes: 7 additions & 0 deletions src/Catty/Defines/LanguageTranslationDefinesSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,12 @@ let kLocalizedGreen = NSLocalizedString("green", comment: "")
let kLocalizedBlue = NSLocalizedString("blue", comment: "")
let kLocalizedStamp = NSLocalizedString("Stamp", comment: "")

// plot bricks
let kLocalizedStartPlot = NSLocalizedString("Start to plot", comment: "")
let kLocalizedStopPlot = NSLocalizedString("Stop to plot", comment: "")
let kLocalizedSavePlot = NSLocalizedString("Save plot as SVG", comment: "")
let kLocalizedSavePlotDefaultFile = NSLocalizedString("plot.svg", comment: "")

// sound bricks
let kLocalizedSound = NSLocalizedString("Sound", comment: "")
let kLocalizedPlaySound = NSLocalizedString("Start sound", comment: "")
Expand Down Expand Up @@ -913,6 +919,7 @@ let kLocalizedCategoryArduino = NSLocalizedString("Arduino", comment: "")
let kLocalizedCategoryPhiro = NSLocalizedString("Phiro", comment: "")
let kLocalizedCategoryPen = NSLocalizedString("Pen", comment: "")
let kLocalizedCategoryEmbroidery = NSLocalizedString("Embroidery", comment: "")
let kLocalizedCategoryPlot = NSLocalizedString("Plot", comment: "")

//************************************************************************************************************
//************************************ PhiroDefines ********************************************
Expand Down
1 change: 1 addition & 0 deletions src/Catty/Defines/UIDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typedef NS_ENUM(NSUInteger, kBrickCategoryType) {
kPhiroBrick = 8,
kPenBrick = 9,
kEmbroideryBrick = 10,
kPlotBrick = 11,
kInvisible = 99,
kRecentlyUsedBricks = 0
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,43 @@
extension CBSpriteNode {

@objc func drawPenLine() {
//swiftlint:disable:next unused_enumerated
for (_, positionLine) in self.penConfiguration.previousPositionLines.enumerated() {
drawLineFromConfiguration(with: positionLine)
}
self.penConfiguration.previousPositionLines.removeAll()
if self.penConfiguration.previousPositions.last != self.position && penConfiguration.penDown {
self.penConfiguration.previousPositions.append(self.position)
}
let positions = self.penConfiguration.previousPositions
drawLineFromConfiguration(with: positions)
if positions.count > 1 {
self.penConfiguration.previousPositions.removeSubrange(0..<positions.count - 1)
}
}

if penConfiguration.previousPositions.last != self.position && penConfiguration.penDown {
penConfiguration.previousPositions.append(self.position)
@objc func drawPlotLine() {
//swiftlint:disable:next unused_enumerated
for (_, cutPositionLine) in self.penConfiguration.previousCutPositionLines.enumerated() {
drawLineFromConfiguration(with: cutPositionLine)
}
if self.penConfiguration.previousCutPositions.last != self.position && penConfiguration.isCut {
self.penConfiguration.previousCutPositions.append(self.position)
}
drawLineFromConfiguration(with: self.penConfiguration.previousCutPositions, from: self.penConfiguration.drawnCutPoints - 1)
self.penConfiguration.drawnCutPoints = self.penConfiguration.previousCutPositions.count
}

let positionCount = penConfiguration.previousPositions.count
private func drawLineFromConfiguration(with positions: SynchronizedArray<CGPoint>, from startIndex: Int = 0) {
let positionCount = positions.count
if positionCount > 1 {
for (index, point) in penConfiguration.previousPositions.enumerated() where index > 0 {
guard let lineFrom = penConfiguration.previousPositions[index - 1] else {
for (index, point) in positions.enumerated() where index > startIndex && index > 0 {
guard let lineFrom = positions[index - 1] else {
fatalError("This should never happen")
}
let lineTo = point

self.addLine(from: lineFrom, to: lineTo, withColor: penConfiguration.color, withSize: penConfiguration.size)
self.addLine(from: lineFrom, to: point, withColor: penConfiguration.color, withSize: penConfiguration.size)
}

penConfiguration.previousPositions.removeSubrange(0..<positionCount - 1)
}

}

private func addLine(from startPoint: CGPoint, to endPoint: CGPoint, withColor color: UIColor, withSize size: CGFloat) {
Expand All @@ -53,5 +71,4 @@ extension CBSpriteNode {

self.scene?.addChild(line)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ extension CBSpriteNode {
if self.penConfiguration.penDown {
self.penConfiguration.previousPositions.append(CGPoint(x: self.position.x, y: self.position.y))
}
if self.penConfiguration.isCut {
self.penConfiguration.previousCutPositions.append(CGPoint(x: self.position.x, y: self.position.y))
}
if let activePattern = embroideryStream.activePattern {
activePattern.spriteDidMove(to: CGPoint(x: self.position.x, y: self.position.y), rotation: Double(self.catrobatRotation))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ extension UIColor {
UIColor(red: 241.0 / 255.0, green: 167.0 / 255.0, blue: 126.0 / 255.0, alpha: 1.0)
}

static var plotBrick: UIColor {
UIColor(red: 145.0 / 255.0, green: 13.0 / 255.0, blue: 6.0 / 255.0, alpha: 1.0)
}

static var plotBrickStroke: UIColor {
UIColor(red: 117.0 / 255.0, green: 7.0 / 255.0, blue: 1.0 / 255.0, alpha: 1.0)
}

static var variableBrickRed: UIColor {
UIColor(red: 234.0 / 255.0, green: 59.0 / 255.0, blue: 59.0 / 255.0, alpha: 1.0)
}
Expand Down
4 changes: 3 additions & 1 deletion src/Catty/Helpers/Util/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ if (__functor) __functor(__VA_ARGS__); \

+ (void)setNetworkActivityIndicator:(BOOL)enabled;

+ (BOOL)isPhiroActivated;

+ (BOOL)isArduinoActivated;

+ (BOOL)isEmbroideryActivated;

+ (BOOL)isPhiroActivated;
+ (BOOL)isPlotActivated;

+ (BOOL)isPhone;

Expand Down
5 changes: 5 additions & 0 deletions src/Catty/Helpers/Util/Util.m
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,11 @@ + (BOOL)isEmbroideryActivated
return kEmbroideryActivated == 1;
}

+ (BOOL)isPlotActivated
{
return kPlotActivated == 1;
}

+ (BOOL)isPhone
{
#ifdef IS_IPHONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class CBSpriteNode: SKSpriteNode {

@objc func update(_ currentTime: TimeInterval) {
self.drawPenLine()
self.drawPlotLine()
self.drawEmbroidery()

for script in self.spriteObject.scriptList where ((script as? WhenConditionScript) != nil) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ extension PenUpBrick: CBInstructionProtocol {
else { fatalError("This should never happen!") }

return {
spriteNode.penConfiguration.previousPositionLines.append(spriteNode.penConfiguration.previousPositions)
spriteNode.penConfiguration.previousPositions = SynchronizedArray<CGPoint>()
spriteNode.penConfiguration.penDown = false
}
}
Expand Down
Loading
Loading