From: <Blu...@us...> - 2010-08-09 17:28:31
|
Revision: 364 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=364&view=rev Author: BlueWolf_ Date: 2010-08-09 17:28:24 +0000 (Mon, 09 Aug 2010) Log Message: ----------- Fixed the GUI textbox regarding getting focus and getting clicks. The GUI was a bit confused with both of them Modified Paths: -------------- trunk/client/gui.py trunk/client/playground.py Modified: trunk/client/gui.py =================================================================== --- trunk/client/gui.py 2010-08-08 17:38:24 UTC (rev 363) +++ trunk/client/gui.py 2010-08-09 17:28:24 UTC (rev 364) @@ -32,6 +32,9 @@ self.focus = None self.mousedown = None + # Settings + self.can_lose_focus = True + # Create a surface where an object blits itself onto self.surf = pygame.Surface((self.rect.width, self.rect.height), \ SRCALPHA).convert_alpha() @@ -147,13 +150,12 @@ # Tell this object child.focus = True - child.got_focus(0) + child.got_focus() def event(self, ev): """ All the events from pygame should go here """ - if self.frozen: return if ev.type == MOUSEMOTION: # Mouse movement @@ -164,7 +166,7 @@ # the interface pos = (ev.pos[0] - self.mousedown[0][0], \ ev.pos[1] - self.mousedown[0][1]) - self.mousedown[1].got_focus(1, pos) + self.mousedown[1].click(1, pos) return True else: # Check which object we hover @@ -177,28 +179,29 @@ # 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 + pos = (ev.pos[0] - rect[0], ev.pos[1] - rect[1]) + hover.click(0, pos) - 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.got_focus(0, pos) + if self.focus != hover: + # Change the focus + if self.focus: + self.focus.focus = False + self.focus.lost_focus() + self.focus = None + + self.focus = hover - self.update_mouse() + # Tell this object + hover.focus = True + hover.got_focus() return True else: # Did an object lost focus? - if self.focus: + if self.focus and self.can_lose_focus: self.focus.focus = False self.focus.lost_focus() self.focus = None @@ -207,7 +210,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].got_focus(2, pos) + self.mousedown[1].click(2, pos) self.mousedown = None self.update_mouse() @@ -238,6 +241,9 @@ self.give_focus(self.children[pos]) + + + class Object(): def __init__(self, name, rect, main_parent, parent): @@ -364,6 +370,9 @@ self.give_focus(self.children[pos]) + + + class Textbox(Object): def __defaults__(self): @@ -378,15 +387,20 @@ def set_style(self, style): """ + Change the stle for this textbox Possible styles: * login """ if style == "login": + + # Check the requirements if self.rect.height != 37: raise GuiError("Height should be 37") if self.rect.width < 25: raise GuiError("Width should be 25 or bigger ") + # Load the base images. We will generated the actual images right + # now layoutimg = load_image(True, "images", "gui", "text_login.png") self.font = pygame.font.Font(real_path("fonts", \ @@ -395,12 +409,12 @@ # Create backgroundsurf self.backgroundsurf.fill([0,0,0,0]) - # Left + # Left part self.backgroundsurf.blit(layoutimg, (0, 0), (0, 0, 12, 74)) - # Right + # Right part self.backgroundsurf.blit(layoutimg, (self.rect.width-12, 0), \ (15, 0, 12, 74)) - # Middle + # Middle part 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)) @@ -414,6 +428,7 @@ def set_type(self, type): """ + Change the type for this textbox Possible types: * text * password @@ -425,6 +440,9 @@ self.update() def render(self): + """ + Render our surface, as requested from the all mighty Container + """ if self.backgroundsurf == None: return self.surf.fill([0,0,0,0]) @@ -467,20 +485,21 @@ 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 + pass - def got_focus(self, state, pos = None): - print "got_focus", self.name, state, pos - - if state == 0: # We got the focus! + def click(self, state, pos): + if state == 0: # [TODO] Calculate position based on pos + # self.cursorpos = ... - self.update() + if self.focus: + self.update() + + def got_focus(self): + # We got the focus! + self.update() def lost_focus(self): - print "lost_focus", self.name - - self.cursorpos = len(self.input) self.update() def keypress(self, ev): @@ -492,7 +511,7 @@ self.cursorpos = 0 else: - + print pygame.key.name(ev.key) if ev.key == K_LEFT: # Cursorpos change if self.cursorpos > 0: self.cursorpos -= 1 @@ -500,9 +519,10 @@ if self.cursorpos < len(self.input): self.cursorpos += 1 elif ev.key == K_BACKSPACE: # Backspace - self.input = self.input[:self.cursorpos-1] + \ - self.input[self.cursorpos:] - if self.cursorpos > 0: self.cursorpos -= 1 + if self.cursorpos != 0: + self.input = self.input[:self.cursorpos-1] + \ + self.input[self.cursorpos:] + self.cursorpos -= 1 elif ev.key == K_DELETE: # Delete self.input = self.input[:self.cursorpos] + \ Modified: trunk/client/playground.py =================================================================== --- trunk/client/playground.py 2010-08-08 17:38:24 UTC (rev 363) +++ trunk/client/playground.py 2010-08-09 17:28:24 UTC (rev 364) @@ -99,16 +99,17 @@ self.surf.blit(self.loginbox, (292,256)) # Create login-field - self.logingui = gui(self.drawlogingui) + self.logingui = gui(self.logingui_draw) + self.logingui.can_lose_focus = False - username = self.logingui.add("textbox", "usr", (351, 279, 298, 37)) + username = self.logingui.add("textbox", "usr", (345, 279, 310, 37)) username.set_style("login") - username.cb_keypress = self.guikeypress + username.cb_keypress = self.logingui_keypress - password = self.logingui.add("textbox", "pwd", (351, 340, 298, 37)) + password = self.logingui.add("textbox", "pwd", (345, 340, 310, 37)) password.set_style("login") password.set_type("password") - password.cb_keypress = self.guikeypress + password.cb_keypress = self.logingui_keypress #login = gui.Button("login", (0, 100, 417, 50)) self.logingui.give_focus(username) @@ -122,7 +123,7 @@ sh['update']() - def drawlogingui(self, rect): + def logingui_draw(self, rect): print "GUIupdate", rect self.surf.blit(self.loginbg, rect, rect) @@ -131,7 +132,7 @@ sh['update'](rect) - def guikeypress(self, obj, ev): + def logingui_keypress(self, obj, ev): if ev.type == KEYDOWN: if ev.unicode == "\r": if obj.name == "usr": This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |