Subversion Repositories Kolibri OS

Rev

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