Subversion Repositories Kolibri OS

Rev

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

  1. //===================================================//
  2. //                                                   //
  3. //                       DATA                        //
  4. //                                                   //
  5. //===================================================//
  6.  
  7. _image selection;
  8.  
  9. enum {
  10.         STATE_INACTIVE=0,
  11.         STATE_CHOSING=1,
  12.         STATE_SELECTED=2
  13. };
  14. int selection_state = STATE_INACTIVE;
  15.  
  16. int selection_start_x = -1;
  17. int selection_start_y = -1;
  18. int selection_end_x = -1;
  19. int selection_end_y = -1;
  20.  
  21. int selection_pivot_x = -1;
  22. int selection_pivot_y = -1;
  23.  
  24. //===================================================//
  25. //                                                   //
  26. //                       CODE                        //
  27. //                                                   //
  28. //===================================================//
  29.  
  30. void SelectTool_normalizeSelection() {
  31.         // Restructuring of the selection coordinates
  32.         if (selection_end_x < selection_start_x) {
  33.                 selection_start_x >< selection_end_x;
  34.         }
  35.         if (selection_end_y < selection_start_y) {
  36.                 selection_end_y >< selection_start_y;
  37.         }
  38. }
  39.  
  40. void reset_selection_moving() {
  41.         if (STATE_SELECTED == selection_state) {
  42.                 SelectTool_drawBuffer(selection_start_x, selection_start_y, 1);
  43.  
  44.                 selection_pivot_x = -1;
  45.                 selection_pivot_y = -1;
  46.                
  47.                 selection_state = STATE_SELECTED;
  48.                
  49.                 actionsHistory.saveCurrentState();
  50.                 DrawCanvas();
  51.         }
  52. }
  53.  
  54. bool is_selection_moving() {
  55.         if (STATE_SELECTED == selection_state) return true;
  56.         return false;
  57. }
  58.  
  59. void reset_selection() {
  60.         reset_selection_moving();
  61.        
  62.         selection_start_x = -1;
  63.         selection_start_y = -1;
  64.         selection_end_x = -1;
  65.         selection_end_y = -1;  
  66. }
  67.  
  68. void SelectTool_activate() {
  69.         reset_selection();
  70.         selection_state = STATE_INACTIVE;
  71. }
  72.  
  73. void SelectTool_deactivate() {
  74.         reset_selection_moving();
  75.         selection_state = STATE_INACTIVE;
  76. }
  77.  
  78. bool SelectTool_pointInSelection(int x, int y) {
  79.         if (x >= selection_start_x) && (x <= selection_end_x) && (y >= selection_start_y) && (y <= selection_end_y)
  80.                 return true;
  81.         else
  82.                 return false;
  83. }
  84.  
  85.  
  86. void SelectTool_copyToBuffer() {
  87.         dword r, c;
  88.  
  89.         selection_state = STATE_SELECTED;
  90.         selection.rows = selection_end_y - selection_start_y + 1;
  91.         selection.columns = selection_end_x - selection_start_x + 1;
  92.  
  93.         for (r = selection_start_y; r <= selection_end_y; r++) {
  94.                 for (c = selection_start_x; c <= selection_end_x; c++) {
  95.                         selection.set_pixel(r - selection_start_y, c - selection_start_x, image.get_pixel(r, c) );
  96.                 }
  97.         }
  98. }
  99.  
  100. void SelectTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
  101.         int dx, dy, m_x, m_y;
  102.        
  103.         m_x = TO_CANVAS_X(mouseX);
  104.         m_y = TO_CANVAS_Y(mouseY);
  105.  
  106.         if (mouse.down) && (canvas.hovered())
  107.         {
  108.                 if (selection_start_x != -1) && (SelectTool_pointInSelection(m_x, m_y)) {
  109.                         if (selection_pivot_x == -1) {
  110.                                 selection_pivot_x = m_x;
  111.                                 selection_pivot_y = m_y;
  112.                                
  113.                                 GetKeys();
  114.                                
  115.                                 if ( !(key_modifier&KEY_LSHIFT) ) {
  116.                                         DrawBarIcon(selection_start_x, selection_start_y, selection_end_x, selection_end_y, color2, TOIMAGE);
  117.                                 }
  118.  
  119.                                 selection_state = STATE_SELECTED;
  120.                         }
  121.                 }
  122.                 else { 
  123.                         reset_selection();
  124.                         selection_state = STATE_CHOSING;
  125.                 }
  126.         }
  127.  
  128.         if (selection_pivot_x != -1) {
  129.                 dx = m_x - selection_pivot_x;
  130.                 dy = m_y - selection_pivot_y;
  131.  
  132.                 if (selection_start_x + dx < 0)
  133.                         dx = selection_start_x;
  134.                
  135.                 if (selection_end_x + dx >= image.columns)
  136.                         dx = image.columns-1 - selection_end_x;
  137.                
  138.                 if (selection_start_y + dy < 0)
  139.                         dy = selection_start_y;
  140.                
  141.                 if (selection_end_y + dy >= image.rows)
  142.                         dy = image.rows-1 - selection_end_y;
  143.                
  144.                
  145.                 selection_start_x += dx;
  146.                 selection_end_x += dx;
  147.                
  148.                 selection_start_y += dy;
  149.                 selection_end_y += dy;
  150.                
  151.                 selection_pivot_x += dx;
  152.                 selection_pivot_y += dy;
  153.                
  154.                 DrawCanvas();
  155.         }
  156.        
  157.         if (STATE_CHOSING == selection_state)
  158.         {
  159.                 if (mouseX>canvas.x+canvas.w-zoom.value) mouseX = canvas.x+canvas.w-zoom.value;
  160.                 if (mouseY>canvas.y+canvas.h-zoom.value) mouseY = canvas.y+canvas.h-zoom.value;
  161.  
  162.                 if (mouseX<canvas.x) mouseX = canvas.x;
  163.                 if (mouseY<canvas.y) mouseY = canvas.y;
  164.  
  165.                 if (mouse.key) {
  166.                         selection_end_x = TO_CANVAS_X(mouseX);
  167.                         selection_end_y = TO_CANVAS_Y(mouseY);
  168.  
  169.                         if ((selection_start_x < 0) || (selection_start_y < 0)) {
  170.                                 selection_start_x = TO_CANVAS_X(mouseX);
  171.                                 selection_start_y = TO_CANVAS_Y(mouseY);
  172.                         }
  173.                         else {
  174.                                 DrawCanvas();
  175.  
  176.                                 /**if ((calc(TO_CANVAS_X(mouseX)) != selection_end_x)
  177.                                         || (calc(TO_CANVAS_Y(mouseY)) != selection_end_y))
  178.                                 {
  179.                                         DrawCanvas();
  180.                                 }*/
  181.                         }
  182.  
  183.                 }
  184.                
  185.                 if (mouse.up) {                
  186.                         SelectTool_normalizeSelection();
  187.                         SelectTool_copyToBuffer();
  188.                 }
  189.         }
  190.        
  191.         if (mouse.up) {
  192.                 if (selection_pivot_x != -1) {
  193.                         selection_pivot_x = -1;
  194.                         selection_pivot_y = -1;
  195.                 }
  196.         }
  197. }
  198.  
  199. void SelectTool_onCanvasDraw() {       
  200.         if ((selection_start_x >= 0) && (selection_start_y >= 0) && (selection_end_x >= 0) && (selection_end_y >= 0)) {
  201.                 DrawSelection();
  202.         }      
  203. }
  204.  
  205. void SelectTool_drawBuffer(int insert_x, int insert_y, int target) {
  206.         dword color;
  207.         dword r, c;
  208.         dword insert_to_x, insert_to_y;
  209.        
  210.         if (STATE_INACTIVE != selection_state) {
  211.                 insert_to_x = insert_x + selection.columns - 1;
  212.                        
  213.                 if (insert_to_x >= image.columns)
  214.                         insert_to_x = image.columns-1;
  215.  
  216.                 insert_to_y = insert_y + selection.rows - 1;
  217.                        
  218.                 if (insert_to_y >= image.rows)
  219.                         insert_to_y = image.rows-1;
  220.  
  221.                 for (r = insert_y; r <= insert_to_y; r++) {
  222.                         for (c = insert_x; c <= insert_to_x; c++) {
  223.  
  224.                                         color = selection.get_pixel(r - insert_y, c - insert_x);
  225.                                        
  226.                                         if (TOIMAGE == target)
  227.                                                 image.set_pixel(r, c, color);
  228.                                         else
  229.                                                 DrawCanvasPixel(r, c, color);
  230.                         }
  231.                 }      
  232.         }      
  233. }
  234.  
  235. void SelectTool_onKeyEvent(dword keycode) {
  236.         dword r, c;
  237.  
  238.         if (keycode == SCAN_CODE_KEY_V) {
  239.                 if (STATE_SELECTED == selection_state) {
  240.                         reset_selection();
  241.                        
  242.                         selection_state = STATE_SELECTED;
  243.                         selection_start_x = 0;
  244.                         selection_end_x = selection.columns - 1;
  245.                        
  246.                         selection_start_y = 0;
  247.                         selection_end_y = selection.rows - 1;
  248.                        
  249.                         DrawCanvas();
  250.                 }
  251.         }
  252. }
  253.  
  254. void DrawSelection() {
  255.         #define SELECTION_COLOR 0xAAE5EF
  256.         int p1x, p1y, p2x, p2y, r, c, old_color, new_color;
  257.  
  258.         if (selection_start_x <= selection_end_x) {
  259.                 p1x = selection_start_x;
  260.                 p2x = selection_end_x;
  261.         }
  262.         else {
  263.                 p1x = selection_end_x;
  264.                 p2x = selection_start_x;
  265.         }
  266.  
  267.         if (selection_start_y <= selection_end_y) {
  268.                 p2y = selection_start_y;
  269.                 p1y = selection_end_y;
  270.         }
  271.         else {
  272.                 p2y = selection_end_y;
  273.                 p1y = selection_start_y;
  274.         }
  275.  
  276.         for (r = p1y; r >= p2y; r--) {
  277.                 for (c = p1x; c <= p2x; c++) {
  278.                         image.pixel_state.set_drawable_state(r, c, false);
  279.                        
  280.                         if (STATE_SELECTED == selection_state) && (SelectTool_pointInSelection(c, r)) {
  281.                                 old_color = selection.get_pixel(r - selection_start_y, c - selection_start_x);
  282.                         }
  283.                         else {
  284.                                 old_color = image.get_pixel(r, c);
  285.                         }
  286.                        
  287.                         new_color = MixColors(old_color, SELECTION_COLOR, 64);
  288.  
  289.                         DrawCanvasPixel(r, c, new_color);
  290.                 }
  291.         }
  292. }