[Pychord-commits] r37 - trunk/pgui

danilo at garage.maemo.org danilo at garage.maemo.org
Sun Mar 2 18:26:21 EET 2008


Author: danilo
Date: 2008-03-02 18:26:14 +0200 (Sun, 02 Mar 2008)
New Revision: 37

Added:
   trunk/pgui/box.py
   trunk/pgui/locale.py
   trunk/pgui/window.py
Removed:
   trunk/pgui/background.py
Modified:
   trunk/pgui/__init__.py
   trunk/pgui/container.py
   trunk/pgui/pguiobject.py
   trunk/pgui/widget.py
Log:


Modified: trunk/pgui/__init__.py
===================================================================
--- trunk/pgui/__init__.py	2008-03-02 02:49:23 UTC (rev 36)
+++ trunk/pgui/__init__.py	2008-03-02 16:26:14 UTC (rev 37)
@@ -1,7 +1,6 @@
-import widget
+from widget import *
+from window import *
+from pguiobject import *
+from box import *
 
-
-class pyw_defines:
-        
-    MAX_X = 800
-    MAX_Y = 420
\ No newline at end of file
+from locale import *
\ No newline at end of file

Deleted: trunk/pgui/background.py
===================================================================
--- trunk/pgui/background.py	2008-03-02 02:49:23 UTC (rev 36)
+++ trunk/pgui/background.py	2008-03-02 16:26:14 UTC (rev 37)
@@ -1,23 +0,0 @@
-# -*- coding: utf-8 -*-
-
-from pgui import widget
-
-import pygame
-from pygame.locals import *
-
-
-class background (widget):
-    
-    # All widgets needs to know: father, position and size.
-    # background needs to know too the color, the bordersize and
-    # the background color
-    def __init__(self, father, size, color, bsize = 0, bcolor = (200,200,200)):
-        
-        img = pygame.Surface(size)
-        if bsize > 0:
-            img.fill(bcolor)
-        
-        pygame.draw.rect(img, color, Rect(bsize, bsize, size[0]-2*bsize, size[1]-2*bsize))
-        
-        # Creating a new widget
-        pyw_widget.__init__(self, father, position, size, img)

Copied: trunk/pgui/box.py (from rev 36, trunk/pgui/background.py)
===================================================================
--- trunk/pgui/box.py	                        (rev 0)
+++ trunk/pgui/box.py	2008-03-02 16:26:14 UTC (rev 37)
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+
+from pgui import widget
+
+import pygame
+from pygame.locals import *
+
+
+class box (widget):
+    
+    # All widgets needs to know: father, position and size.
+    # background needs to know too the color, the bordersize and
+    # the background color
+    def __init__(self, father, position, size, color, bsize = 0, bcolor = (200,200,200)):
+        
+        img = pygame.Surface(size)
+        if bsize > 0:
+            img.fill(bcolor)
+        
+        pygame.draw.rect(img, color, Rect(bsize, bsize, size[0]-2*bsize, size[1]-2*bsize))
+        
+        # Creating a new widget
+        widget.__init__(self, father, position, size, img)
+        
+    def update(self):
+        
+        if self.clicked:
+            
+            pos = pygame.mouse.get_rel()
+            self.move(pos)

Modified: trunk/pgui/container.py
===================================================================
--- trunk/pgui/container.py	2008-03-02 02:49:23 UTC (rev 36)
+++ trunk/pgui/container.py	2008-03-02 16:26:14 UTC (rev 37)
@@ -1,28 +1,40 @@
 # -*- coding: utf-8 -*-
 
+from pguiobject import * 
+
+import pygame
 from pygame import Surface
 from pygame.locals import *
 
 
 class container (pguiobject):
     
-    def __init__(self, father, objlst = [], position = [0,0], size = [800,420] ):
+    def __init__(self, father, elementList = [], position = [0,0], size = [800,420] ):
 
-        pguiobject.__init__()
+        pguiobject.__init__(self, father, position, size)
+            
+        # Image's rectangle
+        self.rect  =  Rect(self.position,size)
+        
+        
         self.objects = []
-        self.elements_reverted = []
-
+        self.objectsReverted = []
         
-        
+        for obj in elementList:
+            self.addElement(obj)
+            
+        self.objPressed = None
+  
     def addElement(self, obj):
-        self.elements.append(obj)
-        self.elements_reverted.insert(0,obj)
+        self.objects.append(obj)
+        self.objectsReverted.insert(0,obj)
         
         
     def delElement(self, obj):
         self.elements.remove(obj)
-        self.elements_reverted.remove(obj)
+        self.objectsReverted.remove(obj)
         
+        
     def draw(self, screen):
         """
             Simple draw of the screen
@@ -32,18 +44,36 @@
         if self.dirty:
             # if we paint the window
             # we need to pain all the widgets of this window too 
-            self.draw(self.screen)
-            self.dirty = False
-            
-            for obj in self.elements:
+            #self.draw(self.screen)
+
+            pguiobject.drawBackground(self, screen)
+            for obj in self.objects:
                     obj.draw(self.screen)
-                    
+            self.dirty = False        
 
         else:
             # If window is not dirty, we need to
-            # paint only dirtys widgets
-            for obj in self.elements:
+            # paint only dirty's widgets
+            for obj in self.objects:
                 if obj.dirty:
                     obj.draw(self.screen)
                     
-        pygame.display.flip()
\ No newline at end of file
+        pygame.display.flip()
+        
+    def update(self):
+        
+        for obj in self.objects:
+            obj.update()
+            
+    def sendMouseEvents(self, pos):
+        if self.objPressed != None:
+            self.objPressed.mouseState(pos)
+            
+    def setIsPress(self):
+        for obj in self.objects:
+            # For widgets
+            if obj.rect.collidepoint(pygame.mouse.get_pos()):
+                obj.mouseOn = True
+                obj.clicked = True
+                self.objPressed = obj
+                return
\ No newline at end of file

Added: trunk/pgui/locale.py
===================================================================
--- trunk/pgui/locale.py	                        (rev 0)
+++ trunk/pgui/locale.py	2008-03-02 16:26:14 UTC (rev 37)
@@ -0,0 +1,8 @@
+pguiEXIT = 1
+
+
+#background flags
+pguiColor = 1
+pguiImageString = 2
+pguiImage = 3
+

Modified: trunk/pgui/pguiobject.py
===================================================================
--- trunk/pgui/pguiobject.py	2008-03-02 02:49:23 UTC (rev 36)
+++ trunk/pgui/pguiobject.py	2008-03-02 16:26:14 UTC (rev 37)
@@ -1,18 +1,76 @@
 # -*- coding: utf-8 -*-
 
 from pygame import Surface
-from pygame.locals import *
+import pygame
 
+from locale import *
 
-class pguiobject ():
+class pguiobject (object):
     
     """
-     Classe de todos os elemento grafico
+     All graph Element class
 
     :version: 2.0
     :author:
     """
-    def __init__(self):
+    def __init__(self, father, position = [0,0], size = [0,0]):
         self.dirty = True
+        self.background = None
+        self.mouseOn = False
+        self.clicked = False
         
-        
\ No newline at end of file
+        
+        self.father  = father     # father widget in hiearquy
+        self.rel_pos = position   # Defining relative position
+        self.size = size          # Defining widget size
+
+                   
+        # Calculating absolute position on screen
+        if self.father != None:                   
+            self.position = (self.father.position[0] + position[0],
+                             self.father.position[1] + position[1]  ) 
+        else:
+            self.position = position
+            
+            
+        
+    def setBackground(self, bg, flag = pguiColor):
+        
+        if flag == pguiColor:
+            self.background = Surface(self.size)
+            pygame.draw.rect(self.background, bg, self.rect)
+            
+        self.dirty = True
+            
+    def drawBackground(self, screen):
+        
+        
+        if self.background != None:
+            print  "Imprimindo background"
+            screen.blit(self.background, self.position)
+            self.dirty = True
+    
+    def mouseState(self, pos):
+        if self.mouseOn == False:
+            if self.rect.collidepoint(pos):
+                self.mouseOn = True
+                self.onMouseOn()
+        else:
+             if not self.rect.collidepoint(pos):
+                self.mouseOn = False
+                self.onMouseOff()      
+        
+    def onMouseOn(self):
+        print self,"pnMouseON"
+        pass
+    
+    def onMouseOff(self):
+        print self,"onMouseOFF"
+        pass
+    
+
+    def update(self):
+        pass
+    
+    def click(self):
+        pass
\ No newline at end of file

Modified: trunk/pgui/widget.py
===================================================================
--- trunk/pgui/widget.py	2008-03-02 02:49:23 UTC (rev 36)
+++ trunk/pgui/widget.py	2008-03-02 16:26:14 UTC (rev 37)
@@ -1,5 +1,7 @@
 # -*- coding: utf-8 -*-
 
+from pguiobject import *
+
 from pygame import Surface
 from pygame.locals import *
 
@@ -14,20 +16,7 @@
     """
     def __init__(self, father, position = [0,0], size = [0,0], img = None):
         
-        pguiobject.__init__()
-        
-        self.father  = father     # father widget in hiearquy
-        self.rel_pos = position   # Defining relative position
-        self.size = size          # Defining widget size
-
-                   
-        # Calculating absolute position on screen
-        if self.father != None:                   
-            self.position = (self.father.position[0] + position[0],
-                             self.father.position[1] + position[1]  ) 
-        else:
-            print  self.__class__, self.father
-            self.position = position
+        pguiobject.__init__(self, father, position, size)
             
         # Image of this widget
         # We need to do this verification, because a father widget can
@@ -58,4 +47,13 @@
 
             screen.blit(self.img, self.position)
         
-        self.dirty = False
\ No newline at end of file
+        self.dirty = False
+        
+    def move(self, pos):
+        if pos[0] != pos[1] or pos[1] != 0:
+            print "position.old",self.position,"Rect.old",self.rect
+            self.position = (self.position[0] + pos[0], self.position[1] + pos[1])
+            self.rect  =  Rect(self.position,self.img.get_size())
+            print "position.new",self.position,"Rect.new",self.rect
+            self.father.dirty = True
+        
\ No newline at end of file

Added: trunk/pgui/window.py
===================================================================
--- trunk/pgui/window.py	                        (rev 0)
+++ trunk/pgui/window.py	2008-03-02 16:26:14 UTC (rev 37)
@@ -0,0 +1,113 @@
+from container import *
+
+
+from pygame.time   import Clock
+from pygame.locals import *
+
+
+class window (container):
+
+    """
+     Implementacao de janela
+
+    :version:
+    :author:
+    """
+
+    def __init__(self, father, screen, elementList = [], position = [0,0], size = [800,420] ):
+        
+        # SuperClass initialization
+        container.__init__(self, father, elementList, position, size)
+        # MainLoop ON
+        self.loop = True
+        
+        self.screen = screen
+        
+        self.draw(screen)
+        
+        self.clock = Clock()
+        
+        self.fps = 20
+        
+        
+        self.mousePressed = False
+
+
+    def event_handler(self):
+        """
+         Recebe os eventos do pygame
+
+        @return  :
+        @author
+        """
+        events = pygame.event.get()
+        
+        
+        for e in events:
+            if (e.type == MOUSEBUTTONDOWN):
+                self.mousePressed = True
+                pygame.mouse.get_rel()
+                self.setIsPress()
+                
+            if (e.type == MOUSEBUTTONUP):
+                self.mousePressed = False
+                self.sendClick()
+                
+            if (e.type == MOUSEMOTION and self.mousePressed):
+                self.sendMouseEvents(pygame.mouse.get_pos())
+                
+            if (e.type == QUIT):
+                self.close()
+        
+    def show(self):
+        """
+         Abrir a janela
+
+        @return  :
+        @author
+        """
+        self.dirty = True
+        self.__Loop = True
+        
+        while self.__Loop:
+
+            #updating the elements
+            self.update()
+            
+            self.event_handler()
+            
+            # drawning the elements if necessary
+            self.draw(self.screen)
+            
+            
+            self.clock.tick(self.fps)
+    
+    def sendClick(self):
+        
+        if self.objPressed != None:
+            self.objPressed.click()
+            self.objPressed.mouseOn = False
+            self.objPressed.clicked = False
+            self.objPressed = None
+            
+       
+
+    def close(self):
+        """
+         Fechar a janela
+
+        @return  :
+        @author
+        """
+        self.stop()  
+    
+    def bt_close(self):
+        
+        pass
+
+    def stop(self):
+        self.__Loop = False
+        
+        
+    def repaint(self):
+        self.dirty = True
\ No newline at end of file



More information about the Pychord-commits mailing list