Subversion Repositories Kolibri OS

Rev

Rev 7257 | Rev 7268 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7267 leency 1
//===================================================//
2
//                                                   //
3
//                       DATA                        //
4
//                                                   //
5
//===================================================//
6
 
7
_image selection;
8
 
9
enum {
10
	STATE_INACTIVE=0,
11
	STATE_CHOSING=1,
12
	STATE_SELECTED=2,
13
	STATE_MOVING=3
14
};
15
int selection_state = STATE_INACTIVE;
16
 
7257 leency 17
int selection_start_x = -1;
18
int selection_start_y = -1;
19
int selection_end_x = -1;
20
int selection_end_y = -1;
21
 
22
int selection_pivot_x = -1;
23
int selection_pivot_y = -1;
24
 
7267 leency 25
//===================================================//
26
//                                                   //
27
//                       CODE                        //
28
//                                                   //
29
//===================================================//
30
 
7257 leency 31
void SelectTool_normalizeSelection() {
32
	int t;
33
 
34
	// Restructuring of the selection coordinates
35
	if (selection_end_x < selection_start_x) {
36
		t = selection_start_x;
37
		selection_start_x = selection_end_x;
38
		selection_end_x = t;
39
	}
40
 
41
	if (selection_end_y < selection_start_y) {
42
		t = selection_end_y;
43
		selection_end_y = selection_start_y;
44
		selection_start_y = t;
45
	}
46
}
47
 
48
void reset_selection_moving() {
7267 leency 49
	if (STATE_MOVING == selection_state) {
7257 leency 50
		SelectTool_drawBuffer(selection_start_x, selection_start_y, 1);
51
 
52
		selection_pivot_x = -1;
53
		selection_pivot_y = -1;
54
 
7267 leency 55
		selection_state = STATE_SELECTED;
7257 leency 56
 
57
		actionsHistory.saveCurrentState();
58
		DrawCanvas();
59
	}
60
}
61
 
62
bool is_selection_moving() {
7267 leency 63
	if (STATE_MOVING == selection_state) return true;
64
	return false;
7257 leency 65
}
66
 
67
void reset_selection() {
68
	reset_selection_moving();
69
 
70
	selection_start_x = -1;
71
	selection_start_y = -1;
72
	selection_end_x = -1;
73
	selection_end_y = -1;
74
}
75
 
76
void SelectTool_activate() {
77
	reset_selection();
7267 leency 78
	selection_state = STATE_INACTIVE;
7257 leency 79
}
80
 
81
void SelectTool_deactivate() {
82
	reset_selection_moving();
7267 leency 83
	selection_state = STATE_INACTIVE;
7257 leency 84
}
85
 
86
bool SelectTool_pointInSelection(int x, int y) {
87
	if (x >= selection_start_x) && (x <= selection_end_x) && (y >= selection_start_y) && (y <= selection_end_y)
88
		return true;
89
	else
90
		return false;
91
}
92
 
93
 
94
void SelectTool_copyToBuffer() {
7267 leency 95
	dword r, c;
7257 leency 96
 
7267 leency 97
	selection_state = STATE_SELECTED;
98
	selection.rows = selection_end_y - selection_start_y + 1;
99
	selection.columns = selection_end_x - selection_start_x + 1;
7257 leency 100
 
7267 leency 101
	for (r = selection_start_y; r <= selection_end_y; r++) {
102
		for (c = selection_start_x; c <= selection_end_x; c++) {
103
			selection.set_pixel(r - selection_start_y, c - selection_start_x, image.get_pixel(r, c) );
7257 leency 104
		}
7267 leency 105
	}
7257 leency 106
}
107
 
108
void SelectTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
7267 leency 109
	int dx, dy, m_x, m_y, r, c;
7257 leency 110
	dword pixel;
111
 
112
	m_x = TO_CANVAS_X(mouseX);
113
	m_y = TO_CANVAS_Y(mouseY);
7267 leency 114
 
115
	if (mouse.down)
116
	&& (canvas.hovered())
117
	&& ((STATE_INACTIVE == selection_state) || (STATE_CHOSING == selection_state) || (STATE_SELECTED == selection_state) || (STATE_MOVING == selection_state))
118
	{
7257 leency 119
		if (selection_start_x != -1) && (SelectTool_pointInSelection(m_x, m_y)) {
120
			if (selection_pivot_x == -1) {
121
				selection_pivot_x = m_x;
122
				selection_pivot_y = m_y;
123
 
124
				GetKeys();
125
 
7267 leency 126
				if (STATE_MOVING != selection_state) && ( !(key_modifier&KEY_LSHIFT) ) {
7257 leency 127
					for (r = selection_start_y; r <= selection_end_y; r++)
128
						for (c = selection_start_x; c <= selection_end_x; c++) {
129
							image.set_pixel(r, c, color2);
130
						}
131
				}
7267 leency 132
 
133
				selection_state = STATE_MOVING;
7257 leency 134
			}
135
		}
7267 leency 136
		else {
7257 leency 137
			reset_selection();
7267 leency 138
			selection_state = STATE_CHOSING;
7257 leency 139
		}
140
	}
141
 
142
	if (selection_pivot_x != -1) {
143
		dx = m_x - selection_pivot_x;
144
		dy = m_y - selection_pivot_y;
145
 
146
		if (selection_start_x + dx < 0)
147
			dx = selection_start_x;
148
 
149
		if (selection_end_x + dx >= image.columns)
150
			dx = image.columns-1 - selection_end_x;
151
 
152
		if (selection_start_y + dy < 0)
153
			dy = selection_start_y;
154
 
155
		if (selection_end_y + dy >= image.rows)
156
			dy = image.rows-1 - selection_end_y;
157
 
158
 
159
		selection_start_x += dx;
160
		selection_end_x += dx;
161
 
162
		selection_start_y += dy;
163
		selection_end_y += dy;
164
 
165
		selection_pivot_x += dx;
166
		selection_pivot_y += dy;
167
 
168
		DrawCanvas();
169
	}
170
 
7267 leency 171
	if (STATE_CHOSING == selection_state)
7257 leency 172
	{
173
		if (mouseX>canvas.x+canvas.w-zoom.value) mouseX = canvas.x+canvas.w-zoom.value;
174
		if (mouseY>canvas.y+canvas.h-zoom.value) mouseY = canvas.y+canvas.h-zoom.value;
175
 
176
		if (mouseX
177
		if (mouseY
178
 
179
		if (mouse.key) {
180
			selection_end_x = TO_CANVAS_X(mouseX);
181
			selection_end_y = TO_CANVAS_Y(mouseY);
182
 
183
			if ((selection_start_x < 0) || (selection_start_y < 0)) {
184
				selection_start_x = TO_CANVAS_X(mouseX);
185
				selection_start_y = TO_CANVAS_Y(mouseY);
186
			}
187
			else {
188
				DrawCanvas();
189
 
190
				/**if ((calc(TO_CANVAS_X(mouseX)) != selection_end_x)
191
					|| (calc(TO_CANVAS_Y(mouseY)) != selection_end_y))
192
				{
193
					DrawCanvas();
194
				}*/
195
			}
196
 
197
		}
198
 
199
		if (mouse.up) {
200
			SelectTool_normalizeSelection();
201
			SelectTool_copyToBuffer();
202
		}
203
	}
204
 
205
	if (mouse.up) {
206
		if (selection_pivot_x != -1) {
207
			selection_pivot_x = -1;
208
			selection_pivot_y = -1;
209
		}
210
	}
211
}
212
 
213
void SelectTool_onCanvasDraw() {
214
	if ((selection_start_x >= 0) && (selection_start_y >= 0) && (selection_end_x >= 0) && (selection_end_y >= 0)) {
7267 leency 215
		DrawSelection();
7257 leency 216
	}
217
}
218
 
219
void SelectTool_drawBuffer(int insert_x, int insert_y, int target) {
220
	dword color;
7267 leency 221
	dword r, c;
7257 leency 222
	dword insert_to_x, insert_to_y;
223
 
7267 leency 224
	if (STATE_INACTIVE != selection_state) {
225
		insert_to_x = insert_x + selection.columns - 1;
7257 leency 226
 
227
		if (insert_to_x >= image.columns)
228
			insert_to_x = image.columns-1;
229
 
7267 leency 230
		insert_to_y = insert_y + selection.rows - 1;
7257 leency 231
 
232
		if (insert_to_y >= image.rows)
233
			insert_to_y = image.rows-1;
234
 
235
		for (r = insert_y; r <= insert_to_y; r++) {
236
			for (c = insert_x; c <= insert_to_x; c++) {
237
 
7267 leency 238
					color = selection.get_pixel(r - insert_y, c - insert_x);
7257 leency 239
 
240
					if (TOIMAGE == target)
241
						image.set_pixel(r, c, color);
242
					else
243
						DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y,
244
							zoom.value, zoom.value, color);
245
			}
246
		}
247
	}
248
}
249
 
250
void SelectTool_onKeyEvent(dword keycode) {
7267 leency 251
	dword r, c;
7257 leency 252
 
253
	if (keycode == SCAN_CODE_KEY_V) {
7267 leency 254
		if (STATE_SELECTED == selection_state) {
7257 leency 255
			reset_selection();
256
 
7267 leency 257
			selection_state = STATE_MOVING;
7257 leency 258
			selection_start_x = 0;
7267 leency 259
			selection_end_x = selection.columns - 1;
7257 leency 260
 
261
			selection_start_y = 0;
7267 leency 262
			selection_end_y = selection.rows - 1;
7257 leency 263
 
264
			DrawCanvas();
265
		}
266
	}
267
}
268
 
7267 leency 269
void DrawSelection() {
7257 leency 270
	#define SELECTION_COLOR 0xAAE5EF
271
	int p1x, p1y, p2x, p2y, r, c, old_color, new_color;
272
 
7267 leency 273
	if (selection_start_x <= selection_end_x) {
274
		p1x = selection_start_x;
275
		p2x = selection_end_x;
7257 leency 276
	}
277
	else {
7267 leency 278
		p1x = selection_end_x;
279
		p2x = selection_start_x;
7257 leency 280
	}
281
 
7267 leency 282
	if (selection_start_y <= selection_end_y) {
283
		p2y = selection_start_y;
284
		p1y = selection_end_y;
7257 leency 285
	}
286
	else {
7267 leency 287
		p2y = selection_end_y;
288
		p1y = selection_start_y;
7257 leency 289
	}
290
 
291
	for (r = p1y; r >= p2y; r--) {
292
		for (c = p1x; c <= p2x; c++) {
293
			image.pixel_state.set_drawable_state(r, c, false);
294
 
7267 leency 295
			if (STATE_MOVING == selection_state) && (SelectTool_pointInSelection(c, r)) {
296
				old_color = selection.get_pixel(r - selection_start_y, c - selection_start_x);
7257 leency 297
			}
298
			else {
299
				old_color = image.get_pixel(r, c);
300
			}
301
 
302
			new_color = MixColors(old_color, SELECTION_COLOR, 64);
303
 
304
			DrawCanvasPixel(r, c, new_color);
305
		}
306
	}
307
}