I have this Button2D class and everything works fine except that the text isn´t drawn in the button.
Discovered the problem, the font color is always White because of this line
Globals.normalEffect.Parameters["filterColor"].SetValue(tempColor.ToVector4());
How can I fix it?
public class Button2D : Basic2D
{
protected bool pressed, hovered;
protected string text;
protected Color hoverColor;
protected SpriteFont font;
protected object info;
protected PassObject buttonClicked;
public Button2D(string _path,Vector2 _position, Vector2 _dimensions,string _font,string _text,object _info,PassObject _buttonClicked)
: base(_path,_position,_dimensions)
{
text = _text;
buttonClicked = _buttonClicked;
if (_font != null)
{
font = Globals.content.Load<SpriteFont>(_font);
}
pressed = false;
hoverColor = new Color(200, 230, 255);
info = _info;
}
public override void Update(Vector2 _offset)
{
if (Hover(_offset))
{
hovered = true;
if (Globals.mouseControl.LeftClick())
{
hovered = false;
pressed = true;
}
else if (Globals.mouseControl.LeftClickRelease())
{
RunButtonClicked();
GameGlobals.score = 0;
}
}
else
{
hovered = false;
}
if(!Globals.mouseControl.LeftClick() && !Globals.mouseControl.LeftClickHold())
{
pressed = false;
}
base.Update(_offset);
}
public virtual void Reset()
{
pressed = false;
hovered = false;
}
public virtual void RunButtonClicked()
{
if (buttonClicked != null)
{
Debug.WriteLine("BUTOON CLICKED NAO E NULO");
buttonClicked(info);
}
else
{
Debug.WriteLine("Button CLICKED IS NULL");
}
Reset();
}
public override void Draw(Vector2 _offset)
{
Color tempColor = Color.White;
if (pressed)
{
tempColor = Color.Gray;
}
else if (hovered)
{
tempColor = hoverColor;
}
Globals.normalEffect.Parameters["xSize"].SetValue((float)GetModel().Bounds.Width);
Globals.normalEffect.Parameters["ySize"].SetValue((float)GetModel().Bounds.Height);
Globals.normalEffect.Parameters["xDraw"].SetValue((float)((int)dimensions.X));
Globals.normalEffect.Parameters["yDraw"].SetValue((float)((int)dimensions.Y));
Globals.normalEffect.Parameters["filterColor"].SetValue(tempColor.ToVector4());
Globals.normalEffect.CurrentTechnique.Passes[0].Apply();
base.Draw(_offset);
Vector2 strDims = font.MeasureString(text);
Debug.WriteLine("Text:" + text);
Globals.spriteBatch.DrawString(font, text, position + _offset + new Vector2(-strDims.X / 2, -strDims.Y / 2),Color.Black);
}
}