Subversion Repositories Kolibri OS

Rev

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