Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // HoveredItemInfo.cs "$Revision: 941 $" "$Date: 2019-02-03 15:38:54 +0200 (su, 03 helmi 2019) $"
- using SharpDX.DirectInput;
- using System;
- using System.Text;
- using System.Drawing;
- using Turbo.Plugins;
- using Turbo.Plugins.Default;
- using System.Collections.Generic;
- using SharpDX.DirectWrite;
- namespace Turbo.plugins.JarJar
- {
- class HoveredItemInfo : BasePlugin, IKeyEventHandler, IInGameTopPainter
- {
- public Key ToggleDialogKey { get; set; } = Key.LeftControl; // Key to toggle Dialog on/off.
- 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";
- // Default fonts.
- public IFont LooksGood { get; set; }
- public IFont DefinitelyBad { get; set; }
- public IFont TextFont { get; set; }
- // Rank fonts.
- public IFont TextFontMissing;
- public IFont[] rankFont;
- public ColorDialog Dialog { 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
- string titleText;
- string titleQuality;
- List<string> contentLines;
- IItem hoveredItem;
- string hoveredItemUniqueId;
- StringBuilder builder = new StringBuilder();
- PaintData dialogData;
- SizeF dialogSize;
- bool stashVisible;
- bool dialogVisible = true; // For dialog toggle.
- 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, 204, 102, 0, true, false, false); // brownish
- TextFont = hud.Render.CreateFont("consolas", 7, 255, 255, 255, 255, false, false, false); // white
- int alpha80 = (int)(256 * 0.80);
- TextFontMissing = Hud.Render.CreateFont("consolas", 7, alpha80, 0xe0, 0xe0, 0xe0, true, false, false); // Light grey with alpha.
- int[][] rgb = new[]
- {
- // Orange "rank" slider.
- new int[] { 0x00, 0xcc, 0x00 }, // Greenish.
- new int[] { 0xcc, 0x66, 0x00 },
- new int[] { 0xf0, 0x80, 0x00 },
- new int[] { 0xff, 0x99, 0x33 },
- new int[] { 0xff, 0xb2, 0x66 },
- new int[] { 0xff, 0xcc, 0x99 },
- new int[] { 0xff, 0xcc, 0xcc }, // Pinkish.
- };
- rankFont = new IFont[rgb.Length];
- for (int i = 0; i < rgb.Length; ++i)
- {
- rankFont[i] = Hud.Render.CreateFont("consolas", 7, 255, rgb[i][0], rgb[i][1], rgb[i][2], true, false, false);
- }
- Dialog = new ColorDialog(hud)
- {
- BorderBrush = hud.Render.CreateBrush(255, 180, 147, 109, -1),
- BackgroundBrush = hud.Render.CreateBrush(225, 0, 0, 0, 0),
- };
- dialogData = new PaintData()
- {
- LeftHeader = new Tuple<string, IFont>("Left", LooksGood),
- RightHeader = new Tuple<string, IFont>("Right", DefinitelyBad),
- Lines = new List<System.Tuple<string, IFont>>(),
- };
- }
- IFont getLineFont(int rank)
- {
- if (rank >= 100) return rankFont[0];
- if (rank >= 90) return rankFont[1];
- if (rank >= 80) return rankFont[2];
- if (rank >= 60) return rankFont[3];
- if (rank >= 40) return rankFont[4];
- if (rank >= 20) return rankFont[5];
- if (rank >= 00) return rankFont[6];
- if (rank == -1) return TextFont;
- return TextFontMissing;
- }
- public void PaintTopInGame(ClipState clipState)
- {
- if (Hud.Render.UiHidden) return;
- if (clipState == ClipState.Inventory)
- {
- var stash = Hud.Inventory.StashMainUiElement;
- stashVisible = stash.Visible;
- return;
- }
- if (!Hud.Game.IsInTown || Hud.Render.UiHidden) return;
- if (clipState != ClipState.AfterClip) return;
- hoveredItem = Hud.Inventory.HoveredItem; // Save current hovered item.
- if (hoveredItem != null)
- {
- if (hoveredItem.Unidentified || hoveredItem.Location == ItemLocation.Merchant)
- {
- reset(); // Can't or won't show.
- }
- else if (hoveredItem.ItemUniqueId != hoveredItemUniqueId)
- {
- bool isValid = IsValid(hoveredItem);
- hoveredItemUniqueId = hoveredItem.ItemUniqueId;
- List<int> quality = new List<int>();
- try
- {
- titleText = GetItemTitle(hoveredItem, isValid);
- titleQuality = GetItemQuality(hoveredItem, isValid);
- contentLines = GetItemDetails(hoveredItem, ref quality);
- }
- catch (Exception x)
- {
- titleText = "Error";
- titleQuality = "";
- contentLines.Clear();
- contentLines.Add(x.Message);
- }
- dialogData.LeftHeader = new Tuple<string, IFont>(titleText, LooksGood);
- dialogData.RightHeader = new Tuple<string, IFont>(titleQuality, DefinitelyBad);
- dialogData.Lines.Clear();
- for (int i = 0; i < contentLines.Count; ++i)
- {
- string line = contentLines[i];
- dialogData.Lines.Add(new Tuple<string, IFont>(line, getLineFont(quality[i])));
- }
- dialogSize = Dialog.CalculateLayout(dialogData);
- }
- }
- else if (contentLines != null)
- {
- reset();
- }
- // Draw Actual dialog if available!
- if (hoveredItem != null)
- {
- if (stashVisible)
- {
- // bottom right.
- float curX = rightX - dialogSize.Width;
- float curY = bottomY - dialogSize.Height;
- if (Dialog.Location.X != curX || Dialog.Location.Y != curY)
- {
- Dialog.Location = new PointF(curX, curY);
- }
- Dialog.RenderDialog(dialogSize);
- }
- else
- {
- // top left
- if (Dialog.Location.X != leftX)
- {
- Dialog.Location = new PointF(leftX, topY);
- }
- Dialog.RenderDialog(dialogSize);
- }
- }
- }
- public void OnKeyEvent(IKeyEvent keyEvent)
- {
- if (keyEvent.IsPressed)
- {
- if (keyEvent.Key == DumpKey)
- {
- if (hoveredItem != null)
- {
- Hud.Debug(string.Format("ITEM NameEnglish \"{0}\"\r\n{1} {2}\r\n{3}",
- hoveredItem.SnoItem.NameEnglish,
- titleText, titleQuality,
- string.Join("\r\n", contentLines)));
- }
- }
- }
- }
- void reset()
- {
- hoveredItem = null;
- hoveredItemUniqueId = null;
- }
- string GetItemTitle(IItem item, bool isValid)
- {
- return hoveredItem.FullNameEnglish;
- }
- string GetItemQuality(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:0}%)",
- itemType,
- UseKeepDecision ? GetKeepDecision(item) : "",
- item.Perfection);
- }
- return "";
- }
- List<string> GetItemDetails(IItem item, ref List<int> quality)
- {
- List<string> lines = new List<string>();
- builder.Clear();
- builder
- .AppendFormat("{0,-37}", item.SnoItem.Code)
- .AppendFormat("{0,12}", GetAncientRank(item))
- .AppendFormat(" sno={0,-11}", item.SnoItem.Sno);
- if (item.SetSno != uint.MaxValue)
- {
- builder.AppendFormat(" set={0,-11}", item.SetSno);
- }
- builder.Append(' ').Append(item.Location);
- lines.Add(builder.ToString());
- quality.Add(-1);
- builder.Clear();
- builder
- .AppendFormat("#{0}", item.Perfections == null ? 0 : item.Perfections.Length);
- lines.Add(builder.ToString());
- quality.Add(-1);
- builder.Clear();
- 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);
- double rankValue = calculateRank(perfection);
- builder
- .AppendFormat("{0,-37}", perfection.Attribute.Code)
- .AppendFormat("{0,13}", range)
- .AppendFormat("{0,6}", curValue)
- .AppendFormat("{0,5}", formatRankValue(perfection, rankValue))
- .AppendFormat(" mod={0,-8}", modifier)
- .Append(GetAttributeDescription(perfection));
- lines.Add(builder.ToString());
- quality.Add((int)rankValue);
- builder.Clear();
- }
- }
- if (item.SocketCount > 0)
- {
- builder
- .AppendFormat("sockets = {0}", item.SocketCount);
- lines.Add(builder.ToString());
- quality.Add(-1);
- builder.Clear();
- }
- return lines;
- }
- const uint MOD_NaturalConstant = 1048575;
- static char GetAncientRank(IItem item)
- {
- return item == null || item.AncientRank < 1 ? ' ' : item.AncientRank == 1 ? 'A' : 'P';
- }
- static string formatRankValue(IItemPerfection perfection, double rankValue)
- {
- if (perfection.Min == perfection.Max) return "~ ";
- return string.Format("{0:0}%", rankValue);
- }
- 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;
- }
- }
- // this is not a plugin, just a helper class to display a dialog on the screen.
- public class PaintData
- {
- public Tuple<string, IFont> LeftHeader; // Left header
- public Tuple<string, IFont> RightHeader; // Right header
- public List<Tuple<string, IFont>> Lines; // Content lines.
- }
- public class ColorDialog : ITransparentCollection
- {
- public PointF Location { get; set; } // Dialog location.
- public float PaddingW { get; set; } // Vertical padding.
- public float PaddingH { get; set; } // Horizontal padding.
- public IBrush BackgroundBrush { get; set; }
- public IBrush BorderBrush { get; set; }
- IController Hud { get; }
- PaintData data;
- TextLayout leftLayout;
- TextLayout rightLayout;
- float titleHeight;
- float maxLineWidth;
- TextLayout[] textLayout = new TextLayout[0];
- int drawCount = 0;
- public ColorDialog(IController hud)
- {
- Hud = hud;
- Location = new PointF(Hud.Window.Size.Width / 100f, Hud.Window.Size.Height / 100f);
- PaddingW = 4 * Hud.Window.Size.Width / 1920.0f;
- PaddingH = 3 * Hud.Window.Size.Height / 1020.0f;
- }
- public void Paint(PaintData data)
- {
- // Convenience method when dialog position is stationary.
- RenderDialog(CalculateLayout(data));
- }
- public SizeF CalculateLayout(PaintData data)
- {
- // Calcualte dialog size and prepare for rendering it.
- this.data = data;
- leftLayout = data.LeftHeader != null ? data.LeftHeader.Item2.GetTextLayout(data.LeftHeader.Item1) : null;
- rightLayout = data.RightHeader != null ? data.RightHeader.Item2.GetTextLayout(data.RightHeader.Item1) : null;
- maxLineWidth = (leftLayout != null ? leftLayout.Metrics.Width : 0.0f) + (rightLayout != null ? rightLayout.Metrics.Width : 0.0f);
- titleHeight = Math.Max((leftLayout != null ? leftLayout.Metrics.Height : 0.0f), (rightLayout != null ? rightLayout.Metrics.Height : 0.0f));
- float totalHeight = titleHeight;
- if (BorderBrush != null)
- {
- totalHeight += 2f * PaddingH;
- }
- if (data.Lines != null)
- {
- if (data.Lines.Count > textLayout.Length)
- {
- textLayout = new TextLayout[data.Lines.Count];
- }
- for (int i = 0; i < data.Lines.Count; ++i)
- {
- textLayout[i] = data.Lines[i].Item2.GetTextLayout(data.Lines[i].Item1);
- if (maxLineWidth < textLayout[i].Metrics.Width)
- {
- maxLineWidth = textLayout[i].Metrics.Width;
- }
- totalHeight += textLayout[i].Metrics.Height;
- }
- }
- drawCount = 0;
- return new SizeF(maxLineWidth + 2f * PaddingW, totalHeight + 2f * PaddingH);
- }
- public void RenderDialog(SizeF dialogSize)
- {
- if (drawCount > 0)
- {
- // Recalculate layouts.
- dialogSize = CalculateLayout(data);
- }
- // Render dialog using given rect.
- if (BackgroundBrush != null)
- {
- BackgroundBrush.DrawRectangle(Location.X, Location.Y, dialogSize.Width, dialogSize.Height);
- }
- // Line pointer.
- float currentY = Location.Y;
- // Header line.
- if (leftLayout != null || rightLayout != null)
- {
- if (leftLayout != null)
- {
- data.LeftHeader.Item2.DrawText(leftLayout, Location.X + PaddingW, currentY + PaddingH);
- }
- if (rightLayout != null)
- {
- data.RightHeader.Item2.DrawText(rightLayout, Location.X - PaddingW + maxLineWidth - rightLayout.Metrics.Width, currentY + PaddingH);
- }
- if (BorderBrush != null)
- {
- // Borderline under header.
- currentY += titleHeight + 2f * PaddingH;
- BorderBrush.DrawLine(Location.X, currentY, Location.X + dialogSize.Width, currentY);
- currentY += PaddingH;
- }
- else
- {
- currentY += titleHeight;
- }
- }
- // Content lines.
- if (data.Lines != null)
- {
- for (int i = 0; i < data.Lines.Count; ++i)
- {
- data.Lines[i].Item2.DrawText(textLayout[i], Location.X + PaddingW, currentY);
- currentY += textLayout[i].Metrics.Height;
- }
- }
- if (BorderBrush != null)
- {
- BorderBrush.DrawRectangle(Location.X, Location.Y, dialogSize.Width, dialogSize.Height);
- }
- drawCount += 1;
- }
- public IEnumerable<ITransparent> GetTransparents()
- {
- yield return BackgroundBrush;
- yield return BorderBrush;
- if (data != null)
- {
- if (data.LeftHeader != null) yield return data.LeftHeader.Item2;
- if (data.RightHeader != null) yield return data.RightHeader.Item2;
- if (data.Lines != null)
- {
- foreach (var line in data.Lines)
- {
- yield return line.Item2;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement