Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // HoveredItemInfo.cs "$Revision: 930 $" "$Date: 2019-02-02 10:40:38 +0200 (la, 02 helmi 2019) $"
- using SharpDX.DirectInput;
- using System;
- using System.Text;
- using Turbo.Plugins;
- using Turbo.Plugins.Default;
- namespace Turbo.plugins.JarJar
- {
- class HoveredItemInfo : BasePlugin, IKeyEventHandler, IInGameTopPainter
- {
- public Key DumpKey { get; set; } = Key.Multiply;
- public bool UseKeepDecision { get; set; } = false;
- public string[] KeepLabels { get; set; } = {
- /* 0 */ "is null",
- /* 1 */ "is Good",
- /* 2 */ "is Bad",
- /* 3 */ "is Irrelevant",
- /* 4 */ "is Yellow",
- /* 5 */ "is Blue",
- /* 6 */ "is Grey",
- };
- // Item description parameter substitution.
- public string ValueChar { get; set; } = "$";
- public string ValueChar1 { get; set; } = "$1";
- public string ValueChar2 { get; set; } = "$2";
- // Title fonts.
- public IFont LooksGood { get; set; }
- public IFont DefinitelyBad { get; set; }
- // Override whole dialogs.
- public TopLabelWithTitleDecorator GoodDecorator { get; set; }
- public TopLabelWithTitleDecorator BadDecorator { get; set; }
- // Dialog positioning.
- public float leftX { get; set; } = 0.005f; // left pos
- public float topY { get; set; } = 0.005f; // top pos
- public float rightX { get; set; } = 0.995f; // right pos
- public float bottomY { get; set; } = 0.995f; // bottom pos
- public float marginW { get; set; } = 0.01f; // base width w/o content (margin around content)
- public float marginH { get; set; } = 0.03f; // base height
- TopLabelWithTitleDecorator Decorator;
- string titleText;
- string contentText;
- float contentW;
- float contentH;
- IItem hoveredItem;
- string hoveredItemUniqueId;
- StringBuilder builder = new StringBuilder();
- public HoveredItemInfo() { Enabled = true; }
- public override void Load(IController hud)
- {
- base.Load(hud);
- leftX *= Hud.Window.Size.Width;
- rightX *= Hud.Window.Size.Width;
- topY *= Hud.Window.Size.Height;
- bottomY *= Hud.Window.Size.Height;
- marginW *= Hud.Window.Size.Width;
- marginH *= Hud.Window.Size.Height;
- LooksGood = hud.Render.CreateFont("tahoma", 9, 255, 0, 204, 0, true, false, false); // green
- DefinitelyBad = hud.Render.CreateFont("tahoma", 9, 255, 255, 0, 0, true, false, false); // red
- GoodDecorator = new TopLabelWithTitleDecorator(hud)
- {
- BorderBrush = hud.Render.CreateBrush(255, 180, 147, 109, -1),
- BackgroundBrush = hud.Render.CreateBrush(200, 0, 0, 0, 0),
- TextFont = hud.Render.CreateFont("consolas", 7, 255, 255, 255, 255, false, false, false), // white
- TitleFont = LooksGood,
- };
- BadDecorator = new TopLabelWithTitleDecorator(hud)
- {
- BorderBrush = GoodDecorator.BorderBrush,
- BackgroundBrush = GoodDecorator.BackgroundBrush,
- TextFont = GoodDecorator.TextFont,
- TitleFont = DefinitelyBad,
- };
- }
- bool stashVisible;
- public void PaintTopInGame(ClipState clipState)
- {
- if (Hud.Render.UiHidden) return;
- if (clipState == ClipState.Inventory)
- {
- var stash = Hud.Inventory.StashMainUiElement;
- stashVisible = stash.Visible;
- }
- else if (clipState == ClipState.AfterClip)
- {
- hoveredItem = Hud.Inventory.HoveredItem; // Save current hovered item.
- if (hoveredItem != null)
- {
- if (hoveredItem.ItemUniqueId != hoveredItemUniqueId)
- {
- bool isValid = IsValid(hoveredItem);
- hoveredItemUniqueId = hoveredItem.ItemUniqueId;
- Decorator = !UseKeepDecision || hoveredItem.KeepDecision == ItemKeepDecision.LooksGood
- ? GoodDecorator
- : BadDecorator;
- try
- {
- titleText = GetItemTitle(hoveredItem, isValid);
- contentText = GetItemDetails(hoveredItem);
- }
- catch (Exception x)
- {
- titleText = "Error";
- contentText = x.Message;
- }
- var layout = Decorator.TextFont.GetTextLayout(contentText);
- if (isValid)
- {
- contentW = marginW + layout.Metrics.Width; // Consider content only for normal items.
- }
- else
- {
- contentW = marginW + Math.Max(layout.Metrics.Width, Decorator.TitleFont.GetTextLayout(titleText).Metrics.Width);
- }
- contentH = marginH + layout.Metrics.Height;
- }
- }
- else if (contentText != null)
- {
- hoveredItemUniqueId = null;
- Decorator = null;
- }
- }
- // Draw Actual dialog if available!
- if (Decorator != null)
- {
- if (stashVisible)
- {
- Decorator.Paint(rightX - contentW, bottomY - contentH, contentW, contentH, contentText, titleText); // bottom right
- }
- else
- {
- Decorator.Paint(leftX, topY, contentW, contentH, contentText, titleText); // top left
- }
- }
- }
- public void OnKeyEvent(IKeyEvent keyEvent)
- {
- if (keyEvent.IsPressed && keyEvent.Key == DumpKey && hoveredItem != null)
- {
- Hud.Debug(string.Format("ITEM NameEnglish \"{0}\"\r\n{1}\r\n{2}", hoveredItem.SnoItem.NameEnglish, titleText, contentText));
- }
- }
- string GetItemTitle(IItem item, bool isValid)
- {
- if (isValid)
- {
- string itemType = item.SnoItem.MainGroupCode == "pants" || string.Compare(item.SnoItem.MainGroupCode, item.SnoItem.SnoItemType.Code, true) == 0
- ? item.SnoItem.SnoItemType.Code
- : item.SnoItem.MainGroupCode + " " + item.SnoItem.SnoItemType.Code;
- return string.Format("{0} - {1} {2} ({3:0}%)",
- hoveredItem.FullNameEnglish,
- itemType,
- UseKeepDecision ? GetKeepDecision(item) : "",
- item.Perfection);
- }
- return hoveredItem.FullNameEnglish;
- }
- string GetItemDetails(IItem item)
- {
- builder.Clear();
- builder
- .AppendFormat("{0,-51}", item.SnoItem.Code)
- .AppendFormat(" sno={0,-17}", item.SnoItem.Sno);
- if (item.SetSno != uint.MaxValue)
- {
- builder.AppendFormat(" set={0}", item.SetSno);
- }
- builder
- .AppendLine()
- .AppendFormat("#{0,-37}", item.Perfections == null ? 0 : item.Perfections.Length)
- .AppendFormat("{0,11}", GetAncientRank(item));
- if (item.Perfections != null)
- {
- foreach (IItemPerfection perfection in item.Perfections)
- {
- double rawRank = calculateRank(perfection);
- string precision = perfection.Attribute.ValueType == AttributeValueType._int ? "G0" : "G7";
- string range = string.Format("[{0}-{1}]", perfection.Min.ToString(precision), perfection.Max.ToString(precision));
- string curValue = Math.Round(perfection.Cur, 3).ToString(precision);
- string modifier = getModifier(perfection);
- builder
- .AppendLine()
- .AppendFormat("{0,-37}", perfection.Attribute.Code)
- .AppendFormat("{0,13}", range)
- .AppendFormat("{0,6}", curValue)
- .AppendFormat("{0,5}", getRankValue(perfection))
- .AppendFormat(" mod={0,-8}", modifier)
- .Append(GetAttributeDescription(perfection));
- }
- }
- return builder.ToString();
- }
- const uint MOD_NaturalConstant = 1048575;
- static char GetAncientRank(IItem item)
- {
- return item == null || item.AncientRank < 1 ? ' ' : item.AncientRank == 1 ? 'A' : 'P';
- }
- static string getRankValue(IItemPerfection perfection)
- {
- if (perfection.Min == perfection.Max) return "~ ";
- return string.Format("{0:0}%", calculateRank(perfection));
- }
- public string GetKeepDecision(IItem item)
- {
- if (item == null) return KeepLabels[0];
- if (item.IsLegendary)
- {
- return item.KeepDecision == ItemKeepDecision.LooksGood ? KeepLabels[1]
- : item.KeepDecision == ItemKeepDecision.DefinitelyBad ? KeepLabels[2]
- : KeepLabels[3];
- }
- if (item.IsRare) return KeepLabels[4];
- if (item.IsMagic) return KeepLabels[5];
- return KeepLabels[6];
- }
- public static bool IsValid(IItem item)
- {
- ISnoItem snoItem = item.SnoItem;
- if (snoItem == null || !(snoItem.Kind == ItemKind.loot || snoItem.Kind == ItemKind.craft)) return false;
- return
- snoItem.MainGroupCode == "1h" ||
- snoItem.MainGroupCode == "2h" ||
- snoItem.MainGroupCode == "amulet" ||
- snoItem.MainGroupCode == "belt" ||
- snoItem.MainGroupCode == "boots" ||
- snoItem.MainGroupCode == "bracers" ||
- snoItem.MainGroupCode == "chestarmor" ||
- snoItem.MainGroupCode == "crusadershield" ||
- snoItem.MainGroupCode == "follower" ||
- snoItem.MainGroupCode == "gloves" ||
- snoItem.MainGroupCode == "helm" ||
- snoItem.MainGroupCode == "mojo" ||
- snoItem.MainGroupCode == "necromanceroffhand" ||
- snoItem.MainGroupCode == "pants" ||
- snoItem.MainGroupCode == "quiver" ||
- snoItem.MainGroupCode == "ring" ||
- snoItem.MainGroupCode == "shield" ||
- snoItem.MainGroupCode == "shoulders" ||
- snoItem.MainGroupCode == "source" ||
- snoItem.MainGroupCode == "staffofcow";
- }
- static string getModifier(IItemPerfection perfection)
- {
- return perfection.Modifier == 1048575 // typical/default attribute modifier in perfections.
- ? "-"
- : perfection.Modifier.ToString();
- }
- string GetAttributeDescription(IItemPerfection perfection)
- {
- string description = perfection.Attribute.GetDescription(perfection.Modifier);
- if (description == null)
- {
- description = "null";
- }
- return description
- .Replace("{VALUE}", ValueChar)
- .Replace("{VALUE1}", ValueChar1)
- .Replace("{VALUE2}", ValueChar2);
- }
- static double calculateRank(IItemPerfection perfection)
- {
- if (perfection == null) return 0;
- double cur = perfection.Cur;
- double min = perfection.Min;
- double max = perfection.Max;
- if (perfection.Attribute.ValueType == AttributeValueType._float)
- {
- cur = Math.Round(perfection.Cur, 3);
- min = Math.Round(perfection.Min, 3);
- max = Math.Round(perfection.Max, 3);
- }
- else
- {
- cur = perfection.Cur;
- min = perfection.Min;
- max = perfection.Max;
- }
- return calculate(cur, min, max);
- }
- static double calculate(double cur, double min, double max)
- {
- if (cur > min && cur < max)
- {
- // Current value relative distance % from Min to Max (0-100).
- return (((cur - min) / (max - min)) * 100.0);
- }
- // Edge cases and for example: "Sockets [1-1]=1" or "Crossbow [1-1]=2"
- return !(cur < max) ? 100.0 : 0.0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement