Jump to content
  • Advertisement
Sign in to follow this  
iliak

[.net] Draggable rectangle

This topic is 3831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi For my tile editor I need to select rectangles on a texture like in many painting software (ie Photoshop, Paint .Net...). Here's the kind of draggable / resizable tool I want : My needs are : Resize in 8 directions Drag the selection when click inside the rectangle Zoomable when I zoom in / zoom out the texture At first I tought the framework has such a function, but after looking closer, no. So, does anyone have an idea or should I make my own ?

Share this post


Link to post
Share on other sites
Advertisement
Here are some methods I've written to manage a selection box with sizing handles. You can use the IsHit() function on the MouseMove event to change the mouse cursor when the user rolls over a size handle.

public enum MouseTools
{
NoTool,
MoveObject,
SizeUp,
SizeUpLeft,
SizeUpRight,
SizeDown,
SizeDownLeft,
SizeDownRight,
SizeLeft,
SizeRight
};

public Rectangle Rect = Rectangle.Empty;

public virtual MouseTools IsHit(int xp, int yp)
{
if (xp >= Rect.X && xp <= Rect.X + Rect.Width && yp >= Rect.Y && yp <= Rect.Y + Rect.Height)
{
if (xp >= Rect.X + 4 && xp <= Rect.X + Rect.Width - 4 && yp >= Rect.Y + 4 && yp <= Rect.Y + Rect.Height - 4)
return MouseTools.MoveObject;
else
{
if (yp >= Rect.Y + Rect.Height - 4)
{
if (xp >= Rect.X + 4 && xp <= Rect.X + Rect.Width - 4)
return MouseTools.SizeDown;
else if (xp <= Rect.X + 4)
return MouseTools.SizeDownLeft;
else if (xp >= Rect.X + Rect.Width - 4)
return MouseTools.SizeDownRight;
}
if ((yp <= Rect.Y + 4))
{
if (xp >= Rect.X + 4 && xp <= Rect.X + Rect.Width - 4)
return MouseTools.SizeUp;
else if (xp <= Rect.X + 4)
return MouseTools.SizeUpLeft;
else if (xp >= Rect.X + Rect.Width - 4)
return MouseTools.SizeUpRight;
}
if (xp >= Rect.X + Rect.Width - 4)
{
if (yp >= Rect.Y + 4 && yp <= Rect.Y + Rect.Height - 4)
return MouseTools.SizeRight;
else if (yp <= Rect.Y + 4)
return MouseTools.SizeUpRight;
else if (yp >= Rect.Y + Rect.Height - 4)
return (MouseTools.SizeDownRight);
}
if (xp <= Rect.X + 4)
{
if (yp >= Rect.Y + 4 && yp <= Rect.Y + Rect.Height - 4)
return MouseTools.SizeLeft;
else if (yp <= Rect.Y + 4)
return MouseTools.SizeUpLeft;
else if (yp >= Rect.Y + Rect.Height - 4)
return MouseTools.SizeDownRight;
}
}
}

return MouseTools.NoTool;
}

public bool Move(int x, int y)
{
bool LocationChanged = false;

if (Rect.X != x || Rect.Y != y)
LocationChanged = true;

Rect.X = x;
Rect.Y = y;

return LocationChanged;
}

public virtual bool Size(Object.MouseTools tool, int x, int y)
{
bool LocationChanged = false;

if (x != 0 || y != 0)
LocationChanged = true;

Point Offset = new Point(Rect.X - x, Rect.Y - y);

switch (tool)
{
case Object.MouseTools.SizeUp:
Rect.Y -= Offset.Y;
Rect.Height += Offset.Y;
break;
case Object.MouseTools.SizeUpLeft:
Rect.X -= Offset.X;
Rect.Y -= Offset.Y;
Rect.Width += Offset.X;
Rect.Height += Offset.Y;
break;
case Object.MouseTools.SizeUpRight:
Rect.Y -= Offset.Y;
Rect.Width = -Offset.X;
Rect.Height += Offset.Y;
break;
case Object.MouseTools.SizeDown:
Rect.Height = -Offset.Y;
break;
case Object.MouseTools.SizeDownLeft:
Rect.X -= Offset.X;
Rect.Width += Offset.X;
Rect.Height = -Offset.Y;
break;
case Object.MouseTools.SizeDownRight:
Rect.Width = -Offset.X;
Rect.Height = -Offset.Y;
break;
case Object.MouseTools.SizeLeft:
Rect.X -= Offset.X;
Rect.Width += Offset.X;
break;
case Object.MouseTools.SizeRight:
Rect.Width = -Offset.X;
break;
}

if (Rect.Width < 8)
Rect.Width = 8;
if (Rect.Height < 8)
Rect.Height = 8;

return LocationChanged;
}

public void DrawSizeHandles(Graphics g)
{
SolidBrush sb = new SolidBrush(Color.Red);

g.FillRectangle(sb, Rect.X, Rect.Y, 4, 4);
g.FillRectangle(sb, Rect.X + (Rect.Width / 2) - 2, Rect.Y, 4, 4);
g.FillRectangle(sb, Rect.X + Rect.Width - 4, Rect.Y, 4, 4);
g.FillRectangle(sb, Rect.X, Rect.Y + (Rect.Height / 2) - 2, 4, 4);
g.FillRectangle(sb, Rect.X + Rect.Width - 4, (Rect.Y + (Rect.Height / 2) - 2), 4, 4);
g.FillRectangle(sb, Rect.X, Rect.Y + Rect.Height - 4, 4, 4);
g.FillRectangle(sb, Rect.X + (Rect.Width / 2) - 2, Rect.Y + Rect.Height - 4, 4, 4);
g.FillRectangle(sb, Rect.X + Rect.Width - 4, Rect.Y + Rect.Height - 4, 4, 4);

sb.Dispose();
}

public void DrawBoundingBox(Graphics g)
{
Pen p = new Pen(Color.Red);

p.DashPattern = new float[] { 4, 4 };
g.DrawRectangle(p, Rect);

DrawSizeHandles(g);

p.Dispose();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!