1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using System;
- namespace etoy
- {
- abstract class Command : IContextSetter, ICommand
- {
- bool _completed;
- bool _running;
- float _progress;
- Exception _exception;
- void IContextSetter.SetContext(Context context) => Context = context;
- bool ICommand.Start => _running;
- bool ICommand.Done => _completed;
- float ICommand.Progress => _progress;
- Exception ICommand.Exception => _exception;
- void ICommand.Execute()
- {
- if (!_running)
- {
- _running = true;
- OnProcess();
- }
- }
- /// <summary>
- /// 上下文
- /// </summary>
- protected Context Context { get; private set; }
- /// <summary>
- /// 异步完成
- /// </summary>
- protected void Completed()
- {
- _progress = 1.0f;
- _completed = true;
- }
- protected void SetException(Exception e)
- {
- _exception = e;
- }
- /// <summary>
- /// 设置进度
- /// </summary>
- /// <param name="progress"></param>
- protected void SetProgress(float progress)
- {
- if (progress >= 1.0f)
- _progress = 1.0f;
- else if (progress < 0.0f)
- _progress = 0.0f;
- else
- _progress = progress;
- }
- /// <summary>
- /// 处理回调
- /// </summary>
- protected abstract void OnProcess();
- /// <summary>
- /// 命令描述, 用于显示在控制台上
- /// </summary>
- public abstract string Description { get; }
- }
- }
|