Skip to content

Commit

Permalink
Merge branch 'feature/AddSupportForLettersAndGreek' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Benzi committed Jun 15, 2019
2 parents bbb842f + 7bfc48b commit 880b699
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Here I will update any noteworthy changes to the TetraVex project.
- CocoaPods support.
- Icon for app.
- Basic test for UI.
- Greek and latin characters

### Changed
- Better graphics
Expand Down
73 changes: 52 additions & 21 deletions TetraVex/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
* Reminder, First Responder actions have to marked with @IBAction
* to appear in the Interface Builder.
*/

var optionMenu: NSMenuItem? {
return NSApplication.shared.mainMenu?
.item(withTitle: "Options")
}
@IBAction func setBoardTo2x2(sender: Any?) {
setBoardSize(width: 2, height: 2)
let sm = NSApplication.shared.mainMenu?
.item(withTitle: "Options")?.submenu?.item(withTitle: "Size")?.submenu
let sm = optionMenu?.submenu?.item(withTitle: "Size")?.submenu
sm?.item(withTitle: "2x2")?.state = NSControl.StateValue(rawValue: 1)
sm?.item(withTitle: "3x3")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "4x4")?.state = NSControl.StateValue(rawValue: 0)
Expand All @@ -37,8 +41,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

@IBAction func setBoardTo3x3(sender: Any?) {
setBoardSize(width: 3, height: 3)
let sm = NSApplication.shared.mainMenu?
.item(withTitle: "Options")?.submenu?.item(withTitle: "Size")?.submenu
let sm = optionMenu?.submenu?.item(withTitle: "Size")?.submenu
sm?.item(withTitle: "2x2")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "3x3")?.state = NSControl.StateValue(rawValue: 1)
sm?.item(withTitle: "4x4")?.state = NSControl.StateValue(rawValue: 0)
Expand All @@ -48,8 +51,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

@IBAction func setBoardTo4x4(sender: Any?) {
setBoardSize(width: 4, height: 4)
let sm = NSApplication.shared.mainMenu?
.item(withTitle: "Options")?.submenu?.item(withTitle: "Size")?.submenu
let sm = optionMenu?.submenu?.item(withTitle: "Size")?.submenu
sm?.item(withTitle: "2x2")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "3x3")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "4x4")?.state = NSControl.StateValue(rawValue: 1)
Expand All @@ -59,8 +61,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

@IBAction func setBoardTo5x5(sender: Any?) {
setBoardSize(width: 5, height: 5)
let sm = NSApplication.shared.mainMenu?
.item(withTitle: "Options")?.submenu?.item(withTitle: "Size")?.submenu
let sm = optionMenu?.submenu?.item(withTitle: "Size")?.submenu
sm?.item(withTitle: "2x2")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "3x3")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "4x4")?.state = NSControl.StateValue(rawValue: 0)
Expand All @@ -70,8 +71,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

@IBAction func setBoardTo6x6(sender: Any?) {
setBoardSize(width: 6, height: 6)
let sm = NSApplication.shared.mainMenu?
.item(withTitle: "Options")?.submenu?.item(withTitle: "Size")?.submenu
let sm = optionMenu?.submenu?.item(withTitle: "Size")?.submenu
sm?.item(withTitle: "2x2")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "3x3")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "4x4")?.state = NSControl.StateValue(rawValue: 0)
Expand All @@ -86,8 +86,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

@IBAction func setNumberOfDigitsTo6(sender: Any?) {
setNumberOfDigits(num: 5)
let sm = NSApplication.shared.mainMenu?
.item(withTitle: "Options")?.submenu?.item(withTitle: "Digits")?.submenu
let sm = optionMenu?.submenu?.item(withTitle: "Digits")?.submenu
sm?.item(withTitle: "6")?.state = NSControl.StateValue(rawValue: 1)
sm?.item(withTitle: "7")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "8")?.state = NSControl.StateValue(rawValue: 0)
Expand All @@ -97,8 +96,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

@IBAction func setNumberOfDigitsTo7(sender: Any?) {
setNumberOfDigits(num: 6)
let sm = NSApplication.shared.mainMenu?
.item(withTitle: "Options")?.submenu?.item(withTitle: "Digits")?.submenu
let sm = optionMenu?.submenu?.item(withTitle: "Digits")?.submenu
sm?.item(withTitle: "6")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "7")?.state = NSControl.StateValue(rawValue: 1)
sm?.item(withTitle: "8")?.state = NSControl.StateValue(rawValue: 0)
Expand All @@ -108,19 +106,17 @@ class AppDelegate: NSObject, NSApplicationDelegate {

@IBAction func setNumberOfDigitsTo8(sender: Any?) {
setNumberOfDigits(num: 7)
let sm = NSApplication.shared.mainMenu?
.item(withTitle: "Options")?.submenu?.item(withTitle: "Digits")?.submenu
let sm = optionMenu?.submenu?.item(withTitle: "Digits")?.submenu
sm?.item(withTitle: "6")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "7")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "8")?.state = NSControl.StateValue(rawValue: 1)
sm?.item(withTitle: "9")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "10")?.state = NSControl.StateValue(rawValue: 0)
}

@IBAction func setNumberOfDigitsTo9(sender: Any?) {
setNumberOfDigits(num: 8)
let sm = NSApplication.shared.mainMenu?
.item(withTitle: "Options")?.submenu?.item(withTitle: "Digits")?.submenu
let sm = optionMenu?.submenu?.item(withTitle: "Digits")?.submenu
sm?.item(withTitle: "6")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "7")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "8")?.state = NSControl.StateValue(rawValue: 0)
Expand All @@ -130,14 +126,49 @@ class AppDelegate: NSObject, NSApplicationDelegate {

@IBAction func setNumberOfDigitsTo10(sender: Any?) {
setNumberOfDigits(num: 9)
let sm = NSApplication.shared.mainMenu?
.item(withTitle: "Options")?.submenu?.item(withTitle: "Digits")?.submenu
let sm = optionMenu?.submenu?.item(withTitle: "Digits")?.submenu
sm?.item(withTitle: "6")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "7")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "8")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "9")?.state = NSControl.StateValue(rawValue: 0)
sm?.item(withTitle: "10")?.state = NSControl.StateValue(rawValue: 1)
}


@IBAction func setTextStyleToDigits(sender: Any?) {
guard let controller = NSApplication.shared.mainWindow?.contentViewController as? TVGameViewController else {
return
}

optionMenu?.submenu?.item(withTitle: "Numbers")?.state = .on
optionMenu?.submenu?.item(withTitle: "Letters")?.state = .off
optionMenu?.submenu?.item(withTitle: "Greek")?.state = .off

controller.setTextStyle(to: .digits)
}

@IBAction func setTextStyleToLetters(sender: Any?) {
guard let controller = NSApplication.shared.mainWindow?.contentViewController as? TVGameViewController else {
return
}

optionMenu?.submenu?.item(withTitle: "Numbers")?.state = .off
optionMenu?.submenu?.item(withTitle: "Letters")?.state = .on
optionMenu?.submenu?.item(withTitle: "Greek")?.state = .off

controller.setTextStyle(to: .letters)
}
@IBAction func setTextStyleToGreekSymbols(sender: Any?) {
guard let controller = NSApplication.shared.mainWindow?.contentViewController as? TVGameViewController else {
return
}

optionMenu?.submenu?.item(withTitle: "Numbers")?.state = .off
optionMenu?.submenu?.item(withTitle: "Letters")?.state = .off
optionMenu?.submenu?.item(withTitle: "Greek")?.state = .on

controller.setTextStyle(to: .greekSymbols)
}

//MARK: - Game actions
@IBAction func newGame(sender: Any?) {
Expand Down
9 changes: 9 additions & 0 deletions TetraVex/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,21 @@
<menuItem isSeparatorItem="YES" id="aOC-h8-jqT"/>
<menuItem title="Numbers" state="on" id="fH6-js-PJf">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="setTextStyleToDigitsWithSender:" target="Ady-hI-5gd" id="q4r-JI-SnB"/>
</connections>
</menuItem>
<menuItem title="Letters" id="img-Li-R8j">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="setTextStyleToLettersWithSender:" target="Ady-hI-5gd" id="8zP-1m-hKT"/>
</connections>
</menuItem>
<menuItem title="Greek" id="EUB-SS-9iA">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="setTextStyleToGreekSymbolsWithSender:" target="Ady-hI-5gd" id="GWB-Mm-nSh"/>
</connections>
</menuItem>
</items>
</menu>
Expand Down
23 changes: 23 additions & 0 deletions TetraVex/TVGameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class TVGameViewController: NSViewController
pv.removeFromSuperview()
}
if ((solvedBoard) != nil) {
visiblePieces = [TVPieceView]()
for i in 0..<width {
for j in 0..<height {
let nfr = templatePieceView.frame.offsetBy(dx: pw*CGFloat(i), dy: -ph*CGFloat(j))
Expand All @@ -82,6 +83,7 @@ class TVGameViewController: NSViewController
pv.pieceModel = solvedBoard![i][j]
pv.delegate = self
self.view.addSubview(pv)
visiblePieces?.append(pv)
}
}

Expand Down Expand Up @@ -142,6 +144,27 @@ class TVGameViewController: NSViewController
secondsPassed += 1
timerLabel.stringValue = TVHighScores.timeToString(secondsPassed)
}

// MARK: Changing the piece text style
var visiblePieces : [TVPieceView]?

func setTextStyle(to style:TVPieceModel.TextStyle) {
guard let pieces = visiblePieces else {
return
}
textStyle(for: pieces, style)
}

func textStyle(for pieces:[TVPieceView],_ style: TVPieceModel.TextStyle) {
for var piece in pieces {
guard var model = piece.pieceModel else {
continue
}
model.textStyle = style
piece.pieceModel = model
piece.needsDisplay = true
}
}

}

Expand Down
66 changes: 65 additions & 1 deletion TetraVex/TVPieceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,73 @@ class TVPieceView : NSView, NSAccessibilityButton {
}

// MARK: - Drawing operations
var textStyle: TVPieceModel.TextStyle {
return pieceModel?.textStyle ?? TVPieceModel.TextStyle.digits
}

func convert(_ str: String, with style:TVPieceModel.TextStyle) -> NSString {
switch style {
case .digits:
return NSString(string: str)
case .greekSymbols:
let nstr: String
switch (str) {
case "0":
nstr = "α"
case "1":
nstr = "β"
case "2":
nstr = "γ"
case "3":
nstr = "δ"
case "4":
nstr = "ϵ"
case "5":
nstr = "ζ"
case "6":
nstr = "η"
case "7":
nstr = "θ"
case "8":
nstr = "ι"
case "9":
nstr = "κ"
default:
nstr = "ω"
}
return NSString(string: nstr)
case .letters:
let nstr: String
switch (str) {
case "0":
nstr = "A"
case "1":
nstr = "B"
case "2":
nstr = "C"
case "3":
nstr = "D"
case "4":
nstr = "E"
case "5":
nstr = "F"
case "6":
nstr = "G"
case "7":
nstr = "H"
case "8":
nstr = "I"
case "9":
nstr = "J"
default:
nstr = "X"
}
return NSString(string: nstr)
}
}

func drawStringCenteredAt(_ center: NSPoint, str: String, attribs: [NSAttributedString.Key: Any]?) {
let nsstr = NSString(string: str)
let nsstr = convert(str, with: textStyle)
let b = nsstr.boundingRect(with: NSSize(width: 300,height: 300), options: NSString.DrawingOptions.usesFontLeading, attributes: attribs)
var dCenter = center
dCenter.x = center.x - b.width/2
Expand Down
9 changes: 9 additions & 0 deletions TetraVexKit/TVPieceModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public struct TVPieceModel {
public var rightValue : Int
public var boltedInPlace : Bool = false
public var isOnBoard : Bool = false

// MARK: - Setting a different piece letter style
public enum TextStyle {
case digits
case letters
case greekSymbols
}

public var textStyle : TextStyle = .digits

// MARK: - Initializer
public init(top: Int, left: Int, bottom: Int, right: Int) {
Expand Down

0 comments on commit 880b699

Please sign in to comment.