Skip to content

Commit

Permalink
Major changes introducing document contexts and selection command han…
Browse files Browse the repository at this point in the history
…dling
  • Loading branch information
ReMinoer committed May 15, 2021
1 parent 5361e3c commit e987720
Show file tree
Hide file tree
Showing 51 changed files with 743 additions and 279 deletions.
11 changes: 11 additions & 0 deletions Calame.DocumentContexts/Calame.DocumentContexts.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
<UseWPF>True</UseWPF>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\External\Glyph\External\Fingear\Fingear\Fingear.csproj" />
<ProjectReference Include="..\External\Glyph\Glyph.Composition\Glyph.Composition.csproj" />
<ProjectReference Include="..\External\Glyph\Glyph\Glyph.csproj" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions Calame.DocumentContexts/IRootComponentsContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using Glyph.Composition;

namespace Calame.DocumentContexts
{
public interface IRootComponentsContext
{
IEnumerable<IGlyphComponent> RootComponents { get; }
}
}
10 changes: 10 additions & 0 deletions Calame.DocumentContexts/IRootInteractivesContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using Fingear.Interactives;

namespace Calame.DocumentContexts
{
public interface IRootInteractivesContext
{
IEnumerable<IInteractive> RootInteractives { get; }
}
}
10 changes: 10 additions & 0 deletions Calame.DocumentContexts/IRootScenesContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using Glyph;

namespace Calame.DocumentContexts
{
public interface IRootScenesContext
{
IEnumerable<ISceneNode> RootScenes { get; }
}
}
9 changes: 9 additions & 0 deletions Calame.DocumentContexts/IRootsContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections;

namespace Calame.DocumentContexts
{
public interface IRootsContext
{
IEnumerable Roots { get; }
}
}
20 changes: 20 additions & 0 deletions Calame.DocumentContexts/ISelectionCommandContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Calame.DocumentContexts
{
public interface ISelectionCommandContext
{
ICommand SelectCommand { get; }
event EventHandler CanSelectChanged;
bool CanSelect(object instance);
Task SelectAsync(object instance);
}

public interface ISelectionCommandContext<in T> : ISelectionCommandContext
{
bool CanSelect(T instance);
Task SelectAsync(T instance);
}
}
1 change: 1 addition & 0 deletions Calame.Icons/Providers/CalameIconProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protected override PackIconMaterialKind GetTargetKey(CalameIconKey key)
case CalameIconKey.Pause: return PackIconMaterialKind.Pause;
case CalameIconKey.Stop: return PackIconMaterialKind.Stop;
case CalameIconKey.NextFrame: return PackIconMaterialKind.SkipNext;
case CalameIconKey.ViewerDebugMode: return PackIconMaterialKind.ApplicationCog;
case CalameIconKey.ResetSession: return PackIconMaterialKind.RotateLeft;

case CalameIconKey.DefaultCamera: return PackIconMaterialKind.AppleAirplay;
Expand Down
1 change: 1 addition & 0 deletions Calame.Viewer/Calame.Viewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<UseWPF>True</UseWPF>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Calame.DocumentContexts\Calame.DocumentContexts.csproj" />
<ProjectReference Include="..\Calame\Calame.csproj" />
<ProjectReference Include="..\External\Glyph\External\Fingear\External\Stave\Diese.Collections %28Stave%29\Diese.Collections %28Stave%29.csproj" />
<ProjectReference Include="..\External\Glyph\External\Fingear\External\Stave\Stave\Stave.csproj" />
Expand Down
31 changes: 31 additions & 0 deletions Calame.Viewer/Commands/ViewerDebugModeCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Threading.Tasks;
using Calame.Commands.Base;
using Calame.Icons;
using Calame.Viewer.Commands.Base;
using Gemini.Framework.Commands;

namespace Calame.Viewer.Commands
{
[CommandDefinition]
public class ViewerDebugModeCommand : CalameCommandDefinitionBase
{
public override string Text => "Viewer _Debug Mode";
public override object IconKey => CalameIconKey.ViewerDebugMode;

[CommandHandler]
public class CommandHandler : ViewerDocumentCommandHandlerBase<IViewerDocument, ViewerDebugModeCommand>
{
protected override void UpdateStatus(Command command, IViewerDocument document)
{
base.UpdateStatus(command, document);
command.Checked = document?.DebugMode ?? false;
}

protected override Task RunAsync(Command command, IViewerDocument document)
{
document.DebugMode = !document.DebugMode;
return Task.CompletedTask;
}
}
}
}
148 changes: 148 additions & 0 deletions Calame.Viewer/DebuggableViewerContexts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Calame.DocumentContexts;
using Calame.Viewer.ViewModels;
using Caliburn.Micro;
using Fingear.Interactives;
using Glyph;
using Glyph.Composition;
using Glyph.Engine;
using Stave;

namespace Calame.Viewer
{
public class DebuggableViewerContexts : PropertyChangedBase, IRootsContext, IRootComponentsContext, IRootScenesContext, IRootInteractivesContext
{
private ViewerViewModel _viewer;
public ViewerViewModel Viewer
{
get => _viewer;
private set => Set(ref _viewer, value);
}

private IEnumerable _roots;
public IEnumerable Roots
{
get => _roots;
private set => Set(ref _roots, value);
}

private IEnumerable<IGlyphComponent> _rootComponents;
public IEnumerable<IGlyphComponent> RootComponents
{
get => _rootComponents;
private set => Set(ref _rootComponents, value);
}

private IEnumerable<IInteractive> _rootInteractives;
public IEnumerable<IInteractive> RootInteractives
{
get => _rootInteractives;
private set => Set(ref _rootInteractives, value);
}

private bool _debugMode;
public bool DebugMode
{
get => _debugMode;
set
{
if (Set(ref _debugMode, value))
RefreshContexts();
}
}

public GlyphEngine Engine => _viewer.Runner.Engine;
public IEnumerable<ISceneNode> RootScenes => _viewer.Runner.Engine.ProjectionManager.SceneRoots;

private IGlyphComponent _userParentComponent;

public IGlyphComponent UserParentComponent
{
get => _userParentComponent;
set
{
if (_userParentComponent == value)
return;

_userParentComponent = value;

if (!DebugMode)
RefreshContexts();
}
}

public DebuggableViewerContexts(ViewerViewModel viewer, ISelectionCommandContext selectionCommandContext)
{
Viewer = viewer;
SelectCommand = new SelectionCommand(selectionCommandContext);
}

public void RefreshContexts()
{
if (DebugMode)
{
RootComponents = new IGlyphComponent[] { _viewer.Runner.Engine.Root };
RootInteractives = new IInteractive[] { _viewer.Runner.Engine.InteractionManager.Root };
}
else
{
RootComponents = (UserParentComponent ?? _viewer.UserRoot).Components;
RootInteractives = _viewer.InteractiveModes.Where(x => x.IsUserMode).Select(x => x.Interactive);
}

Roots = RootComponents;
CanSelectChanged?.Invoke(this, EventArgs.Empty);
}

public ICommand SelectCommand { get; }
public event EventHandler CanSelectChanged;

public bool CanSelect(IGlyphComponent component)
{
if (DebugMode)
return CanSelectInDebugMode(component);

return CanSelectInUserMode(component);
}

private bool CanSelectInDebugMode(IGlyphComponent component)
{
return CanSelectBase(component)
&& !component.AndAllParents().Any(_viewer.NotSelectableComponents.Contains);
}

private bool CanSelectInUserMode(IGlyphComponent component)
{
return CanSelectBase(component)
&& component.AllParents().Contains(UserParentComponent ?? _viewer.UserRoot);
}

private bool CanSelectBase(IGlyphComponent component)
{
return component != null && !component.GetType().IsValueType;
}

private class SelectionCommand : ICommand
{
private readonly ISelectionCommandContext _context;

public SelectionCommand(ISelectionCommandContext context)
{
_context = context;
}

public event EventHandler CanExecuteChanged
{
add => _context.CanSelectChanged += value;
remove => _context.CanSelectChanged -= value;
}

public bool CanExecute(object parameter) => _context.CanSelect(parameter);
public void Execute(object parameter) => _context.SelectAsync(parameter).Wait();
}
}
}
16 changes: 14 additions & 2 deletions Calame.Viewer/IViewerDocument.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
using Calame.Viewer.ViewModels;
using Calame.DocumentContexts;
using Calame.Viewer.ViewModels;
using Gemini.Framework;
using Glyph.Composition;
using Glyph.Engine;

namespace Calame.Viewer
{
public interface IViewerDocument : IDocument, IViewerViewModelOwner, IDocumentContext<GlyphEngine>, IDocumentContext<ViewerViewModel>, IDocumentContext<IComponentFilter>
public interface IViewerDocument : IDocument, IViewerViewModelOwner,
IDocumentContext<ViewerViewModel>,
IDocumentContext<GlyphEngine>,
IDocumentContext<IRootsContext>,
IDocumentContext<IRootComponentsContext>,
IDocumentContext<IRootScenesContext>,
IDocumentContext<IRootInteractivesContext>,
IDocumentContext<ISelectionCommandContext>,
IDocumentContext<ISelectionCommandContext<IGlyphComponent>>,
ISelectionCommandContext<IGlyphComponent>
{
ViewerViewModel Viewer { get; }
bool DebugMode { get; set; }
void EnableFreeCamera();
}
}
1 change: 1 addition & 0 deletions Calame.Viewer/IViewerInteractiveMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public interface IViewerInteractiveMode
IInteractive Interactive { get; }
Cursor Cursor { get; }
bool UseFreeCamera { get; }
bool IsUserMode { get; }
}
}
13 changes: 7 additions & 6 deletions Calame.Viewer/Modules/BoxedComponentSelectorModule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Calame.DocumentContexts;
using Calame.Viewer.Modules.Base;
using Calame.Viewer.ViewModels;
using Caliburn.Micro;
Expand All @@ -19,8 +20,7 @@ namespace Calame.Viewer.Modules
public class BoxedComponentSelectorModule : ViewerModuleBase, IHandle<IDocumentContext<ViewerViewModel>>
{
private readonly IEventAggregator _eventAggregator;
private IDocumentContext _currentDocument;
private IDocumentContext<IComponentFilter> _filteringContext;
private ISelectionCommandContext _selectionCommandContext;

private GlyphObject _root;
private ShapedObjectSelector _shapedObjectSelector;
Expand Down Expand Up @@ -68,7 +68,7 @@ protected override void DisconnectRunner()

private bool ComponentFilter(IGlyphComponent glyphComponent)
{
return _filteringContext?.Context.Filter(glyphComponent) ?? true;
return _selectionCommandContext?.CanSelect(glyphComponent) ?? true;
}

private async void OnShapedObjectSelectorSelectionChanged(object sender, IBoxedComponent boxedComponent)
Expand All @@ -77,13 +77,14 @@ private async void OnShapedObjectSelectorSelectionChanged(object sender, IBoxedC
return;

SelectedComponent = boxedComponent?.AllParents().OfType<IBoxedComponent>().First(x => x.Components.AnyOfType<ISceneNodeComponent>());
await _eventAggregator.PublishAsync(new SelectionRequest<IBoxedComponent>(_currentDocument, SelectedComponent));

if (_selectionCommandContext != null)
await _selectionCommandContext.SelectAsync(SelectedComponent);
}

Task IHandle<IDocumentContext<ViewerViewModel>>.HandleAsync(IDocumentContext<ViewerViewModel> message, CancellationToken cancellationToken)
{
_currentDocument = message;
_filteringContext = message as IDocumentContext<IComponentFilter>;
_selectionCommandContext = (message as IDocumentContext<ISelectionCommandContext>)?.Context;

return Task.CompletedTask;
}
Expand Down
2 changes: 1 addition & 1 deletion Calame.Viewer/Modules/SelectionRendererModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected override void HandleComponent(IGlyphComponent selection)
if (!(selection is IBoxedComponent boxedSelection))
return;

_root = Model.EditorModeRoot.Add<GlyphObject>(Model.ComponentsFilter.ExcludedRoots.Add);
_root = Model.EditorModeRoot.Add<GlyphObject>(beforeAdding: Model.NotSelectableComponents.Add);
_root.Add<SceneNode>();

var lineMesh = new LineMesh(Color.Purple);
Expand Down
4 changes: 2 additions & 2 deletions Calame.Viewer/Modules/TransformationEditorModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override void HandleComponent(IGlyphComponent selection)
if (sceneNode == null)
return;

var transformationEditor = Model.EditorModeRoot.Add<MultiModeTransformationEditor>(beforeAdding: Model.ComponentsFilter.ExcludedRoots.Add);
var transformationEditor = Model.EditorModeRoot.Add<MultiModeTransformationEditor>(beforeAdding: Model.NotSelectableComponents.Add);
transformationEditor.EditedObject = new MultiModeTransformationController(sceneNode);
transformationEditor.RaycastClient = Model.Client;

Expand All @@ -51,7 +51,7 @@ protected override void HandleData(IGlyphData selection)
if (controller.Anchor == null)
return;

var transformationEditor = Model.EditorModeRoot.Add<TransformationEditor>(beforeAdding: Model.ComponentsFilter.ExcludedRoots.Add);
var transformationEditor = Model.EditorModeRoot.Add<TransformationEditor>(beforeAdding: Model.NotSelectableComponents.Add);
transformationEditor.EditedObject = controller;
transformationEditor.RaycastClient = Model.Client;

Expand Down
Loading

0 comments on commit e987720

Please sign in to comment.