Subversion Repositories Kolibri OS

Rev

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