1 using System.Collections.Generic;
13 using Microsoft.Xna.Framework.Graphics;
19 namespace Loot.UI.Tabs.CraftingTab
24 bool AcceptsItem(Item item);
26 void OverrideSlottedItem(Item newItem);
35 public override int GetPageHeight() => 100;
37 internal abstract CraftingComponentButton GetComponentButton();
40 protected virtual Dictionary<string, object> CustomData =>
new Dictionary<string, object>
47 public virtual bool AcceptsItem(Item item)
49 return ItemButton?.CanTakeItem(item) ??
false;
52 internal CraftingComponentButton ComponentButton;
53 internal GuiCraftItemButton ItemButton;
55 internal CraftingComponentSelector<T> ComponentSelector;
57 protected GuiImageButton _craftButton;
58 protected GuiTextPanel[] _modifierPanels;
60 public override void OnInitialize()
64 RollingStrategyProperties =
new RollingStrategyProperties();
66 ComponentButton = GetComponentButton();
67 ComponentButton.OnItemChange += item =>
69 ComponentSelector.CalculateAvailableComponents(ItemButton.Item);
70 if (!ItemButton.Item.IsAir && !IsStrategyAllowed())
72 ComponentButton.SetLink(null);
75 TabFrame.Append(ComponentButton);
77 ItemButton =
new GuiCraftItemButton(
78 GuiButton.ButtonType.Parchment,
79 hintTexture: ModContent.GetTexture(
"Terraria/Item_24"),
80 hintText:
"Place an item here" 82 ItemButton.CanTakeItemAction += IsStrategyAllowed;
83 ItemButton.PreOnClickAction += () =>
85 if (!ItemButton.Item.IsAir)
90 ItemButton.OnItemChange += item =>
92 ComponentSelector.CalculateAvailableComponents(ItemButton.Item);
93 if (!ComponentSelector.IsAvailable(ComponentButton.Item.type))
95 ComponentButton.SetLink(null);
102 UpdateModifiersInGui();
104 TabFrame.Append(ItemButton.Below(ComponentButton));
106 _modifierPanels =
new GuiTextPanel[4];
107 for (
int i = 0; i < 4; i++)
109 _modifierPanels[i] =
new GuiTextPanel();
110 var panel = _modifierPanels[i].RightOf(ComponentButton);
113 panel.Below(_modifierPanels[i - 1]);
116 TabFrame.Append(panel);
119 Texture2D craftTexture = ModContent.GetTexture(
"Terraria/UI/Craft_Toggle_3");
120 _craftButton =
new GuiImageButton(GuiButton.ButtonType.StoneOuterBevel, craftTexture);
121 _craftButton.OnClick += (evt, element) =>
123 ComponentSelector.CalculateAvailableComponents(ItemButton.Item);
124 if (!ComponentSelector.IsAvailable(ComponentButton.Item.type))
126 ComponentButton.SetLink(null);
129 HandleCraftButtonClick(evt, element);
132 TabFrame.Append(_craftButton.Below(ItemButton));
134 ComponentSelector =
new CraftingComponentSelector<T>();
135 ComponentSelector.OnComponentClick += link =>
137 if (link == null)
return false;
138 ComponentButton.SetLink(link);
141 Height.Set(Height.Pixels + ComponentSelector.Height.Pixels, 0);
142 Append(ComponentSelector);
145 private bool CraftRequirementsMet()
147 return ComponentButton.Link?.Component.modItem is
MagicalCube 148 && ItemButton.CanTakeItem(ItemButton.Item)
149 && ComponentButton.CanTakeItem(ComponentButton.Item)
150 && IsStrategyAllowed();
154 private bool IsStrategyAllowed(Item overrideItem = null)
156 RollingStrategyProperties =
new RollingStrategyProperties();
157 var ctx = GetContext(overrideItem ?? ItemButton.Item);
158 var strategy = ctx.Strategy;
160 return preRolledLines.All(x => x.CanRoll(ctx));
163 public override void OnShow()
165 ComponentSelector?.CalculateAvailableComponents(ItemButton.Item);
166 UpdateModifiersInGui();
173 Method = CraftMethod,
175 Player = Main.LocalPlayer,
176 CustomData = this.CustomData,
177 Strategy = ComponentButton.GetRollingStrategy(ItemButton.Item, RollingStrategyProperties)
181 private void HandleCraftButtonClick(UIMouseEvent evt, UIElement listeningElement)
183 if (!CraftRequirementsMet())
185 SoundHelper.PlayCustomSound(SoundHelper.SoundType.Decline);
189 RollingStrategyProperties =
new RollingStrategyProperties();
191 if (ComponentButton.Item.stack <= 0)
193 SoundHelper.PlayCustomSound(SoundHelper.SoundType.Decline);
194 ComponentButton.SetLink(null);
198 var item = GetClonedItem(ItemButton.Item);
200 var strategy = ComponentButton.GetRollingStrategy(item, RollingStrategyProperties);
205 SoundHelper.PlayCustomSound(SoundHelper.SoundType.Decline);
210 var context = GetContext(item);
215 item.UpdateModifiers(rolled);
217 strategy.PlaySoundEffect(item);
218 ItemButton.Item = item;
221 UpdateModifiersInGui();
224 private Item GetClonedItem(Item toClone)
226 var clone =
new Item();
227 clone.netDefaults(toClone.type);
229 clone = clone.CloneWithModdedDataFrom(ItemButton.Item);
231 if (ItemButton.Item.prefix > 0)
233 clone.Prefix(toClone.prefix);
236 clone.stack = toClone.stack;
240 public void OverrideSlottedItem(Item newItem)
242 ItemButton.ChangeItem(0, newItem.Clone());
244 UpdateModifiersInGui();
245 Main.PlaySound(SoundID.Grab);
248 private void UpdateModifiersInGui()
250 foreach (var panel
in _modifierPanels)
255 if (ItemButton.Item.IsAir || _modifierPanels == null)
262 IEnumerable<ModifierTooltipLine[]> GetTooltipLines(Item item)
264 .Select(x => x.GetTooltip().Build().ToArray())
266 .Where(x => x != null);
268 foreach (var lines
in GetTooltipLines(ItemButton.Item))
270 string line = lines.Aggregate(
"", (current, tooltipLine) => current + $
"{tooltipLine.Text} ");
271 line = line.TrimEnd();
272 var measure = Main.fontMouseText.MeasureString(line);
273 if (measure.X >= _modifierPanels[i].Width.Pixels + SPACING * 4)
275 _modifierPanels[i].SetHoverText(line);
276 line = Main.fontMouseText.CreateWrappedText(line, _modifierPanels[i].Width.Pixels)
277 .Split(
'\n')[0] +
"...";
281 _modifierPanels[i].SetHoverText(null);
284 _modifierPanels[i].UpdateText(line);
291 private void ConsumeComponents()
293 ComponentButton.Link.Component.stack--;
294 ComponentButton.Item.stack--;
295 if (ComponentButton.Link.Component.stack <= 0)
297 ComponentButton.SetLink(null);
299 ComponentSelector.CalculateAvailableComponents(ItemButton.Item);
302 public override void GiveBackItems()
304 if (!ItemButton?.Item?.IsAir ??
false)
307 ItemButton.Item.noGrabDelay = 0;
308 Main.LocalPlayer.GetItem(Main.myPlayer, GetClonedItem(ItemButton.Item));
309 ItemButton.Item.TurnToAir();
Defines a context in which a Modifier might be rolled Which fields are available (not null) depends o...
Defines a "Null" modifier which represents a pool with no modifiers Cannot be rolled normally ...
static NullModifierPool INSTANCE
Defines properties that will be used when an item is being rolled in a IRollingStrategy<T> These can ...
ModifierContextMethod
Defines a method for a context in which a Modifier might be rolled Used in ModifierContext ...
Defines an item that may be modified by modifiers from mods
Defines a magical cube A magical cube is used to change modifiers on an item
static ModifierPool GetPool(ModifierContext context)
static List< Modifier > GetActivePool(Item item)
static LootModItem GetInfo(Item item)
FiniteModifierPool Modifiers