// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. using System; using FlaxEngine; namespace FlaxEditor.Surface.Undo { /// /// The helper structure for Surface context handle. /// [HideInEditor] public struct ContextHandle { private readonly string[] _path; /// /// Initializes a new instance of the struct. /// /// The context. public ContextHandle(VisjectSurfaceContext context) { int count = 0; var parent = context.Parent; while (parent != null) { count++; parent = parent.Parent; } if (count == 0) { // Root context _path = null; } else { _path = new string[count]; for (int i = 0; i < count; i++) { _path[i] = context.Context.SurfaceName; context = context.Parent; } } } /// /// Gets the context. /// /// The Surface. /// The restored context. public VisjectSurfaceContext Get(VisjectSurface surface) { var context = surface.RootContext; var count = _path?.Length ?? 0; for (int i = count - 1; i >= 0; i--) { var found = false; foreach (var child in context.Children) { if (child.Context.SurfaceName == _path[i]) { found = true; context = child; break; } } if (!found) throw new Exception(string.Format("Missing context \'{0}\' in \'{1}\'", _path[i], context.Context.SurfaceName)); } return context; } } }