resolution and input (wp8)

Hello, How do I get my buttons on another resolution if the coordinates do not match?
Screen 800x400, which was created for all works. but if you change the resolution of the coordinates change.
tell me how to properly remake please?

class Button
{
    public delegate void ButtonPressed();
    public event ButtonPressed buttonPressedEvent;

    private Texture2D texture;
    private Rectangle rectangle;
    private Rectangle bound;

    private Vector2 position;

    private bool enabled = true;
    public Button(Texture2D texture, int width, int height)
    {
        this.texture = texture;
        rectangle = new Rectangle(0, 0, width, height);
        bound = new Rectangle(0, 0, width, height);
    }

    public void Update(GameTime gameTime, TouchCollection touchCollection)
    {
        if (enabled)
        {
            if (touchCollection.Count > 0)
            {
                TouchLocation touchLocation = touchCollection[0];
                if (touchLocation.State == TouchLocationState.Pressed)
                {
                    
                    if (bound.Contains((int)touchLocation.Position.X, (int)touchLocation.Position.Y))
                        rectangle.X = rectangle.Width;
                }
                else if (touchLocation.State == TouchLocationState.Released)
                {
                    Debug.WriteLine(touchLocation.Position);//{X:1350 Y:618.0196}
                    Debug.WriteLine(bound);//{X:450 Y:240 Width:200 Height:88}
                    if (bound.Contains((int)touchLocation.Position.X, (int)touchLocation.Position.Y) && buttonPressedEvent != null)
                        buttonPressedEvent();

                    rectangle.X = 0;

                }
            }
        }

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, position, rectangle, Color.White);
    }
    public bool Enabled
    {
        get
        {
            return enabled;
        }
        set
        {
            if (value)
                rectangle.X = 0;
            else
                rectangle.X = rectangle.Width;
            enabled = value;

        }
    }
    public Vector2 Position
    {
        get
        {
            return position;
        }
        set
        {
            bound.X = (int)value.X;
            bound.Y = (int)value.Y;

            position = value;
        }

    }
}

drawing from the example https://github.com/Mono-Game/MonoGame.Samples/blob/develop/Platformer2D/Game.cs

 GraphicsDevice.Clear(Color.CornflowerBlue);
 spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null, globalTransformation);
 myButton.Draw(spriteBatch);
 spriteBatch.End();

update button:

 touchCollection = TouchPanel.GetState();
 myButton.Update(gameTime, touchCollection);

Well u are scaling your rendering to match the screen res, u also need to scale the touch points by multiplying them by the difference in screen size both on x and y

1 Like