Advertisement
jarppaaja

HoveredItemInfo rev 930

Feb 2nd, 2019
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.03 KB | None | 0 0
  1. // HoveredItemInfo.cs "$Revision: 930 $" "$Date: 2019-02-02 10:40:38 +0200 (la, 02 helmi 2019) $"
  2. using SharpDX.DirectInput;
  3. using System;
  4. using System.Text;
  5. using Turbo.Plugins;
  6. using Turbo.Plugins.Default;
  7.  
  8. namespace Turbo.plugins.JarJar
  9. {
  10.     class HoveredItemInfo : BasePlugin, IKeyEventHandler, IInGameTopPainter
  11.     {
  12.         public Key DumpKey { get; set; } = Key.Multiply;
  13.  
  14.         public bool UseKeepDecision { get; set; } = false;
  15.         public string[] KeepLabels { get; set; } = {
  16.             /* 0 */ "is null",
  17.             /* 1 */ "is Good",
  18.             /* 2 */ "is Bad",
  19.             /* 3 */ "is Irrelevant",
  20.             /* 4 */ "is Yellow",
  21.             /* 5 */ "is Blue",
  22.             /* 6 */ "is Grey",
  23.         };
  24.  
  25.         // Item description parameter substitution.
  26.         public string ValueChar { get; set; } = "$";
  27.         public string ValueChar1 { get; set; } = "$1";
  28.         public string ValueChar2 { get; set; } = "$2";
  29.  
  30.         // Title fonts.
  31.         public IFont LooksGood { get; set; }
  32.         public IFont DefinitelyBad { get; set; }
  33.  
  34.         // Override whole dialogs.
  35.         public TopLabelWithTitleDecorator GoodDecorator { get; set; }
  36.         public TopLabelWithTitleDecorator BadDecorator { get; set; }
  37.  
  38.         // Dialog positioning.
  39.         public float leftX { get; set; } = 0.005f;      // left pos
  40.         public float topY { get; set; } = 0.005f;       // top pos
  41.         public float rightX { get; set; } = 0.995f;     // right pos
  42.         public float bottomY { get; set; } = 0.995f;    // bottom pos
  43.         public float marginW { get; set; } = 0.01f;     // base width w/o content (margin around content)
  44.         public float marginH { get; set; } = 0.03f;     // base height
  45.  
  46.         TopLabelWithTitleDecorator Decorator;
  47.         string titleText;
  48.         string contentText;
  49.         float contentW;
  50.         float contentH;
  51.  
  52.         IItem hoveredItem;
  53.         string hoveredItemUniqueId;
  54.         StringBuilder builder = new StringBuilder();
  55.  
  56.         public HoveredItemInfo() { Enabled = true; }
  57.  
  58.         public override void Load(IController hud)
  59.         {
  60.             base.Load(hud);
  61.  
  62.             leftX *= Hud.Window.Size.Width;
  63.             rightX *= Hud.Window.Size.Width;
  64.             topY *= Hud.Window.Size.Height;
  65.             bottomY *= Hud.Window.Size.Height;
  66.             marginW *= Hud.Window.Size.Width;
  67.             marginH *= Hud.Window.Size.Height;
  68.  
  69.             LooksGood = hud.Render.CreateFont("tahoma", 9, 255, 0, 204, 0, true, false, false);             // green
  70.             DefinitelyBad = hud.Render.CreateFont("tahoma", 9, 255, 255, 0, 0, true, false, false);         // red
  71.  
  72.             GoodDecorator = new TopLabelWithTitleDecorator(hud)
  73.             {
  74.                 BorderBrush = hud.Render.CreateBrush(255, 180, 147, 109, -1),
  75.                 BackgroundBrush = hud.Render.CreateBrush(200, 0, 0, 0, 0),
  76.                 TextFont = hud.Render.CreateFont("consolas", 7, 255, 255, 255, 255, false, false, false),   // white
  77.                 TitleFont = LooksGood,
  78.             };
  79.             BadDecorator = new TopLabelWithTitleDecorator(hud)
  80.             {
  81.                 BorderBrush = GoodDecorator.BorderBrush,
  82.                 BackgroundBrush = GoodDecorator.BackgroundBrush,
  83.                 TextFont = GoodDecorator.TextFont,
  84.                 TitleFont = DefinitelyBad,
  85.             };
  86.         }
  87.  
  88.         bool stashVisible;
  89.         public void PaintTopInGame(ClipState clipState)
  90.         {
  91.             if (Hud.Render.UiHidden) return;
  92.             if (clipState == ClipState.Inventory)
  93.             {
  94.                 var stash = Hud.Inventory.StashMainUiElement;
  95.                 stashVisible = stash.Visible;
  96.             }
  97.             else if (clipState == ClipState.AfterClip)
  98.             {
  99.                 hoveredItem = Hud.Inventory.HoveredItem;    // Save current hovered item.
  100.                 if (hoveredItem != null)
  101.                 {
  102.                     if (hoveredItem.ItemUniqueId != hoveredItemUniqueId)
  103.                     {
  104.                         bool isValid = IsValid(hoveredItem);
  105.                         hoveredItemUniqueId = hoveredItem.ItemUniqueId;
  106.                         Decorator = !UseKeepDecision || hoveredItem.KeepDecision == ItemKeepDecision.LooksGood
  107.                             ? GoodDecorator
  108.                             : BadDecorator;
  109.                         try
  110.                         {
  111.                             titleText = GetItemTitle(hoveredItem, isValid);
  112.                             contentText = GetItemDetails(hoveredItem);
  113.                         }
  114.                         catch (Exception x)
  115.                         {
  116.                             titleText = "Error";
  117.                             contentText = x.Message;
  118.                         }
  119.                         var layout = Decorator.TextFont.GetTextLayout(contentText);
  120.                         if (isValid)
  121.                         {
  122.                             contentW = marginW + layout.Metrics.Width;     // Consider content only for normal items.
  123.                         }
  124.                         else
  125.                         {
  126.                             contentW = marginW + Math.Max(layout.Metrics.Width, Decorator.TitleFont.GetTextLayout(titleText).Metrics.Width);
  127.                         }
  128.                         contentH = marginH + layout.Metrics.Height;
  129.                     }
  130.                 }
  131.                 else if (contentText != null)
  132.                 {
  133.                     hoveredItemUniqueId = null;
  134.                     Decorator = null;
  135.                 }
  136.             }
  137.             // Draw Actual dialog if available!
  138.             if (Decorator != null)
  139.             {
  140.                 if (stashVisible)
  141.                 {
  142.                     Decorator.Paint(rightX - contentW, bottomY - contentH, contentW, contentH, contentText, titleText);     // bottom right
  143.                 }
  144.                 else
  145.                 {
  146.                     Decorator.Paint(leftX, topY, contentW, contentH, contentText, titleText);                   // top left
  147.                 }
  148.             }
  149.         }
  150.  
  151.         public void OnKeyEvent(IKeyEvent keyEvent)
  152.         {
  153.             if (keyEvent.IsPressed && keyEvent.Key == DumpKey && hoveredItem != null)
  154.             {
  155.                 Hud.Debug(string.Format("ITEM NameEnglish \"{0}\"\r\n{1}\r\n{2}", hoveredItem.SnoItem.NameEnglish, titleText, contentText));
  156.             }
  157.         }
  158.  
  159.         string GetItemTitle(IItem item, bool isValid)
  160.         {
  161.             if (isValid)
  162.             {
  163.                 string itemType = item.SnoItem.MainGroupCode == "pants" || string.Compare(item.SnoItem.MainGroupCode, item.SnoItem.SnoItemType.Code, true) == 0
  164.                     ? item.SnoItem.SnoItemType.Code
  165.                     : item.SnoItem.MainGroupCode + " " + item.SnoItem.SnoItemType.Code;
  166.                 return string.Format("{0} - {1} {2} ({3:0}%)",
  167.                     hoveredItem.FullNameEnglish,
  168.                     itemType,
  169.                     UseKeepDecision ? GetKeepDecision(item) : "",
  170.                     item.Perfection);
  171.             }
  172.             return hoveredItem.FullNameEnglish;
  173.         }
  174.  
  175.         string GetItemDetails(IItem item)
  176.         {
  177.             builder.Clear();
  178.             builder
  179.                 .AppendFormat("{0,-51}", item.SnoItem.Code)
  180.                 .AppendFormat("  sno={0,-17}", item.SnoItem.Sno);
  181.             if (item.SetSno != uint.MaxValue)
  182.             {
  183.                 builder.AppendFormat(" set={0}", item.SetSno);
  184.             }
  185.             builder
  186.                 .AppendLine()
  187.                 .AppendFormat("#{0,-37}", item.Perfections == null ? 0 : item.Perfections.Length)
  188.                 .AppendFormat("{0,11}", GetAncientRank(item));
  189.             if (item.Perfections != null)
  190.             {
  191.                 foreach (IItemPerfection perfection in item.Perfections)
  192.                 {
  193.                     double rawRank = calculateRank(perfection);
  194.                     string precision = perfection.Attribute.ValueType == AttributeValueType._int ? "G0" : "G7";
  195.                     string range = string.Format("[{0}-{1}]", perfection.Min.ToString(precision), perfection.Max.ToString(precision));
  196.                     string curValue = Math.Round(perfection.Cur, 3).ToString(precision);
  197.                     string modifier = getModifier(perfection);
  198.                     builder
  199.                         .AppendLine()
  200.                         .AppendFormat("{0,-37}", perfection.Attribute.Code)
  201.                         .AppendFormat("{0,13}", range)
  202.                         .AppendFormat("{0,6}", curValue)
  203.                         .AppendFormat("{0,5}", getRankValue(perfection))
  204.                         .AppendFormat("  mod={0,-8}", modifier)
  205.                         .Append(GetAttributeDescription(perfection));
  206.                 }
  207.             }
  208.             return builder.ToString();
  209.         }
  210.  
  211.         const uint MOD_NaturalConstant = 1048575;    
  212.  
  213.         static char GetAncientRank(IItem item)
  214.         {
  215.             return item == null || item.AncientRank < 1 ? ' ' : item.AncientRank == 1 ? 'A' : 'P';
  216.         }
  217.  
  218.         static string getRankValue(IItemPerfection perfection)
  219.         {
  220.             if (perfection.Min == perfection.Max) return "~ ";
  221.             return string.Format("{0:0}%", calculateRank(perfection));
  222.         }
  223.  
  224.         public string GetKeepDecision(IItem item)
  225.         {
  226.             if (item == null) return KeepLabels[0];
  227.             if (item.IsLegendary)
  228.             {
  229.                 return item.KeepDecision == ItemKeepDecision.LooksGood ? KeepLabels[1]
  230.                       : item.KeepDecision == ItemKeepDecision.DefinitelyBad ? KeepLabels[2]
  231.                       : KeepLabels[3];
  232.             }
  233.             if (item.IsRare) return KeepLabels[4];
  234.             if (item.IsMagic) return KeepLabels[5];
  235.             return KeepLabels[6];
  236.         }
  237.  
  238.         public static bool IsValid(IItem item)
  239.         {
  240.             ISnoItem snoItem = item.SnoItem;
  241.             if (snoItem == null || !(snoItem.Kind == ItemKind.loot || snoItem.Kind == ItemKind.craft)) return false;
  242.             return
  243.                 snoItem.MainGroupCode == "1h" ||
  244.                 snoItem.MainGroupCode == "2h" ||
  245.                 snoItem.MainGroupCode == "amulet" ||
  246.                 snoItem.MainGroupCode == "belt" ||
  247.                 snoItem.MainGroupCode == "boots" ||
  248.                 snoItem.MainGroupCode == "bracers" ||
  249.                 snoItem.MainGroupCode == "chestarmor" ||
  250.                 snoItem.MainGroupCode == "crusadershield" ||
  251.                 snoItem.MainGroupCode == "follower" ||
  252.                 snoItem.MainGroupCode == "gloves" ||
  253.                 snoItem.MainGroupCode == "helm" ||
  254.                 snoItem.MainGroupCode == "mojo" ||
  255.                 snoItem.MainGroupCode == "necromanceroffhand" ||
  256.                 snoItem.MainGroupCode == "pants" ||
  257.                 snoItem.MainGroupCode == "quiver" ||
  258.                 snoItem.MainGroupCode == "ring" ||
  259.                 snoItem.MainGroupCode == "shield" ||
  260.                 snoItem.MainGroupCode == "shoulders" ||
  261.                 snoItem.MainGroupCode == "source" ||
  262.                 snoItem.MainGroupCode == "staffofcow";
  263.         }
  264.  
  265.         static string getModifier(IItemPerfection perfection)
  266.         {
  267.             return perfection.Modifier == 1048575 // typical/default attribute modifier in perfections.
  268.                 ? "-"
  269.                 : perfection.Modifier.ToString();
  270.         }
  271.  
  272.         string GetAttributeDescription(IItemPerfection perfection)
  273.         {
  274.             string description = perfection.Attribute.GetDescription(perfection.Modifier);
  275.             if (description == null)
  276.             {
  277.                 description = "null";
  278.             }
  279.             return description
  280.                 .Replace("{VALUE}", ValueChar)
  281.                 .Replace("{VALUE1}", ValueChar1)
  282.                 .Replace("{VALUE2}", ValueChar2);
  283.         }
  284.  
  285.         static double calculateRank(IItemPerfection perfection)
  286.         {
  287.             if (perfection == null) return 0;
  288.             double cur = perfection.Cur;
  289.             double min = perfection.Min;
  290.             double max = perfection.Max;
  291.             if (perfection.Attribute.ValueType == AttributeValueType._float)
  292.             {
  293.                 cur = Math.Round(perfection.Cur, 3);
  294.                 min = Math.Round(perfection.Min, 3);
  295.                 max = Math.Round(perfection.Max, 3);
  296.             }
  297.             else
  298.             {
  299.                 cur = perfection.Cur;
  300.                 min = perfection.Min;
  301.                 max = perfection.Max;
  302.             }
  303.             return calculate(cur, min, max);
  304.         }
  305.  
  306.         static double calculate(double cur, double min, double max)
  307.         {
  308.             if (cur > min && cur < max)
  309.             {
  310.                 // Current value relative distance % from Min to Max (0-100).
  311.                 return (((cur - min) / (max - min)) * 100.0);
  312.             }
  313.             // Edge cases and for example: "Sockets [1-1]=1" or "Crossbow [1-1]=2"
  314.             return !(cur < max) ? 100.0 : 0.0;
  315.         }
  316.     }
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement