Advertisement
nux95

Untitled

Oct 9th, 2011
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.41 KB | None | 0 0
  1. import  c4d
  2. from    c4d.bitmaps import BaseBitmap
  3. from    c4d.gui     import GeUserArea, GeDialog
  4.  
  5. class GeBitmapButton(GeUserArea):
  6.     """
  7.    This GeUserArea subclass shows an image and reacts when the user clicks.
  8.    """
  9.    
  10.     functionMouseInput  = None  # called when the mouse clicks on the userarea
  11.                                 # takes the following arguments:
  12.                                 # (GeBitmapButton, c4d.BaseContainer)
  13.     currentBitmap_      = None  # The bitmap that is currently shown, private
  14.                                 # use GeBitmapButton.setCurrentBitmap and
  15.                                 # GeBitmapButton.getCurrentBitmap instead
  16.     doScaleBitmap       = True  # wether the bitmap is scaled or not
  17.     doScaleHighQuality  = False # wether high quality scaling is enabled or not
  18.     useDoubleBuffer     = True  # wether double buffering is enabled or not
  19.                                 # (double buffering avoids flickering)
  20.     backgroundColor     = c4d.Vector(0) # the userareas backgroundcolor
  21.  
  22.     def __init__(self, bmp = None):
  23.         """
  24.        Initialize the GeBitmapButton instance with a c4d.bitmaps.BaseBitmap
  25.        instance or with None if now bitmap should be drawn.
  26.        """
  27.         self.setCurrentBitmap(bmp)
  28.        
  29.     def GetMinSize(self):
  30.         # overloaded from c4d.gui.GeUserarea
  31.         if self.currentBitmap_:
  32.             return self.currentBitmap_.GetSize()
  33.         else:
  34.             return 0, 0
  35.      
  36.     def DrawMsg(self, x1, y1, x2, y2, msg):
  37.         # overloaded from c4d.gui.GeUserArea
  38.         if self.useDoubleBuffer:
  39.             self.OffScreenOn(x1, y1, x2, y2)    # double buffering, avoid flickering
  40.        
  41.         if self.backgroundColor:
  42.             self.DrawSetPen(self.backgroundColor)
  43.             self.DrawRectangle(x1, y1, x2, y2)
  44.            
  45.         if self.currentBitmap_:
  46.             w, h    = self.currentBitmap_.GetSize()
  47.             if self.doScaleBitmap:
  48.                 destination     = x2 - x1, y2 - y1
  49.             else:
  50.                 destination     = w, h
  51.                
  52.             if self.doScaleHighQuality:
  53.                 drawType    = c4d.BMP_NORMALSCALED
  54.             else:
  55.                 drawType    = c4d.BMP_NORMAL
  56.  
  57.             self.DrawBitmap(
  58.                 self.currentBitmap_,
  59.                 x1, y1, destination[0], destination[1],
  60.                 0, 0, w, h, drawType
  61.             )
  62.            
  63.     def InputEvent(self, msg):
  64.         # overloaded from c4d.gui.GeUserArea
  65.         if msg[c4d.BFM_INPUT_DEVICE] == c4d.BFM_INPUT_MOUSE:
  66.             if self.functionMouseInput:
  67.                 self.functionMouseInput(self, msg)
  68.                
  69.         return True
  70.    
  71.     def setCurrentBitmap(self, bmp):
  72.         """
  73.        Set the current bitmap of the GeBitmapButton instance.
  74.        """
  75.         if bmp and not isinstance(bmp, BaseBitmap):
  76.             raise TypeError, "bmp must be None or c4d.bitmaps.BaseBitmap"
  77.         self.currentBitmap_     = bmp
  78.         self.LayoutChanged()    # from c4d.gui.GeUserArea
  79.            
  80.     def getCurrentBitmap(self):
  81.         """
  82.        Returns the current bitmap.
  83.        """
  84.         return self.currentBitmap
  85.  
  86. class UserAreaDialog(GeDialog):
  87.     """
  88.    A dialog simply showing a GeUserArea.
  89.    """
  90.  
  91.     DEFAULT_OPEN = None
  92.  
  93.     def __init__(self, area):
  94.         self.area = area
  95.  
  96.     def CreateLayout(self):
  97.         self.AddUserArea(1000, c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT)
  98.         self.AttachUserArea(self.area, 1000)
  99.         return True
  100.  
  101.     def Open(self, type = None, *args):
  102.         if type is None:
  103.             if self.DEFAULT_OPEN is None:
  104.                 type = c4d.DLG_TYPE_MODAL_RESIZEABLE
  105.             else:
  106.                 type = self.DEFAULT_OPEN
  107.  
  108.         GeDialog.Open(self, type, *args)
  109.  
  110. IMG01   = r"C:\Users\niklas\Desktop\img1.png"
  111. IMG02   = r"C:\Users\niklas\Desktop\img2.png"
  112.  
  113.      
  114. def main():
  115.     bmp1    = BaseBitmap()
  116.     bmp1.InitWith(IMG01)
  117.     bmp2    = BaseBitmap()
  118.     bmp2.InitWith(IMG02)
  119.    
  120.     toggle  = [0]
  121.    
  122.     def mouseInput(self, msg):
  123.         if toggle[0] == 0:
  124.             toggle[0] = 1
  125.             self.setCurrentBitmap(bmp2)
  126.         else:
  127.             toggle[0] = 0
  128.             self.setCurrentBitmap(bmp1)
  129.         self.Redraw()
  130.  
  131.     bmpb    = GeBitmapButton(bmp1)
  132.     bmpb.functionMouseInput = mouseInput
  133.     dlg     = UserAreaDialog(bmpb)
  134.     dlg.Open()
  135.  
  136. main()
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement