I am working on a program for displaying and playing the popular D & D: D board game. Now I am working on getting basic functions such as dragging and dropping user interface elements, snapping to the grid and checking for conflicts.
Right now, every object, when it is discarded with the mouse, instantly snaps to the nearest grid point. This causes a problem when something like a playerβs object snaps to a grid point that has a wall or another. Thus, in essence, when a player falls, they finish the part of the wall covering them. This works fine and works as intended, but the problem is that now my collision detection will work whenever you try to move this player, because he is sitting under the wall, and because of this you can no longer drag the player.
Here is the relevant code:
void UIObj_MouseMove(object sender, MouseEventArgs e)
{
blocked = false;
if (dragging)
{
foreach (UIElement o in ((Floor)Parent).Children)
{
if (o.GetType() != GetType() && o.GetType().BaseType == typeof(UIObj) &&
Math.Sqrt(Math.Pow(((UIObj)o).cX - cX, 2) + Math.Pow(((UIObj)o).cY - cY, 2)) <
Math.Max(r.Height + ((UIObj)o).r.Height, r.Width + ((UIObj)o).r.Width))
{
double Y = e.GetPosition((Floor)Parent).Y;
double X = e.GetPosition((Floor)Parent).X;
Geometry newRect = new RectangleGeometry(new Rect(Margin.Left + (X - prevX),
Margin.Top + (Y - prevY), Margin.Right + (X - prevX), Margin.Bottom + (Y - prevY)));
GeometryHitTestParameters ghtp = new GeometryHitTestParameters(newRect);
VisualTreeHelper.HitTest(o, null, new HitTestResultCallback(MyHitTestResultCallback), ghtp);
}
}
if (!blocked)
{
Margin = new Thickness(Margin.Left + (e.GetPosition((Floor)Parent).X - prevX),
Margin.Top + (e.GetPosition((Floor)Parent).Y - prevY),
Margin.Right + (e.GetPosition((Floor)Parent).X - prevX),
Margin.Bottom + (e.GetPosition((Floor)Parent).Y - prevY));
InvalidateVisual();
}
prevX = e.GetPosition((Floor)Parent).X;
prevY = e.GetPosition((Floor)Parent).Y;
cX = Margin.Left + r.Width / 2;
cY = Margin.Top + r.Height / 2;
}
}
internal virtual void SnapToGrid()
{
double xPos = Margin.Left;
double yPos = Margin.Top;
double xMarg = xPos % ((Floor)Parent).cellDim;
double yMarg = yPos % ((Floor)Parent).cellDim;
if (xMarg < ((Floor)Parent).cellDim / 2)
{
if (yMarg < ((Floor)Parent).cellDim / 2)
{
Margin = new Thickness(xPos - xMarg, yPos - yMarg, xPos - xMarg + r.Width, yPos - yMarg + r.Height);
}
else
{
Margin = new Thickness(xPos - xMarg, yPos - yMarg + ((Floor)Parent).cellDim, xPos - xMarg + r.Width,
yPos - yMarg + ((Floor)Parent).cellDim + r.Height);
}
}
else
{
if (yMarg < ((Floor)Parent).cellDim / 2)
{
Margin = new Thickness(xPos - xMarg + ((Floor)Parent).cellDim, yPos - yMarg,
xPos - xMarg + ((Floor)Parent).cellDim + r.Width, yPos - yMarg + r.Height);
}
else
{
Margin = new Thickness(xPos - xMarg + ((Floor)Parent).cellDim, yPos - yMarg + ((Floor)Parent).cellDim,
xPos - xMarg + ((Floor)Parent).cellDim + r.Width, yPos - yMarg + ((Floor)Parent).cellDim + r.Height);
}
}
}
Essentially, I'm looking for an easy way to modify existing code to allow the user interface element to have another single on top of it. Thank!