Subversion Repositories Kolibri OS

Rev

Rev 7268 | Rev 7273 | 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 SelectTool_drawBufferToImage(int insert_x, int insert_y) {
  41.         dword r, c;
  42.         dword insert_to_x, insert_to_y;
  43.        
  44.                 insert_to_x = math.min(insert_x + selection.columns - 1, image.columns-1);
  45.                 insert_to_y = math.min(insert_y + selection.rows - 1, image.rows-1);
  46.  
  47.                 for (r = insert_y; r <= insert_to_y; r++) {
  48.                         for (c = insert_x; c <= insert_to_x; c++) {
  49.                                         image.set_pixel(r, c, selection.get_pixel(r - insert_y, c - insert_x) );
  50.                         }
  51.                 }      
  52. }
  53.  
  54. void ApplySelectionToImage() {
  55.         if (STATE_SELECTED != selection_state) return;
  56.  
  57.         SelectTool_drawBufferToImage(selection_start_x, selection_start_y);
  58.  
  59.         selection_pivot_x = -1;
  60.         selection_pivot_y = -1;
  61.        
  62.         actionsHistory.saveCurrentState();
  63.         DrawCanvas();
  64. }
  65.  
  66. bool is_selection_moving() {
  67.         if (STATE_SELECTED == selection_state) return true;
  68.         return false;
  69. }
  70.  
  71. void reset_selection() {
  72.         ApplySelectionToImage();
  73.        
  74.         selection_start_x = -1;
  75.         selection_start_y = -1;
  76.         selection_end_x = -1;
  77.         selection_end_y = -1;  
  78. }
  79.  
  80. void SelectTool_activate() {
  81.         reset_selection();
  82.         selection_state = STATE_INACTIVE;
  83. }
  84.  
  85. void SelectTool_deactivate() {
  86.         ApplySelectionToImage();
  87.         selection_state = STATE_INACTIVE;
  88. }
  89.  
  90. bool SelectTool_pointInSelection(int x, int y) {
  91.         if (x >= selection_start_x) && (x <= selection_end_x) && (y >= selection_start_y) && (y <= selection_end_y)
  92.                 return true;
  93.         else
  94.                 return false;
  95. }
  96.  
  97.  
  98. void SelectTool_copyToBuffer() {
  99.         dword r, c;
  100.  
  101.         selection_state = STATE_SELECTED;
  102.         selection.rows = selection_end_y - selection_start_y + 1;
  103.         selection.columns = selection_end_x - selection_start_x + 1;
  104.  
  105.         for (r = selection_start_y; r <= selection_end_y; r++) {
  106.                 for (c = selection_start_x; c <= selection_end_x; c++) {
  107.                         selection.set_pixel(r - selection_start_y, c - selection_start_x, image.get_pixel(r, c) );
  108.                 }
  109.         }
  110. }
  111.  
  112. void SelectTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
  113.         int dx, dy, m_x, m_y;
  114.        
  115.         m_x = TO_CANVAS_X(mouseX);
  116.         m_y = TO_CANVAS_Y(mouseY);
  117.  
  118.         if (mouse.down) && (canvas.hovered())
  119.         {
  120.                 if (selection_start_x != -1) && (SelectTool_pointInSelection(m_x, m_y)) {
  121.                         if (selection_pivot_x == -1) {
  122.                                 selection_pivot_x = m_x;
  123.                                 selection_pivot_y = m_y;
  124.                                
  125.                                 GetKeys();
  126.                                 if ( (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) ) {
  127.                                         DrawBarIcon(selection_start_x, selection_start_y, selection_end_x,
  128.                                                 selection_end_y, color2, TOIMAGE);
  129.                                 }
  130.  
  131.                                 selection_state = STATE_SELECTED;
  132.                         }
  133.                 }
  134.                 else { 
  135.                         reset_selection();
  136.                         selection_state = STATE_CHOSING;
  137.                 }
  138.         }
  139.  
  140.         if (selection_pivot_x != -1) {
  141.                 dx = m_x - selection_pivot_x;
  142.                 dy = m_y - selection_pivot_y;
  143.  
  144.                 if (selection_start_x + dx < 0)
  145.                         dx = selection_start_x;
  146.                
  147.                 if (selection_end_x + dx >= image.columns)
  148.                         dx = image.columns-1 - selection_end_x;
  149.                
  150.                 if (selection_start_y + dy < 0)
  151.                         dy = selection_start_y;
  152.                
  153.                 if (selection_end_y + dy >= image.rows)
  154.                         dy = image.rows-1 - selection_end_y;
  155.                
  156.                 selection_start_x += dx;
  157.                 selection_end_x += dx;
  158.                
  159.                 selection_start_y += dy;
  160.                 selection_end_y += dy;
  161.                
  162.                 selection_pivot_x += dx;
  163.                 selection_pivot_y += dy;
  164.                
  165.                 DrawCanvas();
  166.         }
  167.        
  168.         if (STATE_CHOSING == selection_state)
  169.         {
  170.                 mouseX = math.in(mouseX, canvas.x, canvas.x+canvas.w-zoom.value);
  171.                 mouseY = math.in(mouseY, canvas.y, canvas.y+canvas.h-zoom.value);
  172.  
  173.                 if (mouse.key) {
  174.                         selection_end_x = TO_CANVAS_X(mouseX);
  175.                         selection_end_y = TO_CANVAS_Y(mouseY);
  176.  
  177.                         if ((selection_start_x < 0) || (selection_start_y < 0)) {
  178.                                 selection_start_x = TO_CANVAS_X(mouseX);
  179.                                 selection_start_y = TO_CANVAS_Y(mouseY);
  180.                         }
  181.                         else {
  182.                                 DrawCanvas();
  183.                         }
  184.  
  185.                 }
  186.                
  187.                 if (mouse.up) {                
  188.                         SelectTool_normalizeSelection();
  189.                         SelectTool_copyToBuffer();
  190.                 }
  191.         }
  192.        
  193.         if (mouse.up) {
  194.                 if (selection_pivot_x != -1) {
  195.                         selection_pivot_x = -1;
  196.                         selection_pivot_y = -1;
  197.                 }
  198.         }
  199. }
  200.  
  201. void SelectTool_onKeyEvent(dword keycode) {
  202.         dword r, c;
  203.  
  204.         if (SCAN_CODE_DEL == keycode) {
  205.                 selection_start_x = -1;
  206.                 selection_start_y = -1;
  207.                 selection_end_x = -1;
  208.                 selection_end_y = -1;
  209.                 selection_state = STATE_INACTIVE;
  210.                 DrawCanvas();
  211.         }
  212.  
  213.         if (SCAN_CODE_ESC == keycode) {
  214.                 reset_selection();
  215.                 DrawCanvas();
  216.         }
  217.  
  218.         if (SCAN_CODE_KEY_V == keycode) {
  219.                 if (STATE_SELECTED == selection_state) {
  220.  
  221.                         selection_start_x = 0;
  222.                         selection_start_y = 0;
  223.                         selection_end_x = selection.columns - 1;                       
  224.                         selection_end_y = selection.rows - 1;
  225.                        
  226.                         DrawCanvas();
  227.                 }
  228.         }
  229. }
  230.  
  231. void SelectTool_onCanvasDraw()
  232. {      
  233.         #define SELECTION_COLOR 0xAAE5EF
  234.         int p1x, p1y, p2x, p2y, r, c, old_color, new_color;
  235.  
  236.         if ((selection_start_x >= 0) && (selection_start_y >= 0) && (selection_end_x >= 0) && (selection_end_y >= 0)) {
  237.  
  238.                 p1x = math.min(selection_start_x, selection_end_x);
  239.                 p2x = math.max(selection_start_x, selection_end_x);
  240.  
  241.                 p1y = math.min(selection_start_y, selection_end_y);
  242.                 p2y = math.max(selection_start_y, selection_end_y);
  243.  
  244.                 for (r = p1y; r <= p2y; r++) {
  245.                         for (c = p1x; c <= p2x; c++) {
  246.                                 image.pixel_state.set_drawable_state(r, c, false);
  247.                                
  248.                                 if (STATE_SELECTED == selection_state) && (SelectTool_pointInSelection(c, r)) {
  249.                                         old_color = selection.get_pixel(r - selection_start_y, c - selection_start_x);
  250.                                 }
  251.                                 else {
  252.                                         old_color = image.get_pixel(r, c);
  253.                                 }
  254.                                
  255.                                 new_color = MixColors(old_color, SELECTION_COLOR, 64);
  256.  
  257.                                 DrawCanvasPixel(r, c, new_color);
  258.                         }
  259.                 }
  260.         }      
  261. }
  262.  
  263.  
  264.