This repository has been archived by the owner on Oct 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSetupMenu.swift
77 lines (71 loc) · 1.99 KB
/
SetupMenu.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
//
// SetupMenu.swift
// Radio Automne
//
// Created by Aydar Nasibullin on 03.10.2020.
// Copyright © 2020-2021 Fetch Development. All rights reserved.
//
import Foundation
struct SetupMenu {
struct SetupMenuElement {
let title: String
var value: Int
let bounds : (Int, Int)
let isAction: Bool
}
var elements: [SetupMenuElement]
var selected: Int
func getRaw() -> String{
var n = " *** USE ARROW KEYS ***\n"
var i = 1
for e in self.elements{
e.isAction ?
n.append((i == self.selected ? "> " : " ") + e.title + "\n") :
n.append((i == self.selected ? "> " : " ") + e.title + " = " + String(e.value) + "\n")
i += 1
}
return n
}
public static func get(for: [SetupMenuElement]) -> SetupMenu{
return SetupMenu(elements: `for`, selected: 1)
}
public func increment(_ completion: [() -> ()] = [{}]) -> SetupMenu{
var els = self.elements
let sel = self.selected - 1
if !els[sel].isAction{
if els[sel].value < els[sel].bounds.1 {
els[sel].value += 1
}
}else{
completion[sel]()
}
return SetupMenu(elements: els, selected: self.selected)
}
public func decrement() -> SetupMenu{
var els = self.elements
if els[self.selected - 1].value > els[self.selected - 1].bounds.0 {
els[self.selected - 1].value -= 1
}
return SetupMenu(elements: els, selected: self.selected)
}
public func upSel() -> SetupMenu{
var m = self
if m.selected < m.elements.count {
m.selected += 1
}
else{
m.selected = 1
}
return m
}
public func downSel() -> SetupMenu{
var m = self
if m.selected > 1 {
m.selected -= 1
}
else{
m.selected = m.elements.count
}
return m
}
}