Subversion Repositories Kolibri OS

Rev

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

  1. //===================================================//
  2. //                                                   //
  3. //                    SELECTION                      //
  4. //                                                   //
  5. //===================================================//
  6.  
  7. enum {
  8.         STATE_INACTIVE=0,
  9.         STATE_CHOSING=1,
  10.         STATE_SELECTED=2
  11. };
  12.  
  13. struct _selection
  14. {
  15.         _image buf;
  16.         int state;
  17.  
  18.         int start_x;
  19.         int start_y;
  20.         int end_x;
  21.         int end_y;
  22.  
  23.         int pivot_x;
  24.         int pivot_y;
  25.  
  26.         void reset();
  27.         bool is_point_in_selection();
  28.         void copy_to_buf();
  29.         void move_to_point();
  30.         void apply_to_image();
  31. } selection;
  32.  
  33. void _selection::reset() {     
  34.         start_x = -1;
  35.         start_y = -1;
  36.         end_x = -1;
  37.         end_y = -1;
  38.         pivot_x = -1;
  39.         pivot_y = -1;
  40.         state = STATE_INACTIVE;
  41. }
  42.  
  43. bool _selection::is_point_in_selection(int x, int y) {
  44.         if (x >= start_x) && (x <= end_x)
  45.         && (y >= start_y) && (y <= end_y)
  46.                 return true;
  47.         else
  48.                 return false;
  49. }
  50.  
  51. void _selection::copy_to_buf() {
  52.         dword r, c;
  53.  
  54.         // Normalize: Restructuring of the selection coordinates
  55.         if (end_x < start_x) {
  56.                 start_x >< end_x;
  57.         }
  58.         if (end_y < start_y) {
  59.                 end_y >< start_y;
  60.         }
  61.  
  62.         state = STATE_SELECTED;
  63.         buf.rows = end_y - start_y + 1;
  64.         buf.columns = end_x - start_x + 1;
  65.  
  66.         for (r = start_y; r <= end_y; r++) {
  67.                 for (c = start_x; c <= end_x; c++) {
  68.                         buf.set_pixel(r - start_y, c - start_x, image.get_pixel(r, c) );
  69.                 }
  70.         }
  71. }
  72.  
  73. void _selection::move_to_point(int _new_x, _new_y)
  74. {
  75.         start_x = _new_x;
  76.         start_y = _new_y;
  77.         end_x = buf.columns - 1 + start_x;
  78.         end_y = buf.rows - 1 + start_y;
  79. }
  80.  
  81. void _selection::apply_to_image() {
  82.         dword r, c;
  83.         for (r = 0; r <= buf.rows - 1; r++) {
  84.                 for (c = 0; c <= buf.columns - 1; c++) {
  85.                                 image.set_pixel(r + start_y, c + start_x, buf.get_pixel(r, c) );
  86.                 }
  87.         }
  88. }
  89.  
  90. //===================================================//
  91. //                                                   //
  92. //                      EVENTS                       //
  93. //                                                   //
  94. //===================================================//
  95.  
  96. void ApplySelectionToImage() {
  97.  
  98.         if (STATE_SELECTED != selection.state) return;
  99.  
  100.         selection.apply_to_image();
  101.         selection.reset();
  102.        
  103.         actionsHistory.saveCurrentState();
  104.         DrawCanvas();
  105. }
  106.  
  107. bool is_selection_moving() {
  108.         if (STATE_SELECTED == selection.state) return true;
  109.         return false;
  110. }
  111.  
  112. void SelectTool_activate() {
  113.         ApplySelectionToImage();
  114.         selection.reset();
  115. }
  116.  
  117. void SelectTool_deactivate() {
  118.         ApplySelectionToImage();
  119.         selection.reset();
  120. }
  121.  
  122. void SelectTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
  123.         int dx, dy, m_x, m_y;
  124.        
  125.         m_x = TO_CANVAS_X(mouseX);
  126.         m_y = TO_CANVAS_Y(mouseY);
  127.  
  128.         if (mouse.down) && (canvas.hovered())
  129.         {
  130.                 if (selection.start_x != -1) && (selection.is_point_in_selection(m_x, m_y)) {
  131.                         if (selection.pivot_x == -1) {
  132.                                 selection.pivot_x = m_x;
  133.                                 selection.pivot_y = m_y;
  134.                                
  135.                                 GetKeys();
  136.                                 if ( (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) ) {
  137.                                         DrawBarIcon(selection.start_x, selection.start_y, selection.end_x,
  138.                                                 selection.end_y, color2, TOIMAGE);
  139.                                 }
  140.  
  141.                                 selection.state = STATE_SELECTED;
  142.                         }
  143.                 }
  144.                 else { 
  145.                         ApplySelectionToImage();
  146.                         selection.state = STATE_CHOSING;
  147.                 }
  148.         }
  149.  
  150.         //Drag selection
  151.         if (STATE_SELECTED == selection.state)
  152.         {      
  153.                 if (selection.pivot_x != -1) {
  154.                         dx = m_x - selection.pivot_x;
  155.                         dy = m_y - selection.pivot_y;
  156.  
  157.                         if (selection.start_x + dx < 0)
  158.                                 dx = selection.start_x;
  159.                        
  160.                         if (selection.end_x + dx >= image.columns)
  161.                                 dx = image.columns-1 - selection.end_x;
  162.                        
  163.                         if (selection.start_y + dy < 0)
  164.                                 dy = selection.start_y;
  165.                        
  166.                         if (selection.end_y + dy >= image.rows)
  167.                                 dy = image.rows-1 - selection.end_y;
  168.  
  169.                         selection.move_to_point(selection.start_x + dx, selection.start_y + dy);
  170.                        
  171.                         selection.pivot_x += dx;
  172.                         selection.pivot_y += dy;
  173.                        
  174.                         DrawCanvas();
  175.                 }
  176.                 if (mouse.up) {
  177.                         if (selection.pivot_x != -1) {
  178.                                 selection.pivot_x = -1;
  179.                                 selection.pivot_y = -1;
  180.                         }
  181.                 }
  182.         }
  183.        
  184.         if (STATE_CHOSING == selection.state)
  185.         {
  186.                 mouseX = math.in(mouseX, canvas.x, canvas.x+canvas.w-zoom.value);
  187.                 mouseY = math.in(mouseY, canvas.y, canvas.y+canvas.h-zoom.value);
  188.  
  189.                 if (mouse.key) {
  190.                         selection.end_x = TO_CANVAS_X(mouseX);
  191.                         selection.end_y = TO_CANVAS_Y(mouseY);
  192.  
  193.                         if ((selection.start_x < 0) || (selection.start_y < 0)) {
  194.                                 selection.start_x = TO_CANVAS_X(mouseX);
  195.                                 selection.start_y = TO_CANVAS_Y(mouseY);
  196.                         }
  197.                         else {
  198.                                 DrawCanvas();
  199.                         }
  200.                 }
  201.                
  202.                 if (mouse.up) {
  203.                         selection.copy_to_buf();
  204.                 }
  205.         }
  206. }
  207.  
  208. void SelectTool_onKeyEvent(dword keycode) {
  209.         dword r, c;
  210.  
  211.         if (SCAN_CODE_DEL == keycode) {
  212.                 DrawBarIcon(selection.start_x, selection.start_y, selection.end_x, selection.end_y, color2, TOIMAGE);
  213.                 selection.reset();
  214.                 DrawCanvas();
  215.         }
  216.  
  217.         if (SCAN_CODE_ESC == keycode) {
  218.                 ApplySelectionToImage();
  219.                 DrawCanvas();
  220.         }
  221.  
  222.         if (SCAN_CODE_KEY_V == keycode) {
  223.                 if (STATE_SELECTED == selection.state)
  224.                 {
  225.                         selection.move_to_point(0, 0); 
  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)
  237.         //&& (selection.end_x >= 0) && (selection.end_y >= 0)) {
  238.  
  239.         if (STATE_SELECTED == selection.state)
  240.         || (STATE_CHOSING == selection.state)
  241.         {
  242.                 p1x = math.min(selection.start_x, selection.end_x);
  243.                 p2x = math.max(selection.start_x, selection.end_x);
  244.  
  245.                 p1y = math.min(selection.start_y, selection.end_y);
  246.                 p2y = math.max(selection.start_y, selection.end_y);
  247.  
  248.                 for (r = p1y; r <= p2y; r++) {
  249.                         for (c = p1x; c <= p2x; c++) {
  250.                                 image.pixel_state.set_drawable_state(r, c, false);
  251.                                
  252.                                 if (STATE_SELECTED == selection.state) && (selection.is_point_in_selection(c, r)) {
  253.                                         old_color = selection.buf.get_pixel(r - selection.start_y, c - selection.start_x);
  254.                                 }
  255.                                 else {
  256.                                         old_color = image.get_pixel(r, c);
  257.                                 }
  258.                                
  259.                                 new_color = MixColors(old_color, SELECTION_COLOR, 64);
  260.  
  261.                                 DrawCanvasPixel(r, c, new_color);
  262.                         }
  263.                 }
  264.         }      
  265. }
  266.  
  267.  
  268.