From: <Blu...@us...> - 2010-08-08 15:24:59
|
Revision: 362 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=362&view=rev Author: BlueWolf_ Date: 2010-08-08 15:24:53 +0000 (Sun, 08 Aug 2010) Log Message: ----------- Textbox now with working cursor! Modified Paths: -------------- trunk/client/gui.py trunk/client/playground.py Modified: trunk/client/gui.py =================================================================== --- trunk/client/gui.py 2010-08-07 21:37:36 UTC (rev 361) +++ trunk/client/gui.py 2010-08-08 15:24:53 UTC (rev 362) @@ -55,7 +55,7 @@ else: raise GuiError("Object '%s' has not been found" % objname) - obj = Obj(name, rect, self.update, self.update_mouse) + obj = Obj(name, rect, self, self) self.children.append(obj) return obj @@ -130,6 +130,25 @@ self.hover.hovering(False) self.hover = None + def give_focus(self, *obj): + if type(obj[0]) == str: + child = self + for part in obj: + child = child[part] + else: + child = obj[0] + + if self.focus: + self.focus.focus = False + self.focus.lost_focus() + self.focus = None + + self.focus = child + + # Tell this object + child.focus = True + child.got_focus(0) + def event(self, ev): """ All the events from pygame should go here @@ -145,7 +164,7 @@ # the interface pos = (ev.pos[0] - self.mousedown[0][0], \ ev.pos[1] - self.mousedown[0][1]) - self.mousedown[1].click(1, pos) + self.mousedown[1].got_focus(1, pos) return True else: # Check which object we hover @@ -171,7 +190,7 @@ hover.focus = True pos = (ev.pos[0] - rect[0], ev.pos[1] - rect[1]) - hover.click(0, pos) + hover.got_focus(0, pos) self.update_mouse() @@ -188,7 +207,7 @@ # Release mousebutton after we've clicked on an object pos = (ev.pos[0] - self.mousedown[0][0], \ ev.pos[1] - self.mousedown[0][1]) - self.mousedown[1].click(2, pos) + self.mousedown[1].got_focus(2, pos) self.mousedown = None self.update_mouse() @@ -211,15 +230,21 @@ return rect, hover return None, None + + def focus_next(self, current): + pos = self.children.index(current) + 1 + if pos == len(self.children): pos = 0 + + self.give_focus(self.children[pos]) class Object(): - def __init__(self, name, rect, update, update_mouse): + def __init__(self, name, rect, main_parent, parent): self.name = name self.rect = Rect(rect) - self.parent_update = update - self.update_mouse = update_mouse + self.main_parent = main_parent + self.parent = parent self.needs_update = False self.frozen = True # Don't update until everything has been # loaded @@ -228,6 +253,9 @@ self.hover = False self.focus = False + # Callbacks + self.cb_keypress = None + # Create a surface where an object blits itself onto self.surf = pygame.Surface((self.rect.width, self.rect.height), \ SRCALPHA).convert_alpha() @@ -254,7 +282,7 @@ else: raise GuiError("Object '%s' has not been found" % objname) - obj = Obj(name, rect, self.update, self.update_mouse) + obj = Obj(name, rect, self.main_parent, self) self.children.append(obj) return obj @@ -278,7 +306,7 @@ # We need to calculate our relative position rect.move_ip(self.rect.left, self.rect.top) - self.parent_update(rect) + self.parent.update(rect) def blit(self, rect, surf, onto): """ @@ -309,13 +337,12 @@ child.unfreeze(False) if isparent: - self.update_mouse() + self.main_parent.update_mouse() self.frozen = False self.update(self.rect) else: self.frozen = False - - + def this_pos(self, pos): # Return the object on this position @@ -329,6 +356,12 @@ return rect, hover return self.rect.move(0,0), self + + def focus_next(self, current): + pos = self.children.index(current) + 1 + if pos == len(self.children): pos = 0 + + self.give_focus(self.children[pos]) class Textbox(Object): @@ -406,6 +439,8 @@ self.surf.blit(self.backgroundsurf, (0,0), \ (0, top, self.rect.width, 37)) + truewidth = self.rect.width - 12 # Width without the borders + # Blit the text inp = self.input if self.type == "password": @@ -413,13 +448,29 @@ color = [(141, 56, 0), (189, 84, 15)][self.focus] text = self.font.render(inp, True, color) - self.surf.blit(text, (10, 10)) + textwidth = text.get_width() + + cursorpos = self.font.size(inp[:self.cursorpos])[0] + if textwidth < truewidth: # Text is smaller than the input + pos = (truewidth/2) - (textwidth/2) + elif cursorpos < (truewidth/2): # Cursor is on the left + pos = 0 + elif textwidth-cursorpos < (truewidth/2): # Cursor is on the right + pos = -textwidth + truewidth - 2 + else: # Cursor is in the center + pos = -cursorpos + (truewidth/2) + + self.surf.blit(text, (6, 10), (-pos, 0, truewidth, 17)) + + # Blit cursor + if self.focus: + pygame.draw.line(self.surf, [118, 58, 19], (pos+cursorpos+6, 10), (pos+cursorpos+6, 26), 2) def hovering(self, value): print "hover", self.name, value - def click(self, state, pos): - print "click", self.name, state, pos + def got_focus(self, state, pos = None): + print "got_focus", self.name, state, pos if state == 0: # We got the focus! # [TODO] Calculate position based on pos @@ -452,14 +503,26 @@ elif ev.key == K_DELETE: # Delete self.input = self.input[:self.cursorpos] + \ self.input[self.cursorpos+1:] - + + elif ev.key == K_HOME: # Go to the beginning + self.cursorpos = 0 + + elif ev.key == K_END: # Go to the end + self.cursorpos = len(self.input) + + elif ev.key == K_TAB: # Focus the next object + self.parent.focus_next(self) + + elif ev.unicode == "\r": # Ignore this + pass elif ev.unicode != "": # Normal text - self.input = self.input[:self.cursorpos] + ev.unicode + \ self.input[self.cursorpos:] self.cursorpos += 1 self.update() + + if self.cb_keypress: self.cb_keypress(self, ev) class GuiError(Exception): Modified: trunk/client/playground.py =================================================================== --- trunk/client/playground.py 2010-08-07 21:37:36 UTC (rev 361) +++ trunk/client/playground.py 2010-08-08 15:24:53 UTC (rev 362) @@ -38,7 +38,7 @@ if self.status == "login": # Some temporary debug stuff - if ev.type == KEYDOWN and ev.unicode == "\r": + if ev.type == KEYDOWN and ev.key == K_F2: # Fake login self.drawlogin("logging in") sh['client'].login("test123", sha1("test123").hexdigest()) @@ -103,13 +103,15 @@ username = self.logingui.add("textbox", "usr", (351, 279, 298, 37)) username.set_style("login") + username.cb_keypress = self.guikeypress password = self.logingui.add("textbox", "pwd", (351, 340, 298, 37)) password.set_style("login") password.set_type("password") + password.cb_keypress = self.guikeypress #login = gui.Button("login", (0, 100, 417, 50)) - + self.logingui.give_focus(username) self.logingui.unfreeze() @@ -128,3 +130,9 @@ self.surf.blit(self.logingui.surf, rect, rect) sh['update'](rect) + + def guikeypress(self, obj, ev): + if ev.type == KEYDOWN: + if ev.unicode == "\r": + if obj.name == "usr": + self.logingui.give_focus("pwd") This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |