-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up a control that's essentially the same as ngIf in Angular.
- Loading branch information
1 parent
bdfc945
commit 713c281
Showing
4 changed files
with
163 additions
and
70 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
FinModelUtility/UniversalAssetTool/UniversalAssetTool.Ui.Avalonia/common/controls/If.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Avalonia; | ||
using Avalonia.Controls; | ||
using Avalonia.Layout; | ||
using Avalonia.Markup.Xaml.Templates; | ||
using Avalonia.Metadata; | ||
|
||
namespace uni.ui.avalonia.common.controls; | ||
|
||
public class If : Control { | ||
private object deferredContent_; | ||
private Control control_; | ||
|
||
public static readonly StyledProperty<bool> ConditionProperty | ||
= AvaloniaProperty.Register<If, bool>( | ||
"Condition"); | ||
|
||
public bool Condition { | ||
get => this.GetValue(ConditionProperty); | ||
set => this.SetValue(ConditionProperty, value); | ||
} | ||
|
||
[Content, TemplateContent] | ||
public object DeferredContent { | ||
get => this.deferredContent_; | ||
set { | ||
this.deferredContent_ = value; | ||
if (this.Condition) { | ||
this.DoLoad_(true); | ||
} | ||
} | ||
} | ||
|
||
public Control Control => this.control_; | ||
|
||
static If() { | ||
ConditionProperty.Changed.AddClassHandler<If>( | ||
(c, e) => { | ||
if (e.NewValue is bool v) { | ||
c.DoLoad_(v); | ||
} | ||
}); | ||
} | ||
|
||
protected override Size MeasureOverride(Size availableSize) | ||
=> LayoutHelper.MeasureChild(this.control_, availableSize, default); | ||
|
||
protected override Size ArrangeOverride(Size finalSize) | ||
=> LayoutHelper.ArrangeChild(this.control_, finalSize, default); | ||
|
||
|
||
private void DoLoad_(bool load) { | ||
if ((this.control_ != null) == load) | ||
return; | ||
|
||
if (load) { | ||
this.control_ = TemplateContent.Load(this.DeferredContent).Result; | ||
((ISetLogicalParent) this.control_).SetParent(this); | ||
this.VisualChildren.Add(this.control_); | ||
this.LogicalChildren.Add(this.control_); | ||
} else { | ||
((ISetLogicalParent) this.control_).SetParent(null); | ||
this.LogicalChildren.Clear(); | ||
this.VisualChildren.Remove(this.control_); | ||
this.control_ = null; | ||
} | ||
|
||
this.InvalidateMeasure(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...rsalAssetTool/UniversalAssetTool.Ui.Avalonia/common/treeViews/FileBundleTreeViewModels.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Collections.ObjectModel; | ||
|
||
using fin.audio.io; | ||
using fin.audio.io.importers.ogg; | ||
using fin.image; | ||
using fin.io; | ||
using fin.io.bundles; | ||
using fin.model.io; | ||
using fin.scene; | ||
using fin.util.asserts; | ||
|
||
using grezzo.api; | ||
|
||
using uni.ui.avalonia.resources; | ||
using uni.ui.avalonia.ViewModels; | ||
|
||
using IImage = Avalonia.Media.IImage; | ||
|
||
namespace uni.ui.avalonia.common.treeViews { | ||
|
||
// Top-level view model types | ||
public class FileBundleTreeViewModel<T> | ||
: ViewModelBase, IFilterTreeViewViewModel<T> { | ||
public ObservableCollection<INode<T>> Nodes { get; protected set; } | ||
|
||
public event EventHandler<INode<T>>? NodeSelected; | ||
|
||
public void ChangeSelection(INode node) | ||
=> this.NodeSelected?.Invoke(this, Asserts.AsA<INode<T>>(node)); | ||
} | ||
|
||
public class FileBundleTreeViewModel : FileBundleTreeViewModel<IAnnotatedFileBundle> { | ||
public FileBundleTreeViewModel() { | ||
this.Nodes = [ | ||
new FileBundleDirectoryNode("Animals", | ||
[ | ||
new FileBundleDirectoryNode("Mammals", | ||
[ | ||
new FileBundleLeafNode("Lion", new CmbModelFileBundle("foo", new FinFile()).Annotate(null)), | ||
new FileBundleLeafNode("Cat", new OggAudioFileBundle(new FinFile()).Annotate(null)) | ||
]) | ||
]) | ||
]; | ||
} | ||
} | ||
|
||
// Node types | ||
public class FileBundleDirectoryNode( | ||
string label, | ||
ObservableCollection<INode<IAnnotatedFileBundle>>? subNodes = null) | ||
: ViewModelBase, INode<IAnnotatedFileBundle> { | ||
public ObservableCollection<INode<IAnnotatedFileBundle>>? SubNodes { get; } | ||
= subNodes; | ||
|
||
public IImage? Icon => null; | ||
|
||
public string Label { get; } = label; | ||
} | ||
|
||
public class FileBundleLeafNode(string label, IAnnotatedFileBundle data) | ||
: ViewModelBase, INode<IAnnotatedFileBundle> { | ||
public ObservableCollection<INode<IAnnotatedFileBundle>>? SubNodes => null; | ||
|
||
public static readonly IImage MUSIC_ICON | ||
= EmbeddedResourceUtil.LoadAvaloniaImage("music"); | ||
|
||
public static readonly IImage PICTURE_ICON | ||
= EmbeddedResourceUtil.LoadAvaloniaImage("picture"); | ||
|
||
public static readonly IImage MODEL_ICON | ||
= EmbeddedResourceUtil.LoadAvaloniaImage("model"); | ||
|
||
public static readonly IImage SCENE_ICON | ||
= EmbeddedResourceUtil.LoadAvaloniaImage("scene"); | ||
|
||
public IImage Icon => data.FileBundle switch { | ||
IAudioFileBundle => MUSIC_ICON, | ||
IImageFileBundle => PICTURE_ICON, | ||
IModelFileBundle => MODEL_ICON, | ||
ISceneFileBundle => SCENE_ICON, | ||
}; | ||
|
||
public string Label { get; } = label; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters