From: <Blu...@us...> - 2010-08-07 21:37:42
|
Revision: 361 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=361&view=rev Author: BlueWolf_ Date: 2010-08-07 21:37:36 +0000 (Sat, 07 Aug 2010) Log Message: ----------- Added more interaction with the GUI. You can now type something into the textboxes. I also moved the fake-registration button to F1, in case one still needs it Modified Paths: -------------- trunk/client/gui.py trunk/client/playground.py Modified: trunk/client/gui.py =================================================================== --- trunk/client/gui.py 2010-08-07 20:18:26 UTC (rev 360) +++ trunk/client/gui.py 2010-08-07 21:37:36 UTC (rev 361) @@ -30,6 +30,7 @@ self.children = [] self.hover = None self.focus = None + self.mousedown = None # Create a surface where an object blits itself onto self.surf = pygame.Surface((self.rect.width, self.rect.height), \ @@ -103,31 +104,32 @@ if sh['has_hover']: # Get the current hovered object - hover = self.this_pos(pos) + rect, 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.hovering(False) self.hover = hover # Hover the current one if self.hover: self.hover.hover = True - self.hover.update() + self.hover.hovering(True) + + if hover: return True # Disable events further down the interface + else: # Remove the previous hover if self.hover: self.hover.hover = False - self.hover.update() + self.hover.hovering(False) self.hover = None - - def event(self, ev): """ All the events from pygame should go here @@ -136,23 +138,79 @@ if self.frozen: return if ev.type == MOUSEMOTION: # Mouse movement - self.update_mouse(ev.pos) + + # When the mousebutton is pressed on a object + if self.mousedown: + # Tell this object and stop processing this event further down + # the interface + pos = (ev.pos[0] - self.mousedown[0][0], \ + ev.pos[1] - self.mousedown[0][1]) + self.mousedown[1].click(1, pos) + return True + else: + # Check which object we hover + return self.update_mouse(ev.pos) elif ev.type == ACTIVEEVENT and ev.state == 1: # Mouse focus self.update_mouse() + + elif ev.type == MOUSEBUTTONDOWN: # Clicking + # Check which object we've focused + rect, hover = self.this_pos(ev.pos) + if hover: + # Change the focus + if self.focus: + self.focus.focus = False + self.focus.lost_focus() + self.focus = None + + self.focus = hover + + # Tell this object + self.mousedown = (rect, hover) + + hover.focus = True + pos = (ev.pos[0] - rect[0], ev.pos[1] - rect[1]) + hover.click(0, pos) + + self.update_mouse() + + return True + else: + # Did an object lost focus? + if self.focus: + self.focus.focus = False + self.focus.lost_focus() + self.focus = None + + elif ev.type == MOUSEBUTTONUP and self.mousedown: # Clicking + # 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 = None + + self.update_mouse() + + return True + + elif (ev.type == KEYDOWN or ev.type == KEYUP) and self.focus: + self.focus.keypress(ev) + def this_pos(self, pos): - # Return the object on this position + # Return the object and their actual rect 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 + rect, hover = child.this_pos(child_pos) + if hover: + return rect, hover - return None + return None, None class Object(): @@ -266,9 +324,11 @@ child_pos = (pos[0] - child.rect[0], pos[1] - child.rect[1]) - return child.this_pos(child_pos) + rect, hover = child.this_pos(child_pos) + rect.move_ip(child.rect[0], child.rect[1]) + return rect, hover - return self + return self.rect.move(0,0), self class Textbox(Object): @@ -279,6 +339,7 @@ self.style = None self.type = "text" + self.font = None self.backgroundsurf = pygame.Surface((self.rect.width, \ self.rect.height*2), SRCALPHA).convert_alpha() @@ -295,6 +356,9 @@ layoutimg = load_image(True, "images", "gui", "text_login.png") + self.font = pygame.font.Font(real_path("fonts", \ + "DejaVuSans-Bold.ttf"), 14) + # Create backgroundsurf self.backgroundsurf.fill([0,0,0,0]) @@ -334,13 +398,68 @@ if self.style == "login": # Which state do we need? - if self.hover: + if self.focus: top = 37 else: top = 0 self.surf.blit(self.backgroundsurf, (0,0), \ (0, top, self.rect.width, 37)) + + # Blit the text + inp = self.input + if self.type == "password": + inp = "*"*len(self.input) + + color = [(141, 56, 0), (189, 84, 15)][self.focus] + text = self.font.render(inp, True, color) + self.surf.blit(text, (10, 10)) + + def hovering(self, value): + print "hover", self.name, value + + def click(self, state, pos): + print "click", self.name, state, pos + + if state == 0: # We got the focus! + # [TODO] Calculate position based on pos + + self.update() + + def lost_focus(self): + print "lost_focus", self.name + + self.cursorpos = len(self.input) + self.update() + + def keypress(self, ev): + if ev.type == KEYDOWN: + if ev.key == K_LEFT: # Cursorpos change + if self.cursorpos > 0: self.cursorpos -= 1 + + elif ev.key == K_RIGHT: # Cursorpos change + if self.cursorpos < len(self.input): self.cursorpos += 1 + + elif ev.key == K_BACKSPACE: # Backspace + if ev.mod & KMOD_CTRL: + self.input = "" + self.cursorpos = 0 + else: + self.input = self.input[:self.cursorpos-1] + \ + self.input[self.cursorpos:] + if self.cursorpos > 0: self.cursorpos -= 1 + + elif ev.key == K_DELETE: # Delete + self.input = self.input[:self.cursorpos] + \ + self.input[self.cursorpos+1:] + + elif ev.unicode != "": # Normal text + + self.input = self.input[:self.cursorpos] + ev.unicode + \ + self.input[self.cursorpos:] + self.cursorpos += 1 + + self.update() class GuiError(Exception): Modified: trunk/client/playground.py =================================================================== --- trunk/client/playground.py 2010-08-07 20:18:26 UTC (rev 360) +++ trunk/client/playground.py 2010-08-07 21:37:36 UTC (rev 361) @@ -43,7 +43,7 @@ self.drawlogin("logging in") sh['client'].login("test123", sha1("test123").hexdigest()) - elif ev.type == KEYDOWN and ev.unicode == "r": + elif ev.type == KEYDOWN and ev.key == K_F1: # Fake signup sh['client'].signup("test123", sha1("test123").hexdigest(), { @@ -52,7 +52,7 @@ }) if self.logingui != None: - self.logingui.event(ev) + return self.logingui.event(ev) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |