waitForEvent<IEvent extends Event> method

Future<IEvent> waitForEvent<IEvent extends Event>(
  1. EventType eventType, {
  2. bool markAsHandled = false,
  3. bool continueRendering = true,
})

Implementation

Future<IEvent> waitForEvent<IEvent extends Event>(EventType eventType,
    {bool markAsHandled = false, bool continueRendering = true}) async {
  if (continueRendering) {
    var completed = false;
    var event;

    var listener = EventListener<IEvent>(eventType, (_event) {
      if (!completed) {
        event = _event;
        completed = true;
      }

      return markAsHandled;
    });

    addEventListener(listener);

    while (!completed) {
      var dt = await display.nextFrame();
      updateMobjects(dt);
      updateFrame(dt);
    }

    removeEventListener(listener);

    return event;
  } else {
    var completer = Completer<IEvent>();

    var listener = EventListener<IEvent>(eventType, (event) {
      if (!completer.isCompleted) {
        completer.complete(event);
      }

      return markAsHandled;
    });

    addEventListener(listener);

    var event = await completer.future;

    removeEventListener(listener);

    return event;
  }
}