// // LocationsController.swift // spotTheSpy // // Created by Paolo Alberti on 17/05/18. // Copyright © 2018 Paolo. All rights reserved. // import UIKit private let reuseIdentifier = "Cell" class LocationsController: UICollectionViewController, UICollectionViewDelegateFlowLayout { @IBOutlet var graphicSel: UISegmentedControl! var daSelezionare = false var nTentativi = 0 override func viewDidLoad() { super.viewDidLoad() // Register cell classes self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) graphicSel.backgroundColor = .clear graphicSel.tintColor = .clear graphicSel.setTitleTextAttributes([NSAttributedStringKey.font : UIFont(name: fontBurbank, size: 18)!, NSAttributedStringKey.foregroundColor: UIColor.lightGray], for: .normal) graphicSel.setTitleTextAttributes([NSAttributedStringKey.font : UIFont(name: fontBurbank, size: 18)!, NSAttributedStringKey.foregroundColor: UIColor.orange], for: .selected) // Carico le impostazioni generali della Partita switch DataManager.shared.stileGrafica { case "a": self.graphicSel.selectedSegmentIndex = 0 case "b": self.graphicSel.selectedSegmentIndex = 1 case "c": self.graphicSel.selectedSegmentIndex = 2 default: break } nTentativi = DataManager.shared.nSpie } @IBAction func chiudiView(_ sender: UIBarButtonItem) { if daSelezionare { let appearance = SCLAlertView.SCLAppearance( kTitleFont: UIFont(name: fontBurbank, size: 26)!, kTextFont: UIFont(name: fontBurbank, size: 20)!, kButtonFont: UIFont(name: fontBurbank, size: 20)!, showCloseButton: true ) let alertView = SCLAlertView(appearance: appearance) alertView.showWarning(NSLocalizedString("Attenzione", comment: ""), subTitle: NSLocalizedString("Msg_seleziona", comment: "")) } else { self.dismiss(animated: true, completion: nil) } } @IBAction func selStyle(_ sender: UISegmentedControl) { DataManager.shared.lightGen.impactOccurred() switch self.graphicSel.selectedSegmentIndex { case 0: DataManager.shared.cambiaStile(stile: "a"); case 1: DataManager.shared.cambiaStile(stile: "b"); case 2: DataManager.shared.cambiaStile(stile: "c"); default: break } DataManager.shared.NuovaPartitaC?.graphicSel.selectedSegmentIndex = self.graphicSel.selectedSegmentIndex collectionView?.reloadData() } // MARK: UICollectionViewDataSource override func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return DataManager.shared.nLocations } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "luogoCell", for: indexPath) as! LocationsCell let luogo : String = String(indexPath.row + 1) let luogoID = "l"+luogo let luogoTxt = NSLocalizedString(luogoID, comment: "") let fileImg = luogo.leftPadding(toLength: 2, withPad: "0") + "_" + String(DataManager.shared.stileGrafica) cell.locationImg.image = UIImage(named: fileImg) cell.locationLabel.text = luogoTxt return cell } override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { if daSelezionare { print(indexPath.row + 1) print(DataManager.shared.location) if (indexPath.row + 1) == DataManager.shared.location { // indovinato DataManager.shared.TimerC?.stopTimer() Sound.play(file: "vittoria.wav") let luogo : String = String(DataManager.shared.location) let fileImg = luogo.leftPadding(toLength: 2, withPad: "0") + "_" + String(DataManager.shared.stileGrafica) DataManager.shared.TimerC?.centerImg.image = UIImage(named: fileImg) self.dismiss(animated: true, completion: nil) } else { if nTentativi == 1 { // Era l'ultimo tentativo, non ha indovinato DataManager.shared.TimerC?.stopTimer() Sound.play(file: "failure.mp3") let luogo : String = String(DataManager.shared.location) let fileImg = luogo.leftPadding(toLength: 2, withPad: "0") + "_" + String(DataManager.shared.stileGrafica) DataManager.shared.TimerC?.centerImg.image = UIImage(named: fileImg) self.dismiss(animated: true, completion: nil) } else { nTentativi -= 1 let appearance = SCLAlertView.SCLAppearance( kTitleFont: UIFont(name: fontBurbank, size: 26)!, kTextFont: UIFont(name: fontBurbank, size: 20)!, kButtonFont: UIFont(name: fontBurbank, size: 20)!, showCloseButton: true ) let alertView = SCLAlertView(appearance: appearance) alertView.showWarning(NSLocalizedString("Sbagliato", comment: ""), subTitle: NSLocalizedString("Msg_altro_tentativo", comment: "")) } } } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { var nColonne : CGFloat = 3.0 if (UIDevice.current.userInterfaceIdiom == .pad) { nColonne = 5.0 } let width = (self.view.frame.size.width - (nColonne + 1) * 2) / nColonne let height = width * 1 //ratio return CGSize(width: width, height: height) } }