Subversion Repositories Kolibri OS

Rev

Rev 7243 | Rev 7254 | 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.45"
  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. #define TO_CANVAS_X(xval) xval - canvas.x/zoom.value
  43. #define TO_CANVAS_Y(yval) yval - canvas.y/zoom.value
  44.  
  45. block canvas = { NULL, NULL, NULL, NULL };
  46. block wrapper = { PANEL_LEFT_W, TOOLBAR_H, NULL, NULL };
  47. block right_bar = { NULL, TOOLBAR_H, RIGHT_BAR_W+10, NULL };
  48.  
  49. block b_color_gradient = {NULL, 30+TOOLBAR_H, RIGHT_BAR_W, 30};
  50. block b_last_colors = {NULL, 70+TOOLBAR_H, RIGHT_BAR_W, COLSIZE*2};
  51. block b_default_palette = {NULL, COLSIZE*2+10+70+TOOLBAR_H, RIGHT_BAR_W, COLSIZE*9};
  52.  
  53. dword color1 = 0x000000;
  54. dword color2 = 0xFFFfff;
  55. dword tool_color;
  56.  
  57. enum {
  58.         BTN_NEW = 40,
  59.         BTN_OPEN,
  60.         BTN_SAVE,
  61.         BTN_MOVE_LEFT,
  62.         BTN_MOVE_RIGHT,
  63.         BTN_MOVE_UP,
  64.         BTN_MOVE_DOWN,
  65.         BTN_FLIP_HOR,
  66.         BTN_FLIP_VER,
  67.         BTN_ROTATE_LEFT,
  68.         BTN_ROTATE_RIGHT,
  69.         BTN_PENCIL,
  70.         BTN_PICK,
  71.         BTN_FILL,
  72.         BTN_LINE,
  73.         BTN_RECT,
  74.         BTN_SELECT,
  75.         BTN_ZOOM_IN,
  76.         BTN_ZOOM_OUT,
  77.         BTNS_PALETTE_COLOR_MAS = 100,
  78.         BTNS_LAST_USED_COLORS = 400
  79. };
  80.  
  81. proc_info Form;
  82.  
  83. more_less_box zoom = { 11, 1, 40, "Zoom" };
  84.  
  85. dword default_palette[] = {
  86. 0x330000,0x331900,0x333300,0x193300,0x003300,0x003319,0x003333,0x001933,0x000033,0x190033,
  87. 0x330033,0x330019,0x000000,0x660000,0x663300,0x666600,0x336600,0x006600,0x006633,0x006666,
  88. 0x003366,0x000066,0x330066,0x660066,0x660033,0x202020,0x990000,0x994C00,0x999900,0x4C9900,
  89. 0x009900,0x00994C,0x009999,0x004C99,0x000099,0x4C0099,0x990099,0x99004C,0x404040,0xCC0000,
  90. 0xCC6600,0xCCCC00,0x66CC00,0x00CC00,0x00CC66,0x00CCCC,0x0066CC,0x0000CC,0x6600CC,0xCC00CC,
  91. 0xCC0066,0x606060,0xFF0000,0xFF8000,0xFFFF00,0x80FF00,0x00FF00,0x00FF80,0x00FFFF,0x0080FF,
  92. 0x0000FF,0x7F00FF,0xFF00FF,0xFF007F,0x808080,0xFF3333,0xFF9933,0xFFFF33,0x99FF33,0x33FF33,
  93. 0x33FF99,0x33FFFF,0x3399FF,0x3333FF,0x9933FF,0xFF33FF,0xFF3399,0xA0A0A0,0xFF6666,0xFFB266,
  94. 0xFFFF66,0xB2FF66,0x66FF66,0x66FFB2,0x66FFFF,0x66B2FF,0x6666FF,0xB266FF,0xFF66FF,0xFF66B2,
  95. 0xC0C0C0,0xFF9999,0xFFCC99,0xFFFF99,0xCCFF99,0x99FF99,0x99FFCC,0x99FFFF,0x99CCFF,0x9999FF,
  96. 0xCC99FF,0xFF99FF,0xFF99CC,0xE0E0E0,0xFFCCCC,0xFFE5CC,0xFFFFCC,0xE5FFCC,0xCCFFCC,0xCCFFE5,
  97. 0xCCFFFF,0xCCE5FF,0xCCCCFF,0xE5CCFF,0xFFCCFF,0xFFCCE5,0xFFFFFF 
  98. };
  99. dword last_used_colors[13*2] = {
  100. 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,
  101. 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,
  102. 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF
  103. };
  104.  
  105. _image image;
  106.  
  107. #include "actions_history.h"
  108.  
  109. libimg_image open_image;
  110. _ActionsHistory actionsHistory;
  111.  
  112. enum {
  113.         TOOL_NONE = -1,
  114.         TOOL_PENCIL,
  115.         TOOL_PIPETTE,
  116.         TOOL_FILL,
  117.         TOOL_LINE,
  118.         TOOL_RECT,
  119.         TOOL_SELECT
  120. };
  121.  
  122. struct Tool {
  123.         int id;
  124.        
  125.         void (*activate)();
  126.         void (*deactivate)();
  127.         void (*onMouseEvent)(int x, int y, int lkm, int pkm);
  128.         void (*onKeyEvent)(dword keycode);
  129.         void (*onCanvasDraw)();
  130. };
  131.  
  132. Tool tools[6];
  133. int currentTool = -1;
  134.  
  135. void resetCurrentTool() {
  136.         if ((currentTool != TOOL_NONE) && (tools[currentTool].deactivate != 0)) {
  137.                 tools[currentTool].deactivate();
  138.         }
  139.        
  140.         currentTool = TOOL_NONE;
  141. }
  142.  
  143. void setCurrentTool(int index) {
  144.         resetCurrentTool();
  145.  
  146.         currentTool = index;
  147.        
  148.         if ((index != TOOL_NONE) && (tools[index].activate != 0))
  149.                 tools[index].activate();
  150.  
  151.         DrawLeftPanel();
  152.         DrawCanvas();
  153. }
  154.  
  155. //===================================================//
  156. //                                                   //
  157. //                       CODE                        //
  158. //                                                   //
  159. //===================================================//
  160.  
  161. void FillTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
  162.         if (canvas.hovered()) && (currentTool==TOOL_FILL) && (mouse.up)
  163.         {
  164.                 EventFill(mouseY-canvas.y/zoom.value,
  165.                                 mouseX-canvas.x/zoom.value, tool_color);
  166.                 actionsHistory.saveCurrentState();                     
  167.                 DrawCanvas();
  168.         }
  169. }
  170.  
  171. void PipetteTool_activate() {
  172.         SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE);
  173. }
  174.  
  175. void PipetteTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
  176.         tool_color = GetPixelUnderMouse();
  177.         DrawBar(Form.cwidth-30, 5, 20, 20, tool_color);
  178.        
  179.         if (mouse.down) {
  180.                 SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
  181.                 if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, tool_color);
  182.                 if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, tool_color);
  183.                
  184.                 setCurrentTool(TOOL_PENCIL);
  185.         }
  186. }
  187.  
  188. bool PencilTool_Drawing = false;
  189.  
  190. void PencilTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
  191.         if (canvas.hovered())
  192.         {
  193.                 if ((PencilTool_Drawing == true) && (!mouse.key)) {
  194.                         actionsHistory.saveCurrentState();
  195.                         PencilTool_Drawing = false;
  196.                 }
  197.  
  198.                 if (mouse.key) {
  199.                         image.set_pixel(mouseY-canvas.y/zoom.value,
  200.                                 mouseX-canvas.x/zoom.value, tool_color);
  201.                         PencilTool_Drawing = true;
  202.                 }
  203.                 DrawCanvas();
  204.         }
  205. }
  206.  
  207. void PencilTool_reset() {
  208.         PencilTool_Drawing = false;
  209. }
  210.  
  211. // Line tool
  212. struct SimpleFigureTool_State {
  213.         int startX, startY;
  214.         int lastTempPosX, lastTempPosY;
  215. };
  216.  
  217. enum {
  218.         TOOL_LINE_STATE,
  219.         TOOL_RECT_STATE
  220. };
  221.  
  222. dword currentFigToolState = -1;
  223. SimpleFigureTool_State figTool_States[2];
  224.  
  225. void SimpleFigureTool_Reset() {
  226.         if (currentTool == TOOL_LINE)
  227.                 currentFigToolState = TOOL_LINE_STATE;
  228.         else if (currentTool == TOOL_RECT)
  229.                 currentFigToolState = TOOL_RECT_STATE;
  230.  
  231.         figTool_States[currentFigToolState].startX = -1;
  232.         figTool_States[currentFigToolState].startY = -1;
  233.         figTool_States[currentFigToolState].lastTempPosX = -1;
  234.         figTool_States[currentFigToolState].lastTempPosY = -1;
  235. }
  236.  
  237. int mouseX_last;
  238. int mouseY_last;
  239. bool first_click_in_canvas = false;
  240.  
  241. void SimpleFigureTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
  242.         if (mouse.down) && (canvas.hovered()) first_click_in_canvas = true;
  243.         if (first_click_in_canvas)
  244.         {
  245.                 if (mouseX>canvas.x+canvas.w-zoom.value) mouseX = canvas.x+canvas.w-zoom.value;
  246.                 if (mouseY>canvas.y+canvas.h-zoom.value) mouseY = canvas.y+canvas.h-zoom.value;
  247.                 if (mouseX<canvas.x) mouseX = canvas.x;
  248.                 if (mouseY<canvas.y) mouseY = canvas.y;
  249.  
  250.                 if (mouse.key) {
  251.                         if ((figTool_States[currentFigToolState].startX < 0) || (figTool_States[currentFigToolState].startY < 0)) {
  252.                                 figTool_States[currentFigToolState].startX = mouseX;
  253.                                 figTool_States[currentFigToolState].startY = mouseY;
  254.                         }
  255.                         else {
  256.                                 if ((calc(mouseX - canvas.x/zoom.value) != figTool_States[currentFigToolState].lastTempPosX)
  257.                                         || (calc(mouseY - canvas.y/zoom.value) != figTool_States[currentFigToolState].lastTempPosY))
  258.                                 {
  259.                                         DrawCanvas();
  260.                                 }
  261.                         }
  262.                         mouseX_last = mouseX;
  263.                         mouseY_last = mouseY;
  264.                 }
  265.                 if (mouse.up) {
  266.                         if ((figTool_States[currentFigToolState].startX >= 0) && (figTool_States[currentFigToolState].startY >= 0)) {
  267.                                 // Draw line from start position to current position
  268.                                 if (currentTool == TOOL_LINE) {
  269.                                         DrawLine(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,
  273.                                                 tool_color,
  274.                                                 1);
  275.                                 }
  276.                                 else if (currentTool == TOOL_RECT) {
  277.                                         DrawRectangleInCanvas(figTool_States[currentFigToolState].startX - canvas.x/zoom.value,
  278.                                                 figTool_States[currentFigToolState].startY - canvas.y/zoom.value,
  279.                                                 mouseX - canvas.x/zoom.value,
  280.                                                 mouseY - canvas.y/zoom.value, tool_color, 1);
  281.                                 }
  282.  
  283.                                 DrawCanvas();
  284.  
  285.                                 actionsHistory.saveCurrentState();
  286.  
  287.                                 // Reset start position
  288.                                 figTool_States[currentFigToolState].startX = -1;
  289.                                 figTool_States[currentFigToolState].startY = -1;
  290.  
  291.                                 first_click_in_canvas = false;
  292.                         }
  293.                 }
  294.         }
  295. }
  296.  
  297. void SimpleFigureTool_onCanvasDraw() {
  298.         if ((figTool_States[currentFigToolState].startX >= 0) && (figTool_States[currentFigToolState].startY >= 0) && (mouse.key)) {
  299.                 if (currentTool == TOOL_LINE) {
  300.                         DrawLine(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.                 else if (currentTool == TOOL_RECT) {
  308.                         DrawRectangleInCanvas(figTool_States[currentFigToolState].startX - canvas.x/zoom.value,
  309.                                 figTool_States[currentFigToolState].startY - canvas.y/zoom.value,
  310.                                 mouseX_last - canvas.x/zoom.value,
  311.                                 mouseY_last - canvas.y/zoom.value,
  312.                                 tool_color,
  313.                                 2);
  314.                 }
  315.  
  316.                 figTool_States[currentFigToolState].lastTempPosX = mouseX_last - canvas.x/zoom.value;
  317.                 figTool_States[currentFigToolState].lastTempPosY = mouseY_last - canvas.y/zoom.value;
  318.         }
  319. }
  320.  
  321. // Selection
  322. int selection_start_x = -1;
  323. int selection_start_y = -1;
  324. int selection_end_x = -1;
  325. int selection_end_y = -1;
  326. bool selection_active = false;
  327.  
  328. dword SelectionTool_buffer = 0;
  329. dword SelectionTool_buffer_r = 0;
  330. dword SelectionTool_buffer_c = 0;
  331.  
  332. bool selection_moving_started = false;
  333. int selection_pivot_x = -1;
  334. int selection_pivot_y = -1;
  335.  
  336. void SelectTool_normalizeSelection() {
  337.         int t;
  338.  
  339.         // Restructuring of the selection coordinates
  340.         if (selection_end_x < selection_start_x) {
  341.                 t = selection_start_x;
  342.                 selection_start_x = selection_end_x;
  343.                 selection_end_x = t;
  344.         }
  345.  
  346.         if (selection_end_y < selection_start_y) {
  347.                 t = selection_end_y;
  348.                 selection_end_y = selection_start_y;
  349.                 selection_start_y = t;
  350.         }
  351. }
  352.  
  353. void reset_selection_moving() {
  354.         if (selection_moving_started) {
  355.                 SelectTool_drawBuffer(selection_start_x, selection_start_y, 1);
  356.  
  357.                 selection_pivot_x = -1;
  358.                 selection_pivot_y = -1;
  359.                
  360.                 selection_moving_started = false;
  361.                
  362.                 actionsHistory.saveCurrentState();
  363.                 DrawCanvas();
  364.         }
  365. }
  366.  
  367. bool is_selection_moving() {
  368.         return selection_moving_started;
  369. }
  370.  
  371. void reset_selection() {
  372.         reset_selection_moving();
  373.        
  374.         selection_start_x = -1;
  375.         selection_start_y = -1;
  376.         selection_end_x = -1;
  377.         selection_end_y = -1;  
  378. }
  379.  
  380. void SelectTool_activate() {
  381.         reset_selection();
  382.  
  383.         selection_active = false;
  384. }
  385.  
  386. void SelectTool_deactivate() {
  387.         reset_selection_moving();
  388. }
  389.  
  390. bool SelectTool_pointInSelection(int x, int y) {
  391.         if (x >= selection_start_x) && (x <= selection_end_x) && (y >= selection_start_y) && (y <= selection_end_y)
  392.                 return true;
  393.         else
  394.                 return false;
  395. }
  396.  
  397.  
  398. void SelectTool_copyToBuffer() {
  399.         dword offset, r, c;
  400.  
  401.                 if (SelectionTool_buffer != 0)
  402.                         free(SelectionTool_buffer);
  403.  
  404.                 SelectionTool_buffer_r = selection_end_y - selection_start_y + 1;
  405.                 SelectionTool_buffer_c = selection_end_x - selection_start_x + 1;
  406.                 SelectionTool_buffer = malloc(SelectionTool_buffer_r * SelectionTool_buffer_c * 4);
  407.  
  408.                 for (r = selection_start_y; r <= selection_end_y; r++) {
  409.                         for (c = selection_start_x; c <= selection_end_x; c++) {
  410.                                 offset = calc(SelectionTool_buffer_c * calc(r - selection_start_y) + calc(c - selection_start_x)) * 4;
  411.  
  412.                                 ESDWORD[SelectionTool_buffer + offset] = image.get_pixel(r, c);
  413.                         }
  414.                 }
  415. }
  416.  
  417. void SelectTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
  418.         int click_x, click_y, dx, dy, m_x, m_y, r, c, color;
  419.         dword pixel;
  420.        
  421.         m_x = TO_CANVAS_X(mouseX);
  422.         m_y = TO_CANVAS_Y(mouseY);
  423.        
  424.         if (mouse.down) && (canvas.hovered()) && (!selection_active) {
  425.                 if (selection_start_x != -1) && (SelectTool_pointInSelection(m_x, m_y)) {
  426.                         if (selection_pivot_x == -1) {
  427.                                 selection_pivot_x = m_x;
  428.                                 selection_pivot_y = m_y;
  429.                                
  430.                                 GetKeys();
  431.                                
  432.                                 if (!selection_moving_started) && ( !(key_modifier&KEY_LSHIFT) ) {
  433.                                         for (r = selection_start_y; r <= selection_end_y; r++)
  434.                                                 for (c = selection_start_x; c <= selection_end_x; c++) {
  435.                                                         image.set_pixel(r, c, color2);
  436.                                                 }
  437.                                 }
  438.                                
  439.                                 selection_moving_started = true;
  440.                         }
  441.                 }
  442.                 else {
  443.                
  444.                         reset_selection();
  445.                         selection_active = true;
  446.                 }
  447.         }
  448.  
  449.         if (selection_pivot_x != -1) {
  450.                 dx = m_x - selection_pivot_x;
  451.                 dy = m_y - selection_pivot_y;
  452.  
  453.                 if (selection_start_x + dx < 0)
  454.                         dx = selection_start_x;
  455.                
  456.                 if (selection_end_x + dx >= 32)
  457.                         dx = 31 - selection_end_x;
  458.                
  459.                 if (selection_start_y + dy < 0)
  460.                         dy = selection_start_y;
  461.                
  462.                 if (selection_end_y + dy >= 32)
  463.                         dy = 31 - selection_end_y;
  464.                
  465.                
  466.                 selection_start_x += dx;
  467.                 selection_end_x += dx;
  468.                
  469.                 selection_start_y += dy;
  470.                 selection_end_y += dy;
  471.                
  472.                 selection_pivot_x += dx;
  473.                 selection_pivot_y += dy;
  474.                
  475.                 DrawCanvas();
  476.         }
  477.        
  478.         if (selection_active)
  479.         {
  480.                 if (mouseX>canvas.x+canvas.w-zoom.value) mouseX = canvas.x+canvas.w-zoom.value;
  481.                 if (mouseY>canvas.y+canvas.h-zoom.value) mouseY = canvas.y+canvas.h-zoom.value;
  482.  
  483.                 if (mouseX<canvas.x) mouseX = canvas.x;
  484.                 if (mouseY<canvas.y) mouseY = canvas.y;
  485.  
  486.                 if (mouse.key) {
  487.                         selection_end_x = TO_CANVAS_X(mouseX);
  488.                         selection_end_y = TO_CANVAS_Y(mouseY);
  489.  
  490.                         if ((selection_start_x < 0) || (selection_start_y < 0)) {
  491.                                 selection_start_x = TO_CANVAS_X(mouseX);
  492.                                 selection_start_y = TO_CANVAS_Y(mouseY);
  493.                         }
  494.                         else {
  495.                                 DrawCanvas();
  496.  
  497.                                 /**if ((calc(TO_CANVAS_X(mouseX)) != selection_end_x)
  498.                                         || (calc(TO_CANVAS_Y(mouseY)) != selection_end_y))
  499.                                 {
  500.                                         DrawCanvas();
  501.                                 }*/
  502.                         }
  503.  
  504.                 }
  505.                
  506.                 if (mouse.up) {                
  507.                         selection_active = false;
  508.                        
  509.                         SelectTool_normalizeSelection();
  510.                         SelectTool_copyToBuffer();
  511.                 }
  512.         }
  513.        
  514.         if (mouse.up) {
  515.                 if (selection_pivot_x != -1) {
  516.                         selection_pivot_x = -1;
  517.                         selection_pivot_y = -1;
  518.                 }
  519.         }
  520. }
  521.  
  522. void SelectTool_onCanvasDraw() {       
  523.         if (selection_moving_started)
  524.                 SelectTool_drawBuffer(selection_start_x, selection_start_y, 2);
  525.  
  526.         if ((selection_start_x >= 0) && (selection_start_y >= 0) && (selection_end_x >= 0) && (selection_end_y >= 0)) {
  527.                 DrawSelection(selection_start_x, selection_start_y, selection_end_x, selection_end_y);
  528.         }      
  529. }
  530.  
  531. void SelectTool_drawBuffer(int insert_x, int insert_y, int target) {
  532.         dword color;
  533.         dword offset, r, c;
  534.         dword insert_to_x, insert_to_y;
  535.        
  536.         if (SelectionTool_buffer != 0) {
  537.                 insert_to_x = insert_x + SelectionTool_buffer_c - 1;
  538.                        
  539.                 if (insert_to_x >= image.columns)
  540.                         insert_to_x = image.columns-1;
  541.  
  542.                 insert_to_y = insert_y + SelectionTool_buffer_r - 1;
  543.                        
  544.                 if (insert_to_y >= image.rows)
  545.                         insert_to_y = image.rows-1;
  546.  
  547.                 for (r = insert_y; r <= insert_to_y; r++) {
  548.                         for (c = insert_x; c <= insert_to_x; c++) {
  549.                                         offset = calc(SelectionTool_buffer_c * calc(r - insert_y) + calc(c - insert_x)) * 4;
  550.  
  551.                                         color = ESDWORD[SelectionTool_buffer + offset];
  552.                                        
  553.                                         if (target == 1)
  554.                                                 image.set_pixel(r, c, color);
  555.                                         else
  556.                                                 DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y,
  557.                                                         zoom.value, zoom.value, color);
  558.                         }
  559.                 }      
  560.         }      
  561. }
  562.  
  563. void SelectTool_onKeyEvent(dword keycode) {
  564.         dword offset, r, c;
  565.         dword insert_x, insert_y, insert_to_x, insert_to_y;
  566.  
  567.         if (keycode == SCAN_CODE_KEY_V) {
  568.                 if (SelectionTool_buffer != 0) {
  569.                         reset_selection();
  570.                        
  571.                         selection_moving_started = true;
  572.                         selection_start_x = 0;
  573.                         selection_end_x = SelectionTool_buffer_c - 1;
  574.                        
  575.                         selection_start_y = 0;
  576.                         selection_end_y = SelectionTool_buffer_r - 1;
  577.                        
  578.                         DrawCanvas();
  579.        
  580.                 }
  581.         }
  582. }
  583.  
  584. void initTools()
  585. {
  586.         tools[0].id = TOOL_PENCIL;
  587.         tools[0].onMouseEvent = #PencilTool_onMouseEvent;
  588.         tools[0].deactivate = #PencilTool_reset;
  589.        
  590.         tools[1].id = TOOL_PIPETTE;
  591.         tools[1].activate = #PipetteTool_activate;
  592.         tools[1].onMouseEvent = #PipetteTool_onMouseEvent;
  593.        
  594.         tools[2].id = TOOL_FILL;
  595.         tools[2].onMouseEvent = #FillTool_onMouseEvent;
  596.        
  597.         tools[3].id = TOOL_LINE;
  598.         tools[3].activate = #SimpleFigureTool_Reset;
  599.         tools[3].deactivate = #SimpleFigureTool_Reset;
  600.         tools[3].onMouseEvent = #SimpleFigureTool_onMouseEvent;
  601.         tools[3].onCanvasDraw = #SimpleFigureTool_onCanvasDraw;
  602.        
  603.         tools[4].id = TOOL_RECT;
  604.         tools[4].activate = #SimpleFigureTool_Reset;
  605.         tools[4].deactivate = #SimpleFigureTool_Reset;
  606.         tools[4].onMouseEvent = #SimpleFigureTool_onMouseEvent;
  607.         tools[4].onCanvasDraw = #SimpleFigureTool_onCanvasDraw;
  608.  
  609.         tools[5].id = TOOL_SELECT;
  610.         tools[5].activate = #SelectTool_activate;
  611.         tools[5].deactivate = #SelectTool_deactivate;
  612.         tools[5].onMouseEvent = #SelectTool_onMouseEvent;
  613.         tools[5].onCanvasDraw = #SelectTool_onCanvasDraw;      
  614.         tools[5].onKeyEvent = #SelectTool_onKeyEvent;  
  615. }
  616.  
  617. void main()
  618. {
  619.         word btn;
  620.  
  621.         load_dll(libio,  #libio_init,  1);
  622.         load_dll(libimg, #libimg_init, 1);
  623.         Libimg_LoadImage(#skin, "/sys/icons16.png");
  624.         //system.color.get();
  625.         //Libimg_ReplaceColor(tools_img.image, tools_img.w, tools_img.h, 0xFFF8C0D0, system.color.work);
  626.        
  627.         image.create(32, 32);
  628.  
  629.         if (param[0]) {
  630.                 Libimg_LoadImage(#open_image, #param);
  631.                 if (open_image.w==32) && (open_image.h==32) {
  632.                         image.set_image(open_image.imgsrc);
  633.                 }
  634.                 else {
  635.                         notify("'Error: image format is unacceptable (PNG, 32x32x16b expected)' -E");
  636.                 }
  637.         }
  638.  
  639.         actionsHistory.init();
  640.  
  641.         initTools();
  642.         setCurrentTool(TOOL_PENCIL);
  643.        
  644.         SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
  645.  
  646.         loop() switch(WaitEvent())
  647.         {
  648.                 case evMouse:
  649.                         mouse.get();
  650.                        
  651.                         if (mouse.lkm) tool_color = color1;
  652.                         if (mouse.pkm) tool_color = color2;
  653.                         if (mouse.mkm) break;
  654.  
  655.                         if (currentTool != TOOL_NONE)
  656.                                 tools[currentTool].onMouseEvent(mouse.x, mouse.y, mouse.lkm, mouse.pkm);
  657.  
  658.                         if (mouse.vert) {
  659.                                 if (mouse.vert==65535) zoom.inc();
  660.                                 if (mouse.vert==1) zoom.dec();
  661.                                 DrawEditArea();
  662.                         }
  663.  
  664.                         if (mouse.down) {
  665.                                 if (b_color_gradient.hovered())
  666.                                 || (b_last_colors.hovered())
  667.                                 || (b_default_palette.hovered()) {
  668.                                         if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, GetPixelUnderMouse());
  669.                                         if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, GetPixelUnderMouse());
  670.                                 }      
  671.                         }
  672.  
  673.                         break;
  674.  
  675.                 case evButton:
  676.                         btn = GetButtonID();
  677.  
  678.                         switch(btn)
  679.                         {
  680.                                 case BTN_NEW:
  681.                                         image.create(32, 32);
  682.                                         DrawCanvas();
  683.                                         break;
  684.                                 case BTN_OPEN:
  685.                                         RunProgram("/sys/lod", sprintf(#param, "*png* %s",#program_path));
  686.                                         break;
  687.                                 case BTN_SAVE:
  688.                                         EventSave();
  689.                                         break;
  690.                                 case BTN_MOVE_LEFT:
  691.                                         image.move(MOVE_LEFT);
  692.                                         DrawCanvas();
  693.                                         break;
  694.                                 case BTN_MOVE_RIGHT:
  695.                                         image.move(MOVE_RIGHT);
  696.                                         DrawCanvas();
  697.                                         break;
  698.                                 case BTN_MOVE_UP:
  699.                                         image.move(MOVE_UP);
  700.                                         DrawCanvas();
  701.                                         break;
  702.                                 case BTN_MOVE_DOWN:
  703.                                         image.move(MOVE_DOWN);
  704.                                         DrawCanvas();
  705.                                         break;
  706.                                 case BTN_FLIP_VER:
  707.                                         image.move(FLIP_VER);
  708.                                         DrawCanvas();
  709.                                         break;
  710.                                 case BTN_FLIP_HOR:
  711.                                         image.move(FLIP_HOR);
  712.                                         DrawCanvas();
  713.                                         break;
  714.                                 case BTN_PENCIL:
  715.                                         setCurrentTool(TOOL_PENCIL);
  716.                                         break;
  717.                                 case BTN_PICK:
  718.                                         setCurrentTool(TOOL_PIPETTE);
  719.                                         //EventPickActivate();
  720.                                         break;
  721.                                 case BTN_FILL:
  722.                                         setCurrentTool(TOOL_FILL);
  723.                                         //EventFillActivate();
  724.                                         break;
  725.                                 case BTN_LINE:
  726.                                         setCurrentTool(TOOL_LINE);
  727.                                         break;
  728.                                 case BTN_RECT:
  729.                                         setCurrentTool(TOOL_RECT);
  730.                                         break;
  731.                                 case BTN_SELECT:
  732.                                         setCurrentTool(TOOL_SELECT);
  733.                                         break;
  734.                                 case BTN_ZOOM_IN:
  735.                                         zoom.inc();
  736.                                         DrawEditArea();
  737.                                         break;
  738.                                 case BTN_ZOOM_OUT:
  739.                                         zoom.dec();
  740.                                         DrawEditArea();
  741.                                         break;
  742.                                 case CLOSE_BTN:
  743.                                         ExitProcess();
  744.                                         break;
  745.                         }
  746.                         break;
  747.          
  748.                 case evKey:
  749.                         GetKeys();
  750.  
  751.                         if (currentTool != TOOL_NONE) && (tools[currentTool].onKeyEvent != 0)
  752.                                 tools[currentTool].onKeyEvent(key_scancode);
  753.  
  754.                         if (key_scancode == SCAN_CODE_ESC) setCurrentTool(TOOL_PENCIL);
  755.                         if (key_scancode == SCAN_CODE_KEY_P) setCurrentTool(TOOL_PENCIL);
  756.                         if (key_scancode == SCAN_CODE_KEY_I) setCurrentTool(TOOL_PIPETTE);
  757.                         if (key_scancode == SCAN_CODE_KEY_F) setCurrentTool(TOOL_FILL);
  758.                         if (key_scancode == SCAN_CODE_KEY_L) setCurrentTool(TOOL_LINE);
  759.                         if (key_scancode == SCAN_CODE_KEY_R) setCurrentTool(TOOL_RECT);
  760.  
  761.                         if (key_scancode == SCAN_CODE_KEY_Z) && (key_modifier&KEY_LCTRL) actionsHistory.undoLastAction();
  762.                         if (key_scancode == SCAN_CODE_KEY_Y) && (key_modifier&KEY_LCTRL) actionsHistory.redoLastAction();
  763.  
  764.                         if (key_scancode == SCAN_CODE_MINUS) {zoom.dec(); DrawEditArea();}
  765.                         if (key_scancode == SCAN_CODE_PLUS)  {zoom.inc();  DrawEditArea();}
  766.  
  767.                         break;
  768.                  
  769.                 case evReDraw:
  770.                         draw_window();
  771.                         break;
  772.         }
  773. }
  774.  
  775. void DrawToolbarButton(dword _id, _x, _icon_n)
  776. {
  777.         DrawWideRectangle(_x, 4, 22, 22, 3, 0xFFFfff);
  778.         DefineHiddenButton(_x, 4, 21, 21, _id);
  779.         img_draw stdcall(skin.image, _x+3, 7, 16, 16, 0, _icon_n*16);
  780. }
  781.  
  782. void DrawLeftPanelButton(dword _id, _y, _icon_n)
  783. {
  784.         int x = 5;
  785.         DrawWideRectangle(x, _y, 22, 22, 3, 0xFFFfff);
  786.         DefineHiddenButton(x, _y, 21, 21, _id);
  787.         img_draw stdcall(skin.image, x+3, _y+3, 16, 16, 0, _icon_n*16);
  788. }
  789.  
  790. void DrawStatusBar()
  791. {
  792.         zoom.draw(wrapper.x, wrapper.y + wrapper.h + 6);
  793.  
  794.         sprintf(#param,"Canvas: %ix%i", image.rows, image.columns);
  795.         WriteText(wrapper.x+wrapper.w-calc(strlen(#param)*8), zoom.y+2, 0x90, system.color.work_text, #param);
  796. }
  797.  
  798. void draw_window()
  799. {
  800.         incn tx;
  801.         system.color.get();
  802.         DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x33, system.color.work, T_TITLE, 0);
  803.         GetProcessInfo(#Form, SelfInfo);
  804.         if (Form.status_window>2) return;
  805.         if (Form.width  < 560) { MoveSize(OLD,OLD,560,OLD); return; }
  806.         if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; }
  807.  
  808.         right_bar.x = Form.cwidth - right_bar.w;
  809.         b_color_gradient.x = b_last_colors.x = b_default_palette.x = right_bar.x;
  810.  
  811.         tx.n = 10-TB_ICON_PADDING;
  812.         DrawToolbarButton(BTN_NEW,    tx.inc(TB_ICON_PADDING), 2); //not implemented
  813.         DrawToolbarButton(BTN_OPEN,   tx.inc(TB_ICON_PADDING), 0); //not implemented
  814.         DrawToolbarButton(BTN_SAVE,   tx.inc(TB_ICON_PADDING), 5);
  815.         DrawToolbarButton(BTN_MOVE_LEFT,  tx.inc(TB_ICON_PADDING+8), 30);
  816.         DrawToolbarButton(BTN_MOVE_RIGHT, tx.inc(TB_ICON_PADDING),   31);
  817.         DrawToolbarButton(BTN_MOVE_UP,    tx.inc(TB_ICON_PADDING),   32);
  818.         DrawToolbarButton(BTN_MOVE_DOWN,  tx.inc(TB_ICON_PADDING),   33);
  819.        
  820.         DrawToolbarButton(BTN_FLIP_HOR,   tx.inc(TB_ICON_PADDING+8), 34);
  821.         DrawToolbarButton(BTN_FLIP_VER,   tx.inc(TB_ICON_PADDING),   35);
  822.         // DrawToolbarButton(BTN_ROTATE_LEFT,   tx.inc(TB_ICON_PADDING), 36); //not implemented
  823.         // DrawToolbarButton(BTN_ROTATE_RIGHT,  tx.inc(TB_ICON_PADDING), 37); //not implemented
  824.  
  825.         DrawLeftPanel();
  826.        
  827.         DrawEditArea();
  828.  
  829.         DrawActiveColor(right_bar.y);
  830.         DrawColorPallets();
  831.  
  832.         DrawStatusBar();
  833. }
  834.  
  835. void DrawLeftPanel()
  836. {
  837.         incn ty;
  838.         ty.n = TOOLBAR_H-TB_ICON_PADDING;
  839.         DrawLeftPanelButton(BTN_PENCIL, ty.inc(TB_ICON_PADDING), 38);
  840.         DrawLeftPanelButton(BTN_PICK,   ty.inc(TB_ICON_PADDING), 39);
  841.         DrawLeftPanelButton(BTN_FILL,   ty.inc(TB_ICON_PADDING), 40);
  842.         DrawLeftPanelButton(BTN_LINE,   ty.inc(TB_ICON_PADDING), 41);
  843.         DrawLeftPanelButton(BTN_RECT,   ty.inc(TB_ICON_PADDING), 42);
  844.         DrawLeftPanelButton(BTN_SELECT,   ty.inc(TB_ICON_PADDING), 43);
  845.         DrawRectangle3D(5, currentTool*TB_ICON_PADDING+TOOLBAR_H, 16+3+2, 16+3+2, 0x333333, 0x777777);
  846. }
  847.  
  848. void DrawEditArea()
  849. {
  850.         dword color1=0xC0C0C0;
  851.         int top_side;
  852.         int left_side;
  853.  
  854.         wrapper.w = Form.cwidth - right_bar.w - 10 - wrapper.x;
  855.         wrapper.h = Form.cheight - TOOLBAR_H - 35;
  856.  
  857.         //canvas{
  858.         canvas.w = image.columns * zoom.value;
  859.         canvas.h = image.rows * zoom.value;
  860.         if (canvas.w+2 > wrapper.w) || (canvas.h+2 > wrapper.h) {
  861.                 zoom.dec();
  862.                 DrawEditArea();
  863.                 return;
  864.         }
  865.         canvas.x = -zoom.value*image.columns+wrapper.w/2 + wrapper.x;
  866.         canvas.y = -zoom.value*image.rows+wrapper.h/2 + wrapper.y;
  867.         DrawCanvas();
  868.         //}
  869.  
  870.         left_side = canvas.x-wrapper.x-1;
  871.         top_side = canvas.y-wrapper.y-1;
  872.  
  873.         DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w, wrapper.h, system.color.work_graph);
  874.  
  875.         if (left_side>0)
  876.         {
  877.                 DrawBar(wrapper.x, wrapper.y, wrapper.w-1, top_side, color1); //top
  878.                 DrawBar(wrapper.x, wrapper.y+wrapper.h-top_side-1, wrapper.w-1, top_side, color1); //bottom
  879.         }
  880.         if (top_side>0)
  881.         {
  882.                 //left
  883.                 DrawBar(wrapper.x, wrapper.y+top_side, left_side,
  884.                         wrapper.h-top_side-top_side, color1);
  885.                 //right
  886.                 DrawBar(wrapper.x+wrapper.w-left_side-1, wrapper.y+top_side, left_side,
  887.                         wrapper.h-top_side-top_side, color1);
  888.         }
  889.         DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080);
  890. }
  891.  
  892. void DrawActiveColor(dword iny)
  893. {
  894.         static dword outy;
  895.         if (iny != NULL) outy = iny;
  896.         DrawBar(right_bar.x, outy, 20, 20, color1);
  897.         sprintf(#param, "%A", color1);
  898.         EDI = system.color.work;
  899.         WriteText(right_bar.x + 30, outy + 3, 0xD0, system.color.work_text, #param+4);
  900.  
  901.         DrawBar(right_bar.x+110, outy, 20, 20, color2);
  902.         sprintf(#param, "%A", color2);
  903.         EDI = system.color.work;
  904.         WriteText(right_bar.x+110 + 30, outy + 3, 0xD0, system.color.work_text, #param+4);     
  905.         DrawCurrentColorGradientByLightness();
  906. }
  907.  
  908. void DrawCurrentColorGradientByLightness()
  909. {
  910.         int i;
  911.         int w = right_bar.w-10/2;
  912.         for (i=0; i<w; i++)
  913.                 DrawBar(b_color_gradient.x+i, b_color_gradient.y,
  914.                         1, b_color_gradient.h, MixColors(color1,0xFFFfff,255*i/w));
  915.         for (i=0 ; i<=w; i++)
  916.                 DrawBar(b_color_gradient.x+w+w-i, b_color_gradient.y,
  917.                         1, b_color_gradient.h, MixColors(color1,0x000000,255*i/w));
  918. }
  919.  
  920. void DrawColorPallets()
  921. {
  922.         int r, c, i=0;
  923.         //Last used colors
  924.         for (r = 0; r < 2; r++)
  925.         {
  926.                 for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
  927.                 {
  928.                         DrawBar(c*COLSIZE + b_last_colors.x, r*COLSIZE + b_last_colors.y,
  929.                                 COLSIZE, COLSIZE, last_used_colors[i]);
  930.                 }
  931.         }
  932.         i=0;
  933.         //Default colors
  934.         for (r = 0; r < 9; r++)
  935.         {
  936.                 for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
  937.                 {
  938.                         DrawBar(c*COLSIZE + b_default_palette.x, r*COLSIZE + b_default_palette.y,
  939.                                 COLSIZE, COLSIZE, default_palette[PALLETE_SIZE-i]);
  940.                 }
  941.         }
  942. }
  943.  
  944. void DrawCanvas()
  945. {
  946.         int r, c;
  947.         for (r = 0; r < image.rows; r++)
  948.         {
  949.                 for (c = 0; c < image.columns; c++)
  950.                 {
  951.                         DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y,
  952.                                 zoom.value, zoom.value, image.get_pixel(r, c));
  953.                 }
  954.         }
  955.        
  956.         if ((currentTool != TOOL_NONE) && (tools[currentTool].onCanvasDraw != 0))
  957.                 tools[currentTool].onCanvasDraw();
  958.  
  959.         DrawPreview();
  960. }
  961.  
  962. void DrawPreview()
  963. {
  964.         int x = right_bar.x;
  965.         int y = wrapper.y + wrapper.h - image.rows-2;
  966.         DrawRectangle(x, y, image.columns+1, image.rows+1, system.color.work_graph);
  967.         _PutImage(x+1,y+1, image.columns, image.rows, image.get_image());
  968. }
  969.  
  970. dword GetPixelUnderMouse()
  971. {
  972.         return GetPixelColorFromScreen(mouse.x + Form.left + 5, mouse.y + Form.top + skin_height);
  973. }
  974.  
  975. //===================================================//
  976. //                                                   //
  977. //                      EVENTS                       //
  978. //                                                   //
  979. //===================================================//
  980.  
  981. void EventSave()
  982. {
  983.         dword encoded_data=0;
  984.         dword encoded_size=0;
  985.         dword image_ptr = 0;
  986.        
  987.         image_ptr = create_image(Image_bpp24, 32, 32);
  988.  
  989.         if (image_ptr == 0) {
  990.                 notify("'Error saving file, probably not enought memory!' -E");
  991.         }
  992.         else {
  993.                 EDI = image_ptr;
  994.                 memmov(EDI._Image.Data, image.get_image(), image.rows * image.columns * 3);
  995.  
  996.                 encoded_data = encode_image(image_ptr, LIBIMG_FORMAT_PNG, 0, #encoded_size);
  997.  
  998.                 img_destroy stdcall(image_ptr);
  999.  
  1000.                 if(encoded_data == 0) {
  1001.                         notify("'Error saving file, incorrect data!' -E");
  1002.                 }
  1003.                 else {
  1004.                         if (CreateFile(encoded_size, encoded_data, "/rd/1/saved_image.png") == 0) {
  1005.                                 notify("'File saved as /rd/1/saved_image.png' -O");
  1006.                         }
  1007.                         else {
  1008.                                 notify("'Error saving file, probably not enought space on ramdisk!' -E");
  1009.                         }
  1010.                 }
  1011.         }
  1012. }
  1013.  
  1014. void EventSetActiveColor(int _number, _color)
  1015. {
  1016.         int i;
  1017.         for (i=13*2-1; i>0; i--) {
  1018.                 last_used_colors[i] = last_used_colors[i-1];
  1019.         }
  1020.         last_used_colors[0] = _color;
  1021.  
  1022.         if (_number == 1) color1 = _color;
  1023.         if (_number == 2) color2 = _color;
  1024.  
  1025.         DrawActiveColor(NULL);
  1026.         DrawColorPallets();
  1027. }
  1028.  
  1029. void EventFill(dword _r, _c, _color)
  1030. {
  1031.         #define MARKED 6
  1032.         int r, c, i, restart;
  1033.  
  1034.         dword old_color = image.get_pixel(_r, _c);
  1035.         image.set_pixel(_r, _c, MARKED);
  1036.  
  1037.         do {
  1038.                 restart=false; 
  1039.                 for (r = 0; r < image.rows; r++)
  1040.                         for (c = 0; c < image.columns; c++)
  1041.                         {
  1042.                                 IF (image.get_pixel(r,c) != old_color) continue;
  1043.                                 IF (image.get_pixel(r,c) == MARKED) continue;
  1044.                                
  1045.                                 IF (c>0)               && (image.get_pixel(r,c-1) == MARKED) image.set_pixel(r,c,MARKED);
  1046.                                 IF (r>0)               && (image.get_pixel(r-1,c) == MARKED) image.set_pixel(r,c,MARKED);
  1047.                                 IF (c<image.columns-1) && (image.get_pixel(r,c+1) == MARKED) image.set_pixel(r,c,MARKED);
  1048.                                 IF (r<image.rows-1)    && (image.get_pixel(r+1,c) == MARKED) image.set_pixel(r,c,MARKED);
  1049.                                
  1050.                                 IF (image.get_pixel(r,c)==MARKED) restart=true;
  1051.                         }
  1052.         }while(restart);
  1053.  
  1054.         for (i=0; i<image.columns*image.rows; i++)
  1055.                         IF (image.mas[i]==MARKED) image.mas[i] = _color;
  1056. }
  1057.  
  1058. // target - image (1) or canvas (2)
  1059. void DrawLine(int x1, int y1, int x2, int y2, dword color, int target) {
  1060.         int dx, dy, signX, signY, error, error2;
  1061.  
  1062.    dx = x2 - x1;
  1063.    
  1064.    if (dx < 0)
  1065.            dx = -dx;
  1066.    
  1067.    dy = y2 - y1;
  1068.  
  1069.    if (dy < 0)
  1070.            dy = -dy;
  1071.    
  1072.    if (x1 < x2)
  1073.            signX = 1;
  1074.    else
  1075.            signX = -1;
  1076.    
  1077.    if (y1 < y2)
  1078.            signY = 1;
  1079.    else
  1080.            signY = -1;
  1081.    
  1082.    error = dx - dy;
  1083.  
  1084.         if (target == 1)
  1085.                 image.set_pixel(y2, x2, color);
  1086.         else
  1087.                 DrawBar(x2*zoom.value + canvas.x, y2*zoom.value + canvas.y,
  1088.                                 zoom.value, zoom.value, color);
  1089.    
  1090.    while((x1 != x2) || (y1 != y2))
  1091.   {
  1092.                 if (target == 1)
  1093.                         image.set_pixel(y1, x1, color);
  1094.                 else
  1095.                         DrawBar(x1*zoom.value + canvas.x, y1*zoom.value + canvas.y,
  1096.                                 zoom.value, zoom.value, color);
  1097.                
  1098.            error2 = error * 2;
  1099.  
  1100.        if(error2 > calc(-dy))
  1101.        {
  1102.            error -= dy;
  1103.            x1 += signX;
  1104.        }
  1105.            
  1106.        if(error2 < dx)
  1107.        {
  1108.            error += dx;
  1109.            y1 += signY;
  1110.        }
  1111.    }
  1112.  
  1113. }
  1114.  
  1115. void DrawRectangleInCanvas(int x1, int y1, int x2, int y2, dword color, int target) {
  1116.         DrawLine(x1, y1, x2, y1, color, target);
  1117.         DrawLine(x2, y1, x2, y2, color, target);
  1118.         DrawLine(x2, y2, x1, y2, color, target);
  1119.         DrawLine(x1, y2, x1, y1, color, target);
  1120. }
  1121.  
  1122. #define SELECTION_COLOR 0xAAE5EF
  1123.  
  1124. void DrawSelection(int x1, int y1, int x2, int y2) {
  1125.         int p1x, p1y, p2x, p2y, r, c, old_color, new_color;
  1126.         dword offset;
  1127.  
  1128.         if (x1 <= x2) {
  1129.                 p1x = x1;
  1130.                 p2x = x2;
  1131.         }
  1132.         else {
  1133.                 p1x = x2;
  1134.                 p2x = x1;
  1135.         }
  1136.  
  1137.         if (y1 <= y2) {
  1138.                 p2y = y1;
  1139.                 p1y = y2;
  1140.         }
  1141.         else {
  1142.                 p2y = y2;
  1143.                 p1y = y1;
  1144.         }
  1145.  
  1146.         for (r = p1y; r >= p2y; r--) {
  1147.                 for (c = p1x; c <= p2x; c++) {
  1148.                        
  1149.                         if (selection_moving_started) && (SelectTool_pointInSelection(c, r)) {
  1150.                                 offset = calc(SelectionTool_buffer_c * calc(r - selection_start_y) + calc(c - selection_start_x)) * 4;
  1151.                                 old_color = ESDWORD[SelectionTool_buffer + offset];
  1152.                         }
  1153.                         else {
  1154.                                 old_color = image.get_pixel(r, c);
  1155.                         }
  1156.                        
  1157.                         new_color = MixColors(old_color, SELECTION_COLOR, 64);
  1158.                        
  1159.                         DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y,
  1160.                                 zoom.value, zoom.value, new_color);
  1161.  
  1162.                 }
  1163.         }
  1164. }