Skip to content

Commit

Permalink
feat: Add color indicators on pattern preview
Browse files Browse the repository at this point in the history
  • Loading branch information
WEGFan committed Jan 9, 2022
1 parent 9d8de8c commit 8d7db3a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions Code/UI/PatternPreview.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Monocle;

Expand All @@ -7,6 +9,7 @@ public class PatternPreview : TextMenu.Item {
public TrailPattern Pattern { get; set; }

private readonly VirtualTexture previewTexture;
private readonly MTexture indicator;
private BeforeRenderHook beforeRenderHook;

private const int GradientBarWidth = 1000;
Expand All @@ -15,6 +18,7 @@ public class PatternPreview : TextMenu.Item {
public PatternPreview(TrailPattern pattern) {
Pattern = pattern;
previewTexture = VirtualContent.CreateTexture("pattern-preview", GradientBarWidth, 1, Color.White);
indicator = GFX.Gui["Trailine/patternPreview/indicator"];
}

public override void Added() {
Expand All @@ -30,7 +34,7 @@ public override float LeftWidth() {
}

public override float Height() {
return 60f;
return 80f;
}

public void BeforeRender() {
Expand All @@ -43,8 +47,27 @@ public void BeforeRender() {

public override void Render(Vector2 position, bool highlighted) {
float menuCenterX = position.X + Container.Width / 2;
new MTexture(previewTexture).DrawJustified(new Vector2(menuCenterX, position.Y), new Vector2(0.5f, 0.5f),
new MTexture(previewTexture).DrawJustified(new Vector2(menuCenterX, position.Y), new Vector2(0.5f, 0f),
Color.White, new Vector2(1f, GradientBarHeight));

Vector2 left = new Vector2(menuCenterX, position.Y) - new Vector2(GradientBarWidth / 2f, 0f);

if (Pattern.ColorStops.Count == 1) {
indicator.DrawJustified(left, new Vector2(0.5f, 1f), Pattern.ColorStops[0].Color, 1f);
indicator.DrawJustified(left + new Vector2(GradientBarWidth, 0), new Vector2(0.5f, 1f), Pattern.ColorStops[0].Color, 1f);
} else {
List<float> weightAccumulated = new List<float>(Pattern.ColorStops.Count) {0};
float currentWeight = 0;
for (int i = 1; i < Pattern.ColorStops.Count; i++) {
currentWeight += Pattern.ColorStops[i].Width;
weightAccumulated.Add(currentWeight);
}
float weightSum = weightAccumulated.Last();
List<float> offsets = weightAccumulated.Select(i => GradientBarWidth * (i / weightSum)).ToList();
for (int i = 0; i < offsets.Count; i++) {
indicator.DrawJustified(left + new Vector2(offsets[i], 0), new Vector2(0.5f, 1f), Pattern.ColorStops[i].Color, 1f);
}
}
}

public static void Load() {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8d7db3a

Please sign in to comment.