Rev 7229 | Rev 7253 | 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" |
21 | |||
7151 | leency | 22 | #include "colors_mas.h" |
23 | |||
7089 | leency | 24 | //===================================================// |
25 | // // |
||
26 | // DATA // |
||
27 | // // |
||
28 | //===================================================// |
||
29 | |||
7207 | leency | 30 | #define T_TITLE "Icon Editor 0.39" |
7096 | leency | 31 | |
7152 | leency | 32 | #define TOOLBAR_H 24+8 |
7187 | leency | 33 | #define PANEL_LEFT_W 16+5+5+3+3 |
7096 | leency | 34 | #define PALLETE_SIZE 116 |
7187 | leency | 35 | #define TB_ICON_PADDING 26 |
7089 | leency | 36 | |
7194 | leency | 37 | #define PAL_ITEMS_X_COUNT 13 |
38 | #define COLSIZE 18 |
||
7200 | leency | 39 | #define RIGHT_BAR_W PAL_ITEMS_X_COUNT*COLSIZE |
7187 | leency | 40 | |
7200 | leency | 41 | block canvas = { NULL, NULL, NULL, NULL }; |
42 | block wrapper = { PANEL_LEFT_W, TOOLBAR_H, NULL, NULL }; |
||
43 | block right_bar = { NULL, TOOLBAR_H, RIGHT_BAR_W+10, NULL }; |
||
44 | |||
45 | block b_color_gradient = {NULL, 30+TOOLBAR_H, RIGHT_BAR_W, 30}; |
||
46 | block b_last_colors = {NULL, 70+TOOLBAR_H, RIGHT_BAR_W, COLSIZE*2}; |
||
47 | block b_default_palette = {NULL, COLSIZE*2+10+70+TOOLBAR_H, RIGHT_BAR_W, COLSIZE*9}; |
||
48 | |||
7207 | leency | 49 | dword color1 = 0x000000; |
50 | dword color2 = 0xFFFfff; |
||
51 | dword tool_color; |
||
7089 | leency | 52 | |
53 | enum { |
||
7096 | leency | 54 | BTN_NEW = 40, |
55 | BTN_OPEN, |
||
56 | BTN_SAVE, |
||
57 | BTN_MOVE_LEFT, |
||
58 | BTN_MOVE_RIGHT, |
||
59 | BTN_MOVE_UP, |
||
60 | BTN_MOVE_DOWN, |
||
61 | BTN_FLIP_HOR, |
||
62 | BTN_FLIP_VER, |
||
63 | BTN_ROTATE_LEFT, |
||
64 | BTN_ROTATE_RIGHT, |
||
7187 | leency | 65 | BTN_PENCIL, |
7096 | leency | 66 | BTN_PICK, |
7156 | leency | 67 | BTN_FILL, |
7186 | leency | 68 | BTN_LINE, |
69 | BTN_RECT, |
||
7155 | leency | 70 | BTNS_PALETTE_COLOR_MAS = 100, |
71 | BTNS_LAST_USED_COLORS = 400 |
||
7089 | leency | 72 | }; |
73 | |||
74 | proc_info Form; |
||
75 | |||
7243 | leency | 76 | more_less_box zoom = { 11, 1, 40, "Zoom" }; |
7150 | leency | 77 | |
7152 | leency | 78 | dword default_palette[] = { |
7190 | leency | 79 | 0x330000,0x331900,0x333300,0x193300,0x003300,0x003319,0x003333,0x001933,0x000033,0x190033, |
80 | 0x330033,0x330019,0x000000,0x660000,0x663300,0x666600,0x336600,0x006600,0x006633,0x006666, |
||
81 | 0x003366,0x000066,0x330066,0x660066,0x660033,0x202020,0x990000,0x994C00,0x999900,0x4C9900, |
||
82 | 0x009900,0x00994C,0x009999,0x004C99,0x000099,0x4C0099,0x990099,0x99004C,0x404040,0xCC0000, |
||
83 | 0xCC6600,0xCCCC00,0x66CC00,0x00CC00,0x00CC66,0x00CCCC,0x0066CC,0x0000CC,0x6600CC,0xCC00CC, |
||
84 | 0xCC0066,0x606060,0xFF0000,0xFF8000,0xFFFF00,0x80FF00,0x00FF00,0x00FF80,0x00FFFF,0x0080FF, |
||
85 | 0x0000FF,0x7F00FF,0xFF00FF,0xFF007F,0x808080,0xFF3333,0xFF9933,0xFFFF33,0x99FF33,0x33FF33, |
||
86 | 0x33FF99,0x33FFFF,0x3399FF,0x3333FF,0x9933FF,0xFF33FF,0xFF3399,0xA0A0A0,0xFF6666,0xFFB266, |
||
87 | 0xFFFF66,0xB2FF66,0x66FF66,0x66FFB2,0x66FFFF,0x66B2FF,0x6666FF,0xB266FF,0xFF66FF,0xFF66B2, |
||
88 | 0xC0C0C0,0xFF9999,0xFFCC99,0xFFFF99,0xCCFF99,0x99FF99,0x99FFCC,0x99FFFF,0x99CCFF,0x9999FF, |
||
89 | 0xCC99FF,0xFF99FF,0xFF99CC,0xE0E0E0,0xFFCCCC,0xFFE5CC,0xFFFFCC,0xE5FFCC,0xCCFFCC,0xCCFFE5, |
||
90 | 0xCCFFFF,0xCCE5FF,0xCCCCFF,0xE5CCFF,0xFFCCFF,0xFFCCE5,0xFFFFFF |
||
7089 | leency | 91 | }; |
7155 | leency | 92 | dword last_used_colors[13*2] = { |
7190 | leency | 93 | 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF, |
94 | 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF, |
||
95 | 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF |
||
7155 | leency | 96 | }; |
7089 | leency | 97 | |
7155 | leency | 98 | _image image; |
7148 | leency | 99 | |
7204 | leency | 100 | #include "actions_history.h" |
101 | |||
7151 | leency | 102 | libimg_image open_image; |
7204 | leency | 103 | _ActionsHistory actionsHistory; |
7151 | leency | 104 | |
7156 | leency | 105 | enum { |
7186 | leency | 106 | TOOL_NONE = -1, |
7187 | leency | 107 | TOOL_PENCIL, |
108 | TOOL_PIPETTE, |
||
109 | TOOL_FILL, |
||
110 | TOOL_LINE, |
||
111 | TOOL_RECT, |
||
7156 | leency | 112 | }; |
113 | |||
7186 | leency | 114 | struct Tool { |
115 | int id; |
||
116 | |||
117 | void (*activate)(); |
||
118 | void (*deactivate)(); |
||
119 | void (*onMouseEvent)(int x, int y, int lkm, int pkm); |
||
120 | void (*onCanvasDraw)(); |
||
121 | }; |
||
122 | |||
123 | Tool tools[5]; |
||
124 | int currentTool = -1; |
||
125 | |||
126 | void resetCurrentTool() { |
||
127 | if ((currentTool != TOOL_NONE) && (tools[currentTool].deactivate != 0)) { |
||
128 | tools[currentTool].deactivate(); |
||
129 | } |
||
130 | |||
131 | currentTool = TOOL_NONE; |
||
132 | } |
||
133 | |||
134 | void setCurrentTool(int index) { |
||
135 | resetCurrentTool(); |
||
7204 | leency | 136 | |
137 | currentTool = index; |
||
7186 | leency | 138 | |
139 | if ((index != TOOL_NONE) && (tools[index].activate != 0)) |
||
140 | tools[index].activate(); |
||
7187 | leency | 141 | |
142 | DrawLeftPanel(); |
||
7186 | leency | 143 | } |
144 | |||
7089 | leency | 145 | //===================================================// |
146 | // // |
||
147 | // CODE // |
||
148 | // // |
||
149 | //===================================================// |
||
150 | |||
7186 | leency | 151 | void FillTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { |
7207 | leency | 152 | if (canvas.hovered()) && (currentTool==TOOL_FILL) && (mouse.up) |
7186 | leency | 153 | { |
7207 | leency | 154 | EventFill(mouseY-canvas.y/zoom.value, |
155 | mouseX-canvas.x/zoom.value, tool_color); |
||
156 | actionsHistory.saveCurrentState(); |
||
7186 | leency | 157 | DrawCanvas(); |
158 | } |
||
159 | } |
||
160 | |||
161 | void PipetteTool_activate() { |
||
162 | SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE); |
||
163 | } |
||
164 | |||
165 | void PipetteTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { |
||
7207 | leency | 166 | tool_color = GetPixelUnderMouse(); |
167 | DrawBar(Form.cwidth-30, 5, 20, 20, tool_color); |
||
7186 | leency | 168 | |
7207 | leency | 169 | if (mouse.down) { |
7186 | leency | 170 | SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER); |
7207 | leency | 171 | if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, tool_color); |
172 | if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, tool_color); |
||
7186 | leency | 173 | |
174 | setCurrentTool(TOOL_PENCIL); |
||
175 | } |
||
176 | } |
||
177 | |||
7204 | leency | 178 | bool PencilTool_Drawing = false; |
179 | |||
7186 | leency | 180 | void PencilTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { |
7200 | leency | 181 | if (canvas.hovered()) |
7186 | leency | 182 | { |
7207 | leency | 183 | if ((PencilTool_Drawing == true) && (!mouse.key)) { |
7204 | leency | 184 | actionsHistory.saveCurrentState(); |
185 | PencilTool_Drawing = false; |
||
186 | } |
||
187 | |||
7207 | leency | 188 | if (mouse.key) { |
7186 | leency | 189 | image.set_pixel(mouseY-canvas.y/zoom.value, |
7207 | leency | 190 | mouseX-canvas.x/zoom.value, tool_color); |
7204 | leency | 191 | PencilTool_Drawing = true; |
7207 | leency | 192 | } |
7186 | leency | 193 | DrawCanvas(); |
194 | } |
||
195 | } |
||
196 | |||
7204 | leency | 197 | void PencilTool_reset() { |
198 | PencilTool_Drawing = false; |
||
199 | } |
||
200 | |||
7186 | leency | 201 | // Line tool |
7204 | leency | 202 | struct SimpleFigureTool_State { |
203 | int startX, startY; |
||
204 | int lastTempPosX, lastTempPosY; |
||
205 | }; |
||
7186 | leency | 206 | |
7204 | leency | 207 | enum { |
208 | TOOL_LINE_STATE, |
||
209 | TOOL_RECT_STATE |
||
210 | }; |
||
7186 | leency | 211 | |
7204 | leency | 212 | dword currentFigToolState = -1; |
213 | SimpleFigureTool_State figTool_States[2]; |
||
214 | |||
215 | void SimpleFigureTool_Reset() { |
||
216 | if (currentTool == TOOL_LINE) |
||
217 | currentFigToolState = TOOL_LINE_STATE; |
||
218 | else if (currentTool == TOOL_RECT) |
||
219 | currentFigToolState = TOOL_RECT_STATE; |
||
220 | |||
221 | figTool_States[currentFigToolState].startX = -1; |
||
222 | figTool_States[currentFigToolState].startY = -1; |
||
223 | figTool_States[currentFigToolState].lastTempPosX = -1; |
||
224 | figTool_States[currentFigToolState].lastTempPosY = -1; |
||
7186 | leency | 225 | } |
226 | |||
7206 | leency | 227 | int mouseX_last; |
228 | int mouseY_last; |
||
229 | bool first_click_in_canvas = false; |
||
230 | |||
7204 | leency | 231 | void SimpleFigureTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) { |
7206 | leency | 232 | if (mouse.down) && (canvas.hovered()) first_click_in_canvas = true; |
233 | if (first_click_in_canvas) |
||
7186 | leency | 234 | { |
7206 | leency | 235 | if (mouseX>canvas.x+canvas.w-zoom.value) mouseX = canvas.x+canvas.w-zoom.value; |
236 | if (mouseY>canvas.y+canvas.h-zoom.value) mouseY = canvas.y+canvas.h-zoom.value; |
||
237 | if (mouseX |
||
238 | if (mouseY |
||
239 | |||
7207 | leency | 240 | if (mouse.key) { |
7204 | leency | 241 | if ((figTool_States[currentFigToolState].startX < 0) || (figTool_States[currentFigToolState].startY < 0)) { |
242 | figTool_States[currentFigToolState].startX = mouseX; |
||
243 | figTool_States[currentFigToolState].startY = mouseY; |
||
7186 | leency | 244 | } |
245 | else { |
||
7204 | leency | 246 | if ((calc(mouseX - canvas.x/zoom.value) != figTool_States[currentFigToolState].lastTempPosX) |
247 | || (calc(mouseY - canvas.y/zoom.value) != figTool_States[currentFigToolState].lastTempPosY)) |
||
248 | { |
||
249 | DrawCanvas(); |
||
250 | } |
||
7186 | leency | 251 | } |
7206 | leency | 252 | mouseX_last = mouseX; |
253 | mouseY_last = mouseY; |
||
7186 | leency | 254 | } |
7206 | leency | 255 | if (mouse.up) { |
7204 | leency | 256 | if ((figTool_States[currentFigToolState].startX >= 0) && (figTool_States[currentFigToolState].startY >= 0)) { |
257 | // Draw line from start position to current position |
||
258 | if (currentTool == TOOL_LINE) { |
||
259 | DrawLine(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, |
||
260 | figTool_States[currentFigToolState].startY - canvas.y/zoom.value, |
||
261 | mouseX - canvas.x/zoom.value, |
||
262 | mouseY - canvas.y/zoom.value, |
||
7207 | leency | 263 | tool_color, |
7204 | leency | 264 | 1); |
265 | } |
||
266 | else if (currentTool == TOOL_RECT) { |
||
267 | DrawRectangleInCanvas(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, |
||
268 | figTool_States[currentFigToolState].startY - canvas.y/zoom.value, |
||
269 | mouseX - canvas.x/zoom.value, |
||
7207 | leency | 270 | mouseY - canvas.y/zoom.value, tool_color, 1); |
7204 | leency | 271 | } |
272 | |||
7186 | leency | 273 | DrawCanvas(); |
274 | |||
7204 | leency | 275 | actionsHistory.saveCurrentState(); |
7186 | leency | 276 | |
277 | // Reset start position |
||
7204 | leency | 278 | figTool_States[currentFigToolState].startX = -1; |
279 | figTool_States[currentFigToolState].startY = -1; |
||
7206 | leency | 280 | |
281 | first_click_in_canvas = false; |
||
7186 | leency | 282 | } |
283 | } |
||
284 | } |
||
285 | } |
||
286 | |||
7204 | leency | 287 | void SimpleFigureTool_onCanvasDraw() { |
7207 | leency | 288 | if ((figTool_States[currentFigToolState].startX >= 0) && (figTool_States[currentFigToolState].startY >= 0) && (mouse.key)) { |
7204 | leency | 289 | if (currentTool == TOOL_LINE) { |
290 | DrawLine(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, |
||
291 | figTool_States[currentFigToolState].startY - canvas.y/zoom.value, |
||
7206 | leency | 292 | mouseX_last - canvas.x/zoom.value, |
293 | mouseY_last - canvas.y/zoom.value, |
||
7207 | leency | 294 | tool_color, |
7204 | leency | 295 | 2); |
296 | } |
||
297 | else if (currentTool == TOOL_RECT) { |
||
298 | DrawRectangleInCanvas(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, |
||
299 | figTool_States[currentFigToolState].startY - canvas.y/zoom.value, |
||
7206 | leency | 300 | mouseX_last - canvas.x/zoom.value, |
301 | mouseY_last - canvas.y/zoom.value, |
||
7207 | leency | 302 | tool_color, |
7206 | leency | 303 | 2); |
7204 | leency | 304 | } |
305 | |||
7206 | leency | 306 | figTool_States[currentFigToolState].lastTempPosX = mouseX_last - canvas.x/zoom.value; |
307 | figTool_States[currentFigToolState].lastTempPosY = mouseY_last - canvas.y/zoom.value; |
||
7186 | leency | 308 | } |
309 | } |
||
310 | |||
311 | void initTools() |
||
312 | { |
||
7187 | leency | 313 | tools[0].id = TOOL_PENCIL; |
314 | tools[0].onMouseEvent = #PencilTool_onMouseEvent; |
||
7204 | leency | 315 | tools[0].deactivate = #PencilTool_reset; |
7186 | leency | 316 | |
317 | tools[1].id = TOOL_PIPETTE; |
||
318 | tools[1].activate = #PipetteTool_activate; |
||
319 | tools[1].onMouseEvent = #PipetteTool_onMouseEvent; |
||
320 | |||
7187 | leency | 321 | tools[2].id = TOOL_FILL; |
322 | tools[2].onMouseEvent = #FillTool_onMouseEvent; |
||
7186 | leency | 323 | |
7204 | leency | 324 | tools[3].id = TOOL_LINE; |
325 | tools[3].activate = #SimpleFigureTool_Reset; |
||
326 | tools[3].deactivate = #SimpleFigureTool_Reset; |
||
327 | tools[3].onMouseEvent = #SimpleFigureTool_onMouseEvent; |
||
328 | tools[3].onCanvasDraw = #SimpleFigureTool_onCanvasDraw; |
||
7186 | leency | 329 | |
330 | tools[4].id = TOOL_RECT; |
||
7204 | leency | 331 | tools[4].activate = #SimpleFigureTool_Reset; |
332 | tools[4].deactivate = #SimpleFigureTool_Reset; |
||
333 | tools[4].onMouseEvent = #SimpleFigureTool_onMouseEvent; |
||
334 | tools[4].onCanvasDraw = #SimpleFigureTool_onCanvasDraw; |
||
7186 | leency | 335 | } |
336 | |||
7089 | leency | 337 | void main() |
338 | { |
||
339 | word btn; |
||
340 | |||
7096 | leency | 341 | load_dll(libio, #libio_init, 1); |
342 | load_dll(libimg, #libimg_init, 1); |
||
343 | Libimg_LoadImage(#skin, "/sys/icons16.png"); |
||
7156 | leency | 344 | //system.color.get(); |
7187 | leency | 345 | //Libimg_ReplaceColor(tools_img.image, tools_img.w, tools_img.h, 0xFFF8C0D0, system.color.work); |
7186 | leency | 346 | |
7155 | leency | 347 | image.create(32, 32); |
7089 | leency | 348 | |
7156 | leency | 349 | if (param[0]) { |
7151 | leency | 350 | Libimg_LoadImage(#open_image, #param); |
351 | if (open_image.w==32) && (open_image.h==32) { |
||
7156 | leency | 352 | image.set_image(open_image.imgsrc); |
7151 | leency | 353 | } |
354 | else { |
||
7190 | leency | 355 | notify("'Error: image format is unacceptable (PNG, 32x32x16b expected)' -E"); |
7151 | leency | 356 | } |
357 | } |
||
358 | |||
7204 | leency | 359 | actionsHistory.init(); |
360 | |||
7186 | leency | 361 | initTools(); |
362 | setCurrentTool(TOOL_PENCIL); |
||
363 | |||
7096 | leency | 364 | SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER); |
365 | |||
7089 | leency | 366 | loop() switch(WaitEvent()) |
367 | { |
||
7096 | leency | 368 | case evMouse: |
369 | mouse.get(); |
||
7186 | leency | 370 | |
7207 | leency | 371 | if (mouse.lkm) tool_color = color1; |
372 | if (mouse.pkm) tool_color = color2; |
||
373 | if (mouse.mkm) break; |
||
374 | |||
7186 | leency | 375 | if (currentTool != TOOL_NONE) |
376 | tools[currentTool].onMouseEvent(mouse.x, mouse.y, mouse.lkm, mouse.pkm); |
||
377 | |||
7156 | leency | 378 | if (mouse.vert) { |
7243 | leency | 379 | if (mouse.vert==65535) zoom.inc(); |
380 | if (mouse.vert==1) zoom.dec(); |
||
7152 | leency | 381 | DrawEditArea(); |
382 | } |
||
7186 | leency | 383 | |
7200 | leency | 384 | if (mouse.down) { |
385 | if (b_color_gradient.hovered()) |
||
386 | || (b_last_colors.hovered()) |
||
387 | || (b_default_palette.hovered()) { |
||
388 | if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, GetPixelUnderMouse()); |
||
389 | if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, GetPixelUnderMouse()); |
||
390 | } |
||
391 | } |
||
392 | |||
7148 | leency | 393 | break; |
7096 | leency | 394 | |
7089 | leency | 395 | case evButton: |
7148 | leency | 396 | btn = GetButtonID(); |
7243 | leency | 397 | if (zoom.click(btn)) DrawEditArea(); |
7096 | leency | 398 | switch(btn) |
399 | { |
||
7151 | leency | 400 | case BTN_NEW: |
7155 | leency | 401 | image.create(32, 32); |
7151 | leency | 402 | DrawCanvas(); |
403 | break; |
||
404 | case BTN_OPEN: |
||
7190 | leency | 405 | RunProgram("/sys/lod", sprintf(#param, "*png* %s",#program_path)); |
7151 | leency | 406 | break; |
407 | case BTN_SAVE: |
||
408 | EventSave(); |
||
409 | break; |
||
7096 | leency | 410 | case BTN_MOVE_LEFT: |
7151 | leency | 411 | image.move(MOVE_LEFT); |
412 | DrawCanvas(); |
||
7096 | leency | 413 | break; |
414 | case BTN_MOVE_RIGHT: |
||
7151 | leency | 415 | image.move(MOVE_RIGHT); |
416 | DrawCanvas(); |
||
7096 | leency | 417 | break; |
7147 | leency | 418 | case BTN_MOVE_UP: |
7151 | leency | 419 | image.move(MOVE_UP); |
420 | DrawCanvas(); |
||
7147 | leency | 421 | break; |
422 | case BTN_MOVE_DOWN: |
||
7151 | leency | 423 | image.move(MOVE_DOWN); |
424 | DrawCanvas(); |
||
7147 | leency | 425 | break; |
7151 | leency | 426 | case BTN_FLIP_VER: |
427 | image.move(FLIP_VER); |
||
428 | DrawCanvas(); |
||
429 | break; |
||
430 | case BTN_FLIP_HOR: |
||
431 | image.move(FLIP_HOR); |
||
432 | DrawCanvas(); |
||
433 | break; |
||
7187 | leency | 434 | case BTN_PENCIL: |
435 | setCurrentTool(TOOL_PENCIL); |
||
436 | break; |
||
7096 | leency | 437 | case BTN_PICK: |
7186 | leency | 438 | setCurrentTool(TOOL_PIPETTE); |
439 | //EventPickActivate(); |
||
7096 | leency | 440 | break; |
7156 | leency | 441 | case BTN_FILL: |
7186 | leency | 442 | setCurrentTool(TOOL_FILL); |
443 | //EventFillActivate(); |
||
7156 | leency | 444 | break; |
7186 | leency | 445 | case BTN_LINE: |
446 | setCurrentTool(TOOL_LINE); |
||
447 | break; |
||
448 | case BTN_RECT: |
||
449 | setCurrentTool(TOOL_RECT); |
||
450 | break; |
||
7152 | leency | 451 | case CLOSE_BTN: |
452 | ExitProcess(); |
||
453 | break; |
||
7089 | leency | 454 | } |
455 | break; |
||
456 | |||
457 | case evKey: |
||
458 | GetKeys(); |
||
7186 | leency | 459 | if (key_scancode == SCAN_CODE_ESC) setCurrentTool(TOOL_PENCIL); |
7187 | leency | 460 | if (key_scancode == SCAN_CODE_KEY_P) setCurrentTool(TOOL_PENCIL); |
7186 | leency | 461 | if (key_scancode == SCAN_CODE_KEY_I) setCurrentTool(TOOL_PIPETTE); |
7187 | leency | 462 | if (key_scancode == SCAN_CODE_KEY_F) setCurrentTool(TOOL_FILL); |
463 | if (key_scancode == SCAN_CODE_KEY_L) setCurrentTool(TOOL_LINE); |
||
464 | if (key_scancode == SCAN_CODE_KEY_R) setCurrentTool(TOOL_RECT); |
||
7204 | leency | 465 | |
466 | if (key_scancode == SCAN_CODE_KEY_S) actionsHistory.undoLastAction(); |
||
467 | if (key_scancode == SCAN_CODE_KEY_C) actionsHistory.redoLastAction(); |
||
468 | |||
7243 | leency | 469 | if (key_scancode == SCAN_CODE_MINUS) {zoom.inc(); DrawEditArea();} |
470 | if (key_scancode == SCAN_CODE_PLUS) {zoom.dec(); DrawEditArea();} |
||
7089 | leency | 471 | break; |
472 | |||
473 | case evReDraw: |
||
474 | draw_window(); |
||
475 | break; |
||
476 | } |
||
477 | } |
||
478 | |||
7096 | leency | 479 | void DrawToolbarButton(dword _id, _x, _icon_n) |
480 | { |
||
7152 | leency | 481 | DrawWideRectangle(_x, 4, 22, 22, 3, 0xFFFfff); |
482 | DefineHiddenButton(_x, 4, 21, 21, _id); |
||
483 | img_draw stdcall(skin.image, _x+3, 7, 16, 16, 0, _icon_n*16); |
||
7096 | leency | 484 | } |
485 | |||
7187 | leency | 486 | void DrawLeftPanelButton(dword _id, _y, _icon_n) |
487 | { |
||
488 | int x = 5; |
||
489 | DrawWideRectangle(x, _y, 22, 22, 3, 0xFFFfff); |
||
490 | DefineHiddenButton(x, _y, 21, 21, _id); |
||
491 | img_draw stdcall(skin.image, x+3, _y+3, 16, 16, 0, _icon_n*16); |
||
492 | } |
||
493 | |||
7148 | leency | 494 | void DrawStatusBar() |
495 | { |
||
7243 | leency | 496 | zoom.draw(wrapper.x, wrapper.y + wrapper.h + 6); |
7150 | leency | 497 | |
7151 | leency | 498 | sprintf(#param,"Canvas: %ix%i", image.rows, image.columns); |
7150 | leency | 499 | WriteText(wrapper.x+wrapper.w-calc(strlen(#param)*8), zoom.y+2, 0x90, system.color.work_text, #param); |
7148 | leency | 500 | } |
501 | |||
7089 | leency | 502 | void draw_window() |
503 | { |
||
7096 | leency | 504 | incn tx; |
7089 | leency | 505 | system.color.get(); |
7151 | leency | 506 | DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x33, system.color.work, T_TITLE, 0); |
7089 | leency | 507 | GetProcessInfo(#Form, SelfInfo); |
7152 | leency | 508 | if (Form.status_window>2) return; |
509 | if (Form.width < 560) { MoveSize(OLD,OLD,560,OLD); return; } |
||
510 | if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; } |
||
511 | |||
7150 | leency | 512 | right_bar.x = Form.cwidth - right_bar.w; |
7200 | leency | 513 | b_color_gradient.x = b_last_colors.x = b_default_palette.x = right_bar.x; |
7089 | leency | 514 | |
7155 | leency | 515 | tx.n = 10-TB_ICON_PADDING; |
7148 | leency | 516 | DrawToolbarButton(BTN_NEW, tx.inc(TB_ICON_PADDING), 2); //not implemented |
517 | DrawToolbarButton(BTN_OPEN, tx.inc(TB_ICON_PADDING), 0); //not implemented |
||
7151 | leency | 518 | DrawToolbarButton(BTN_SAVE, tx.inc(TB_ICON_PADDING), 5); |
7207 | leency | 519 | DrawToolbarButton(BTN_MOVE_LEFT, tx.inc(TB_ICON_PADDING+8), 30); |
7096 | leency | 520 | DrawToolbarButton(BTN_MOVE_RIGHT, tx.inc(TB_ICON_PADDING), 31); |
7147 | leency | 521 | DrawToolbarButton(BTN_MOVE_UP, tx.inc(TB_ICON_PADDING), 32); |
522 | DrawToolbarButton(BTN_MOVE_DOWN, tx.inc(TB_ICON_PADDING), 33); |
||
523 | |||
7155 | leency | 524 | DrawToolbarButton(BTN_FLIP_HOR, tx.inc(TB_ICON_PADDING+8), 34); |
525 | DrawToolbarButton(BTN_FLIP_VER, tx.inc(TB_ICON_PADDING), 35); |
||
526 | // DrawToolbarButton(BTN_ROTATE_LEFT, tx.inc(TB_ICON_PADDING), 36); //not implemented |
||
527 | // DrawToolbarButton(BTN_ROTATE_RIGHT, tx.inc(TB_ICON_PADDING), 37); //not implemented |
||
7148 | leency | 528 | |
7187 | leency | 529 | DrawLeftPanel(); |
7186 | leency | 530 | |
7150 | leency | 531 | DrawEditArea(); |
7096 | leency | 532 | |
7155 | leency | 533 | DrawActiveColor(right_bar.y); |
7200 | leency | 534 | DrawColorPallets(); |
7089 | leency | 535 | |
7148 | leency | 536 | DrawStatusBar(); |
7089 | leency | 537 | } |
538 | |||
7187 | leency | 539 | void DrawLeftPanel() |
540 | { |
||
541 | incn ty; |
||
542 | ty.n = TOOLBAR_H-TB_ICON_PADDING; |
||
543 | DrawLeftPanelButton(BTN_PENCIL, ty.inc(TB_ICON_PADDING), 38); |
||
544 | DrawLeftPanelButton(BTN_PICK, ty.inc(TB_ICON_PADDING), 39); |
||
545 | DrawLeftPanelButton(BTN_FILL, ty.inc(TB_ICON_PADDING), 40); |
||
546 | DrawLeftPanelButton(BTN_LINE, ty.inc(TB_ICON_PADDING), 41); |
||
547 | DrawLeftPanelButton(BTN_RECT, ty.inc(TB_ICON_PADDING), 42); |
||
548 | DrawRectangle3D(5, currentTool*TB_ICON_PADDING+TOOLBAR_H, 16+3+2, 16+3+2, 0x333333, 0x777777); |
||
549 | } |
||
550 | |||
7151 | leency | 551 | void DrawEditArea() |
7089 | leency | 552 | { |
7150 | leency | 553 | dword color1=0xC0C0C0; |
7155 | leency | 554 | int top_side; |
555 | int left_side; |
||
7089 | leency | 556 | |
7194 | leency | 557 | wrapper.w = Form.cwidth - right_bar.w - 10 - wrapper.x; |
7150 | leency | 558 | wrapper.h = Form.cheight - TOOLBAR_H - 35; |
7148 | leency | 559 | |
7150 | leency | 560 | //canvas{ |
7151 | leency | 561 | canvas.w = image.columns * zoom.value; |
562 | canvas.h = image.rows * zoom.value; |
||
563 | if (canvas.w+2 > wrapper.w) || (canvas.h+2 > wrapper.h) { |
||
7243 | leency | 564 | zoom.dec(); |
7151 | leency | 565 | DrawEditArea(); |
566 | return; |
||
567 | } |
||
568 | canvas.x = -zoom.value*image.columns+wrapper.w/2 + wrapper.x; |
||
569 | canvas.y = -zoom.value*image.rows+wrapper.h/2 + wrapper.y; |
||
570 | DrawCanvas(); |
||
7150 | leency | 571 | //} |
7089 | leency | 572 | |
7155 | leency | 573 | left_side = canvas.x-wrapper.x-1; |
574 | top_side = canvas.y-wrapper.y-1; |
||
7148 | leency | 575 | |
7155 | leency | 576 | DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w, wrapper.h, system.color.work_graph); |
7150 | leency | 577 | |
7155 | leency | 578 | if (left_side>0) |
7150 | leency | 579 | { |
7155 | leency | 580 | DrawBar(wrapper.x, wrapper.y, wrapper.w-1, top_side, color1); //top |
581 | DrawBar(wrapper.x, wrapper.y+wrapper.h-top_side-1, wrapper.w-1, top_side, color1); //bottom |
||
7150 | leency | 582 | } |
7155 | leency | 583 | if (top_side>0) |
7150 | leency | 584 | { |
7190 | leency | 585 | //left |
586 | DrawBar(wrapper.x, wrapper.y+top_side, left_side, |
||
587 | wrapper.h-top_side-top_side, color1); |
||
588 | //right |
||
589 | DrawBar(wrapper.x+wrapper.w-left_side-1, wrapper.y+top_side, left_side, |
||
590 | wrapper.h-top_side-top_side, color1); |
||
7150 | leency | 591 | } |
7194 | leency | 592 | DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080); |
7089 | leency | 593 | } |
594 | |||
595 | void DrawActiveColor(dword iny) |
||
596 | { |
||
597 | static dword outy; |
||
598 | if (iny != NULL) outy = iny; |
||
7207 | leency | 599 | DrawBar(right_bar.x, outy, 20, 20, color1); |
600 | sprintf(#param, "%A", color1); |
||
7089 | leency | 601 | EDI = system.color.work; |
7148 | leency | 602 | WriteText(right_bar.x + 30, outy + 3, 0xD0, system.color.work_text, #param+4); |
7151 | leency | 603 | |
7207 | leency | 604 | DrawBar(right_bar.x+110, outy, 20, 20, color2); |
605 | sprintf(#param, "%A", color2); |
||
7151 | leency | 606 | EDI = system.color.work; |
607 | WriteText(right_bar.x+110 + 30, outy + 3, 0xD0, system.color.work_text, #param+4); |
||
7200 | leency | 608 | DrawCurrentColorGradientByLightness(); |
7089 | leency | 609 | } |
610 | |||
7200 | leency | 611 | void DrawCurrentColorGradientByLightness() |
7194 | leency | 612 | { |
613 | int i; |
||
614 | int w = right_bar.w-10/2; |
||
615 | for (i=0; i |
||
7200 | leency | 616 | DrawBar(b_color_gradient.x+i, b_color_gradient.y, |
7207 | leency | 617 | 1, b_color_gradient.h, MixColors(color1,0xFFFfff,255*i/w)); |
7194 | leency | 618 | for (i=0 ; i<=w; i++) |
7200 | leency | 619 | DrawBar(b_color_gradient.x+w+w-i, b_color_gradient.y, |
7207 | leency | 620 | 1, b_color_gradient.h, MixColors(color1,0x000000,255*i/w)); |
7194 | leency | 621 | } |
622 | |||
7200 | leency | 623 | void DrawColorPallets() |
7089 | leency | 624 | { |
7155 | leency | 625 | int r, c, i=0; |
626 | //Last used colors |
||
627 | for (r = 0; r < 2; r++) |
||
628 | { |
||
7194 | leency | 629 | for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++) |
7155 | leency | 630 | { |
7200 | leency | 631 | DrawBar(c*COLSIZE + b_last_colors.x, r*COLSIZE + b_last_colors.y, |
632 | COLSIZE, COLSIZE, last_used_colors[i]); |
||
7155 | leency | 633 | } |
634 | } |
||
635 | i=0; |
||
636 | //Default colors |
||
7089 | leency | 637 | for (r = 0; r < 9; r++) |
638 | { |
||
7194 | leency | 639 | for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++) |
7089 | leency | 640 | { |
7200 | leency | 641 | DrawBar(c*COLSIZE + b_default_palette.x, r*COLSIZE + b_default_palette.y, |
642 | COLSIZE, COLSIZE, default_palette[PALLETE_SIZE-i]); |
||
7089 | leency | 643 | } |
644 | } |
||
645 | } |
||
646 | |||
7155 | leency | 647 | void DrawCanvas() |
648 | { |
||
649 | int r, c; |
||
650 | for (r = 0; r < image.rows; r++) |
||
651 | { |
||
652 | for (c = 0; c < image.columns; c++) |
||
653 | { |
||
654 | DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y, |
||
655 | zoom.value, zoom.value, image.get_pixel(r, c)); |
||
656 | } |
||
657 | } |
||
7186 | leency | 658 | |
659 | if ((currentTool != TOOL_NONE) && (tools[currentTool].onCanvasDraw != 0)) |
||
660 | tools[currentTool].onCanvasDraw(); |
||
661 | |||
7200 | leency | 662 | DrawPreview(); |
7155 | leency | 663 | } |
664 | |||
7200 | leency | 665 | void DrawPreview() |
666 | { |
||
667 | int x = right_bar.x; |
||
668 | int y = wrapper.y + wrapper.h - image.rows-2; |
||
669 | DrawRectangle(x, y, image.columns+1, image.rows+1, system.color.work_graph); |
||
670 | _PutImage(x+1,y+1, image.columns, image.rows, image.get_image()); |
||
671 | } |
||
7155 | leency | 672 | |
7200 | leency | 673 | dword GetPixelUnderMouse() |
674 | { |
||
675 | return GetPixelColorFromScreen(mouse.x + Form.left + 5, mouse.y + Form.top + skin_height); |
||
676 | } |
||
677 | |||
7089 | leency | 678 | //===================================================// |
679 | // // |
||
680 | // EVENTS // |
||
681 | // // |
||
682 | //===================================================// |
||
683 | |||
7151 | leency | 684 | void EventSave() |
7150 | leency | 685 | { |
7190 | leency | 686 | dword encoded_data=0; |
687 | dword encoded_size=0; |
||
688 | dword image_ptr = 0; |
||
689 | |||
690 | image_ptr = create_image(Image_bpp24, 32, 32); |
||
691 | |||
692 | if (image_ptr == 0) { |
||
693 | notify("'Error saving file, probably not enought memory!' -E"); |
||
7150 | leency | 694 | } |
7151 | leency | 695 | else { |
7190 | leency | 696 | EDI = image_ptr; |
697 | memmov(EDI._Image.Data, image.get_image(), image.rows * image.columns * 3); |
||
698 | |||
699 | encoded_data = encode_image(image_ptr, LIBIMG_FORMAT_PNG, 0, #encoded_size); |
||
700 | |||
701 | img_destroy stdcall(image_ptr); |
||
702 | |||
703 | if(encoded_data == 0) { |
||
704 | notify("'Error saving file, incorrect data!' -E"); |
||
705 | } |
||
706 | else { |
||
7227 | leency | 707 | if (CreateFile(encoded_size, encoded_data, "/rd/1/saved_image.png") == 0) { |
7190 | leency | 708 | notify("'File saved as /rd/1/saved_image.png' -O"); |
709 | } |
||
710 | else { |
||
711 | notify("'Error saving file, probably not enought space on ramdisk!' -E"); |
||
712 | } |
||
713 | } |
||
7151 | leency | 714 | } |
7150 | leency | 715 | } |
7151 | leency | 716 | |
7155 | leency | 717 | void EventSetActiveColor(int _number, _color) |
7151 | leency | 718 | { |
719 | int i; |
||
7155 | leency | 720 | for (i=13*2-1; i>0; i--) { |
721 | last_used_colors[i] = last_used_colors[i-1]; |
||
722 | } |
||
723 | last_used_colors[0] = _color; |
||
7151 | leency | 724 | |
7207 | leency | 725 | if (_number == 1) color1 = _color; |
726 | if (_number == 2) color2 = _color; |
||
7155 | leency | 727 | |
728 | DrawActiveColor(NULL); |
||
7200 | leency | 729 | DrawColorPallets(); |
7156 | leency | 730 | } |
731 | |||
732 | void EventFill(dword _r, _c, _color) |
||
733 | { |
||
734 | #define MARKED 6 |
||
735 | int r, c, i, restart; |
||
736 | |||
737 | dword old_color = image.get_pixel(_r, _c); |
||
738 | image.set_pixel(_r, _c, MARKED); |
||
739 | |||
740 | do { |
||
741 | restart=false; |
||
742 | for (r = 0; r < image.rows; r++) |
||
743 | for (c = 0; c < image.columns; c++) |
||
744 | { |
||
745 | IF (image.get_pixel(r,c) != old_color) continue; |
||
746 | IF (image.get_pixel(r,c) == MARKED) continue; |
||
747 | |||
7194 | leency | 748 | IF (c>0) && (image.get_pixel(r,c-1) == MARKED) image.set_pixel(r,c,MARKED); |
749 | IF (r>0) && (image.get_pixel(r-1,c) == MARKED) image.set_pixel(r,c,MARKED); |
||
750 | IF (c |
||
751 | IF (r |
||
7156 | leency | 752 | |
7194 | leency | 753 | IF (image.get_pixel(r,c)==MARKED) restart=true; |
7156 | leency | 754 | } |
755 | }while(restart); |
||
756 | |||
757 | for (i=0; i |
||
758 | IF (image.mas[i]==MARKED) image.mas[i] = _color; |
||
759 | } |
||
760 | |||
7186 | leency | 761 | // target - image (1) or canvas (2) |
762 | void DrawLine(int x1, int y1, int x2, int y2, dword color, int target) { |
||
763 | int dx, dy, signX, signY, error, error2; |
||
764 | |||
765 | dx = x2 - x1; |
||
766 | |||
767 | if (dx < 0) |
||
768 | dx = -dx; |
||
769 | |||
770 | dy = y2 - y1; |
||
771 | |||
772 | if (dy < 0) |
||
773 | dy = -dy; |
||
774 | |||
775 | if (x1 < x2) |
||
776 | signX = 1; |
||
777 | else |
||
778 | signX = -1; |
||
779 | |||
780 | if (y1 < y2) |
||
781 | signY = 1; |
||
782 | else |
||
783 | signY = -1; |
||
784 | |||
785 | error = dx - dy; |
||
786 | |||
787 | if (target == 1) |
||
788 | image.set_pixel(y2, x2, color); |
||
789 | else |
||
790 | DrawBar(x2*zoom.value + canvas.x, y2*zoom.value + canvas.y, |
||
791 | zoom.value, zoom.value, color); |
||
792 | |||
793 | while((x1 != x2) || (y1 != y2)) |
||
794 | { |
||
795 | if (target == 1) |
||
796 | image.set_pixel(y1, x1, color); |
||
797 | else |
||
798 | DrawBar(x1*zoom.value + canvas.x, y1*zoom.value + canvas.y, |
||
799 | zoom.value, zoom.value, color); |
||
800 | |||
801 | error2 = error * 2; |
||
802 | |||
803 | if(error2 > calc(-dy)) |
||
804 | { |
||
805 | error -= dy; |
||
806 | x1 += signX; |
||
807 | } |
||
808 | |||
809 | if(error2 < dx) |
||
810 | { |
||
811 | error += dx; |
||
812 | y1 += signY; |
||
813 | } |
||
814 | } |
||
815 | |||
816 | } |
||
817 | |||
818 | void DrawRectangleInCanvas(int x1, int y1, int x2, int y2, dword color, int target) { |
||
819 | DrawLine(x1, y1, x2, y1, color, target); |
||
820 | DrawLine(x2, y1, x2, y2, color, target); |
||
821 | DrawLine(x2, y2, x1, y2, color, target); |
||
822 | DrawLine(x1, y2, x1, y1, color, target); |
||
823 | }>>>>>>>>>>>>>=w;>>>>> |