Subversion Repositories Kolibri OS

Rev

Rev 7150 | Rev 7152 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * BACKGEN - Background generator
  3.  * Author: Leency
  4.  * Licence: GPL v2
  5. */
  6.  
  7. /*
  8. TODO/BUGS
  9. Flip first pixel doesn't work well
  10. Open with param
  11. Set minimal window size
  12. Scroll mouse, +/- keyboard
  13. */
  14.  
  15. #define MEMSIZE 4096*40
  16.  
  17. #include "../lib/gui.h"
  18. #include "../lib/random.h"
  19. #include "../lib/mem.h"
  20. #include "../lib/obj/libimg.h"
  21. #include "../lib/patterns/rgb.h"
  22. #include "../lib/patterns/libimg_load_skin.h"
  23.  
  24. #include "colors_mas.h"
  25.  
  26. //===================================================//
  27. //                                                   //
  28. //                       DATA                        //
  29. //                                                   //
  30. //===================================================//
  31.  
  32. #define T_TITLE "Icon Editor 0.08"
  33.  
  34. #define TOOLBAR_H    24+9
  35. #define PALLETE_SIZE 116
  36.  
  37. rect wrapper = { 10, TOOLBAR_H, NULL, NULL };
  38. rect right_bar = { NULL, TOOLBAR_H, 280, NULL };
  39. rect canvas = { NULL, NULL, NULL, NULL };
  40.  
  41. dword active_color_1 = 0x000000;
  42. dword active_color_2 = 0xFFFfff;
  43.  
  44. enum {
  45.         BTN_NEW = 40,
  46.         BTN_OPEN,
  47.         BTN_SAVE,
  48.         BTN_MOVE_LEFT,
  49.         BTN_MOVE_RIGHT,
  50.         BTN_MOVE_UP,
  51.         BTN_MOVE_DOWN,
  52.         BTN_FLIP_HOR,
  53.         BTN_FLIP_VER,
  54.         BTN_ROTATE_LEFT,
  55.         BTN_ROTATE_RIGHT,
  56.         BTN_PICK,
  57.         BTN_PALETTE_COLOR_MAS = 100,
  58. };
  59.  
  60. proc_info Form;
  61.  
  62. bool pick_active = false;
  63.  
  64. #define ZOOM_IN 22
  65. #define ZOOM_OUT 23
  66. more_less_box zoom = { NULL, NULL, 11, 1, 40, ZOOM_IN, ZOOM_OUT, "Zoom" };
  67.  
  68. dword palette_colors[] = {
  69. 0x330000,0x331900,0x333300,0x193300,0x003300,0x003319,0x003333,0x001933,0x000033,0x190033,0x330033,0x330019,0x000000,
  70. 0x660000,0x663300,0x666600,0x336600,0x006600,0x006633,0x006666,0x003366,0x000066,0x330066,0x660066,0x660033,0x202020,
  71. 0x990000,0x994C00,0x999900,0x4C9900,0x009900,0x00994C,0x009999,0x004C99,0x000099,0x4C0099,0x990099,0x99004C,0x404040,
  72. 0xCC0000,0xCC6600,0xCCCC00,0x66CC00,0x00CC00,0x00CC66,0x00CCCC,0x0066CC,0x0000CC,0x6600CC,0xCC00CC,0xCC0066,0x606060,
  73. 0xFF0000,0xFF8000,0xFFFF00,0x80FF00,0x00FF00,0x00FF80,0x00FFFF,0x0080FF,0x0000FF,0x7F00FF,0xFF00FF,0xFF007F,0x808080,
  74. 0xFF3333,0xFF9933,0xFFFF33,0x99FF33,0x33FF33,0x33FF99,0x33FFFF,0x3399FF,0x3333FF,0x9933FF,0xFF33FF,0xFF3399,0xA0A0A0,
  75. 0xFF6666,0xFFB266,0xFFFF66,0xB2FF66,0x66FF66,0x66FFB2,0x66FFFF,0x66B2FF,0x6666FF,0xB266FF,0xFF66FF,0xFF66B2,0xC0C0C0,
  76. 0xFF9999,0xFFCC99,0xFFFF99,0xCCFF99,0x99FF99,0x99FFCC,0x99FFFF,0x99CCFF,0x9999FF,0xCC99FF,0xFF99FF,0xFF99CC,0xE0E0E0,
  77. 0xFFCCCC,0xFFE5CC,0xFFFFCC,0xE5FFCC,0xCCFFCC,0xCCFFE5,0xCCFFFF,0xCCE5FF,0xCCCCFF,0xE5CCFF,0xFFCCFF,0xFFCCE5,0xFFFFFF   
  78. };
  79.  
  80. _colors image;
  81.  
  82. libimg_image open_image;
  83.  
  84. //===================================================//
  85. //                                                   //
  86. //                       CODE                        //
  87. //                                                   //
  88. //===================================================//
  89.  
  90. void main()
  91. {
  92.         word btn;
  93.  
  94.         load_dll(libio,  #libio_init,  1);
  95.         load_dll(libimg, #libimg_init, 1);
  96.         Libimg_LoadImage(#skin, "/sys/icons16.png");
  97.  
  98.         CreateCanvas();
  99.  
  100.         /*
  101.         if (param) {
  102.                 Libimg_LoadImage(#open_image, #param);
  103.                 if (open_image.w==32) && (open_image.h==32) {
  104.                         memmov(#image.mas, open_image.image, 32*32*3);
  105.                 }
  106.                 else {
  107.                         notify("'Error: image format is unacceptable.\nOnly images created in IconEditor (BMP, 32x32x16b) can be opened!' -E");
  108.                 }
  109.         }
  110.         */
  111.  
  112.         SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
  113.  
  114.         loop() switch(WaitEvent())
  115.         {
  116.                 case evMouse:
  117.                         mouse.get();
  118.                         if (pick_active) EventPickColor(mouse.lkm, mouse.pkm);
  119.                         else {
  120.                                 if (mouse.x>canvas.x) && (mouse.y>canvas.y)
  121.                                 && (mouse.y<canvas.y+canvas.h) && (mouse.x<canvas.x+canvas.w)
  122.                                 {
  123.                                         if (mouse.lkm) image.set_pixel(mouse.y-canvas.y/zoom.value,
  124.                                                 mouse.x-canvas.x/zoom.value, active_color_1);
  125.                                         if (mouse.pkm) image.set_pixel(mouse.y-canvas.y/zoom.value,
  126.                                                 mouse.x-canvas.x/zoom.value, active_color_2);
  127.                                         DrawCanvas();
  128.                                 }
  129.                         }
  130.                         break;
  131.  
  132.                 case evButton:
  133.                         btn = GetButtonID();
  134.                         switch(btn)
  135.                         {
  136.                                 case BTN_NEW:
  137.                                         CreateCanvas();
  138.                                         DrawCanvas();
  139.                                         break;
  140.                                 case BTN_OPEN:
  141.                                         RunProgram("/sys/lod", sprintf(#param, "*bmp* %s",#program_path));
  142.                                         break;
  143.                                 case BTN_SAVE:
  144.                                         EventSave();
  145.                                         break;
  146.                                 case BTN_MOVE_LEFT:
  147.                                         image.move(MOVE_LEFT);
  148.                                         DrawCanvas();
  149.                                         break;
  150.                                 case BTN_MOVE_RIGHT:
  151.                                         image.move(MOVE_RIGHT);
  152.                                         DrawCanvas();
  153.                                         break;
  154.                                 case BTN_MOVE_UP:
  155.                                         image.move(MOVE_UP);
  156.                                         DrawCanvas();
  157.                                         break;
  158.                                 case BTN_MOVE_DOWN:
  159.                                         image.move(MOVE_DOWN);
  160.                                         DrawCanvas();
  161.                                         break;
  162.                                 case BTN_FLIP_VER:
  163.                                         image.move(FLIP_VER);
  164.                                         DrawCanvas();
  165.                                         break;
  166.                                 case BTN_FLIP_HOR:
  167.                                         image.move(FLIP_HOR);
  168.                                         DrawCanvas();
  169.                                         break;
  170.                                 case CLOSE_BTN:
  171.                                         ExitProcess();
  172.                                 case BTN_PICK:
  173.                                         EventPickActivate();
  174.                                         break;
  175.                         }              
  176.                         if (btn >= BTN_PALETTE_COLOR_MAS) && (btn < BTN_PALETTE_COLOR_MAS+PALLETE_SIZE)
  177.                         {
  178.                                 if (mouse.lkm) active_color_1 = palette_colors[btn-BTN_PALETTE_COLOR_MAS];
  179.                                 if (mouse.pkm) active_color_2 = palette_colors[btn-BTN_PALETTE_COLOR_MAS];
  180.                                 DrawActiveColor(NULL);
  181.                         }
  182.                         if (zoom.click(btn)) DrawEditArea();
  183.                         break;
  184.          
  185.                 case evKey:
  186.                         GetKeys();
  187.                         if (key_scancode == SCAN_CODE_ESC) pick_active=false;
  188.                         if (key_scancode == SCAN_CODE_KEY_I) EventPickActivate();
  189.                         break;
  190.                  
  191.                 case evReDraw:
  192.                         draw_window();
  193.                         break;
  194.         }
  195. }
  196.  
  197. void DrawToolbarButton(dword _id, _x, _icon_n)
  198. {
  199.         DrawWideRectangle(_x, 4, 24, 24, 4, 0xFFFfff);
  200.         DefineHiddenButton(_x, 4, 23, 23, _id);
  201.         img_draw stdcall(skin.image, _x+4, 8, 16, 16, 0, _icon_n*16);
  202. }
  203.  
  204. void DrawStatusBar()
  205. {
  206.         zoom.y = wrapper.y + wrapper.h + 6;
  207.         zoom.x = wrapper.x;
  208.         zoom.draw();
  209.  
  210.         sprintf(#param,"Canvas: %ix%i", image.rows, image.columns);
  211.         WriteText(wrapper.x+wrapper.w-calc(strlen(#param)*8), zoom.y+2, 0x90, system.color.work_text, #param);
  212. }
  213.  
  214. void draw_window()
  215. {
  216.         #define TB_ICON_PADDING 26
  217.         incn tx;
  218.         system.color.get();
  219.         DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x33, system.color.work, T_TITLE, 0);
  220.         GetProcessInfo(#Form, SelfInfo);
  221.         right_bar.x = Form.cwidth - right_bar.w;
  222.  
  223.         tx.n = wrapper.x - TB_ICON_PADDING;
  224.         DrawToolbarButton(BTN_NEW,    tx.inc(TB_ICON_PADDING), 2); //not implemented
  225.         DrawToolbarButton(BTN_OPEN,   tx.inc(TB_ICON_PADDING), 0); //not implemented
  226.         DrawToolbarButton(BTN_SAVE,   tx.inc(TB_ICON_PADDING), 5);
  227.         DrawToolbarButton(BTN_MOVE_LEFT,  tx.inc(TB_ICON_PADDING+8),   30);
  228.         DrawToolbarButton(BTN_MOVE_RIGHT, tx.inc(TB_ICON_PADDING),   31);
  229.         DrawToolbarButton(BTN_MOVE_UP,    tx.inc(TB_ICON_PADDING),   32);
  230.         DrawToolbarButton(BTN_MOVE_DOWN,  tx.inc(TB_ICON_PADDING),   33);
  231.        
  232.         DrawToolbarButton(BTN_FLIP_HOR,   tx.inc(TB_ICON_PADDING+8), 34); //not implemented
  233.         DrawToolbarButton(BTN_FLIP_VER,   tx.inc(TB_ICON_PADDING),   35); //not implemented
  234.         DrawToolbarButton(BTN_ROTATE_LEFT,   tx.inc(TB_ICON_PADDING), 36); //not implemented
  235.         DrawToolbarButton(BTN_ROTATE_RIGHT,  tx.inc(TB_ICON_PADDING), 37); //not implemented
  236.  
  237.         DrawToolbarButton(BTN_PICK,   tx.inc(TB_ICON_PADDING+8), 38);
  238.  
  239.         DrawEditArea();
  240.  
  241.         DrawDefaultColors(right_bar.x, right_bar.y);
  242.         DrawActiveColor(right_bar.y + 200);
  243.  
  244.         DrawStatusBar();
  245. }
  246.  
  247. void DrawEditArea()
  248. {
  249.         dword color1=0xC0C0C0;
  250.         int left_padding;
  251.         int top_padding;
  252.  
  253.         wrapper.w = Form.cwidth - right_bar.w - 30;
  254.         wrapper.h = Form.cheight - TOOLBAR_H - 35;
  255.  
  256.         //canvas{
  257.         canvas.w = image.columns * zoom.value;
  258.         canvas.h = image.rows * zoom.value;
  259.         if (canvas.w+2 > wrapper.w) || (canvas.h+2 > wrapper.h) {
  260.                 zoom.value--;
  261.                 DrawEditArea();
  262.                 return;
  263.         }
  264.         canvas.x = -zoom.value*image.columns+wrapper.w/2 + wrapper.x;
  265.         canvas.y = -zoom.value*image.rows+wrapper.h/2 + wrapper.y;
  266.         DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080);
  267.         DrawCanvas();
  268.         //}
  269.  
  270.         left_padding = canvas.x-wrapper.x-1;
  271.         top_padding = canvas.y-wrapper.y-1;
  272.  
  273.         DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w+1, wrapper.h+1, system.color.work_graph);
  274.  
  275.         if (left_padding>0)
  276.         {
  277.                 DrawBar(wrapper.x, wrapper.y, wrapper.w, top_padding, color1); //top
  278.                 DrawBar(wrapper.x, wrapper.y+wrapper.h-top_padding, wrapper.w, top_padding, color1); //bottom
  279.         }
  280.         if (top_padding>0)
  281.         {
  282.                 DrawBar(wrapper.x, wrapper.y+top_padding, left_padding, wrapper.h-top_padding-top_padding, color1); //left
  283.                 DrawBar(wrapper.x+wrapper.w-left_padding, wrapper.y+top_padding, left_padding, wrapper.h-top_padding-top_padding, color1); //right
  284.         }
  285. }
  286.  
  287. void DrawActiveColor(dword iny)
  288. {
  289.         static dword outy;
  290.         if (iny != NULL) outy = iny;
  291.         DrawBar(right_bar.x, outy, 20, 20, active_color_1);
  292.         sprintf(#param, "%A", active_color_1);
  293.         EDI = system.color.work;
  294.         WriteText(right_bar.x + 30, outy + 3, 0xD0, system.color.work_text, #param+4);
  295.  
  296.         DrawBar(right_bar.x+110, outy, 20, 20, active_color_2);
  297.         sprintf(#param, "%A", active_color_2);
  298.         EDI = system.color.work;
  299.         WriteText(right_bar.x+110 + 30, outy + 3, 0xD0, system.color.work_text, #param+4);     
  300. }
  301.  
  302. void DrawDefaultColors(dword _x, _y)
  303. {
  304.         int r, c, i;
  305.         int cellw = 20;
  306.  
  307.         i = 0;
  308.         for (r = 0; r < 9; r++)
  309.         {
  310.                 for (c = 0; c < 13; c++)
  311.                 {
  312.                         DrawBar(c*cellw + _x, r*cellw + _y, cellw, cellw, palette_colors[PALLETE_SIZE-i]);
  313.                         DefineHiddenButton(c*cellw + _x, r*cellw + _y, cellw-1, cellw-1, BTN_PALETTE_COLOR_MAS+PALLETE_SIZE-i);
  314.                         i++;
  315.                 }
  316.         }
  317.         DrawRectangle(_x-1, _y-1, c*cellw+1, r*cellw+1, system.color.work_light);
  318.         DrawRectangle(_x-2, _y-2, c*cellw+3, r*cellw+3, system.color.work_dark);
  319. }
  320.  
  321.  
  322. //===================================================//
  323. //                                                   //
  324. //                      EVENTS                       //
  325. //                                                   //
  326. //===================================================//
  327.  
  328. void EventPickActivate()
  329. {
  330.         SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE);
  331.         pick_active = true;
  332. }
  333.  
  334. void EventPickColor(dword lkm_status, pkm_status)
  335. {
  336.         active_color_1 = GetPixelColorFromScreen(mouse.x + Form.left + 5, mouse.y + Form.top + skin_height);
  337.         DrawActiveColor(NULL);
  338.         if (mouse.down) && (mouse.key&MOUSE_LEFT) {
  339.                 pick_active = false;
  340.                 SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
  341.         }
  342. }
  343.  
  344. dword bmp_32x32x16_header[] = FROM "bmp32x32header";
  345. void EventSave()
  346. {
  347.         char save_buf[3126];
  348.         dword image_buf = image.get_image();
  349.         memmov(#save_buf, #bmp_32x32x16_header, sizeof(bmp_32x32x16_header));
  350.         memmov(#save_buf+sizeof(bmp_32x32x16_header), image_buf, sizeof(save_buf)-sizeof(bmp_32x32x16_header));
  351.         if (WriteFile(sizeof(save_buf), #save_buf, "/rd/1/saved_image.bmp")==0)
  352.         {
  353.                 notify("'File saved as /rd/1/saved_image.bmp' -O");
  354.         }
  355.         else {
  356.                 notify("'Error saving BPM file, probably not enought space on ramdisk!' -E");
  357.         }
  358. }
  359.  
  360. void CreateCanvas()
  361. {
  362.         int i;
  363.         image.columns = 32;
  364.         image.rows = 32;
  365.         for (i = 0; i < image.columns*image.rows; i++) image.mas[i]=0xBFCAD2;
  366. }
  367.  
  368. void DrawCanvas()
  369. {
  370.         int r, c;
  371.         for (r = 0; r < image.rows; r++)
  372.         {
  373.                 for (c = 0; c < image.columns; c++)
  374.                 {
  375.                         DrawBar(c*zoom.value + canvas.x, r*zoom.value + canvas.y,
  376.                                 zoom.value, zoom.value, image.get_pixel(r, c));
  377.                 }
  378.         }
  379. }