Subversion Repositories Kolibri OS

Rev

Rev 7373 | Rev 7444 | 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. #define MEMSIZE 4096*500
  8.  
  9. #include "../lib/gui.h"
  10. #include "../lib/random.h"
  11. #include "../lib/mem.h"
  12. #include "../lib/cursor.h"
  13.  
  14. #include "../lib/obj/libimg.h"
  15. #include "../lib/obj/box_lib.h"
  16.  
  17. #include "../lib/patterns/rgb.h"
  18. #include "../lib/patterns/libimg_load_skin.h"
  19.  
  20. #include "colors_mas.h"
  21.  
  22. #ifndef AUTOBUILD
  23. #include "lang.h--"
  24. #endif
  25.  
  26. //===================================================//
  27. //                                                   //
  28. //                       DATA                        //
  29. //                                                   //
  30. //===================================================//
  31.  
  32. #ifdef LANG_RUS
  33. #define T_NOTIFY_OPEN "'IconEdit
  34. ‚ ¤ ­­ë© ¬®¬¥­â IconEdit ¬®¦¥â ®âªà뢠âì ⮫쪮 ¨ª®­ª¨, ᮧ¤ ­­ë¥ ¢ á ¬®¬ । ªâ®à¥.
  35. …᫨ ­ã¦­® ®âªàëâì ¤à㣮¥ ¨§®¡à ¦¥­¨¥, ¢®á¯®«ì§ã©â¥áì ¨­áâà㬥­â®¬ <”®â® ¯¯ à â>
  36. ¤«ï § å¢ â  ª à⨭ª¨ á íªà ­ .' -Wt"
  37. #else
  38. #define T_NOTIFY_OPEN "'IconEdit
  39. You can open only files created in IconEdit for now!
  40. In other case please use <Photo> tool to get an image from screen.' -Wt"
  41. #endif
  42.  
  43.  
  44. #define T_TITLE "Icon Editor 0.57b Alpha"
  45.  
  46. #define TOPBAR_H    24+8
  47. #define LEFTBAR_W 16+5+5+3+3
  48. #define PALLETE_SIZE 116
  49.  
  50. #define PAL_ITEMS_X_COUNT 13
  51. #define COLSIZE 18
  52. #define RIGHT_BAR_W PAL_ITEMS_X_COUNT*COLSIZE
  53.  
  54. #define TO_CANVAS_X(xval) xval - canvas.x/zoom.value
  55. #define TO_CANVAS_Y(yval) yval - canvas.y/zoom.value
  56.  
  57. block canvas = { NULL, NULL, NULL, NULL };
  58. block wrapper = { LEFTBAR_W, TOPBAR_H, NULL, NULL };
  59. block right_bar = { NULL, 10+TOPBAR_H, RIGHT_BAR_W+10, NULL };
  60.  
  61. dword linear_gradient[RIGHT_BAR_W];
  62. block b_color_gradient = {NULL, 40+TOPBAR_H, RIGHT_BAR_W, 25};
  63. //block b_opacity_gradient = {NULL, 75+TOPBAR_H, RIGHT_BAR_W, 15};
  64. block b_last_colors = {NULL, 75+TOPBAR_H, RIGHT_BAR_W, COLSIZE};
  65. block b_default_palette = {NULL, COLSIZE+10+75+TOPBAR_H, RIGHT_BAR_W, COLSIZE*9};
  66.  
  67. dword transparent = 0xBFCAD2;
  68. dword color1 = 0x000000;
  69. dword color2 = 0xBFCAD2;
  70. dword tool_color;
  71.  
  72. signed hoverX;
  73. signed hoverY;
  74. signed priorHoverX;
  75. signed priorHoverY;
  76. bool canvasMouseMoved = false;
  77.  
  78. enum {
  79.         BTN_NEW = 40,
  80.         BTN_OPEN,
  81.         BTN_SAVE,
  82.         BTN_MOVE_LEFT,
  83.         BTN_MOVE_RIGHT,
  84.         BTN_MOVE_UP,
  85.         BTN_MOVE_DOWN,
  86.         BTN_FLIP_HOR,
  87.         BTN_FLIP_VER,
  88.         BTN_ROTATE_LEFT,
  89.         BTN_ROTATE_RIGHT,
  90.         BTN_TEST_ICON,
  91.         BTN_PENCIL,
  92.         BTN_PICK,
  93.         BTN_FILL,
  94.         BTN_LINE,
  95.         BTN_RECT,
  96.         BTN_BAR,
  97.         BTN_SELECT,
  98.         BTN_SCREEN_COPY,
  99.         BTN_ZOOM_IN,
  100.         BTN_ZOOM_OUT,
  101.         BTN_CANVAS_RESIZE,
  102.         BTN_CROP,
  103.         BTNS_PALETTE_COLOR_MAS = 100,
  104.         BTNS_LAST_USED_COLORS = 400
  105. };
  106.  
  107. proc_info Form;
  108. dword semi_white;
  109. bool bg_dark=false;
  110.  
  111. more_less_box zoom = { 11, 1, 40, "Zoom" };
  112.  
  113. dword default_palette[] = {
  114. 0x330000,0x331900,0x333300,0x193300,0x003300,0x003319,0x003333,0x001933,0x000033,0x190033,
  115. 0x330033,0x330019,0x000000,0x660000,0x663300,0x666600,0x336600,0x006600,0x006633,0x006666,
  116. 0x003366,0x000066,0x330066,0x660066,0x660033,0x202020,0x990000,0x994C00,0x999900,0x4C9900,
  117. 0x009900,0x00994C,0x009999,0x004C99,0x000099,0x4C0099,0x990099,0x99004C,0x404040,0xCC0000,
  118. 0xCC6600,0xCCCC00,0x66CC00,0x00CC00,0x00CC66,0x00CCCC,0x0066CC,0x0000CC,0x6600CC,0xCC00CC,
  119. 0xCC0066,0x606060,0xFF0000,0xFF8000,0xFFFF00,0x80FF00,0x00FF00,0x00FF80,0x00FFFF,0x0080FF,
  120. 0x0000FF,0x7F00FF,0xFF00FF,0xFF007F,0x808080,0xFF3333,0xFF9933,0xFFFF33,0x99FF33,0x33FF33,
  121. 0x33FF99,0x33FFFF,0x3399FF,0x3333FF,0x9933FF,0xFF33FF,0xFF3399,0xA0A0A0,0xFF6666,0xFFB266,
  122. 0xFFFF66,0xB2FF66,0x66FF66,0x66FFB2,0x66FFFF,0x66B2FF,0x6666FF,0xB266FF,0xFF66FF,0xFF66B2,
  123. 0xC0C0C0,0xFF9999,0xFFCC99,0xFFFF99,0xCCFF99,0x99FF99,0x99FFCC,0x99FFFF,0x99CCFF,0x9999FF,
  124. 0xCC99FF,0xFF99FF,0xFF99CC,0xE0E0E0,0xFFCCCC,0xFFE5CC,0xFFFFCC,0xE5FFCC,0xCCFFCC,0xCCFFE5,
  125. 0xCCFFFF,0xCCE5FF,0xCCCCFF,0xE5CCFF,0xFFCCFF,0xFFCCE5,0xFFFFFF 
  126. };
  127.  
  128. #define LAST_USED_MAX 13
  129. dword last_used_colors[LAST_USED_MAX] = {
  130. 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,
  131. 0xFFFFFF,0xFFFFFF,0xFFFFFF
  132. };
  133.  
  134. CustomCursor Cursor;
  135. dword CursorBar = FROM "cursors/bar.cur";
  136. dword CursorFill = FROM "cursors/fill.cur";
  137. dword CursorLine = FROM "cursors/line.cur";
  138. dword CursorPencil = FROM "cursors/pencil.cur";
  139. dword CursorPipette = FROM "cursors/pipette.cur";
  140. dword CursorRectangle = FROM "cursors/rectangle.cur";
  141. dword CursorSelect = FROM "cursors/select.cur";
  142.  
  143. _image image;
  144.  
  145. #include "actions_history.h"
  146.  
  147. _ActionsHistory actionsHistory;
  148.  
  149. #include "tools.h"
  150. #include "canvas_resize.h"
  151.  
  152. //===================================================//
  153. //                                                   //
  154. //                       CODE                        //
  155. //                                                   //
  156. //===================================================//
  157.  
  158. libimg_image top_icons;
  159. libimg_image left_icons;
  160.  
  161. void main()
  162. {
  163.         word btn;
  164.         libimg_image open_image;
  165.         dword tmp_bg_col;
  166.  
  167.         load_dll(libio,  #libio_init,  1);
  168.         load_dll(libimg, #libimg_init, 1);
  169.         load_dll(boxlib, #box_lib_init,0);
  170.  
  171.         Libimg_LoadImage(#top_icons, "/sys/icons16.png");
  172.         Libimg_LoadImage(#left_icons, "/sys/icons16.png");
  173.  
  174.         system.color.get();
  175.  
  176.         semi_white = MixColors(system.color.work, 0xFFFfff, 96);
  177.         Libimg_ReplaceColor(top_icons.image, top_icons.w, top_icons.h, 0xffFFFfff, semi_white);
  178.         Libimg_ReplaceColor(top_icons.image, top_icons.w, top_icons.h, 0xffCACBD6, MixColors(semi_white, 0, 220));
  179.  
  180.         Libimg_ReplaceColor(left_icons.image, left_icons.w, left_icons.h, 0xffFFFfff, system.color.work);
  181.         Libimg_ReplaceColor(left_icons.image, left_icons.w, left_icons.h, 0xffCACBD6, MixColors(system.color.work, 0, 200));
  182.  
  183.         //fix line and rectandle color for dark skins
  184.         if (GrayScaleImage(#system.color.work,1,1)<65) bg_dark=true; else bg_dark=false;
  185.         if (bg_dark) Libimg_ReplaceColor(left_icons.image, left_icons.w, left_icons.h, 0xff545454, 0xffD3D3D4);
  186.  
  187.         EventSetActiveColor(1, color1);
  188.  
  189.         if (!param[0]) {
  190.                 image.create(32, 32);
  191.         }
  192.         else
  193.         {
  194.                 Libimg_LoadImage(#open_image, #param);
  195.  
  196.                 if (open_image.w*open_image.h>MAX_CELL_SIZE*MAX_CELL_SIZE) {
  197.                         notify("'Hey, this is just an icon editor,\nselected image is too big to open!' -E");
  198.                         ExitProcess();
  199.                 }
  200.                 else {
  201.                         image.create(open_image.h, open_image.w);
  202.                         image.set_image(open_image.imgsrc);
  203.                 }
  204.         }
  205.  
  206.         actionsHistory.init();
  207.  
  208.         initTools();
  209.         setCurrentTool(TOOL_PENCIL);
  210.        
  211.         SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
  212.  
  213.         loop() switch(WaitEvent())
  214.         {
  215.                 case evMouse:
  216.                         if (Window_CanvasReSize.thread_exists()) break;
  217.                         mouse.get();
  218.                        
  219.                         if (mouse.lkm) tool_color = color1;
  220.                         if (mouse.pkm) tool_color = color2;
  221.                         if (mouse.mkm) break;
  222.  
  223.                         hoverX = mouse.x - canvas.x / zoom.value;
  224.                         hoverY = mouse.y - canvas.y / zoom.value;
  225.                         if (hoverX<0) hoverX = 0;
  226.                         if (hoverY<0) hoverY = 0;
  227.                         if (hoverX>image.columns-1) hoverX = image.columns-1;
  228.                         if (hoverY>image.rows-1) hoverY = image.rows-1;
  229.                         canvasMouseMoved = false;
  230.                         if (priorHoverX != hoverX) canvasMouseMoved = true;
  231.                         if (priorHoverY != hoverY) canvasMouseMoved = true;
  232.                         priorHoverX = hoverX;
  233.                         priorHoverY = hoverY;
  234.                         //DrawBar(Form.cwidth-100, 3, 80, 12, 0xFFFfff);
  235.                         //WriteText(Form.cwidth-100, 3, 0x80, 0x000000,
  236.                         //      sprintf(#param, "%i %i", hoverX, hoverY));
  237.  
  238.                         if (currentTool != TOOL_NONE)
  239.                                 tools[currentTool].onMouseEvent(mouse.x, mouse.y, mouse.lkm, mouse.pkm);
  240.  
  241.                         if (mouse.vert) {
  242.                                 if (mouse.vert==65535) zoom.inc();
  243.                                 if (mouse.vert==1) zoom.dec();
  244.                                 DrawEditArea();
  245.                         }
  246.  
  247.                         if (wrapper.hovered()) SetCursor();
  248.                         else Cursor.Restore();
  249.  
  250.                         if (mouse.down) {
  251.                                 if (b_color_gradient.hovered())
  252.                                 || (b_last_colors.hovered())
  253.                                 || (b_default_palette.hovered()) {
  254.                                         if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, GetPixelUnderMouse());
  255.                                         if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, GetPixelUnderMouse());
  256.                                 }      
  257.                         }
  258.  
  259.                         break;
  260.  
  261.                 case evButton:
  262.                         if (Window_CanvasReSize.thread_exists()) break;
  263.                         btn = GetButtonID();
  264.  
  265.                         if (zoom.click(btn)) DrawEditArea();
  266.  
  267.                         switch(btn)
  268.                         {
  269.                                 case BTN_NEW:
  270.                                         EventCreateNewIcon();
  271.                                         break;
  272.                                 case BTN_OPEN:
  273.                                         EventOpenIcon();
  274.                                         break;
  275.                                 case BTN_SAVE:
  276.                                         EventSaveIconToFile();
  277.                                         break;
  278.                                 case BTN_MOVE_LEFT:
  279.                                         EventMove(MOVE_LEFT);
  280.                                         break;
  281.                                 case BTN_MOVE_RIGHT:
  282.                                         EventMove(MOVE_RIGHT);
  283.                                         break;
  284.                                 case BTN_MOVE_UP:
  285.                                         EventMove(MOVE_UP);
  286.                                         break;
  287.                                 case BTN_MOVE_DOWN:
  288.                                         EventMove(MOVE_DOWN);
  289.                                         break;
  290.                                 case BTN_FLIP_VER:
  291.                                         EventMove(FLIP_VER);
  292.                                         break;
  293.                                 case BTN_FLIP_HOR:
  294.                                         EventMove(FLIP_HOR);
  295.                                         break;
  296.                                 case BTN_TEST_ICON:
  297.                                         EventTestIcon();
  298.                                         break;
  299.                                 case BTN_PENCIL:
  300.                                         setCurrentTool(TOOL_PENCIL);
  301.                                         break;
  302.                                 case BTN_PICK:
  303.                                         setCurrentTool(TOOL_PIPETTE);
  304.                                         break;
  305.                                 case BTN_FILL:
  306.                                         setCurrentTool(TOOL_FILL);
  307.                                         break;
  308.                                 case BTN_LINE:
  309.                                         setCurrentTool(TOOL_LINE);
  310.                                         break;
  311.                                 case BTN_RECT:
  312.                                         setCurrentTool(TOOL_RECT);
  313.                                         break;
  314.                                 case BTN_BAR:
  315.                                         setCurrentTool(TOOL_BAR);
  316.                                         break;
  317.                                 case BTN_SELECT:
  318.                                         setCurrentTool(TOOL_SELECT);
  319.                                         break;
  320.                                 case BTN_SCREEN_COPY:
  321.                                         setCurrentTool(TOOL_SCREEN_COPY);
  322.                                         break;
  323.                                 case BTN_CANVAS_RESIZE:
  324.                                         notify("Sorry, not implemented yet.");
  325.                                         break;
  326.                                 case BTN_CROP:
  327.                                         EventCrop();
  328.                                         break;
  329.                                 case CLOSE_BTN:
  330.                                         EventExitIconEdit();
  331.                                         break;
  332.                         }
  333.                         break;
  334.          
  335.                 case evKey:
  336.                         GetKeys();
  337.  
  338.                         if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL)
  339.                         {
  340.                                 switch(key_scancode)
  341.                                 {
  342.                                         case SCAN_CODE_KEY_S:
  343.                                                 EventSaveIconToFile();
  344.                                                 break;
  345.                                         case SCAN_CODE_KEY_O:
  346.                                                 EventOpenIcon();
  347.                                                 break;
  348.                                 }
  349.                         }
  350.  
  351.                         if (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) {
  352.                                 if (key_scancode == SCAN_CODE_DEL) EventCleanCanvas();
  353.                         }
  354.  
  355.                         if (currentTool != TOOL_NONE) && (tools[currentTool].onKeyEvent != 0)
  356.                                 tools[currentTool].onKeyEvent(key_scancode);
  357.  
  358.                         if (key_scancode == SCAN_CODE_KEY_P) setCurrentTool(TOOL_PENCIL);
  359.                         if (key_scancode == SCAN_CODE_KEY_I) setCurrentTool(TOOL_PIPETTE);
  360.                         if (key_scancode == SCAN_CODE_KEY_F) setCurrentTool(TOOL_FILL);
  361.                         if (key_scancode == SCAN_CODE_KEY_L) setCurrentTool(TOOL_LINE);
  362.                         if (key_scancode == SCAN_CODE_KEY_R) setCurrentTool(TOOL_RECT);
  363.                         if (key_scancode == SCAN_CODE_KEY_B) setCurrentTool(TOOL_BAR);
  364.                         if (key_scancode == SCAN_CODE_KEY_S) setCurrentTool(TOOL_SELECT);
  365.  
  366.                         if (key_scancode == SCAN_CODE_KEY_T) EventTestIcon();
  367.  
  368.                         if (key_scancode == SCAN_CODE_KEY_Z) actionsHistory.undoLastAction();
  369.                         if (key_scancode == SCAN_CODE_KEY_Y) actionsHistory.redoLastAction();
  370.  
  371.                         if (key_scancode == SCAN_CODE_MINUS) {zoom.dec(); DrawEditArea();}
  372.                         if (key_scancode == SCAN_CODE_PLUS)  {zoom.inc(); DrawEditArea();}
  373.  
  374.                         break;
  375.                  
  376.                 case evReDraw:
  377.                         Window_CanvasReSize.thread_exists();
  378.                         DrawWindow();
  379.                         break;
  380.         }
  381. }
  382.  
  383. void DrawTopPanelButton(dword _id, _x, _icon_n)
  384. {
  385.         DrawWideRectangle(_x, 4, 22, 22, 3, semi_white);
  386.         PutPixel(_x,4,system.color.work);
  387.         PutPixel(_x,4+21,system.color.work);
  388.         PutPixel(_x+21,4,system.color.work);
  389.         PutPixel(_x+21,4+21,system.color.work);
  390.         DefineHiddenButton(_x, 4, 21, 21, _id);
  391.         img_draw stdcall(top_icons.image, _x+3, 7, 16, 16, 0, _icon_n*16);
  392. }
  393.  
  394. void DrawLeftPanelButton(dword _id, _y, _icon_n)
  395. {
  396.         int x = 5;
  397.         DrawRectangle(x, _y, 22-1, 22-1, system.color.work);
  398.         DefineHiddenButton(x, _y, 21, 21, _id);
  399.         img_draw stdcall(left_icons.image, x+3, _y+3, 16, 16, 0, _icon_n*16);
  400. }
  401.  
  402. void DrawStatusBar()
  403. {
  404.         zoom.draw(wrapper.x, wrapper.y + wrapper.h + 6);
  405.  
  406.         sprintf(#param,"%i x %i", image.columns, image.rows);
  407.         DrawCaptButton(
  408.                 wrapper.x+wrapper.w-calc(strlen(#param)*8) -6 - 1,
  409.                 zoom.y,
  410.                 calc(strlen(#param)*8)+6,
  411.                 18,
  412.                 BTN_CANVAS_RESIZE,
  413.                 system.color.work_button,
  414.                 system.color.work_button_text,
  415.                 #param
  416.                 );
  417. }
  418.  
  419. void DrawWindow()
  420. {
  421.         #define GAP 27
  422.         #define BLOCK_SPACE 10
  423.         incn tx;
  424.         system.color.get();
  425.         DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x73, NULL, T_TITLE, 0);
  426.         GetProcessInfo(#Form, SelfInfo);
  427.         if (Form.status_window>2) return;
  428.         if (Form.width  < 560) { MoveSize(OLD,OLD,560,OLD); return; }
  429.         if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; }
  430.  
  431.         right_bar.x = Form.cwidth - right_bar.w;
  432.         b_color_gradient.x = b_last_colors.x = b_default_palette.x = right_bar.x;
  433.         DrawBar(0, 0, Form.cwidth, TOPBAR_H-1, system.color.work);
  434.         DrawBar(0, TOPBAR_H-1, Form.cwidth, 1, system.color.work_graph);
  435.  
  436.         tx.n = 5-GAP;
  437.         DrawTopPanelButton(BTN_NEW,    tx.inc(GAP), 2); //not implemented
  438.         DrawTopPanelButton(BTN_OPEN,   tx.inc(GAP), 0); //not implemented
  439.         DrawTopPanelButton(BTN_SAVE,   tx.inc(GAP), 5);
  440.         DrawTopPanelButton(BTN_MOVE_LEFT,  tx.inc(GAP+BLOCK_SPACE), 30);
  441.         DrawTopPanelButton(BTN_MOVE_RIGHT, tx.inc(GAP),   31);
  442.         DrawTopPanelButton(BTN_MOVE_UP,    tx.inc(GAP),   32);
  443.         DrawTopPanelButton(BTN_MOVE_DOWN,  tx.inc(GAP),   33);
  444.        
  445.         DrawTopPanelButton(BTN_FLIP_HOR,   tx.inc(GAP+BLOCK_SPACE), 34);
  446.         DrawTopPanelButton(BTN_FLIP_VER,   tx.inc(GAP),   35);
  447.  
  448.         DrawTopPanelButton(BTN_TEST_ICON,  tx.inc(GAP+BLOCK_SPACE), 12);
  449.  
  450.         DrawTopPanelButton(BTN_CROP,  tx.inc(GAP+BLOCK_SPACE), 46);
  451.         // DrawTopPanelButton(BTN_ROTATE_LEFT,   tx.inc(GAP), 36); //not implemented
  452.         // DrawTopPanelButton(BTN_ROTATE_RIGHT,  tx.inc(GAP), 37); //not implemented
  453.        
  454.         DrawEditArea();
  455.  
  456.         DrawBar(0, TOPBAR_H, LEFTBAR_W-1, Form.cheight - TOPBAR_H, system.color.work);
  457.         DrawLeftPanel();
  458.  
  459.         DrawBar(wrapper.x+wrapper.w, TOPBAR_H, Form.cwidth-wrapper.x-wrapper.w,
  460.                 Form.cheight - TOPBAR_H, system.color.work);
  461.         DrawActiveColor(right_bar.y);
  462.         DrawColorPallets();
  463.         DrawPreview();
  464.  
  465.         DrawBar(LEFTBAR_W-1, wrapper.y + wrapper.h, wrapper.w+1,
  466.                 Form.cheight - wrapper.y - wrapper.h, system.color.work);
  467.         DrawStatusBar();
  468. }
  469.  
  470. void DrawLeftPanel()
  471. {
  472.         #define GAP 28
  473.         incn ty;
  474.         ty.n = right_bar.y - GAP - 2;
  475.         DrawLeftPanelButton(BTN_PENCIL, ty.inc(GAP), 38);
  476.         DrawLeftPanelButton(BTN_PICK,   ty.inc(GAP), 39);
  477.         DrawLeftPanelButton(BTN_FILL,   ty.inc(GAP), 40);
  478.         DrawLeftPanelButton(BTN_LINE,   ty.inc(GAP), 41);
  479.         DrawLeftPanelButton(BTN_RECT,   ty.inc(GAP), 42);
  480.         DrawLeftPanelButton(BTN_BAR,    ty.inc(GAP), 43);
  481.         DrawLeftPanelButton(BTN_SELECT, ty.inc(GAP), 44);
  482.         DrawLeftPanelButton(BTN_SCREEN_COPY, ty.inc(GAP), 45);
  483.         DrawRectangle3D(5, currentTool*GAP+right_bar.y-2, 16+3+2, 16+3+2, 0x333333, 0x777777);
  484. }
  485.  
  486. void DrawEditArea()
  487. {
  488.         dword color1=0xC0C0C0;
  489.         int top_side;
  490.         int left_side;
  491.  
  492.         wrapper.w = Form.cwidth - right_bar.w - 10 - wrapper.x;
  493.         wrapper.h = Form.cheight - TOPBAR_H - 35;
  494.  
  495.         //canvas{
  496.         canvas.w = image.columns * zoom.value;
  497.         canvas.h = image.rows * zoom.value;
  498.         if (canvas.w+2 > wrapper.w) || (canvas.h+2 > wrapper.h) {
  499.                 zoom.value--;
  500.                 if (zoom.x) zoom.redraw();
  501.                 DrawEditArea();
  502.                 return;
  503.         }
  504.         canvas.x = -zoom.value*image.columns+wrapper.w/2 + wrapper.x;
  505.         canvas.y = -zoom.value*image.rows+wrapper.h/2 + wrapper.y;
  506.         DrawCanvas();
  507.         //}
  508.  
  509.         left_side = canvas.x-wrapper.x-1;
  510.         top_side = canvas.y-wrapper.y-1;
  511.  
  512.         DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w, wrapper.h, system.color.work_graph);
  513.  
  514.         if (left_side>0)
  515.         {
  516.                 DrawBar(wrapper.x, wrapper.y, wrapper.w-1, top_side, color1); //top
  517.                 DrawBar(wrapper.x, wrapper.y+wrapper.h-top_side-1, wrapper.w-1, top_side, color1); //bottom
  518.         }
  519.         if (top_side>0)
  520.         {
  521.                 //left
  522.                 DrawBar(wrapper.x, wrapper.y+top_side, left_side,
  523.                         wrapper.h-top_side-top_side, color1);
  524.                 //right
  525.                 DrawBar(wrapper.x+wrapper.w-left_side-1, wrapper.y+top_side, left_side,
  526.                         wrapper.h-top_side-top_side, color1);
  527.         }
  528.         DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080);
  529. }
  530.  
  531. void DrawActiveColor(dword iny)
  532. {
  533.         #define CELL 20
  534.         static dword outy;
  535.         if (iny != NULL) outy = iny;
  536.         DrawFrame(right_bar.x, outy, CELL, CELL, NULL);
  537.         DrawBar(right_bar.x+2, outy+2, CELL-4, CELL-4, color1);
  538.  
  539.         DrawFrame(right_bar.x+CELL+5, outy, CELL, CELL, NULL);
  540.         DrawBar(right_bar.x+CELL+5+2, outy+2, CELL-4, CELL-4, color2);
  541.  
  542.         //sprintf(#param, "%A", color1);
  543.         //WriteTextWithBg(right_bar.x+30, outy+3, 0xD0, system.color.work_text, #param+4, system.color.work);
  544.         DrawCurrentColorGradient();
  545. }
  546.  
  547. int lmax;
  548. void GenerateCurrentColorGradient()
  549. {
  550.         int i, avg, rmax;
  551.  
  552.         rgb.DwordToRgb(color1);
  553.         avg = 255 - calc(rgb.r + rgb.g + rgb.b / 3);
  554.  
  555.         lmax = b_color_gradient.w *avg/255 | 1;
  556.         rmax = b_color_gradient.w - lmax | 1;
  557.         if (lmax == 0) lmax=1;
  558.         if (rmax == 0) rmax=1;
  559.        
  560.         for (i=0; i<lmax; i++) {
  561.                 linear_gradient[i] = MixColors(color1,0xFFFfff,255*i/lmax);
  562.         }
  563.  
  564.         for (i=0 ; i<=rmax; i++) {
  565.                 linear_gradient[lmax+rmax - i] = MixColors(color1,0x000000,255*i/rmax);
  566.         }
  567. }
  568.  
  569. void DrawCurrentColorGradient()
  570. {
  571.         int i;
  572.         dword hitch_color=system.color.work;
  573.         int hitch_x = b_color_gradient.x+lmax-1;
  574.         if (lmax>b_color_gradient.w-2) hitch_x=b_color_gradient.x+b_color_gradient.w-3;
  575.  
  576.         for (i=0 ; i<b_color_gradient.w; i++) {
  577.                 DrawBar(b_color_gradient.x+i, b_color_gradient.y, 1, b_color_gradient.h, linear_gradient[i]);          
  578.         }
  579.         //current color marker 
  580.         DrawBar( b_color_gradient.x-1, b_color_gradient.y-2, b_color_gradient.w+4, 2, system.color.work);
  581.        
  582.         if (bg_dark) hitch_color=0xFFFfff; else hitch_color=0;
  583.         DrawBar(hitch_x, b_color_gradient.y-2, 3,2, hitch_color);
  584. }
  585.  
  586. void DrawColorPallets()
  587. {
  588.         int r, c, i=0;
  589.         //Last used colors
  590.         for (r = 0; r < LAST_USED_MAX/PAL_ITEMS_X_COUNT; r++)
  591.         {
  592.                 for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
  593.                 {
  594.                         DrawBar(c*COLSIZE + b_last_colors.x, r*COLSIZE + b_last_colors.y,
  595.                                 COLSIZE, COLSIZE, last_used_colors[i]);
  596.                 }
  597.         }
  598.         i=0;
  599.         //Default colors
  600.         for (r = 0; r < 9; r++)
  601.         {
  602.                 for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
  603.                 {
  604.                         DrawBar(c*COLSIZE + b_default_palette.x, r*COLSIZE + b_default_palette.y,
  605.                                 COLSIZE, COLSIZE, default_palette[PALLETE_SIZE-i]);
  606.                 }
  607.         }
  608. }
  609.  
  610. void DrawCanvasPixel(dword _r,_c,_color)
  611. {
  612.         DrawBar(_c*zoom.value + canvas.x, _r*zoom.value + canvas.y,
  613.         zoom.value, zoom.value, _color);
  614. }
  615.  
  616. void DrawCanvas()
  617. {
  618.         int r, c;
  619.         dword color;
  620.  
  621.         if ((currentTool != TOOL_NONE) && (tools[currentTool].onCanvasDraw != 0))
  622.         {
  623.                 tools[currentTool].onCanvasDraw();
  624.         }
  625.  
  626.         for (r = 0; r < image.rows; r++)
  627.         {
  628.                 for (c = 0; c < image.columns; c++)
  629.                 {
  630.                         if (image.pixel_state.is_drawable(r,c))
  631.                                 DrawCanvasPixel(r, c, image.get_pixel(r,c));
  632.                 }
  633.         }
  634.         image.pixel_state.reset_and_set_all_drawable();
  635.  
  636.         DrawPreview();
  637. }
  638.  
  639. void DrawPreview()
  640. {
  641.         int x = right_bar.x;
  642.         int y = wrapper.y + wrapper.h - image.rows-2;
  643.         DrawRectangle(x, y, image.columns+1, image.rows+1, system.color.work_graph);
  644.         _PutImage(x+1,y+1, image.columns, image.rows, image.get_image());
  645. }
  646.  
  647. dword GetPixelUnderMouse()
  648. {
  649.         return GetPixelColorFromScreen(mouse.x + Form.left + 5, mouse.y + Form.top + skin_height);
  650. }
  651.  
  652. int preview_size = 128;
  653. void DrawImageWithBg(dword _x, _y, _col_to)
  654. {
  655.         _x *= preview_size;
  656.         _y *= preview_size;
  657.         DrawWideRectangle(_x,_y, preview_size, preview_size, preview_size-image.columns/2, _col_to);
  658.         _PutImage(preview_size - image.columns / 2 + _x, preview_size - image.rows / 2 + _y,
  659.                 image.columns, image.rows, image.get_image_with_replaced_color(color2, _col_to));
  660. }
  661.  
  662. void ShowWindow_TestIcon()
  663. {
  664.         if (image.rows>=preview_size) || (image.columns>=preview_size) {
  665.                 notify("'IconEdit\nImage is too big for preview!' -tE");
  666.                 return;
  667.         }
  668.         loop() switch(WaitEvent())
  669.         {
  670.                 case evButton:
  671.                         if (GetButtonID()) ExitProcess();
  672.                         break;
  673.          
  674.                 case evKey:
  675.                         GetKeys();
  676.                         if (key_scancode == SCAN_CODE_ESC) ExitProcess();
  677.                         break;
  678.                  
  679.                 case evReDraw:
  680.                         DefineAndDrawWindow(Form.left+100, Form.top+100, preview_size*2+9,
  681.                                 preview_size*2+skin_height+4, 0x74, NULL, "Test Icon", 0);
  682.                         DrawImageWithBg(0, 0, 0x000000);
  683.                         DrawImageWithBg(1, 0, 0xFFFfff);
  684.                         DrawImageWithBg(0, 1, GetPixelColorFromScreen(0, 0));
  685.                         DrawImageWithBg(1, 1, system.color.work);
  686.                         break;
  687.         }
  688. }
  689.  
  690. //===================================================//
  691. //                                                   //
  692. //                      EVENTS                       //
  693. //                                                   //
  694. //===================================================//
  695.  
  696. void EventCreateNewIcon()
  697. {
  698.         EventSaveIconToFile();
  699.         Window_CanvasReSize.create();
  700. }
  701.  
  702. void EventOpenIcon()
  703. {
  704.         //notify(T_NOTIFY_OPEN);
  705.         RunProgram("/sys/lod", sprintf(#param, "*png* %s",#program_path));
  706. }
  707.  
  708. void EventSaveIconToFile()
  709. {
  710.         int i=0;
  711.         char save_file_name[4096];
  712.         char save_path_stable[4096];
  713.         strcpy(#save_path_stable, "/tmp0/1");
  714.         do {
  715.                 i++;
  716.                 sprintf(#save_file_name, "%s/saved_icon_%i.png", #save_path_stable, i);
  717.         } while (file_exists(#save_file_name));
  718.         save_image(image.get_image(), image.columns, image.rows, #save_file_name);
  719. }
  720.  
  721. void EventCleanCanvas()
  722. {
  723.         image.create(image.rows, image.columns);
  724.         actionsHistory.saveCurrentState();
  725.         DrawCanvas();
  726. }
  727.  
  728. void EventExitIconEdit()
  729. {
  730.         EventSaveIconToFile();
  731.         ExitProcess();
  732. }
  733.  
  734. void EventSetActiveColor(int _number, _color)
  735. {
  736.         int i;
  737.         if (last_used_colors[0] == _color) return;
  738.         for (i=LAST_USED_MAX-1; i>0; i--) {
  739.                 last_used_colors[i] = last_used_colors[i-1];
  740.         }
  741.         last_used_colors[0] = _color;
  742.  
  743.         if (_number == 1) color1 = _color;
  744.         if (_number == 2) color2 = _color;
  745.  
  746.         if (b_color_gradient.hovered()) {
  747.                 lmax = mouse.x - b_color_gradient.x;
  748.         }
  749.         else {
  750.                 GenerateCurrentColorGradient();
  751.         }
  752.         DrawActiveColor(NULL);
  753.         DrawColorPallets();
  754. }
  755.  
  756. void EventTestIcon()
  757. {
  758.         CreateThread(#ShowWindow_TestIcon, #test_icon_stak+4092);
  759. }
  760.  
  761. void EventMove(dword _action)
  762. {
  763.         if (selection.state) {
  764.                 selection.buf.move(_action);
  765.                 SelectTool_onCanvasDraw();
  766.         }
  767.         else {
  768.                 image.move(_action);
  769.                 DrawCanvas();
  770.         }
  771.         actionsHistory.saveCurrentState();
  772. }
  773.  
  774. void EventCrop()
  775. {
  776.         if (selection.state) {
  777.                 EventSaveIconToFile();
  778.                 image.create(selection.buf.rows, selection.buf.columns);
  779.                 selection.move_to_point(0,0);
  780.                 selection.apply_to_image();
  781.                 selection.reset();
  782.                 actionsHistory.init();
  783.                 DrawWindow();
  784.         }
  785.         else {
  786.                 notify("'You need to select something before usnig crop tool.' -W");
  787.         }
  788. }
  789.  
  790. stop:
  791.  
  792. char test_icon_stak[4096];
  793.