MOUSEDOWN and selection

Hi! I’d like to detect selected nodes on MOUSEDOWN, but it seems like the selecting happens after the callback, doesn’t it?
Here’s what happens:

  1. Nothing is selected;
  2. I click on a node selecting it and expect the callback to recognize it, but it doesn’t;
  3. I click away, nothing is selected, but the callback now prints the previously selected node.

What’s the correct way to achieve it? Tried def mouseDown_ in the select plugin, but it then doesn’t select individual nodes at all.

What are you trying to do?

Trying to do some actions to selected nodes when they are moved. Ideally, just click-and-drag a node should run the code, but MOUSEDOWN doesn’t recognize the node as selected. So it requires the first click to get selection, and then second click and drag to run the code, which doesn’t seem right.

I don’t know if there’s a better way to do it, but now I’m getting selection with MOUSEDOWN (for a single node) and MOUSEUP (for marquee box selection) and run actions with DRAWFOREGROUND.
(also it needs to calculate some stuff at the moment of selection, that’s why its separated from drawforeground)

So you need a notification if selection changes and if nodes are moved? Can you give me a hint what exactly you are trying to do?

Not quite. Here’s a little screengrab: if you first select the node and then drag it, the other nodes adjust accordingly (it gets selection and calculates the relation to other nodes on mousedown). But if you just click+drag in one click, it the MOUSEDOWN doesn’t recognize the node as selected and doesn’t run the code:


Maybe you can register for MOUSEDRAGGED?
How do you switch this mode on or off? Maybe make your own tool. Then you also can respond to the keyboard.

Ooh, just got what you meant before! Yes, is it possible to subscribe to changes in selection and if nodes are moved? I guess it would be better than mousedown/dragged!

I’ll prepare a better example. Ping me if I haven’t posted something in the next few days.

1 Like

Hi Georg, any chance you have time for an example for this?

Any chance? Have a couple of scripts in process relying on this principle, but can’t seem to figure it out on my own or find any examples

Sorry for not answering earlier. Unfortunately I don’t remember what I had in mind.

But to repeat my question: How do you plan to en/disable this functionality? It seems to me that this should be a tool in the toolbar (next to the select tool).

Hi, thanks for getting back to this!
Yes, I have it as a toolbar tool, but unfortunately it doesn’t make a difference. Or i don’t see how it should.

So basically, it needs some callback for changes in selection, and MOUSEUP / DOWN don’t always work for that: I’d expect that if you click on a node selecting it, you’d immediately get that node as selection from MOUSEDOWN, but it doesn’t get it, if that makes sense.

Is KVO the only way to go in that case?

If you have a tool, you don’t need all that callback and KVO stuff. Just overwrite ´mouseDragged_()and do all your processing there (probably callsuper` first). I’ll try to add that to the templates.

1 Like

Ohh now it makes sense, thank you!
Is the code from mouseDragged_ of the original selection tool available somewhere? I could probably keep a good part of it, but can’t find it anywhere.

You should call the implementation from the parent class like this:

objc.super(MYClassName, self).mouseDragged_(sender)

(Replace the MYClassName with the name of you class)

Edit: I just added some sample code in the selection tool template.

1 Like

Hi Georg! Is there a way to add those mouse callbacks from a Palette plugin? Using the line above doesn’t seem to do anything. Or is there a better way to run code from Palette plugin whenever the current layer has changed?

I tried using DRAWFOREGROUND and checking glyph.lastChange, but it includes changes in selection, which makes the code override any further user action.