Subversion Repositories Kolibri OS

Rev

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