rokojori_action_library/Runtime/UI/Styling/UISelector.cs

222 lines
5.3 KiB
C#

using Godot;
using Rokojori;
using System.Collections.Generic;
using System.Linq;
namespace Rokojori
{
[Tool]
[GlobalClass,Icon("res://addons/rokojori_action_library/Icons/UISelector.svg")]
public partial class UISelector:Resource
{
public enum Value
{
___,
True,
False
// True_For_Any_Parent,
// False_For_All_Parents
}
[Export]
public Value hover = Value.___;
[Export]
public Value dragging = Value.___;
[Export]
public Value scrolling = Value.___;
[Export]
public Value focus = Value.___;
[Export]
public Value active = Value.___;
[Export]
public UISelectorFlagEntry[] selectors = [];
bool Matches( UIStylePropertyContainer container, Value value, UISelectorFlag flag )
{
if ( Value.___ == value )
{
return true;
}
var containsFlag = container.GetUISelectorFlags().Contains( flag );
var needsFlag = ( Value.True == value );
// if ( container.HasDebugFlag() )
// {
// this.LogInfo(
// "Checking for flag",
// "container:", container,
// "value:",value,
// "flag:", flag,
// "containsFlag:", containsFlag,
// "needsFlag:", needsFlag
// );
// }
if ( Value.True == value || Value.False == value )
{
return containsFlag == needsFlag;
}
// if ( Value.True_For_Any_Parent == value || Value.False_For_All_Parents == value )
// {
// return container.GetParentUISelectorFlags().Contains( flag ) == ( Value.True_For_Any_Parent == value );
// }
return true;
}
public bool Selects( UIStylePropertyContainer container )
{
// if ( container.HasDebugFlag() )
// {
// this.LogInfo(
// "Context:", context,
// "Hover:", hover, container.GetUISelectorFlags().Contains( UISelectorFlag.Hover ),
// "Dragging:", dragging, container.GetUISelectorFlags().Contains( UISelectorFlag.Dragging ),
// "Scrolling:", scrolling, container.GetUISelectorFlags().Contains( UISelectorFlag.Scrolling ),
// "Focus:", focus, container.GetUISelectorFlags().Contains( UISelectorFlag.Focus ),
// "Active:", active, container.GetUISelectorFlags().Contains( UISelectorFlag.Active )
// );
// }
if ( ! Matches( container, hover, UISelectorFlag.Hover ) )
{
// if ( container.HasDebugFlag() )
// {
// this.LogInfo( context, "Hover not matching" );
// }
return false;
}
if ( ! Matches( container, dragging, UISelectorFlag.Dragging ) )
{
// if ( container.HasDebugFlag() )
// {
// this.LogInfo( context,"Dragging not matching" );
// }
return false;
}
if ( ! Matches( container, scrolling, UISelectorFlag.Scrolling ) )
{
// if ( container.HasDebugFlag() )
// {
// this.LogInfo( context, "Scrolling not matching" );
// }
return false;
}
if ( ! Matches( container, focus, UISelectorFlag.Focus ) )
{
// if ( container.HasDebugFlag() )
// {
// this.LogInfo( context, "Focus not matching" );
// }
return false;
}
if ( ! Matches( container, active, UISelectorFlag.Active ) )
{
// if ( container.HasDebugFlag() )
// {
// this.LogInfo( context, "Active not matching" );
// }
return false;
}
if ( selectors == null || selectors.Length == 0 )
{
// if ( container.HasDebugFlag() )
// {
// this.LogInfo( context, "No selectors, everthing matched" );
// }
return true;
}
foreach ( var entry in selectors )
{
if ( entry == null || ! Matches( container, entry.value, entry.selectorFlag ) )
{
// if ( container.HasDebugFlag() )
// {
// this.LogInfo( context, "flag selector not matching", entry.value, entry.selectorFlag );
// }
return false;
}
}
// if ( container.HasDebugFlag() )
// {
// this.LogInfo( context, "Flag Selectors matched" );
// }
return true;
}
public static void UpdateParentUISelectorFlags( UIStylePropertyContainerNode target )
{
if ( target != null )
{
return;
}
var node = target as Node;
var walker = NodesWalker.Get();
CombineParentFlags( target );
walker.PruneChildTraversal( node,
( Node n ) =>
{
if ( n != node && n is UIStylePropertyContainerNode c )
{
UpdateParentUISelectorFlags( c );
return false;
}
else
{
return true;
}
}
);
}
static void CombineParentFlags( UIStylePropertyContainerNode target )
{
var parentFlags = target.GetParentUISelectorFlags();
parentFlags.Clear();
parentFlags.AddRange( target.GetUISelectorFlags() );
var parent = UI.GetStylePropertyContainerParent( target as Control);
if ( parent != null )
{
parentFlags.Union( parent.GetParentUISelectorFlags() );
}
target.SetDirty();
}
}
}