1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2025-02-14 12:36:41 -05:00
OpenDiablo2/OpenDiablo2.Core/UI/MiniPanel.cs

133 lines
4.5 KiB
C#
Raw Normal View History

/* OpenDiablo 2 - An open source re-implementation of Diablo 2 in C#
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using OpenDiablo2.Common;
using OpenDiablo2.Common.Enums;
using OpenDiablo2.Common.Extensions;
using OpenDiablo2.Common.Interfaces;
using System;
2018-12-07 18:45:23 -05:00
using System.Collections.Generic;
using System.Drawing;
2018-12-07 18:45:23 -05:00
using System.Linq;
namespace OpenDiablo2.Core.UI
{
public sealed class MiniPanel : IMiniPanel
{
2018-12-07 18:45:23 -05:00
private static readonly IEnumerable<eButtonType> panelButtons = new[] { eButtonType.MinipanelCharacter, eButtonType.MinipanelInventory,
eButtonType.MinipanelSkill, eButtonType.MinipanelAutomap, eButtonType.MinipanelMessage, eButtonType.MinipanelQuest, eButtonType.MinipanelMenu };
private readonly IRenderWindow renderWindow;
private readonly IMouseInfoProvider mouseInfoProvider;
2018-12-07 18:45:23 -05:00
private readonly IGameState gameState;
private readonly ISprite sprite;
private readonly IReadOnlyList<IButton> buttons;
private readonly IEnumerable<IPanel> panels;
private bool isPanelVisible;
public event OnPanelToggledEvent OnPanelToggled;
2018-12-07 18:45:23 -05:00
public MiniPanel(IRenderWindow renderWindow,
IGameState gameState,
IMouseInfoProvider mouseInfoProvider,
IEnumerable<IPanel> panels,
Func<eButtonType, IButton> createButton)
2018-12-07 18:45:23 -05:00
{
this.renderWindow = renderWindow;
this.mouseInfoProvider = mouseInfoProvider;
2018-12-07 18:45:23 -05:00
this.gameState = gameState;
this.panels = panels;
2018-12-09 14:55:01 -05:00
sprite = renderWindow.LoadSprite(ResourcePaths.MinipanelSmall, Palettes.Units, true);
2018-12-07 18:45:23 -05:00
buttons = panelButtons.Select((x, i) =>
{
var newBtn = createButton(x);
var panel = panels.SingleOrDefault(o => o.PanelType == x.GetPanelType());
if (panel != null)
2018-12-07 18:45:23 -05:00
{
newBtn.OnActivate = () => OnPanelToggled?.Invoke(panel);
panel.OnPanelClosed += Panel_OnPanelClosed;
}
2018-12-07 18:45:23 -05:00
return newBtn;
}).ToList().AsReadOnly();
UpdatePanelLocation();
}
public void OnMenuToggle(bool isToggled) => isPanelVisible = isToggled;
public IPanel GetPanel(ePanelType panelType)
{
return panels.SingleOrDefault(o => o.PanelType == panelType);
}
public bool IsMouseOver()
{
int xDiff = mouseInfoProvider.MouseX - sprite.Location.X;
int yDiff = mouseInfoProvider.MouseY - sprite.Location.Y + sprite.LocalFrameSize.Height;
2018-12-07 18:45:23 -05:00
return isPanelVisible
&& xDiff >= 0 && xDiff <= sprite.LocalFrameSize.Width
&& yDiff >= 0 && yDiff <= sprite.LocalFrameSize.Height;
}
2018-12-07 18:45:23 -05:00
public void Render()
{
if (!isPanelVisible)
2018-12-07 18:45:23 -05:00
return;
2018-12-07 18:45:23 -05:00
renderWindow.Draw(sprite);
2018-12-07 18:45:23 -05:00
foreach (var button in buttons)
button.Render();
}
public void Update()
2018-12-07 18:45:23 -05:00
{
if (!isPanelVisible)
return;
foreach (var button in buttons)
button.Update();
}
public void Dispose()
{
foreach (var button in buttons)
button.Dispose();
sprite.Dispose();
}
public void UpdatePanelLocation()
{
2018-12-07 18:45:23 -05:00
sprite.Location = new Point((800 - sprite.LocalFrameSize.Width + (int)(gameState.CameraOffset * 1.3f)) / 2,
526 + sprite.LocalFrameSize.Height);
2018-12-07 18:45:23 -05:00
for (int i = 0; i < buttons.Count; i++)
buttons[i].Location = new Point(3 + 21 * i + sprite.Location.X, 3 + sprite.Location.Y - sprite.LocalFrameSize.Height);
}
private void Panel_OnPanelClosed(IPanel panel)
{
OnPanelToggled?.Invoke(panel);
}
}
}