From eadc8ad68933a16a8b1018419fbd0f2763fa773e Mon Sep 17 00:00:00 2001 From: Ivaylo Draganov Date: Tue, 22 Feb 2022 19:09:29 +0200 Subject: [PATCH] Change color of labels that correspond to disabled inputs - Add a new widget type for input and extend it from other input widgets - Add a new label type that can be linked to an input widget - Change the label color when the input's disabled state changes --- OpenRA.Game/Widgets/Widget.cs | 19 ++++++++ OpenRA.Mods.Common/Widgets/ButtonWidget.cs | 6 +-- OpenRA.Mods.Common/Widgets/CheckboxWidget.cs | 8 ++++ .../Widgets/HotkeyEntryWidget.cs | 3 +- .../Widgets/LabelForInputWidget.cs | 48 +++++++++++++++++++ OpenRA.Mods.Common/Widgets/SliderWidget.cs | 3 +- OpenRA.Mods.Common/Widgets/TextFieldWidget.cs | 11 +---- .../cnc/chrome/ingame-info-lobby-options.yaml | 6 ++- mods/cnc/chrome/lobby-options.yaml | 9 ++-- mods/cnc/chrome/settings-display.yaml | 9 ++-- .../chrome/ingame-info-lobby-options.yaml | 6 ++- mods/common/chrome/lobby-options.yaml | 9 ++-- mods/common/chrome/settings-display.yaml | 9 ++-- mods/common/metrics.yaml | 1 + 14 files changed, 113 insertions(+), 34 deletions(-) create mode 100644 OpenRA.Mods.Common/Widgets/LabelForInputWidget.cs diff --git a/OpenRA.Game/Widgets/Widget.cs b/OpenRA.Game/Widgets/Widget.cs index 97497367c18a..c3cfa0946134 100644 --- a/OpenRA.Game/Widgets/Widget.cs +++ b/OpenRA.Game/Widgets/Widget.cs @@ -606,6 +606,25 @@ public override bool HandleMouseInput(MouseInput mi) } } + public class InputWidget : Widget + { + public bool Disabled = false; + public Func IsDisabled = () => false; + + public InputWidget() + { + IsDisabled = () => Disabled; + } + + public InputWidget(InputWidget other) + : base(other) + { + IsDisabled = () => other.Disabled; + } + + public override Widget Clone() { return new InputWidget(this); } + } + public class WidgetArgs : Dictionary { public WidgetArgs() { } diff --git a/OpenRA.Mods.Common/Widgets/ButtonWidget.cs b/OpenRA.Mods.Common/Widgets/ButtonWidget.cs index 7513171f254d..193246085180 100644 --- a/OpenRA.Mods.Common/Widgets/ButtonWidget.cs +++ b/OpenRA.Mods.Common/Widgets/ButtonWidget.cs @@ -16,7 +16,7 @@ namespace OpenRA.Mods.Common.Widgets { - public class ButtonWidget : Widget + public class ButtonWidget : InputWidget { public readonly string TooltipContainer; public readonly string TooltipTemplate = "BUTTON_TOOLTIP"; @@ -42,14 +42,12 @@ public class ButtonWidget : Widget public int ContrastRadius = ChromeMetrics.Get("ButtonTextContrastRadius"); public string ClickSound = ChromeMetrics.Get("ClickSound"); public string ClickDisabledSound = ChromeMetrics.Get("ClickDisabledSound"); - public bool Disabled = false; public bool Highlighted = false; public Func GetText; public Func GetColor; public Func GetColorDisabled; public Func GetContrastColorDark; public Func GetContrastColorLight; - public Func IsDisabled; public Func IsHighlighted; public Action OnMouseDown = _ => { }; public Action OnMouseUp = _ => { }; @@ -83,7 +81,6 @@ public ButtonWidget(ModData modData) GetContrastColorLight = () => ContrastColorLight; OnMouseUp = _ => OnClick(); OnKeyPress = _ => OnClick(); - IsDisabled = () => Disabled; IsHighlighted = () => Highlighted; GetTooltipText = () => TooltipText; GetTooltipDesc = () => TooltipDesc; @@ -118,7 +115,6 @@ protected ButtonWidget(ButtonWidget other) GetContrastColorLight = other.GetContrastColorLight; OnMouseDown = other.OnMouseDown; Disabled = other.Disabled; - IsDisabled = other.IsDisabled; Highlighted = other.Highlighted; IsHighlighted = other.IsHighlighted; diff --git a/OpenRA.Mods.Common/Widgets/CheckboxWidget.cs b/OpenRA.Mods.Common/Widgets/CheckboxWidget.cs index e6dfec99d62d..703547aabcd9 100644 --- a/OpenRA.Mods.Common/Widgets/CheckboxWidget.cs +++ b/OpenRA.Mods.Common/Widgets/CheckboxWidget.cs @@ -29,6 +29,10 @@ public CheckboxWidget(ModData modData) : base(modData) { GetCheckType = () => CheckType; + TextColor = ChromeMetrics.Get("TextColor"); + TextColorDisabled = ChromeMetrics.Get("TextDisabledColor"); + GetColor = () => TextColor; + GetColorDisabled = () => TextColorDisabled; } protected CheckboxWidget(CheckboxWidget other) @@ -39,6 +43,10 @@ protected CheckboxWidget(CheckboxWidget other) IsChecked = other.IsChecked; CheckOffset = other.CheckOffset; HasPressedState = other.HasPressedState; + TextColor = other.TextColor; + TextColorDisabled = other.TextColorDisabled; + GetColor = other.GetColor; + GetColorDisabled = other.GetColorDisabled; } public override void Draw() diff --git a/OpenRA.Mods.Common/Widgets/HotkeyEntryWidget.cs b/OpenRA.Mods.Common/Widgets/HotkeyEntryWidget.cs index 7b0d310d6110..9d97f919dbe4 100644 --- a/OpenRA.Mods.Common/Widgets/HotkeyEntryWidget.cs +++ b/OpenRA.Mods.Common/Widgets/HotkeyEntryWidget.cs @@ -16,7 +16,7 @@ namespace OpenRA.Mods.Common.Widgets { - public class HotkeyEntryWidget : Widget + public class HotkeyEntryWidget : InputWidget { public Hotkey Key; @@ -27,7 +27,6 @@ public class HotkeyEntryWidget : Widget public Action OnEscKey = _ => { }; public Action OnLoseFocus = () => { }; - public Func IsDisabled = () => false; public Func IsValid = () => false; public string Font = ChromeMetrics.Get("HotkeyFont"); public Color TextColor = ChromeMetrics.Get("HotkeyColor"); diff --git a/OpenRA.Mods.Common/Widgets/LabelForInputWidget.cs b/OpenRA.Mods.Common/Widgets/LabelForInputWidget.cs new file mode 100644 index 000000000000..154c17ff0608 --- /dev/null +++ b/OpenRA.Mods.Common/Widgets/LabelForInputWidget.cs @@ -0,0 +1,48 @@ +#region Copyright & License Information +/* + * Copyright 2007-2022 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System; +using OpenRA.Graphics; +using OpenRA.Primitives; +using OpenRA.Widgets; + +namespace OpenRA.Mods.Common.Widgets +{ + public class LabelForInputWidget : LabelWidget + { + public string For = null; + public readonly Color TextDisabledColor = ChromeMetrics.Get("TextDisabledColor"); + readonly Lazy inputWidget; + readonly CachedTransform textColor; + + [ObjectCreator.UseCtor] + public LabelForInputWidget() + : base() + { + inputWidget = Exts.Lazy(() => Parent.Get(For)); + textColor = new CachedTransform(disabled => disabled ? TextDisabledColor : TextColor); + } + + protected LabelForInputWidget(LabelForInputWidget other) + : base(other) + { + inputWidget = Exts.Lazy(() => Parent.Get(other.For)); + textColor = new CachedTransform(disabled => disabled ? TextDisabledColor : TextColor); + } + + protected override void DrawInner(string text, SpriteFont font, Color color, int2 position) + { + font.DrawText(text, position, textColor.Update(inputWidget.Value.IsDisabled())); + } + + public override Widget Clone() { return new LabelForInputWidget(this); } + } +} diff --git a/OpenRA.Mods.Common/Widgets/SliderWidget.cs b/OpenRA.Mods.Common/Widgets/SliderWidget.cs index 4b023b5a8693..56030739c648 100644 --- a/OpenRA.Mods.Common/Widgets/SliderWidget.cs +++ b/OpenRA.Mods.Common/Widgets/SliderWidget.cs @@ -16,9 +16,8 @@ namespace OpenRA.Mods.Common.Widgets { - public class SliderWidget : Widget + public class SliderWidget : InputWidget { - public Func IsDisabled = () => false; public event Action OnChange = _ => { }; public int Ticks = 0; public int TrackHeight = 5; diff --git a/OpenRA.Mods.Common/Widgets/TextFieldWidget.cs b/OpenRA.Mods.Common/Widgets/TextFieldWidget.cs index b76afd6dd369..912a6c8ac578 100644 --- a/OpenRA.Mods.Common/Widgets/TextFieldWidget.cs +++ b/OpenRA.Mods.Common/Widgets/TextFieldWidget.cs @@ -18,7 +18,7 @@ namespace OpenRA.Mods.Common.Widgets { public enum TextFieldType { General, Filename, Integer } - public class TextFieldWidget : Widget + public class TextFieldWidget : InputWidget { string text = ""; public string Text @@ -39,8 +39,6 @@ public string Text public int RightMargin = 5; public string Background = "textfield"; - public bool Disabled = false; - TextFieldType type = TextFieldType.General; public TextFieldType Type { @@ -64,7 +62,6 @@ public TextFieldType Type public Action OnTextEdited = () => { }; public int CursorPosition { get; set; } - public Func IsDisabled; public Func IsValid = () => true; public string Font = ChromeMetrics.Get("TextfieldFont"); public Color TextColor = ChromeMetrics.Get("TextfieldColor"); @@ -76,10 +73,7 @@ public TextFieldType Type protected int selectionEndIndex = -1; protected bool mouseSelectionActive = false; - public TextFieldWidget() - { - IsDisabled = () => Disabled; - } + public TextFieldWidget() { } protected TextFieldWidget(TextFieldWidget widget) : base(widget) @@ -95,7 +89,6 @@ protected TextFieldWidget(TextFieldWidget widget) TextColorInvalid = widget.TextColorInvalid; TextColorHighlight = widget.TextColorHighlight; VisualHeight = widget.VisualHeight; - IsDisabled = widget.IsDisabled; } public override bool YieldKeyboardFocus() diff --git a/mods/cnc/chrome/ingame-info-lobby-options.yaml b/mods/cnc/chrome/ingame-info-lobby-options.yaml index 89aba2056834..98f1dcf9dc9c 100644 --- a/mods/cnc/chrome/ingame-info-lobby-options.yaml +++ b/mods/cnc/chrome/ingame-info-lobby-options.yaml @@ -35,11 +35,12 @@ Container@LOBBY_OPTIONS_PANEL: Height: 60 Width: PARENT_RIGHT Children: - Label@A_DESC: + LabelForInput@A_DESC: X: 10 Width: PARENT_RIGHT / 2 - 20 Height: 20 Visible: False + For: A DropDownButton@A: X: 10 Y: 25 @@ -48,11 +49,12 @@ Container@LOBBY_OPTIONS_PANEL: Font: Regular Visible: False TooltipContainer: TOOLTIP_CONTAINER - Label@B_DESC: + LabelForInput@B_DESC: X: PARENT_RIGHT / 2 + 10 Width: PARENT_RIGHT / 2 - 20 Height: 20 Visible: False + For: B DropDownButton@B: X: PARENT_RIGHT / 2 + 10 Y: 25 diff --git a/mods/cnc/chrome/lobby-options.yaml b/mods/cnc/chrome/lobby-options.yaml index 2a711fe946f4..48eaaa945bec 100644 --- a/mods/cnc/chrome/lobby-options.yaml +++ b/mods/cnc/chrome/lobby-options.yaml @@ -47,11 +47,12 @@ Container@LOBBY_OPTIONS_BIN: Height: 60 Width: PARENT_RIGHT Children: - Label@A_DESC: + LabelForInput@A_DESC: X: 10 Width: PARENT_RIGHT / 3 - 20 Height: 20 Visible: False + For: A DropDownButton@A: X: 10 Width: PARENT_RIGHT / 3 - 20 @@ -60,11 +61,12 @@ Container@LOBBY_OPTIONS_BIN: Font: Regular Visible: False TooltipContainer: TOOLTIP_CONTAINER - Label@B_DESC: + LabelForInput@B_DESC: X: PARENT_RIGHT / 3 + 10 Width: PARENT_RIGHT / 3 - 20 Height: 20 Visible: False + For: B DropDownButton@B: X: PARENT_RIGHT / 3 + 10 Width: PARENT_RIGHT / 3 - 20 @@ -73,11 +75,12 @@ Container@LOBBY_OPTIONS_BIN: Font: Regular Visible: False TooltipContainer: TOOLTIP_CONTAINER - Label@C_DESC: + LabelForInput@C_DESC: X: (PARENT_RIGHT / 3) * 2 + 10 Width: PARENT_RIGHT / 3 - 20 Height: 20 Visible: False + For: C DropDownButton@C: X: (PARENT_RIGHT / 3) * 2 + 10 Width: PARENT_RIGHT / 3 - 20 diff --git a/mods/cnc/chrome/settings-display.yaml b/mods/cnc/chrome/settings-display.yaml index 4fe073ed3450..c4a603e7918c 100644 --- a/mods/cnc/chrome/settings-display.yaml +++ b/mods/cnc/chrome/settings-display.yaml @@ -30,10 +30,11 @@ Container@DISPLAY_PANEL: X: 10 Width: PARENT_RIGHT / 2 - 20 Children: - Label@PLAYER: + LabelForInput@PLAYER: Width: PARENT_RIGHT Height: 20 Text: Player Name: + For: PLAYERNAME TextField@PLAYERNAME: Y: 25 Width: PARENT_RIGHT @@ -44,10 +45,11 @@ Container@DISPLAY_PANEL: X: PARENT_RIGHT / 2 + 10 Width: PARENT_RIGHT / 2 - 20 Children: - Label@COLOR: + LabelForInput@COLOR: Width: PARENT_RIGHT Height: 20 Text: Preferred Color: + For: PLAYERCOLOR DropDownButton@PLAYERCOLOR: Y: 25 Width: 75 @@ -111,10 +113,11 @@ Container@DISPLAY_PANEL: X: 10 Width: PARENT_RIGHT / 2 - 20 Children: - Label@UI_SCALE: + LabelForInput@UI_SCALE: Width: PARENT_RIGHT Height: 20 Text: UI Scale: + For: UI_SCALE_DROPDOWN DropDownButton@UI_SCALE_DROPDOWN: Y: 25 Width: PARENT_RIGHT diff --git a/mods/common/chrome/ingame-info-lobby-options.yaml b/mods/common/chrome/ingame-info-lobby-options.yaml index 9269d3c5913c..aa00fc2583f0 100644 --- a/mods/common/chrome/ingame-info-lobby-options.yaml +++ b/mods/common/chrome/ingame-info-lobby-options.yaml @@ -33,11 +33,12 @@ Container@LOBBY_OPTIONS_PANEL: Height: 60 Width: PARENT_RIGHT Children: - Label@A_DESC: + LabelForInput@A_DESC: X: 10 Width: PARENT_RIGHT / 2 - 20 Height: 20 Visible: False + For: A DropDownButton@A: X: 10 Y: 25 @@ -46,11 +47,12 @@ Container@LOBBY_OPTIONS_PANEL: Font: Regular Visible: False TooltipContainer: TOOLTIP_CONTAINER - Label@B_DESC: + LabelForInput@B_DESC: X: PARENT_RIGHT / 2 + 10 Width: PARENT_RIGHT / 2 - 20 Height: 20 Visible: False + For: B DropDownButton@B: X: PARENT_RIGHT / 2 + 10 Y: 25 diff --git a/mods/common/chrome/lobby-options.yaml b/mods/common/chrome/lobby-options.yaml index cbc9b4f59208..01073d12f950 100644 --- a/mods/common/chrome/lobby-options.yaml +++ b/mods/common/chrome/lobby-options.yaml @@ -44,11 +44,12 @@ Container@LOBBY_OPTIONS_BIN: Height: 60 Width: PARENT_RIGHT Children: - Label@A_DESC: + LabelForInput@A_DESC: X: 10 Width: PARENT_RIGHT / 3 - 20 Height: 20 Visible: False + For: A DropDownButton@A: X: 10 Width: PARENT_RIGHT / 3 - 20 @@ -56,11 +57,12 @@ Container@LOBBY_OPTIONS_BIN: Height: 25 Visible: False TooltipContainer: TOOLTIP_CONTAINER - Label@B_DESC: + LabelForInput@B_DESC: X: PARENT_RIGHT / 3 + 10 Width: PARENT_RIGHT / 3 - 20 Height: 20 Visible: False + For: B DropDownButton@B: X: PARENT_RIGHT / 3 + 10 Width: PARENT_RIGHT / 3 - 20 @@ -68,11 +70,12 @@ Container@LOBBY_OPTIONS_BIN: Height: 25 Visible: False TooltipContainer: TOOLTIP_CONTAINER - Label@C_DESC: + LabelForInput@C_DESC: X: (PARENT_RIGHT / 3) * 2 + 10 Width: PARENT_RIGHT / 3 - 20 Height: 20 Visible: False + For: C DropDownButton@C: X: (PARENT_RIGHT / 3) * 2 + 10 Width: PARENT_RIGHT / 3 - 20 diff --git a/mods/common/chrome/settings-display.yaml b/mods/common/chrome/settings-display.yaml index 7431da76248b..f94f0da43093 100644 --- a/mods/common/chrome/settings-display.yaml +++ b/mods/common/chrome/settings-display.yaml @@ -30,10 +30,11 @@ Container@DISPLAY_PANEL: X: 10 Width: PARENT_RIGHT / 2 - 20 Children: - Label@PLAYER: + LabelForInput@PLAYER: Width: PARENT_RIGHT Height: 20 Text: Player Name: + For: PLAYERNAME TextField@PLAYERNAME: Y: 25 Width: PARENT_RIGHT @@ -44,10 +45,11 @@ Container@DISPLAY_PANEL: X: PARENT_RIGHT / 2 + 10 Width: PARENT_RIGHT / 2 - 20 Children: - Label@COLOR: + LabelForInput@COLOR: Width: PARENT_RIGHT Height: 20 Text: Preferred Color: + For: PLAYERCOLOR DropDownButton@PLAYERCOLOR: Y: 25 Width: 75 @@ -111,10 +113,11 @@ Container@DISPLAY_PANEL: X: 10 Width: PARENT_RIGHT / 2 - 20 Children: - Label@UI_SCALE: + LabelForInput@UI_SCALE: Width: PARENT_RIGHT Height: 20 Text: UI Scale: + For: UI_SCALE_DROPDOWN DropDownButton@UI_SCALE_DROPDOWN: Y: 25 Width: PARENT_RIGHT diff --git a/mods/common/metrics.yaml b/mods/common/metrics.yaml index e766239a2343..976e57d9d08a 100644 --- a/mods/common/metrics.yaml +++ b/mods/common/metrics.yaml @@ -30,6 +30,7 @@ Metrics: SpawnLabelOffset: 0,1 TextColor: FFFFFF TextHighlightColor: FFFF00 + TextDisabledColor: 808080 TextContrast: false TextContrastColorDark: 000000 TextContrastColorLight: 7F7F7F