Implementing clicking is really getting into some juicy programming and I find it immensely satisfying.
This is a really nice simple way to implement clicking with a mouse callback, and if this works for you, use it, mark it as the solution, and ignore my ramblings below.
But … What if multiple icons overlap? Or you need to deal with complex perspectives? What if you need the clicks to be very precise, and transparent areas of the icons not clickable? Then you’ll have to render your viewport as a bitmap and index the colours of each item.
This code will make all of the non-transparent pixels of each bitmap a flat colour, unique from every other bitmap.
One issue with the below is that its VERY slow in python. Normally I Lock the bits, but this is causing me issues currently.
## Colorise the bitmap for each alert icon
i = 1.0
for bitmap in bitmaps:
i+=1.0
col = System.Drawing.Color.FromArgb(i/255, i % 255, 255)
for x in range(0, bitmap.Width):
for y in range(0, bitmap.Height):
if bitmap.GetPixel(x, y).A > 0:
bitmap.SetPixel(x, y, col)
This code returns a flat bitmap of all the unique coloured bitmaps (Called inside of DrawForeground
.)
### Get a bitmap of the viewport we clicked in
def DrawForeground(self, sender, arg):
...
selection_bitmap = arg.Viewport.ParentView.CaptureToBitmap(False, False, False);
This code gets the index that was clicked using MouseCallback
## Retrieve the selected Index
px_colour = selection_bitmap.GetPixel(click.X, click.Y)
index = px_colour.R * 255 + px_colour.G
clicked = bitmaps[index]
– cs