Skip to content

Commit

Permalink
More clear board initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Benzi-Tobar committed Apr 23, 2019
1 parent f25d030 commit 254db5a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 18 deletions.
31 changes: 31 additions & 0 deletions TetraVex/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,37 @@
<customView identifier="TemplatePieceView" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="s9g-zA-QnV" customClass="TVPieceView" customModule="TetraVex" customModuleProvider="target">
<rect key="frame" x="275" y="122" width="90" height="90"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES" flexibleMinY="YES"/>
<shadow key="shadow" blurRadius="3">
<size key="offset" width="5" height="-5"/>
<color key="color" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</shadow>
<contentFilters>
<ciFilter name="CIToneCurve">
<configuration>
<null key="inputImage"/>
<ciVector key="inputPoint0">
<real value="0.0"/>
<real value="0.0"/>
</ciVector>
<ciVector key="inputPoint1">
<real value="0.25"/>
<real value="0.25"/>
</ciVector>
<ciVector key="inputPoint2">
<real value="0.5"/>
<real value="0.5"/>
</ciVector>
<ciVector key="inputPoint3">
<real value="0.75"/>
<real value="0.75"/>
</ciVector>
<ciVector key="inputPoint4">
<real value="1"/>
<real value="1"/>
</ciVector>
</configuration>
</ciFilter>
</contentFilters>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="color" keyPath="backgroundColor">
<color key="value" name="Board Background Color"/>
Expand Down
33 changes: 31 additions & 2 deletions TetraVex/TVBoardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -35,10 +42,10 @@ class TVBoardView: NSView {
path.fill()
path.stroke()


guard let model = model else {
return
}

let pathSquare = NSBezierPath(
rect: NSRect(
x: 0,
Expand All @@ -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)
}
}
}
Expand All @@ -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)

}
}
29 changes: 15 additions & 14 deletions TetraVex/TVGameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions TetraVex/TVPieceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 254db5a

Please sign in to comment.