Merge branch 'saved-colors' of https://github.com/Tryibion/FlaxEngine into Tryibion-saved-colors

This commit is contained in:
Wojtek Figat
2023-12-17 13:03:57 +01:00

View File

@@ -3,6 +3,8 @@
using FlaxEditor.GUI.Input;
using FlaxEngine;
using FlaxEngine.GUI;
using FlaxEngine.Json;
using System.Collections.Generic;
namespace FlaxEditor.GUI.Dialogs
{
@@ -30,6 +32,8 @@ namespace FlaxEditor.GUI.Dialogs
private const float HSVMargin = 0.0f;
private const float ChannelsMargin = 4.0f;
private const float ChannelTextWidth = 12.0f;
private const float SavedColorButtonWidth = 20.0f;
private const float SavedColorButtonHeight = 20.0f;
private Color _initialValue;
private Color _value;
@@ -52,6 +56,9 @@ namespace FlaxEditor.GUI.Dialogs
private Button _cOK;
private Button _cEyedropper;
private List<Color> _savedColors = new List<Color>();
private List<Button> _savedColorButtons = new List<Button>();
/// <summary>
/// Gets the selected color.
/// </summary>
@@ -111,6 +118,12 @@ namespace FlaxEditor.GUI.Dialogs
_onChanged = colorChanged;
_onClosed = pickerClosed;
// Get saved colors if they exist
if (Editor.Instance.ProjectCache.TryGetCustomData("ColorPickerSavedColors", out var savedColors))
{
_savedColors = JsonSerializer.Deserialize<List<Color>>(savedColors);
}
// Selector
_cSelector = new ColorSelectorWithSliders(180, 18)
{
@@ -195,6 +208,9 @@ namespace FlaxEditor.GUI.Dialogs
};
_cOK.Clicked += OnSubmit;
// Create saved color buttons
CreateAllSaveButtons();
// Eyedropper button
var style = Style.Current;
_cEyedropper = new Button(_cOK.X - EyedropperMargin, _cHex.Bottom + PickerMargin)
@@ -216,6 +232,50 @@ namespace FlaxEditor.GUI.Dialogs
SelectedColor = initialValue;
}
private void OnSavedColorButtonClicked(Button button)
{
if (button.Tag == null)
{
// Prevent setting same color 2 times... because why...
foreach (var color in _savedColors)
{
if (color == _value)
{
return;
}
}
// Set color of button to current value;
button.BackgroundColor = _value;
button.BackgroundColorHighlighted = _value;
button.BackgroundColorSelected = _value.RGBMultiplied(0.8f);
button.Text = "";
button.Tag = _value;
// Save new colors
_savedColors.Add(_value);
var savedColors = JsonSerializer.Serialize(_savedColors, typeof(List<Color>));
Editor.Instance.ProjectCache.SetCustomData("ColorPickerSavedColors", savedColors);
// create new + button
if (_savedColorButtons.Count < 8)
{
var savedColorButton = new Button(PickerMargin * (_savedColorButtons.Count + 1) + SavedColorButtonWidth * _savedColorButtons.Count, Height - SavedColorButtonHeight - PickerMargin, SavedColorButtonWidth, SavedColorButtonHeight)
{
Text = "+",
Parent = this,
Tag = null,
};
savedColorButton.ButtonClicked += (b) => OnSavedColorButtonClicked(b);
_savedColorButtons.Add(savedColorButton);
}
}
else
{
SelectedColor = (Color)button.Tag;
}
}
private void OnColorPicked(Color32 colorPicked)
{
if (_activeEyedropper)
@@ -340,6 +400,111 @@ namespace FlaxEditor.GUI.Dialogs
return base.OnKeyDown(key);
}
/// <inheritdoc />
public override bool OnMouseUp(Float2 location, MouseButton button)
{
if (base.OnMouseUp(location, button))
{
return true;
}
var child = GetChildAtRecursive(location);
if (button == MouseButton.Right && child is Button b && b.Tag is Color c)
{
// Show menu
var menu = new ContextMenu.ContextMenu();
var replaceButton = menu.AddButton("Replace");
replaceButton.Clicked += () => OnSavedColorReplace(b);
var deleteButton = menu.AddButton("Delete");
deleteButton.Clicked += () => OnSavedColorDelete(b);
_disableEvents = true;
menu.Show(this, location);
menu.VisibleChanged += (c) => _disableEvents = false;
return true;
}
return false;
}
private void OnSavedColorReplace(Button button)
{
// Prevent setting same color 2 times... because why...
foreach (var color in _savedColors)
{
if (color == _value)
{
return;
}
}
// Set new Color in spot
for (int i = 0; i < _savedColors.Count; i++)
{
var color = _savedColors[i];
if (color == (Color)button.Tag)
{
color = _value;
}
}
// Set color of button to current value;
button.BackgroundColor = _value;
button.BackgroundColorHighlighted = _value;
button.Text = "";
button.Tag = _value;
// Save new colors
var savedColors = JsonSerializer.Serialize(_savedColors, typeof(List<Color>));
Editor.Instance.ProjectCache.SetCustomData("ColorPickerSavedColors", savedColors);
}
private void OnSavedColorDelete(Button button)
{
_savedColors.Remove((Color)button.Tag);
foreach (var b in _savedColorButtons)
{
Children.Remove(b);
}
_savedColorButtons.Clear();
CreateAllSaveButtons();
// Save new colors
var savedColors = JsonSerializer.Serialize(_savedColors, typeof(List<Color>));
Editor.Instance.ProjectCache.SetCustomData("ColorPickerSavedColors", savedColors);
}
private void CreateAllSaveButtons()
{
// Create saved color buttons
for (int i = 0; i < _savedColors.Count; i++)
{
var savedColor = _savedColors[i];
var savedColorButton = new Button(PickerMargin * (i + 1) + SavedColorButtonWidth * i, Height - SavedColorButtonHeight - PickerMargin, SavedColorButtonWidth, SavedColorButtonHeight)
{
Parent = this,
Tag = savedColor,
BackgroundColor = savedColor,
BackgroundColorHighlighted = savedColor,
BackgroundColorSelected = savedColor.RGBMultiplied(0.8f),
};
savedColorButton.ButtonClicked += (b) => OnSavedColorButtonClicked(b);
_savedColorButtons.Add(savedColorButton);
}
if (_savedColors.Count < 8)
{
var savedColorButton = new Button(PickerMargin * (_savedColors.Count + 1) + SavedColorButtonWidth * _savedColors.Count, Height - SavedColorButtonHeight - PickerMargin, SavedColorButtonWidth, SavedColorButtonHeight)
{
Text = "+",
Parent = this,
Tag = null,
};
savedColorButton.ButtonClicked += (b) => OnSavedColorButtonClicked(b);
_savedColorButtons.Add(savedColorButton);
}
}
/// <inheritdoc />
public override void OnSubmit()
{