Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mydrawing.h"
- #include "gcontext.h"
- #include "matrix.h"
- #include "Image.h"
- #include <iostream> // TODO: temporary?
- // Constructor
- MyDrawing::MyDrawing() {
- dragging = false;
- x0 = x1 = y0 = y1 = 0;
- color = GraphicsContext::CYAN;
- // Default mode will be line
- drawingMode = MyDrawing::DrawingMode::Rectangle;
- return;
- }
- MyDrawing::~MyDrawing() {
- // TODO: make sure to delete image
- }
- void MyDrawing::paint(GraphicsContext *gc) {
- // TODO: what am i supposed to do here??
- // for fun, let’s draw a "fixed" shape in the middle of the screen
- // it will only show up after an exposure
- int middlex = gc->getWindowWidth() / 2;
- int middley = gc->getWindowHeight() / 2;
- gc->setColor(GraphicsContext::MAGENTA);
- for (int yi = middley - 50; yi <= middley + 50; yi++) {
- gc->drawLine(middlex - 50, yi, middlex + 50, yi);
- }
- gc->setColor(GraphicsContext::GREEN);
- // redraw the line if requested
- gc->drawLine(x0, y0, x1, y1);
- return;
- }
- void MyDrawing::mouseButtonDown(GraphicsContext * gc, unsigned int button,
- int x, int y) {
- // mouse button pushed down
- // - clear context
- // - set origin of new hypothetical line
- // - set XOR mode for rubber-banding
- // - draw shape in XOR mode. Note, at this point, the hypothetical line is
- // degenerate (0 length), but need to do it for consistency
- // Originally, the hypothetical line is degenerate
- x0 = x1 = x;
- y0 = y1 = y;
- // For drawing and undrawing the shapes
- gc->setMode(GraphicsContext::MODE_XOR);
- // Using the hypothetical segment, draw the shape
- drawShape(gc);
- dragging = true;
- return;
- }
- void MyDrawing::mouseButtonUp(GraphicsContext * gc, unsigned int button, int x,
- int y) {
- if (dragging) {
- // undraw old shape
- drawShape(gc);
- // just in x and y here do not match x and y of last mouse move
- x1 = x;
- y1 = y;
- // go back to COPY mode
- gc->setMode(GraphicsContext::MODE_NORMAL);
- // new shape drawn in copy mode
- drawShape(gc);
- // clear flag
- dragging = false;
- }
- return;
- }
- void MyDrawing::mouseMove(GraphicsContext * gc, int x, int y) {
- if (dragging) {
- // mouse moved - "undraw" old shape, then re-draw in new position
- // will already be in XOR mode if dragging
- // old shape undrawn
- drawShape(gc);
- // update
- x1 = x;
- y1 = y;
- // new shape drawn
- drawShape(gc);
- }
- return;
- }
- void MyDrawing::keyUp(GraphicsContext* gc, unsigned int keycode)
- {
- if (dragging)
- {
- // TODO: throw exception
- std::cerr << "CANNOT PRESS KEY WHILE DRAGGING MAN" << std::endl;
- }
- std::cout << (char)keycode;
- if ((char)keycode == 'n')
- {
- std::cout << std::endl;
- }
- }
- void MyDrawing::drawShape(GraphicsContext * gc) {
- switch (drawingMode) {
- case MyDrawing::DrawingMode::Line: {
- gc->drawLine(x0, y0, x1, y1);
- }
- break;
- case MyDrawing::DrawingMode::Triangle: {
- drawTriangle(gc);
- }
- break;
- case MyDrawing::DrawingMode::Rectangle: {
- drawRectangle(gc);
- }
- break;
- case MyDrawing::DrawingMode::Circle: {
- drawCircle(gc);
- }
- break;
- case MyDrawing::DrawingMode::Polygon: {
- std::cout << "Draw me!" << std::endl; // TODO: please
- }
- break;
- default:
- // TODO: shouldn't reach here. throw exception?
- std::cout << "Hello? How are you here??" << std::endl;
- }
- }
- void MyDrawing::drawTriangle(GraphicsContext * gc) {
- // Define the triangle to be contained in the hypothetical box
- // double dx = x1 - x0;
- // double dy = y1 - y0;
- // Its origin will be halfway from p0 and p1
- // matrix m(3,3);
- // m[0][0] = x0; m[0][1] = ; m[0][2] = ;
- // m[1][0] = y1; m[1][1] = ; m[1][2] = ;
- // m[2][0] = 0; m[2][1] = ; m[2][2] = ;
- // Draw the rectangle
- // Rectangle rect(m, w, h, this->color);
- // rect.draw(gc);
- std::cout << "I am Triangle-chan! Draw me!" << std::endl; // TODO: please
- }
- void MyDrawing::drawRectangle(GraphicsContext *gc) {
- // Define the rectangle to be the hypothetical box
- double w = x1 - x0;
- double h = y1 - y0;
- // Its origin will be halfway from p0 and p1
- matrix m(3,1);
- m[0][0] = w/2;
- m[1][0] = h/2;
- m[2][0] = 0;
- // Draw the rectangle
- // Rectangle r(m, w, h, color);
- // r.draw(gc);
- }
- void MyDrawing::drawCircle(GraphicsContext * gc) {
- std::cout << "I am Circle-tan! Draw me!" << std::endl; // TODO: please
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement