Better support for dynamic tooltip sizes
This commit is contained in:
@@ -42,11 +42,8 @@ namespace OpenRA.Mods.Cnc.Widgets
|
||||
|
||||
public override void MouseEntered()
|
||||
{
|
||||
if (TooltipContainer == null)
|
||||
return;
|
||||
|
||||
tooltipContainer.Value.SetTooltip(
|
||||
Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "world", world }, { "wic", this }}));
|
||||
if (TooltipContainer == null) return;
|
||||
tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() {{ "world", world }, { "wic", this }});
|
||||
}
|
||||
|
||||
public override void MouseExited()
|
||||
|
||||
@@ -18,22 +18,26 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
|
||||
{
|
||||
[ObjectCreator.UseCtor]
|
||||
public SimpleTooltipLogic([ObjectCreator.Param] Widget widget,
|
||||
[ObjectCreator.Param] TooltipContainerWidget tooltipContainer,
|
||||
[ObjectCreator.Param] Func<string> getText)
|
||||
{
|
||||
var label = widget.GetWidget<LabelWidget>("LABEL");
|
||||
var cachedWidth = 0;
|
||||
|
||||
var font = Game.Renderer.Fonts[label.Font];
|
||||
label.GetText = () =>
|
||||
var cachedWidth = 0;
|
||||
var labelText = "";
|
||||
tooltipContainer.BeforeRender = () =>
|
||||
{
|
||||
var text = getText();
|
||||
var textWidth = font.Measure(text).X;
|
||||
labelText = getText();
|
||||
var textWidth = font.Measure(labelText).X;
|
||||
if (textWidth != cachedWidth)
|
||||
{
|
||||
label.Bounds.Width = textWidth;
|
||||
widget.Bounds.Width = 2*label.Bounds.X + textWidth;
|
||||
}
|
||||
return text;
|
||||
};
|
||||
|
||||
label.GetText = () => labelText;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,25 +18,28 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
|
||||
{
|
||||
[ObjectCreator.UseCtor]
|
||||
public WorldTooltipLogic([ObjectCreator.Param] Widget widget,
|
||||
[ObjectCreator.Param] TooltipContainerWidget tooltipContainer,
|
||||
[ObjectCreator.Param] CncWorldInteractionControllerWidget wic)
|
||||
{
|
||||
widget.IsVisible = () => wic.TooltipType != T.WorldTooltipType.None;
|
||||
|
||||
var label = widget.GetWidget<LabelWidget>("LABEL");
|
||||
|
||||
var font = Game.Renderer.Fonts[label.Font];
|
||||
var cachedWidth = 0;
|
||||
label.GetText = () =>
|
||||
var labelText = "";
|
||||
tooltipContainer.BeforeRender = () =>
|
||||
{
|
||||
var text = wic.TooltipType == T.WorldTooltipType.Unexplored ? "Unexplored Terrain" :
|
||||
labelText = wic.TooltipType == T.WorldTooltipType.Unexplored ? "Unexplored Terrain" :
|
||||
wic.ActorTooltip.Name();
|
||||
var textWidth = font.Measure(text).X;
|
||||
var textWidth = font.Measure(labelText).X;
|
||||
if (textWidth != cachedWidth)
|
||||
{
|
||||
label.Bounds.Width = textWidth;
|
||||
widget.Bounds.Width = 2*label.Bounds.X + textWidth;
|
||||
}
|
||||
return text;
|
||||
};
|
||||
|
||||
label.GetText = () => labelText;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,8 +39,7 @@ namespace OpenRA.Mods.Cnc.Widgets
|
||||
{
|
||||
if (TooltipContainer == null) return;
|
||||
Func<string> getText = () => "Power Usage: {0}/{1}".F(pm.PowerDrained, pm.PowerProvided);
|
||||
tooltipContainer.Value.SetTooltip(
|
||||
Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "getText", getText }}));
|
||||
tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() {{ "getText", getText }});
|
||||
}
|
||||
|
||||
public override void MouseExited()
|
||||
|
||||
@@ -92,11 +92,8 @@ namespace OpenRA.Mods.Cnc.Widgets
|
||||
|
||||
public override void MouseEntered()
|
||||
{
|
||||
if (TooltipContainer == null)
|
||||
return;
|
||||
|
||||
var panel = Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "palette", this }});
|
||||
tooltipContainer.Value.SetTooltip(panel);
|
||||
if (TooltipContainer == null) return;
|
||||
tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() {{ "palette", this }});
|
||||
}
|
||||
|
||||
public override void MouseExited()
|
||||
|
||||
@@ -40,8 +40,7 @@ namespace OpenRA.Mods.Cnc.Widgets
|
||||
{
|
||||
if (TooltipContainer == null) return;
|
||||
Func<string> getText = () => "Silo Usage: {0}/{1}".F(pr.Ore, pr.OreCapacity);
|
||||
tooltipContainer.Value.SetTooltip(
|
||||
Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "getText", getText }}));
|
||||
tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() {{ "getText", getText }});
|
||||
}
|
||||
|
||||
public override void MouseExited()
|
||||
|
||||
@@ -122,11 +122,8 @@ namespace OpenRA.Mods.Cnc.Widgets
|
||||
|
||||
public override void MouseEntered()
|
||||
{
|
||||
if (TooltipContainer == null)
|
||||
return;
|
||||
|
||||
var panel = Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "palette", this }});
|
||||
tooltipContainer.Value.SetTooltip(panel);
|
||||
if (TooltipContainer == null) return;
|
||||
tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() {{ "palette", this }});
|
||||
}
|
||||
|
||||
public override void MouseExited()
|
||||
|
||||
@@ -39,11 +39,8 @@ namespace OpenRA.Mods.Cnc.Widgets
|
||||
|
||||
public override void MouseEntered()
|
||||
{
|
||||
if (TooltipContainer == null)
|
||||
return;
|
||||
|
||||
tooltipContainer.Value.SetTooltip(
|
||||
Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "button", this }}));
|
||||
if (TooltipContainer == null) return;
|
||||
tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() {{ "button", this }});
|
||||
}
|
||||
|
||||
public override void MouseExited()
|
||||
|
||||
@@ -19,9 +19,11 @@ using System;
|
||||
|
||||
namespace OpenRA.Mods.Cnc.Widgets
|
||||
{
|
||||
class TooltipContainerWidget : Widget
|
||||
public class TooltipContainerWidget : Widget
|
||||
{
|
||||
static readonly Action Nothing = () => {};
|
||||
public int2 CursorOffset = new int2(0, 20);
|
||||
public Action BeforeRender = Nothing;
|
||||
public int TooltipDelay = 10;
|
||||
Widget tooltip;
|
||||
|
||||
@@ -30,18 +32,19 @@ namespace OpenRA.Mods.Cnc.Widgets
|
||||
IsVisible = () => Viewport.TicksSinceLastMove >= TooltipDelay;
|
||||
}
|
||||
|
||||
public void SetTooltip(Widget tooltip)
|
||||
public void SetTooltip(string id, WidgetArgs args)
|
||||
{
|
||||
this.tooltip = tooltip;
|
||||
RemoveChildren();
|
||||
AddChild(tooltip);
|
||||
RemoveTooltip();
|
||||
tooltip = Widget.LoadWidget(id, this, new WidgetArgs(args) {{ "tooltipContainer", this }});
|
||||
}
|
||||
|
||||
public void RemoveTooltip()
|
||||
{
|
||||
RemoveChildren();
|
||||
BeforeRender = Nothing;
|
||||
}
|
||||
|
||||
public override void Draw() { BeforeRender(); }
|
||||
public override Rectangle GetEventBounds() { return Rectangle.Empty; }
|
||||
public override int2 ChildOrigin
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user