Announcement
Collapse
No announcement yet.
use mouse draw line in the 2D,how to do?
Collapse
X
-
Here's my take on it, see if it helps:
I created an array to store the coordinates of where in the widget the mouse clicks: Points
I've overridden OnMouseButtonDown and add a 2d coordinate to the aforementioned Points array whenever the user clicks with the LeftMouseButton only.
And here's the OnPaint:
DrawLines handles all the lines for the generated points and an additional DrawLine draws a line from the last element of the Points array to the mouse position on the screen.
If you want to detect clicks on the entire canvas, set its Visibility to Visible. Here I've placed a border which is the only Visible element so the clicks outside do not register.
This is by no means perfect by should get you started, good luck!Attached Files
- 3 likes
Comment
-
The easiest way would be to store the Points arrays in another Array.
You probably do not want to use OnMouseButtonUp here - it will interrupt your drawing every time you click. Perhaps you could add a check for the RightMouseButton in the OnMouseButtonDown. Every time the user pressed RMB, add Points array to another array - the OnPaint can draw the whole thing in a loop.
Comment
-
It might be only me but the service you use for hosting those video asks me to install additional software and it's not in English so I can either not watch it or risk installing something dodgy. It works on my phone, though. If you make these examples easier to access, chances are you'll attract more attention.
The solution with the nested array I posted above should work fine for drawing multiple lines. I saw you post multiple questions, I'll just answer some of them here:
- To loop through an array of lines containing points:
- To display text, you'll need to create widgets, something very simple will do:
Set the text_label's isVariable to True (upper-right corner of the details panel) so you can access it later and set its text (distance)
text_label is centred horizontally and vertically, the rest is on default
- To add distance labels, create a widget every time the user clicks:
The widgets that have already been placed are stored in the DistWidgets array
The widget that is still being dragged around has its own variable DistWidget, and is added to the DistWidgets array as soon as the point has been placed; a new widget is created for the next line fragment
- To find coordinates, you'll need a bit of math:
Position between two Points is (A+B)/2
Distance is length of (A-B)
Those two values are sent to the Widget
- To make a straight line, constrain either X or Y of the mouse input
- 2 likes
Comment
- To loop through an array of lines containing points:
-
I think that HUD's class allows for tinkering with line's size.
How to paint separate line - can't remember this setup well but I'd create an array of points arrays. Add new elements once you've finished with your line.
As in, contain a set of points which makes a line within an outer array that contains the lines. Rinse & repeat.
Comment
Comment