Refactor checkbox
This commit is contained in:
@@ -18,73 +18,74 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
{
|
||||
public class CheckboxWidget : ButtonWidget
|
||||
{
|
||||
public string CheckType = "checked";
|
||||
public Func<string> GetCheckType;
|
||||
public new string Background = "checkbox";
|
||||
public string Checkmark = "tick";
|
||||
public Func<string> GetCheckmark;
|
||||
public Func<bool> IsChecked = () => false;
|
||||
public int CheckOffset = 2;
|
||||
public bool HasPressedState = ChromeMetrics.Get<bool>("CheckboxPressedState");
|
||||
|
||||
CachedTransform<(string, bool, bool), CachedTransform<(bool, bool, bool, bool), Sprite>> getCheckmarkImageCache;
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
public CheckboxWidget(ModData modData)
|
||||
: base(modData)
|
||||
{
|
||||
GetCheckType = () => CheckType;
|
||||
GetCheckmark = () => Checkmark;
|
||||
TextColor = ChromeMetrics.Get<Color>("TextColor");
|
||||
TextColorDisabled = ChromeMetrics.Get<Color>("TextDisabledColor");
|
||||
GetColor = () => TextColor;
|
||||
GetColorDisabled = () => TextColorDisabled;
|
||||
CreateCheckmarkImageCache();
|
||||
}
|
||||
|
||||
protected CheckboxWidget(CheckboxWidget other)
|
||||
: base(other)
|
||||
{
|
||||
CheckType = other.CheckType;
|
||||
GetCheckType = other.GetCheckType;
|
||||
Background = other.Background;
|
||||
Checkmark = other.Checkmark;
|
||||
GetCheckmark = other.GetCheckmark;
|
||||
IsChecked = other.IsChecked;
|
||||
CheckOffset = other.CheckOffset;
|
||||
HasPressedState = other.HasPressedState;
|
||||
TextColor = other.TextColor;
|
||||
TextColorDisabled = other.TextColorDisabled;
|
||||
GetColor = other.GetColor;
|
||||
GetColorDisabled = other.GetColorDisabled;
|
||||
CreateCheckmarkImageCache();
|
||||
}
|
||||
|
||||
void CreateCheckmarkImageCache()
|
||||
{
|
||||
getCheckmarkImageCache = new CachedTransform<(string, bool, bool), CachedTransform<(bool, bool, bool, bool), Sprite>>(
|
||||
((string CheckType, bool Highlighted, bool Checked) args) =>
|
||||
{
|
||||
var variantImageCollection = args.Highlighted ? "checkmark-highlighted-" : "checkmark-" + args.CheckType;
|
||||
var variantBaseName = args.Checked ? "checked" : "unchecked";
|
||||
return WidgetUtils.GetCachedStatefulImage(variantImageCollection, variantBaseName);
|
||||
});
|
||||
}
|
||||
|
||||
public override void Draw()
|
||||
{
|
||||
var disabled = IsDisabled();
|
||||
var font = Game.Renderer.Fonts[Font];
|
||||
var hover = Ui.MouseOverWidget == this;
|
||||
var color = GetColor();
|
||||
var colordisabled = GetColorDisabled();
|
||||
var bgDark = GetContrastColorDark();
|
||||
var bgLight = GetContrastColorLight();
|
||||
var rect = RenderBounds;
|
||||
var text = GetText();
|
||||
var textSize = font.Measure(text);
|
||||
var check = new Rectangle(rect.Location, new Size(Bounds.Height, Bounds.Height));
|
||||
var baseName = IsHighlighted() ? "checkbox-highlighted" : "checkbox";
|
||||
var state = WidgetUtils.GetStatefulImageName(baseName, disabled, Depressed && HasPressedState, Ui.MouseOverWidget == this);
|
||||
var rect = new Rectangle(RenderBounds.Location, new Size(Bounds.Height, Bounds.Height));
|
||||
|
||||
WidgetUtils.DrawPanel(state, check);
|
||||
|
||||
var topOffset = font.TopOffset;
|
||||
var position = new float2(rect.Left + rect.Height * 1.5f, RenderOrigin.Y + (Bounds.Height - textSize.Y - topOffset) / 2);
|
||||
DrawBackground(Background, rect, disabled, Depressed, hover, IsHighlighted());
|
||||
|
||||
var textPosition = new float2(RenderBounds.Left + RenderBounds.Height * 1.5f, RenderOrigin.Y + (Bounds.Height - font.Measure(text).Y - font.TopOffset) / 2);
|
||||
if (Contrast)
|
||||
font.DrawTextWithContrast(text, position,
|
||||
disabled ? colordisabled : color, bgDark, bgLight, 2);
|
||||
font.DrawTextWithContrast(text, textPosition,
|
||||
disabled ? colordisabled : color, GetContrastColorDark(), GetContrastColorLight(), 2);
|
||||
else
|
||||
font.DrawText(text, position,
|
||||
font.DrawText(text, textPosition,
|
||||
disabled ? colordisabled : color);
|
||||
|
||||
if (IsChecked() || (Depressed && HasPressedState && !disabled))
|
||||
{
|
||||
var checkType = GetCheckType();
|
||||
if (HasPressedState && (Depressed || disabled))
|
||||
checkType += "-disabled";
|
||||
|
||||
var offset = new float2(rect.Left + CheckOffset, rect.Top + CheckOffset);
|
||||
WidgetUtils.DrawSprite(ChromeProvider.GetImage("checkbox-bits", checkType), offset);
|
||||
}
|
||||
var checkmarkImage = getCheckmarkImageCache
|
||||
.Update((GetCheckmark(), IsHighlighted(), IsChecked()))
|
||||
.Update((disabled, Depressed, hover, false));
|
||||
WidgetUtils.DrawSprite(checkmarkImage, new float2(rect.Right - (int)((rect.Height + checkmarkImage.Size.X) / 2), rect.Top + (int)((rect.Height - checkmarkImage.Size.Y) / 2)));
|
||||
}
|
||||
|
||||
public override Widget Clone() { return new CheckboxWidget(this); }
|
||||
|
||||
Reference in New Issue
Block a user