From: <Blu...@us...> - 2010-08-07 15:52:52
|
Revision: 355 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=355&view=rev Author: BlueWolf_ Date: 2010-08-07 15:52:43 +0000 (Sat, 07 Aug 2010) Log Message: ----------- GUI is slowly starting to work! I also changed all the self.sh's to plain sh Modified Paths: -------------- trunk/client/VP.py trunk/client/gui.py trunk/client/layout.py trunk/client/playground.py trunk/client/windows.py Modified: trunk/client/VP.py =================================================================== --- trunk/client/VP.py 2010-08-03 22:21:05 UTC (rev 354) +++ trunk/client/VP.py 2010-08-07 15:52:43 UTC (rev 355) @@ -28,67 +28,85 @@ class Main(): def __init__(self): - self.sh = {} + sh['has_focus'] = True + sh['has_hover'] = True # Fire up pygame os.environ['SDL_VIDEO_CENTERED']='1' pygame.init() pygame.display.set_caption('Virtual Playground') - self.sh['screen'] = pygame.display.set_mode((1000, 700)) + sh['screen'] = pygame.display.set_mode((1000, 700)) #icon = load_image('img','icon.png') #pygame.display.set_icon(icon) pygame.key.set_repeat(250, 40) pygame.scrap.init() self.clock = pygame.time.Clock() - self.sh['main'] = self + sh['main'] = self # Give the surfaces a way to blit itself - self.sh['update'] = self.update + sh['update'] = self.update self.updaterect = [] self.updatewaiting = False # Create all our surfaces - self.sh['playground'] = Playground(self.sh) - self.sh['layout'] = Layout(self.sh) - self.sh['windows'] = Windows(self.sh) + sh['playground'] = Playground() + sh['layout'] = Layout() + sh['windows'] = Windows() self.update() # Connect to the server - self.sh['client'] = core.Client({ + sh['client'] = core.Client({ 'app_name': "Virtual Playground", 'app_version': VERSION}, - Callback(self.sh)) + Callback()) self.changestatus("connecting") - self.sh['client'].connect('localhost', 6653) + sh['client'].connect('localhost', 6653) # Wait for all the events to come try: while 1: ev = pygame.event.wait() - + if ev.type == QUIT: break if ev.type == MOUSEMOTION: + # Prevent this event from overflowing the queue. Always + # return the position right now and remove al other + # mousemotions in the queue + pos = pygame.mouse.get_pos() pygame.event.clear(MOUSEMOTION) + ev = pygame.event.Event(MOUSEMOTION, + buttons = ev.buttons, + pos = pos) + + # As (sometimes) the mouse focus doesn't get through when + # switching workspaces (Linux), this should "fix" it + sh['has_hover'] = True + elif ev.type == ACTIVEEVENT: + if ev.state == 1: # Mouse focus + sh['has_hover'] = (ev.gain == 1) + elif ev.state == 2: # Window focus + sh['has_focus'] = (ev.gain == 1) + # Send through all the layers - if self.sh['windows'].event(ev): continue - if self.sh['layout'].event(ev): continue - if self.sh['playground'].event(ev): continue + if sh['windows'].event(ev): continue + if sh['layout'].event(ev): continue + if sh['playground'].event(ev): continue except KeyboardInterrupt: # Note: this does not work properly print # Someone decided to close VP (Why??) - try: self.sh['client'].close() + try: sh['client'].close() except: pass sys.exit() @@ -125,21 +143,19 @@ # The actual updating happens here - self.sh['screen'].blit(self.sh['playground'].surf, rect, rect) - self.sh['screen'].blit(self.sh['layout'].surf, rect, rect) - self.sh['screen'].blit(self.sh['windows'].surf, rect, rect) + sh['screen'].blit(sh['playground'].surf, rect, rect) + sh['screen'].blit(sh['layout'].surf, rect, rect) + sh['screen'].blit(sh['windows'].surf, rect, rect) pygame.display.update(rect) def changestatus(self, status): - self.sh['playground'].changestatus(status) - self.sh['layout'].changestatus(status) - self.sh['windows'].changestatus(status) + sh['playground'].changestatus(status) + sh['layout'].changestatus(status) + sh['windows'].changestatus(status) class Callback(core.Callback): - def __init__(self, sh): - self.sh = sh def data_received(self, data): print " --> " + repr(data) @@ -152,10 +168,10 @@ def received_rsa(self, public): # We are connected - self.sh['main'].changestatus("login") + sh['main'].changestatus("login") def logged_in(self, username, cid, owner): - self.sh['main'].changestatus("playground") + sh['main'].changestatus("playground") if __name__ == "__main__": Main() Modified: trunk/client/gui.py =================================================================== --- trunk/client/gui.py 2010-08-03 22:21:05 UTC (rev 354) +++ trunk/client/gui.py 2010-08-07 15:52:43 UTC (rev 355) @@ -18,6 +18,143 @@ from functions import * import random + +class Container(): # A modified version of Object + + def __init__(self, update): + self.rect = Rect(0, 0, 1000, 700) + self.parent_update = update + self.frozen = True # Don't update until everything has been + # loaded + + self.children = [] + self.hover = None + self.focus = None + + # Create a surface where an object blits itself onto + self.surf = pygame.Surface((self.rect.width, self.rect.height), \ + SRCALPHA).convert_alpha() + + def __getitem__(self, key): + for child in self.children: + if child.name == key: + return child + + raise KeyError(key) + + def add(self, objname, name, rect): + """ + Append a child to this object + """ + + if objname == "textbox": + Obj = Textbox + elif objname == "button": + Obj = Button + else: + raise GuiError("Object '%s' has not been found" % objname) + + obj = Obj(name, rect, self.update, self.update_mouse) + self.children.append(obj) + + return obj + + + def update(self, rect = None): + """ + Update this part of the object + """ + + if self.frozen: return + + if rect == None: + rect = self.rect + + # Clear this part of our surface + self.surf.fill([0,0,0,0], rect) + + # Find all the children within this rect + for child in self.children: + if not rect.colliderect(child.rect): continue + + new_rect = rect.move(-child.rect.left, -child.rect.top) + child.blit(new_rect, self.surf, rect) + + self.parent_update(rect) + + def unfreeze(self): + """ + Unfreeze this and all the children and update all of them + """ + + for child in self.children: + child.unfreeze(False) + + self.update_mouse() + self.frozen = False + self.update() + + def update_mouse(self, pos = None): + """ + Get the current mouse position and check if the focus has changed + """ + + if pos == None: pos = pygame.mouse.get_pos() + + if sh['has_hover']: + # Get the current hovered object + hover = self.this_pos(pos) + + # Is it different from the previous check? + if hover != self.hover: + # De-hover the previous one + if self.hover: + self.hover.hover = False + self.hover.update() + + self.hover = hover + + # Hover the current one + if self.hover: + self.hover.hover = True + self.hover.update() + + else: + # Remove the previous hover + if self.hover: + self.hover.hover = False + self.hover.update() + self.hover = None + + + + def event(self, ev): + """ + All the events from pygame should go here + """ + + if self.frozen: return + + if ev.type == MOUSEMOTION: # Mouse movement + self.update_mouse(ev.pos) + + elif ev.type == ACTIVEEVENT and ev.state == 1: # Mouse focus + self.update_mouse() + + def this_pos(self, pos): + # Return the object on this position + + for child in self.children: + if not child.rect.collidepoint(pos): continue + + child_pos = (pos[0] - child.rect[0], pos[1] - child.rect[1]) + + hover = child.this_pos(child_pos) + if hover: return hover + + return None + + class Object(): def __init__(self, name, rect, update, update_mouse): @@ -53,9 +190,9 @@ """ if objname == "textbox": - obj = Text + Obj = Textbox elif objname == "button": - obj = Button + Obj = Button else: raise GuiError("Object '%s' has not been found" % objname) @@ -77,7 +214,7 @@ if self.frozen: return if rect == None: - rect = self.rect + rect = self.rect.move(0, 0) else: # We need to calculate our relative position @@ -87,10 +224,10 @@ def blit(self, rect, surf, onto): """ - Render our object and our childs to this surface + Render our object and our children to this surface """ - # Render ourself and our childs to surf + # Render ourself and our children to surf if self.needs_update: self.render() self.needs_update = False @@ -107,7 +244,7 @@ def unfreeze(self, isparent = True): """ - Unfreeze this and all the childs and update all of them + Unfreeze this and all the children and update all of them """ for child in self.children: @@ -119,92 +256,21 @@ self.update(self.rect) else: self.frozen = False - -class Container(): # A modified version of Object - - def __init__(self, update): - self.rect = Rect(0, 0, 1000, 700) - self.parent_update = update - self.frozen = True # Don't update until everything has been - # loaded - - self.children = [] - self.hover = None - self.focus = None - - # Create a surface where an object blits itself onto - self.surf = pygame.Surface((self.rect.width, self.rect.height), \ - SRCALPHA).convert_alpha() - - def __getitem__(self, key): - for child in self.children: - if child.name == key: - return child + def this_pos(self, pos): + # Return the object on this position - raise KeyError(key) - - def add(self, objname, name, rect): - """ - Append a child to this object - """ - - if objname == "textbox": - Obj = Textbox - elif objname == "button": - Obj = Button - else: - raise GuiError("Object '%s' has not been found" % objname) - - obj = Obj(name, rect, self.update, self.update_mouse) - self.children.append(obj) - - return obj - - - def update(self, rect = None): - """ - Update this part of the object - """ - - if self.frozen: return - - if rect == None: - rect = self.rect - - # Clear this part of our surface - self.surf.fill([0,0,0,0], rect) - - # Find all the children within this rect for child in self.children: - if not rect.colliderect(child.rect): continue + if not child.rect.collidepoint(pos): continue - new_rect = rect.move(-child.rect.left, -child.rect.top) - child.blit(new_rect, self.surf, rect) + child_pos = (pos[0] - child.rect[0], pos[1] - child.rect[1]) + + return child.this_pos(child_pos) - self.parent_update(rect) - - def unfreeze(self): - """ - Unfreeze this and all the childs and update all of them - """ - - for child in self.children: - child.unfreeze(False) - - self.update_mouse() - self.frozen = False - self.update() - - def update_mouse(self): - """ - Get the current mouse position and check if the focus has changed - """ - - print "mousecheck" - - + return self + + class Textbox(Object): def __defaults__(self): @@ -268,13 +334,13 @@ if self.style == "login": # Which state do we need? - if self.focus: + if self.hover: top = 37 else: top = 0 self.surf.blit(self.backgroundsurf, (0,0), \ - (0,top, self.rect.width, 37)) + (0, top, self.rect.width, 37)) class GuiError(Exception): Modified: trunk/client/layout.py =================================================================== --- trunk/client/layout.py 2010-08-03 22:21:05 UTC (rev 354) +++ trunk/client/layout.py 2010-08-07 15:52:43 UTC (rev 355) @@ -24,9 +24,7 @@ class Layout(): - def __init__(self, sh): - self.sh = sh - + def __init__(self): self.surf = pygame.Surface((1000, 700), SRCALPHA).convert_alpha() self.status = None @@ -109,7 +107,7 @@ self.surf.blit(self.topbar, rect, rect2) - self.sh['update']((0,0, 1000, 65)) + sh['update']((0,0, 1000, 65)) def topbarcursor(self, pos): if pos[0] > TOPLEFT and pos[0] < (TOPBUTTONWIDTH*TOPBUTTONS+TOPLEFT) \ Modified: trunk/client/playground.py =================================================================== --- trunk/client/playground.py 2010-08-03 22:21:05 UTC (rev 354) +++ trunk/client/playground.py 2010-08-07 15:52:43 UTC (rev 355) @@ -18,9 +18,8 @@ from functions import * class Playground(): - def __init__(self, sh): - self.sh = sh - + + def __init__(self): self.surf = pygame.Surface((1000, 700), SRCALPHA).convert_alpha() self.surf.fill([0,0,0]) @@ -42,18 +41,18 @@ if ev.type == KEYDOWN and ev.unicode == "\r": # Fake login self.drawlogin("logging in") - self.sh['client'].login("test123", sha1("test123").hexdigest()) + sh['client'].login("test123", sha1("test123").hexdigest()) elif ev.type == KEYDOWN and ev.unicode == "r": # Fake signup - self.sh['client'].signup("test123", sha1("test123").hexdigest(), + sh['client'].signup("test123", sha1("test123").hexdigest(), { "realname": "Pietje Precies", "gender": "M" }) - #if self.logingui != None: - # self.logingui.event(ev) + if self.logingui != None: + self.logingui.event(ev) @@ -74,7 +73,7 @@ self.logingui = None self.surf.fill([0,0,0]) - self.sh['update']() + sh['update']() if status == "connecting": @@ -119,7 +118,7 @@ posleft = (1000/2)-(text.get_size()[0]/2) self.surf.blit(text, (posleft, 330)) - self.sh['update']() + sh['update']() def drawlogingui(self, rect): print "GUIupdate", rect @@ -128,4 +127,4 @@ self.surf.blit(self.loginbox, (292, 256)) self.surf.blit(self.logingui.surf, rect, rect) - self.sh['update'](rect) + sh['update'](rect) Modified: trunk/client/windows.py =================================================================== --- trunk/client/windows.py 2010-08-03 22:21:05 UTC (rev 354) +++ trunk/client/windows.py 2010-08-07 15:52:43 UTC (rev 355) @@ -18,9 +18,7 @@ ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. class Windows(): - def __init__(self, sh): - self.sh = sh - + def __init__(self): self.surf = pygame.Surface((1000, 700), SRCALPHA).convert_alpha() def event(self, ev): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |