Subversion Repositories Kolibri OS

Rev

Rev 7271 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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