Subversion Repositories Kolibri OS

Rev

Rev 7422 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7267 leency 1
//===================================================//
2
//                                                   //
7273 leency 3
//                    SELECTION                      //
7267 leency 4
//                                                   //
5
//===================================================//
6
 
7
enum {
8
	STATE_INACTIVE=0,
9
	STATE_CHOSING=1,
7268 leency 10
	STATE_SELECTED=2
7267 leency 11
};
12
 
7273 leency 13
struct _selection
14
{
15
	_image buf;
16
	int state;
7257 leency 17
 
7273 leency 18
	int start_x;
19
	int start_y;
20
	int end_x;
21
	int end_y;
7257 leency 22
 
7273 leency 23
	int pivot_x;
24
	int pivot_y;
7267 leency 25
 
7273 leency 26
	void reset();
27
	bool is_point_in_selection();
28
	void copy_to_buf();
29
	void move_to_point();
30
	void apply_to_image();
31
} selection;
32
 
33
void _selection::reset() {
34
	start_x = -1;
35
	start_y = -1;
36
	end_x = -1;
37
	end_y = -1;
38
	pivot_x = -1;
39
	pivot_y = -1;
40
	state = STATE_INACTIVE;
41
}
42
 
43
bool _selection::is_point_in_selection(int x, int y) {
44
	if (x >= start_x) && (x <= end_x)
45
	&& (y >= start_y) && (y <= end_y)
46
		return true;
47
	else
48
		return false;
49
}
50
 
51
void _selection::copy_to_buf() {
52
	dword r, c;
53
 
54
	// Normalize: Restructuring of the selection coordinates
55
	if (end_x < start_x) {
56
		start_x >< end_x;
7257 leency 57
	}
7273 leency 58
	if (end_y < start_y) {
59
		end_y >< start_y;
7257 leency 60
	}
7273 leency 61
 
62
	state = STATE_SELECTED;
63
	buf.rows = end_y - start_y + 1;
64
	buf.columns = end_x - start_x + 1;
65
 
66
	for (r = start_y; r <= end_y; r++) {
67
		for (c = start_x; c <= end_x; c++) {
68
			buf.set_pixel(r - start_y, c - start_x, image.get_pixel(r, c) );
69
		}
70
	}
7257 leency 71
}
72
 
7273 leency 73
void _selection::move_to_point(int _new_x, _new_y)
74
{
75
	start_x = _new_x;
76
	start_y = _new_y;
77
	end_x = buf.columns - 1 + start_x;
78
	end_y = buf.rows - 1 + start_y;
79
}
80
 
81
void _selection::apply_to_image() {
7271 leency 82
	dword r, c;
7273 leency 83
	for (r = 0; r <= buf.rows - 1; r++) {
84
		for (c = 0; c <= buf.columns - 1; c++) {
85
				image.set_pixel(r + start_y, c + start_x, buf.get_pixel(r, c) );
86
		}
87
	}
7257 leency 88
}
89
 
7273 leency 90
//===================================================//
91
//                                                   //
92
//                      EVENTS                       //
93
//                                                   //
94
//===================================================//
95
 
7271 leency 96
void ApplySelectionToImage() {
97
 
7273 leency 98
	if (STATE_SELECTED != selection.state) return;
7271 leency 99
 
7273 leency 100
	selection.apply_to_image();
7275 leency 101
	selection.reset();
7271 leency 102
 
103
	actionsHistory.saveCurrentState();
104
	DrawCanvas();
105
}
106
 
7257 leency 107
bool is_selection_moving() {
7273 leency 108
	if (STATE_SELECTED == selection.state) return true;
7267 leency 109
	return false;
7257 leency 110
}
111
 
7273 leency 112
void SelectTool_activate() {
7271 leency 113
	ApplySelectionToImage();
7273 leency 114
	selection.reset();
7257 leency 115
}
116
 
117
void SelectTool_deactivate() {
7271 leency 118
	ApplySelectionToImage();
7273 leency 119
	selection.reset();
7257 leency 120
}
121
 
122
void SelectTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
7268 leency 123
	int dx, dy, m_x, m_y;
7257 leency 124
 
125
	m_x = TO_CANVAS_X(mouseX);
126
	m_y = TO_CANVAS_Y(mouseY);
7267 leency 127
 
7268 leency 128
	if (mouse.down) && (canvas.hovered())
7267 leency 129
	{
7273 leency 130
		if (selection.start_x != -1) && (selection.is_point_in_selection(m_x, m_y)) {
131
			if (selection.pivot_x == -1) {
132
				selection.pivot_x = m_x;
133
				selection.pivot_y = m_y;
7257 leency 134
 
135
				GetKeys();
7271 leency 136
				if ( (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) ) {
7273 leency 137
					DrawBarIcon(selection.start_x, selection.start_y, selection.end_x,
138
						selection.end_y, color2, TOIMAGE);
7257 leency 139
				}
7267 leency 140
 
7273 leency 141
				selection.state = STATE_SELECTED;
7257 leency 142
			}
143
		}
7267 leency 144
		else {
7273 leency 145
			ApplySelectionToImage();
146
			selection.state = STATE_CHOSING;
7257 leency 147
		}
148
	}
149
 
7273 leency 150
	//Drag selection
151
	if (STATE_SELECTED == selection.state)
152
	{
153
		if (selection.pivot_x != -1) {
154
			dx = m_x - selection.pivot_x;
155
			dy = m_y - selection.pivot_y;
7257 leency 156
 
7273 leency 157
			if (selection.start_x + dx < 0)
158
				dx = selection.start_x;
159
 
160
			if (selection.end_x + dx >= image.columns)
161
				dx = image.columns-1 - selection.end_x;
162
 
163
			if (selection.start_y + dy < 0)
164
				dy = selection.start_y;
165
 
166
			if (selection.end_y + dy >= image.rows)
167
				dy = image.rows-1 - selection.end_y;
168
 
169
			selection.move_to_point(selection.start_x + dx, selection.start_y + dy);
170
 
171
			selection.pivot_x += dx;
172
			selection.pivot_y += dy;
173
 
174
			DrawCanvas();
175
		}
176
		if (mouse.up) {
177
			if (selection.pivot_x != -1) {
178
				selection.pivot_x = -1;
179
				selection.pivot_y = -1;
180
			}
181
		}
7257 leency 182
	}
183
 
7273 leency 184
	if (STATE_CHOSING == selection.state)
7257 leency 185
	{
7271 leency 186
		mouseX = math.in(mouseX, canvas.x, canvas.x+canvas.w-zoom.value);
187
		mouseY = math.in(mouseY, canvas.y, canvas.y+canvas.h-zoom.value);
7257 leency 188
 
189
		if (mouse.key) {
7273 leency 190
			selection.end_x = TO_CANVAS_X(mouseX);
191
			selection.end_y = TO_CANVAS_Y(mouseY);
7257 leency 192
 
7273 leency 193
			if ((selection.start_x < 0) || (selection.start_y < 0)) {
194
				selection.start_x = TO_CANVAS_X(mouseX);
195
				selection.start_y = TO_CANVAS_Y(mouseY);
7257 leency 196
			}
197
			else {
198
				DrawCanvas();
199
			}
200
		}
201
 
7273 leency 202
		if (mouse.up) {
7444 leency 203
				selection.copy_to_buf();
7257 leency 204
		}
205
	}
7444 leency 206
/*
207
	//forbid to select a single pixel
208
	if (mouse.up) {
209
		if (! selection.end_x-selection.start_x)
210
		&& (! selection.end_y-selection.start_y) {
211
			selection.reset();
212
		}
213
	}
214
*/
7257 leency 215
}
216
 
7271 leency 217
void SelectTool_onKeyEvent(dword keycode) {
7267 leency 218
	dword r, c;
7257 leency 219
 
7271 leency 220
	if (SCAN_CODE_DEL == keycode) {
7422 leency 221
		DrawBarIcon(selection.start_x, selection.start_y, selection.end_x, selection.end_y, color2, TOIMAGE);
7273 leency 222
		selection.reset();
7271 leency 223
		DrawCanvas();
224
	}
7257 leency 225
 
7271 leency 226
	if (SCAN_CODE_ESC == keycode) {
7273 leency 227
		ApplySelectionToImage();
7271 leency 228
		DrawCanvas();
229
	}
7257 leency 230
 
7271 leency 231
	if (SCAN_CODE_KEY_V == keycode) {
7273 leency 232
		if (STATE_SELECTED == selection.state)
233
		{
234
			selection.move_to_point(0, 0);
7257 leency 235
			DrawCanvas();
236
		}
237
	}
238
}
239
 
7271 leency 240
void SelectTool_onCanvasDraw()
241
{
7257 leency 242
	#define SELECTION_COLOR 0xAAE5EF
243
	int p1x, p1y, p2x, p2y, r, c, old_color, new_color;
244
 
7273 leency 245
	//if ((selection.start_x >= 0) && (selection.start_y >= 0)
246
	//&& (selection.end_x >= 0) && (selection.end_y >= 0)) {
7257 leency 247
 
7273 leency 248
	if (STATE_SELECTED == selection.state)
249
	|| (STATE_CHOSING == selection.state)
250
	{
251
		p1x = math.min(selection.start_x, selection.end_x);
252
		p2x = math.max(selection.start_x, selection.end_x);
7257 leency 253
 
7273 leency 254
		p1y = math.min(selection.start_y, selection.end_y);
255
		p2y = math.max(selection.start_y, selection.end_y);
7271 leency 256
 
257
		for (r = p1y; r <= p2y; r++) {
258
			for (c = p1x; c <= p2x; c++) {
259
				image.pixel_state.set_drawable_state(r, c, false);
260
 
7273 leency 261
				if (STATE_SELECTED == selection.state) && (selection.is_point_in_selection(c, r)) {
262
					old_color = selection.buf.get_pixel(r - selection.start_y, c - selection.start_x);
7271 leency 263
				}
264
				else {
265
					old_color = image.get_pixel(r, c);
266
				}
267
 
268
				new_color = MixColors(old_color, SELECTION_COLOR, 64);
269
 
270
				DrawCanvasPixel(r, c, new_color);
7257 leency 271
			}
7271 leency 272
		}
273
	}
274
}
7257 leency 275