Skip to content

Commit

Permalink
Move dialog editor vm commands into partial class
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurumaker72 committed Nov 12, 2023
1 parent 6ed7d26 commit 8f13ea3
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 179 deletions.
179 changes: 0 additions & 179 deletions RsrcArchitect.ViewModels/DialogEditorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,186 +105,7 @@ private Vector2 RelativePositionToDialog(Vector2 position)
}

#endregion

#region Commands

[RelayCommand]
private void Close()
{
WeakReferenceMessenger.Default.Send(new DialogEditorViewModelClosingMessage(this));
}

[RelayCommand]
private void PointerPress(Vector2 position)
{
var dialogPosition = RelativePositionToDialog(position);

// do grip-test first, then store if it hits
var isGripHit = false;
(Transformation transformation, Sizing sizing) transformationOperation = (Transformation.None, Sizing.Empty);
if (SelectedNodes.Count > 0)
{
// if any candidate hits, we grabbed a control's grip
transformationOperation = SelectedNodes
.Select(x =>
TransformationHelper.GetCandidate(x.Data, dialogPosition, _dialogEditorSettingsViewModel.GripSize))
.FirstOrDefault(x => x.Item1 != Transformation.None, (Transformation.None, Sizing.Empty));

isGripHit = transformationOperation.transformation != Transformation.None;
}

// if no grip hits, we know we aren't starting to resize or move a control,
// thus we can select a new one
if (!isGripHit)
{
Debug.Print("No grip hit");
var newSelectedNode = GetControlNodeAtPosition(dialogPosition);
SelectedNodes.Clear();
if (newSelectedNode != null)
{
SelectedNodes.Add(GetControlNodeAtPosition(dialogPosition));
}
}

if (SelectedNodes.Count > 0)
{
Debug.Print("Start control transformation");
(_transformation, _sizing) = (transformationOperation.transformation, transformationOperation.sizing);
_transformationStartRectangles = SelectedNodes.Select(x => x.Data.Rectangle).ToList();
_transformationStartPointerPosition = dialogPosition;
}
else
{
// no node caught but stil clicked,
// so start translating the camera
Debug.Print("Start pan");
_isPanning = true;
_panStartPointerPosition = position;
_panStartTranslation = Translation;
}

WeakReferenceMessenger.Default.Send(new CanvasInvalidationMessage(0));
}

[RelayCommand]
private void PointerRelease()
{
_isPanning = false;
_transformation = Transformation.None;
_sizing = Sizing.Empty;
}

[RelayCommand]
private void PointerMove(Vector2 position)
{
if (_isPanning)
{
Translation = _panStartTranslation + (position - _panStartPointerPosition);
WeakReferenceMessenger.Default.Send(new CanvasInvalidationMessage(0));
return;
}

if (SelectedNodes.Count == 0) return;

position = RelativePositionToDialog(position);

int i = 0;
foreach (var controlViewModel in SelectedControlViewModels)
{
var transformationStartRectangle = _transformationStartRectangles[i];
switch (_transformation)
{
case Transformation.Size:
if (_sizing.Left)
{
position.X = Math.Min(position.X, transformationStartRectangle.Right);
controlViewModel.X = (int)position.X;
controlViewModel.Width =
transformationStartRectangle.Right - controlViewModel.X;
}

if (_sizing.Top)
{
position.Y = Math.Min(position.Y, transformationStartRectangle.Bottom);
controlViewModel.Y = (int)position.Y;
controlViewModel.Height =
transformationStartRectangle.Bottom - controlViewModel.Y;
}

if (_sizing.Right)
{
position.X = Math.Max(position.X, transformationStartRectangle.X);
controlViewModel.Width = (int)(transformationStartRectangle.Width +
(position.X - transformationStartRectangle.Right));
}

if (_sizing.Bottom)
{
position.Y = Math.Max(position.Y, transformationStartRectangle.Y);
controlViewModel.Height = (int)(transformationStartRectangle.Height +
(position.Y - transformationStartRectangle.Bottom));
}

break;
case Transformation.Move:
controlViewModel.X =
(int)(transformationStartRectangle.X + (position.X - _transformationStartPointerPosition.X));
controlViewModel.Y =
(int)(transformationStartRectangle.Y + (position.Y - _transformationStartPointerPosition.Y));
break;
}

var processedRectangle =
_dialogEditorSettingsViewModel.Positioner.Transform(DialogViewModel.Dialog.Root,
controlViewModel.Control);
controlViewModel.X = processedRectangle.X;
controlViewModel.Y = processedRectangle.Y;
controlViewModel.Width = processedRectangle.Width;
controlViewModel.Height = processedRectangle.Height;
i++;
}
}

[RelayCommand(CanExecute = nameof(HasSelection))]
private void DeleteSelectedNode()
{
// DialogViewModel.Dialog.Root.Children.Remove(SelectedNodes!);
// SelectedNode = null;
WeakReferenceMessenger.Default.Send(new CanvasInvalidationMessage(0));
}

[RelayCommand(CanExecute = nameof(HasSelection))]
private void BringSelectedNodeToFront()
{
// DialogViewModel.Dialog.Root.Children.Remove(SelectedNode!);
// DialogViewModel.Dialog.Root.Children.Add(SelectedNode!);
WeakReferenceMessenger.Default.Send(new CanvasInvalidationMessage(0));
}

[RelayCommand]
private async Task SaveRc()
{
var rc = new RcDialogSerializer()
{
GenerateCompilable = true
}.Serialize(
new DefaultLayoutEngine().DoLayout(DialogViewModel.Dialog), DialogViewModel.Dialog);
var header = new CxxHeaderInformationGenerator().Generate(DialogViewModel.Dialog);

var resourceFile =
await _filePickerService.TryPickSaveFileAsync("rsrc.rc", ("Resource File", new[] { "rc" }));
var headerFile =
await _filePickerService.TryPickSaveFileAsync("resource.h", ("C/C++ Header File", new[] { "h" }));

if (resourceFile == null || headerFile == null) return;

await File.WriteAllTextAsync(resourceFile, rc);
await File.WriteAllTextAsync(headerFile, header);
}

#endregion


internal void AddControl(Control control)
{
// generate randomized unique identifier because we can't have duplicate identifiers
Expand Down
189 changes: 189 additions & 0 deletions RsrcArchitect.ViewModels/DialogEditorViewModel_Commands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
using System.Diagnostics;
using System.Numerics;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using RsrcArchitect.ViewModels.Helpers;
using RsrcArchitect.ViewModels.Messages;
using RsrcArchitect.ViewModels.Types;
using RsrcCore.Generators.Implementations;
using RsrcCore.Layout.Implementations;
using RsrcCore.Serializers.Implementations;

namespace RsrcArchitect.ViewModels;

public partial class DialogEditorViewModel
{
[RelayCommand]
private void Close()
{
WeakReferenceMessenger.Default.Send(new DialogEditorViewModelClosingMessage(this));
}

[RelayCommand]
private void PointerPress(Vector2 position)
{
var dialogPosition = RelativePositionToDialog(position);

// do grip-test first, then store if it hits
var isGripHit = false;
(Transformation transformation, Sizing sizing) transformationOperation = (Transformation.None, Sizing.Empty);
if (SelectedNodes.Count > 0)
{
// if any candidate hits, we grabbed a control's grip
transformationOperation = SelectedNodes
.Select(x =>
TransformationHelper.GetCandidate(x.Data, dialogPosition, _dialogEditorSettingsViewModel.GripSize))
.FirstOrDefault(x => x.Item1 != Transformation.None, (Transformation.None, Sizing.Empty));

isGripHit = transformationOperation.transformation != Transformation.None;
}

// if no grip hits, we know we aren't starting to resize or move a control,
// thus we can select a new one
if (!isGripHit)
{
Debug.Print("No grip hit");
var newSelectedNode = GetControlNodeAtPosition(dialogPosition);
SelectedNodes.Clear();
if (newSelectedNode != null)
{
SelectedNodes.Add(newSelectedNode);
}
}

if (SelectedNodes.Count > 0)
{
Debug.Print("Start control transformation");
(_transformation, _sizing) = (transformationOperation.transformation, transformationOperation.sizing);
_transformationStartRectangles = SelectedNodes.Select(x => x.Data.Rectangle).ToList();
_transformationStartPointerPosition = dialogPosition;
}
else
{
// no node caught but stil clicked,
// so start translating the camera
Debug.Print("Start pan");
_isPanning = true;
_panStartPointerPosition = position;
_panStartTranslation = Translation;
}

WeakReferenceMessenger.Default.Send(new CanvasInvalidationMessage(0));
}

[RelayCommand]
private void PointerRelease()
{
_isPanning = false;
_transformation = Transformation.None;
_sizing = Sizing.Empty;
}

[RelayCommand]
private void PointerMove(Vector2 position)
{
if (_isPanning)
{
Translation = _panStartTranslation + (position - _panStartPointerPosition);
WeakReferenceMessenger.Default.Send(new CanvasInvalidationMessage(0));
return;
}

if (SelectedNodes.Count == 0) return;

position = RelativePositionToDialog(position);

int i = 0;
foreach (var controlViewModel in SelectedControlViewModels)
{
var transformationStartRectangle = _transformationStartRectangles[i];
switch (_transformation)
{
case Transformation.Size:
if (_sizing.Left)
{
position.X = Math.Min(position.X, transformationStartRectangle.Right);
controlViewModel.X = (int)position.X;
controlViewModel.Width =
transformationStartRectangle.Right - controlViewModel.X;
}

if (_sizing.Top)
{
position.Y = Math.Min(position.Y, transformationStartRectangle.Bottom);
controlViewModel.Y = (int)position.Y;
controlViewModel.Height =
transformationStartRectangle.Bottom - controlViewModel.Y;
}

if (_sizing.Right)
{
position.X = Math.Max(position.X, transformationStartRectangle.X);
controlViewModel.Width = (int)(transformationStartRectangle.Width +
(position.X - transformationStartRectangle.Right));
}

if (_sizing.Bottom)
{
position.Y = Math.Max(position.Y, transformationStartRectangle.Y);
controlViewModel.Height = (int)(transformationStartRectangle.Height +
(position.Y - transformationStartRectangle.Bottom));
}

break;
case Transformation.Move:
controlViewModel.X =
(int)(transformationStartRectangle.X + (position.X - _transformationStartPointerPosition.X));
controlViewModel.Y =
(int)(transformationStartRectangle.Y + (position.Y - _transformationStartPointerPosition.Y));
break;
}

var processedRectangle =
_dialogEditorSettingsViewModel.Positioner.Transform(DialogViewModel.Dialog.Root,
controlViewModel.Control);
controlViewModel.X = processedRectangle.X;
controlViewModel.Y = processedRectangle.Y;
controlViewModel.Width = processedRectangle.Width;
controlViewModel.Height = processedRectangle.Height;
i++;
}
}

[RelayCommand(CanExecute = nameof(HasSelection))]
private void DeleteSelectedNode()
{
// DialogViewModel.Dialog.Root.Children.Remove(SelectedNodes!);
// SelectedNode = null;
WeakReferenceMessenger.Default.Send(new CanvasInvalidationMessage(0));
}

[RelayCommand(CanExecute = nameof(HasSelection))]
private void BringSelectedNodeToFront()
{
// DialogViewModel.Dialog.Root.Children.Remove(SelectedNode!);
// DialogViewModel.Dialog.Root.Children.Add(SelectedNode!);
WeakReferenceMessenger.Default.Send(new CanvasInvalidationMessage(0));
}

[RelayCommand]
private async Task SaveRc()
{
var rc = new RcDialogSerializer()
{
GenerateCompilable = true
}.Serialize(
new DefaultLayoutEngine().DoLayout(DialogViewModel.Dialog), DialogViewModel.Dialog);
var header = new CxxHeaderInformationGenerator().Generate(DialogViewModel.Dialog);

var resourceFile =
await _filePickerService.TryPickSaveFileAsync("rsrc.rc", ("Resource File", new[] { "rc" }));
var headerFile =
await _filePickerService.TryPickSaveFileAsync("resource.h", ("C/C++ Header File", new[] { "h" }));

if (resourceFile == null || headerFile == null) return;

await File.WriteAllTextAsync(resourceFile, rc);
await File.WriteAllTextAsync(headerFile, header);
}
}

0 comments on commit 8f13ea3

Please sign in to comment.