Don't use Bindable for AccentColour

Implements IHasAccentColour for conformity to rest of project. Also fixes a nullref when opening the login menu.
This commit is contained in:
Dean Herbert 2017-08-29 18:17:01 +09:00
parent e53f2cb359
commit 39b5b04700
6 changed files with 113 additions and 120 deletions

View File

@ -1,9 +1,9 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Linq;
using OpenTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
@ -14,35 +14,43 @@ using OpenTK;
namespace osu.Game.Graphics.UserInterface
{
public class OsuDropdown<T> : Dropdown<T>
public class OsuDropdown<T> : Dropdown<T>, IHasAccentColour
{
public readonly Bindable<Color4?> AccentColour = new Bindable<Color4?>();
private Color4 accentColour;
public Color4 AccentColour
{
get { return accentColour; }
set
{
accentColour = value;
updateAccentColour();
}
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
if (AccentColour.Value == null)
AccentColour.Value = colours.PinkDarker;
if (accentColour == default(Color4))
accentColour = colours.PinkDarker;
updateAccentColour();
}
protected override DropdownHeader CreateHeader()
private void updateAccentColour()
{
var newHeader = new OsuDropdownHeader();
newHeader.AccentColour.BindTo(AccentColour);
var header = Header as IHasAccentColour;
if (header != null) header.AccentColour = accentColour;
return newHeader;
var menu = Menu as IHasAccentColour;
if (menu != null) menu.AccentColour = accentColour;
}
protected override DropdownMenu CreateMenu()
{
var newMenu = new OsuDropdownMenu();
newMenu.AccentColour.BindTo(AccentColour);
protected override DropdownHeader CreateHeader() => new OsuDropdownHeader();
return newMenu;
}
protected override DropdownMenu CreateMenu() => new OsuDropdownMenu();
#region OsuDropdownMenu
protected class OsuDropdownMenu : DropdownMenu
protected class OsuDropdownMenu : DropdownMenu, IHasAccentColour
{
// todo: this uses the same styling as OsuMenu. hopefully we can just use OsuMenu in the future with some refactoring
public OsuDropdownMenu()
@ -65,23 +73,41 @@ namespace osu.Game.Graphics.UserInterface
this.ResizeHeightTo(State == MenuState.Opened ? actualHeight : 0, 300, Easing.OutQuint);
}
public readonly Bindable<Color4?> AccentColour = new Bindable<Color4?>();
protected override DrawableMenuItem CreateDrawableMenuItem(MenuItem item)
private Color4 accentColour;
public Color4 AccentColour
{
var newItem = new DrawableOsuDropdownMenuItem(item);
newItem.AccentColour.BindTo(AccentColour);
return newItem;
get { return accentColour; }
set
{
accentColour = value;
foreach (var c in Children.OfType<IHasAccentColour>())
c.AccentColour = value;
}
}
#region DrawableOsuDropdownMenuItem
protected class DrawableOsuDropdownMenuItem : DrawableDropdownMenuItem
{
public readonly Bindable<Color4?> AccentColour = new Bindable<Color4?>();
protected override DrawableMenuItem CreateDrawableMenuItem(MenuItem item) => new DrawableOsuDropdownMenuItem(item) { AccentColour = accentColour };
private TextContainer textContainer;
#region DrawableOsuDropdownMenuItem
protected class DrawableOsuDropdownMenuItem : DrawableDropdownMenuItem, IHasAccentColour
{
private Color4? accentColour;
public Color4 AccentColour
{
get { return accentColour ?? nonAccentSelectedColour; }
set
{
accentColour = value;
updateColours();
}
}
private void updateColours()
{
BackgroundColourHover = accentColour ?? nonAccentHoverColour;
BackgroundColourSelected = accentColour ?? nonAccentSelectedColour;
UpdateBackgroundColour();
UpdateForegroundColour();
}
private Color4 nonAccentHoverColour;
private Color4 nonAccentSelectedColour;
@ -93,36 +119,29 @@ namespace osu.Game.Graphics.UserInterface
Masking = true;
CornerRadius = 6;
AccentColour.ValueChanged += updateAccent;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundColour = Color4.Transparent;
nonAccentHoverColour = colours.PinkDarker;
nonAccentSelectedColour = Color4.Black.Opacity(0.5f);
}
private void updateAccent(Color4? newValue)
{
BackgroundColourHover = newValue ?? nonAccentHoverColour;
BackgroundColourSelected = newValue ?? nonAccentSelectedColour;
UpdateBackgroundColour();
UpdateForegroundColour();
updateColours();
}
protected override void UpdateForegroundColour()
{
base.UpdateForegroundColour();
textContainer.Chevron.Alpha = IsHovered ? 1 : 0;
var content = Foreground.Child as Content;
if (content != null) content.Chevron.Alpha = IsHovered ? 1 : 0;
}
protected override Drawable CreateContent() => textContainer = new TextContainer();
protected override Drawable CreateContent() => new Content();
protected class TextContainer : FillFlowContainer, IHasText
protected class Content : FillFlowContainer, IHasText
{
public string Text
{
@ -133,7 +152,7 @@ namespace osu.Game.Graphics.UserInterface
public readonly OsuSpriteText Label;
public readonly SpriteIcon Chevron;
public TextContainer()
public Content()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
@ -165,7 +184,7 @@ namespace osu.Game.Graphics.UserInterface
}
#endregion
public class OsuDropdownHeader : DropdownHeader
public class OsuDropdownHeader : DropdownHeader, IHasAccentColour
{
protected readonly SpriteText Text;
protected override string Label
@ -176,7 +195,16 @@ namespace osu.Game.Graphics.UserInterface
protected readonly SpriteIcon Icon;
public readonly Bindable<Color4?> AccentColour = new Bindable<Color4?>();
private Color4 accentColour;
public virtual Color4 AccentColour
{
get { return accentColour; }
set
{
accentColour = value;
BackgroundColourHover = accentColour;
}
}
public OsuDropdownHeader()
{
@ -203,20 +231,13 @@ namespace osu.Game.Graphics.UserInterface
Size = new Vector2(20),
}
};
AccentColour.ValueChanged += accentColourChanged;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundColour = Color4.Black.Opacity(0.5f);
BackgroundColourHover = AccentColour?.Value ?? colours.PinkDarker;
}
private void accentColourChanged(Color4? newValue)
{
BackgroundColourHover = newValue ?? Color4.White;
BackgroundColourHover = colours.PinkDarker;
}
}
}

View File

@ -37,34 +37,34 @@ namespace osu.Game.Graphics.UserInterface
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
if (accentColour == null)
if (accentColour == default(Color4))
AccentColour = colours.Blue;
}
private Color4? accentColour;
private Color4 accentColour;
public Color4 AccentColour
{
get { return accentColour.GetValueOrDefault(); }
get { return accentColour; }
set
{
accentColour = value;
var dropdown = Dropdown as OsuTabDropdown;
var dropdown = Dropdown as IHasAccentColour;
if (dropdown != null)
dropdown.AccentColour.Value = value;
foreach (var item in TabContainer.Children.OfType<OsuTabItem>())
item.AccentColour = value;
dropdown.AccentColour = value;
foreach (var i in TabContainer.Children.OfType<IHasAccentColour>())
i.AccentColour = value;
}
}
public class OsuTabItem : TabItem<T>
public class OsuTabItem : TabItem<T>, IHasAccentColour
{
protected readonly SpriteText Text;
private readonly Box box;
private Color4? accentColour;
private Color4 accentColour;
public Color4 AccentColour
{
get { return accentColour.GetValueOrDefault(); }
get { return accentColour; }
set
{
accentColour = value;
@ -103,7 +103,7 @@ namespace osu.Game.Graphics.UserInterface
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
if (accentColour == null)
if (accentColour == default(Color4))
AccentColour = colours.Blue;
}
@ -148,25 +148,13 @@ namespace osu.Game.Graphics.UserInterface
RelativeSizeAxes = Axes.X;
}
protected override DropdownMenu CreateMenu()
protected override DropdownMenu CreateMenu() => new OsuTabDropdownMenu();
protected override DropdownHeader CreateHeader() => new OsuTabDropdownHeader
{
var menu = new OsuTabDropdownMenu();
menu.AccentColour.BindTo(AccentColour);
return menu;
}
protected override DropdownHeader CreateHeader()
{
var newHeader = new OsuTabDropdownHeader
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight
};
newHeader.AccentColour.BindTo(AccentColour);
return newHeader;
}
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight
};
private class OsuTabDropdownMenu : OsuDropdownMenu
{
@ -179,12 +167,7 @@ namespace osu.Game.Graphics.UserInterface
MaxHeight = 400;
}
protected override DrawableMenuItem CreateDrawableMenuItem(MenuItem item)
{
var result = new DrawableOsuTabDropdownMenuItem(item);
result.AccentColour.BindTo(AccentColour);
return result;
}
protected override DrawableMenuItem CreateDrawableMenuItem(MenuItem item) => new DrawableOsuTabDropdownMenuItem(item) { AccentColour = AccentColour };
private class DrawableOsuTabDropdownMenuItem : DrawableOsuDropdownMenuItem
{
@ -199,6 +182,20 @@ namespace osu.Game.Graphics.UserInterface
protected class OsuTabDropdownHeader : OsuDropdownHeader
{
public override Color4 AccentColour
{
get
{
return base.AccentColour;
}
set
{
base.AccentColour = value;
Foreground.Colour = value;
}
}
public OsuTabDropdownHeader()
{
RelativeSizeAxes = Axes.None;
@ -227,13 +224,6 @@ namespace osu.Game.Graphics.UserInterface
};
Padding = new MarginPadding { Left = 5, Right = 5 };
AccentColour.ValueChanged += accentColourChanged;
}
private void accentColourChanged(Color4? newValue)
{
Foreground.Colour = newValue ?? Color4.White;
}
protected override bool OnHover(InputState state)

View File

@ -36,7 +36,7 @@ namespace osu.Game.Overlays.Direct
[BackgroundDependencyLoader(true)]
private void load(OsuGame game, RulesetStore rulesets, OsuColour colours)
{
DisplayStyleControl.Dropdown.AccentColour.Value = colours.BlueDark;
DisplayStyleControl.Dropdown.AccentColour = colours.BlueDark;
Ruleset.BindTo(game?.Ruleset ?? new Bindable<RulesetInfo> { Value = rulesets.GetRuleset(0) });
foreach (var r in rulesets.AllRulesets)

View File

@ -18,16 +18,10 @@ namespace osu.Game.Overlays.Music
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
AccentColour.Value = colours.Gray6;
AccentColour = colours.Gray6;
}
protected override DropdownHeader CreateHeader()
{
var newHeader = new CollectionsHeader();
newHeader.AccentColour.BindTo(AccentColour);
return newHeader;
}
protected override DropdownHeader CreateHeader() => new CollectionsHeader();
protected override DropdownMenu CreateMenu() => new CollectionsMenu();

View File

@ -12,13 +12,7 @@ namespace osu.Game.Overlays.SearchableList
{
public class SlimEnumDropdown<T> : OsuEnumDropdown<T>
{
protected override DropdownHeader CreateHeader()
{
var newHeader = new SlimDropdownHeader();
newHeader.AccentColour.BindTo(AccentColour);
return newHeader;
}
protected override DropdownHeader CreateHeader() => new SlimDropdownHeader();
protected override DropdownMenu CreateMenu() => new SlimMenu();

View File

@ -257,13 +257,7 @@ namespace osu.Game.Overlays.Settings.Sections.General
private class UserDropdown : OsuEnumDropdown<UserAction>
{
protected override DropdownHeader CreateHeader()
{
var newHeader = new UserDropdownHeader();
newHeader.AccentColour.BindTo(AccentColour);
return newHeader;
}
protected override DropdownHeader CreateHeader() => new UserDropdownHeader();
protected override DropdownMenu CreateMenu() => new UserDropdownMenu();
@ -280,7 +274,7 @@ namespace osu.Game.Overlays.Settings.Sections.General
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
AccentColour.Value = colours.Gray5;
AccentColour = colours.Gray5;
}
private class UserDropdownMenu : OsuDropdownMenu
@ -319,7 +313,7 @@ namespace osu.Game.Overlays.Settings.Sections.General
CornerRadius = 5;
}
protected override Drawable CreateContent() => new TextContainer
protected override Drawable CreateContent() => new Content
{
Label = { Margin = new MarginPadding { Left = UserDropdownHeader.LABEL_LEFT_MARGIN - 11 } }
};