/* * Icon Editor for KolibriOS * Authors: Leency, Nicolas * Licence: GPL v2 */ /* TODO: window colors enhance icon pipet aside color view */ #define MEMSIZE 4096*40 #include "../lib/gui.h" #include "../lib/random.h" #include "../lib/mem.h" #include "../lib/obj/libimg.h" #include "../lib/patterns/rgb.h" #include "../lib/patterns/libimg_load_skin.h" #include "colors_mas.h" //===================================================// // // // DATA // // // //===================================================// #define T_TITLE "Icon Editor 0.37" #define TOOLBAR_H 24+8 #define PANEL_LEFT_W 16+5+5+3+3 #define PALLETE_SIZE 116 #define TB_ICON_PADDING 26 #define PAL_ITEMS_X_COUNT 13 #define COLSIZE 18 #define RIGHT_BAR_W PAL_ITEMS_X_COUNT*COLSIZE struct block { int x,y,w,h; bool hovered(); }; bool block::hovered() { if ((mouse.x>x) && (mouse.y>y) && (mouse.y= 0) && (figTool_States[currentFigToolState].startY >= 0)) { // Draw line from start position to current position if (currentTool == TOOL_LINE) { DrawLine(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, figTool_States[currentFigToolState].startY - canvas.y/zoom.value, mouseX - canvas.x/zoom.value, mouseY - canvas.y/zoom.value, active_color_1, 1); } else if (currentTool == TOOL_RECT) { DrawRectangleInCanvas(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, figTool_States[currentFigToolState].startY - canvas.y/zoom.value, mouseX - canvas.x/zoom.value, mouseY - canvas.y/zoom.value, active_color_1, 1); } DrawCanvas(); actionsHistory.saveCurrentState(); // Reset start position figTool_States[currentFigToolState].startX = -1; figTool_States[currentFigToolState].startY = -1; } } } } void SimpleFigureTool_onCanvasDraw() { if ((figTool_States[currentFigToolState].startX >= 0) && (figTool_States[currentFigToolState].startY >= 0) && (mouse.lkm)) { if (currentTool == TOOL_LINE) { DrawLine(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, figTool_States[currentFigToolState].startY - canvas.y/zoom.value, mouse.x - canvas.x/zoom.value, mouse.y - canvas.y/zoom.value, active_color_1, 2); } else if (currentTool == TOOL_RECT) { DrawRectangleInCanvas(figTool_States[currentFigToolState].startX - canvas.x/zoom.value, figTool_States[currentFigToolState].startY - canvas.y/zoom.value, mouse.x - canvas.x/zoom.value, mouse.y - canvas.y/zoom.value, active_color_1, 2); } figTool_States[currentFigToolState].lastTempPosX = mouse.x - canvas.x/zoom.value; figTool_States[currentFigToolState].lastTempPosY = mouse.y - canvas.y/zoom.value; } } void initTools() { tools[0].id = TOOL_PENCIL; tools[0].onMouseEvent = #PencilTool_onMouseEvent; tools[0].deactivate = #PencilTool_reset; tools[1].id = TOOL_PIPETTE; tools[1].activate = #PipetteTool_activate; tools[1].onMouseEvent = #PipetteTool_onMouseEvent; tools[2].id = TOOL_FILL; tools[2].onMouseEvent = #FillTool_onMouseEvent; tools[3].id = TOOL_LINE; tools[3].activate = #SimpleFigureTool_Reset; tools[3].deactivate = #SimpleFigureTool_Reset; tools[3].onMouseEvent = #SimpleFigureTool_onMouseEvent; tools[3].onCanvasDraw = #SimpleFigureTool_onCanvasDraw; tools[4].id = TOOL_RECT; tools[4].activate = #SimpleFigureTool_Reset; tools[4].deactivate = #SimpleFigureTool_Reset; tools[4].onMouseEvent = #SimpleFigureTool_onMouseEvent; tools[4].onCanvasDraw = #SimpleFigureTool_onCanvasDraw; } void main() { word btn; load_dll(libio, #libio_init, 1); load_dll(libimg, #libimg_init, 1); Libimg_LoadImage(#skin, "/sys/icons16.png"); //system.color.get(); //Libimg_ReplaceColor(tools_img.image, tools_img.w, tools_img.h, 0xFFF8C0D0, system.color.work); image.create(32, 32); if (param[0]) { Libimg_LoadImage(#open_image, #param); if (open_image.w==32) && (open_image.h==32) { image.set_image(open_image.imgsrc); } else { notify("'Error: image format is unacceptable (PNG, 32x32x16b expected)' -E"); } } actionsHistory.init(); initTools(); setCurrentTool(TOOL_PENCIL); SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER); loop() switch(WaitEvent()) { case evMouse: mouse.get(); if (currentTool != TOOL_NONE) tools[currentTool].onMouseEvent(mouse.x, mouse.y, mouse.lkm, mouse.pkm); if (mouse.vert) { if (mouse.vert==65535) zoom.click(BTN_ZOOM_IN); if (mouse.vert==1) zoom.click(BTN_ZOOM_OUT); DrawEditArea(); } if (mouse.down) { if (b_color_gradient.hovered()) || (b_last_colors.hovered()) || (b_default_palette.hovered()) { if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, GetPixelUnderMouse()); if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, GetPixelUnderMouse()); } } break; case evButton: btn = GetButtonID(); switch(btn) { case BTN_NEW: image.create(32, 32); DrawCanvas(); break; case BTN_OPEN: RunProgram("/sys/lod", sprintf(#param, "*png* %s",#program_path)); break; case BTN_SAVE: EventSave(); break; case BTN_MOVE_LEFT: image.move(MOVE_LEFT); DrawCanvas(); break; case BTN_MOVE_RIGHT: image.move(MOVE_RIGHT); DrawCanvas(); break; case BTN_MOVE_UP: image.move(MOVE_UP); DrawCanvas(); break; case BTN_MOVE_DOWN: image.move(MOVE_DOWN); DrawCanvas(); break; case BTN_FLIP_VER: image.move(FLIP_VER); DrawCanvas(); break; case BTN_FLIP_HOR: image.move(FLIP_HOR); DrawCanvas(); break; case BTN_PENCIL: setCurrentTool(TOOL_PENCIL); break; case BTN_PICK: setCurrentTool(TOOL_PIPETTE); //EventPickActivate(); break; case BTN_FILL: setCurrentTool(TOOL_FILL); //EventFillActivate(); break; case BTN_LINE: setCurrentTool(TOOL_LINE); break; case BTN_RECT: setCurrentTool(TOOL_RECT); break; case BTN_ZOOM_IN: zoom.click(BTN_ZOOM_IN); DrawEditArea(); break; case BTN_ZOOM_OUT: zoom.click(BTN_ZOOM_OUT); DrawEditArea(); break; case CLOSE_BTN: ExitProcess(); break; } break; case evKey: GetKeys(); if (key_scancode == SCAN_CODE_ESC) setCurrentTool(TOOL_PENCIL); if (key_scancode == SCAN_CODE_KEY_P) setCurrentTool(TOOL_PENCIL); if (key_scancode == SCAN_CODE_KEY_I) setCurrentTool(TOOL_PIPETTE); if (key_scancode == SCAN_CODE_KEY_F) setCurrentTool(TOOL_FILL); if (key_scancode == SCAN_CODE_KEY_L) setCurrentTool(TOOL_LINE); if (key_scancode == SCAN_CODE_KEY_R) setCurrentTool(TOOL_RECT); if (key_scancode == SCAN_CODE_KEY_S) actionsHistory.undoLastAction(); if (key_scancode == SCAN_CODE_KEY_C) actionsHistory.redoLastAction(); if (key_scancode == SCAN_CODE_MINUS) {zoom.click(BTN_ZOOM_OUT); DrawEditArea();} if (key_scancode == SCAN_CODE_PLUS) {zoom.click(BTN_ZOOM_IN); DrawEditArea();} break; case evReDraw: draw_window(); break; } } void DrawToolbarButton(dword _id, _x, _icon_n) { DrawWideRectangle(_x, 4, 22, 22, 3, 0xFFFfff); DefineHiddenButton(_x, 4, 21, 21, _id); img_draw stdcall(skin.image, _x+3, 7, 16, 16, 0, _icon_n*16); } void DrawLeftPanelButton(dword _id, _y, _icon_n) { int x = 5; DrawWideRectangle(x, _y, 22, 22, 3, 0xFFFfff); DefineHiddenButton(x, _y, 21, 21, _id); img_draw stdcall(skin.image, x+3, _y+3, 16, 16, 0, _icon_n*16); } void DrawStatusBar() { zoom.y = wrapper.y + wrapper.h + 6; zoom.x = wrapper.x; zoom.draw(); sprintf(#param,"Canvas: %ix%i", image.rows, image.columns); WriteText(wrapper.x+wrapper.w-calc(strlen(#param)*8), zoom.y+2, 0x90, system.color.work_text, #param); } void draw_window() { incn tx; system.color.get(); DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x33, system.color.work, T_TITLE, 0); GetProcessInfo(#Form, SelfInfo); if (Form.status_window>2) return; if (Form.width < 560) { MoveSize(OLD,OLD,560,OLD); return; } if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; } right_bar.x = Form.cwidth - right_bar.w; b_color_gradient.x = b_last_colors.x = b_default_palette.x = right_bar.x; tx.n = 10-TB_ICON_PADDING; DrawToolbarButton(BTN_NEW, tx.inc(TB_ICON_PADDING), 2); //not implemented DrawToolbarButton(BTN_OPEN, tx.inc(TB_ICON_PADDING), 0); //not implemented DrawToolbarButton(BTN_SAVE, tx.inc(TB_ICON_PADDING), 5); DrawToolbarButton(BTN_MOVE_LEFT, tx.inc(TB_ICON_PADDING+8), 30); DrawToolbarButton(BTN_MOVE_RIGHT, tx.inc(TB_ICON_PADDING), 31); DrawToolbarButton(BTN_MOVE_UP, tx.inc(TB_ICON_PADDING), 32); DrawToolbarButton(BTN_MOVE_DOWN, tx.inc(TB_ICON_PADDING), 33); DrawToolbarButton(BTN_FLIP_HOR, tx.inc(TB_ICON_PADDING+8), 34); DrawToolbarButton(BTN_FLIP_VER, tx.inc(TB_ICON_PADDING), 35); // DrawToolbarButton(BTN_ROTATE_LEFT, tx.inc(TB_ICON_PADDING), 36); //not implemented // DrawToolbarButton(BTN_ROTATE_RIGHT, tx.inc(TB_ICON_PADDING), 37); //not implemented DrawLeftPanel(); DrawEditArea(); DrawActiveColor(right_bar.y); DrawColorPallets(); DrawStatusBar(); } void DrawLeftPanel() { incn ty; ty.n = TOOLBAR_H-TB_ICON_PADDING; DrawLeftPanelButton(BTN_PENCIL, ty.inc(TB_ICON_PADDING), 38); DrawLeftPanelButton(BTN_PICK, ty.inc(TB_ICON_PADDING), 39); DrawLeftPanelButton(BTN_FILL, ty.inc(TB_ICON_PADDING), 40); DrawLeftPanelButton(BTN_LINE, ty.inc(TB_ICON_PADDING), 41); DrawLeftPanelButton(BTN_RECT, ty.inc(TB_ICON_PADDING), 42); DrawRectangle3D(5, currentTool*TB_ICON_PADDING+TOOLBAR_H, 16+3+2, 16+3+2, 0x333333, 0x777777); } void DrawEditArea() { dword color1=0xC0C0C0; int top_side; int left_side; wrapper.w = Form.cwidth - right_bar.w - 10 - wrapper.x; wrapper.h = Form.cheight - TOOLBAR_H - 35; //canvas{ canvas.w = image.columns * zoom.value; canvas.h = image.rows * zoom.value; if (canvas.w+2 > wrapper.w) || (canvas.h+2 > wrapper.h) { zoom.click(BTN_ZOOM_OUT); DrawEditArea(); return; } canvas.x = -zoom.value*image.columns+wrapper.w/2 + wrapper.x; canvas.y = -zoom.value*image.rows+wrapper.h/2 + wrapper.y; DrawCanvas(); //} left_side = canvas.x-wrapper.x-1; top_side = canvas.y-wrapper.y-1; DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w, wrapper.h, system.color.work_graph); if (left_side>0) { DrawBar(wrapper.x, wrapper.y, wrapper.w-1, top_side, color1); //top DrawBar(wrapper.x, wrapper.y+wrapper.h-top_side-1, wrapper.w-1, top_side, color1); //bottom } if (top_side>0) { //left DrawBar(wrapper.x, wrapper.y+top_side, left_side, wrapper.h-top_side-top_side, color1); //right DrawBar(wrapper.x+wrapper.w-left_side-1, wrapper.y+top_side, left_side, wrapper.h-top_side-top_side, color1); } DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080); } void DrawActiveColor(dword iny) { static dword outy; if (iny != NULL) outy = iny; DrawBar(right_bar.x, outy, 20, 20, active_color_1); sprintf(#param, "%A", active_color_1); EDI = system.color.work; WriteText(right_bar.x + 30, outy + 3, 0xD0, system.color.work_text, #param+4); DrawBar(right_bar.x+110, outy, 20, 20, active_color_2); sprintf(#param, "%A", active_color_2); EDI = system.color.work; WriteText(right_bar.x+110 + 30, outy + 3, 0xD0, system.color.work_text, #param+4); DrawCurrentColorGradientByLightness(); } void DrawCurrentColorGradientByLightness() { int i; int w = right_bar.w-10/2; for (i=0; i0; i--) { last_used_colors[i] = last_used_colors[i-1]; } last_used_colors[0] = _color; if (_number == 1) active_color_1 = _color; if (_number == 2) active_color_2 = _color; DrawActiveColor(NULL); DrawColorPallets(); } void EventFill(dword _r, _c, _color) { #define MARKED 6 int r, c, i, restart; dword old_color = image.get_pixel(_r, _c); image.set_pixel(_r, _c, MARKED); do { restart=false; for (r = 0; r < image.rows; r++) for (c = 0; c < image.columns; c++) { IF (image.get_pixel(r,c) != old_color) continue; IF (image.get_pixel(r,c) == MARKED) continue; IF (c>0) && (image.get_pixel(r,c-1) == MARKED) image.set_pixel(r,c,MARKED); IF (r>0) && (image.get_pixel(r-1,c) == MARKED) image.set_pixel(r,c,MARKED); IF (c calc(-dy)) { error -= dy; x1 += signX; } if(error2 < dx) { error += dx; y1 += signY; } } } void DrawRectangleInCanvas(int x1, int y1, int x2, int y2, dword color, int target) { DrawLine(x1, y1, x2, y1, color, target); DrawLine(x2, y1, x2, y2, color, target); DrawLine(x2, y2, x1, y2, color, target); DrawLine(x1, y2, x1, y1, color, target); }