Subversion Repositories Kolibri OS

Rev

Rev 7207 | Rev 7229 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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