Subversion Repositories Kolibri OS

Rev

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

  1. // Line tool
  2. struct SimpleFigureTool_State {
  3.         int startX, startY;
  4.         int lastTempPosX, lastTempPosY;
  5. };
  6.  
  7. enum {
  8.         TOOL_LINE_STATE,
  9.         TOOL_RECT_STATE
  10. };
  11.  
  12. SimpleFigureTool_State figTool;
  13.  
  14. void SimpleFigureTool_Reset() {
  15.         figTool.startX = -1;
  16.         figTool.startY = -1;
  17.         figTool.lastTempPosX = -1;
  18.         figTool.lastTempPosY = -1;
  19. }
  20.  
  21. int mouseX_last;
  22. int mouseY_last;
  23. bool first_click_in_canvas = false;
  24.  
  25. void SimpleFigureTool_onMouseEvent(int mouseX, int mouseY, int lkm, int pkm) {
  26.         int x1, y1, x2, y2;
  27.  
  28.         if (mouse.down) && (canvas.hovered()) first_click_in_canvas = true;
  29.         if (first_click_in_canvas)
  30.         {
  31.                 if (mouseX>canvas.x+canvas.w-zoom.value) mouseX = canvas.x+canvas.w-zoom.value;
  32.                 if (mouseY>canvas.y+canvas.h-zoom.value) mouseY = canvas.y+canvas.h-zoom.value;
  33.                 if (mouseX<canvas.x) mouseX = canvas.x;
  34.                 if (mouseY<canvas.y) mouseY = canvas.y;
  35.  
  36.                 if (mouse.key) {
  37.                         if ((figTool.startX < 0)
  38.                         || (figTool.startY < 0)) {
  39.                                 figTool.startX = mouseX;
  40.                                 figTool.startY = mouseY;
  41.                         }
  42.                         else {
  43.                                 if ((calc(mouseX - canvas.x/zoom.value) != figTool.lastTempPosX)
  44.                                         || (calc(mouseY - canvas.y/zoom.value) != figTool.lastTempPosY))
  45.                                 {
  46.                                         DrawCanvas();
  47.                                 }
  48.                         }
  49.                         mouseX_last = mouseX;
  50.                         mouseY_last = mouseY;
  51.                 }
  52.                 if (mouse.up) {
  53.                         if ((figTool.startX >= 0)
  54.                         && (figTool.startY >= 0)) {
  55.  
  56.                                 x1 = figTool.startX - canvas.x/zoom.value;
  57.                                 y1 = figTool.startY - canvas.y/zoom.value;
  58.                                 x2 = mouseX - canvas.x/zoom.value;
  59.                                 y2 = mouseY - canvas.y/zoom.value;
  60.  
  61.                                 // Draw line from start position to current position
  62.                                 if (currentTool == TOOL_LINE) {
  63.                                         DrawLineIcon(x1, y1, x2, y2, tool_color, TOIMAGE);
  64.                                 }
  65.                                 else if (currentTool == TOOL_RECT) {
  66.                                         DrawRectangleIcon(x1, y1, x2, y2, tool_color, TOIMAGE);
  67.                                 }
  68.                                 else if (currentTool == TOOL_BAR) {
  69.                                         DrawBarIcon(x1, y1, x2, y2, tool_color, TOIMAGE);
  70.                                 }
  71.  
  72.                                 DrawCanvas();
  73.  
  74.                                 actionsHistory.saveCurrentState();
  75.  
  76.                                 // Reset start position
  77.                                 figTool.startX = -1;
  78.                                 figTool.startY = -1;
  79.  
  80.                                 first_click_in_canvas = false;
  81.                         }
  82.                 }
  83.         }
  84. }
  85.  
  86. void SimpleFigureTool_onCanvasDraw() {
  87.         int x1, y1, x2, y2;
  88.         if ((figTool.startX >= 0)
  89.         && (figTool.startY >= 0) && (mouse.key)) {
  90.  
  91.                 x1 = figTool.startX - canvas.x/zoom.value;
  92.                 y1 = figTool.startY - canvas.y/zoom.value;
  93.                 x2 = mouseX_last - canvas.x/zoom.value;
  94.                 y2 = mouseY_last - canvas.y/zoom.value;
  95.  
  96.                 if (currentTool == TOOL_LINE)
  97.                         DrawLineIcon(x1, y1, x2, y2, tool_color, TOCANVAS);
  98.                 else if (currentTool == TOOL_RECT)
  99.                         DrawRectangleIcon(x1, y1, x2, y2, tool_color, TOCANVAS);
  100.                 else if (currentTool == TOOL_BAR)
  101.                         DrawBarIcon(x1, y1, x2, y2, tool_color, TOCANVAS);
  102.  
  103.                 figTool.lastTempPosX = mouseX_last - canvas.x/zoom.value;
  104.                 figTool.lastTempPosY = mouseY_last - canvas.y/zoom.value;
  105.         }
  106. }
  107.  
  108.  
  109. //===================================================//
  110. //                                                   //
  111. //                      DRAWs                        //
  112. //                                                   //
  113. //===================================================//
  114.  
  115.  
  116.  
  117. // target - TOMAGE, TOCANVAS
  118. void DrawLineIcon(int x1, int y1, int x2, int y2, dword color, int target) {
  119.         int dx, dy, signX, signY, error, error2;
  120.  
  121.         if (TOIMAGE == target) {
  122.                 image.draw_line(x1, y1, x2, y2, color);
  123.                 return;
  124.         }
  125.        
  126.         dx = x2 - x1;
  127.  
  128.         if (dx < 0)
  129.                 dx = -dx;
  130.    
  131.         dy = y2 - y1;
  132.  
  133.         if (dy < 0)
  134.                 dy = -dy;
  135.    
  136.         if (x1 < x2)
  137.                 signX = 1;
  138.         else
  139.                 signX = -1;
  140.    
  141.         if (y1 < y2)
  142.                 signY = 1;
  143.         else
  144.                 signY = -1;
  145.    
  146.         error = dx - dy;
  147.  
  148.         DrawCanvasPixel(y2, x2, color);
  149.         image.pixel_state.set_drawable_state(y2, x2, false);
  150.  
  151.         while((x1 != x2) || (y1 != y2))
  152.         {
  153.                 DrawCanvasPixel(y1, x1, color);
  154.  
  155.                 image.pixel_state.set_drawable_state(y1, x1, false);
  156.                
  157.                 error2 = error * 2;
  158.  
  159.                 if(error2 > calc(-dy))
  160.                 {
  161.                         error -= dy;
  162.                         x1 += signX;
  163.                 }
  164.  
  165.                 if(error2 < dx)
  166.                 {
  167.                         error += dx;
  168.                         y1 += signY;
  169.                 }
  170.         }
  171. }
  172.  
  173. void DrawRectangleIcon(int x1, int y1, int x2, int y2, dword color, int target) {
  174.         DrawLineIcon(x1, y1, x2, y1, color, target);
  175.         DrawLineIcon(x2, y1, x2, y2, color, target);
  176.         DrawLineIcon(x2, y2, x1, y2, color, target);
  177.         DrawLineIcon(x1, y2, x1, y1, color, target);
  178. }
  179.  
  180. void DrawBarIcon(int x1, int y1, int x2, int y2, dword color, int target) {
  181.         signed signY;
  182.         if (y1 < y2)
  183.                 signY = 1;
  184.         else
  185.                 signY = -1;
  186.  
  187.         while (y1 != y2+signY) {
  188.                 DrawLineIcon(x1, y1, x2, y1, color, target);
  189.                 y1 += signY;
  190.         }
  191. }