void test_front_back() { string colors[3] = {"blue", "red", "green"}; const int objWidth = 100; const int objHeight = 100; const int x0 = 100; const int y0 = 100; const int dx = 40; const int dy = 0; GButton *forward = new GButton("Cycle Forward"); GButton *backward = new GButton("Cycle Backward"); GButton *stepUp = new GButton("Backmost +1"); GButton *stepDown = new GButton("Frontmost -1"); Vector<GObject *> objects; gw->addToRegion(forward, "south"); gw->addToRegion(backward, "south"); gw->addToRegion(stepUp, "south"); gw->addToRegion(stepDown, "south"); GCompound *compound = new GCompound(); for (int i = 0; i < 3; i++) { GOval *oval = new GOval(x0 + i*dx, y0 + i*dy, objWidth, objHeight); if (i == 1) oval->setLocation(oval->getLocation().getX(), oval->getLocation().getY() + 50); oval->setFilled(true); oval->setFillColor(colors[i]); objects.add(oval); compound->add(oval); } gw->add(new GLabel("Click in window to end test", 10, 30)); gw->add(compound); while(true) { GEvent e = waitForEvent(ACTION_EVENT | CLICK_EVENT); if (e.getEventType() == MOUSE_CLICKED) break; else if (e.getEventType() == ACTION_PERFORMED) { string cmd = ((GActionEvent) e).getActionCommand(); if (cmd == "Cycle Forward") cycleObjects(objects); if (cmd == "Cycle Backward") cycleObjects(objects, false); if (cmd == "Backmost +1") moveBottomUp(objects); if (cmd == "Frontmost -1") moveTopDown(objects); } } }
bool MarbleGraphics::makeMove(const Move move){ GImage * movingMarble = marbles[move.startRow][move.startCol]; int jumpedRow = move.startRow+(move.endRow-move.startRow)/2; int jumpedCol = move.startCol+(move.endCol-move.startCol)/2; GImage * jumpedMarble = marbles[jumpedRow][jumpedCol]; if (movingMarble == NULL || jumpedMarble == NULL){ return false; } //Move empty spot from end to start of move GOval* endSpot = spaces[move.endRow][move.endCol]; double tempX = endSpot->getX(); double tempY = endSpot->getY(); endSpot->setLocation(movingMarble->getX(), movingMarble->getY()); spaces[move.startRow][move.startCol] = endSpot; spaces[move.endRow][move.endCol] = NULL; spaceCoords[endSpot] = Coord(move.startRow, move.startCol); //Move image at start location to image at end location movingMarble->setLocation(tempX, tempY); marbles[move.endRow][move.endCol] = movingMarble; marbles[move.startRow][move.startCol] = NULL; marbleCoords[movingMarble] = Coord(move.endRow, move.endCol); //Delete marble for jumped space and create empty space there instead marbles[jumpedRow][jumpedCol] = NULL; marbleCoords.remove(jumpedMarble); remove(jumpedMarble); delete jumpedMarble; GOval* jumpedSpace = new GOval(kMarbleDimension, kMarbleDimension); spaces[jumpedRow][jumpedCol] = jumpedSpace; spaceCoords[jumpedSpace] = Coord(jumpedRow, jumpedCol); add(jumpedSpace, jumpedCol*(kMarbleDimension+kMarbleSpacingWidth)+kMarbleSpacingWidth, jumpedRow*(kMarbleDimension+kMarbleSpacingWidth)+kMarbleSpacingWidth); return true; }