-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmenu.py
157 lines (122 loc) · 4.07 KB
/
menu.py
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
import os
os.chdir(os.path.dirname(os.path.realpath(__file__)))
from menu.menuitems import create_menu_items
from screenfactory import create_screen
import time
import math
from helpers import *
import config
import input
class Menu(object):
def __init__(self, screen, items):
self.screen = screen
input.on_press.append(self.on_key_down)
self.index = config.default_item_index
self.items = items
self.module = None
self.reset(redraw = False)
self.resume_animation()
if len(input.available_input_methods) == 0:
self.launch()
def reset(self, redraw = True):
self.dir = 0
self.offset = 0
self.zoom = 1
self.brightness = 1
if redraw:
self.draw()
def draw_on_screen(self, x, y, zoom, graphic):
if zoom == 0:
return
if self.brightness == 1 and zoom == 1:
for source_x in range(8):
for source_y in range(8):
target = Point(source_x + x - 4, source_y + y - 4)
if target.x >= 0 and target.x < 16 and target.y >= 0 and target.y < 16:
self.screen.pixel[target.x][target.y] = rgb_to_int(graphic[source_x][source_y])
return
for target_x in range(16):
for target_y in range(16):
source = Point(int((target_x - x) / zoom + 4), int((target_y - y) / zoom + 4))
if source.x >= 0 and source.y >= 0 and source.x < 8 and source.y < 8:
c = graphic[source.x][source.y]
self.screen.pixel[target_x][target_y] = Color(int(c.r * self.brightness), int(c.g * self.brightness), int(c.b * self.brightness))
def draw_scrollbar(self):
size = int(math.floor(16 / len(self.items)))
start = int(math.floor((16 - size) * self.index / (len(self.items) - 1)))
for x in range(size):
self.screen.pixel[(start + x - int(size * self.offset) + 16) % 16][15] = Color(int(80 * self.brightness), int(80 * self.brightness), int(80 * self.brightness))
def draw(self):
if self.module != None:
return
self.screen.clear()
self.draw_scrollbar()
self.draw_on_screen(8 + int(self.offset * 12), 8, self.zoom, self.items[self.index].get_preview())
if self.dir != 0:
self.draw_on_screen(8 + int(self.offset * 12) - self.dir * 12, 8, self.zoom, self.items[(self.index - self.dir + len(self.items)) % len(self.items)].get_preview())
self.screen.update()
def ease(self, x):
return x
def tick(self):
input.tick()
if (self.dir != 0):
self.offset = self.dir * self.ease((1 - (time.clock() - self.start) / (self.end - self.start)))
if (time.clock() > self.end):
self.offset = 0
self.dir = 0
self.draw()
def move(self, direction):
self.index = (self.index + direction + len(self.items)) % len(self.items)
self.dir = direction
self.start = time.clock()
self.end = self.start + 0.3
def on_key_down(self, key):
self.items[self.index].on_key_press(key, self)
if self.module != None:
if key == input.Key.HOME or key == input.Key.BACK:
self.stop()
return
if key == input.Key.RIGHT:
self.move(1)
if key == input.Key.LEFT:
self.move(-1)
if key == input.Key.ENTER or key == input.Key.A:
self.launch()
return True
def stop(self):
self.module.stop()
self.screen.fade_out(0.3)
self.module = None
self.resume_animation()
input.on_press = [self.on_key_down]
input.on_release = []
def launch(self):
if self.items[self.index].is_launchable() == False:
return
self.offset = 0
self.dir = 0
self.start_animation()
self.module = self.items[self.index].get_module(self.screen)
self.module.start()
def start_animation(self):
start = time.clock()
end = start + 1
while time.clock () <= end:
self.zoom = 1 + 16 * ((time.clock() - start) / (end - start)) ** 2
self.brightness = min(1, 1 - ((time.clock() - start) / (end - start)))
self.draw()
time.sleep(0.1)
self.reset(redraw = False)
def resume_animation(self):
start = time.clock()
end = start + 0.5
while time.clock () <= end:
self.zoom = ((time.clock() - start) / (end - start))
self.brightness = min(1, 1 * ((time.clock() - start) / (end - start)))
self.draw()
self.reset()
if __name__ == '__main__':
menu = Menu(create_screen(), create_menu_items())
while True:
menu.tick()
time.sleep(0.01)