From: <Blu...@us...> - 2010-08-03 22:21:11
|
Revision: 354 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=354&view=rev Author: BlueWolf_ Date: 2010-08-03 22:21:05 +0000 (Tue, 03 Aug 2010) Log Message: ----------- The GUI so far. Looks pretty solid. Will be adding events soon Modified Paths: -------------- trunk/client/gui.py trunk/client/playground.py Added Paths: ----------- trunk/client/images/gui/ trunk/client/images/gui/text_login.png Modified: trunk/client/gui.py =================================================================== --- trunk/client/gui.py 2010-08-03 19:13:58 UTC (rev 353) +++ trunk/client/gui.py 2010-08-03 22:21:05 UTC (rev 354) @@ -16,24 +16,29 @@ ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. from functions import * +import random class Object(): - def __init__(self, name, rect, update): + def __init__(self, name, rect, update, update_mouse): self.name = name self.rect = Rect(rect) self.parent_update = update + self.update_mouse = update_mouse + self.needs_update = False + self.frozen = True # Don't update until everything has been + # loaded self.children = [] self.hover = False self.focus = False - # Load the defaults - self.__defaults__() - # Create a surface where an object blits itself onto self.surf = pygame.Surface((self.rect.width, self.rect.height), \ SRCALPHA).convert_alpha() + + # Load the defaults + self.__defaults__() def __getitem__(self, key): for child in self.children: @@ -43,6 +48,10 @@ raise KeyError(key) def add(self, objname, name, rect): + """ + Append a child to this object + """ + if objname == "textbox": obj = Text elif objname == "button": @@ -50,28 +59,80 @@ else: raise GuiError("Object '%s' has not been found" % objname) - obj = Obj(name, rect, self.update) + obj = Obj(name, rect, self.update, self.update_mouse) self.children.append(obj) return obj - def update(self, rect): + def update(self, rect = None): + """ + Update this part of the object + """ + # Send a message to our parent, telling them this rect needs to be # updated - # We need to calculate our relative position - return rect.move(self.rect.left, self.rect.top) + self.needs_update = True + + if self.frozen: return + + if rect == None: + rect = self.rect + + else: + # We need to calculate our relative position + rect.move_ip(self.rect.left, self.rect.top) + self.parent_update(rect) + + def blit(self, rect, surf, onto): + """ + Render our object and our childs to this surface + """ + + # Render ourself and our childs to surf + if self.needs_update: + self.render() + self.needs_update = False + + surf.blit(self.surf, onto, 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, surf, onto) + + def unfreeze(self, isparent = True): + """ + Unfreeze this and all the childs and update all of them + """ + + for child in self.children: + child.unfreeze(False) + + if isparent: + self.update_mouse() + self.frozen = False + self.update(self.rect) + else: + self.frozen = False + + + class Container(): # A modified version of Object - def __init__(self, size, update): - self.rect = Rect(0,0,size[0], size[1]) + 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 = False - self.focus = False + 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), \ @@ -85,6 +146,10 @@ raise KeyError(key) def add(self, objname, name, rect): + """ + Append a child to this object + """ + if objname == "textbox": Obj = Textbox elif objname == "button": @@ -92,63 +157,97 @@ else: raise GuiError("Object '%s' has not been found" % objname) - obj = Obj(name, rect, self.update) + obj = Obj(name, rect, self.update, self.update_mouse) self.children.append(obj) return obj - def update(self, rect): + 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(rect, [0,0,0,0]) + 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.render(rect, surf, rect) + child.blit(new_rect, self.surf, rect) 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" + + class Textbox(Object): def __defaults__(self): self.input = "" self.cursorpos = 0 self.style = None - self.layout = None self.type = "text" - - def render(self, correction, surf): - if self.layout == None: return - pos = correction.move(self.rect.topleft) - pos.size = self.rect.size + self.backgroundsurf = pygame.Surface((self.rect.width, \ + self.rect.height*2), SRCALPHA).convert_alpha() - print correction, pos - surf.fill([255,0,0], pos) - - def update(self): - pass - def set_style(self, style): """ Possible styles: * login """ - if style == "text": - pass - elif style == "login": - self.layout = load_image(True, "images", "gui", "text_login.png") + if style == "login": + if self.rect.height != 37: + raise GuiError("Height should be 37") + if self.rect.width < 25: + raise GuiError("Width should be 25 or bigger ") + + layoutimg = load_image(True, "images", "gui", "text_login.png") + + # Create backgroundsurf + self.backgroundsurf.fill([0,0,0,0]) + + # Left + self.backgroundsurf.blit(layoutimg, (0, 0), (0, 0, 12, 74)) + # Right + self.backgroundsurf.blit(layoutimg, (self.rect.width-12, 0), \ + (15, 0, 12, 74)) + # Middle + middle = pygame.Surface((1, 74), SRCALPHA).convert_alpha() + middle.blit(layoutimg, (0, 0,), (13, 0, 1, 74)) + middle = pygame.transform.scale(middle, (self.rect.width-24, 74)) + self.backgroundsurf.blit(middle, (12, 0)) + else: raise GuiError("Style '%s' has not been found" % style) self.style = style + self.update() def set_type(self, type): """ @@ -160,20 +259,23 @@ raise GuiError("Type '%s' has not been found" % type) self.type = type + self.update() + + def render(self): + if self.backgroundsurf == None: return - - -class Button(Object): - - def __defaults__(self): - self.caption = "" - - def render(self, correction, surf): - pos = correction.move(self.rect.topleft) - pos.size = self.rect.size + self.surf.fill([0,0,0,0]) - surf.fill([0,0,255], pos) + if self.style == "login": + # Which state do we need? + if self.focus: + top = 37 + else: + top = 0 + + self.surf.blit(self.backgroundsurf, (0,0), \ + (0,top, self.rect.width, 37)) + - class GuiError(Exception): pass Added: trunk/client/images/gui/text_login.png =================================================================== (Binary files differ) Property changes on: trunk/client/images/gui/text_login.png ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Modified: trunk/client/playground.py =================================================================== --- trunk/client/playground.py 2010-08-03 19:13:58 UTC (rev 353) +++ trunk/client/playground.py 2010-08-03 22:21:05 UTC (rev 354) @@ -37,6 +37,8 @@ def event(self, ev): if self.status == "login": + + # Some temporary debug stuff if ev.type == KEYDOWN and ev.unicode == "\r": # Fake login self.drawlogin("logging in") @@ -49,6 +51,11 @@ "realname": "Pietje Precies", "gender": "M" }) + + #if self.logingui != None: + # self.logingui.event(ev) + + def changestatus(self, status): login_list = ['connecting', 'login', 'logging in'] @@ -93,22 +100,19 @@ self.surf.blit(self.loginbox, (292,256)) # Create login-field - self.logingui = gui((417,189), self.drawlogingui) - - username = self.logingui.add("textbox", "usr", (0,0, 417, 50)) - #username = gui.Text("username", (0,0, 417, 50)) - #username.set_style("login") + self.logingui = gui(self.drawlogingui) - #password = gui.Text("password", (0,50, 417, 50)) - #password.set_style("login") - #password.set_type("password") + username = self.logingui.add("textbox", "usr", (351, 279, 298, 37)) + username.set_style("login") + password = self.logingui.add("textbox", "pwd", (351, 340, 298, 37)) + password.set_style("login") + password.set_type("password") + #login = gui.Button("login", (0, 100, 417, 50)) - # [TODO] Give mouseposition + self.logingui.unfreeze() - #self.logingui.renderall() - elif status == "logging in": text = self.loginfont.render("Logging in...", True, (132,125,114)) @@ -118,11 +122,10 @@ self.sh['update']() def drawlogingui(self, rect): - print rect - rect_main = rect.move(292,256) + print "GUIupdate", rect - self.surf.blit(self.loginbg, rect_main, rect_main) - self.surf.blit(self.loginbox, rect_main, rect) - self.surf.blit(self.logingui.surf, rect_main, rect) + self.surf.blit(self.loginbg, rect, rect) + self.surf.blit(self.loginbox, (292, 256)) + self.surf.blit(self.logingui.surf, rect, rect) - self.sh['update'](rect_main) + self.sh['update'](rect) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |