How to make an object "scalable" when rendering in the form

I make my game in Winform in the same way as in this example: WinForms Series 1: graphics device

In my game, I have some kind of object, for example a rectangle, which I can already put and move in my game world, once created. My project here is a level editor.

What I want to do is make each object “significant” or “scalable” (sorry if this is a wrong word) in the same way as every software we usually use, I mean: enter image description here

I have a class like:

public abstract class GameObject { protected Vector2 position_ = Vector2.Zero; protected float rotation_ = 0.0f; protected Vector2 scale_ = Vector2.One; protected float depth_ = 0.0f; protected bool is_passable_ = true; protected GameObject( Vector2 starting_position) { this.position_ = starting_position; } [DisplayName("Position")] public virtual Vector2 Position { get { return position_; } set { position_ = value; } } [BrowsableAttribute(false)] public abstract Rectangle PositionRectangle { get; } [BrowsableAttribute(false)] public abstract Rectangle SelectionRectangle { get; } [DisplayName("Scale")] public abstract Vector2 Scale { get; set; } [BrowsableAttribute(false)] public virtual float Depth { get { return depth_; } set { depth_ = value; } } [DisplayName("IsPassable?")] public bool IsPassable { get { return is_passable_; } set { is_passable_ = value; } } [BrowsableAttribute(false)] public abstract Vector2 TextureSize { get; } public abstract void Update(GameTime gameTime); public abstract void Draw(SpriteBatch spriteBatch); } 

As soon as an instance of the class is created, inside the form I thought of doing something like: (gameWrapper is a control created using a sample to draw a game inside the form)

 private void gameWrapper_MouseClick_1(object sender, MouseEventArgs e) { Vector2 mouse_xy = new Vector2(e.Location.X, e.Location.Y); GameObject obj = gameWrapper.GetObjectByPosition(mouse_xy); propertyGrid1.SelectedObject = obj; if (obj != null) gameWrapper.SelectObject(obj); else gameWrapper.Unselect(); propertyGrid1.Refresh(); } 

Inside gameWrapper:

 public SelectObject(GameObject obj) { List<Vector2> 4verticesList = new List(); // // Code to add 4 vertices coordinates of the SelectionRectangle to 4verticesList // foreach (Vector2 vertex_xy in 4VerticesList) DrawLittleRectangle(vertex_xy); } 

This is what I thought. Draw small buttons / rectangles using the function, and then handle clicks on them.

Is there any code already written to achieve this behavior? In fact, I'm not worried about how the object will change, but just for aesthetic buttons.

+8
c # visual-studio-2010 winforms
source share
2 answers

I did it!

The main function:

 private void DrawObjectSelection(SpriteBatch spriteBatch, Rectangle r) { r = Telecamera.Transform(r); Vector2 v1 = new Vector2(rX, rY); Vector2 v2 = new Vector2(rX + r.Width, rY); Vector2 v3 = new Vector2(rX, rY + r.Height); Vector2 v4 = new Vector2(rX + r.Width, rY + r.Height); //The side rectangle DrawEmptyRectangle(spriteBatch, v1, v2, v3, v4); //4 squares at vertices DrawFilledSquare(spriteBatch, v1); DrawFilledSquare(spriteBatch, v2); DrawFilledSquare(spriteBatch, v3); DrawFilledSquare(spriteBatch, v4); //the other 4 in the middle of every side float height = v4.Y - v1.Y; float width = v2.X - v1.X; DrawFilledSquare(spriteBatch, new Vector2(v1.X, v1.Y + height / 2)); DrawFilledSquare(spriteBatch, new Vector2(v2.X, v2.Y + height / 2)); DrawFilledSquare(spriteBatch, new Vector2(v1.X + width / 2, v1.Y)); DrawFilledSquare(spriteBatch, new Vector2(v1.X + width / 2, v4.Y)); } 

Using these functions:

 private void DrawLine( SpriteBatch spriteBatch, float width, Vector2 point1, Vector2 point2) { float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X); float length = Vector2.Distance(point1, point2); spriteBatch.Draw( grid_texture, point1, null, selection_color, angle, Vector2.Zero, new Vector2(length, width), SpriteEffects.None, 0); } private void DrawEmptyRectangle( SpriteBatch spriteBatch, Vector2 v1, Vector2 v2, Vector2 v3, Vector2 v4) { /* * V1****V2 * * * * * * * V3****V4 */ DrawLine(spriteBatch, 1.0f, v1, v2); DrawLine(spriteBatch, 1.0f, v1, v3); DrawLine(spriteBatch, 1.0f, v2, v4); DrawLine(spriteBatch, 1.0f, v3, v4); } private void DrawFilledSquare( SpriteBatch spriteBatch, Vector2 c_pos) //With center position { int lato = 8; spriteBatch.Draw( grid_texture, new Rectangle( (int)c_pos.X - lato/2, (int)c_pos.Y - lato/2, lato, lato), selection_color); } 

When I want to draw it, I need a rectangle, for example:

 //... DrawObjectSelection(spriteBatch, Camera.Transform(gameObject1.PositionRectangle)); //... 

The only thing missing is click processing on each square. This is done using the simple “Contains” test with the mouse position coordinate.

Result Screen:

A screen of the result

+6
source share

I think what you are looking for is something like CRectTracker ( http://msdn.microsoft.com/en-us/library/41731bbw.aspx ) in VC ++. Unfortunately, the C # equivalent is still not available. You can see the link below for the starter.

http://www.codeproject.com/Articles/8765/C-Rect-Tracker

+1
source share

All Articles