From 254db5a5379b5ac7fd1c09264be67b437f2f4056 Mon Sep 17 00:00:00 2001 From: Marco Benzi-Tobar Date: Tue, 23 Apr 2019 14:50:11 +0200 Subject: [PATCH] More clear board initialization --- TetraVex/Base.lproj/Main.storyboard | 31 +++++++++++++++++++++++++++ TetraVex/TVBoardView.swift | 33 +++++++++++++++++++++++++++-- TetraVex/TVGameViewController.swift | 29 +++++++++++++------------ TetraVex/TVPieceView.swift | 4 ++-- 4 files changed, 79 insertions(+), 18 deletions(-) diff --git a/TetraVex/Base.lproj/Main.storyboard b/TetraVex/Base.lproj/Main.storyboard index 7db2802..692a3c5 100644 --- a/TetraVex/Base.lproj/Main.storyboard +++ b/TetraVex/Base.lproj/Main.storyboard @@ -383,6 +383,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TetraVex/TVBoardView.swift b/TetraVex/TVBoardView.swift index 239382f..77baad5 100644 --- a/TetraVex/TVBoardView.swift +++ b/TetraVex/TVBoardView.swift @@ -17,7 +17,14 @@ class TVBoardView: NSView { @IBInspectable var pieceHeight : CGFloat = 90 @IBInspectable var insetStrokeLineWidth : CGFloat = 3 - var model : TVBoardModel? = nil + var model : TVBoardModel? = nil { + didSet { + guard let model = model else { + return + } + prepareBoard(with: model) + } + } override func mouseUp(with event: NSEvent) { @@ -35,10 +42,10 @@ class TVBoardView: NSView { path.fill() path.stroke() - guard let model = model else { return } + let pathSquare = NSBezierPath( rect: NSRect( x: 0, @@ -57,6 +64,15 @@ class TVBoardView: NSView { CGPoint(x: CGFloat(i)*pieceWidth, y: CGFloat(j)*pieceHeight), to: CGPoint(x: CGFloat(i+1)*pieceWidth, y: CGFloat(j)*pieceHeight), color: .white) + + drawInsetLine( + CGPoint(x: CGFloat(i)*pieceWidth, y: CGFloat(j)*pieceHeight), + to: CGPoint(x: CGFloat(i)*pieceWidth, y: CGFloat(j+1)*pieceHeight), + color: .gray) + drawInsetLine( + CGPoint(x: CGFloat(i)*pieceWidth, y: CGFloat(j+1)*pieceHeight), + to: CGPoint(x: CGFloat(i+1)*pieceWidth, y: CGFloat(j+1)*pieceHeight), + color: .gray) } } } @@ -74,4 +90,17 @@ class TVBoardView: NSView { model = TVBoardModel(width: 2, height: 2) } + func prepareBoard(with model:TVBoardModel) { + let topY = self.frame.origin.y + self.frame.height + let newBox = CGRect(x: self.frame.origin.x, + y: topY - (pieceHeight)*CGFloat(model.boardHeight), + width: pieceWidth*CGFloat(model.boardWidth), + height: pieceHeight*CGFloat(model.boardHeight)) + self.frame = newBox + self.bounds = NSRect(x: 0, + y: 0, + width: frame.width, + height: frame.height) + + } } diff --git a/TetraVex/TVGameViewController.swift b/TetraVex/TVGameViewController.swift index 4deffbf..d0b40b3 100644 --- a/TetraVex/TVGameViewController.swift +++ b/TetraVex/TVGameViewController.swift @@ -58,21 +58,12 @@ class TVGameViewController: NSViewController } func newBoard(_ width: Int, height: Int) { - /* Resize board outline */ - let pw = templatePieceView.frame.width - let ph = templatePieceView.frame.height - let topY = boardAreaBox.frame.origin.y + boardAreaBox.frame.height - - let newBox = CGRect(x: boardAreaBox.frame.origin.x, - y: topY - (ph)*CGFloat(height), - width: pw*CGFloat(width), - height: ph*CGFloat(height)) - boardAreaBox.frame = newBox - boardAreaBox.bounds = NSRect(x: 0, y: 0, width: boardAreaBox.frame.width, height: boardAreaBox.frame.height) - boardModel = TVBoardModel(width: width, height: height) // Refresh autolayout + let pw = templatePieceView.frame.width + let ph = templatePieceView.frame.height + boardWidthConstraint.constant = pw*CGFloat(width) boardHeightConstraint.constant = ph*CGFloat(height) @@ -151,16 +142,26 @@ class TVGameViewController: NSViewController secondsPassed += 1 timerLabel.stringValue = TVHighScores.timeToString(secondsPassed) } - + } extension TVGameViewController : TVPieceViewDelegate { func wasLiftedFromBoard(piece: TVPieceView) { removeFromBoard(piece: piece) - + guard let layer = piece.layer else { + return + } + layer.shadowColor = NSColor.black.cgColor + layer.shadowOpacity = 0.75 + layer.shadowOffset = CGSize(width: 5,height: 10) + layer.shadowRadius = 10 } func wasDropped(piece: TVPieceView, at: NSPoint) { checkPiece(with: piece, at: at) + guard let layer = piece.layer else { + return + } + layer.shadowOpacity = 0.0 } } diff --git a/TetraVex/TVPieceView.swift b/TetraVex/TVPieceView.swift index 8ee5d47..0090094 100644 --- a/TetraVex/TVPieceView.swift +++ b/TetraVex/TVPieceView.swift @@ -28,9 +28,9 @@ class TVPieceView : NSView, NSAccessibilityButton { var lastDraggedPosition : NSPoint = NSPoint() @IBInspectable var backgroundColor : NSColor = #colorLiteral(red: 0.7480000257, green: 0.7480000257, blue: 0.7480000257, alpha: 1) - @IBInspectable var roundedRectRadius : CGFloat = 2 + @IBInspectable var roundedRectRadius : CGFloat = 0 @IBInspectable var insetStrokeColor : NSColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) - @IBInspectable var insetStrokeLineWidth : CGFloat = 1 + @IBInspectable var insetStrokeLineWidth : CGFloat = 3 @IBInspectable var insetOffset: CGFloat = 3 @IBInspectable var insetShadowColor: NSColor = .gray @IBInspectable var outerStrokeColor : NSColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)