Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import c4d
- from c4d.bitmaps import BaseBitmap
- from c4d.gui import GeUserArea, GeDialog
- class GeBitmapButton(GeUserArea):
- """
- This GeUserArea subclass shows an image and reacts when the user clicks.
- """
- functionMouseInput = None # called when the mouse clicks on the userarea
- # takes the following arguments:
- # (GeBitmapButton, c4d.BaseContainer)
- currentBitmap_ = None # The bitmap that is currently shown, private
- # use GeBitmapButton.setCurrentBitmap and
- # GeBitmapButton.getCurrentBitmap instead
- doScaleBitmap = True # wether the bitmap is scaled or not
- doScaleHighQuality = False # wether high quality scaling is enabled or not
- useDoubleBuffer = True # wether double buffering is enabled or not
- # (double buffering avoids flickering)
- backgroundColor = c4d.Vector(0) # the userareas backgroundcolor
- def __init__(self, bmp = None):
- """
- Initialize the GeBitmapButton instance with a c4d.bitmaps.BaseBitmap
- instance or with None if now bitmap should be drawn.
- """
- self.setCurrentBitmap(bmp)
- def GetMinSize(self):
- # overloaded from c4d.gui.GeUserarea
- if self.currentBitmap_:
- return self.currentBitmap_.GetSize()
- else:
- return 0, 0
- def DrawMsg(self, x1, y1, x2, y2, msg):
- # overloaded from c4d.gui.GeUserArea
- if self.useDoubleBuffer:
- self.OffScreenOn(x1, y1, x2, y2) # double buffering, avoid flickering
- if self.backgroundColor:
- self.DrawSetPen(self.backgroundColor)
- self.DrawRectangle(x1, y1, x2, y2)
- if self.currentBitmap_:
- w, h = self.currentBitmap_.GetSize()
- if self.doScaleBitmap:
- destination = x2 - x1, y2 - y1
- else:
- destination = w, h
- if self.doScaleHighQuality:
- drawType = c4d.BMP_NORMALSCALED
- else:
- drawType = c4d.BMP_NORMAL
- self.DrawBitmap(
- self.currentBitmap_,
- x1, y1, destination[0], destination[1],
- 0, 0, w, h, drawType
- )
- def InputEvent(self, msg):
- # overloaded from c4d.gui.GeUserArea
- if msg[c4d.BFM_INPUT_DEVICE] == c4d.BFM_INPUT_MOUSE:
- if self.functionMouseInput:
- self.functionMouseInput(self, msg)
- return True
- def setCurrentBitmap(self, bmp):
- """
- Set the current bitmap of the GeBitmapButton instance.
- """
- if bmp and not isinstance(bmp, BaseBitmap):
- raise TypeError, "bmp must be None or c4d.bitmaps.BaseBitmap"
- self.currentBitmap_ = bmp
- self.LayoutChanged() # from c4d.gui.GeUserArea
- def getCurrentBitmap(self):
- """
- Returns the current bitmap.
- """
- return self.currentBitmap
- class UserAreaDialog(GeDialog):
- """
- A dialog simply showing a GeUserArea.
- """
- DEFAULT_OPEN = None
- def __init__(self, area):
- self.area = area
- def CreateLayout(self):
- self.AddUserArea(1000, c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT)
- self.AttachUserArea(self.area, 1000)
- return True
- def Open(self, type = None, *args):
- if type is None:
- if self.DEFAULT_OPEN is None:
- type = c4d.DLG_TYPE_MODAL_RESIZEABLE
- else:
- type = self.DEFAULT_OPEN
- GeDialog.Open(self, type, *args)
- IMG01 = r"C:\Users\niklas\Desktop\img1.png"
- IMG02 = r"C:\Users\niklas\Desktop\img2.png"
- def main():
- bmp1 = BaseBitmap()
- bmp1.InitWith(IMG01)
- bmp2 = BaseBitmap()
- bmp2.InitWith(IMG02)
- toggle = [0]
- def mouseInput(self, msg):
- if toggle[0] == 0:
- toggle[0] = 1
- self.setCurrentBitmap(bmp2)
- else:
- toggle[0] = 0
- self.setCurrentBitmap(bmp1)
- self.Redraw()
- bmpb = GeBitmapButton(bmp1)
- bmpb.functionMouseInput = mouseInput
- dlg = UserAreaDialog(bmpb)
- dlg.Open()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement