Skip to content

Commit

Permalink
feat: 轨道色相变化功能
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYunPlayer committed Nov 21, 2024
1 parent 2a0e32c commit f8bc3ba
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 8 deletions.
3 changes: 3 additions & 0 deletions TuneLab/Configs/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal static class Settings
public static NotifiableProperty<double> ParameterBoundaryExtension { get; } = DefaultSettings.ParameterBoundaryExtension;
public static NotifiableProperty<string> PianoKeySamplesPath { get; } = DefaultSettings.PianoKeySamplesPath;
public static NotifiableProperty<int> AutoSaveInterval { get; } = DefaultSettings.AutoSaveInterval;
public static NotifiableProperty<double> TrackHueChangeRate { get; } = DefaultSettings.TrackHueChangeRate;

public static void Init(string path)
{
Expand All @@ -45,6 +46,7 @@ public static void Init(string path)
ParameterBoundaryExtension.Value = settingsFile.ParameterBoundaryExtension;
PianoKeySamplesPath.Value = settingsFile.PianoKeySamplesPath;
AutoSaveInterval.Value = settingsFile.AutoSaveInterval;
TrackHueChangeRate.Value = settingsFile.TrackHueChangeRate;
}

public static void Save(string path)
Expand All @@ -59,6 +61,7 @@ public static void Save(string path)
ParameterBoundaryExtension = ParameterBoundaryExtension,
PianoKeySamplesPath = PianoKeySamplesPath,
AutoSaveInterval = AutoSaveInterval,
TrackHueChangeRate = TrackHueChangeRate
}, JsonSerializerOptions);

File.WriteAllText(path, content);
Expand Down
1 change: 1 addition & 0 deletions TuneLab/Configs/SettingsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ internal class SettingsFile
public double ParameterBoundaryExtension { get; set; } = 5;
public string PianoKeySamplesPath { get; set; } = string.Empty;
public int AutoSaveInterval { get; set; } = 10;
public double TrackHueChangeRate { get; set; } = 0;
}
1 change: 1 addition & 0 deletions TuneLab/Resources/Translations/zh-CN.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"Piano Key Samples" = "钢琴音采样"
"Auto Save Interval (second)" = "自动保存间隔(秒)"
"Parameter Boundary Extension (tick)" = "参数边界扩展(tick)"
"Track Hue Change Rate (degree/second)" = "轨道色相变化速率(度/秒)"

[FunctionBar]
"Play" = "播放"
Expand Down
73 changes: 73 additions & 0 deletions TuneLab/UI/MainWindow/Editor/Common/TrackColorManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Avalonia.Media;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TuneLab.Configs;
using TuneLab.Data;
using TuneLab.GUI;

namespace TuneLab.UI;

internal static class TrackColorManager
{
public static double HueChangeRate
{
get => mHueChangeRate;
set
{
if (mHueChangeRate == value)
return;

mHueChangeRate = value;
if (mHueChangeRate == 0)
timer.Stop();
else
timer.Start();
}
}

public static Color GetColor(this ITrack track)
{
var color = GetFixedColor(track);
var hsv = color.ToHsv();
var newHSV = new HsvColor(hsv.A, (hsv.H + offset) % 360, hsv.S, hsv.V);
return newHSV.ToRgb();
}

public static Color GetFixedColor(this ITrack track)
{
if (Color.TryParse(track.Color.Value, out var color))
return color;

return Style.DefaultTrackColor;
}

public static void RegisterOnTrackColorUpdated(this Avalonia.Visual visual, Action? action = null)
{
var context = SynchronizationContext.Current;
if (context == null)
return;

timer.Elapsed += (s, e) => { context.Post(_ => { visual.InvalidateVisual(); action?.Invoke(); }, null); };
}

static TrackColorManager()
{
timer.Elapsed += (s, e) =>
{
offset += HueChangeRate / 1000 * timer.Interval;
offset %= 360;
if (offset < 0)
offset += 360;
};
HueChangeRate = Settings.TrackHueChangeRate;
Settings.TrackHueChangeRate.Modified.Subscribe(() => HueChangeRate = Settings.TrackHueChangeRate);
}

static System.Timers.Timer timer = new System.Timers.Timer() { Interval = 16.5 };
static double offset = 0;
static double mHueChangeRate = 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public TrackHead()
this.AddDock(bottomArea);

mTrackProvider.When(track => track.Color.Modified).Subscribe(() => { if (Track == null) return; mIndexLabel.Background = Track.GetColor().ToBrush(); mIndexPanel.Background = Track.GetColor().ToBrush(); }, s);
mIndexPanel.RegisterOnTrackColorUpdated(() => { if (Track == null) return; mIndexLabel.Background = Track.GetColor().ToBrush(); mIndexPanel.Background = Track.GetColor().ToBrush(); });
mTrackProvider.ObjectWillChange.Subscribe(() =>
{
if (Track == null)
Expand Down Expand Up @@ -245,6 +246,7 @@ public TrackHead()
ContextMenu = menu;
Background = Brushes.Transparent;

this.RegisterOnTrackColorUpdated();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public TrackScrollView(IDependency dependency)
AddHandler(DragDrop.DragOverEvent, OnDragOver);
AddHandler(DragDrop.DragLeaveEvent, OnDragLeave);
AddHandler(DragDrop.DropEvent, OnDrop);

this.RegisterOnTrackColorUpdated(InvalidateVisual);
}

~TrackScrollView()
Expand Down
15 changes: 15 additions & 0 deletions TuneLab/UI/Settings/SettingsWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ public SettingsWindow()
}
listView.Content.Children.Add(panel);
}
{
var panel = new DockPanel() { Margin = new(24, 12) };
{
var slider = new SliderController() { Width = 180, IsInterger = true };
slider.SetRange(-720, 720);
slider.SetDefaultValue(Settings.DefaultSettings.TrackHueChangeRate);
slider.Bind(Settings.TrackHueChangeRate, true, s);
panel.AddDock(slider, Dock.Right);
}
{
var name = new TextBlock() { Text = "Track Hue Change Rate (degree/second)".Tr(this) + ": ", VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center };
panel.AddDock(name);
}
listView.Content.Children.Add(panel);
}
Content.AddDock(listView);
}

Expand Down
8 changes: 0 additions & 8 deletions TuneLab/Utils/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,6 @@ public static void AddDock(this DockPanel panel, Control control)
panel.Children.Add(control);
}

public static Color GetColor(this ITrack track)
{
if (Color.TryParse(track.Color.Value, out var color))
return color;

return Style.DefaultTrackColor;
}

public static void NewTrack(this IProject project)
{
project.AddTrack(new TrackInfo() { Name = "Track".Tr(TC.Document) + "_" + (project.Tracks.Count + 1), Color = Style.GetNewColor(project.Tracks.Count) });
Expand Down

0 comments on commit f8bc3ba

Please sign in to comment.