Subversion Repositories Kolibri OS

Rev

Rev 7229 | Rev 7253 | 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.         BTNS_PALETTE_COLOR_MAS = 100,
  71.         BTNS_LAST_USED_COLORS = 400
  72. };
  73.  
  74. proc_info Form;
  75.  
  76. more_less_box zoom = { 11, 1, 40, "Zoom" };
  77.  
  78. dword default_palette[] = {
  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 
  91. };
  92. dword last_used_colors[13*2] = {
  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
  96. };
  97.  
  98. _image image;
  99.  
  100. #include "actions_history.h"
  101.  
  102. libimg_image open_image;
  103. _ActionsHistory actionsHistory;
  104.  
  105. enum {
  106.         TOOL_NONE = -1,
  107.         TOOL_PENCIL,
  108.         TOOL_PIPETTE,
  109.         TOOL_FILL,
  110.         TOOL_LINE,
  111.         TOOL_RECT,
  112. };
  113.  
  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();
  136.  
  137.         currentTool = index;
  138.        
  139.         if ((index != TOOL_NONE) && (tools[index].activate != 0))
  140.                 tools[index].activate();
  141.  
  142.         DrawLeftPanel();
  143. }
  144.  
  145. //===================================================//
  146. //                                                   //
  147. //                       CODE                        //
  148. //                                                   //
  149. //===================================================//
  150.  
  151. void FillTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
  152.         if (canvas.hovered()) && (currentTool==TOOL_FILL) && (mouse.up)
  153.         {
  154.                 EventFill(mouseY-canvas.y/zoom.value,
  155.                                 mouseX-canvas.x/zoom.value, tool_color);
  156.                 actionsHistory.saveCurrentState();                     
  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) {
  166.         tool_color = GetPixelUnderMouse();
  167.         DrawBar(Form.cwidth-30, 5, 20, 20, tool_color);
  168.        
  169.         if (mouse.down) {
  170.                 SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
  171.                 if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, tool_color);
  172.                 if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, tool_color);
  173.                
  174.                 setCurrentTool(TOOL_PENCIL);
  175.         }
  176. }
  177.  
  178. bool PencilTool_Drawing = false;
  179.  
  180. void PencilTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
  181.         if (canvas.hovered())
  182.         {
  183.                 if ((PencilTool_Drawing == true) && (!mouse.key)) {
  184.                         actionsHistory.saveCurrentState();
  185.                         PencilTool_Drawing = false;
  186.                 }
  187.  
  188.                 if (mouse.key) {
  189.                         image.set_pixel(mouseY-canvas.y/zoom.value,
  190.                                 mouseX-canvas.x/zoom.value, tool_color);
  191.                         PencilTool_Drawing = true;
  192.                 }
  193.                 DrawCanvas();
  194.         }
  195. }
  196.  
  197. void PencilTool_reset() {
  198.         PencilTool_Drawing = false;
  199. }
  200.  
  201. // Line tool
  202. struct SimpleFigureTool_State {
  203.         int startX, startY;
  204.         int lastTempPosX, lastTempPosY;
  205. };
  206.  
  207. enum {
  208.         TOOL_LINE_STATE,
  209.         TOOL_RECT_STATE
  210. };
  211.  
  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;
  225. }
  226.  
  227. int mouseX_last;
  228. int mouseY_last;
  229. bool first_click_in_canvas = false;
  230.  
  231. void SimpleFigureTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
  232.         if (mouse.down) && (canvas.hovered()) first_click_in_canvas = true;
  233.         if (first_click_in_canvas)
  234.         {
  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<canvas.x) mouseX = canvas.x;
  238.                 if (mouseY<canvas.y) mouseY = canvas.y;
  239.  
  240.                 if (mouse.key) {
  241.                         if ((figTool_States[currentFigToolState].startX < 0) || (figTool_States[currentFigToolState].startY < 0)) {
  242.                                 figTool_States[currentFigToolState].startX = mouseX;
  243.                                 figTool_States[currentFigToolState].startY = mouseY;
  244.                         }
  245.                         else {
  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.                                 }
  251.                         }
  252.                         mouseX_last = mouseX;
  253.                         mouseY_last = mouseY;
  254.                 }
  255.                 if (mouse.up) {
  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,
  263.                                                 tool_color,
  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,
  270.                                                 mouseY - canvas.y/zoom.value, tool_color, 1);
  271.                                 }
  272.  
  273.                                 DrawCanvas();
  274.  
  275.                                 actionsHistory.saveCurrentState();
  276.  
  277.                                 // Reset start position
  278.                                 figTool_States[currentFigToolState].startX = -1;
  279.                                 figTool_States[currentFigToolState].startY = -1;
  280.  
  281.                                 first_click_in_canvas = false;
  282.                         }
  283.                 }
  284.         }
  285. }
  286.  
  287. void SimpleFigureTool_onCanvasDraw() {
  288.         if ((figTool_States[currentFigToolState].startX >= 0) && (figTool_States[currentFigToolState].startY >= 0) && (mouse.key)) {
  289.                 if (currentTool == TOOL_LINE) {
  290.                         DrawLine(figTool_States[currentFigToolState].startX - canvas.x/zoom.value,
  291.                                 figTool_States[currentFigToolState].startY - canvas.y/zoom.value,
  292.                                 mouseX_last - canvas.x/zoom.value,
  293.                                 mouseY_last - canvas.y/zoom.value,
  294.                                 tool_color,
  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,
  300.                                 mouseX_last - canvas.x/zoom.value,
  301.                                 mouseY_last - canvas.y/zoom.value,
  302.                                 tool_color,
  303.                                 2);
  304.                 }
  305.  
  306.                 figTool_States[currentFigToolState].lastTempPosX = mouseX_last - canvas.x/zoom.value;
  307.                 figTool_States[currentFigToolState].lastTempPosY = mouseY_last - canvas.y/zoom.value;
  308.         }
  309. }
  310.  
  311. void initTools()
  312. {
  313.         tools[0].id = TOOL_PENCIL;
  314.         tools[0].onMouseEvent = #PencilTool_onMouseEvent;
  315.         tools[0].deactivate = #PencilTool_reset;
  316.        
  317.         tools[1].id = TOOL_PIPETTE;
  318.         tools[1].activate = #PipetteTool_activate;
  319.         tools[1].onMouseEvent = #PipetteTool_onMouseEvent;
  320.        
  321.         tools[2].id = TOOL_FILL;
  322.         tools[2].onMouseEvent = #FillTool_onMouseEvent;
  323.        
  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;
  329.        
  330.         tools[4].id = TOOL_RECT;
  331.         tools[4].activate = #SimpleFigureTool_Reset;
  332.         tools[4].deactivate = #SimpleFigureTool_Reset;
  333.         tools[4].onMouseEvent = #SimpleFigureTool_onMouseEvent;
  334.         tools[4].onCanvasDraw = #SimpleFigureTool_onCanvasDraw;
  335. }
  336.  
  337. void main()
  338. {
  339.         word btn;
  340.  
  341.         load_dll(libio,  #libio_init,  1);
  342.         load_dll(libimg, #libimg_init, 1);
  343.         Libimg_LoadImage(#skin, "/sys/icons16.png");
  344.         //system.color.get();
  345.         //Libimg_ReplaceColor(tools_img.image, tools_img.w, tools_img.h, 0xFFF8C0D0, system.color.work);
  346.        
  347.         image.create(32, 32);
  348.  
  349.         if (param[0]) {
  350.                 Libimg_LoadImage(#open_image, #param);
  351.                 if (open_image.w==32) && (open_image.h==32) {
  352.                         image.set_image(open_image.imgsrc);
  353.                 }
  354.                 else {
  355.                         notify("'Error: image format is unacceptable (PNG, 32x32x16b expected)' -E");
  356.                 }
  357.         }
  358.  
  359.         actionsHistory.init();
  360.  
  361.         initTools();
  362.         setCurrentTool(TOOL_PENCIL);
  363.        
  364.         SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
  365.  
  366.         loop() switch(WaitEvent())
  367.         {
  368.                 case evMouse:
  369.                         mouse.get();
  370.                        
  371.                         if (mouse.lkm) tool_color = color1;
  372.                         if (mouse.pkm) tool_color = color2;
  373.                         if (mouse.mkm) break;
  374.  
  375.                         if (currentTool != TOOL_NONE)
  376.                                 tools[currentTool].onMouseEvent(mouse.x, mouse.y, mouse.lkm, mouse.pkm);
  377.  
  378.                         if (mouse.vert) {
  379.                                 if (mouse.vert==65535) zoom.inc();
  380.                                 if (mouse.vert==1) zoom.dec();
  381.                                 DrawEditArea();
  382.                         }
  383.  
  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.  
  393.                         break;
  394.  
  395.                 case evButton:
  396.                         btn = GetButtonID();
  397.                         if (zoom.click(btn)) DrawEditArea();
  398.                         switch(btn)
  399.                         {
  400.                                 case BTN_NEW:
  401.                                         image.create(32, 32);
  402.                                         DrawCanvas();
  403.                                         break;
  404.                                 case BTN_OPEN:
  405.                                         RunProgram("/sys/lod", sprintf(#param, "*png* %s",#program_path));
  406.                                         break;
  407.                                 case BTN_SAVE:
  408.                                         EventSave();
  409.                                         break;
  410.                                 case BTN_MOVE_LEFT:
  411.                                         image.move(MOVE_LEFT);
  412.                                         DrawCanvas();
  413.                                         break;
  414.                                 case BTN_MOVE_RIGHT:
  415.                                         image.move(MOVE_RIGHT);
  416.                                         DrawCanvas();
  417.                                         break;
  418.                                 case BTN_MOVE_UP:
  419.                                         image.move(MOVE_UP);
  420.                                         DrawCanvas();
  421.                                         break;
  422.                                 case BTN_MOVE_DOWN:
  423.                                         image.move(MOVE_DOWN);
  424.                                         DrawCanvas();
  425.                                         break;
  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;
  434.                                 case BTN_PENCIL:
  435.                                         setCurrentTool(TOOL_PENCIL);
  436.                                         break;
  437.                                 case BTN_PICK:
  438.                                         setCurrentTool(TOOL_PIPETTE);
  439.                                         //EventPickActivate();
  440.                                         break;
  441.                                 case BTN_FILL:
  442.                                         setCurrentTool(TOOL_FILL);
  443.                                         //EventFillActivate();
  444.                                         break;
  445.                                 case BTN_LINE:
  446.                                         setCurrentTool(TOOL_LINE);
  447.                                         break;
  448.                                 case BTN_RECT:
  449.                                         setCurrentTool(TOOL_RECT);
  450.                                         break;
  451.                                 case CLOSE_BTN:
  452.                                         ExitProcess();
  453.                                         break;
  454.                         }
  455.                         break;
  456.          
  457.                 case evKey:
  458.                         GetKeys();
  459.                         if (key_scancode == SCAN_CODE_ESC) setCurrentTool(TOOL_PENCIL);
  460.                         if (key_scancode == SCAN_CODE_KEY_P) setCurrentTool(TOOL_PENCIL);
  461.                         if (key_scancode == SCAN_CODE_KEY_I) setCurrentTool(TOOL_PIPETTE);
  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);
  465.  
  466.                         if (key_scancode == SCAN_CODE_KEY_S) actionsHistory.undoLastAction();
  467.                         if (key_scancode == SCAN_CODE_KEY_C) actionsHistory.redoLastAction();
  468.  
  469.                         if (key_scancode == SCAN_CODE_MINUS) {zoom.inc(); DrawEditArea();}
  470.                         if (key_scancode == SCAN_CODE_PLUS)  {zoom.dec();  DrawEditArea();}
  471.                         break;
  472.                  
  473.                 case evReDraw:
  474.                         draw_window();
  475.                         break;
  476.         }
  477. }
  478.  
  479. void DrawToolbarButton(dword _id, _x, _icon_n)
  480. {
  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);
  484. }
  485.  
  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.  
  494. void DrawStatusBar()
  495. {
  496.         zoom.draw(wrapper.x, wrapper.y + wrapper.h + 6);
  497.  
  498.         sprintf(#param,"Canvas: %ix%i", image.rows, image.columns);
  499.         WriteText(wrapper.x+wrapper.w-calc(strlen(#param)*8), zoom.y+2, 0x90, system.color.work_text, #param);
  500. }
  501.  
  502. void draw_window()
  503. {
  504.         incn tx;
  505.         system.color.get();
  506.         DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x33, system.color.work, T_TITLE, 0);
  507.         GetProcessInfo(#Form, SelfInfo);
  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.  
  512.         right_bar.x = Form.cwidth - right_bar.w;
  513.         b_color_gradient.x = b_last_colors.x = b_default_palette.x = right_bar.x;
  514.  
  515.         tx.n = 10-TB_ICON_PADDING;
  516.         DrawToolbarButton(BTN_NEW,    tx.inc(TB_ICON_PADDING), 2); //not implemented
  517.         DrawToolbarButton(BTN_OPEN,   tx.inc(TB_ICON_PADDING), 0); //not implemented
  518.         DrawToolbarButton(BTN_SAVE,   tx.inc(TB_ICON_PADDING), 5);
  519.         DrawToolbarButton(BTN_MOVE_LEFT,  tx.inc(TB_ICON_PADDING+8), 30);
  520.         DrawToolbarButton(BTN_MOVE_RIGHT, tx.inc(TB_ICON_PADDING),   31);
  521.         DrawToolbarButton(BTN_MOVE_UP,    tx.inc(TB_ICON_PADDING),   32);
  522.         DrawToolbarButton(BTN_MOVE_DOWN,  tx.inc(TB_ICON_PADDING),   33);
  523.        
  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
  528.  
  529.         DrawLeftPanel();
  530.        
  531.         DrawEditArea();
  532.  
  533.         DrawActiveColor(right_bar.y);
  534.         DrawColorPallets();
  535.  
  536.         DrawStatusBar();
  537. }
  538.  
  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.  
  551. void DrawEditArea()
  552. {
  553.         dword color1=0xC0C0C0;
  554.         int top_side;
  555.         int left_side;
  556.  
  557.         wrapper.w = Form.cwidth - right_bar.w - 10 - wrapper.x;
  558.         wrapper.h = Form.cheight - TOOLBAR_H - 35;
  559.  
  560.         //canvas{
  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) {
  564.                 zoom.dec();
  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();
  571.         //}
  572.  
  573.         left_side = canvas.x-wrapper.x-1;
  574.         top_side = canvas.y-wrapper.y-1;
  575.  
  576.         DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w, wrapper.h, system.color.work_graph);
  577.  
  578.         if (left_side>0)
  579.         {
  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
  582.         }
  583.         if (top_side>0)
  584.         {
  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);
  591.         }
  592.         DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080);
  593. }
  594.  
  595. void DrawActiveColor(dword iny)
  596. {
  597.         static dword outy;
  598.         if (iny != NULL) outy = iny;
  599.         DrawBar(right_bar.x, outy, 20, 20, color1);
  600.         sprintf(#param, "%A", color1);
  601.         EDI = system.color.work;
  602.         WriteText(right_bar.x + 30, outy + 3, 0xD0, system.color.work_text, #param+4);
  603.  
  604.         DrawBar(right_bar.x+110, outy, 20, 20, color2);
  605.         sprintf(#param, "%A", color2);
  606.         EDI = system.color.work;
  607.         WriteText(right_bar.x+110 + 30, outy + 3, 0xD0, system.color.work_text, #param+4);     
  608.         DrawCurrentColorGradientByLightness();
  609. }
  610.  
  611. void DrawCurrentColorGradientByLightness()
  612. {
  613.         int i;
  614.         int w = right_bar.w-10/2;
  615.         for (i=0; i<w; i++)
  616.                 DrawBar(b_color_gradient.x+i, b_color_gradient.y,
  617.                         1, b_color_gradient.h, MixColors(color1,0xFFFfff,255*i/w));
  618.         for (i=0 ; i<=w; i++)
  619.                 DrawBar(b_color_gradient.x+w+w-i, b_color_gradient.y,
  620.                         1, b_color_gradient.h, MixColors(color1,0x000000,255*i/w));
  621. }
  622.  
  623. void DrawColorPallets()
  624. {
  625.         int r, c, i=0;
  626.         //Last used colors
  627.         for (r = 0; r < 2; r++)
  628.         {
  629.                 for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
  630.                 {
  631.                         DrawBar(c*COLSIZE + b_last_colors.x, r*COLSIZE + b_last_colors.y,
  632.                                 COLSIZE, COLSIZE, last_used_colors[i]);
  633.                 }
  634.         }
  635.         i=0;
  636.         //Default colors
  637.         for (r = 0; r < 9; r++)
  638.         {
  639.                 for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
  640.                 {
  641.                         DrawBar(c*COLSIZE + b_default_palette.x, r*COLSIZE + b_default_palette.y,
  642.                                 COLSIZE, COLSIZE, default_palette[PALLETE_SIZE-i]);
  643.                 }
  644.         }
  645. }
  646.  
  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.         }
  658.        
  659.         if ((currentTool != TOOL_NONE) && (tools[currentTool].onCanvasDraw != 0))
  660.                 tools[currentTool].onCanvasDraw();
  661.  
  662.         DrawPreview();
  663. }
  664.  
  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. }
  672.  
  673. dword GetPixelUnderMouse()
  674. {
  675.         return GetPixelColorFromScreen(mouse.x + Form.left + 5, mouse.y + Form.top + skin_height);
  676. }
  677.  
  678. //===================================================//
  679. //                                                   //
  680. //                      EVENTS                       //
  681. //                                                   //
  682. //===================================================//
  683.  
  684. void EventSave()
  685. {
  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");
  694.         }
  695.         else {
  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 {
  707.                         if (CreateFile(encoded_size, encoded_data, "/rd/1/saved_image.png") == 0) {
  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.                 }
  714.         }
  715. }
  716.  
  717. void EventSetActiveColor(int _number, _color)
  718. {
  719.         int i;
  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;
  724.  
  725.         if (_number == 1) color1 = _color;
  726.         if (_number == 2) color2 = _color;
  727.  
  728.         DrawActiveColor(NULL);
  729.         DrawColorPallets();
  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.                                
  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<image.columns-1) && (image.get_pixel(r,c+1) == MARKED) image.set_pixel(r,c,MARKED);
  751.                                 IF (r<image.rows-1)    && (image.get_pixel(r+1,c) == MARKED) image.set_pixel(r,c,MARKED);
  752.                                
  753.                                 IF (image.get_pixel(r,c)==MARKED) restart=true;
  754.                         }
  755.         }while(restart);
  756.  
  757.         for (i=0; i<image.columns*image.rows; i++)
  758.                         IF (image.mas[i]==MARKED) image.mas[i] = _color;
  759. }
  760.  
  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. }