Rev 7243 | Rev 7254 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
7089 | leency | 1 | /* |
7154 | leency | 2 | * Icon Editor for KolibriOS |
7187 | leency | 3 | * Authors: Leency, Nicolas |
7089 | leency | 4 | * Licence: GPL v2 |
5 | */ |
||
6 | |||
7151 | leency | 7 | /* |
7156 | leency | 8 | TODO: |
9 | window colors |
||
10 | enhance icon |
||
7194 | leency | 11 | pipet aside color view |
7151 | leency | 12 | */ |
13 | |||
7096 | leency | 14 | #define MEMSIZE 4096*40 |
7089 | leency | 15 | |
16 | #include "../lib/gui.h" |
||
7151 | leency | 17 | #include "../lib/random.h" |
18 | #include "../lib/mem.h" |
||
7096 | leency | 19 | #include "../lib/obj/libimg.h" |
7089 | leency | 20 | #include "../lib/patterns/rgb.h" |
7253 | leency | 21 | #include "../lib/patterns/libimg_load_skin.h" |
7089 | leency | 22 | |
7151 | leency | 23 | #include "colors_mas.h" |
24 | |||
7089 | leency | 25 | //===================================================// |
26 | // // |
||
27 | // DATA // |
||
28 | // // |
||
29 | //===================================================// |
||
30 | |||
7253 | leency | 31 | #define T_TITLE "Icon Editor 0.45" |
7096 | leency | 32 | |
7152 | leency | 33 | #define TOOLBAR_H 24+8 |
7187 | leency | 34 | #define PANEL_LEFT_W 16+5+5+3+3 |
7096 | leency | 35 | #define PALLETE_SIZE 116 |
7187 | leency | 36 | #define TB_ICON_PADDING 26 |
7089 | leency | 37 | |
7194 | leency | 38 | #define PAL_ITEMS_X_COUNT 13 |
39 | #define COLSIZE 18 |
||
7200 | leency | 40 | #define RIGHT_BAR_W PAL_ITEMS_X_COUNT*COLSIZE |
7187 | leency | 41 | |
7253 | leency | 42 | #define TO_CANVAS_X(xval) xval - canvas.x/zoom.value |
43 | #define TO_CANVAS_Y(yval) yval - canvas.y/zoom.value |
||
44 | |||
7200 | leency | 45 | block canvas = { NULL, NULL, NULL, NULL }; |
46 | block wrapper = { PANEL_LEFT_W, TOOLBAR_H, NULL, NULL }; |
||
47 | block right_bar = { NULL, TOOLBAR_H, RIGHT_BAR_W+10, NULL }; |
||
48 | |||
49 | block b_color_gradient = {NULL, 30+TOOLBAR_H, RIGHT_BAR_W, 30}; |
||
50 | block b_last_colors = {NULL, 70+TOOLBAR_H, RIGHT_BAR_W, COLSIZE*2}; |
||
51 | block b_default_palette = {NULL, COLSIZE*2+10+70+TOOLBAR_H, RIGHT_BAR_W, COLSIZE*9}; |
||
52 | |||
7207 | leency | 53 | dword color1 = 0x000000; |
54 | dword color2 = 0xFFFfff; |
||
55 | dword tool_color; |
||
7089 | leency | 56 | |
57 | enum { |
||
7096 | leency | 58 | BTN_NEW = 40, |
59 | BTN_OPEN, |
||
60 | BTN_SAVE, |
||
61 | BTN_MOVE_LEFT, |
||
62 | BTN_MOVE_RIGHT, |
||
63 | BTN_MOVE_UP, |
||
64 | BTN_MOVE_DOWN, |
||
65 | BTN_FLIP_HOR, |
||
66 | BTN_FLIP_VER, |
||
67 | BTN_ROTATE_LEFT, |
||
68 | BTN_ROTATE_RIGHT, |
||
7187 | leency | 69 | BTN_PENCIL, |
7096 | leency | 70 | BTN_PICK, |
7156 | leency | 71 | BTN_FILL, |
7186 | leency | 72 | BTN_LINE, |
73 | BTN_RECT, |
||
7253 | leency | 74 | BTN_SELECT, |
75 | BTN_ZOOM_IN, |
||
76 | BTN_ZOOM_OUT, |
||
7155 | leency | 77 | BTNS_PALETTE_COLOR_MAS = 100, |
78 | BTNS_LAST_USED_COLORS = 400 |
||
7089 | leency | 79 | }; |
80 | |||
81 | proc_info Form; |
||
82 | |||
7243 | leency | 83 | more_less_box zoom = { 11, 1, 40, "Zoom" }; |
7150 | leency | 84 | |
7152 | leency | 85 | dword default_palette[] = { |
7190 | leency | 86 | 0x330000,0x331900,0x333300,0x193300,0x003300,0x003319,0x003333,0x001933,0x000033,0x190033, |
87 | 0x330033,0x330019,0x000000,0x660000,0x663300,0x666600,0x336600,0x006600,0x006633,0x006666, |
||
88 | 0x003366,0x000066,0x330066,0x660066,0x660033,0x202020,0x990000,0x994C00,0x999900,0x4C9900, |
||
89 | 0x009900,0x00994C,0x009999,0x004C99,0x000099,0x4C0099,0x990099,0x99004C,0x404040,0xCC0000, |
||
90 | 0xCC6600,0xCCCC00,0x66CC00,0x00CC00,0x00CC66,0x00CCCC,0x0066CC,0x0000CC,0x6600CC,0xCC00CC, |
||
91 | 0xCC0066,0x606060,0xFF0000,0xFF8000,0xFFFF00,0x80FF00,0x00FF00,0x00FF80,0x00FFFF,0x0080FF, |
||
92 | 0x0000FF,0x7F00FF,0xFF00FF,0xFF007F,0x808080,0xFF3333,0xFF9933,0xFFFF33,0x99FF33,0x33FF33, |
||
93 | 0x33FF99,0x33FFFF,0x3399FF,0x3333FF,0x9933FF,0xFF33FF,0xFF3399,0xA0A0A0,0xFF6666,0xFFB266, |
||
94 | 0xFFFF66,0xB2FF66,0x66FF66,0x66FFB2,0x66FFFF,0x66B2FF,0x6666FF,0xB266FF,0xFF66FF,0xFF66B2, |
||
95 | 0xC0C0C0,0xFF9999,0xFFCC99,0xFFFF99,0xCCFF99,0x99FF99,0x99FFCC,0x99FFFF,0x99CCFF,0x9999FF, |
||
96 | 0xCC99FF,0xFF99FF,0xFF99CC,0xE0E0E0,0xFFCCCC,0xFFE5CC,0xFFFFCC,0xE5FFCC,0xCCFFCC,0xCCFFE5, |
||
97 | 0xCCFFFF,0xCCE5FF,0xCCCCFF,0xE5CCFF,0xFFCCFF,0xFFCCE5,0xFFFFFF |
||
7089 | leency | 98 | }; |
7155 | leency | 99 | dword last_used_colors[13*2] = { |
7190 | leency | 100 | 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF, |
101 | 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF, |
||
102 | 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF |
||
7155 | leency | 103 | }; |
7089 | leency | 104 | |
7155 | leency | 105 | _image image; |
7148 | leency | 106 | |
7204 | leency | 107 | #include "actions_history.h" |
108 | |||
7151 | leency | 109 | libimg_image open_image; |
7204 | leency | 110 | _ActionsHistory actionsHistory; |
7151 | leency | 111 | |
7156 | leency | 112 | enum { |
7186 | leency | 113 | TOOL_NONE = -1, |
7187 | leency | 114 | TOOL_PENCIL, |
115 | TOOL_PIPETTE, |
||
116 | TOOL_FILL, |
||
117 | TOOL_LINE, |
||
118 | TOOL_RECT, |
||
7253 | leency | 119 | TOOL_SELECT |
7156 | leency | 120 | }; |
121 | |||
7186 | leency | 122 | struct Tool { |
123 | int id; |
||
124 | |||
125 | void (*activate)(); |
||
126 | void (*deactivate)(); |
||
127 | void (*onMouseEvent)(int x, int y, int lkm, int pkm); |
||
7253 | leency | 128 | void (*onKeyEvent)(dword keycode); |
7186 | leency | 129 | void (*onCanvasDraw)(); |
130 | }; |
||
131 | |||
7253 | leency | 132 | Tool tools[6]; |
7186 | leency | 133 | int currentTool = -1; |
134 | |||
135 | void resetCurrentTool() { |
||
136 | if ((currentTool != TOOL_NONE) && (tools[currentTool].deactivate != 0)) { |
||
137 | tools[currentTool].deactivate(); |
||
138 | } |
||
139 | |||
140 | currentTool = TOOL_NONE; |
||
141 | } |
||
142 | |||
143 | void setCurrentTool(int index) { |
||
144 | resetCurrentTool(); |
||
7204 | leency | 145 | |
146 | currentTool = index; |
||
7186 | leency | 147 | |
148 | if ((index != TOOL_NONE) && (tools[index].activate != 0)) |
||
149 | tools[index].activate(); |
||
7187 | leency | 150 | |
151 | DrawLeftPanel(); |
||
7253 | leency | 152 | DrawCanvas(); |
7186 | leency | 153 | } |
154 | |||
7089 | leency | 155 | //===================================================// |
156 | // // |
||
157 | // CODE // |
||
158 | // // |
||
159 | //===================================================// |
||
160 | |||
7186 | leency | 161 | void FillTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { |
7207 | leency | 162 | if (canvas.hovered()) && (currentTool==TOOL_FILL) && (mouse.up) |
7186 | leency | 163 | { |
7207 | leency | 164 | EventFill(mouseY-canvas.y/zoom.value, |
165 | mouseX-canvas.x/zoom.value, tool_color); |
||
166 | actionsHistory.saveCurrentState(); |
||
7186 | leency | 167 | DrawCanvas(); |
168 | } |
||
169 | } |
||
170 | |||
171 | void PipetteTool_activate() { |
||
172 | SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE); |
||
173 | } |
||
174 | |||
175 | void PipetteTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { |
||
7207 | leency | 176 | tool_color = GetPixelUnderMouse(); |
177 | DrawBar(Form.cwidth-30, 5, 20, 20, tool_color); |
||
7186 | leency | 178 | |
7207 | leency | 179 | if (mouse.down) { |
7186 | leency | 180 | SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER); |
7207 | leency | 181 | if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, tool_color); |
182 | if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, tool_color); |
||
7186 | leency | 183 | |
184 | setCurrentTool(TOOL_PENCIL); |
||
185 | } |
||
186 | } |
||
187 | |||
7204 | leency | 188 | bool PencilTool_Drawing = false; |
189 | |||
7186 | leency | 190 | void PencilTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { |
7200 | leency | 191 | if (canvas.hovered()) |
7186 | leency | 192 | { |
7207 | leency | 193 | if ((PencilTool_Drawing == true) && (!mouse.key)) { |
7204 | leency | 194 | actionsHistory.saveCurrentState(); |
195 | PencilTool_Drawing = false; |
||
196 | } |
||
197 | |||
7207 | leency | 198 | if (mouse.key) { |
7186 | leency | 199 | image.set_pixel(mouseY-canvas.y/zoom.value, |
7207 | leency | 200 | mouseX-canvas.x/zoom.value, tool_color); |
7204 | leency | 201 | PencilTool_Drawing = true; |
7207 | leency | 202 | } |
7186 | leency | 203 | DrawCanvas(); |
204 | } |
||
205 | } |
||
206 | |||
7204 | leency | 207 | void PencilTool_reset() { |
208 | PencilTool_Drawing = false; |
||
209 | } |
||
210 | |||
7186 | leency | 211 | // Line tool |
7204 | leency | 212 | struct SimpleFigureTool_State { |
213 | int startX, startY; |
||
214 | int lastTempPosX, lastTempPosY; |
||
215 | }; |
||
7186 | leency | 216 | |
7204 | leency | 217 | enum { |
218 | TOOL_LINE_STATE, |
||
219 | TOOL_RECT_STATE |
||
220 | }; |
||
7186 | leency | 221 | |
7204 | leency | 222 | dword currentFigToolState = -1; |
223 | SimpleFigureTool_State figTool_States[2]; |
||
224 | |||
225 | void SimpleFigureTool_Reset() { |
||
226 | if (currentTool == TOOL_LINE) |
||
227 | currentFigToolState = TOOL_LINE_STATE; |
||
228 | else if (currentTool == TOOL_RECT) |
||
229 | currentFigToolState = TOOL_RECT_STATE; |
||
230 | |||
231 | figTool_States[currentFigToolState].startX = -1; |
||
232 | figTool_States[currentFigToolState].startY = -1; |
||
233 | figTool_States[currentFigToolState].lastTempPosX = -1; |
||
234 | figTool_States[currentFigToolState].lastTempPosY = -1; |
||
7186 | leency | 235 | } |
236 | |||
7206 | leency | 237 | int mouseX_last; |
238 | int mouseY_last; |
||
239 | bool first_click_in_canvas = false; |
||
240 | |||
7204 | leency | 241 | void SimpleFigureTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { |
7206 | leency | 242 | if (mouse.down) && (canvas.hovered()) first_click_in_canvas = true; |
243 | if (first_click_in_canvas) |
||
7186 | leency | 244 | { |
7206 | leency | 245 | if (mouseX>canvas.x+canvas.w-zoom.value) mouseX = canvas.x+canvas.w-zoom.value; |
246 | if (mouseY>canvas.y+canvas.h-zoom.value) mouseY = canvas.y+canvas.h-zoom.value; |
||
247 | if (mouseX |
||
248 | if (mouseY |
||
249 | |||
7207 | leency | 250 | if (mouse.key) { |
7204 | leency | 251 | if ((figTool_States[currentFigToolState].startX < 0) || (figTool_States[currentFigToolState].startY < 0)) { |
252 | figTool_States[currentFigToolState].startX = mouseX; |
||
253 | figTool_States[currentFigToolState].startY = mouseY; |
||
7186 | leency | 254 | } |
255 | else { |
||
7204 | leency | 256 | if ((calc(mouseX - canvas.x/zoom.value) != figTool_States[currentFigToolState].lastTempPosX) |
257 | || (calc(mouseY - canvas.y/zoom.value) != figTool_States[currentFigToolState].lastTempPosY)) |
||
258 | { |
||
259 | DrawCanvas(); |
||
260 | } |
||
7186 | leency | 261 | } |
7206 | leency | 262 | mouseX_last = mouseX; |
263 | mouseY_last = mouseY; |
||
7186 | leency | 264 | } |
7206 | leency | 265 | if (mouse.up) { |
7204 | leency | 266 | if ((figTool_States[currentFigToolState].startX >= 0) && (figTool_States[currentFigToolState].startY >= 0)) { |
267 | // Draw line from start position to current position |
||
268 | if (currentTool == TOOL_LINE) { |
||
269 | DrawLine(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, |
||
270 | figTool_States[currentFigToolState].startY - canvas.y/zoom.value, |
||
271 | mouseX - canvas.x/zoom.value, |
||
272 | mouseY - canvas.y/zoom.value, |
||
7207 | leency | 273 | tool_color, |
7204 | leency | 274 | 1); |
275 | } |
||
276 | else if (currentTool == TOOL_RECT) { |
||
277 | DrawRectangleInCanvas(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, |
||
278 | figTool_States[currentFigToolState].startY - canvas.y/zoom.value, |
||
279 | mouseX - canvas.x/zoom.value, |
||
7207 | leency | 280 | mouseY - canvas.y/zoom.value, tool_color, 1); |
7204 | leency | 281 | } |
282 | |||
7186 | leency | 283 | DrawCanvas(); |
284 | |||
7204 | leency | 285 | actionsHistory.saveCurrentState(); |
7186 | leency | 286 | |
287 | // Reset start position |
||
7204 | leency | 288 | figTool_States[currentFigToolState].startX = -1; |
289 | figTool_States[currentFigToolState].startY = -1; |
||
7206 | leency | 290 | |
291 | first_click_in_canvas = false; |
||
7186 | leency | 292 | } |
293 | } |
||
294 | } |
||
295 | } |
||
296 | |||
7204 | leency | 297 | void SimpleFigureTool_onCanvasDraw() { |
7207 | leency | 298 | if ((figTool_States[currentFigToolState].startX >= 0) && (figTool_States[currentFigToolState].startY >= 0) && (mouse.key)) { |
7204 | leency | 299 | if (currentTool == TOOL_LINE) { |
300 | DrawLine(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, |
||
301 | figTool_States[currentFigToolState].startY - canvas.y/zoom.value, |
||
7206 | leency | 302 | mouseX_last - canvas.x/zoom.value, |
303 | mouseY_last - canvas.y/zoom.value, |
||
7207 | leency | 304 | tool_color, |
7204 | leency | 305 | 2); |
306 | } |
||
307 | else if (currentTool == TOOL_RECT) { |
||
308 | DrawRectangleInCanvas(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, |
||
309 | figTool_States[currentFigToolState].startY - canvas.y/zoom.value, |
||
7206 | leency | 310 | mouseX_last - canvas.x/zoom.value, |
311 | mouseY_last - canvas.y/zoom.value, |
||
7207 | leency | 312 | tool_color, |
7206 | leency | 313 | 2); |
7204 | leency | 314 | } |
315 | |||
7206 | leency | 316 | figTool_States[currentFigToolState].lastTempPosX = mouseX_last - canvas.x/zoom.value; |
317 | figTool_States[currentFigToolState].lastTempPosY = mouseY_last - canvas.y/zoom.value; |
||
7186 | leency | 318 | } |
319 | } |
||
320 | |||
7253 | leency | 321 | // Selection |
322 | int selection_start_x = -1; |
||
323 | int selection_start_y = -1; |
||
324 | int selection_end_x = -1; |
||
325 | int selection_end_y = -1; |
||
326 | bool selection_active = false; |
||
327 | |||
328 | dword SelectionTool_buffer = 0; |
||
329 | dword SelectionTool_buffer_r = 0; |
||
330 | dword SelectionTool_buffer_c = 0; |
||
331 | |||
332 | bool selection_moving_started = false; |
||
333 | int selection_pivot_x = -1; |
||
334 | int selection_pivot_y = -1; |
||
335 | |||
336 | void SelectTool_normalizeSelection() { |
||
337 | int t; |
||
338 | |||
339 | // Restructuring of the selection coordinates |
||
340 | if (selection_end_x < selection_start_x) { |
||
341 | t = selection_start_x; |
||
342 | selection_start_x = selection_end_x; |
||
343 | selection_end_x = t; |
||
344 | } |
||
345 | |||
346 | if (selection_end_y < selection_start_y) { |
||
347 | t = selection_end_y; |
||
348 | selection_end_y = selection_start_y; |
||
349 | selection_start_y = t; |
||
350 | } |
||
351 | } |
||
352 | |||
353 | void reset_selection_moving() { |
||
354 | if (selection_moving_started) { |
||
355 | SelectTool_drawBuffer(selection_start_x, selection_start_y, 1); |
||
356 | |||
357 | selection_pivot_x = -1; |
||
358 | selection_pivot_y = -1; |
||
359 | |||
360 | selection_moving_started = false; |
||
361 | |||
362 | actionsHistory.saveCurrentState(); |
||
363 | DrawCanvas(); |
||
364 | } |
||
365 | } |
||
366 | |||
367 | bool is_selection_moving() { |
||
368 | return selection_moving_started; |
||
369 | } |
||
370 | |||
371 | void reset_selection() { |
||
372 | reset_selection_moving(); |
||
373 | |||
374 | selection_start_x = -1; |
||
375 | selection_start_y = -1; |
||
376 | selection_end_x = -1; |
||
377 | selection_end_y = -1; |
||
378 | } |
||
379 | |||
380 | void SelectTool_activate() { |
||
381 | reset_selection(); |
||
382 | |||
383 | selection_active = false; |
||
384 | } |
||
385 | |||
386 | void SelectTool_deactivate() { |
||
387 | reset_selection_moving(); |
||
388 | } |
||
389 | |||
390 | bool SelectTool_pointInSelection(int x, int y) { |
||
391 | if (x >= selection_start_x) && (x <= selection_end_x) && (y >= selection_start_y) && (y <= selection_end_y) |
||
392 | return true; |
||
393 | else |
||
394 | return false; |
||
395 | } |
||
396 | |||
397 | |||
398 | void SelectTool_copyToBuffer() { |
||
399 | dword offset, r, c; |
||
400 | |||
401 | if (SelectionTool_buffer != 0) |
||
402 | free(SelectionTool_buffer); |
||
403 | |||
404 | SelectionTool_buffer_r = selection_end_y - selection_start_y + 1; |
||
405 | SelectionTool_buffer_c = selection_end_x - selection_start_x + 1; |
||
406 | SelectionTool_buffer = malloc(SelectionTool_buffer_r * SelectionTool_buffer_c * 4); |
||
407 | |||
408 | for (r = selection_start_y; r <= selection_end_y; r++) { |
||
409 | for (c = selection_start_x; c <= selection_end_x; c++) { |
||
410 | offset = calc(SelectionTool_buffer_c * calc(r - selection_start_y) + calc(c - selection_start_x)) * 4; |
||
411 | |||
412 | ESDWORD[SelectionTool_buffer + offset] = image.get_pixel(r, c); |
||
413 | } |
||
414 | } |
||
415 | } |
||
416 | |||
417 | void SelectTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { |
||
418 | int click_x, click_y, dx, dy, m_x, m_y, r, c, color; |
||
419 | dword pixel; |
||
420 | |||
421 | m_x = TO_CANVAS_X(mouseX); |
||
422 | m_y = TO_CANVAS_Y(mouseY); |
||
423 | |||
424 | if (mouse.down) && (canvas.hovered()) && (!selection_active) { |
||
425 | if (selection_start_x != -1) && (SelectTool_pointInSelection(m_x, m_y)) { |
||
426 | if (selection_pivot_x == -1) { |
||
427 | selection_pivot_x = m_x; |
||
428 | selection_pivot_y = m_y; |
||
429 | |||
430 | GetKeys(); |
||
431 | |||
432 | if (!selection_moving_started) && ( !(key_modifier&KEY_LSHIFT) ) { |
||
433 | for (r = selection_start_y; r <= selection_end_y; r++) |
||
434 | for (c = selection_start_x; c <= selection_end_x; c++) { |
||
435 | image.set_pixel(r, c, color2); |
||
436 | } |
||
437 | } |
||
438 | |||
439 | selection_moving_started = true; |
||
440 | } |
||
441 | } |
||
442 | else { |
||
443 | |||
444 | reset_selection(); |
||
445 | selection_active = true; |
||
446 | } |
||
447 | } |
||
448 | |||
449 | if (selection_pivot_x != -1) { |
||
450 | dx = m_x - selection_pivot_x; |
||
451 | dy = m_y - selection_pivot_y; |
||
452 | |||
453 | if (selection_start_x + dx < 0) |
||
454 | dx = selection_start_x; |
||
455 | |||
456 | if (selection_end_x + dx >= 32) |
||
457 | dx = 31 - selection_end_x; |
||
458 | |||
459 | if (selection_start_y + dy < 0) |
||
460 | dy = selection_start_y; |
||
461 | |||
462 | if (selection_end_y + dy >= 32) |
||
463 | dy = 31 - selection_end_y; |
||
464 | |||
465 | |||
466 | selection_start_x += dx; |
||
467 | selection_end_x += dx; |
||
468 | |||
469 | selection_start_y += dy; |
||
470 | selection_end_y += dy; |
||
471 | |||
472 | selection_pivot_x += dx; |
||
473 | selection_pivot_y += dy; |
||
474 | |||
475 | DrawCanvas(); |
||
476 | } |
||
477 | |||
478 | if (selection_active) |
||
479 | { |
||
480 | if (mouseX>canvas.x+canvas.w-zoom.value) mouseX = canvas.x+canvas.w-zoom.value; |
||
481 | if (mouseY>canvas.y+canvas.h-zoom.value) mouseY = canvas.y+canvas.h-zoom.value; |
||
482 | |||
483 | if (mouseX |
||
484 | if (mouseY |
||
485 | |||
486 | if (mouse.key) { |
||
487 | selection_end_x = TO_CANVAS_X(mouseX); |
||
488 | selection_end_y = TO_CANVAS_Y(mouseY); |
||
489 | |||
490 | if ((selection_start_x < 0) || (selection_start_y < 0)) { |
||
491 | selection_start_x = TO_CANVAS_X(mouseX); |
||
492 | selection_start_y = TO_CANVAS_Y(mouseY); |
||
493 | } |
||
494 | else { |
||
495 | DrawCanvas(); |
||
496 | |||
497 | /**if ((calc(TO_CANVAS_X(mouseX)) != selection_end_x) |
||
498 | || (calc(TO_CANVAS_Y(mouseY)) != selection_end_y)) |
||
499 | { |
||
500 | DrawCanvas(); |
||
501 | }*/ |
||
502 | } |
||
503 | |||
504 | } |
||
505 | |||
506 | if (mouse.up) { |
||
507 | selection_active = false; |
||
508 | |||
509 | SelectTool_normalizeSelection(); |
||
510 | SelectTool_copyToBuffer(); |
||
511 | } |
||
512 | } |
||
513 | |||
514 | if (mouse.up) { |
||
515 | if (selection_pivot_x != -1) { |
||
516 | selection_pivot_x = -1; |
||
517 | selection_pivot_y = -1; |
||
518 | } |
||
519 | } |
||
520 | } |
||
521 | |||
522 | void SelectTool_onCanvasDraw() { |
||
523 | if (selection_moving_started) |
||
524 | SelectTool_drawBuffer(selection_start_x, selection_start_y, 2); |
||
525 | |||
526 | if ((selection_start_x >= 0) && (selection_start_y >= 0) && (selection_end_x >= 0) && (selection_end_y >= 0)) { |
||
527 | DrawSelection(selection_start_x, selection_start_y, selection_end_x, selection_end_y); |
||
528 | } |
||
529 | } |
||
530 | |||
531 | void SelectTool_drawBuffer(int insert_x, int insert_y, int target) { |
||
532 | dword color; |
||
533 | dword offset, r, c; |
||
534 | dword insert_to_x, insert_to_y; |
||
535 | |||
536 | if (SelectionTool_buffer != 0) { |
||
537 | insert_to_x = insert_x + SelectionTool_buffer_c - 1; |
||
538 | |||
539 | if (insert_to_x >= image.columns) |
||
540 | insert_to_x = image.columns-1; |
||
541 | |||
542 | insert_to_y = insert_y + SelectionTool_buffer_r - 1; |
||
543 | |||
544 | if (insert_to_y >= image.rows) |
||
545 | insert_to_y = image.rows-1; |
||
546 | |||
547 | for (r = insert_y; r <= insert_to_y; r++) { |
||
548 | for (c = insert_x; c <= insert_to_x; c++) { |
||
549 | offset = calc(SelectionTool_buffer_c * calc(r - insert_y) + calc(c - insert_x)) * 4; |
||
550 | |||
551 | color = ESDWORD[SelectionTool_buffer + offset]; |
||
552 | |||
553 | if (target == 1) |
||
554 | image.set_pixel(r, c, color); |
||
555 | else |
||
556 | DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y, |
||
557 | zoom.value, zoom.value, color); |
||
558 | } |
||
559 | } |
||
560 | } |
||
561 | } |
||
562 | |||
563 | void SelectTool_onKeyEvent(dword keycode) { |
||
564 | dword offset, r, c; |
||
565 | dword insert_x, insert_y, insert_to_x, insert_to_y; |
||
566 | |||
567 | if (keycode == SCAN_CODE_KEY_V) { |
||
568 | if (SelectionTool_buffer != 0) { |
||
569 | reset_selection(); |
||
570 | |||
571 | selection_moving_started = true; |
||
572 | selection_start_x = 0; |
||
573 | selection_end_x = SelectionTool_buffer_c - 1; |
||
574 | |||
575 | selection_start_y = 0; |
||
576 | selection_end_y = SelectionTool_buffer_r - 1; |
||
577 | |||
578 | DrawCanvas(); |
||
579 | |||
580 | } |
||
581 | } |
||
582 | } |
||
583 | |||
7186 | leency | 584 | void initTools() |
585 | { |
||
7187 | leency | 586 | tools[0].id = TOOL_PENCIL; |
587 | tools[0].onMouseEvent = #PencilTool_onMouseEvent; |
||
7204 | leency | 588 | tools[0].deactivate = #PencilTool_reset; |
7186 | leency | 589 | |
590 | tools[1].id = TOOL_PIPETTE; |
||
591 | tools[1].activate = #PipetteTool_activate; |
||
592 | tools[1].onMouseEvent = #PipetteTool_onMouseEvent; |
||
593 | |||
7187 | leency | 594 | tools[2].id = TOOL_FILL; |
595 | tools[2].onMouseEvent = #FillTool_onMouseEvent; |
||
7186 | leency | 596 | |
7204 | leency | 597 | tools[3].id = TOOL_LINE; |
598 | tools[3].activate = #SimpleFigureTool_Reset; |
||
599 | tools[3].deactivate = #SimpleFigureTool_Reset; |
||
600 | tools[3].onMouseEvent = #SimpleFigureTool_onMouseEvent; |
||
601 | tools[3].onCanvasDraw = #SimpleFigureTool_onCanvasDraw; |
||
7186 | leency | 602 | |
603 | tools[4].id = TOOL_RECT; |
||
7204 | leency | 604 | tools[4].activate = #SimpleFigureTool_Reset; |
605 | tools[4].deactivate = #SimpleFigureTool_Reset; |
||
606 | tools[4].onMouseEvent = #SimpleFigureTool_onMouseEvent; |
||
607 | tools[4].onCanvasDraw = #SimpleFigureTool_onCanvasDraw; |
||
7253 | leency | 608 | |
609 | tools[5].id = TOOL_SELECT; |
||
610 | tools[5].activate = #SelectTool_activate; |
||
611 | tools[5].deactivate = #SelectTool_deactivate; |
||
612 | tools[5].onMouseEvent = #SelectTool_onMouseEvent; |
||
613 | tools[5].onCanvasDraw = #SelectTool_onCanvasDraw; |
||
614 | tools[5].onKeyEvent = #SelectTool_onKeyEvent; |
||
7186 | leency | 615 | } |
616 | |||
7089 | leency | 617 | void main() |
618 | { |
||
619 | word btn; |
||
620 | |||
7096 | leency | 621 | load_dll(libio, #libio_init, 1); |
622 | load_dll(libimg, #libimg_init, 1); |
||
623 | Libimg_LoadImage(#skin, "/sys/icons16.png"); |
||
7156 | leency | 624 | //system.color.get(); |
7187 | leency | 625 | //Libimg_ReplaceColor(tools_img.image, tools_img.w, tools_img.h, 0xFFF8C0D0, system.color.work); |
7186 | leency | 626 | |
7155 | leency | 627 | image.create(32, 32); |
7089 | leency | 628 | |
7156 | leency | 629 | if (param[0]) { |
7151 | leency | 630 | Libimg_LoadImage(#open_image, #param); |
631 | if (open_image.w==32) && (open_image.h==32) { |
||
7156 | leency | 632 | image.set_image(open_image.imgsrc); |
7151 | leency | 633 | } |
634 | else { |
||
7190 | leency | 635 | notify("'Error: image format is unacceptable (PNG, 32x32x16b expected)' -E"); |
7151 | leency | 636 | } |
637 | } |
||
638 | |||
7204 | leency | 639 | actionsHistory.init(); |
640 | |||
7186 | leency | 641 | initTools(); |
642 | setCurrentTool(TOOL_PENCIL); |
||
643 | |||
7096 | leency | 644 | SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER); |
645 | |||
7089 | leency | 646 | loop() switch(WaitEvent()) |
647 | { |
||
7096 | leency | 648 | case evMouse: |
649 | mouse.get(); |
||
7186 | leency | 650 | |
7207 | leency | 651 | if (mouse.lkm) tool_color = color1; |
652 | if (mouse.pkm) tool_color = color2; |
||
653 | if (mouse.mkm) break; |
||
654 | |||
7186 | leency | 655 | if (currentTool != TOOL_NONE) |
656 | tools[currentTool].onMouseEvent(mouse.x, mouse.y, mouse.lkm, mouse.pkm); |
||
657 | |||
7156 | leency | 658 | if (mouse.vert) { |
7243 | leency | 659 | if (mouse.vert==65535) zoom.inc(); |
660 | if (mouse.vert==1) zoom.dec(); |
||
7152 | leency | 661 | DrawEditArea(); |
662 | } |
||
7186 | leency | 663 | |
7200 | leency | 664 | if (mouse.down) { |
665 | if (b_color_gradient.hovered()) |
||
666 | || (b_last_colors.hovered()) |
||
667 | || (b_default_palette.hovered()) { |
||
668 | if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, GetPixelUnderMouse()); |
||
669 | if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, GetPixelUnderMouse()); |
||
670 | } |
||
671 | } |
||
672 | |||
7148 | leency | 673 | break; |
7096 | leency | 674 | |
7089 | leency | 675 | case evButton: |
7148 | leency | 676 | btn = GetButtonID(); |
7253 | leency | 677 | |
7096 | leency | 678 | switch(btn) |
679 | { |
||
7151 | leency | 680 | case BTN_NEW: |
7155 | leency | 681 | image.create(32, 32); |
7151 | leency | 682 | DrawCanvas(); |
683 | break; |
||
684 | case BTN_OPEN: |
||
7190 | leency | 685 | RunProgram("/sys/lod", sprintf(#param, "*png* %s",#program_path)); |
7151 | leency | 686 | break; |
687 | case BTN_SAVE: |
||
688 | EventSave(); |
||
689 | break; |
||
7096 | leency | 690 | case BTN_MOVE_LEFT: |
7151 | leency | 691 | image.move(MOVE_LEFT); |
692 | DrawCanvas(); |
||
7096 | leency | 693 | break; |
694 | case BTN_MOVE_RIGHT: |
||
7151 | leency | 695 | image.move(MOVE_RIGHT); |
696 | DrawCanvas(); |
||
7096 | leency | 697 | break; |
7147 | leency | 698 | case BTN_MOVE_UP: |
7151 | leency | 699 | image.move(MOVE_UP); |
700 | DrawCanvas(); |
||
7147 | leency | 701 | break; |
702 | case BTN_MOVE_DOWN: |
||
7151 | leency | 703 | image.move(MOVE_DOWN); |
704 | DrawCanvas(); |
||
7147 | leency | 705 | break; |
7151 | leency | 706 | case BTN_FLIP_VER: |
707 | image.move(FLIP_VER); |
||
708 | DrawCanvas(); |
||
709 | break; |
||
710 | case BTN_FLIP_HOR: |
||
711 | image.move(FLIP_HOR); |
||
712 | DrawCanvas(); |
||
713 | break; |
||
7187 | leency | 714 | case BTN_PENCIL: |
715 | setCurrentTool(TOOL_PENCIL); |
||
716 | break; |
||
7096 | leency | 717 | case BTN_PICK: |
7186 | leency | 718 | setCurrentTool(TOOL_PIPETTE); |
719 | //EventPickActivate(); |
||
7096 | leency | 720 | break; |
7156 | leency | 721 | case BTN_FILL: |
7186 | leency | 722 | setCurrentTool(TOOL_FILL); |
723 | //EventFillActivate(); |
||
7156 | leency | 724 | break; |
7186 | leency | 725 | case BTN_LINE: |
726 | setCurrentTool(TOOL_LINE); |
||
727 | break; |
||
728 | case BTN_RECT: |
||
729 | setCurrentTool(TOOL_RECT); |
||
730 | break; |
||
7253 | leency | 731 | case BTN_SELECT: |
732 | setCurrentTool(TOOL_SELECT); |
||
733 | break; |
||
734 | case BTN_ZOOM_IN: |
||
735 | zoom.inc(); |
||
736 | DrawEditArea(); |
||
737 | break; |
||
738 | case BTN_ZOOM_OUT: |
||
739 | zoom.dec(); |
||
740 | DrawEditArea(); |
||
741 | break; |
||
7152 | leency | 742 | case CLOSE_BTN: |
743 | ExitProcess(); |
||
744 | break; |
||
7089 | leency | 745 | } |
746 | break; |
||
747 | |||
748 | case evKey: |
||
749 | GetKeys(); |
||
7253 | leency | 750 | |
751 | if (currentTool != TOOL_NONE) && (tools[currentTool].onKeyEvent != 0) |
||
752 | tools[currentTool].onKeyEvent(key_scancode); |
||
753 | |||
7186 | leency | 754 | if (key_scancode == SCAN_CODE_ESC) setCurrentTool(TOOL_PENCIL); |
7187 | leency | 755 | if (key_scancode == SCAN_CODE_KEY_P) setCurrentTool(TOOL_PENCIL); |
7186 | leency | 756 | if (key_scancode == SCAN_CODE_KEY_I) setCurrentTool(TOOL_PIPETTE); |
7187 | leency | 757 | if (key_scancode == SCAN_CODE_KEY_F) setCurrentTool(TOOL_FILL); |
758 | if (key_scancode == SCAN_CODE_KEY_L) setCurrentTool(TOOL_LINE); |
||
759 | if (key_scancode == SCAN_CODE_KEY_R) setCurrentTool(TOOL_RECT); |
||
7204 | leency | 760 | |
7253 | leency | 761 | if (key_scancode == SCAN_CODE_KEY_Z) && (key_modifier&KEY_LCTRL) actionsHistory.undoLastAction(); |
762 | if (key_scancode == SCAN_CODE_KEY_Y) && (key_modifier&KEY_LCTRL) actionsHistory.redoLastAction(); |
||
7204 | leency | 763 | |
7253 | leency | 764 | if (key_scancode == SCAN_CODE_MINUS) {zoom.dec(); DrawEditArea();} |
765 | if (key_scancode == SCAN_CODE_PLUS) {zoom.inc(); DrawEditArea();} |
||
766 | |||
7089 | leency | 767 | break; |
768 | |||
769 | case evReDraw: |
||
770 | draw_window(); |
||
771 | break; |
||
772 | } |
||
773 | } |
||
774 | |||
7096 | leency | 775 | void DrawToolbarButton(dword _id, _x, _icon_n) |
776 | { |
||
7152 | leency | 777 | DrawWideRectangle(_x, 4, 22, 22, 3, 0xFFFfff); |
778 | DefineHiddenButton(_x, 4, 21, 21, _id); |
||
779 | img_draw stdcall(skin.image, _x+3, 7, 16, 16, 0, _icon_n*16); |
||
7096 | leency | 780 | } |
781 | |||
7187 | leency | 782 | void DrawLeftPanelButton(dword _id, _y, _icon_n) |
783 | { |
||
784 | int x = 5; |
||
785 | DrawWideRectangle(x, _y, 22, 22, 3, 0xFFFfff); |
||
786 | DefineHiddenButton(x, _y, 21, 21, _id); |
||
787 | img_draw stdcall(skin.image, x+3, _y+3, 16, 16, 0, _icon_n*16); |
||
788 | } |
||
789 | |||
7148 | leency | 790 | void DrawStatusBar() |
791 | { |
||
7243 | leency | 792 | zoom.draw(wrapper.x, wrapper.y + wrapper.h + 6); |
7150 | leency | 793 | |
7151 | leency | 794 | sprintf(#param,"Canvas: %ix%i", image.rows, image.columns); |
7150 | leency | 795 | WriteText(wrapper.x+wrapper.w-calc(strlen(#param)*8), zoom.y+2, 0x90, system.color.work_text, #param); |
7148 | leency | 796 | } |
797 | |||
7089 | leency | 798 | void draw_window() |
799 | { |
||
7096 | leency | 800 | incn tx; |
7089 | leency | 801 | system.color.get(); |
7151 | leency | 802 | DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x33, system.color.work, T_TITLE, 0); |
7089 | leency | 803 | GetProcessInfo(#Form, SelfInfo); |
7152 | leency | 804 | if (Form.status_window>2) return; |
805 | if (Form.width < 560) { MoveSize(OLD,OLD,560,OLD); return; } |
||
806 | if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; } |
||
807 | |||
7150 | leency | 808 | right_bar.x = Form.cwidth - right_bar.w; |
7200 | leency | 809 | b_color_gradient.x = b_last_colors.x = b_default_palette.x = right_bar.x; |
7089 | leency | 810 | |
7155 | leency | 811 | tx.n = 10-TB_ICON_PADDING; |
7148 | leency | 812 | DrawToolbarButton(BTN_NEW, tx.inc(TB_ICON_PADDING), 2); //not implemented |
813 | DrawToolbarButton(BTN_OPEN, tx.inc(TB_ICON_PADDING), 0); //not implemented |
||
7151 | leency | 814 | DrawToolbarButton(BTN_SAVE, tx.inc(TB_ICON_PADDING), 5); |
7207 | leency | 815 | DrawToolbarButton(BTN_MOVE_LEFT, tx.inc(TB_ICON_PADDING+8), 30); |
7096 | leency | 816 | DrawToolbarButton(BTN_MOVE_RIGHT, tx.inc(TB_ICON_PADDING), 31); |
7147 | leency | 817 | DrawToolbarButton(BTN_MOVE_UP, tx.inc(TB_ICON_PADDING), 32); |
818 | DrawToolbarButton(BTN_MOVE_DOWN, tx.inc(TB_ICON_PADDING), 33); |
||
819 | |||
7155 | leency | 820 | DrawToolbarButton(BTN_FLIP_HOR, tx.inc(TB_ICON_PADDING+8), 34); |
821 | DrawToolbarButton(BTN_FLIP_VER, tx.inc(TB_ICON_PADDING), 35); |
||
822 | // DrawToolbarButton(BTN_ROTATE_LEFT, tx.inc(TB_ICON_PADDING), 36); //not implemented |
||
823 | // DrawToolbarButton(BTN_ROTATE_RIGHT, tx.inc(TB_ICON_PADDING), 37); //not implemented |
||
7148 | leency | 824 | |
7187 | leency | 825 | DrawLeftPanel(); |
7186 | leency | 826 | |
7150 | leency | 827 | DrawEditArea(); |
7096 | leency | 828 | |
7155 | leency | 829 | DrawActiveColor(right_bar.y); |
7200 | leency | 830 | DrawColorPallets(); |
7089 | leency | 831 | |
7148 | leency | 832 | DrawStatusBar(); |
7089 | leency | 833 | } |
834 | |||
7187 | leency | 835 | void DrawLeftPanel() |
836 | { |
||
837 | incn ty; |
||
838 | ty.n = TOOLBAR_H-TB_ICON_PADDING; |
||
839 | DrawLeftPanelButton(BTN_PENCIL, ty.inc(TB_ICON_PADDING), 38); |
||
840 | DrawLeftPanelButton(BTN_PICK, ty.inc(TB_ICON_PADDING), 39); |
||
841 | DrawLeftPanelButton(BTN_FILL, ty.inc(TB_ICON_PADDING), 40); |
||
842 | DrawLeftPanelButton(BTN_LINE, ty.inc(TB_ICON_PADDING), 41); |
||
843 | DrawLeftPanelButton(BTN_RECT, ty.inc(TB_ICON_PADDING), 42); |
||
7253 | leency | 844 | DrawLeftPanelButton(BTN_SELECT, ty.inc(TB_ICON_PADDING), 43); |
7187 | leency | 845 | DrawRectangle3D(5, currentTool*TB_ICON_PADDING+TOOLBAR_H, 16+3+2, 16+3+2, 0x333333, 0x777777); |
846 | } |
||
847 | |||
7151 | leency | 848 | void DrawEditArea() |
7089 | leency | 849 | { |
7150 | leency | 850 | dword color1=0xC0C0C0; |
7155 | leency | 851 | int top_side; |
852 | int left_side; |
||
7089 | leency | 853 | |
7194 | leency | 854 | wrapper.w = Form.cwidth - right_bar.w - 10 - wrapper.x; |
7150 | leency | 855 | wrapper.h = Form.cheight - TOOLBAR_H - 35; |
7148 | leency | 856 | |
7150 | leency | 857 | //canvas{ |
7151 | leency | 858 | canvas.w = image.columns * zoom.value; |
859 | canvas.h = image.rows * zoom.value; |
||
860 | if (canvas.w+2 > wrapper.w) || (canvas.h+2 > wrapper.h) { |
||
7243 | leency | 861 | zoom.dec(); |
7151 | leency | 862 | DrawEditArea(); |
863 | return; |
||
864 | } |
||
865 | canvas.x = -zoom.value*image.columns+wrapper.w/2 + wrapper.x; |
||
866 | canvas.y = -zoom.value*image.rows+wrapper.h/2 + wrapper.y; |
||
867 | DrawCanvas(); |
||
7150 | leency | 868 | //} |
7089 | leency | 869 | |
7155 | leency | 870 | left_side = canvas.x-wrapper.x-1; |
871 | top_side = canvas.y-wrapper.y-1; |
||
7148 | leency | 872 | |
7155 | leency | 873 | DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w, wrapper.h, system.color.work_graph); |
7150 | leency | 874 | |
7155 | leency | 875 | if (left_side>0) |
7150 | leency | 876 | { |
7155 | leency | 877 | DrawBar(wrapper.x, wrapper.y, wrapper.w-1, top_side, color1); //top |
878 | DrawBar(wrapper.x, wrapper.y+wrapper.h-top_side-1, wrapper.w-1, top_side, color1); //bottom |
||
7150 | leency | 879 | } |
7155 | leency | 880 | if (top_side>0) |
7150 | leency | 881 | { |
7190 | leency | 882 | //left |
883 | DrawBar(wrapper.x, wrapper.y+top_side, left_side, |
||
884 | wrapper.h-top_side-top_side, color1); |
||
885 | //right |
||
886 | DrawBar(wrapper.x+wrapper.w-left_side-1, wrapper.y+top_side, left_side, |
||
887 | wrapper.h-top_side-top_side, color1); |
||
7150 | leency | 888 | } |
7194 | leency | 889 | DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080); |
7089 | leency | 890 | } |
891 | |||
892 | void DrawActiveColor(dword iny) |
||
893 | { |
||
894 | static dword outy; |
||
895 | if (iny != NULL) outy = iny; |
||
7207 | leency | 896 | DrawBar(right_bar.x, outy, 20, 20, color1); |
897 | sprintf(#param, "%A", color1); |
||
7089 | leency | 898 | EDI = system.color.work; |
7148 | leency | 899 | WriteText(right_bar.x + 30, outy + 3, 0xD0, system.color.work_text, #param+4); |
7151 | leency | 900 | |
7207 | leency | 901 | DrawBar(right_bar.x+110, outy, 20, 20, color2); |
902 | sprintf(#param, "%A", color2); |
||
7151 | leency | 903 | EDI = system.color.work; |
904 | WriteText(right_bar.x+110 + 30, outy + 3, 0xD0, system.color.work_text, #param+4); |
||
7200 | leency | 905 | DrawCurrentColorGradientByLightness(); |
7089 | leency | 906 | } |
907 | |||
7200 | leency | 908 | void DrawCurrentColorGradientByLightness() |
7194 | leency | 909 | { |
910 | int i; |
||
911 | int w = right_bar.w-10/2; |
||
912 | for (i=0; i |
||
7200 | leency | 913 | DrawBar(b_color_gradient.x+i, b_color_gradient.y, |
7207 | leency | 914 | 1, b_color_gradient.h, MixColors(color1,0xFFFfff,255*i/w)); |
7194 | leency | 915 | for (i=0 ; i<=w; i++) |
7200 | leency | 916 | DrawBar(b_color_gradient.x+w+w-i, b_color_gradient.y, |
7207 | leency | 917 | 1, b_color_gradient.h, MixColors(color1,0x000000,255*i/w)); |
7194 | leency | 918 | } |
919 | |||
7200 | leency | 920 | void DrawColorPallets() |
7089 | leency | 921 | { |
7155 | leency | 922 | int r, c, i=0; |
923 | //Last used colors |
||
924 | for (r = 0; r < 2; r++) |
||
925 | { |
||
7194 | leency | 926 | for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++) |
7155 | leency | 927 | { |
7200 | leency | 928 | DrawBar(c*COLSIZE + b_last_colors.x, r*COLSIZE + b_last_colors.y, |
929 | COLSIZE, COLSIZE, last_used_colors[i]); |
||
7155 | leency | 930 | } |
931 | } |
||
932 | i=0; |
||
933 | //Default colors |
||
7089 | leency | 934 | for (r = 0; r < 9; r++) |
935 | { |
||
7194 | leency | 936 | for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++) |
7089 | leency | 937 | { |
7200 | leency | 938 | DrawBar(c*COLSIZE + b_default_palette.x, r*COLSIZE + b_default_palette.y, |
939 | COLSIZE, COLSIZE, default_palette[PALLETE_SIZE-i]); |
||
7089 | leency | 940 | } |
941 | } |
||
942 | } |
||
943 | |||
7155 | leency | 944 | void DrawCanvas() |
945 | { |
||
946 | int r, c; |
||
947 | for (r = 0; r < image.rows; r++) |
||
948 | { |
||
949 | for (c = 0; c < image.columns; c++) |
||
950 | { |
||
951 | DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y, |
||
952 | zoom.value, zoom.value, image.get_pixel(r, c)); |
||
953 | } |
||
954 | } |
||
7186 | leency | 955 | |
956 | if ((currentTool != TOOL_NONE) && (tools[currentTool].onCanvasDraw != 0)) |
||
957 | tools[currentTool].onCanvasDraw(); |
||
958 | |||
7200 | leency | 959 | DrawPreview(); |
7155 | leency | 960 | } |
961 | |||
7200 | leency | 962 | void DrawPreview() |
963 | { |
||
964 | int x = right_bar.x; |
||
965 | int y = wrapper.y + wrapper.h - image.rows-2; |
||
966 | DrawRectangle(x, y, image.columns+1, image.rows+1, system.color.work_graph); |
||
967 | _PutImage(x+1,y+1, image.columns, image.rows, image.get_image()); |
||
968 | } |
||
7155 | leency | 969 | |
7200 | leency | 970 | dword GetPixelUnderMouse() |
971 | { |
||
972 | return GetPixelColorFromScreen(mouse.x + Form.left + 5, mouse.y + Form.top + skin_height); |
||
973 | } |
||
974 | |||
7089 | leency | 975 | //===================================================// |
976 | // // |
||
977 | // EVENTS // |
||
978 | // // |
||
979 | //===================================================// |
||
980 | |||
7151 | leency | 981 | void EventSave() |
7150 | leency | 982 | { |
7190 | leency | 983 | dword encoded_data=0; |
984 | dword encoded_size=0; |
||
985 | dword image_ptr = 0; |
||
986 | |||
987 | image_ptr = create_image(Image_bpp24, 32, 32); |
||
988 | |||
989 | if (image_ptr == 0) { |
||
990 | notify("'Error saving file, probably not enought memory!' -E"); |
||
7150 | leency | 991 | } |
7151 | leency | 992 | else { |
7190 | leency | 993 | EDI = image_ptr; |
994 | memmov(EDI._Image.Data, image.get_image(), image.rows * image.columns * 3); |
||
995 | |||
996 | encoded_data = encode_image(image_ptr, LIBIMG_FORMAT_PNG, 0, #encoded_size); |
||
997 | |||
998 | img_destroy stdcall(image_ptr); |
||
999 | |||
1000 | if(encoded_data == 0) { |
||
1001 | notify("'Error saving file, incorrect data!' -E"); |
||
1002 | } |
||
1003 | else { |
||
7227 | leency | 1004 | if (CreateFile(encoded_size, encoded_data, "/rd/1/saved_image.png") == 0) { |
7190 | leency | 1005 | notify("'File saved as /rd/1/saved_image.png' -O"); |
1006 | } |
||
1007 | else { |
||
1008 | notify("'Error saving file, probably not enought space on ramdisk!' -E"); |
||
1009 | } |
||
1010 | } |
||
7151 | leency | 1011 | } |
7150 | leency | 1012 | } |
7151 | leency | 1013 | |
7155 | leency | 1014 | void EventSetActiveColor(int _number, _color) |
7151 | leency | 1015 | { |
1016 | int i; |
||
7155 | leency | 1017 | for (i=13*2-1; i>0; i--) { |
1018 | last_used_colors[i] = last_used_colors[i-1]; |
||
1019 | } |
||
1020 | last_used_colors[0] = _color; |
||
7151 | leency | 1021 | |
7207 | leency | 1022 | if (_number == 1) color1 = _color; |
1023 | if (_number == 2) color2 = _color; |
||
7155 | leency | 1024 | |
1025 | DrawActiveColor(NULL); |
||
7200 | leency | 1026 | DrawColorPallets(); |
7156 | leency | 1027 | } |
1028 | |||
1029 | void EventFill(dword _r, _c, _color) |
||
1030 | { |
||
1031 | #define MARKED 6 |
||
1032 | int r, c, i, restart; |
||
1033 | |||
1034 | dword old_color = image.get_pixel(_r, _c); |
||
1035 | image.set_pixel(_r, _c, MARKED); |
||
1036 | |||
1037 | do { |
||
1038 | restart=false; |
||
1039 | for (r = 0; r < image.rows; r++) |
||
1040 | for (c = 0; c < image.columns; c++) |
||
1041 | { |
||
1042 | IF (image.get_pixel(r,c) != old_color) continue; |
||
1043 | IF (image.get_pixel(r,c) == MARKED) continue; |
||
1044 | |||
7194 | leency | 1045 | IF (c>0) && (image.get_pixel(r,c-1) == MARKED) image.set_pixel(r,c,MARKED); |
1046 | IF (r>0) && (image.get_pixel(r-1,c) == MARKED) image.set_pixel(r,c,MARKED); |
||
1047 | IF (c |
||
1048 | IF (r |
||
7156 | leency | 1049 | |
7194 | leency | 1050 | IF (image.get_pixel(r,c)==MARKED) restart=true; |
7156 | leency | 1051 | } |
1052 | }while(restart); |
||
1053 | |||
1054 | for (i=0; i |
||
1055 | IF (image.mas[i]==MARKED) image.mas[i] = _color; |
||
1056 | } |
||
1057 | |||
7186 | leency | 1058 | // target - image (1) or canvas (2) |
1059 | void DrawLine(int x1, int y1, int x2, int y2, dword color, int target) { |
||
1060 | int dx, dy, signX, signY, error, error2; |
||
1061 | |||
1062 | dx = x2 - x1; |
||
1063 | |||
1064 | if (dx < 0) |
||
1065 | dx = -dx; |
||
1066 | |||
1067 | dy = y2 - y1; |
||
1068 | |||
1069 | if (dy < 0) |
||
1070 | dy = -dy; |
||
1071 | |||
1072 | if (x1 < x2) |
||
1073 | signX = 1; |
||
1074 | else |
||
1075 | signX = -1; |
||
1076 | |||
1077 | if (y1 < y2) |
||
1078 | signY = 1; |
||
1079 | else |
||
1080 | signY = -1; |
||
1081 | |||
1082 | error = dx - dy; |
||
1083 | |||
1084 | if (target == 1) |
||
1085 | image.set_pixel(y2, x2, color); |
||
1086 | else |
||
1087 | DrawBar(x2*zoom.value + canvas.x, y2*zoom.value + canvas.y, |
||
1088 | zoom.value, zoom.value, color); |
||
1089 | |||
1090 | while((x1 != x2) || (y1 != y2)) |
||
1091 | { |
||
1092 | if (target == 1) |
||
1093 | image.set_pixel(y1, x1, color); |
||
1094 | else |
||
1095 | DrawBar(x1*zoom.value + canvas.x, y1*zoom.value + canvas.y, |
||
1096 | zoom.value, zoom.value, color); |
||
1097 | |||
1098 | error2 = error * 2; |
||
1099 | |||
1100 | if(error2 > calc(-dy)) |
||
1101 | { |
||
1102 | error -= dy; |
||
1103 | x1 += signX; |
||
1104 | } |
||
1105 | |||
1106 | if(error2 < dx) |
||
1107 | { |
||
1108 | error += dx; |
||
1109 | y1 += signY; |
||
1110 | } |
||
1111 | } |
||
1112 | |||
1113 | } |
||
1114 | |||
1115 | void DrawRectangleInCanvas(int x1, int y1, int x2, int y2, dword color, int target) { |
||
1116 | DrawLine(x1, y1, x2, y1, color, target); |
||
1117 | DrawLine(x2, y1, x2, y2, color, target); |
||
1118 | DrawLine(x2, y2, x1, y2, color, target); |
||
1119 | DrawLine(x1, y2, x1, y1, color, target); |
||
7253 | leency | 1120 | } |
1121 | |||
1122 | #define SELECTION_COLOR 0xAAE5EF |
||
1123 | |||
1124 | void DrawSelection(int x1, int y1, int x2, int y2) { |
||
1125 | int p1x, p1y, p2x, p2y, r, c, old_color, new_color; |
||
1126 | dword offset; |
||
1127 | |||
1128 | if (x1 <= x2) { |
||
1129 | p1x = x1; |
||
1130 | p2x = x2; |
||
1131 | } |
||
1132 | else { |
||
1133 | p1x = x2; |
||
1134 | p2x = x1; |
||
1135 | } |
||
1136 | |||
1137 | if (y1 <= y2) { |
||
1138 | p2y = y1; |
||
1139 | p1y = y2; |
||
1140 | } |
||
1141 | else { |
||
1142 | p2y = y2; |
||
1143 | p1y = y1; |
||
1144 | } |
||
1145 | |||
1146 | for (r = p1y; r >= p2y; r--) { |
||
1147 | for (c = p1x; c <= p2x; c++) { |
||
1148 | |||
1149 | if (selection_moving_started) && (SelectTool_pointInSelection(c, r)) { |
||
1150 | offset = calc(SelectionTool_buffer_c * calc(r - selection_start_y) + calc(c - selection_start_x)) * 4; |
||
1151 | old_color = ESDWORD[SelectionTool_buffer + offset]; |
||
1152 | } |
||
1153 | else { |
||
1154 | old_color = image.get_pixel(r, c); |
||
1155 | } |
||
1156 | |||
1157 | new_color = MixColors(old_color, SELECTION_COLOR, 64); |
||
1158 | |||
1159 | DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y, |
||
1160 | zoom.value, zoom.value, new_color); |
||
1161 | |||
1162 | } |
||
1163 | } |
||
7186 | leency | 1164 | }=>=>=>>>>>>>>>>>>>>=w;>>>=>=>>>>>=>=>=>=>=>=>>>>> |