SimpleCxxLib
packageclass GMouseEvent : public GEvent
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 | |
Creates a GMouseEvent using the specified parameters. | |
Methods | |
Returns the graphics window in which this event occurred. | |
Returns the x coordinate at which the event occurred relative to the window origin at the upper left corner of the window. | |
Returns the y coordinate at which the event occurred relative to the window origin at the upper left corner of the window. | |
Converts the event to a human-readable representation of the event. |
GMouseEvent(EventType type, const GWindow & gw, double x, double y);
GMouseEvent
using the specified parameters.
Usage:
GMouseEvent mouseEvent(type, gw, x, y);
GWindow getGWindow() const;
Usage:
GWindow gw = e.getGWindow();
double getX() const;
Usage:
double x = getX();
double getY() const;
Usage:
double y = getY();
string toString() const;
Usage:
string str = e.toString();