-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
141 lines (119 loc) · 5.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
import pygame
from pygame.locals import *
import math
from input import *
def dist(rect_a,rect_b):
'''
rect_a == top block to snap to
rect_b == bottom block to check'''
return math.sqrt(((rect_a.midbottom[0]-rect_b.midtop[0])**2)+((rect_a.midbottom[1]-rect_b.midtop[1])**2))
class menu(object):
def __init__(self):
self.font = pygame.font.Font(None,48)
self.clickable =[ (button((300,160)
,self.font.render('forward_spawn',True,(0,0,255)).convert_alpha()
,self.font.render('forward_spawn',True,(255,0,00)).convert_alpha()
,self.font.render('forward_spawn',True,(0,255,0)).convert_alpha()
)
,self.spawn_fwd
),
(button((300,240)
,self.font.render('credits',True,(0,0,255)).convert_alpha()
,self.font.render('credits',True,(255,0,0)).convert_alpha()
,self.font.render('credits',True,(0,255,0)).convert_alpha()
)
,self.credits
)
]
self.dragable = [(drag_dock((300,300)
,self.font.render('drag me',True,(0,0,255)).convert_alpha()
,self.font.render('drag me',True,(255,0,0)).convert_alpha()
,self.font.render('drag me',True,(0,255,0)).convert_alpha()
)
)
]
self.receptical = [dock_button((500,200)
,self.font.render('mount me',True,(0,0,255)).convert_alpha()
,self.font.render('mount me',True,(255,0,0)).convert_alpha()
,self.font.render('mount me',True,(0,255,0)).convert_alpha()
)
]
self.dock_dist = 15
self.surf = pygame.Surface((800, 600))
def add_drag_btn(self, btn):
self.dragable.append(btn)
def remove_drag_btn(self,btn):
self.dragable.remove(btn)
del btn
def events(self,events):
'''TODO: drag/click through to ONLY one button...'''
for btn,func in self.clickable:
var = btn.events(events)
if var:
func(btn)
break#break out of events if clciked?
for btn in self.dragable:
if btn.events(events):
self.dragged(btn)
for event in events:
##debug code stuff, remove for production
if event.type == MOUSEBUTTONUP and event.button == 1:
print event.pos
##
if event.type == KEYDOWN:
if event.key == K_1:
print self.dragable[0].mount
print self.receptical[0].docked_btn
else:
print event
def draw(self,screen):
for btn,func in self.clickable:
btn.draw(screen)
for btn in self.dragable:
btn.draw(screen)
for btn in self.receptical:
btn.draw(screen)
def spawn_fwd(self,btn):
print 'fwd spawned'
b=drag_dock((300,300)
,self.font.render('drag me',True,(0,0,255)).convert_alpha()
,self.font.render('drag me',True,(255,0,0)).convert_alpha()
,self.font.render('drag me',True,(0,255,0)).convert_alpha()
)
self.add_drag_btn(b)
def credits(self,btn):
print 'credits clicked'
def dragged(self,btn):
print 'button dragged'
#if button is at top of tree... ?
#if button is on bottom, remove from stack...
for b in self.dragable:
if b.docked_btn:#has been docked, ergo, dont do any math...
continue
elif hasattr(b,'is_dock') and b.is_dock:
if dist(b.rect,btn.rect) <= self.dock_dist:
btn.rect.midtop=b.rect.midbottom
if b.dock(btn):
return
for r in self.receptical:
if not r.docked_btn:
if dist(r.rect,btn.rect) <= self.dock_dist:
btn.rect.midtop=r.rect.midbottom
r.dock(btn)
return
if __name__=='__main__':
pygame.init()
screen = pygame.display.set_mode((800, 600))
me = menu()
while True:
pygame.time.wait(10)
screen.fill((0, 0, 0))
events = pygame.event.get()
for event in events:
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
import sys
sys.exit()
me.events(events)
me.draw(screen)
pygame.display.flip()