public FactionTooltipLogic(Widget widget, ButtonWidget button) { var lines = button.GetTooltipText().Replace("\\n", "\n").Split('\n'); var header = widget.Get <LabelWidget>("HEADER"); var headerLine = lines[0]; var headerFont = Game.Renderer.Fonts[header.Font]; var headerSize = headerFont.Measure(headerLine); header.Bounds.Width += headerSize.X; header.Bounds.Height += headerSize.Y; header.GetText = () => headerLine; if (lines.Length > 1) { var description = widget.Get <LabelWidget>("DESCRIPTION"); var descriptionLines = lines.Skip(1).ToArray(); var descriptionFont = Game.Renderer.Fonts[description.Font]; description.Bounds.Y += header.Bounds.Y + header.Bounds.Height; description.Bounds.Width += descriptionLines.Select(l => descriptionFont.Measure(l).X).Max(); description.Bounds.Height += descriptionFont.Measure(descriptionLines.First()).Y *descriptionLines.Length; description.GetText = () => string.Join("\n", descriptionLines); widget.Bounds.Width = Math.Max(header.Bounds.X + header.Bounds.Width, description.Bounds.X + description.Bounds.Width); widget.Bounds.Height = description.Bounds.Y + description.Bounds.Height; } else { widget.Bounds.Width = header.Bounds.X + header.Bounds.Width; widget.Bounds.Height = header.Bounds.Y + header.Bounds.Height; } }
public ButtonTooltipLogic(Widget widget, ButtonWidget button) { var label = widget.Get <LabelWidget>("LABEL"); var font = Game.Renderer.Fonts[label.Font]; var text = button.GetTooltipText(); var labelWidth = font.Measure(text).X; var key = button.Key.GetValue(); label.GetText = () => text; label.Bounds.Width = labelWidth; widget.Bounds.Width = 2 * label.Bounds.X + labelWidth; if (key.IsValid()) { var hotkey = widget.Get <LabelWidget>("HOTKEY"); hotkey.Visible = true; var hotkeyLabel = "({0})".F(key.DisplayString()); hotkey.GetText = () => hotkeyLabel; hotkey.Bounds.X = labelWidth + 2 * label.Bounds.X; widget.Bounds.Width = hotkey.Bounds.X + label.Bounds.X + font.Measure(hotkeyLabel).X; } var desc = button.GetTooltipDesc(); if (!string.IsNullOrEmpty(desc)) { var descTemplate = widget.Get <LabelWidget>("DESC"); widget.RemoveChild(descTemplate); var descFont = Game.Renderer.Fonts[descTemplate.Font]; var descWidth = 0; var descOffset = descTemplate.Bounds.Y; foreach (var line in desc.Split(new[] { "\\n" }, StringSplitOptions.None)) { descWidth = Math.Max(descWidth, descFont.Measure(line).X); var lineLabel = (LabelWidget)descTemplate.Clone(); lineLabel.GetText = () => line; lineLabel.Bounds.Y = descOffset; widget.AddChild(lineLabel); descOffset += descTemplate.Bounds.Height; } widget.Bounds.Width = Math.Max(widget.Bounds.Width, descTemplate.Bounds.X * 2 + descWidth); widget.Bounds.Height += descOffset - descTemplate.Bounds.Y + descTemplate.Bounds.X; } }
public ButtonTooltipLogic(Widget widget, ButtonWidget button) { var label = widget.Get <LabelWidget>("LABEL"); var font = Game.Renderer.Fonts[label.Font]; var text = button.GetTooltipText(); var labelWidth = font.Measure(text).X; label.GetText = () => text; label.Bounds.Width = labelWidth; widget.Bounds.Width = 2 * label.Bounds.X + labelWidth; if (button.Key.IsValid()) { var hotkey = widget.Get <LabelWidget>("HOTKEY"); hotkey.Visible = true; var hotkeyLabel = "({0})".F(button.Key.DisplayString()); hotkey.GetText = () => hotkeyLabel; hotkey.Bounds.X = labelWidth + 2 * label.Bounds.X; widget.Bounds.Width = hotkey.Bounds.X + label.Bounds.X + font.Measure(hotkeyLabel).X; } }
public ButtonTooltipWithDescHighlightLogic(Widget widget, ButtonWidget button, Dictionary <string, MiniYaml> logicArgs) { var label = widget.Get <LabelWidget>("LABEL"); var font = Game.Renderer.Fonts[label.Font]; var text = button.GetTooltipText(); var labelWidth = font.Measure(text).X; var key = button.Key.GetValue(); label.GetText = () => text; label.Bounds.Width = labelWidth; widget.Bounds.Width = 2 * label.Bounds.X + labelWidth; if (key.IsValid()) { var hotkey = widget.Get <LabelWidget>("HOTKEY"); hotkey.Visible = true; var hotkeyLabel = "({0})".F(key.DisplayString()); hotkey.GetText = () => hotkeyLabel; hotkey.Bounds.X = labelWidth + 2 * label.Bounds.X; widget.Bounds.Width = hotkey.Bounds.X + label.Bounds.X + font.Measure(hotkeyLabel).X; } var desc = button.GetTooltipDesc(); if (!string.IsNullOrEmpty(desc)) { var descTemplate = widget.Get <LabelWidget>("DESC"); var highlightColor = FieldLoader.GetValue <Color>("Highlight", logicArgs["Highlight"].Value); widget.RemoveChild(descTemplate); var descFont = Game.Renderer.Fonts[descTemplate.Font]; var descWidth = 0; var descOffset = descTemplate.Bounds.Y; foreach (var l in desc.Split(new[] { "\\n" }, StringSplitOptions.None)) { var line = l; var lineWidth = 0; while (line.Length > 0) { var highlightStart = line.IndexOf('{'); var highlightEnd = line.IndexOf('}', 0); if (highlightStart > 0 && highlightEnd > highlightStart) { if (highlightStart > 0) { // Normal line segment before highlight var lineNormal = line.Substring(0, highlightStart); var lineNormalWidth = descFont.Measure(lineNormal).X; var lineNormalLabel = (LabelWidget)descTemplate.Clone(); lineNormalLabel.GetText = () => lineNormal; lineNormalLabel.Bounds.X = descTemplate.Bounds.X + lineWidth; lineNormalLabel.Bounds.Y = descOffset; lineNormalLabel.Bounds.Width = lineNormalWidth; widget.AddChild(lineNormalLabel); lineWidth += lineNormalWidth; } // Highlight line segment var lineHighlight = line.Substring(highlightStart + 1, highlightEnd - highlightStart - 1); var lineHighlightWidth = descFont.Measure(lineHighlight).X; var lineHighlightLabel = (LabelWidget)descTemplate.Clone(); lineHighlightLabel.GetText = () => lineHighlight; lineHighlightLabel.GetColor = () => highlightColor; lineHighlightLabel.Bounds.X = descTemplate.Bounds.X + lineWidth; lineHighlightLabel.Bounds.Y = descOffset; lineHighlightLabel.Bounds.Width = lineHighlightWidth; widget.AddChild(lineHighlightLabel); lineWidth += lineHighlightWidth; line = line.Substring(highlightEnd + 1); } else { // Final normal line segment var lineLabel = (LabelWidget)descTemplate.Clone(); var width = descFont.Measure(line).X; lineLabel.GetText = () => line; lineLabel.Bounds.X = descTemplate.Bounds.X + lineWidth; lineLabel.Bounds.Y = descOffset; widget.AddChild(lineLabel); lineWidth += width; break; } } descWidth = Math.Max(descWidth, lineWidth); descOffset += descTemplate.Bounds.Height; } widget.Bounds.Width = Math.Max(widget.Bounds.Width, descTemplate.Bounds.X * 2 + descWidth); widget.Bounds.Height += descOffset - descTemplate.Bounds.Y + descTemplate.Bounds.X; } }