示例#1
0
    /*
     * Creates all cubes in position, including a blank letter centered in
     * the middle of each cube.  Initially the cubes are not highlighted.
     */
    static void setupLetterCubes() {
        double lineWidth = 2;
        double cubeSize = gState.cubeSize - lineWidth;
        double cubeY = gState.board.y + BOARD_BORDER/2;
        for (int row = 0; row < gState.rowCount; row++) {
            double cubeX = gState.board.x + BOARD_BORDER/2;
            for (int col = 0; col < gState.columnCount; col++) {
                // display the letter cubes as rounded rectangles
                double cubeRoundRadius = gState.cubeSize/5;
                GRoundRect* rect = new GRoundRect(cubeX, cubeY, cubeSize,
                                                  cubeSize, cubeRoundRadius * 2);
                rect->setLineWidth(lineWidth);
                rect->setColor("Black");
                rect->setFilled(true);
                rect->setFillColor(DIE_COLOR);
                gwp->add(rect);
                letterCubes[row][col].rect = rect;

                // draw the letter on the cube
                GLabel* label = new GLabel("M");   // start as "M" for getWidth
                label->setFont(CUBE_FONT + "-" + integerToString(int(gState.fontSize)));
                label->setColor(LETTER_COLOR);
                label->setLocation(cubeX + gState.cubeSize/2 - label->getWidth()/2,
                                   cubeY + gState.cubeSize/2 + 0.4 * gState.fontSize);
                label->setLabel(" ");
                gwp->add(label);
                letterCubes[row][col].label = label;
                cubeX += (int) cubeSize + lineWidth;
            }
            cubeY += (int) cubeSize + lineWidth;
        }
    }
void test_contains_and_getBounds() {
    bool useCompounds = false;
    int x0 = 350;
    int y0 = 300;
    Map<string, GObject*> shapeMap;
    GOval *oval = new GOval(x0, y0, 200, 100);
    GRoundRect *roundRect = new GRoundRect(x0, y0, 200, 100, 300);
    roundRect->setLineWidth(20);
    G3DRect *rect3d = new G3DRect(x0, y0, 200, 100, true);
    //rect3d->setLineWidth(5);
    rect3d->setFillColor("green");
    rect3d->setFilled(true);
    GPolygon *poly = new GPolygon;
    poly->addVertex(0, 0);
    poly->addEdge(200, 100);
    poly->addEdge(-200, 0);
    poly->setLocation(x0, y0);
    GPolygon *cpoly = new GPolygon;
    cpoly->addVertex(0, 0);
    cpoly->addEdge(200, 100);
    cpoly->addEdge(0, -100);
    cpoly->addEdge(-200, 100);
    cpoly->setLocation(x0, y0);
    GRect *rect = new GRect(x0, y0, 200, 100);
    GLine *line = new GLine(x0, y0, x0 + 200, y0 + 100);
    GLabel *label = new GLabel("Ostromantus", x0, y0);
    GArc *arc = new GArc(x0, y0, 350, 350, 0, 90);
    //arc->setLineWidth(5);
    arc->setColor("#44000000");
    GArc *filledArc = new GArc(x0, y0, 350, 100, 45, 225);
    filledArc->setFillColor("#88e0e0e0");
    filledArc->setFilled(true);
    GCompound *comp1 = new GCompound;
    comp1->setLocation(x0, y0);
    comp1->add(new GLabel("compound", 0, 15));
    GRect *bgRect1 = new GRect(0, 0);
    gw->add(bgRect1);
    bgRect1->setFillColor("#55dddddd");
    bgRect1->setFilled(true);
    GImage *image = new GImage("homer-transparent.png");
    image->setLocation(x0, y0);
    GCompound *comp = new GCompound;
    comp->setLocation(x0, y0);
    GRect *compRect = new GRect(20, 20, 100, 100);
    GOval *compOval = new GOval(90, 90, 150, 70);
    comp->add(compRect);
    comp->add(compOval);
    GButton *button = new GButton("Testo");
    button->setSize(200, 100);
    button->setLocation(x0, y0);
    shapeMap.put("oval", oval);
    shapeMap.put("rounded rectangle", roundRect);
    shapeMap.put("3D rectangle", rect3d);
    shapeMap.put("polygon", poly);
    shapeMap.put("crazy polygon", cpoly);
    shapeMap.put("rectangle", rect);
    shapeMap.put("line", line);
    shapeMap.put("arc", arc);
    shapeMap.put("filled arc", filledArc);
    shapeMap.put("label", label);
    shapeMap.put("image", image);
    shapeMap.put("compound", comp);
    shapeMap.put("button", button);

    GObject *currObj;
    GChooser *ch = new GChooser;
    ch->setActionCommand("chooser");
    ch->addItem("oval");
    ch->addItem("rounded rectangle");
    ch->addItem(("3D rectangle"));
    ch->addItem("polygon");
    ch->addItem("crazy polygon");
    ch->addItem("rectangle");
    ch->addItem("line");
    ch->addItem("arc");
    ch->addItem("filled arc");
    ch->addItem("label");
    ch->addItem("image");
    ch->addItem("compound");
    ch->addItem("button");
    ch->setSelectedItem("rectangle");
    currObj = rect;

    GButton *endButton = new GButton("End test");
    GButton *fillButton = new GButton("Auto-fill");
    GButton *rotateButton = new GButton("Rotate");
    GButton *scaleButton = new GButton("Scale");

    GCheckBox *compCheckbox = new GCheckBox("compounds");
    compCheckbox->setActionCommand("compounds");
    gw->addToRegion(compCheckbox, "north");
    gw->addToRegion(ch, "north");
    gw->addToRegion(rotateButton, "north");
    gw->addToRegion(scaleButton, "north");
    gw->addToRegion(fillButton, "north");
    gw->addToRegion(endButton, "north");

    while (true) {
        GEvent e = waitForEvent(ACTION_EVENT | MOUSE_EVENT);
        if (!e.isValid())
            continue;
        if (e.getEventClass() == ACTION_EVENT) {
            if (((GActionEvent) e).getActionCommand() == "End test")
                break;
            if (((GActionEvent) e).getActionCommand() == "compounds") {
                bgRect1->setVisible(compCheckbox->isSelected());
                useCompounds = compCheckbox->isSelected();
            }
            if (((GActionEvent) e).getActionCommand() == "Testo") {
                GPoint pt = button->getLocation();
                button->setLocation(pt.getX()-button->getWidth()-10, pt.getY());
                pause(1000);
                button->setLocation(pt);
            }
            if (((GActionEvent) e).getActionCommand() == "Auto-fill") {
                GRectangle bds = currObj->getBounds();
                int xmin = bds.getX();
                int ymin = bds.getY();
                int xmax = bds.getX() + bds.getWidth();
                int ymax = bds.getY() + bds.getHeight();
                int dx = useCompounds ? comp1->getX(): 0;
                int dy = useCompounds ? comp1->getY(): 0;
                for (int y = ymin; y < ymax; y+=1)
                    for (int x = xmin; x < xmax; x+=1) {
                        if (currObj->contains(x, y)) {
                            gw->setColor("red");
                            gw->fillOval(x + dx, y + dy, 1, 1);
                        } else {
                            gw->setColor("green");
                            gw->fillOval(x + dx, y + dy, 1, 1);
                        }
                    }
            }

            if (((GActionEvent) e).getActionCommand() == "Rotate") {
                currObj->rotate(45);
                if (useCompounds) {
                    bgRect1->setBounds(comp1->getBounds());
                }
            }
            if (((GActionEvent) e).getActionCommand() == "Scale") {
                currObj->scale(1.2, 0.8);
                if (useCompounds) {
                    bgRect1->setBounds(comp1->getBounds());
                }
            }
            if (((GActionEvent) e).getActionCommand() == "chooser") {
                string shape = ch->getSelectedItem();
                if (useCompounds) {
                    comp1->remove(currObj);
                    gw->remove(comp1);
                } else {
                    gw->remove(currObj);
                }
                gw->setColor("white");
                gw->fillRect(0, 0, gw->getCanvasWidth(), gw->getCanvasHeight());
                //drawGrid();
                gw->setColor("black");
                currObj = shapeMap.get(shape);
                if (useCompounds) {
                    gw->add(comp1);
                    comp1->add(currObj, 50, 50);
                    bgRect1->setBounds(comp1->getBounds());
                } else {
                    gw->add(currObj);
                }
                gw->drawOval(currObj->getX()-2, currObj->getY()-2, 4, 4);
            }
        } else if (e.getEventType() == MOUSE_CLICKED) {
            double x = ((GMouseEvent) e).getX();
            double y = ((GMouseEvent) e).getY();
            if (currObj->contains(x, y)) {
                gw->setColor("red");
                gw->fillOval(x, y, 1, 1);
            }
        }
    }
}