Files
OldThink/Content.Client/UserInterface/HandsGui.cs

264 lines
7.8 KiB
C#
Raw Normal View History

using Content.Client.GameObjects;
2018-11-21 20:58:11 +01:00
using Content.Client.GameObjects.EntitySystems;
using Content.Client.Interfaces.GameObjects;
2017-09-30 16:56:19 +02:00
using SS14.Client.GameObjects;
using SS14.Client.Graphics;
using SS14.Client.Graphics.Drawing;
using SS14.Client.Input;
using SS14.Client.Interfaces.GameObjects.Components;
using SS14.Client.Interfaces.ResourceManagement;
2017-09-30 16:56:19 +02:00
using SS14.Client.Interfaces.UserInterface;
using SS14.Client.Player;
using SS14.Client.ResourceManagement;
using SS14.Client.UserInterface;
using SS14.Client.UserInterface.Controls;
2017-09-30 16:56:19 +02:00
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.IoC;
using SS14.Shared.Log;
2018-11-21 20:58:11 +01:00
using SS14.Shared.Map;
2017-09-30 16:56:19 +02:00
using SS14.Shared.Maths;
namespace Content.Client.UserInterface
{
public class HandsGui : Control
2017-09-30 16:56:19 +02:00
{
private static readonly Color _inactiveColor = new Color(90, 90, 90);
2018-11-21 20:58:11 +01:00
private const int BOX_SPACING = 1;
2018-11-21 20:58:11 +01:00
// The boxes are square so that's both width and height.
private const int BOX_SIZE = 50;
2017-09-30 16:56:19 +02:00
private readonly IPlayerManager _playerManager = IoCManager.Resolve<IPlayerManager>();
private readonly IUserInterfaceManager _userInterfaceManager = IoCManager.Resolve<IUserInterfaceManager>();
private StyleBoxTexture handBox;
private StyleBoxTexture inactiveHandBox;
2017-09-30 16:56:19 +02:00
2019-03-15 19:07:29 +01:00
private IEntity LeftHand;
private IEntity RightHand;
2018-09-17 10:46:38 +02:00
private UIBox2i handL;
private UIBox2i handR;
2017-09-30 16:56:19 +02:00
2019-03-15 19:07:29 +01:00
private SpriteView LeftSpriteView;
private SpriteView RightSpriteView;
protected override void Initialize()
2017-09-30 16:56:19 +02:00
{
base.Initialize();
2019-02-21 22:01:13 +01:00
var resMgr = IoCManager.Resolve<IResourceCache>();
var handsBoxTexture = resMgr.GetResource<TextureResource>("/Textures/UserInterface/handsbox.png");
handBox = new StyleBoxTexture()
{
Texture = handsBoxTexture,
};
2019-02-21 22:01:13 +01:00
handBox.SetPatchMargin(StyleBox.Margin.All, 6);
inactiveHandBox = new StyleBoxTexture(handBox)
{
Modulate = _inactiveColor,
};
SetMarginsPreset(LayoutPreset.CenterBottom);
SetAnchorPreset(LayoutPreset.CenterBottom);
2018-09-17 10:46:38 +02:00
handL = new UIBox2i(0, 0, BOX_SIZE, BOX_SIZE);
handR = handL.Translated(new Vector2i(BOX_SIZE + BOX_SPACING, 0));
MouseFilter = MouseFilterMode.Stop;
2019-03-15 19:07:29 +01:00
2019-03-22 20:45:12 +01:00
LeftSpriteView = new SpriteView {MouseFilter = MouseFilterMode.Ignore};
2019-03-15 19:07:29 +01:00
AddChild(LeftSpriteView);
LeftSpriteView.Size = handL.Size;
LeftSpriteView.Position = handL.TopLeft;
2019-03-22 20:45:12 +01:00
RightSpriteView = new SpriteView {MouseFilter = MouseFilterMode.Ignore};
2019-03-15 19:07:29 +01:00
AddChild(RightSpriteView);
RightSpriteView.Size = handR.Size;
RightSpriteView.Position = handR.TopLeft;
2017-09-30 16:56:19 +02:00
}
protected override Vector2 CalculateMinimumSize()
2017-09-30 16:56:19 +02:00
{
return new Vector2(BOX_SIZE * 2 + 1, BOX_SIZE);
2017-09-30 16:56:19 +02:00
}
2018-09-17 10:46:38 +02:00
protected override void Draw(DrawingHandleScreen handle)
2017-09-30 16:56:19 +02:00
{
if (!TryGetHands(out IHandsComponent hands))
2017-09-30 16:56:19 +02:00
return;
var leftActive = hands.ActiveIndex == "left";
handle.DrawStyleBox(handBox, leftActive ? handL : handR);
handle.DrawStyleBox(inactiveHandBox, leftActive ? handR : handL);
2017-09-30 16:56:19 +02:00
}
/// <summary>
/// Gets the hands component controling this gui, returns true if successful and false if failure
/// </summary>
/// <param name="hands"></param>
/// <returns></returns>
private bool TryGetHands(out IHandsComponent hands)
2017-09-30 16:56:19 +02:00
{
hands = null;
if (_playerManager?.LocalPlayer == null)
2017-09-30 16:56:19 +02:00
{
return false;
2017-09-30 16:56:19 +02:00
}
IEntity entity = _playerManager.LocalPlayer.ControlledEntity;
if (entity == null || !entity.TryGetComponent(out hands))
2017-09-30 16:56:19 +02:00
{
return false;
2017-09-30 16:56:19 +02:00
}
return true;
}
public void UpdateHandIcons()
{
if (Parent == null)
{
return;
}
2018-11-21 20:58:11 +01:00
UpdateDraw();
2019-03-15 19:07:29 +01:00
if (!TryGetHands(out var hands))
return;
2017-09-30 16:56:19 +02:00
var left = hands.GetEntity("left");
var right = hands.GetEntity("right");
if (left != null)
{
2019-03-15 19:07:29 +01:00
if (left != LeftHand)
2017-09-30 16:56:19 +02:00
{
2019-03-15 19:07:29 +01:00
LeftHand = left;
if (LeftHand.TryGetComponent(out ISpriteComponent sprite))
{
LeftSpriteView.Sprite = sprite;
}
2017-09-30 16:56:19 +02:00
}
}
else
{
2019-03-15 19:07:29 +01:00
LeftHand = null;
LeftSpriteView.Sprite = null;
2017-09-30 16:56:19 +02:00
}
if (right != null)
{
2019-03-15 19:07:29 +01:00
RightHand = right;
if (RightHand.TryGetComponent(out ISpriteComponent sprite))
2017-09-30 16:56:19 +02:00
{
2019-03-15 19:07:29 +01:00
RightSpriteView.Sprite = sprite;
2017-09-30 16:56:19 +02:00
}
}
else
{
2019-03-15 19:07:29 +01:00
RightHand = null;
RightSpriteView.Sprite = null;
2017-09-30 16:56:19 +02:00
}
}
private void SendSwitchHandTo(string index)
{
if (!TryGetHands(out IHandsComponent hands))
2017-09-30 16:56:19 +02:00
return;
2017-09-30 16:56:19 +02:00
hands.SendChangeHand(index);
}
private void UseActiveHand()
{
if (!TryGetHands(out IHandsComponent hands))
return;
//Todo: remove hands interface, so weird
2018-11-21 20:58:11 +01:00
((HandsComponent) hands).UseActiveHand();
}
private void AttackByInHand(string hand)
{
if (!TryGetHands(out var hands))
return;
hands.AttackByInHand(hand);
}
protected override bool HasPoint(Vector2 point)
{
2018-11-21 20:58:11 +01:00
return handL.Contains((Vector2i) point) || handR.Contains((Vector2i) point);
}
protected override void MouseDown(GUIMouseButtonEventArgs args)
2017-09-30 16:56:19 +02:00
{
base.MouseDown(args);
2018-11-21 20:58:11 +01:00
var leftHandContains = handL.Contains((Vector2i) args.RelativePosition);
var rightHandContains = handR.Contains((Vector2i) args.RelativePosition);
string handIndex;
if (leftHandContains)
{
handIndex = "left";
}
else if (rightHandContains)
{
handIndex = "right";
}
else
{
return;
}
if (args.Button == Mouse.Button.Left)
2017-09-30 16:56:19 +02:00
{
2018-11-21 20:58:11 +01:00
if (!TryGetHands(out var hands))
return;
2018-11-21 20:58:11 +01:00
if (hands.ActiveIndex == handIndex)
{
UseActiveHand();
2018-11-21 20:58:11 +01:00
}
else
{
AttackByInHand(handIndex);
}
2017-09-30 16:56:19 +02:00
}
2018-11-21 20:58:11 +01:00
else if (args.Button == Mouse.Button.Middle)
{
SendSwitchHandTo(handIndex);
}
else if (args.Button == Mouse.Button.Right)
2017-09-30 16:56:19 +02:00
{
2018-11-21 20:58:11 +01:00
if (!TryGetHands(out var hands))
{
2018-11-21 20:58:11 +01:00
return;
}
2018-11-21 20:58:11 +01:00
var entity = hands.GetEntity(handIndex);
if (entity == null)
{
2018-11-21 20:58:11 +01:00
return;
}
2018-11-21 20:58:11 +01:00
var esm = IoCManager.Resolve<IEntitySystemManager>();
esm.GetEntitySystem<VerbSystem>().OpenContextMenu(entity, new ScreenCoordinates(args.GlobalPosition));
2017-09-30 16:56:19 +02:00
}
}
private static ISpriteProxy GetSpriteMirror(IEntity entity)
2017-09-30 16:56:19 +02:00
{
if (entity.TryGetComponent(out ISpriteComponent component))
2017-09-30 16:56:19 +02:00
{
return component.CreateProxy();
2017-09-30 16:56:19 +02:00
}
2018-11-21 20:58:11 +01:00
return null;
2017-09-30 16:56:19 +02:00
}
}
}