RichCanvas icon indicating copy to clipboard operation
RichCanvas copied to clipboard

Line x2-y2

Open batuhancengiz opened this issue 11 months ago • 6 comments

Hello,

I want to find x2-y2 position of line as point and with left + width and top + height couldn't get the correct x2-y2 values. Any ideas to find that?

batuhancengiz avatar Mar 27 '24 10:03 batuhancengiz

image

Actually I want to achieve that when I drag the switch element, line should also be dragged according to base element. But top right corner of line should be fixed all the time. Can you explain how can I do this please?

batuhancengiz avatar Mar 27 '24 10:03 batuhancengiz

Hi,

So, first thing, I recommend you read this page from the Wiki ItemContainer Overview, also there's a link pointing to how I implemented something similar to what you've described inside my demo, with Adorners (Adorner Usage), maybe you can clone/download the repo and play around with the demo. Is not fully working, but for a Line drawn with Scale=(1,1) (normally) the resize works as you described. Making it work with another ScaleTransform requires a bit more calculations.

In the first comment, you said that calculating (X2, Y2) coordinates you couldn't get the correct values. In general, when calculating that, you should have in mind 2 things: the BoundingRectangle described in the Wiki (linked that above); and the ScaleTransform value or the "Scaling". And go on from here as (Top, Left) point might not be where you expect and you might need to add or subtract from Width or Height. Basically, you need to know where the "Switch element" is positioned relative to the (Top, Left) of the BoundingRectangle of the Line and then you can easily get the correct (X2, Y2) values.

I hope this can help you getting your correct value and the desired behavior. I don't know the details of implementation about that "Switch element", so that's what I can say so far. It's quite tricky working with Transformations and calculating stuff, I know😅, take a look at the ResizeAdorner.cs class inside RichCanvasDemo and especially at the methods ArrangeOverride() and GetDesiredTransform().

mircea21S avatar Mar 28 '24 14:03 mircea21S

Thank you for your answer! As you know resizing with adorner just possible two aches for example this picture. image

But I need that line resizing 360 degree. Is this possible also to resize around 360 degree in your implementation or not?

batuhancengiz avatar Mar 28 '24 14:03 batuhancengiz

In my implementation resizing 360 degree is not possible, but it can definitely be implemented. You can debug and try to find a solution and understand how it's working now and how you can achieve it for other ScalTransform values.

mircea21S avatar Mar 29 '24 09:03 mircea21S

Thank you for your message, I read and I guess, I understand the logic of adorner/drawing classes and tried something like that:

internal void OnMouseMove(Point delta)
        {
            foreach (var index in ParentCurrentItem.ChildrenIndexes)
            {
                var container = (RichItemContainer)_context.ItemContainerGenerator.ContainerFromIndex(index);
                ScaleTransform scaleTransform = container.ScaleTransform;

                container.Left += delta.X;
                container.Top += delta.Y;

                double width = container.X2 - container.Left;
                double height = container.Y2 - container.Top;

                container.Width = width == 0 ? 1 : Math.Abs(width);
                container.Height = height == 0 ? 1 : Math.Abs(height);

                if (scaleTransform != null)
                {
                    if (width < 0 && scaleTransform.ScaleX == 1)
                    {
                        scaleTransform.ScaleX = -1;
                    }

                    if (height < 0 && scaleTransform.ScaleY == 1)
                    {
                        scaleTransform.ScaleY = -1;
                    }

                    if (height > 0 && scaleTransform.ScaleY == -1)
                    {
                        scaleTransform.ScaleY = 1;
                    }
                    if (width > 0 && scaleTransform.ScaleX == -1)
                    {
                        scaleTransform.ScaleX = 1;
                    }
                }
            }
        }

2024-03-29-15-13-40

X2-Y2 of container is actually x2-y2 position of line. I set this according to Mouse Position when draw is ended. But still it doesn't move the way as I want to, so do you have any idea to solve this?

batuhancengiz avatar Mar 29 '24 12:03 batuhancengiz

Hi!

I will try to give you a piece of code that works when I find some time to do this. The delta Top, Left position needs to be set relative to the ScaleTransform, also the width and height which are needed for X2,Y2 (some thoughts just from a quick look).

I'm thinking of adding this as a feature to the library in the future. It is probably doable.

Sorry for not being able to come with a response now. I will try to come back asap.

Thanks!

mircea21S avatar Apr 09 '24 17:04 mircea21S