-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygletapp.py
148 lines (126 loc) · 4.47 KB
/
pygletapp.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
'''
Main point of entry for the pyglet display engine
Blaine Booher 2012
http://github.com/booherbg/flasklet.git
We are using cocos2d for the presentation layer
'''
from __future__ import division # Float Division
from random import randint
import os
import sys
import cocos
from cocos.director import director
import pyglet.media
from pyglet import font, image
import pyglet.text
from pyglet import clock # For http system...
from pyglet.gl import *
from pyglet.window import key
from pyglet.image.codecs import ImageDecodeException
import pyglet.image
from cocos.actions import *
from cocos.layer import *
from cocos.scenes.transitions import *
from cocos.sprite import *
from cocos import text
import flask_api
from flask_api import get_ipaddresses
FULLSCREEN = False
WEBSERVER = True
director.init(resizable=True, width=1024, height=768)
if FULLSCREEN:
director.window.set_fullscreen(True)
class QuitMediaServer(Exception):
pass
class localDispatcher(pyglet.event.EventDispatcher): pass
localDispatcher.register_event_type("on_change_bgcolor")
localdispatcher = localDispatcher()
def http_poller(dt, http_server):
'''
Polls the http server for gevent wait() events
'''
# dt is like 0.01 or so, be sure to wait less than that to not hold pyglet up
#print 'polling', dt,
http_server._stopped_event.wait(.0001)
#print 'finished'
class ForFunLayer(cocos.layer.Layer):
'''
A demo layer... for fun!
'''
is_event_handler = True
objects = []
def __init__(self):
'''
initialize the scrolling text and color
'''
super(ForFunLayer, self).__init__()
x, y = director.get_window_size()
title = text.Label(
"CincyPy Flasklet Demo", (x / 2, y / 2), font_name='Gill Sans',
font_size=60, anchor_x='center', anchor_y='center')
title.element.color = (255, 255, 0, 255)
self.objects.append(title)
self.add(title)
addresses = get_ipaddresses()
k=0
for interface, ip in addresses:
if ip != '0.0.0.0':
t = text.Label("http://%s:%d (%s)" % (ip, flask_api.DEFAULT_PORT, interface), (0, (k + 1)*30+100),
font_size=20)
t.element.color = (255, 0, 0, 255)
self.add(t)
k += 1
sys.stderr.flush()
# For handling of on_add_text
flask_api.eventdispatcher.push_handlers(self)
localdispatcher.push_handlers(self)
def on_change_bgcolor(self, color=None):
# Add a quick bg layer to the bottom of the stack
if color == None:
color = (randint(0, 255), randint(0, 255), randint(0, 255))
self.add(cocos.layer.ColorLayer(color[0], color[1], color[2], 255), z=-1)
def on_add_text(self, t):
'''
Update the scrolling text
'''
x, y = director.get_window_size()
l = text.Label(t, (randint(0,x - 100), randint(0,y - 50)), font_name='Gill Sans',
font_size=40, anchor_x='center', anchor_y='center')
l.element.color = (randint(0, 255), randint(0, 255), randint(0, 255), 255)
self.objects.append(l)
self.add(l)
def on_reset(self):
'''
Reset the screen - note: objects are not actually deleted
'''
print 'here we go'
for o in self.objects:
self.remove(o)
del o
self.objects = []
def on_key_press(self, keyp, mod):
if keyp in (key.ENTER,):
# Dispatch an event rather than directly call self()
flask_api.eventdispatcher.add_text("your lucky number is %d" % (randint(0, 255)))
elif keyp in (key.Q,):
director.terminate_app = True
elif keyp in (key.B,):
# Dispatch a local event, rather than a flask_api.eventdispatcher event
localdispatcher.dispatch_event("on_change_bgcolor")
elif keyp in (key.R,):
self.on_reset()
def ping(self):
print 'sending pong!'
flask_api.eventdispatcher.pong()
if __name__ == "__main__":
# Register Event Handler
http_server = flask_api.runWebController()
if http_server:
print "web api started successfully!"
else:
# TODO add exception handling
print "unable to start web api"
# Schedule clock to update the http poller
clock.schedule(http_poller, http_server)
# initiate the pyglet run loop
director.run(cocos.scene.Scene(ForFunLayer()))