/* Reacts to a mouse event in the window. */
static void processMouseEvent(State& state, GMouseEvent e) {
  switch (state.uiState) {
    /* Already have something drawn?  Clear it and pretend we're fresh. */
    case DRAWN:
      restoreWorldDisplay();
      state.uiState = FRESH;
      /* deliberate: don't break. */

    /* In a fresh state?  Try to select what the user clicked on. */
    case FRESH:
      if (registerClick(state.world, e.getX(), e.getY(),
                        state.worldType)) {
        state.uiState = MARKED;
      }
      break;

      /* Already marked?  Try to select what the user clicked on, then
       * try to find a path if it worked.
       */
    case MARKED:
      if (registerClick(state.world, e.getX(), e.getY(),
                        state.worldType)) {
				runSearch(state);
        state.uiState = DRAWN;
      }
  }
}
示例#2
0
/* STARTER CODE HELPER FUNCTION - DO NOT EDIT
 *
 * Waits for a mouse click in the GWindow and reports click location.
 *
 * When this function returns, row and col are set to the row and
 * column where a mouse click was detected.
 */
static void getMouseClickLocation(int &row, int &col) {
    GMouseEvent me;
    do {
        me = getNextEvent(MOUSE_EVENT);
    } while (me.getEventType() != MOUSE_CLICKED);
    row = me.getY();
    col = me.getX();
}
示例#3
0
GPoint getMouseClick() {
   while (true) {
      GMouseEvent e;
      waitForEvent(e);
      if (e.getEventType() == MOUSE_CLICKED) {
      	 return GPoint(e.getX(), e.getY());
      }
   }
   return GPoint();
}
示例#4
0
//mouse clicks, chooses column
int playerMoves (GWindow & gw) {
    while (true) {
        double spacer = gw.getWidth()/29;
        GMouseEvent e = waitForEvent();
        if (e.getEventType() == MOUSE_PRESSED) {
            double x = 0;
            for (int i = 1; i <= 7; i++) {
                x += spacer;
                if(e.getX() <= x + i*spacer*3) {
                    return i-1;
                }
            }
        }
    }
}
示例#5
0
bool MarbleGraphics::getNextUserMove(Move& move) {
    GMouseEvent me;
    bool startClick = true;
    while (true) {
        me = getNextEvent(MOUSE_EVENT);
        if (me.getEventType() == MOUSE_CLICKED) {
            GObject* obj = getGObjectAt(me.getX(),me.getY());
            if (obj == NULL) {
                startClick = true;
                errorNotification();
            }
            // User clicked in SOLVE button
            else if (obj->getType() == "GRoundRect" || obj->getType() == "GLabel"){
                return false;
            }
            // User clicked in MARBLE
            else if (obj->getType() == "GImage") {
                // First click = starting location
                if (startClick) {
                    move.startRow = marbleCoords[(GImage*)obj].row;
                    move.startCol = marbleCoords[(GImage*)obj].col;
                    //If click on visible marble (Flash it to show selection detected)
                    if(((GImage*)obj)->isVisible()){
                        ((GImage*) obj)->setVisible(false);
                        pause(50);
                        ((GImage*) obj)->setVisible(true);
                    }
                    startClick = false;
                }
            }
            //User clicked in empty valid space on playing board
            else if (obj->getType() == "GOval"){
                // Second click = ending location
                if (!startClick) {
                    move.endRow = spaceCoords[(GOval*)obj].row;
                    move.endCol = spaceCoords[(GOval*)obj].col;
                    return true;
                }
            }
        }
    }
    return false;
}
示例#6
0
void MarbleGraphics::clearBoard(){
    this->clear();
    for (GObject * obj : marbles){
        if (obj != NULL) {
            delete obj;
            obj = NULL;
        }
    }
    for (GObject * obj : spaces){
        if (obj != NULL) {
            delete obj;
            obj = NULL;
        }
    }
    marbleCoords.clear();
    spaceCoords.clear();

    GMouseEvent me;
    do{
        me = getNextEvent(MOUSE_EVENT);
    }
    while(me.getEventClass() != NULL_EVENT);
}
/*
 * (public function)
 * Reacts to a mouse event in the window.
 */
void processMouseEvent(GMouseEvent e) {
    if (e.getEventType() == MOUSE_CLICKED) {
        switch (state.uiState) {
        case DRAWN:
            // Already have something drawn?  Clear it and pretend we're fresh.
            restoreWorldDisplay();
            state.uiState = FRESH;
        // deliberate: don't break.

        case FRESH:
            // In a fresh state?  Try to select what the user clicked on.
            if (registerClick(state.world, e.getX(), e.getY(), state.worldType)) {
                state.uiState = MARKED;
            }
            break;

        case MARKED:
            // Already marked?  Try to select what the user clicked on, then
            // try to find a path if it worked.
            if (registerClick(state.world, e.getX(), e.getY(),
                              state.worldType)) {
                runSearch(state);
                state.uiState = DRAWN;
            }
        }
    } else if (e.getEventType() == MOUSE_MOVED) {
        // update display of current mouse row/col position to aid testing
        TBLoc loc = coordToLoc(e.getX(), e.getY());
        if (state.world.inBounds(loc.row, loc.col)) {
            string newPositionText = vertexName(loc.row, loc.col, state.world);
            if (newPositionText != gPositionFieldText) {
                gPositionFieldText = newPositionText;
                gPositionField->setText(newPositionText);
            }
        }
    }
}
示例#8
0
void pathfinderEventLoop() {
   while (true) {
      GMouseEvent e;
      waitForEvent(e);
      if (e.getEventType() == MOUSE_CLICKED) {
         int index = findButtonIndex(e.getX(), e.getY());
         if (index == -1) {
            GPoint pt = GPoint(e.getX(), e.getY());
            if (pt.getY() < WINDOW_HEIGHT && clickHook != NULL) {
               clickHook->apply(pt);
               repaint();
            }
         } else {
            buttons[index].callback->apply();
            repaint();
	 }
      }
   }
}