From: <Blu...@us...> - 2010-08-25 11:42:19
|
Revision: 370 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=370&view=rev Author: BlueWolf_ Date: 2010-08-25 11:42:12 +0000 (Wed, 25 Aug 2010) Log Message: ----------- Added labels and (correctly) clickable textboxes! 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-24 23:40:51 UTC (rev 369) +++ trunk/client/functions.py 2010-08-25 11:42:12 UTC (rev 370) @@ -56,4 +56,3 @@ # 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-24 23:40:51 UTC (rev 369) +++ trunk/client/gui.py 2010-08-25 11:42:12 UTC (rev 370) @@ -19,6 +19,17 @@ import random +class Feedback(): + def keypress(self, obj, ev): + pass + + def submit(self, obj): + pass + + def mousedown(self, obj): + pass + + class Container(): # A modified version of Object def __init__(self, update, feedback): @@ -47,18 +58,11 @@ raise KeyError(key) - def add(self, objname, name, rect): + def add(self, Obj, 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, self, self.feedback) self.children.append(obj) @@ -197,6 +201,7 @@ # Tell this object hover.focus = True hover.got_focus() + self.feedback.mousedown(hover) return True @@ -285,18 +290,11 @@ raise KeyError(key) - def add(self, objname, name, rect): + def add(self, Obj, 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.main_parent, self, self.feedback) self.children.append(obj) @@ -390,13 +388,15 @@ self.style = None self.type = "text" + self.textpos = None # Position of the text currently + self.font = None self.backgroundsurf = pygame.Surface((self.rect.width, \ self.rect.height*2), SRCALPHA).convert_alpha() def set_style(self, style): """ - Change the stle for this textbox + Change the style for this textbox Possible styles: * login """ @@ -486,7 +486,9 @@ pos = -textwidth + truewidth - 2 else: # Cursor is in the center pos = -cursorpos + (truewidth/2) - + + self.textpos = pos + self.surf.blit(text, (6, 10), (-pos, 0, truewidth, 17)) # Blit cursor @@ -498,9 +500,22 @@ def click(self, state, pos): if state == 0: - # [TODO] Calculate position based on pos - # self.cursorpos = ... + # Calculate position based on pos + actualpos = pos[0]-6-self.textpos + lastsize = 0 + for i in range(len(self.input)+1): + textsize = self.font.size(self.input[:i])[0] + if textsize > actualpos: + # Which direction is closer, left or right? + if textsize-actualpos > actualpos-lastsize: + i = i -1 + break + lastsize = textsize + + if i == -1: i = 0 + self.cursorpos = i + if self.focus: self.update() @@ -574,7 +589,7 @@ def set_style(self, style): """ - Change the stle for this textbox + Change the style for this textbox Possible styles: * login """ @@ -677,5 +692,65 @@ self.update() +class Label(Object): + + def __defaults__(self): + self.cangetfocus = False + + self.caption = "" + + self.font = None + self.color = [0, 0, 0] + self.align = 0 + + def set_font(self, font, size): + """ + Change the font for this label + """ + + self.font = pygame.font.Font(real_path(*font), size) + + self.update() + + def render(self): + """ + Render our surface, as requested from the all mighty Container + """ + + # [TODO] Make enters work as it should + + if self.font == None: return + + self.surf.fill([0,0,0,0]) + + textwidth = self.font.size(self.caption)[0] + font = self.font.render(self.caption, True, self.color) + + # Calculate position + if self.align == 0: # Left + pos = 0 + elif self.align == 1: # Center + pos = (self.rect.width/2) - (textwidth/2) + elif self.align == 2: # Right + pos = self.rect.width - textwidth + + self.surf.blit(font, (pos, 0)) + + def hovering(self, value): + pass + + def click(self, state, pos): + pass + + def got_focus(self): + pass + + def lost_focus(self): + pass + + def keypress(self, ev): + pass + + class GuiError(Exception): pass Modified: trunk/client/playground.py =================================================================== --- trunk/client/playground.py 2010-08-24 23:40:51 UTC (rev 369) +++ trunk/client/playground.py 2010-08-25 11:42:12 UTC (rev 370) @@ -95,21 +95,36 @@ self.surf.blit(self.loginbox, (292,256)) # Create login-field - self.logingui = gui(self.logingui_draw, LoginFeedback(self)) + self.logingui = gui.Container(self.logingui_draw, \ + LoginFeedback(self)) self.logingui.can_lose_focus = False - username = self.logingui.add("textbox", "usr", (345, 279, 310, 37)) - username.set_style("login") + usrlabel = self.logingui.add(gui.Label, "usrlabel", \ + (345, 265, 310, 18)) + usrlabel.caption = "Username:" + usrlabel.align = 1 + usrlabel.color = [177, 93, 39] + usrlabel.set_font(("fonts", "DejaVuSans-Bold.ttf"), 15) - password = self.logingui.add("textbox", "pwd", (345, 340, 310, 37)) - password.set_style("login") - password.set_type("password") + usr = self.logingui.add(gui.Textbox, "usr", (345, 285, 310, 37)) + usr.set_style("login") - login = self.logingui.add("button", "login", (425, 398, 151, 34)) + pwdlabel = self.logingui.add(gui.Label, "pwdlabel", \ + (345, 330, 310, 18)) + pwdlabel.caption = "Password:" + pwdlabel.align = 1 + pwdlabel.color = [177, 93, 39] + pwdlabel.set_font(("fonts", "DejaVuSans-Bold.ttf"), 15) + + pwd = self.logingui.add(gui.Textbox, "pwd", (345, 350, 310, 37)) + pwd.set_style("login") + pwd.set_type("password") + + login = self.logingui.add(gui.Button, "login", (425, 398, 151, 34)) login.caption = "Log in!" login.set_style("login") - self.logingui.give_focus(username) + self.logingui.give_focus(usr) self.logingui.unfreeze() @@ -132,7 +147,7 @@ sh['update'](rect) -class LoginFeedback(): +class LoginFeedback(gui.Feedback): def __init__(self, parent): self.parent = parent @@ -162,3 +177,10 @@ sh['main'].changestatus("logging in") sh['client'].login(usr, pwd) + + def mousedown(self, obj): + if obj.name == "usrlabel": + self.parent.logingui.give_focus('usr') + + elif obj.name == "pwdlabel": + self.parent.logingui.give_focus('pwd') This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |