On 11/12/2017 at 01:08, xxxxxxxx wrote:
Hi Andreas,
thanks for your help,
some additional notes and a working example:
- drawing everything into one clipmap and at the end draw this clipmap to the userarea,
seems to be the only working way of layering several alpha bitmaps on top of each other.
- it is important to set to c4d.GE_CM_DRAWMODE_BLEND in GeClipMap.SetDrawMode(),
otherwise the alpha channel is not being used.
- it is important to overwrite GeUserArea.Sized() ,
otherwise the clipmap won't show up
If you feel like it´s ok, you can mark this thread as solved.
import c4d
from c4d import bitmaps
OK = 1003
SHOW = 1005
USERAREA = 1004
class Canvas(c4d.gui.GeUserArea) :
def __init__(self) :
self.w = self.GetWidth()
self.h = self.GetHeight()
self.rectangle=[-1,-1,-1,-1]
self.drawMap = bitmaps.GeClipMap()
###########################
#UserArea Functions
def DrawMsg(self, x1, y1, x2, y2, msg) :
self.OffScreenOn()
#draw everything in one clip map
self.drawMap.BeginDraw()
#draw background
self.drawMap.SetColor(51, 51, 51)
self.drawMap.FillRect(x1, y1, x2, y2)
#ensure that there is an object to draw the icon from
obj=doc.GetFirstObject()
if not obj:
print "no item"
self.drawMap.SetColor(255,170, 24)
self.drawMap.TextAt(3,3,"Please, insert an object into the scene for testing!")
self.drawMap.EndDraw()
self.DrawBitmap(self.drawMap.GetBitmap(), 0, 0, self.GetWidth(), self.GetHeight(),
0, 0, self.GetWidth(), self.GetHeight(),
c4d.BMP_NORMAL)
return
#draw text
self.drawMap.SetColor(255,170, 24)
self.drawMap.TextAt(3,3,str(50000000))
#draw a rectangle
self.drawMap.SetColor(100, 100, 100, 255)
self.drawMap.FillRect(150, 0, 250, 50)
#needs to be set to draw alpha maps--------------------#
self.drawMap.SetDrawMode(c4d.GE_CM_DRAWMODE_BLEND, 255)
#------------------------------------------------------#
#draw icon with alpha
objicon = obj.GetIcon()
bmp = objicon['bmp']
wbmp = objicon['w']
hbmp = objicon['h']
xbmp = objicon['x']
ybmp = objicon['y']
icon = bitmaps.BaseBitmap()
icon.Init(wbmp,hbmp,depth=24)
bmp.CopyPartTo(icon, xbmp, ybmp, wbmp, hbmp)
alphaicon =icon.GetInternalChannel()
iconclip = bitmaps.GeClipMap()
iconclip.InitWithBitmap(icon,alphaicon)
self.drawMap.Blit( 50,50, iconclip, 0, 0, wbmp, hbmp, rop = c4d.GE_CM_BLIT_COL)
#draw drag
self.drawMap.SetColor(200, 200, 255, 100)
xdr,ydr,x2dr,y2dr = self.toolDragSortEx()
self.drawMap.FillRect(xdr,ydr,x2dr,y2dr)
self.drawMap.SetColor(255, 255, 255, 255)
self.drawMap.Rect(xdr,ydr,x2dr,y2dr)
self.drawMap.EndDraw()
self.DrawBitmap(self.drawMap.GetBitmap(), 0, 0, self.GetWidth(), self.GetHeight(),
0, 0, self.GetWidth(), self.GetHeight(),
c4d.BMP_NORMAL)
return
def Sized(self, w, h) :
self.w=w
self.h=h
#needs to be set---------------------------------------#
self.drawMap.Destroy()
self.drawMap.Init(self.w, self.h)
#------------------------------------------------------#
return
def GetMinSize(self) :
return self.w, self.h
def InputEvent(self, msg) :
dev = msg.GetLong(c4d.BFM_INPUT_DEVICE)
if dev == c4d.BFM_INPUT_MOUSE:
return self.HandleMouseEvents(msg)
return False
def HandleMouseEvents(self, msg) :
#init values
mousex = msg.GetLong(c4d.BFM_INPUT_X)
mousey = msg.GetLong(c4d.BFM_INPUT_Y)
start_x = mx = mousex - self.Local2Global()['x']
start_y = my = mousey - self.Local2Global()['y']
#drag interaction
state = c4d.BaseContainer()
self.MouseDragStart(c4d.KEY_MLEFT,start_x, start_y, c4d.MOUSEDRAGFLAGS_DONTHIDEMOUSE| c4d.MOUSEDRAGFLAGS_NOMOVE )
while True:
result, dx, dy, channels = self.MouseDrag()
#end of Drag
if result == c4d.MOUSEDRAGRESULT_ESCAPE:
break
if not self.GetInputState(c4d.BFM_INPUT_MOUSE, c4d.BFM_INPUT_MOUSELEFT, state) :
print "mouse right etc"
break
if state[c4d.BFM_INPUT_VALUE] == 0:
#mouse release
self.rectangle = [-1,-1,-1,-1]
self.Redraw()
break
#not moving, continue
if dx == 0 and dy == 0:
continue
#draging
mx -= dx
my -= dy
#start drag with rectangle
self.rectangle = [start_x,start_y,mx,my]
self.Redraw()
return True
def toolDragSortEx(self) :
if self.rectangle[0]<self.rectangle[2]:
x1,x2 = self.rectangle[0],self.rectangle[2]
else:
x1,x2 = self.rectangle[2],self.rectangle[0]
if self.rectangle[1]<self.rectangle[3]:
y1,y2 = self.rectangle[1],self.rectangle[3]
else:
y1,y2 = self.rectangle[3],self.rectangle[1]
return x1,y1,x2,y2
class AreaDialog(c4d.gui.GeDialog) :
def __init__(self,userarea) :
self.userarea = userarea
def CreateLayout(self) :
self.SetTitle("USERAREATEST")
self.AddUserArea(USERAREA, c4d.BFH_SCALEFIT|c4d.BFV_SCALEFIT)
self.AttachUserArea(self.userarea, USERAREA)
self.AddButton(OK, c4d.BFH_LEFT, name="OK")
self.AddButton(SHOW, c4d.BFH_LEFT, name="Show bitmap")
return True
def Command(self, id, msg) :
if id==OK:
self.Close()
return True
if id==SHOW:
bitmaps.ShowBitmap(self.userarea.drawMap.GetBitmap())
return True
def main() :
userarea = Canvas()
dialog = None
dialog = AreaDialog(userarea)
dialog.Open(dlgtype=c4d.DLG_TYPE_MODAL_RESIZEABLE, xpos=100, ypos=100, defaultw=350, defaulth=500)
if __name__=='__main__':
main()
cheers,
Martin