setMouseAction method

bool setMouseAction(
  1. String operation,
  2. int mouse, [
  3. String? key
])
  • Set a mouse action by specifying the operation to be performed and a mouse/key combination. In case of conflict, replaces the existing one
  • operation The operation to be performed ('pan', 'rotate', 'ZOOM', 'fov)
  • mouse A mouse button (0, 1, 2, 3) or for wheel notches
  • key The keyboard modifier ('CTRL', 'SHIFT') or null if key is not needed
  • returns Boolean True if the mouse action has been successfully added, false otherwise

Implementation

bool setMouseAction(String operation, int mouse, [String? key]) {
    final operationInput = ['pan', 'rotate', 'ZOOM', 'fov'];
    final mouseInput = ['0', '1', '2', '3'];
    final keyInput = ['CTRL', 'SHIFT', null];
    int? state;

    if (
      !operationInput.contains(operation) ||
      !mouseInput.contains(mouse.toString()) ||
      !keyInput.contains(key)
    ) {
      //invalid parameters
      return false;
    }

    if (mouse == 3) {
      if (operation != 'ZOOM' && operation != 'fov') {
        //cannot associate 2D operation to 1D input
        return false;
      }
    }

    switch (operation) {
      case 'pan':
        state = State2.pan;
        break;

      case 'rotate':
        state = State2.rotate;
        break;

      case 'ZOOM':
        state = State2.scale;
        break;

      case 'fov':
        state = State2.fov;
        break;
    }

    final action = {
      'operation': operation,
      'mouse': mouse,
      'key': key,
      'state': state
    };

    for (int i = 0; i < mouseActions.length; i++) {
      if (
        mouseActions[i]['mouse'] == action['mouse'] &&
        mouseActions[i]['key'] == action['key']
      ) {
        mouseActions.replaceRange(i, i+1, [action]);
        return true;
      }
    }

    mouseActions.add(action);
    return true;
  }