2018-11-22 17:00:22 -05:00
|
|
|
|
using OpenDiablo2.Common;
|
|
|
|
|
using OpenDiablo2.Common.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace OpenDiablo2.Core.UI
|
|
|
|
|
{
|
|
|
|
|
public sealed class WideButton
|
|
|
|
|
{
|
|
|
|
|
private readonly IMouseInfoProvider mouseInfoProvider;
|
|
|
|
|
private readonly IRenderWindow renderWindow;
|
|
|
|
|
|
|
|
|
|
public delegate void OnActivateDelegate();
|
|
|
|
|
public OnActivateDelegate OnActivate { get; set; }
|
|
|
|
|
|
2018-11-22 18:06:15 -05:00
|
|
|
|
private Point location = new Point();
|
|
|
|
|
public Point Location
|
|
|
|
|
{
|
|
|
|
|
get => location;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
location = value;
|
|
|
|
|
sprite.Location = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 18:16:42 -05:00
|
|
|
|
private readonly int buttonWidth, buttonHeight;
|
2018-11-22 18:06:15 -05:00
|
|
|
|
private ISprite sprite;
|
|
|
|
|
private IFont font;
|
|
|
|
|
private ILabel label;
|
2018-11-22 18:16:42 -05:00
|
|
|
|
private bool pressed = false;
|
2018-11-22 18:46:24 -05:00
|
|
|
|
private bool active = false; // When true, button is actively being focus pressed
|
|
|
|
|
private bool activeLock = false; // When true, something else is being pressed so ignore everything
|
2018-11-22 18:06:15 -05:00
|
|
|
|
private Point labelOffset = new Point();
|
2018-11-22 17:00:22 -05:00
|
|
|
|
|
|
|
|
|
private string text;
|
|
|
|
|
public string Text
|
|
|
|
|
{
|
|
|
|
|
get => text;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
text = value;
|
|
|
|
|
UpdateText();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WideButton(IRenderWindow renderWindow, IMouseInfoProvider mouseInfoProvider)
|
|
|
|
|
{
|
|
|
|
|
this.renderWindow = renderWindow;
|
|
|
|
|
this.mouseInfoProvider = mouseInfoProvider;
|
|
|
|
|
|
2018-11-22 18:16:42 -05:00
|
|
|
|
sprite = renderWindow.LoadSprite(ResourcePaths.WideButtonBlank, Palettes.Units);
|
|
|
|
|
font = renderWindow.LoadFont(ResourcePaths.FontExocet10, Palettes.Units);
|
2018-11-22 18:06:15 -05:00
|
|
|
|
label = renderWindow.CreateLabel(font);
|
2018-11-22 18:16:42 -05:00
|
|
|
|
|
|
|
|
|
// TODO: Less stupid way of doing this would be nice
|
|
|
|
|
sprite.Frame = 0;
|
|
|
|
|
buttonWidth = sprite.LocalFrameSize.Width;
|
|
|
|
|
buttonHeight = sprite.LocalFrameSize.Height;
|
|
|
|
|
sprite.Frame = 1;
|
|
|
|
|
buttonWidth += sprite.LocalFrameSize.Width;
|
|
|
|
|
|
2018-11-22 17:00:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update()
|
|
|
|
|
{
|
2018-11-22 18:16:42 -05:00
|
|
|
|
var hovered = (mouseInfoProvider.MouseX >= location.X && mouseInfoProvider.MouseX < (location.X + buttonWidth))
|
|
|
|
|
&& (mouseInfoProvider.MouseY >= location.Y && mouseInfoProvider.MouseY < (location.Y + buttonHeight));
|
2018-11-22 17:00:22 -05:00
|
|
|
|
|
2018-11-22 18:46:24 -05:00
|
|
|
|
|
|
|
|
|
if (!activeLock && hovered && mouseInfoProvider.LeftMouseDown && !mouseInfoProvider.ReserveMouse)
|
|
|
|
|
{
|
|
|
|
|
// The button is being pressed down
|
|
|
|
|
mouseInfoProvider.ReserveMouse = true;
|
|
|
|
|
active = true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (active && !mouseInfoProvider.LeftMouseDown)
|
|
|
|
|
{
|
|
|
|
|
mouseInfoProvider.ReserveMouse = false;
|
|
|
|
|
active = false;
|
|
|
|
|
|
|
|
|
|
if (hovered)
|
|
|
|
|
OnActivate?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
else if (!active && mouseInfoProvider.LeftMouseDown)
|
|
|
|
|
{
|
|
|
|
|
activeLock = true;
|
|
|
|
|
}
|
|
|
|
|
else if (activeLock && !mouseInfoProvider.LeftMouseDown)
|
|
|
|
|
{
|
|
|
|
|
activeLock = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pressed = (hovered && mouseInfoProvider.LeftMouseDown && active);
|
2018-11-22 17:00:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 18:06:15 -05:00
|
|
|
|
public void Render()
|
2018-11-22 17:00:22 -05:00
|
|
|
|
{
|
2018-11-22 18:06:15 -05:00
|
|
|
|
renderWindow.Draw(sprite, 2, 1, pressed ? 1 : 0);
|
2018-11-22 18:16:42 -05:00
|
|
|
|
var offset = pressed ? -3 : 0;
|
2018-11-22 17:00:22 -05:00
|
|
|
|
|
2018-11-22 18:16:42 -05:00
|
|
|
|
label.Location = new Point(location.X + offset + labelOffset.X, location.Y - offset + labelOffset.Y);
|
2018-11-22 18:06:15 -05:00
|
|
|
|
renderWindow.Draw(label);
|
2018-11-22 17:00:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateText()
|
|
|
|
|
{
|
2018-11-22 18:06:15 -05:00
|
|
|
|
label.Text = text;
|
2018-11-22 18:16:42 -05:00
|
|
|
|
label.TextColor = Color.FromArgb(75, 75, 75);
|
2018-11-22 17:00:22 -05:00
|
|
|
|
|
2018-11-22 18:16:42 -05:00
|
|
|
|
var offsetX = (buttonWidth / 2) - (label.TextArea.Width / 2);
|
2018-11-22 18:06:15 -05:00
|
|
|
|
labelOffset = new Point(offsetX, -5);
|
2018-11-22 17:00:22 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|