-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipeActionButtonComponent.cs
More file actions
163 lines (139 loc) · 5.25 KB
/
RecipeActionButtonComponent.cs
File metadata and controls
163 lines (139 loc) · 5.25 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.Config;
namespace QuickCraft;
internal sealed class RecipeActionButtonComponent : RichTextComponentBase
{
private const double UnscaledHeight = 24.0;
private const double HorizontalPadding = 8.0;
private sealed class FixedBounds : ElementBounds
{
public override double bgDrawX => absFixedX;
public override double bgDrawY => absFixedY;
public override double renderX => absFixedX + renderOffsetX;
public override double renderY => absFixedY + renderOffsetY;
public override double absX => absFixedX;
public override double absY => absFixedY;
public FixedBounds(double width, double height)
{
absInnerWidth = fixedWidth = width;
absInnerHeight = fixedHeight = height;
BothSizing = ElementSizing.Fixed;
ParentBounds = new ElementBounds();
}
}
private readonly Action action;
private readonly string tooltip;
private readonly FixedBounds bounds;
private readonly GuiElementTextButton button;
private readonly GuiElementHoverText hover;
private double hoverSeconds;
private double lastRenderX;
private double lastRenderY;
public RecipeActionButtonComponent(ICoreClientAPI api, string label, string langCode, double[] color, Action action)
: base(api)
{
this.action = action;
tooltip = Lang.Get(ModIds.ModId + ":" + langCode);
Float = EnumFloat.Inline;
VerticalAlign = EnumVerticalAlign.Middle;
double height = Math.Ceiling(GuiElement.scaled(UnscaledHeight));
double width = Math.Ceiling(GuiElement.scaled(label.Length * 8.0 + HorizontalPadding * 2.0));
bounds = new FixedBounds(width, height);
CairoFont normalFont = CairoFont.WhiteMediumText().WithColor(color);
CairoFont pressedFont = CairoFont.WhiteMediumText().WithColor(color);
((FontConfig)normalFont).UnscaledFontsize = GuiElement.scaled(16.0);
((FontConfig)pressedFont).UnscaledFontsize = GuiElement.scaled(16.0);
button = new GuiElementTextButton(api, label, normalFont, pressedFont, NoopClick, bounds, EnumButtonStyle.Small)
{
PlaySound = false
};
button.ComposeElements(null, null);
hover = new GuiElementHoverText(api, tooltip, CairoFont.WhiteSmallText(), 220, bounds, null);
hover.SetAutoDisplay(false);
}
public override EnumCalcBoundsResult CalcBounds(TextFlowPath[] flowPath, double currentLineHeight, double offsetX, double lineY, out double nextOffsetX)
{
double width = bounds.fixedWidth;
double height = bounds.fixedHeight;
BoundsPerLine = new[]
{
new LineRectangled(offsetX + GuiElement.scaled(5.0), lineY + GuiElement.scaled(2.0), width, height)
};
nextOffsetX = offsetX + width + GuiElement.scaled(10.0);
return EnumCalcBoundsResult.Continue;
}
public override void RenderInteractiveElements(float deltaTime, double renderX, double renderY, double renderZ)
{
if (!HandbookSpaceCrafting.CanUseCraftingActions(api))
{
hoverSeconds = 0;
return;
}
lastRenderX = renderX;
lastRenderY = renderY;
SetBounds(lastRenderX, lastRenderY);
button.RenderInteractiveElements(deltaTime);
if (bounds.PointInside(api.Input.MouseX, api.Input.MouseY))
{
hoverSeconds += deltaTime;
}
else
{
hoverSeconds = 0;
}
hover.SetVisible(hoverSeconds > 0.75);
hover.RenderInteractiveElements(deltaTime);
}
public override void OnMouseDown(MouseEvent args)
{
if (HandbookSpaceCrafting.CanUseCraftingActions(api))
{
SetBounds(lastRenderX, lastRenderY);
if (args.Button == EnumMouseButton.Left && bounds.PointInside(api.Input.MouseX, api.Input.MouseY))
{
args.Handled = true;
button.OnMouseDown(api, args);
action();
return;
}
button.OnMouseDown(api, args);
}
}
public override void OnMouseUp(MouseEvent args)
{
if (HandbookSpaceCrafting.CanUseCraftingActions(api))
{
SetBounds(lastRenderX, lastRenderY);
button.OnMouseUp(api, args);
}
}
public override void OnMouseMove(MouseEvent args)
{
if (HandbookSpaceCrafting.CanUseCraftingActions(api))
{
SetBounds(lastRenderX, lastRenderY);
button.PlaySound = true;
button.OnMouseMove(api, args);
button.PlaySound = false;
}
}
public override void Dispose()
{
button.Dispose();
hover.Dispose();
}
private static bool NoopClick() => true;
private void SetBounds(double xOffset = 0.0, double yOffset = 0.0)
{
if (BoundsPerLine == null || BoundsPerLine.Length == 0)
{
return;
}
LineRectangled rect = BoundsPerLine[0];
bounds.absInnerWidth = rect.Width;
bounds.absInnerHeight = rect.Height;
bounds.absFixedX = xOffset + rect.X;
bounds.absFixedY = yOffset + rect.Y;
}
}