I have a project where I can select different items that are images that then become draggable objects on the canvas. All is good with that. Now what I want to do is add the option to draw a line, maybe like line object in the IDE, then have the line become an image for a draggable object.
Ideally I would like to select the draw line option and then be able to draw it vertically or horizontally or diagonally at the length need. Then have it become a draggable object. It won’t need to be edited, that is be able to adjust the length once set.
Assuming you have a tool palette where the user would click a button to begin their line drawing:
Store the coordinates of the location of MouseDown as the start (dragStart as Xojo.Point, for example) and set a flag to denote the current operation (self.isDragging = True)
Store the coordinates of the location of the cursor and refresh in MouseDrag (dragCurrent as Xojo.Point, for example)
Draw a line from the first set of coordinates to the second set of coordinates in the Paint event
if isDragging then
g.DrawLine(dragStart.X, dragStart.Y, dragCurrent.X, dragCurrent.Y)
end if
On MouseUp, create your new virtual Line object representing the sets of coordinates
if isDragging and currentTool = Tools.Line then
myNewObject = new CanvasLineObject(dragStart, dragCurrent)
allCanvasObjects.Add(myNewObject)
end if
This is all pseudocode to give you an idea of how to implement it. You won’t be able to just copy/paste.
Ok so I have it drawing the line, creating the line picture and then adding to my canvas objects. Right now I have the picture width and height are set to the canvas width and height. Which is ok, but that make it rather large. How do I crop the picture to the line or create the picture so it fits with the line dimensions ?
Been meaning to get back t this but some things came up and got side tracked.
If you want to cache an image of the line for drawing later, you’d simply create a picture using the (X,Y) coordinate pairs. You’d then store the relative (X,Y) position of the line on the stage and draw the cached picture at those coordinates when needed.
The black lines are around the image, I’m use that to indicate when object is selected.
Now what I want it to do is in the next pic ,(note its been edited in a graphics app ) the green outline is what I want the actual image/object size to be, not the black lines.