From: <Blu...@us...> - 2010-08-02 20:41:15
|
Revision: 352 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=352&view=rev Author: BlueWolf_ Date: 2010-08-02 20:41:09 +0000 (Mon, 02 Aug 2010) Log Message: ----------- Modified Paths: -------------- trunk/client/functions.py trunk/client/gui.py trunk/client/playground.py Modified: trunk/client/functions.py =================================================================== --- trunk/client/functions.py 2010-08-01 22:16:19 UTC (rev 351) +++ trunk/client/functions.py 2010-08-02 20:41:09 UTC (rev 352) @@ -19,9 +19,7 @@ from pygame.locals import * from hashlib import sha1 import core -import gui - VERSION = "0.0.1" # This will be used to [sh]are all variables, functions and classes across the @@ -51,3 +49,9 @@ print 'Cannot load image:', fullname raise SystemExit, message return image + + +# GUI needs the functions again. That's why it has to be imported after all +# functions have been defined +import gui +gui = gui.Container Modified: trunk/client/gui.py =================================================================== --- trunk/client/gui.py 2010-08-01 22:16:19 UTC (rev 351) +++ trunk/client/gui.py 2010-08-02 20:41:09 UTC (rev 352) @@ -17,74 +17,152 @@ from functions import * -class Plane(): - - def __init__(self, update, width, height): - self.update = update - self.width = width - self.height = height - self.surf = pygame.Surface((self.width, self.height), SRCALPHA) \ - .convert_alpha() - self.norender = True - - self.childs = {} - - def __getitem__(self, key): - return self.childs[key] - - def add(self, *objs): - for obj in objs: - self.childs[obj.name] = obj - - def renderall(self): - self.norender = False - for child in self.childs.values(): - child.renderall(Rect(0,0,0,0), self.surf) - - self.update(Rect(0,0, self.width, self.height)) - - class Object(): - def __init__(self, name, rect): + def __init__(self, name, rect, update): self.name = name self.rect = Rect(rect) - self.childs = {} + self.parent_update = update + + self.children = [] + self.hover = False self.focus = False - self.hover = 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() def __getitem__(self, key): - return self.childs[key] + for child in self.children: + if child.name == key: + return child + + raise KeyError(key) - def add(self, *objs): - for obj in objs: - self.childs[obj.name] = obj + def add(self, objname, name, rect): + if objname == "textbox": + obj = Text + elif objname == "button": + obj = Button + else: + raise GuiError("Object '%s' has not been found" % objname) + + obj = Obj(name, rect, self.update) + self.children.append(obj) + + return obj - def renderall(self, correction, surf): - # First render ourself - self.render(correction, surf) + def update(self, rect): + # Send a message to our parent, telling them this rect needs to be + # updated - # Then render our childs - for child in self.childs.values(): - child.renderall(correction.move(self.rect.topleft), surf) + # We need to calculate our relative position + return rect.move(self.rect.left, self.rect.top) + + +class Container(): # A modified version of Object + + def __init__(self, size, update): + self.rect = Rect(0,0,size[0], size[1]) + self.parent_update = update + + self.children = [] + self.hover = False + self.focus = False + + # 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): + 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.children.append(obj) + + return obj + + + def update(self, rect): + # Clear this part of our surface + self.surf.fill(rect, [0,0,0,0]) + + # 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) + + self.parent_update(rect) + + + -class Text(Object): +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 + 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") + else: + raise GuiError("Style '%s' has not been found" % style) + + self.style = style + def set_type(self, type): + """ + Possible types: + * text + * password + """ + if type not in ("text", "password"): + raise GuiError("Type '%s' has not been found" % type) + + self.type = type + + + class Button(Object): def __defaults__(self): @@ -96,3 +174,6 @@ surf.fill([0,0,255], pos) + +class GuiError(Exception): + pass Modified: trunk/client/playground.py =================================================================== --- trunk/client/playground.py 2010-08-01 22:16:19 UTC (rev 351) +++ trunk/client/playground.py 2010-08-02 20:41:09 UTC (rev 352) @@ -91,14 +91,22 @@ elif status == "login": self.surf.blit(self.loginbox, (292,256)) + # Create login-field - #self.logingui = gui.Plane(self.drawlogingui, 417,189) + 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") + #password = gui.Text("password", (0,50, 417, 50)) + #password.set_style("login") + #password.set_type("password") + #login = gui.Button("login", (0, 100, 417, 50)) - #self.logingui.add(username, password, login) # [TODO] Give mouseposition + #self.logingui.renderall() @@ -110,6 +118,7 @@ self.sh['update']() def drawlogingui(self, rect): + print rect rect_main = rect.move(292,256) self.surf.blit(self.loginbg, rect_main, rect_main) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |