public async Task <ValueOrDirective <SelectedEntity> > GetEntity(UserDirective directive, RubberBandGenerator onCursorMove = null) { OnValueRequested(new ValueRequestedEventArgs(InputType.Entity | InputType.Directive)); await WaitFor(InputType.Entity | InputType.Directive, directive, onCursorMove); ValueOrDirective <SelectedEntity> result; switch (lastType) { case PushedValueType.None: result = new ValueOrDirective <SelectedEntity>(); break; case PushedValueType.Cancel: result = ValueOrDirective <SelectedEntity> .GetCancel(); break; case PushedValueType.Entity: result = ValueOrDirective <SelectedEntity> .GetValue(pushedEntity); break; case PushedValueType.Directive: result = ValueOrDirective <SelectedEntity> .GetDirective(pushedDirective); break; default: throw new Exception("Unexpected pushed value"); } ResetWaiters(); return(result); }
public async Task <ValueOrDirective <Point> > GetPoint(UserDirective directive, RubberBandGenerator onCursorMove = null, Optional <Point> lastPoint = default(Optional <Point>)) { this.LastPoint = lastPoint.HasValue ? lastPoint.Value : this.LastPoint; await WaitFor(InputType.Point | InputType.Directive, directive, onCursorMove); ValueOrDirective <Point> result; switch (lastType) { case PushedValueType.Cancel: result = ValueOrDirective <Point> .GetCancel(); break; case PushedValueType.None: result = new ValueOrDirective <Point>(); // TODO: default directive? break; case PushedValueType.Directive: result = ValueOrDirective <Point> .GetDirective(pushedDirective); break; case PushedValueType.Point: result = ValueOrDirective <Point> .GetValue(pushedPoint); break; default: throw new Exception("Unexpected pushed value"); } ResetWaiters(); return(result); }
private void ResetWaiters() { AllowedInputTypes = InputType.Command; lastType = PushedValueType.None; pushedPoint = default(Point); pushedEntity = null; pushedDirective = null; pushedString = null; currentDirective = null; Workspace.RubberBandGenerator = null; pushValueDone = null; }
private Task WaitFor(InputType type, UserDirective directive, RubberBandGenerator onCursorMove) { pushValueDone = new TaskCompletionSource <bool>(); SetPrompt(directive.Prompt); currentDirective = directive; AllowedInputTypes = type; lastType = PushedValueType.None; pushedPoint = default(Point); pushedEntity = null; pushedDirective = null; pushedString = null; Workspace.RubberBandGenerator = onCursorMove; return(pushValueDone.Task); }
public async Task <bool> Execute(IWorkspace workspace, object arg) { var boundaries = await workspace.InputService.GetEntities(GetBoundsText()); if (boundaries.Cancel || !boundaries.HasValue || !boundaries.Value.Any()) { return(false); } var boundaryPrimitives = boundaries.Value.SelectMany(b => b.GetPrimitives()); var directive = new UserDirective(GetTrimExtendText()); var selected = await workspace.InputService.GetEntity(directive); IEnumerable <Entity> removed; IEnumerable <Entity> added; string entityLayerName; while (!selected.Cancel && selected.HasValue) { var drawing = workspace.Drawing; entityLayerName = drawing.ContainingLayer(selected.Value.Entity).Name; DoTrimExtend(selected.Value, boundaryPrimitives, out removed, out added); foreach (var ent in removed) { drawing = drawing.Remove(ent); } foreach (var ent in added) { drawing = drawing.Add(drawing.Layers.GetValue(entityLayerName), ent); } // commit the change if (workspace.Drawing != drawing) { workspace.Update(drawing: drawing); } // get next entity to trim/extend selected = await workspace.InputService.GetEntity(directive); } return(true); }
public async Task <ValueOrDirective <double> > GetDistance(UserDirective directive = null, Func <double, IEnumerable <IPrimitive> > onCursorMove = null, Optional <double> defaultDistance = default(Optional <double>)) { var allowedType = InputType.Distance | InputType.Directive | InputType.Point; if (directive == null) { var prompt = "Offset distance or first point"; if (defaultDistance.HasValue) { prompt += $" [{defaultDistance.Value}]"; } directive = new UserDirective(prompt); } await WaitFor(allowedType, directive, null); ValueOrDirective <double> result; switch (lastType) { case PushedValueType.Cancel: result = ValueOrDirective <double> .GetCancel(); break; case PushedValueType.None: result = ValueOrDirective <double> .GetValue(defaultDistance.Value); break; case PushedValueType.Distance: result = ValueOrDirective <double> .GetValue(pushedDistance); break; case PushedValueType.Directive: result = ValueOrDirective <double> .GetDirective(pushedDirective); break; case PushedValueType.Point: var first = pushedPoint; ResetWaiters(); onCursorMove ??= _ => Enumerable.Empty <IPrimitive>(); var second = await GetPoint(new UserDirective("Second point of offset distance"), p => { var distance = (p - first).Length; return(new[] { new PrimitiveLine(first, p) }.Concat(onCursorMove(distance))); }); if (second.HasValue) { var dist = (second.Value - first).Length; result = ValueOrDirective <double> .GetValue(dist); } else if (second.Directive != null) { result = ValueOrDirective <double> .GetDirective(second.Directive); } else { result = ValueOrDirective <double> .GetCancel(); } break; default: throw new Exception("Unexpected pushed value"); } ResetWaiters(); return(result); }