• Advertisement
Sign in to follow this  

[Solved] Simple GUI programming. Quadrangle position is resetting?

This topic is 3030 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

Hello there! Here is my Quadrangle struct for GUI's:
    struct Quad_GUI
    {
        Vector2 position;                       // Central position of quadrangle
        short[] index;                          // Quad indices
        VertexPositionTexture[] vertex;         // Quad vertices
        Texture2D texture;                      // Quad texture
        
        public Quad_GUI(Texture2D texture, Vector2 centre)
        {
            index = new short[6] { 0, 1, 2, 1, 3, 2 };
            vertex = new VertexPositionTexture[4];         
            this.texture = texture;
            position = centre;

            SetPosition(centre);
        }

        public void SetPosition(Vector2 centre)
        {
            position = centre;

            Vector2 half = new Vector2(texture.Width / 2, texture.Height / 2);
            Vector2 width = new Vector2(texture.Width, 0);

            // Top Left position of texture
            Vector2 position_TL = position - half;
            Vector2 position_TR = position - half + width;
            Vector2 position_BL = position + half - width;
            Vector2 position_BR = position + half;

            // Set position of new texture vertices in screen coordinates
            vertex[0].Position = new Vector3(GUI.XYCoordToScreenCoord(position_TL), 0);
            vertex[0].TextureCoordinate = new Vector2(0, 0);
            vertex[1].Position = new Vector3(GUI.XYCoordToScreenCoord(position_TR), 0);
            vertex[1].TextureCoordinate = new Vector2(1, 0);
            vertex[2].Position = new Vector3(GUI.XYCoordToScreenCoord(position_BL), 0);
            vertex[2].TextureCoordinate = new Vector2(0, 1);
            vertex[3].Position = new Vector3(GUI.XYCoordToScreenCoord(position_BR), 0);
            vertex[3].TextureCoordinate = new Vector2(1, 1);
        }

        public short[] Indices
        {
            get
            {
                return index;
            }
        }

        public Vector2 Position
        {
            get
            {
                return position;
            }
        }

        public Texture2D Texture
        {
            get
            {
                return texture;
            }
            set
            {
                texture = value;
            }
        }

        public VertexPositionTexture[] Vertices
        {
            get
            {
                return vertex;
            }
        }
    }


and here is my simple GUI class that draws the quads to the screen:
    class GUI
    {
        static GraphicsDevice graphics_Device;          // Current Graphics Device
        VertexPositionTexture[] vertex;                 // 4 Vertices that cover the entire screen
        VertexDeclaration vertex_Declaration;

        float transparency;
        Effect effect_Current;

        List<Quad_GUI> list_Quad;

        public GUI(GraphicsDevice graphics_Device, Effect effect_Current)
        {
            GUI.graphics_Device = graphics_Device;
            this.effect_Current = effect_Current;

            list_Quad = new List<Quad_GUI>();

            // Set initial transparency (affects all quad textures)
            transparency = 1.0f;

            vertex = new VertexPositionTexture[4];
            vertex_Declaration = new VertexDeclaration(graphics_Device, VertexPositionTexture.VertexElements);
        }

        public void AddTexture(Texture2D texture, Vector2 centre)
        {
            list_Quad.Add(new Quad_GUI(texture, centre));
        }

        public void MoveTexture(Vector2 centre, int index)
        {
            // Only move texture if position has changed
            if (list_Quad[index].Position != centre)
            {
                list_Quad[index].SetPosition(centre);
            }
        }

        public void Draw()
        {       
            effect_Current.CurrentTechnique = effect_Current.Techniques["GUI_Transparent"];

            // Set GUI transparency
            effect_Current.Parameters["xGUI_Transparency"].SetValue(transparency);

            foreach (Quad_GUI quad in list_Quad)
            {
                effect_Current.Parameters["xTexture0"].SetValue(quad.Texture);

                effect_Current.Begin();
                foreach (EffectPass pass in effect_Current.CurrentTechnique.Passes)
                {
                    pass.Begin();
                    graphics_Device.VertexDeclaration = vertex_Declaration;
                    graphics_Device.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, quad.Vertices, 0, quad.Vertices.Length, quad.Indices, 0, quad.Indices.Length / 3);
                    pass.End();
                }
                effect_Current.End();
            }
        }

        // Scale XY position to screen coordinates
        public static Vector2 XYCoordToScreenCoord(Vector2 coord)
        {
            // Half pixel offset for correct texel centring
            coord.X -= 0.5f;
            coord.Y -= 0.5f;

            // Scale X and Y from 0 to 1
            coord.X /= graphics_Device.Viewport.Width;
            coord.Y /= graphics_Device.Viewport.Height;

            // Scale X from 0 to 2
            // Scale Y from 0 to -2
            coord *= new Vector2(2, -2);

            // Scale X from -1 to 1
            // Scale Y from 1 to -1
            coord -= new Vector2(1, -1);

            return coord;
        }

        public float Transparency
        {
            get
            {
                return transparency;
            }
            set
            {
                // Clamp the transparency value to the range of 0 to 2
                value = (value > 2.0f) ? 2.0f : value;
                value = (value < 0.0f) ? 0.0f : value;
                transparency = value;
            }
        }
    }


Each time I change the position of a quad using: gui.MoveTexture(new Vector2(10, 50), 1); the following if statement is always true:
        public void MoveTexture(Vector2 centre, int index)
        {
            // Only move texture if position has changed
            if (list_Quad[index].Position != centre)
            {
                list_Quad[index].SetPosition(centre);
            }
        }
I don't understand why the Position returned has not be changed in the SetPosition method. What have I done wrong? Also, can I get your feedback on this simple GUI implementation, have I implemented it in a respectable way? Thank you. [Edited by - Spa8nky on November 5, 2009 6:54:01 PM]

Share this post


Link to post
Share on other sites
Advertisement
Is the != operator properly overloaded for your Vector2 class? If not, then
if (list_Quad[index].Position != centre)
is probably not doing what you think it's doing.

Share this post


Link to post
Share on other sites
Changing the Quad_GUI struct to a class solved the problem. I think there is something funny about get and set accessors with structs.

Share this post


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

  • Advertisement