Jaroslav 203cc8
#pragma once
Jaroslav 203cc8
Jaroslav 203cc8
#ifndef CELL_POSITION_INCLUDED
Jaroslav 203cc8
#define CELL_POSITION_INCLUDED
Jaroslav 203cc8
Jaroslav 203cc8
#include <algorithm></algorithm>
Jaroslav 203cc8
Jaroslav 203cc8
// Identifies cells by frame and layer rather than row and column
Jaroslav 203cc8
class CellPosition {
Jaroslav 203cc8
  int _frame;  // a frame number. corresponds to row in vertical xsheet
Jaroslav 203cc8
  int _layer;  // a layer number. corresponds to col in vertical xsheet
Jaroslav 203cc8
Jaroslav 203cc8
public:
Jaroslav 203cc8
  CellPosition() : _frame(0), _layer(0) {}
Jaroslav 203cc8
  CellPosition(int frame, int layer) : _frame(frame), _layer(layer) {}
Jaroslav 203cc8
Jaroslav 203cc8
  int frame() const { return _frame; }
Jaroslav 203cc8
  int layer() const { return _layer; }
Jaroslav 203cc8
Jaroslav 203cc8
  void setFrame(int frame) { _frame = frame; }
Jaroslav 203cc8
  void setLayer(int layer) { _layer = layer; }
Jaroslav 203cc8
Jaroslav 203cc8
  CellPosition &operator=(const CellPosition &that) = default;
Jaroslav 203cc8
Jaroslav 203cc8
  operator bool() const { return _frame || _layer; }
Jaroslav 203cc8
Jaroslav 203cc8
  CellPosition operator+(const CellPosition &add) {
Jaroslav 203cc8
    return CellPosition(_frame + add._frame, _layer + add._layer);
Jaroslav 203cc8
  }
Jaroslav 203cc8
  CellPosition operator*(const CellPosition &mult) {
Jaroslav 203cc8
    return CellPosition(_frame * mult._frame, _layer * mult._layer);
Jaroslav 203cc8
  }
Jaroslav 203cc8
  void ensureValid() {
John Dancel 421acd
    if (_frame < 0) _frame  = 0;
John Dancel 421acd
    if (_layer < -1) _layer = -1;
Jaroslav 203cc8
  }
Jaroslav 203cc8
};
Jaroslav 203cc8
Jaroslav 203cc8
// A square range identified by two corners
Jaroslav 203cc8
class CellRange {
Jaroslav 203cc8
  CellPosition _from, _to;  // from.frame <= to.frame && from.layer <= to.layer
Jaroslav 203cc8
Jaroslav 203cc8
public:
Jaroslav 203cc8
  CellRange() {}
Jaroslav 203cc8
  CellRange(const CellPosition &from, const CellPosition &to)
otakuto 158f9f
      : _from(std::min(from.frame(), to.frame()),
otakuto 158f9f
              std::min(from.layer(), to.layer()))
otakuto 158f9f
      , _to(std::max(from.frame(), to.frame()),
otakuto 158f9f
            std::max(from.layer(), to.layer())) {}
Jaroslav 203cc8
Jaroslav 203cc8
  const CellPosition &from() const { return _from; }
Jaroslav 203cc8
  const CellPosition &to() const { return _to; }
Jaroslav 203cc8
Jaroslav 203cc8
  CellRange &operator=(const CellRange &that) = default;
Jaroslav 203cc8
};
Jaroslav 203cc8
Jaroslav 203cc8
#endif