Skip to content
Snippets Groups Projects
Action.cpp 1.61 KiB
Newer Older
#include "Action.hpp"

Action::Action(Action::Type type, std::function<void(Config & config, Action & action)> apply, std::function<void(Config & config, Action & action)> undo, std::function<bool(Config & config, Action & action)> appliable)
{
  this->type = type;
  this->apply = apply;
  this->undo = undo;
  this->appliable = appliable;
}

Action Action::addLinesIfNeeded(int nbLines)
{
  auto apply = [nbLines](Config & config, Action &)
  {
    config.addLines(1);
  };

  auto undo = [](Config &, Action &)
  {
  };

  auto appliable = [](Config &, Action &)
  {
    return true;
  };

  return {Type::AddLines, apply, undo, appliable};
}

Action Action::moveWordIndex(int movement)
{
  auto apply = [movement](Config & config, Action &)
  {
    config.moveWordIndex(movement);
  };

  auto undo = [movement](Config & config, Action &)
  {
    config.moveWordIndex(movement);
  };

  auto appliable = [movement](Config & config, Action &)
  {
    bool possible = config.moveWordIndex(movement);
    if (possible)
      moveWordIndex(-movement);
    return possible;
  };

  return {Type::MoveWord, apply, undo, appliable};
}

Action Action::moveCharacterIndex(int movement)
{
  auto apply = [movement](Config & config, Action &)
  {
    config.moveCharacterIndex(movement);
  };

  auto undo = [movement](Config & config, Action &)
  {
    config.moveCharacterIndex(movement);
  };

  auto appliable = [movement](Config & config, Action &)
  {
    bool possible = config.moveCharacterIndex(movement);
    if (possible)
      moveCharacterIndex(-movement);
    return possible;
  };

  return {Type::MoveChar, apply, undo, appliable};
}