Subversion Repositories Kolibri OS

Rev

Rev 7271 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. #define MAX_ACTIONS_COUNT 15
  3.  
  4. struct _ActionsHistory {
  5.         dword stack[MAX_ACTIONS_COUNT];
  6.         dword head;
  7.         dword tail;
  8.         dword currentIndex;
  9.  
  10.         void init();
  11.         bool isEmpty();
  12.  
  13.         void saveCurrentState();
  14.         void restoreState(dword index);
  15.  
  16.         void undoLastAction();
  17.         void redoLastAction();
  18. };
  19.  
  20. void _ActionsHistory::init() {
  21.         dword i;
  22.  
  23.         head = tail = 0;
  24.         currentIndex = -1;
  25.  
  26.         for (i = 0; i < MAX_ACTIONS_COUNT; i++) {
  27.                 stack[i] = free(stack[i]);
  28.                 stack[i] = malloc(image.columns * image.rows * 4);
  29.         }
  30.  
  31.         saveCurrentState();
  32. }
  33.  
  34. bool _ActionsHistory::isEmpty() {
  35.         if (head == tail)
  36.                 return true;
  37.         else
  38.                 return false;
  39. }
  40.  
  41. void _ActionsHistory::saveCurrentState() {
  42.         dword addr, offset;
  43.         int r, c;
  44.        
  45.         tail = currentIndex + 1;
  46.  
  47.         if (tail >= MAX_ACTIONS_COUNT)
  48.                 tail = tail % MAX_ACTIONS_COUNT;
  49.  
  50.         addr = stack[tail];
  51.  
  52.         for (r = 0; r < image.rows; r++)
  53.         {
  54.                 for (c = 0; c < image.columns; c++)
  55.                 {
  56.                         offset = calc(image.columns * r + c) * 4;
  57.  
  58.                         ESDWORD[addr + offset] = image.get_pixel(r, c);
  59.                 }
  60.         }
  61.  
  62.         currentIndex = tail;
  63.         tail = calc(tail + 1) % 10;
  64.  
  65.         if (tail == head)
  66.                 head = calc(head + 1) % 10;
  67. }
  68.  
  69. void _ActionsHistory::restoreState(dword index) {
  70.         dword addr, offset;
  71.         int r, c;
  72.  
  73.         addr = stack[index];
  74.  
  75.         for (r = 0; r < image.rows; r++)
  76.         {
  77.                 for (c = 0; c < image.columns; c++)
  78.                 {
  79.                         offset = calc(image.columns * r + c) * 4;
  80.                         image.set_pixel(r, c, ESDWORD[addr + offset]);
  81.                 }
  82.         }
  83. }
  84.  
  85. void _ActionsHistory::undoLastAction() {
  86.         dword previousAction;
  87.  
  88.         if (!is_selection_moving()) {
  89.                 // Если вышли за левую границу, перемещаемся в конец массива
  90.                 if (currentIndex == 0) {
  91.                         previousAction = MAX_ACTIONS_COUNT - 1;
  92.                 }
  93.                 else {
  94.                         previousAction = currentIndex - 1;
  95.                 }
  96.  
  97.                 if (isEmpty())
  98.                         return;
  99.                 else {
  100.                         if (currentIndex != head) {
  101.                                 restoreState(previousAction);
  102.                                 DrawCanvas();
  103.                         }
  104.  
  105.                         if (currentIndex != head)
  106.                                 currentIndex = previousAction;
  107.                 }
  108.         }
  109. }
  110.  
  111. void _ActionsHistory::redoLastAction() {
  112.         dword nextAction = calc(currentIndex + 1);
  113.  
  114.         if (!is_selection_moving()) {
  115.                 // Если вышли за левую границу, возвращаемся в начало
  116.                 if (nextAction >= MAX_ACTIONS_COUNT)
  117.                         nextAction = nextAction % MAX_ACTIONS_COUNT;
  118.  
  119.                 if (isEmpty())
  120.                         return;
  121.                 else {
  122.                         if (nextAction != tail) {
  123.                                 restoreState(nextAction);
  124.                                 DrawCanvas();
  125.                         }
  126.  
  127.                         if (nextAction != tail)
  128.                                 currentIndex = nextAction;
  129.                 }
  130.         }
  131. }