-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSavesViewController.swift
157 lines (125 loc) · 5.46 KB
/
SavesViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// SavesViewController.swift
// Sokoban
//
// Created by Максим on 08.11.16.
// Copyright © 2016 Максим. All rights reserved.
//
import UIKit
class SavesViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var loadButton: UIButton!
@IBOutlet weak var deleteButton: UIButton!
@IBOutlet weak var playButton: UIButton!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var hintTextField: UITextField!
var savedLevels: [SokobanData]! = []
var levels: [SokobanData]!
var currentIndex: Int! = 0
@IBAction func tapLoad(_ sender: UIButton) {
if savedLevels.count > currentIndex {
self.performSegue(withIdentifier: "savesToGame", sender: self)
}
}
@IBAction func tapDelete(_ sender: UIButton) {
if savedLevels.count > 0 {
print("Current index = \(currentIndex)")
scrollView.subviews[currentIndex*2].removeFromSuperview()
scrollView.subviews[currentIndex*2].removeFromSuperview()
savedLevels.remove(at: currentIndex)
//сдвигает все следущие views влево на страницу
for (i, view) in scrollView.subviews.enumerated() {
if i >= currentIndex*2 {
view.center.x = view.center.x - scrollView.bounds.width
}
}
scrollView.contentSize = CGSize(width: CGFloat(savedLevels.count)*scrollView.bounds.width,
height: scrollView.bounds.height)
var saves = NSKeyedUnarchiver.unarchiveObject(withFile: SokobanData.getLevelPath(name: "saves")) as? [String]
if saves != nil {
if saves!.count > currentIndex {
saves!.remove(at: currentIndex)
let isSuccessfulSave =
NSKeyedArchiver.archiveRootObject(saves!, toFile: SokobanData.getLevelPath(name: "saves"))
if isSuccessfulSave == true {
print("deleted save")
}
}
}
}
}
func updateScrollView(levels: [SokobanData]) {
if levels.count == 0 {
return
}
for (i, level) in savedLevels.enumerated() {
let view = UIImageView()
view.frame = CGRect(x: CGFloat(i) * scrollView.bounds.width + 17,
y: 0,
width: 341,
height: 389)
view.image = UIImage(named: "scroll-view.png")!
//view.image = textToImage(drawText: text, inImage: view.image!, atPoint: CGPoint(x: 10, y: 10))
scrollView.addSubview(view)
scrollView.addSubview(level.levelView)
level.levelView.center = view.center
scrollView.delegate = self
}
scrollView.contentSize = CGSize(width: CGFloat(savedLevels.count)*scrollView.bounds.width,
height: scrollView.bounds.height)
scrollView.isPagingEnabled = true
scrollView.showsHorizontalScrollIndicator = false
scrollView.showsVerticalScrollIndicator = false
}
override func viewDidLoad() {
super.viewDidLoad()
let levelNames = ["level_1", "level_2", "level_3"]
levels = []
for name in levelNames {
if let level = loadLevel(name: name) {
print("level \(name) loaded")
levels.append(level)
}
}
let saves = NSKeyedUnarchiver.unarchiveObject(withFile: SokobanData.getLevelPath(name: "saves")) as? [String]
if saves != nil {
print("Saves:", saves!)
for saveName in saves! {
if let level = loadLevel(name: saveName) {
print("level \(saveName) loaded")
savedLevels.append(level)
}
}
}
updateScrollView(levels: savedLevels)
}
// MARK: NSCoding
func loadLevel(name: String) -> SokobanData? {
return NSKeyedUnarchiver.unarchiveObject(withFile: SokobanData.getLevelPath(name: name)) as? SokobanData
}
//MARK: UIScrollViewDelegate
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//find the page number you are on
let pageWidth = scrollView.frame.size.width
let page = Int(floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1)
currentIndex = page
if currentIndex > 0 {
hintTextField.isHidden = true
} else {
hintTextField.isHidden = false
}
}
// MARK: Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "savesToGame" {
print(segue.identifier!)
print("index:",currentIndex)
print("sending saved level with name \(savedLevels[currentIndex].name)")
if let destinationViewController = segue.destination as? SokobanViewController {
destinationViewController.data = levels
destinationViewController.loaded = true
destinationViewController.sokobanData = savedLevels[currentIndex]
destinationViewController.levelNumber = currentIndex
}
}
}
}