Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

link far end aligns with the mouse pointer position #469

Merged
merged 4 commits into from
Nov 19, 2019

Conversation

antonioru
Copy link
Contributor

@antonioru antonioru commented Nov 17, 2019

Checklist

  • The code has been run through pretty yarn run pretty
  • The tests pass on CircleCI
  • You have referenced the issue(s) or other PR(s) this fixes/relates-to
  • The PR Template has been filled out (see below)
  • Had a beer/coffee because you are awesome

What?

The DragNewLinkState.fireMouseMoved method was not taking into account the click starting position when dragging a new link, it just assumes it's the same point (x,y) of the link port. Sometimes, especially with big ports, it causes a slight mismatch between the link far end point and the mouse position as reported in the issue #467

How?

I saved the mouse starting position within the DragNewLinkState class itself.

Feel good image:

(Add your own one below :])

LOL

@antonioru antonioru changed the title link far end alignes with the mouse pointer position, fixes #467 link far end alignes with the mouse pointer position Nov 17, 2019
@antonioru antonioru changed the title link far end alignes with the mouse pointer position link far end aligns with the mouse pointer position Nov 17, 2019
@antonioru
Copy link
Contributor Author

antonioru commented Nov 18, 2019

@dylanvorster running yarn run pretty will "prettify" some files not related to the Pull Request, I can revert em to the previous state so the PR is clean but I'd wait your feedback.
cheers

@@ -56,6 +60,9 @@ export class DragNewLinkState extends AbstractDisplacementState<DiagramEngine> {
this.link.setSourcePort(this.port);
this.engine.getModel().addLink(this.link);
this.port.reportPosition();

// save the mouse position for further precision in calculating the link's far-end point
this.startingPoint = new Point(event.event.clientX, event.event.clientY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AbstractDisplacementState already contains this data in the abstract class :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AbstractDisplacementState already contains this data in the abstract class :)

that's brilliant!
I'm going to update the PR, thank you for the tip ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dylanvorster MR updated! ;)

@mathiasbaldissera
Copy link
Contributor

I think it's not working 100%.
I Updated my personal State with your calcs and this became the result
React App - Chromium_003

@antonioru
Copy link
Contributor Author

antonioru commented Nov 19, 2019

I think it's not working 100%.
I Updated my personal State with your calcs and this became the result
React App - Chromium_003

Thank you mathiasbaldissera for checking it out, lately I've updated the code by using the internal variables (initialX, initialY), so the method looks like this now:

    fireMouseMoved(event: AbstractDisplacementStateEvent): any {
		const pos = this.port.getPosition();
		const linkNextPosX = pos.x + (this.initialX - pos.x) + event.virtualDisplacementX;
		const linkNextPosY = pos.y + (this.initialY - pos.y) + event.virtualDisplacementY;

		this.link.getLastPoint().setPosition(linkNextPosX, linkNextPosY);
		this.engine.repaintCanvas();
	}

can you please confirm this is not working for you?

@antonioru
Copy link
Contributor Author

antonioru commented Nov 19, 2019

Ok I've better looked into it (and I apologise I haven't done it before) @mathiasbaldissera is right, apparently it works most of the time:

Good-one

but sometimes it doesn't:

Not-Good

I can't really understand why and I'll try to figure out as soon as possible...

any suggestion @dylanvorster ?

@mathiasbaldissera
Copy link
Contributor

mathiasbaldissera commented Nov 19, 2019

@antonioru Using the initialXRelative and initialYRelative less the canvas offset worked to me

		const pos = this.port.getPosition()
		const linkNextPosX =
			pos.x + (this.initialXRelative - this.engine.getModel().getOffsetX() - pos.x) + event.virtualDisplacementX
		const linkNextPosY =
			pos.y + (this.initialYRelative - this.engine.getModel().getOffsetY() - pos.y) + event.virtualDisplacementY

		this.link.getLastPoint().setPosition(linkNextPosX, linkNextPosY)

@antonioru
Copy link
Contributor Author

const pos = this.port.getPosition()
		const linkNextPosX =
			pos.x + (this.initialXRelative - this.engine.getModel().getOffsetX() - pos.x) + event.virtualDisplacementX
		const linkNextPosY =
			pos.y + (this.initialYRelative - this.engine.getModel().getOffsetY() - pos.y) + event.virtualDisplacementY

		this.link.getLastPoint().setPosition(linkNextPosX, linkNextPosY)

It doesn't work for me, it doesn't show the link at all

@antonioru
Copy link
Contributor Author

antonioru commented Nov 19, 2019

@mathiasbaldissera @dylanvorster

Ok, I've updated the PR by using the relative coordinates (initialXRelative, initialYRelative) instead of the absolute ones (initialX, initialY) and it works for be every time now.

Please Mathias let me know if you find any other problem.

The code I've updated is the following:

	/**
	 * Calculates the link's far-end point position on mouse move.
	 * In order to be as precise as possible the mouse initialXRelative & initialYRelative are taken into account.
	 */
	fireMouseMoved(event: AbstractDisplacementStateEvent): any {
		const pos = this.port.getPosition();
		const linkNextPosX = pos.x + (this.initialXRelative - pos.x) + event.virtualDisplacementX;
		const linkNextPosY = pos.y + (this.initialYRelative - pos.y) + event.virtualDisplacementY;

		this.link.getLastPoint().setPosition(linkNextPosX, linkNextPosY);
		this.engine.repaintCanvas();
	}

I hope it helps :)

Thank you guys, you've been really helpful
I sincerely hope to possibly improve the library

@mathiasbaldissera
Copy link
Contributor

mathiasbaldissera commented Nov 19, 2019

I hope it helps :)

If u use only the relative coordinates, and move the canvas (generating an offsetX and offsetY to the canvas) it'll bug, did u try it?

@antonioru
Copy link
Contributor Author

let me k

If u use only the relative coordinates, and move the canvas (generating an offsetX and offsetY to the canvas) it'll bug, did u try it?

I see your point, so the engine offset must be taken into account as well ...
let me dig a bit more into it... I really can't understand why using the engine offset screws the whole thing in demos :\

@antonioru
Copy link
Contributor Author

@mathiasbaldissera

this seems to finally work even when you move the canvas :)

	/**
	 * Calculates the link's far-end point position on mouse move.
	 * In order to be as precise as possible the mouse initialXRelative & initialYRelative are taken into account.
	 */
	fireMouseMoved(event: AbstractDisplacementStateEvent): any {
		const pos = this.port.getPosition();
		const engineOffsetX = this.engine.getModel().getOffsetX();
		const engineOffsetY = this.engine.getModel().getOffsetY();
		const linkNextPosX = pos.x - engineOffsetX + (this.initialXRelative - pos.x) + event.virtualDisplacementX;
		const linkNextPosY = pos.y - engineOffsetY + (this.initialYRelative - pos.y) + event.virtualDisplacementY;

		this.link.getLastPoint().setPosition(linkNextPosX, linkNextPosY);
		this.engine.repaintCanvas();
	}

@mathiasbaldissera
Copy link
Contributor

@mathiasbaldissera

this seems to finally work even when you move the canvas :)

	/**
	 * Calculates the link's far-end point position on mouse move.
	 * In order to be as precise as possible the mouse initialXRelative & initialYRelative are taken into account.
	 */
	fireMouseMoved(event: AbstractDisplacementStateEvent): any {
		const pos = this.port.getPosition();
		const engineOffsetX = this.engine.getModel().getOffsetX();
		const engineOffsetY = this.engine.getModel().getOffsetY();
		const linkNextPosX = pos.x - engineOffsetX + (this.initialXRelative - pos.x) + event.virtualDisplacementX;
		const linkNextPosY = pos.y - engineOffsetY + (this.initialYRelative - pos.y) + event.virtualDisplacementY;

		this.link.getLastPoint().setPosition(linkNextPosX, linkNextPosY);
		this.engine.repaintCanvas();
	}

I'll try it at the afternoon (now i'ts 11:12 here :v)

@antonioru
Copy link
Contributor Author

hehe, take your time mate, I'll update the PR in the meanwhile :)
(( It's 14:14 here, greetings from London :))

@mathiasbaldissera
Copy link
Contributor

mathiasbaldissera commented Nov 19, 2019

@antonioru 1 question. How did you exactly run the demos? Every time I try to run i got erros, and I need to test the changes into my own projects (I didn't find you on Facebook to ask this question hehehe :v)

@antonioru
Copy link
Contributor Author

@antonioru 1 question. How did you exactly run the demos? Every time I try to run i got erros, and I need to test the changes into my own projects (I didn't find you on Facebook to ask this question hehehe :v)

  1. I git cloned the repository locally on my computer
  2. then cd react-diagrams (or the name you gave to your local repository)
  3. npm install or yarn install
  4. then cd packages/diagrams-demo-gallery
  5. again npm install or yarn install
  6. npm start or yarn start

i think it's all reported into the doc somewhere

@mathiasbaldissera
Copy link
Contributor

mathiasbaldissera commented Nov 19, 2019

@antonioru 1 question. How did you exactly run the demos? Every time I try to run i got erros, and I need to test the changes into my own projects (I didn't find you on Facebook to ask this question hehehe :v)

  1. I git cloned the repository locally on my computer
  2. then cd react-diagrams (or the name you gave to your local repository)
  3. npm install or yarn install
  4. then cd packages/diagrams-demo-gallery
  5. again npm install or yarn install
  6. npm start or yarn start

i think it's all reported into the doc somewhere

I really forgot to run the yarn install in the demo-gallery 🤦‍♂️ thx

@mathiasbaldissera
Copy link
Contributor

hehe, take your time mate, I'll update the PR in the meanwhile :)
(( It's 14:14 here, greetings from London :))

It work perfectly now ;)
((now 13:56, greetings from Brazil 😄 )

@antonioru
Copy link
Contributor Author

So it just needs to be merged :))

Thank you @mathiasbaldissera, you really helped me out

@dylanvorster

@dylanvorster
Copy link
Member

what a journey xD

@dylanvorster dylanvorster merged commit a450c9b into projectstorm:master Nov 19, 2019
@mathiasbaldissera mathiasbaldissera mentioned this pull request Nov 20, 2019
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants