|
| 1 | +import UIKit |
| 2 | + |
| 3 | +public class EmbPattern { |
| 4 | + public var blocks = [StitchBlock]() |
| 5 | + private var currentPoint = CGPoint() |
| 6 | + private var currentBlock = StitchBlock() |
| 7 | + private var threadList = [UIColor]() |
| 8 | + |
| 9 | + func addThread(_ thread : UIColor) { |
| 10 | + threadList.append(thread) |
| 11 | + } |
| 12 | + |
| 13 | + func addStitchRel(point: CGPoint, stitchType: StitchType) { |
| 14 | + let tempPoint = CGPoint(x: point.x + currentPoint.x, y: point.y + currentPoint.y) |
| 15 | + self.addStitchAbs(point: tempPoint, stitchType: stitchType) |
| 16 | + } |
| 17 | + |
| 18 | + func addStitchAbs(point: CGPoint, stitchType: StitchType) { |
| 19 | + if blocks.count == 0 { |
| 20 | + if(threadList.count == 0) { |
| 21 | + let newThread = UIColor(red: CGFloat(Float(arc4random()) / Float(UINT32_MAX)), green: CGFloat(Float(arc4random()) / Float(UINT32_MAX)), blue:CGFloat(Float(arc4random()) / Float(UINT32_MAX)), alpha: 1.0) |
| 22 | + self.threadList.append(newThread) |
| 23 | + currentBlock.Color = newThread |
| 24 | + } else{ |
| 25 | + currentBlock.Color = self.threadList[0] |
| 26 | + } |
| 27 | + blocks.append(currentBlock) |
| 28 | + } |
| 29 | + if stitchType == StitchType.End { |
| 30 | + if self.currentBlock.Points.count == 0 { |
| 31 | + self.blocks.remove(at: self.blocks.count - 1) |
| 32 | + return |
| 33 | + } |
| 34 | + } |
| 35 | + if (stitchType.rawValue & StitchType.Stop.rawValue) > 0 { |
| 36 | + if self.currentBlock.Points.count == 0 { |
| 37 | + return |
| 38 | + } |
| 39 | + var threadIndex = 0 |
| 40 | + let currIndex = self.threadList.firstIndex(of: self.currentBlock.Color) |
| 41 | + if currIndex == nil || (currIndex! + 1) >= self.threadList.count { |
| 42 | + let newThread = UIColor(red: CGFloat(Float(arc4random()) / Float(UINT32_MAX)), green: CGFloat(Float(arc4random()) / Float(UINT32_MAX)), blue:CGFloat(Float(arc4random()) / Float(UINT32_MAX)), alpha: 1.0) |
| 43 | + self.threadList.append(newThread) |
| 44 | + } |
| 45 | + if currIndex == nil { |
| 46 | + threadIndex = 0 |
| 47 | + } else { |
| 48 | + threadIndex = currIndex! + 1 |
| 49 | + } |
| 50 | + let sb = StitchBlock() |
| 51 | + self.currentBlock = sb |
| 52 | + sb.Color = self.threadList[threadIndex] |
| 53 | + self.blocks.append(sb) |
| 54 | + return |
| 55 | + } |
| 56 | + if (stitchType.rawValue & StitchType.Trim.rawValue) > 0 { |
| 57 | + currentPoint = point |
| 58 | + if self.currentBlock.Points.count == 0 { |
| 59 | + return |
| 60 | + } |
| 61 | + let currIndex = self.threadList.firstIndex(of: self.currentBlock.Color) |
| 62 | + let sb = StitchBlock() |
| 63 | + self.currentBlock = sb |
| 64 | + sb.Color = self.threadList[currIndex!] |
| 65 | + self.blocks.append(sb) |
| 66 | + return |
| 67 | + } |
| 68 | + currentPoint = point |
| 69 | + if (stitchType.rawValue & StitchType.Jump.rawValue) == 0 { |
| 70 | + self.currentBlock.Points.append(point) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + func calculateBoundingBox() -> CGRect { |
| 75 | + var left = CGFloat.infinity |
| 76 | + var top = CGFloat.infinity |
| 77 | + var right = -CGFloat.infinity |
| 78 | + var bottom = -CGFloat.infinity |
| 79 | + for sb in self.blocks { |
| 80 | + let dim = sb.getBounds() |
| 81 | + left = min(left, dim.minX) |
| 82 | + top = min(top, dim.minY) |
| 83 | + right = max(right, dim.maxX) |
| 84 | + bottom = max(bottom, dim.maxY) |
| 85 | + } |
| 86 | + return CGRect(x: left, y: top, width: right - left, height: bottom - top) |
| 87 | + } |
| 88 | +} |
0 commit comments