From: <Blu...@us...> - 2010-08-25 22:03:09
|
Revision: 375 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=375&view=rev Author: BlueWolf_ Date: 2010-08-25 22:02:44 +0000 (Wed, 25 Aug 2010) Log Message: ----------- Added a Link object in the GUI, starting with the Window one and added the ability to create timers for stuff like animations Modified Paths: -------------- trunk/client/VP.py trunk/client/functions.py trunk/client/gui.py trunk/client/layout.py trunk/client/playground.py Modified: trunk/client/VP.py =================================================================== --- trunk/client/VP.py 2010-08-25 21:07:48 UTC (rev 374) +++ trunk/client/VP.py 2010-08-25 22:02:44 UTC (rev 375) @@ -30,6 +30,8 @@ def __init__(self): sh['has_focus'] = True sh['has_hover'] = True + sh['filetable'] = {} + sh['timer'] = Timer() # Fire up pygame os.environ['SDL_VIDEO_CENTERED']='1' @@ -122,7 +124,7 @@ # aka: using unlimited FPS. This will limit the FPS to 50 and will # remember everything that should be updated in the mean time - self.updaterect.append(rect) + self.updaterect.append(Rect(rect)) if self.updatewaiting: # Rect is saved. Will be updated once self.clock stops blocking return @@ -154,6 +156,44 @@ sh['layout'].changestatus(status) sh['windows'].changestatus(status) +class Timer(): + """ + This timer will update every 33ms (30fps) ones called + """ + def __init__(self): + self.speed = 0.033 + self.callers = {} + self.timer = None + + def start(self, name, call, *extra): + # Start a timer for this call + + self.callers[name] = (call, extra) + + if self.timer == None: # Start a timer + self.timer = threading.Timer(self.speed, self.update) + self.timer.start() + + def stop(self, name): + try: del self.callers[name] + except: pass + + if self.callers == {} and self.timer: + self.timer.cancel() + self.timer = None + + def update(self): + for name, info in self.callers.items(): + if info[0](name, *info[1]): + del self.callers[name] + + # Start the next timer + if self.callers != {}: + self.timer = threading.Timer(self.speed, self.update) + self.timer.start() + else: + self.timer = None + class Callback(core.Callback): @@ -195,7 +235,10 @@ # Not doing anything at all # [TODO] Show popup or something, without reconnecting? sh['main'].changestatus("connecting") - + + def custom_received(self, header, body): + if header == "filetable": # List of all the downloadable objects + sh['filetable'] = body Modified: trunk/client/functions.py =================================================================== --- trunk/client/functions.py 2010-08-25 21:07:48 UTC (rev 374) +++ trunk/client/functions.py 2010-08-25 22:02:44 UTC (rev 375) @@ -15,7 +15,7 @@ ## along with this program; if not, write to the Free Software ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -import pygame, os, sys, threading +import pygame, os, sys, threading, time, math from pygame.locals import * from hashlib import sha1 import core Modified: trunk/client/gui.py =================================================================== --- trunk/client/gui.py 2010-08-25 21:07:48 UTC (rev 374) +++ trunk/client/gui.py 2010-08-25 22:02:44 UTC (rev 375) @@ -58,12 +58,13 @@ raise KeyError(key) - def add(self, Obj, name, rect): + def add(self, Obj, name, rect, feedback = None): """ Append a child to this object """ - obj = Obj(name, rect, self, self, self.feedback) + if feedback == None: feedback = self.feedback + obj = Obj(name, rect, self, self, feedback) self.children.append(obj) return obj @@ -276,6 +277,9 @@ self.hover = False self.focus = False + self.cangetfocus = True + self.canrise = False + # Create a surface where an object blits itself onto self.surf = pygame.Surface((self.rect.width, self.rect.height), \ SRCALPHA).convert_alpha() @@ -290,12 +294,34 @@ raise KeyError(key) - def add(self, Obj, name, rect): + def __defaults__(self): + pass + + def render(self): + pass + + 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 + + def add(self, Obj, name, rect, feedback = None): """ Append a child to this object """ - obj = Obj(name, rect, self.main_parent, self, self.feedback) + if feedback == None: feedback = self.feedback + obj = Obj(name, rect, self.main_parent, self) self.children.append(obj) return obj @@ -376,24 +402,37 @@ +class Window(Object): + def __defaults__(self): + self.cangetfocus = False + self.canrise = True + + self.canmove = True + self.hasborder = True + self.caption = "" + self.canclose = True + + def move(self, topos): + # [TODO] + pass + + class Textbox(Object): def __defaults__(self): - self.cangetfocus = True + self.textpos = None # Position of the text currently - self.input = "" + self.font = None + self.backgroundsurf = pygame.Surface((self.rect.width, \ + self.rect.height*2), SRCALPHA).convert_alpha() self.cursorpos = 0 self.style = None self.type = "text" - self.textpos = None # Position of the text currently + self.input = "" - self.font = None - self.backgroundsurf = pygame.Surface((self.rect.width, \ - self.rect.height*2), SRCALPHA).convert_alpha() - def set_style(self, style): """ Change the style for this textbox @@ -495,9 +534,6 @@ if self.focus: pygame.draw.line(self.surf, [118, 58, 19], (pos+cursorpos+6, 10), (pos+cursorpos+6, 26), 2) - def hovering(self, value): - pass - def click(self, state, pos): if state == 0: @@ -580,16 +616,14 @@ def __defaults__(self): self.cangetfocus = False - - self.caption = "" + self.font = None + self.backgroundsurf = pygame.Surface((self.rect.width, \ + self.rect.height*3), SRCALPHA).convert_alpha() self.style = None - self.mousedown = False self.ispressed = False - self.font = None - self.backgroundsurf = pygame.Surface((self.rect.width, \ - self.rect.height*3), SRCALPHA).convert_alpha() + self.caption = "" def set_style(self, style): """ @@ -629,10 +663,8 @@ if self.style == "login": # Which state do we need? if self.ispressed: - top = 68 - elif self.mousedown and not self.ispressed: - top = 0 - elif self.hover: + top = 68 + elif self.hover:# and not self.mousedown: top = 34 else: top = 0 @@ -678,16 +710,7 @@ self.submit() self.ispressed = False - - def got_focus(self): - pass - - def lost_focus(self): - pass - def keypress(self, ev): - pass - def submit(self): self.feedback.submit(self) @@ -700,10 +723,9 @@ def __defaults__(self): self.cangetfocus = False + self.font = None self.caption = "" - - self.font = None self.color = [0, 0, 0] self.align = 0 @@ -739,21 +761,92 @@ pos = self.rect.width - textwidth self.surf.blit(font, (pos, 0)) + + +class Link(Object): + + def __defaults__(self): + self.cangetfocus = False + self.font = None + self.mousedown = False + self.ispressed = False + + self.caption = "" + self.color = [0, 0, 0] + self.color_hover = [100, 100, 100] + self.color_click = [255, 255, 255] + 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 + """ + + if self.font == None: return + + self.surf.fill([0,0,0,0]) + + textwidth = self.font.size(self.caption)[0] + + # 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 + + # Which state do we need? + if self.ispressed: + color = self.color_click + self.font.set_underline(True) + elif self.hover and not self.mousedown: + color = self.color_hover + self.font.set_underline(True) + else: + color = self.color + self.font.set_underline(False) + + font = self.font.render(self.caption, True, color) + + self.surf.blit(font, (pos, 0)) - def hovering(self, value): - pass + def hovering(self, state): + self.update() def click(self, state, pos): - pass + if state == 0: + self.mousedown = True + self.ispressed = True + self.update() + + elif state == 1: + hover = bool(Rect(0, 0, self.rect.width, self.rect.height).collidepoint(pos)) + if hover != self.ispressed: + self.ispressed = hover + self.update() - def got_focus(self): - pass + elif state == 2: + self.mousedown = False - def lost_focus(self): - pass + self.update() + + if self.ispressed: + self.submit() + + self.ispressed = False - def keypress(self, ev): - pass + def submit(self): + self.feedback.submit(self) class GuiError(Exception): Modified: trunk/client/layout.py =================================================================== --- trunk/client/layout.py 2010-08-25 21:07:48 UTC (rev 374) +++ trunk/client/layout.py 2010-08-25 22:02:44 UTC (rev 375) @@ -38,6 +38,7 @@ # Playground stuff self.topbar = None + self.toppos = None def event(self, ev): if self.status == "playground": @@ -69,33 +70,43 @@ self.selected = None else: self.selected = index + self.updatetop() return True def changestatus(self, status): + sh['timer'].stop("layout-topslide") # Just in case + login_list = ['connecting', 'login', 'logging in'] if status in login_list and self.status not in login_list: # (Un)load all the data - self.topbar = None - if self.status == "playground": - self.cleartop() + if self.topbar: + # Slide the topbar out + sh['timer'].start("layout-topslide", self.timer, time.time(), 0) elif status == "playground": self.topbar = load_image(True, "images", "topbar.png") self.selected = None self.hover = self.topbarcursor(pygame.mouse.get_pos()) - self.surf.blit(self.topbar, (0,0), (0,0,1000,TOPHEIGHT)) - self.updatetop() + + self.toppos = 0 + + # Slide the topbar in + sh['timer'].start("layout-topslide", self.timer, time.time(), 1) self.status = status - def updatetop(self): + def updatetop(self, wholebar = False): + if wholebar: + self.surf.fill([0,0,0,0]) + self.surf.blit(self.topbar, (0,self.toppos), (0,0,1000,TOPHEIGHT)) + for i in range(TOPBUTTONS): left = TOPLEFT + (TOPBUTTONWIDTH*i) - rect = Rect(left, 0, TOPBUTTONWIDTH, TOPHEIGHT) + rect = Rect(left, self.toppos, TOPBUTTONWIDTH, TOPHEIGHT) self.surf.fill([0,0,0,0], rect) @@ -108,18 +119,45 @@ else: rect2 = rect.move(0,0) + rect2.move_ip(0, -self.toppos) self.surf.blit(self.topbar, rect, rect2) sh['update']((0,0, 1000, 65)) - def cleartop(self): - # Remove the top bar from view - self.surf.fill([0,0,0,0]) - sh['update']((0,0, 1000, 65)) - def topbarcursor(self, pos): if pos[0] > TOPLEFT and pos[0] < (TOPBUTTONWIDTH*TOPBUTTONS+TOPLEFT) \ and pos[1] < 55: return (pos[0]-TOPLEFT) / TOPBUTTONWIDTH else: return False + + + def timer(self, name, starttime, direction): + if name == "layout-topslide": # Slide the topbar! + length = 0.3 + timeline = (time.time() - starttime) / length + + # Timer has ended + if timeline > 1: + if direction == 0 : # Up + # Clear the bar + self.topbar = None + self.surf.fill([0,0,0,0]) + sh['update']((0,0, 1000, 65)) + + elif direction == 1: # Down + toppos = 0 + self.updatetop(True) + + return True + + # Calculate current position + if direction == 0: # Up + pos = 1-math.pow(timeline, 2) + pos = (pos - 1) * TOPHEIGHT + elif direction == 1: # Down + pos = 1 - math.pow(1-timeline, 2) + pos = (pos - 1) * TOPHEIGHT + + self.toppos = pos + self.updatetop(True) Modified: trunk/client/playground.py =================================================================== --- trunk/client/playground.py 2010-08-25 21:07:48 UTC (rev 374) +++ trunk/client/playground.py 2010-08-25 22:02:44 UTC (rev 375) @@ -124,6 +124,14 @@ login.caption = "Log in!" login.set_style("login") + signup = self.logingui.add(gui.Link, "signup", (345, 470, 310, 18)) + signup.caption = "Don't have an account yet?" + signup.align = 1 + signup.color = [140, 74, 31] + signup.color_hover = [177, 93, 39] + signup.color_click = [255, 217, 192] + signup.set_font(("fonts", "DejaVuSans.ttf"), 12) + self.logingui.give_focus(usr) self.logingui.unfreeze() @@ -177,6 +185,9 @@ sh['main'].changestatus("logging in") sh['client'].login(usr, pwd) + + elif obj.name == "signup": + print "signup!" def mousedown(self, obj): if obj.name == "usrlabel": This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |