Subversion Repositories Kolibri OS

Rev

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