Page 1 of 1

Shapes, Mouse Events, Ruby...

PostPosted: Tue Nov 16, 2021 6:44 am
by aefa
Hello everyone!
I've been browsing the forum but to no avail. If there is a tread, please point me to it?
I not very good a programming, but I was wandering if there is a way, through ruby, to be able to click on the display window and draw a rectangle (or any other shape) that can receive mouse events. For example: dragging and creating a rectangle (multiple times) with certain settings, then being able to click it and retrieve those settings?

Thank you for any replay! :D

Re: Shapes, Mouse Events, Ruby...

PostPosted: Tue Nov 16, 2021 11:17 am
by tulamide
That's difficult, but not impossible. How good are you at programming in Ruby? You would need to make use of the method isInMousePoint, by setting up exactly the areas that were drawn before. You also need to differentiate between a simple mouse click and click'n'drag to be able to draw a rectangle, without that being interpreted as a click on another rectangle.

The logic would be to draw a rectangle and fill a hash or array with its boundaries. The hash would then be used in isInMousePoint to check for valid clicks on existing rectangles or the drawing of a new one.

Re: Shapes, Mouse Events, Ruby...

PostPosted: Tue Nov 16, 2021 11:44 am
by aefa
Thank your for the response tulamide! I read on the forum you're the guru when it comes to ruby! :D
I've been experimenting with some codes I've found on arpeggiators and I have a decent (I hope) idea on how to handle mouse events. To be more specific. I wanted to make something similar to the piano roll in a DAW but the codes I've found are more on the side of arpeggiators and the note rectangle is locked in the step and if I try to change the step, the whole thing resizes.
If I understood properly: I have to save the boundaries into an array right? Then my complication would be on how to make sure I'm getting the right boundary when clicking a rectangle right? I'll try it and see If my little skills are sufficient! Thanks again! I'll post again if any or no result.

Re: Shapes, Mouse Events, Ruby...

PostPosted: Tue Nov 16, 2021 12:28 pm
by aefa
I have to say, I tried to create a class that would contain the area and the mouse events plus other data. I could instance the class but it couldn't draw the rectangle because it had no access to the viewport. It gave an error when calling the v.createRectangle

Re: Shapes, Mouse Events, Ruby...

PostPosted: Tue Nov 16, 2021 2:13 pm
by tulamide
aefa wrote:I wanted to make something similar to the piano roll in a DAW

For that you don't really need all the complex isInMousePoint checks. Goes to show, how important it is, to ask precisely matching to your real issue ;)

Having a visual piano keyboard and being able to scroll it, but still referencing the correct midi key, is that the task?
If so, there have indeed been examples already, that might help you on the way?

http://www.dsprobotics.com/support/viewtopic.php?f=3&t=12519

aefa wrote:I have to say, I tried to create a class that would contain the area and the mouse events plus other data. I could instance the class but it couldn't draw the rectangle because it had no access to the viewport. It gave an error when calling the v.createRectangle

Of course. v is a local variable. Just provide your class's draw method with the view. For example,
Code: Select all
def my_draw_method(view)
  view.drawRectangle [...]
end

Then in the RubyEdit's draw method call yours, with whatever variable you defined, for example
Code: Select all
def draw(v)
  myClass.my_draw_method(v)
end

Re: Shapes, Mouse Events, Ruby...

PostPosted: Tue Nov 16, 2021 2:20 pm
by aefa
My apologies! I didn't mean a piano roll as in the piano keys. Got the terminology mixed. I meant the grid where the notes are drawn!

Re: Shapes, Mouse Events, Ruby...

PostPosted: Tue Nov 16, 2021 2:22 pm
by aefa
Thanks again tulamide! that line of code to reference the viewport is something that I was missing completely!

Re: Shapes, Mouse Events, Ruby...

PostPosted: Sat Nov 20, 2021 12:50 pm
by aefa
Hello Everyone!
tulamide!

I've got some progress in the project. Still I ended up doing the mouse events through schematics but so far (after coughing blood couple of time... :? ) is working properly. I want to get the region detection working and then I'll post the project for review :)

Have a nice day!

p.s.:
My regards to Spogg who has helped me a lot in the past, and everyone that always takes their time to help noobs like me!

Re: Shapes, Mouse Events, Ruby...

PostPosted: Sat Nov 20, 2021 1:29 pm
by Spogg
aefa wrote:My regards to Spogg who has helped me a lot in the past, and everyone that always takes their time to help noobs like me!


Thank you!
I think we all like to help each other out whether noobs or not. You got the right guy on the case!

Re: Shapes, Mouse Events, Ruby...

PostPosted: Sat Nov 20, 2021 9:33 pm
by aefa
OK. So far...
Click to add. Click to delete. Drag to extend/add. Play with steps.

No other functionality added yet (I have to learn a lot more to manage them :D )
Please let me know if it is on the right track!