Skip to content

Commit

Permalink
Change color of labels that correspond to disabled inputs
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
dragunoff committed Feb 23, 2022
1 parent fa8bfc6 commit 32e1296
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 34 deletions.
19 changes: 19 additions & 0 deletions OpenRA.Game/Widgets/Widget.cs
Expand Up @@ -606,6 +606,25 @@ public override bool HandleMouseInput(MouseInput mi)
}
}

public class InputWidget : Widget
{
public bool Disabled = false;
public Func<bool> 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<string, object>
{
public WidgetArgs() { }
Expand Down
6 changes: 1 addition & 5 deletions OpenRA.Mods.Common/Widgets/ButtonWidget.cs
Expand Up @@ -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";
Expand All @@ -42,14 +42,12 @@ public class ButtonWidget : Widget
public int ContrastRadius = ChromeMetrics.Get<int>("ButtonTextContrastRadius");
public string ClickSound = ChromeMetrics.Get<string>("ClickSound");
public string ClickDisabledSound = ChromeMetrics.Get<string>("ClickDisabledSound");
public bool Disabled = false;
public bool Highlighted = false;
public Func<string> GetText;
public Func<Color> GetColor;
public Func<Color> GetColorDisabled;
public Func<Color> GetContrastColorDark;
public Func<Color> GetContrastColorLight;
public Func<bool> IsDisabled;
public Func<bool> IsHighlighted;
public Action<MouseInput> OnMouseDown = _ => { };
public Action<MouseInput> OnMouseUp = _ => { };
Expand Down Expand Up @@ -83,7 +81,6 @@ public ButtonWidget(ModData modData)
GetContrastColorLight = () => ContrastColorLight;
OnMouseUp = _ => OnClick();
OnKeyPress = _ => OnClick();
IsDisabled = () => Disabled;
IsHighlighted = () => Highlighted;
GetTooltipText = () => TooltipText;
GetTooltipDesc = () => TooltipDesc;
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions OpenRA.Mods.Common/Widgets/CheckboxWidget.cs
Expand Up @@ -23,12 +23,14 @@ public class CheckboxWidget : ButtonWidget
public Func<bool> IsChecked = () => false;
public int CheckOffset = 2;
public bool HasPressedState = ChromeMetrics.Get<bool>("CheckboxPressedState");
public new Color TextColorDisabled = ChromeMetrics.Get<Color>("TextDisabledColor");

[ObjectCreator.UseCtor]
public CheckboxWidget(ModData modData)
: base(modData)
{
GetCheckType = () => CheckType;
GetColorDisabled = () => TextColorDisabled;
}

protected CheckboxWidget(CheckboxWidget other)
Expand All @@ -39,6 +41,7 @@ protected CheckboxWidget(CheckboxWidget other)
IsChecked = other.IsChecked;
CheckOffset = other.CheckOffset;
HasPressedState = other.HasPressedState;
GetColorDisabled = () => other.TextColorDisabled;
}

public override void Draw()
Expand Down
3 changes: 1 addition & 2 deletions OpenRA.Mods.Common/Widgets/HotkeyEntryWidget.cs
Expand Up @@ -16,7 +16,7 @@

namespace OpenRA.Mods.Common.Widgets
{
public class HotkeyEntryWidget : Widget
public class HotkeyEntryWidget : InputWidget
{
public Hotkey Key;

Expand All @@ -27,7 +27,6 @@ public class HotkeyEntryWidget : Widget
public Action<KeyInput> OnEscKey = _ => { };
public Action OnLoseFocus = () => { };

public Func<bool> IsDisabled = () => false;
public Func<bool> IsValid = () => false;
public string Font = ChromeMetrics.Get<string>("HotkeyFont");
public Color TextColor = ChromeMetrics.Get<Color>("HotkeyColor");
Expand Down
48 changes: 48 additions & 0 deletions 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<Color>("TextDisabledColor");
readonly Lazy<InputWidget> inputWidget;
readonly CachedTransform<bool, Color> textColor;

[ObjectCreator.UseCtor]
public LabelForInputWidget()
: base()
{
inputWidget = Exts.Lazy(() => Parent.Get<InputWidget>(For));
textColor = new CachedTransform<bool, Color>(disabled => disabled ? TextDisabledColor : TextColor);
}

protected LabelForInputWidget(LabelForInputWidget other)
: base(other)
{
inputWidget = Exts.Lazy(() => Parent.Get<InputWidget>(other.For));
textColor = new CachedTransform<bool, Color>(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); }
}
}
3 changes: 1 addition & 2 deletions OpenRA.Mods.Common/Widgets/SliderWidget.cs
Expand Up @@ -16,9 +16,8 @@

namespace OpenRA.Mods.Common.Widgets
{
public class SliderWidget : Widget
public class SliderWidget : InputWidget
{
public Func<bool> IsDisabled = () => false;
public event Action<float> OnChange = _ => { };
public int Ticks = 0;
public int TrackHeight = 5;
Expand Down
11 changes: 2 additions & 9 deletions OpenRA.Mods.Common/Widgets/TextFieldWidget.cs
Expand Up @@ -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
Expand All @@ -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
{
Expand All @@ -64,7 +62,6 @@ public TextFieldType Type
public Action OnTextEdited = () => { };
public int CursorPosition { get; set; }

public Func<bool> IsDisabled;
public Func<bool> IsValid = () => true;
public string Font = ChromeMetrics.Get<string>("TextfieldFont");
public Color TextColor = ChromeMetrics.Get<Color>("TextfieldColor");
Expand All @@ -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)
Expand All @@ -95,7 +89,6 @@ protected TextFieldWidget(TextFieldWidget widget)
TextColorInvalid = widget.TextColorInvalid;
TextColorHighlight = widget.TextColorHighlight;
VisualHeight = widget.VisualHeight;
IsDisabled = widget.IsDisabled;
}

public override bool YieldKeyboardFocus()
Expand Down
6 changes: 4 additions & 2 deletions mods/cnc/chrome/ingame-info-lobby-options.yaml
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions mods/cnc/chrome/lobby-options.yaml
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions mods/cnc/chrome/settings-display.yaml
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions mods/common/chrome/ingame-info-lobby-options.yaml
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions mods/common/chrome/lobby-options.yaml
Expand Up @@ -44,35 +44,38 @@ 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
Y: 25
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
Y: 25
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
Expand Down

0 comments on commit 32e1296

Please sign in to comment.