Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. #include "mydrawing.h"
  2. #include "gcontext.h"
  3. #include "matrix.h"
  4. #include "Image.h"
  5. #include <iostream> // TODO: temporary?
  6.  
  7. // Constructor
  8. MyDrawing::MyDrawing() {
  9.     dragging = false;
  10.     x0 = x1 = y0 = y1 = 0;
  11.     color = GraphicsContext::CYAN;
  12.     // Default mode will be line
  13.     drawingMode = MyDrawing::DrawingMode::Rectangle;
  14.     return;
  15. }
  16.  
  17. MyDrawing::~MyDrawing() {
  18.     // TODO: make sure to delete image
  19.    
  20. }
  21.  
  22. void MyDrawing::paint(GraphicsContext *gc) {
  23.     // TODO: what am i supposed to do here??
  24.     // for fun, let’s draw a "fixed" shape in the middle of the screen
  25.     // it will only show up after an exposure
  26.     int middlex = gc->getWindowWidth() / 2;
  27.     int middley = gc->getWindowHeight() / 2;
  28.     gc->setColor(GraphicsContext::MAGENTA);
  29.     for (int yi = middley - 50; yi <= middley + 50; yi++) {
  30.         gc->drawLine(middlex - 50, yi, middlex + 50, yi);
  31.     }
  32.     gc->setColor(GraphicsContext::GREEN);
  33.     // redraw the line if requested
  34.     gc->drawLine(x0, y0, x1, y1);
  35.     return;
  36. }
  37.  
  38. void MyDrawing::mouseButtonDown(GraphicsContext * gc, unsigned int button,
  39.         int x, int y) {
  40.     // mouse button pushed down
  41.     // - clear context
  42.     // - set origin of new hypothetical line
  43.     // - set XOR mode for rubber-banding
  44.     // - draw shape in XOR mode.  Note, at this point, the hypothetical line is
  45.     //   degenerate (0 length), but need to do it for consistency
  46.    
  47.     // Originally, the hypothetical line is degenerate
  48.     x0 = x1 = x;
  49.     y0 = y1 = y;
  50.    
  51.     // For drawing and undrawing the shapes
  52.     gc->setMode(GraphicsContext::MODE_XOR);
  53.    
  54.     // Using the hypothetical segment, draw the shape
  55.     drawShape(gc);
  56.  
  57.     dragging = true;
  58.     return;
  59. }
  60.  
  61. void MyDrawing::mouseButtonUp(GraphicsContext * gc, unsigned int button, int x,
  62.         int y) {
  63.     if (dragging) {
  64.         // undraw old shape
  65.         drawShape(gc);
  66.        
  67.         // just in x and y here do not match x and y of last mouse move
  68.         x1 = x;
  69.         y1 = y;
  70.         // go back to COPY mode
  71.         gc->setMode(GraphicsContext::MODE_NORMAL);
  72.         // new shape drawn in copy mode
  73.         drawShape(gc);
  74.         // clear flag
  75.         dragging = false;
  76.     }
  77.     return;
  78. }
  79.  
  80. void MyDrawing::mouseMove(GraphicsContext * gc, int x, int y) {
  81.     if (dragging) {
  82.         // mouse moved - "undraw" old shape, then re-draw in new position
  83.         // will already be in XOR mode if dragging
  84.         // old shape undrawn
  85.         drawShape(gc);
  86.         // update
  87.         x1 = x;
  88.         y1 = y;
  89.         // new shape drawn
  90.         drawShape(gc);
  91.     }
  92.     return;
  93. }
  94.  
  95.  
  96. void MyDrawing::keyUp(GraphicsContext* gc, unsigned int keycode)
  97. {
  98.  
  99.     if (dragging)
  100.     {
  101.         // TODO: throw exception
  102.         std::cerr << "CANNOT PRESS KEY WHILE DRAGGING MAN" << std::endl;
  103.     }
  104.     std::cout << (char)keycode;
  105.     if ((char)keycode == 'n')
  106.     {
  107.         std::cout << std::endl;
  108.     }
  109. }
  110.  
  111. void MyDrawing::drawShape(GraphicsContext * gc) {
  112.     switch (drawingMode) {
  113.         case MyDrawing::DrawingMode::Line: {
  114.             gc->drawLine(x0, y0, x1, y1);
  115.         }
  116.         break;
  117.         case MyDrawing::DrawingMode::Triangle: {
  118.             drawTriangle(gc);
  119.         }
  120.         break;
  121.         case MyDrawing::DrawingMode::Rectangle: {
  122.             drawRectangle(gc);
  123.         }
  124.         break;
  125.         case MyDrawing::DrawingMode::Circle: {
  126.             drawCircle(gc);
  127.         }
  128.         break;
  129.         case MyDrawing::DrawingMode::Polygon: {
  130.             std::cout << "Draw me!" << std::endl; // TODO: please
  131.         }
  132.         break;
  133.         default:
  134.             // TODO: shouldn't reach here. throw exception?
  135.             std::cout << "Hello? How are you here??" << std::endl;
  136.     }
  137. }
  138.  
  139. void MyDrawing::drawTriangle(GraphicsContext * gc) {
  140.     // Define the triangle to be contained in the hypothetical box
  141. //  double dx = x1 - x0;
  142. //  double dy = y1 - y0;
  143.    
  144.     // Its origin will be halfway from p0 and p1
  145. //  matrix m(3,3);
  146. //  m[0][0] = x0;   m[0][1] = ; m[0][2] = ;
  147. //  m[1][0] = y1;   m[1][1] = ; m[1][2] = ;
  148. //  m[2][0] = 0;    m[2][1] = ; m[2][2] = ;
  149.    
  150.     // Draw the rectangle
  151. //  Rectangle rect(m, w, h, this->color);
  152. //  rect.draw(gc);
  153.  
  154.     std::cout << "I am Triangle-chan! Draw me!" << std::endl; // TODO: please
  155. }
  156.  
  157. void MyDrawing::drawRectangle(GraphicsContext *gc) {
  158.     // Define the rectangle to be the hypothetical box
  159.     double w = x1 - x0;
  160.     double h = y1 - y0;
  161.    
  162.     // Its origin will be halfway from p0 and p1
  163.     matrix m(3,1);
  164.     m[0][0] = w/2;
  165.     m[1][0] = h/2;
  166.     m[2][0] = 0;
  167.    
  168.     // Draw the rectangle
  169. //  Rectangle r(m, w, h, color);
  170. //  r.draw(gc);
  171. }
  172.  
  173. void MyDrawing::drawCircle(GraphicsContext * gc) {
  174.     std::cout << "I am Circle-tan! Draw me!" << std::endl; // TODO: please
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement