Glitchy Dragging an object - codenameone

I have been working on a simple drag feature for my project. I have a touch form which handles all event inputs. My drag implementation works fine, until I move my mouse rapidly. I have heard of getDragRegionStatus() which gets the sensitivity of the event (i could be reading the API wrong). Would this be an option for me? Example
#Override
public void pointerDragged(int[] x, int[] y) {
if(RectController.top.contains(x[0], y[0])) {
PersonalizedData.touchTopX = x[0];
PersonalizedData.touchTopY = y[0];
}
else if(RectController.bottom.contains(x[0], y[0])) {
PersonalizedData.touchTopX = x[0];
PersonalizedData.touchTopY = y[0];
}
if(x.length >= 2 && y.length >= 2 && RectController.top.contains(x[1], y[1])) {
PersonalizedData.touchTopX = x[1];
PersonalizedData.touchTopY = y[1];
}
else if(x.length >= 2 && y.length >= 2 && RectController.bottom.contains(x[1], y[1])) {
PersonalizedData.touchTopX = x[1];
PersonalizedData.touchTopY = y[1];
}
}
I am overriding the pointerDragged with array parameters for multiple touch support. In the end, I will have a top and bottom box, which can be dragged concurrently.
Is there a better way to handle drag events than this? Thanks in advance!

A better approach would be to "select" the currently dragged component in pointerPressed(). Then in pointerDragged, you don't do this check. You just assume that the item that was selected in pointerPressed is the thing you're dragging.
In pointerReleased, you "unselect" the dragged component.

Related

Mouse over on a p5.js class only working on first instance of the class

I have a custom class in p5js that defines an "ImageButton".
This is being used in React through react-p5-wrapper but I don't think that is related to this issue.
I have several different instances of this ImageButton running, and I have a function inside the class to detect if the mouse is over the button. If it detects that it is over, it should change the image to a different one (basically different color icons), and it should also change the mouse cursor to a pointer.
I know the detection works correctly, because all instances change to the "hover" image.
The problem is the mouse cursor only changes to pointer on the first instance of the class, and remains as default everywhere else. Couldn't find any resources on this issue, so it's probably something wrong with my code, but haven't been able to figure it out, so here it is:
export default class ImageButton {
constructor(p5, x, y, img, hoverImg) {
this.p5 = p5;
this.x = x;
this.y = y;
this.img = img;
this.hoverImg = hoverImg;
this.rad = 16;
}
display() {
if (this.over()) {
this.p5.cursor('pointer');
this.p5.image(this.hoverImg, this.x, this.y, this.rad, this.rad);
} else {
this.p5.cursor('default');
this.p5.image(this.img, this.x, this.y, this.rad, this.rad);
}
}
over() {
if (
this.p5.mouseX > this.x &&
this.p5.mouseX < this.x + this.rad &&
this.p5.mouseY > this.y &&
this.p5.mouseY < this.y + this.rad
) {
return true;
} else {
return false;
}
}
}
And here is a small quick video showing the issue: video
Any ideas?
Searched the web for similar problems, couldn't find anything helpful.
I assume you call the 'display()' function for each instance of ImageButton class. In the 'display()' function you are using a simple if statement to check if the element is hovered, which sets the cursor to 'default' when it's not being hovered. It means that only last element that is being checked is able to set the cursor to 'pointer', because every element that is currently not hovered is setting the cursor back to 'default'.
The first solution that comes to my mind is to set the return type of the 'display()' function to boolean and then using it in the main loop function to handle the cursor changing. If any element returned 'true' then the cursor should be a pointer, otherwise set it to default.
You should also consider simplifying the 'over()' function:
over()
{
return
this.p5.mouseX > this.x &&
this.p5.mouseX < this.x + this.rad &&
this.p5.mouseY > this.y &&
this.p5.mouseY < this.y + this.rad;
}

How to detect a 'pinch out' in a list of containers?

I want to be able to pinch two containers in a list of containers away from each other to insert a new empty container between them. Similar to how the iPhone app “Clear” inserts new tasks (see for example the very first picture on this page https://www.raywenderlich.com/22174/how-to-make-a-gesture-driven-to-do-list-app-part-33 - the small red container is inserted when the two sorounding containers are pinched away from each other). Any hints on how I can achieve this in Codename One?
Normally you would override the pinch method to implement pinch to zoom or similar calls. However, this won't work in this case as the pinch will exceed component boundaries and it wouldn't work.
The only way I can think of doing this is to override the pointerDragged(int[],int[]) method in Form and detect the pinch motion as growing to implement this. You can check out the code for pinch in Component.java as it should be a good base for this:
public void pointerDragged(int[] x, int[] y) {
if (x.length > 1) {
double currentDis = distance(x, y);
// prevent division by 0
if (pinchDistance <= 0) {
pinchDistance = currentDis;
}
double scale = currentDis / pinchDistance;
if (pinch((float)scale)) {
return;
}
}
pointerDragged(x[0], y[0]);
}
private double distance(int[] x, int[] y) {
int disx = x[0] - x[1];
int disy = y[0] - y[1];
return Math.sqrt(disx * disx + disy * disy);
}
Adding the entry is simple, just place a blank component in the place and grow its preferred size until it reaches the desired size.

Mouse Over event for corners of screen wpf windows

As the title suggests, I want to get a mouse over event at the corners of desktop. I tried to do it by getting the position of the mouse and polling it continuously to check if it is one of the edge coordinated and then trigger the event but I think thats bad way to do it. I tried to use the library MouseKeyHook but it wasn't useful for me to generate the necessary event.
Is there any better way to get mouse over events to the corners of the screen ?
I tried to improve the code by adding the following, but for some reason the mouse over to the corners isn't triggering the action.
public async void callMainLoop() {
var slowTask = Task<Boolean>.Factory.StartNew(() => mainMethodLoop(GetMousePosition()));
await slowTask;
}
public bool mainMethodLoop(Point point) {
while (true) {
double xRes = System.Windows.SystemParameters.PrimaryScreenWidth - 1;
double yRes = System.Windows.SystemParameters.PrimaryScreenHeight - 1;
if (point.X == 0 && point.Y == 0) {
if (comboBox0.SelectedItem != null) {
executeAction(comboBox0.SelectedItem.ToString());
return true;
}
}
return false;
}
}
I am not calling this in the constructor, there ins't any freezing now.

ToolTip in ZedGraph refreshes continuously and uses a significant amount of CPU

I am using ZedGraph to show Japanese candles. I set the GraphPane.isShowPointValue=true, but when I move my mouse on the candle, the tooltip is refreshing and refreshing.
I find that when the tooltip is shown, it always takes more than 50% CPU time.
How can I solve this?
At this point there is a newer version of ZedGraph that fixes this problem.
currently v5.1.7
https://www.nuget.org/packages/ZedGraph/
I had the same issue with application developed several years ago for Win XP when users started migration to Win 7.
The above mentioned path did not help me, so I wrote quick and dirty workaround:
double prevMouseX = 0; // for storing previos cursor position
double prevMouseY = 0; //
private bool ZedGraphControl1MouseMoveEvent(ZedGraphControl sender, MouseEventArgs e)
{
PointF mousePt = new PointF( e.X, e.Y );
GraphPane pane = sender.MasterPane.FindChartRect( mousePt );
if ( pane != null )
{
double x, y;
pane.ReverseTransform( mousePt, out x, out y );
if ((x == prevMouseX) && (y == prevMouseY))
{
// Do nothing if the mouse position didn't change
return false;
}
else {
prevMouseX = x;
prevMouseY = y;
}
// Our code for toolTip goes here
...
Take a look into this, the patch described in this link might resolve the issue:
http://sourceforge.net/tracker/?func=detail&aid=3061209&group_id=114675&atid=669144

How can I check whether a location is within a MapPolygon in the Silverlight Bing Maps control?

I have a MapPolygon which covers a certain area on the Silverlight Bing Maps control,
and I would like to know if a particular Location is located within this MapPolygon.
I have tried the following code which doesen't return the result I want because it only checks if the tested location is one of the vertices of the MapPolygon, and doesn't check if this Location is contained within this MapPolygon.
polygon.Locations.Contains(new Location(this.Site.Latitude, this.Site.Longitude, this.Site.Altitude));
Is it also possible to determine if two MapPolygons intersect one another?
The polygon.Locations is a list of points defining the polygon.
You have to make a method to find if your point is inside the polygon.
Use something like this (not tested if compiles):
static bool PointInPolygon(LocationCollection polyPoints, Location point)
{
if (polyPoints.Length < 3)
{
return false;
}
bool inside = false;
Location p1, p2;
//iterate each side of the polygon
Location oldPoint = polyPoints[polyPoints.Count - 1];
foreach(Location newPoint in polyPoints)
{
//order points so p1.lat <= p2.lat;
if (newPoint.Latitude > oldPoint.Latitude)
{
p1 = oldPoint;
p2 = newPoint;
}
else
{
p1 = newPoint;
p2 = oldPoint;
}
//test if the line is crossed and if so invert the inside flag.
if ((newPoint.Latitude < point.Latitude) == (point.Latitude <= oldPoint.Latitude)
&& (point.Longitude - p1.Longitude) * (p2.Latitude - p1.Latitude)
< (p2.Longitude - p1.Longitude) * (point.Latitude - p1.Latitude))
{
inside = !inside;
}
oldPoint = newPoint;
}
return inside;
}
And call it like this:
if (PointInPolygon(polygon.Locations, new Location(this.Site.Latitude, this.Site.Longitude, this.Site.Altitude)))
{
//do something
}
Sure both of these things are fairly trivial, take a look at the following article. http://msdn.microsoft.com/en-us/library/cc451895.aspx It gives good methods for Bounding Box, Radius, and Polygon Search. Particularity take note of the pointInPolygon method.

Resources