2018-11-22 17:00:22 -05:00
|
|
|
|
using OpenDiablo2.Common;
|
|
|
|
|
using OpenDiablo2.Common.Interfaces;
|
2018-11-22 22:53:05 -05:00
|
|
|
|
using OpenDiablo2.Common.Models;
|
2018-11-22 17:00:22 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace OpenDiablo2.Core.UI
|
|
|
|
|
{
|
2018-11-22 22:53:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public sealed class Button : IDisposable
|
2018-11-22 17:00:22 -05:00
|
|
|
|
{
|
|
|
|
|
private readonly IMouseInfoProvider mouseInfoProvider;
|
|
|
|
|
private readonly IRenderWindow renderWindow;
|
2018-11-22 22:53:05 -05:00
|
|
|
|
private readonly ButtonLayout buttonLayout;
|
2018-11-22 17:00:22 -05:00
|
|
|
|
|
|
|
|
|
public delegate void OnActivateDelegate();
|
|
|
|
|
public OnActivateDelegate OnActivate { get; set; }
|
|
|
|
|
|
2018-11-24 23:33:04 -05:00
|
|
|
|
public delegate void OnToggleDelegate(bool isToggled);
|
|
|
|
|
public OnToggleDelegate OnToggle { 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 22:53:05 -05:00
|
|
|
|
private 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-24 23:33:04 -05:00
|
|
|
|
private bool toggled = false;
|
|
|
|
|
|
2018-11-22 18:06:15 -05:00
|
|
|
|
private Point labelOffset = new Point();
|
2018-11-23 18:03:29 -05:00
|
|
|
|
|
|
|
|
|
private bool enabled = true;
|
|
|
|
|
public bool Enabled
|
|
|
|
|
{
|
|
|
|
|
get => enabled;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == enabled)
|
|
|
|
|
return;
|
|
|
|
|
enabled = value;
|
|
|
|
|
|
|
|
|
|
sprite.Darken = !enabled;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-22 22:53:05 -05:00
|
|
|
|
|
2018-11-22 17:00:22 -05:00
|
|
|
|
private string text;
|
|
|
|
|
public string Text
|
|
|
|
|
{
|
|
|
|
|
get => text;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
text = value;
|
|
|
|
|
UpdateText();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 22:53:05 -05:00
|
|
|
|
|
|
|
|
|
public Button(
|
|
|
|
|
ButtonLayout buttonLayout,
|
|
|
|
|
IRenderWindow renderWindow,
|
|
|
|
|
IMouseInfoProvider mouseInfoProvider
|
|
|
|
|
)
|
2018-11-22 17:00:22 -05:00
|
|
|
|
{
|
2018-11-22 22:53:05 -05:00
|
|
|
|
this.buttonLayout = buttonLayout;
|
2018-11-22 17:00:22 -05:00
|
|
|
|
this.renderWindow = renderWindow;
|
|
|
|
|
this.mouseInfoProvider = mouseInfoProvider;
|
|
|
|
|
|
2018-11-22 18:16:42 -05:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
2018-11-22 22:53:05 -05:00
|
|
|
|
sprite = renderWindow.LoadSprite(buttonLayout.ResourceName, buttonLayout.PaletteName);
|
|
|
|
|
|
|
|
|
|
// TODO: Less stupid way of doing this would be nice
|
|
|
|
|
buttonWidth = 0;
|
|
|
|
|
buttonHeight = 0;
|
|
|
|
|
for (int i = 0; i < buttonLayout.XSegments; i++)
|
|
|
|
|
{
|
|
|
|
|
sprite.Frame = i;
|
|
|
|
|
buttonWidth += sprite.LocalFrameSize.Width;
|
|
|
|
|
buttonHeight = Math.Max(buttonHeight, sprite.LocalFrameSize.Height);
|
|
|
|
|
}
|
2018-11-22 17:00:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 23:33:04 -05:00
|
|
|
|
public bool Toggle()
|
|
|
|
|
{
|
|
|
|
|
toggled = !toggled;
|
|
|
|
|
|
|
|
|
|
OnToggle?.Invoke(toggled);
|
|
|
|
|
|
|
|
|
|
return toggled;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 17:00:22 -05:00
|
|
|
|
public void Update()
|
|
|
|
|
{
|
2018-11-23 18:03:29 -05:00
|
|
|
|
if (!enabled)
|
|
|
|
|
{
|
|
|
|
|
// Prevent sticky locks
|
|
|
|
|
if (activeLock && mouseInfoProvider.ReserveMouse)
|
|
|
|
|
{
|
|
|
|
|
activeLock = false;
|
|
|
|
|
mouseInfoProvider.ReserveMouse = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
active = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2018-11-24 23:33:04 -05:00
|
|
|
|
{
|
2018-11-22 18:46:24 -05:00
|
|
|
|
OnActivate?.Invoke();
|
2018-11-24 23:33:04 -05:00
|
|
|
|
|
|
|
|
|
if (buttonLayout.Toggleable)
|
|
|
|
|
{
|
|
|
|
|
Toggle();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 18:46:24 -05:00
|
|
|
|
}
|
|
|
|
|
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-24 23:33:04 -05:00
|
|
|
|
var frame = 0;
|
|
|
|
|
|
|
|
|
|
if(toggled && pressed)
|
|
|
|
|
{
|
|
|
|
|
frame = 3;
|
|
|
|
|
}
|
|
|
|
|
else if(pressed)
|
|
|
|
|
{
|
|
|
|
|
frame = 1;
|
|
|
|
|
}
|
|
|
|
|
else if(toggled)
|
|
|
|
|
{
|
|
|
|
|
frame = 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderWindow.Draw(sprite, buttonLayout.XSegments, 1, frame);
|
2018-11-23 00:52:01 -05:00
|
|
|
|
var offset = pressed ? -2 : 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
|
|
|
|
}
|
2018-11-22 22:53:05 -05:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
sprite.Dispose();
|
|
|
|
|
font.Dispose();
|
|
|
|
|
label.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-22 17:00:22 -05:00
|
|
|
|
}
|