-Ajout de la classe abutton qui permet de générer un élément de menu depuis une image, une couleur ou une fonction.
This commit is contained in:
parent
21d978f459
commit
cd70c0f236
160
WireChem.py
160
WireChem.py
|
@ -83,6 +83,140 @@ def duplicateref(d):
|
|||
for k in d.keys():
|
||||
d[d[k]['nom']]=d[k]
|
||||
|
||||
class abutton(object):
|
||||
def update(self):
|
||||
if not self.isvisible():
|
||||
try:
|
||||
self.vertex_list.delete()
|
||||
except:
|
||||
print "destroyed"
|
||||
else:
|
||||
if self.typeof=='icon':
|
||||
self.content.width=self.width
|
||||
self.content.height=self.height
|
||||
self.vertex_list = pyglet.sprite.Sprite(self.content, x=self.x, y=self.y, batch=self.batch)
|
||||
elif self.typeof=='color':
|
||||
self.vertex_list = self.batch.add(4,pyglet.gl.GL_QUADS, None,
|
||||
('v2i', (self.x, self.y, self.x+self.width, self.y, self.x+self.width, self.y+self.height, self.x, self.y+self.height)),
|
||||
('c4B', self.content * 4))
|
||||
elif self.typeof=='function':
|
||||
self.vertex_list = eval(self.content)
|
||||
elif self.typeof=='multicon':
|
||||
self.content[self.index].width=self.width
|
||||
self.content[self.index].height=self.height
|
||||
self.vertex_list = pyglet.sprite.Sprite(self.content[self.index], x=self.x, y=self.y, batch=self.batch)
|
||||
|
||||
def __init__(self, window, name, x, y, width, height , active, hilite, visible, seleted, separe, content, hint, typeof, text, text2, batch):
|
||||
self.name=name
|
||||
self.index=0
|
||||
self.x=x
|
||||
self.y=y
|
||||
self.batch=batch
|
||||
self.width=width
|
||||
self.height=height
|
||||
self.active=active
|
||||
self.hilite=hilite
|
||||
self.visible=visible
|
||||
self.separe=separe
|
||||
self.content=content
|
||||
self.typeof=typeof
|
||||
self.hint=hint
|
||||
self.seleted=seleted
|
||||
self.window=window
|
||||
self.window.push_handlers(self.on_mouse_press)
|
||||
self.window.push_handlers(self.on_mouse_motion)
|
||||
self.window.push_handlers(self.on_mouse_drag)
|
||||
self.window.push_handlers(self.on_mouse_release)
|
||||
self.window.push_handlers(self.on_mouse_scroll)
|
||||
self.update()
|
||||
|
||||
def delete(self):
|
||||
self.vertex_list.delete()
|
||||
|
||||
def on_mouse_press(self, x, y, button, modifiers):
|
||||
if x>self.x and y>self.y and x<self.x+self.width and y<self.y+self.height and self.isactive() and self.isvisible():
|
||||
if hasattr(self.window, "on_mouse_press_"+self.name) and callable(eval("self.window.on_mouse_press_"+self.name)):
|
||||
state={'x':x,'y':y, 'dx':0, 'dy':0, 'buttons':button, 'modifiers':modifiers, 'event': 'press'}
|
||||
if self.typeof=='multicon':
|
||||
self.index+=1
|
||||
if self.index>=len(self.content):
|
||||
self.index=0
|
||||
self.update()
|
||||
eval("self.window.on_mouse_press_"+self.name+"("+str(state)+")")
|
||||
|
||||
def on_mouse_release(self, x, y, button, modifiers):
|
||||
if x>self.x and y>self.y and x<self.x+self.width and y<self.y+self.height and self.isactive() and self.isvisible():
|
||||
if hasattr(self.window, "on_mouse_release_"+self.name) and callable(eval("self.window.on_mouse_release_"+self.name)):
|
||||
state={'x':x,'y':y, 'dx':0, 'dy':0, 'buttons':button, 'modifiers':modifiers, 'event': 'release'}
|
||||
eval("self.window.on_mouse_release_"+self.name+"("+str(state)+")")
|
||||
|
||||
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
|
||||
if x>self.x and y>self.y and x<self.x+self.width and y<self.y+self.height and self.isactive() and self.isvisible():
|
||||
if hasattr(self.window, "on_mouse_drag_"+self.name) and callable(eval("self.window.on_mouse_drag_"+self.name)):
|
||||
state={'x':x,'y':y, 'dx':dx, 'dy':dy, 'buttons':buttons, 'modifiers':modifiers, 'event': 'drag'}
|
||||
eval("self.window.on_mouse_drag_"+self.name+"("+str(state)+")")
|
||||
|
||||
def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
|
||||
if x>self.x and y>self.y and x<self.x+self.width and y<self.y+self.height and self.isactive() and self.isvisible():
|
||||
if hasattr(self.window, "on_mouse_scroll_"+self.name) and callable(eval("self.window.on_mouse_scroll_"+self.name)):
|
||||
state={'x':x,'y':y, 'dx':scroll_x, 'dy':scroll_y, 'buttons':0, 'modifiers':0, 'event': 'scroll'}
|
||||
eval("self.window.on_mouse_scroll_"+self.name+"("+str(state)+")")
|
||||
|
||||
def on_mouse_motion(self, x, y, dx, dy):
|
||||
if x>self.x and y>self.y and x<self.x+self.width and y<self.y+self.height and self.isactive() and self.isvisible():
|
||||
if hasattr(self.window, "on_mouse_motion_"+self.name) and callable(eval("self.window.on_mouse_motion_"+self.name)):
|
||||
state={'x':x,'y':y, 'dx':dx, 'dy':dy, 'buttons':0, 'modifiers':0, 'event': 'motion'}
|
||||
eval("self.window.on_mouse_motion_"+self.name+"("+str(state)+")")
|
||||
|
||||
def isvisible(self):
|
||||
if type(self.visible) is bool:
|
||||
return self.visible
|
||||
else:
|
||||
return eval(self.visible)
|
||||
|
||||
def isactive(self):
|
||||
if type(self.active) is bool:
|
||||
return self.active
|
||||
else:
|
||||
return eval(self.active)
|
||||
|
||||
def ishilite(self):
|
||||
if type(self.hilite) is bool:
|
||||
return self.hilite
|
||||
else:
|
||||
return eval(self.hilite)
|
||||
|
||||
def hide(self):
|
||||
if type(self.visible) is bool:
|
||||
self.visible=False
|
||||
self.update()
|
||||
|
||||
def show(self):
|
||||
if type(self.visible) is bool:
|
||||
self.visible=True
|
||||
self.update()
|
||||
|
||||
def activate(self):
|
||||
if type(self.active) is bool:
|
||||
self.active=True
|
||||
self.update()
|
||||
|
||||
def unactivate(self):
|
||||
if type(self.active) is bool:
|
||||
self.active=False
|
||||
self.update()
|
||||
|
||||
def hilite(self):
|
||||
if type(self.hilite) is bool:
|
||||
self.hilite=True
|
||||
self.update()
|
||||
|
||||
def unhilite(self):
|
||||
if type(self.hilite) is bool:
|
||||
self.hilite=False
|
||||
self.update()
|
||||
|
||||
|
||||
''' *********************************************************************************************** '''
|
||||
''' Gestion du plateau de jeu '''
|
||||
|
||||
|
@ -105,13 +239,35 @@ class menu(pyglet.window.Window):
|
|||
super(menu, self).__init__(resizable=True, fullscreen=False, visible=True, caption="Wirechem: The new chemistry game")
|
||||
self.batch = pyglet.graphics.Batch()
|
||||
self.clocks=[clock.schedule(self.draw)]
|
||||
|
||||
self.buttons=[abutton(self,'test',10, 10, 50, 50 , True, True, True, False, False, (255,0,0,255), 'test', 'color', '', '', self.batch),abutton(self,'prout',150, 150, 150, 50 , True, True, True, False, False, (0,255,0,255), 'test', 'color', '', '', self.batch),abutton(self,'aicon',70, 70, 150, 100 , True, True, True, False, False, [pyglet.image.load('picture/boss.png'),pyglet.image.load('picture/exits.png')], 'test', 'multicon', '', '', self.batch)]
|
||||
|
||||
def draw(self,dt):
|
||||
glClearColor(0,0,0,255)
|
||||
self.clear()
|
||||
self.batch.draw()
|
||||
|
||||
def on_mouse_press_test(self, state):
|
||||
print "test est appuyé"
|
||||
if self.buttons[0].isvisible():
|
||||
self.buttons[0].hide()
|
||||
else:
|
||||
self.buttons[0].show()
|
||||
|
||||
def on_mouse_motion_test(self, state):
|
||||
print "test est motion en "+str(state['x'])
|
||||
|
||||
def on_mouse_press_prout(self, state):
|
||||
self.buttons[0].unactivate()
|
||||
|
||||
def on_mouse_motion_prout(self, state):
|
||||
print "prout "+str(state['x'])
|
||||
|
||||
def on_mouse_scroll_aicon(self, state):
|
||||
print "essai "+str(state['dy'])
|
||||
|
||||
def on_mouse_press_aicon(self, state):
|
||||
print "essai "+str(state['dy'])
|
||||
|
||||
''' *********************************************************************************************** '''
|
||||
''' Initialisation '''
|
||||
|
||||
|
@ -120,7 +276,7 @@ glEnable(GL_BLEND);
|
|||
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);'''
|
||||
glEnable(GL_LINE_STIPPLE)
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
menu_principal = menu(resizable=True)
|
||||
menu_principal = menu()
|
||||
pyglet.app.run()
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue