/
GuiDialogCreatureContents.cs
148 lines (115 loc) · 5.47 KB
/
GuiDialogCreatureContents.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.Config;
using Vintagestory.API.MathTools;
namespace Vintagestory.GameContent
{
public class GuiDialogCreatureContents : GuiDialog
{
public override string ToggleKeyCombinationCode => null;
InventoryGeneric inv;
EntityAgent owningEntity;
protected double FloatyDialogPosition => 0.6;
protected double FloatyDialogAlign => 0.8;
public override bool UnregisterOnClose => true;
public GuiDialogCreatureContents(InventoryGeneric inv, EntityAgent owningEntity, ICoreClientAPI capi, string code) : base(capi)
{
this.inv = inv;
this.owningEntity = owningEntity;
Compose(code);
}
public void Compose(string code)
{
double pad = GuiElementItemSlotGrid.unscaledSlotPadding;
int rows = (int)Math.Ceiling(inv.Count / 4f);
ElementBounds slotBounds = ElementStdBounds.SlotGrid(EnumDialogArea.None, pad, 40 + pad, 4, rows).FixedGrow(2 * pad, 2 * pad);
ElementBounds bgBounds = ElementBounds.Fill.WithFixedPadding(GuiStyle.ElementToDialogPadding);
bgBounds.BothSizing = ElementSizing.FitToChildren;
ElementBounds dialogBounds = ElementStdBounds
.AutosizedMainDialog.WithAlignment(EnumDialogArea.RightMiddle)
.WithFixedAlignmentOffset(-GuiStyle.DialogToScreenPadding, 0);
SingleComposer =
capi.Gui
.CreateCompo(code + owningEntity.EntityId, dialogBounds)
.AddShadedDialogBG(bgBounds, true)
.AddDialogTitleBar(Lang.Get(code), OnTitleBarClose)
.BeginChildElements(bgBounds)
.AddItemSlotGrid(inv, DoSendPacket, 4, slotBounds, "slots")
.EndChildElements()
.Compose()
;
}
private void DoSendPacket(object p)
{
capi.Network.SendEntityPacket(owningEntity.EntityId, p);
}
private void OnTitleBarClose()
{
TryClose();
}
public override void OnGuiOpened()
{
base.OnGuiOpened();
}
public override void OnGuiClosed()
{
base.OnGuiClosed();
capi.Network.SendPacketClient(capi.World.Player.InventoryManager.CloseInventory(inv));
SingleComposer.GetSlotGrid("slots").OnGuiClosed(capi);
}
// TODO: Fix code duplication from GuiDialogBlockEntity
/// <summary>
/// Render's the object in Orthographic mode.
/// </summary>
/// <param name="deltaTime">The time elapsed.</param>
public override void OnRenderGUI(float deltaTime)
{
if (capi.Settings.Bool["immersiveMouseMode"])
{
double offX = owningEntity.SelectionBox.X2 - owningEntity.OriginSelectionBox.X2;
double offZ = owningEntity.SelectionBox.Z2 - owningEntity.OriginSelectionBox.Z2;
Vec3d aboveHeadPos = new Vec3d(owningEntity.Pos.X + offX, owningEntity.Pos.Y + FloatyDialogPosition, owningEntity.Pos.Z + offZ);
Vec3d pos = MatrixToolsd.Project(aboveHeadPos, capi.Render.PerspectiveProjectionMat, capi.Render.PerspectiveViewMat, capi.Render.FrameWidth, capi.Render.FrameHeight);
// Z negative seems to indicate that the name tag is behind us \o/
if (pos.Z < 0) return;
SingleComposer.Bounds.Alignment = EnumDialogArea.None;
SingleComposer.Bounds.fixedOffsetX = 0;
SingleComposer.Bounds.fixedOffsetY = 0;
SingleComposer.Bounds.absFixedX = pos.X - SingleComposer.Bounds.OuterWidth / 2;
SingleComposer.Bounds.absFixedY = capi.Render.FrameHeight - pos.Y - SingleComposer.Bounds.OuterHeight * FloatyDialogAlign;
SingleComposer.Bounds.absMarginX = 0;
SingleComposer.Bounds.absMarginY = 0;
}
base.OnRenderGUI(deltaTime);
}
Vec3d entityPos = new Vec3d();
public override void OnFinalizeFrame(float dt)
{
base.OnFinalizeFrame(dt);
entityPos.Set(owningEntity.Pos.X, owningEntity.Pos.Y, owningEntity.Pos.Z);
entityPos.Add(owningEntity.SelectionBox.X2 - owningEntity.OriginSelectionBox.X2, 0, owningEntity.SelectionBox.Z2 - owningEntity.OriginSelectionBox.Z2);
if (!IsInRangeOfBlock())
{
// Because we cant do it in here
capi.Event.EnqueueMainThreadTask(() => TryClose(), "closedlg");
}
}
public override bool TryClose()
{
return base.TryClose();
}
/// <summary>
/// Checks if the player is in range of the block.
/// </summary>
/// <param name="pos">The block's position.</param>
/// <returns>In range or no?</returns>
public virtual bool IsInRangeOfBlock()
{
Vec3d playerEye = capi.World.Player.Entity.Pos.XYZ.Add(capi.World.Player.Entity.LocalEyePos);
double dist = GameMath.Sqrt(playerEye.SquareDistanceTo(entityPos));
return dist <= capi.World.Player.WorldData.PickingRange;
}
public override bool PrefersUngrabbedMouse => false;
}
}