Hi @m_adam
Thanks for the patience. Your suggestions and reminders works are greatly appreciated.
I guess the confusion stems mainly on my part because I posted slightly two different codes. You were responding to my initial post but I was thinking with the code from the succeeding post(the one in the rar file). Totally my bad.
Anyhow, here is the working code (using the initial post) which works as I expected:
import c4d
class ColorButton(object):
def __init__(self):
self.width = None
self.height = None
self.color = None
self.color = None
self.btn_id = None
self.menu_list = None
def create(self, dlg, w, h, color, btn_id):
self.width = w
self.height = h
self.color = color
self.btn_id = btn_id
bmp_color = c4d.bitmaps.BaseBitmap()
bmp_color.Init(w, h)
for y in xrange(w):
for x in xrange(h):
bmp_color.SetPixel(x, y, color[0], color[1], color[2])
bcBitmapButton = c4d.BaseContainer()
bcBitmapButton[c4d.BITMAPBUTTON_BUTTON] = True
bmp_btn = dlg.AddCustomGui(self.btn_id, c4d.CUSTOMGUI_BITMAPBUTTON, "", c4d.BFH_CENTER | c4d.BFV_CENTER, w, h, bcBitmapButton)
bmp_btn.SetImage(bmp_color, True)
def create_menu(self):
self.menu = c4d.BaseContainer()
for menu_item in self.menu_list:
counter = 0
IDM_MENU = c4d.FIRST_POPUP_ID + counter
self.menu.InsData(IDM_MENU, menu_item)
counter += 1
class MyDialog(c4d.gui.GeDialog):
def __init__(self):
self.btn_id_list = []
self.class_btn_id_dict = {}
def CreateLayout(self):
red_button = ColorButton()
red_button.create(self, w=50,h=50,color=(255,0,0), btn_id=6000)
red_button.menu_list = ['Menu1', 'Menu2', 'Menu3']
self.btn_id_list.append(red_button.btn_id)
self.class_btn_id_dict[6000] = red_button
blue_button = ColorButton()
blue_button.create(self, w=50,h=50,color=(0,0,255), btn_id=7000)
blue_button.menu_list = ['Menu4', 'Menu5', 'Menu6', 'Menu7']
self.btn_id_list.append(blue_button.btn_id)
self.class_btn_id_dict[7000] = blue_button
green_button = ColorButton()
green_button.create(self, w=50,h=50,color=(0,255,0), btn_id=8000)
green_button.menu_list = ['Menu8', 'Menu9']
self.btn_id_list.append(green_button.btn_id)
self.class_btn_id_dict[8000] = green_button
return True
def IsPositionOnGadget(self, gadgetId, x, y):
# Be sure that the windows is opened,
# in our case since we call it in BFM_INTERACTSTART it's ok
buttonData = self.GetItemDim(gadgetId)
if not buttonData["x"] < x < buttonData["x"] + buttonData["w"]:
return False
if not buttonData["y"] < y < buttonData["y"] + buttonData["h"]:
return False
return True
def function_to_determine_gadgetId_under_mouse_cursor(self, x, y):
for gadgetId in self.btn_id_list:
if self.IsPositionOnGadget(gadgetId, x, y):
return gadgetId
def Message(self, msg, result):
if msg.GetId() == c4d.BFM_ADJUSTSIZE:
self._x = msg[3] # Retrieve Y size of the GeDialog
self._y = msg[4] # Retrieve Y size of the GeDialog
# We are on the main thread here
elif msg.GetId() == c4d.BFM_INTERACTSTART:
c4d.StopAllThreads()
state = c4d.BaseContainer()
self.GetInputState(c4d.BFM_INPUT_MOUSE, c4d.BFM_INPUT_MOUSERIGHT, state)
if state.GetInt32(c4d.BFM_INPUT_VALUE) == True:
x = state.GetInt32(c4d.BFM_INPUT_X)
y = state.GetInt32(c4d.BFM_INPUT_Y)
g2l = self.Global2Local()
x += g2l['x']
y += g2l['y']
gadgetId = self.function_to_determine_gadgetId_under_mouse_cursor(x=x,y=y)
if gadgetId in self.btn_id_list:
if self.IsPositionOnGadget(gadgetId=gadgetId, x=x, y=y):
button_class = self.class_btn_id_dict[gadgetId]
button_class.create_menu()
l2s = self.Local2Screen()
print str(x+l2s['x']) + " :: " + str(y+l2s['y'])
self.KillEvents()
res = c4d.gui.ShowPopupDialog(cd=self, bc=button_class.menu, x=x+l2s['x'], y=y+l2s['y'])
return True
return c4d.gui.GeDialog.Message(self, msg, result)
if __name__ == "__main__":
dlg = MyDialog()
dlg.Open(dlgtype=c4d.DLG_TYPE_ASYNC, pluginid=20304050)