-Correction et ajouts sur la classe abutton

-Reprise de l'interface de wirechem en utilisant les abuttons
This commit is contained in:
Le_dahut 2014-06-04 22:26:06 +02:00
parent cd70c0f236
commit eb6494300b
1 changed files with 85 additions and 59 deletions

View File

@ -84,40 +84,72 @@ def duplicateref(d):
d[d[k]['nom']]=d[k] d[d[k]['nom']]=d[k]
class abutton(object): class abutton(object):
def update(self): def update(self,dt):
if not self.isvisible(): try:
try: self.vertex_list.delete()
self.vertex_list.delete() except:
except: foo=0
print "destroyed" try:
else: self.vertex_list2.delete()
if self.typeof=='icon': except:
self.content.width=self.width foo=0
self.content.height=self.height try:
self.vertex_list = pyglet.sprite.Sprite(self.content, x=self.x, y=self.y, batch=self.batch) self.vertex_list3.delete()
elif self.typeof=='color': except:
self.vertex_list = self.batch.add(4,pyglet.gl.GL_QUADS, None, foo=0
try:
self.sprite.delete()
except:
foo=0
if self.isvisible():
if not self.isactive():
color=127
else:
color=255
if self.typeof=='color':
color=(self.content[0],self.content[1],self.content[2],color)
self.vertex_list = self.window.batch.add(4,pyglet.gl.GL_QUADS, self.window.p1,
('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)), ('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)) ('c4B', color * 4))
elif self.typeof=='function': elif self.typeof=='function':
self.vertex_list = eval(self.content) self.vertex_list = eval(self.content)
elif self.typeof=='multicon': else:
self.content[self.index].width=self.width if self.typeof=='multicon':
self.content[self.index].height=self.height image = self.content[self.index]
self.vertex_list = pyglet.sprite.Sprite(self.content[self.index], x=self.x, y=self.y, batch=self.batch) else:
image = self.content
if self.width==0 or self.height==0:
self.width=image.width
self.height=image.height
self.sprite = pyglet.sprite.Sprite(image, x=self.x, y=self.y, batch=self.window.batch, group=self.window.p1)
if self.width/float(self.height) < image.width/float(image.height):
self.sprite.scale=float(self.width)/image.width
else:
self.sprite.scale=float(self.height)/image.height
self.sprite.opacity=color
if type(self.seleted) is tuple:
color=(self.seleted[0],self.seleted[1],self.seleted[2],255)
self.vertex_list2 = self.window.batch.add(4,pyglet.gl.GL_LINE_LOOP, self.window.p2,
('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', color * 4))
elif type(self.seleted) is list:
self.sprite.opacity=(self.seleted[0],self.seleted[1],self.seleted[2])
if self.hilite and int(time.time())%2==0:
color=(255,0,0,128)
self.vertex_list3 = self.window.batch.add(4,pyglet.gl.GL_QUADS, self.window.p2,
('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', color * 4))
def __init__(self, window, name, x, y, width, height , active, hilite, visible, seleted, separe, content, hint, typeof, text, text2, batch): def __init__(self, window, name, x, y, width, height , active, hilite, visible, seleted, content, hint, typeof, text, text2):
self.name=name self.name=name
self.index=0 self.index=0
self.x=x self.x=x
self.y=y self.y=y
self.batch=batch
self.width=width self.width=width
self.height=height self.height=height
self.active=active self.active=active
self.hilite=hilite self.hilite=hilite
self.visible=visible self.visible=visible
self.separe=separe
self.content=content self.content=content
self.typeof=typeof self.typeof=typeof
self.hint=hint self.hint=hint
@ -127,11 +159,14 @@ class abutton(object):
self.window.push_handlers(self.on_mouse_motion) self.window.push_handlers(self.on_mouse_motion)
self.window.push_handlers(self.on_mouse_drag) self.window.push_handlers(self.on_mouse_drag)
self.window.push_handlers(self.on_mouse_release) self.window.push_handlers(self.on_mouse_release)
self.window.push_handlers(self.on_mouse_scroll) self.window.push_handlers(self.on_mouse_scroll)
self.update() self.updateclock=clock.schedule_interval(self.update,1)
self.update(0)
def delete(self): def delete(self):
self.vertex_list.delete() self.vertex_list.delete()
del self
self=None
def on_mouse_press(self, x, y, button, modifiers): 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 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():
@ -141,7 +176,7 @@ class abutton(object):
self.index+=1 self.index+=1
if self.index>=len(self.content): if self.index>=len(self.content):
self.index=0 self.index=0
self.update() self.update(0)
eval("self.window.on_mouse_press_"+self.name+"("+str(state)+")") eval("self.window.on_mouse_press_"+self.name+"("+str(state)+")")
def on_mouse_release(self, x, y, button, modifiers): def on_mouse_release(self, x, y, button, modifiers):
@ -189,32 +224,32 @@ class abutton(object):
def hide(self): def hide(self):
if type(self.visible) is bool: if type(self.visible) is bool:
self.visible=False self.visible=False
self.update() self.update(0)
def show(self): def show(self):
if type(self.visible) is bool: if type(self.visible) is bool:
self.visible=True self.visible=True
self.update() self.update(0)
def activate(self): def activate(self):
if type(self.active) is bool: if type(self.active) is bool:
self.active=True self.active=True
self.update() self.update(0)
def unactivate(self): def unactivate(self):
if type(self.active) is bool: if type(self.active) is bool:
self.active=False self.active=False
self.update() self.update(0)
def hilite(self): def hilitate(self):
if type(self.hilite) is bool: if type(self.hilite) is bool:
self.hilite=True self.hilite=True
self.update() self.update(0)
def unhilite(self): def unhilitate(self):
if type(self.hilite) is bool: if type(self.hilite) is bool:
self.hilite=False self.hilite=False
self.update() self.update(0)
''' *********************************************************************************************** ''' ''' *********************************************************************************************** '''
@ -232,51 +267,42 @@ class game(pyglet.window.Window):
self.clear() self.clear()
''' *********************************************************************************************** ''' ''' *********************************************************************************************** '''
''' Gestion du menu principal ''' ''' Gestion du menu principal '''
class menu(pyglet.window.Window): class menu(pyglet.window.Window):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(menu, self).__init__(resizable=True, fullscreen=False, visible=True, caption="Wirechem: The new chemistry game") super(menu, self).__init__(width=1024, height=768, resizable=True, fullscreen=False, visible=True, caption="Wirechem: The new chemistry game")
self.batch = pyglet.graphics.Batch() self.batch = pyglet.graphics.Batch()
self.p0 = pyglet.graphics.OrderedGroup(0)
self.p1 = pyglet.graphics.OrderedGroup(1)
self.p2 = pyglet.graphics.OrderedGroup(2)
self.p3 = pyglet.graphics.OrderedGroup(3)
self.clocks=[clock.schedule(self.draw)] 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)] self.buttons=[abutton(self,'logo',185, self.height-200, 0, 0 , True, False, True, False, pyglet.image.load('picture/logo.png'), 'test', 'icon', '', ''),
abutton(self,'logo2',45, self.height-150, 0, 0 , True, False, True, False, pyglet.image.load('picture/logo2.png'), 'test', 'icon', '', ''),
abutton(self,'arrows',840, 150, 0, 0 , True, False, True, False, pyglet.image.load('picture/arrows.png'), 'test', 'icon', '', ''),
abutton(self,'arrows2',920, 150, 0, 0 , True, False, True, False, pyglet.image.load('picture/arrows2.png'), 'test', 'icon', '', ''),
abutton(self,'exit2',940, self.height-100, 0, 0 , True, False, True, False, pyglet.image.load('picture/exit2.png'), 'test', 'icon', '', '')]
def draw(self,dt): def draw(self,dt):
glClearColor(0,0,0,255) glClearColor(0,0,0,255)
self.clear() self.clear()
self.batch.draw() self.batch.draw()
def on_mouse_press_test(self, state): def on_mouse_press_exit2(self, state):
print "test est appuyé" pyglet.app.exit()
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 ''' ''' Initialisation '''
menu_principal = menu()
menu_principal.set_minimum_size(1024, 768)
glEnable(GL_BLEND); glEnable(GL_BLEND);
'''glEnable(GL_LINE_SMOOTH); glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);''' glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable(GL_LINE_STIPPLE) glEnable(GL_LINE_STIPPLE)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
menu_principal = menu()
pyglet.app.run() pyglet.app.run()