Hi,
Is there a command to get the Button Gadget ID directly under the mouse?
I understand that you can get the GadgetID by clicking the button but I'm using the Message()
rather than Command()
.
In overview, I have several buttons and when you RMB them, they have there own dedicated pop-up commands.
It works when I have them typed separately but I'm having a problem with making them dynamic through a class.
You can see the illustration here:
https://www.dropbox.com/s/k5d38cctk4hgtz7/c4d206_get_gadgetid_under_mouse.jpg?dl=0
You can check the WIP code here:
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 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,0,255), btn_id=7000)
green_button.menu_list = ['Menu8', 'Menu9']
self.btn_id_list.append(blue_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 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 = function_to_determine_gadgetId_under_mouse_cursor()
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.menu()
l2s = self.Local2Screen()
print str(x+l2s['x']) + " :: " + str(y+l2s['y'])
self.KillEvents()
res = c4d.gui.ShowPopupDialog(cd=self, bc=menu, x=x+l2s['x'], y=y+l2s['y'])
return True
return c4d.gui.GeDialog.Message(self, msg, result)
return True
if __name__ == "__main__":
dlg = MyDialog()
dlg.Open(dlgtype=c4d.DLG_TYPE_ASYNC, pluginid=20304050)