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