The SimpleCxxLib package


#include "gevents.h"

class GMouseEvent : public GEvent

This event subclass represents a mouse event. Each mouse event records the event type (MOUSE_PRESSED, MOUSE_RELEASED, MOUSE_CLICKED, MOUSE_MOVED, MOUSE_DRAGGED) along with the coordinates of the event. Clicking the mouse generates three events in the following order: MOUSE_PRESSED, MOUSE_RELEASED, MOUSE_CLICKED.

As an example, the following program uses mouse events to let the user draw rectangles on the graphics window. The only complexity in this code is the use of the library functions min and abs to ensure that the dimensions of the rectangle are positive.

   int main() {
      GWindow gw;
      cout << "This program lets the user draw rectangles." << endl;
      GRect *rect;
      double startX;
      double startY;
      while (true) {
         GMouseEvent e = waitForEvent();
         if (e.getEventType() == MOUSE_PRESSED) {
            startX = e.getX();
            startY = e.getY();
            rect = new GRect(startX, startY, 0, 0);
            rect->setFilled(true);
            gw.add(rect);
         } else if (e.getEventType() == MOUSE_DRAGGED) {
            double x = min(e.getX(), startX);
            double y = min(e.getY(), startY);
            double width = abs(e.getX() - startX);
            double height = abs(e.getY() - startY);
            rect->setBounds(x, y, width, height);
         }
      }
   }
Constructor
GMouseEvent(type, gw, x, y) Creates a GMouseEvent using the specified parameters.
Methods
getGWindow() Returns the graphics window in which this event occurred.
getX() Returns the x coordinate at which the event occurred relative to the window origin at the upper left corner of the window.
getY() Returns the y coordinate at which the event occurred relative to the window origin at the upper left corner of the window.
toString() Converts the event to a human-readable representation of the event.

Constructor detail


GMouseEvent(EventType type, const GWindow & gw, double x, double y);
Creates a GMouseEvent using the specified parameters.

Usage:

GMouseEvent mouseEvent(type, gw, x, y);

Method detail


GWindow getGWindow() const;
Returns the graphics window in which this event occurred.

Usage:

GWindow gw = e.getGWindow();

double getX() const;
Returns the x coordinate at which the event occurred relative to the window origin at the upper left corner of the window.

Usage:

double x = getX();

double getY() const;
Returns the y coordinate at which the event occurred relative to the window origin at the upper left corner of the window.

Usage:

double y = getY();

string toString() const;
Converts the event to a human-readable representation of the event.

Usage:

string str = e.toString();