protected Menu(MenuItem... items) {
    // menu in the center of the screen
    CCSize s = Director.sharedDirector().winSize();

    //        CCRect r = [[UIApplication sharedApplication] statusBarFrame];
    //        if(Director.sharedDirector().landscape())
    //            s.height -= r.size.width;
    //        else
    //            s.height -= r.size.height;
    setPosition(s.width / 2, s.height / 2);

    setRelativeAnchorPoint(false);
    setAnchorPoint(0.5f, 0.5f);
    setContentSize(s.width, s.height);

    isTouchEnabled_ = true;

    int z = 0;
    for (int i = 0; i < items.length; i++) {
      MenuItem item = items[i];
      addChild(item, z);
      z++;
    }
    //	    alignItemsVertically();

    selectedItem = null;
    state = MenuState.kMenuStateWaiting;
  }
  /** align items in rows of columns */
  public void alignItemsInColumns(int columns[]) {
    ArrayList<Integer> rows = new ArrayList<Integer>();
    for (int i = 0; i < columns.length; i++) {
      rows.add(columns[i]);
    }

    int height = -5;
    int row = 0, rowHeight = 0, columnsOccupied = 0, rowColumns;
    for (int i = 0; i < children.size(); i++) {
      MenuItem item = (MenuItem) children.get(i);
      assert row < rows.size() : "Too many menu items for the amount of rows/columns.";

      rowColumns = rows.get(row);
      assert rowColumns != 0 : "Can't have zero columns on a row";

      rowHeight = (int) Math.max(rowHeight, item.getHeight());
      ++columnsOccupied;

      if (columnsOccupied >= rowColumns) {
        height += rowHeight + 5;

        columnsOccupied = 0;
        rowHeight = 0;
        ++row;
      }
    }

    assert columnsOccupied != 0 : "Too many rows/columns for available menu items.";

    CCSize winSize = Director.sharedDirector().winSize();

    row = 0;
    rowHeight = 0;
    rowColumns = 0;
    float w = 0, x = 0, y = height / 2;
    for (int i = 0; i < children.size(); i++) {
      MenuItem item = (MenuItem) children.get(i);
      if (rowColumns == 0) {
        rowColumns = rows.get(row);
        w = winSize.width / (1 + rowColumns);
        x = w;
      }

      rowHeight = Math.max(rowHeight, (int) item.getHeight());
      item.setPosition(x - winSize.width / 2, y - item.getHeight() / 2);

      x += w + 10;
      ++columnsOccupied;

      if (columnsOccupied >= rowColumns) {
        y -= rowHeight + 5;

        columnsOccupied = 0;
        rowColumns = 0;
        rowHeight = 0;
        ++row;
      }
    }
  }
  @Override
  public void onStart() {
    super.onStart();

    // attach the OpenGL view to a window
    Director.sharedDirector().attachInView(mGLSurfaceView);

    // set landscape mode
    Director.sharedDirector().setLandscape(false);

    // show FPS
    Director.sharedDirector().setDisplayFPS(true);

    // frames per second
    Director.sharedDirector().setAnimationInterval(1.0f / 60);

    Scene scene = Scene.node();
    scene.addChild(nextAction());

    // Make the Scene active
    Director.sharedDirector().runWithScene(scene);
  }
  private MenuItem itemForTouch(MotionEvent event) {
    CCPoint touchLocation = Director.sharedDirector().convertCoordinate(event.getX(), event.getY());

    for (int i = 0; i < children.size(); i++) {
      MenuItem item = (MenuItem) children.get(i);
      CCPoint local = item.convertToNodeSpace(touchLocation.x, touchLocation.y);
      CCRect r = item.rect();
      r.origin = CCPoint.zero();
      if (CCRect.containsPoint(r, local)) {
        return item;
      }
    }
    return null;
  }
    public AtlasDemo() {
      float x, y;

      CCSize s = Director.sharedDirector().winSize();

      Label label = Label.label(title(), "DroidSans", 32);
      addChild(label, 1);
      label.setPosition(s.width / 2, s.height / 2);

      MenuItemImage item1 = MenuItemImage.item("b1.png", "b2.png", this, "backCallback");
      MenuItemImage item2 = MenuItemImage.item("r1.png", "r2.png", this, "restartCallback");
      MenuItemImage item3 = MenuItemImage.item("f1.png", "f2.png", this, "nextCallback");

      Menu menu = Menu.menu(item1, item2, item3);

      menu.setPosition(0, 0);
      item1.setPosition(s.width / 2 - 100, 30);
      item2.setPosition(s.width / 2, 30);
      item3.setPosition(s.width / 2 + 100, 30);
      addChild(menu, 1);
    }
  @Override
  public void onResume() {
    super.onResume();

    Director.sharedDirector().resume();
  }
  @Override
  public void onPause() {
    super.onPause();

    Director.sharedDirector().pause();
  }
 public void backCallback() {
   Scene s = Scene.node();
   s.addChild(backAction());
   Director.sharedDirector().replaceScene(s);
 }
 public static void restartCallback() {
   Scene s = Scene.node();
   s.addChild(restartAction());
   Director.sharedDirector().replaceScene(s);
 }
Exemple #10
0
  /** align items in columns of rows */
  public void alignItemsInRows(int rows[]) {
    ArrayList<Integer> columns = new ArrayList<Integer>();
    for (int i = 0; i < rows.length; i++) {
      columns.add(rows[i]);
    }

    ArrayList<Integer> columnWidths = new ArrayList<Integer>();
    ArrayList<Integer> columnHeights = new ArrayList<Integer>();

    int width = -10, columnHeight = -5;
    int column = 0, columnWidth = 0, rowsOccupied = 0, columnRows;
    for (int i = 0; i < children.size(); i++) {
      MenuItem item = (MenuItem) children.get(i);
      assert column < columns.size() : "Too many menu items for the amount of rows/columns.";

      columnRows = columns.get(column);
      assert columnRows != 0 : "Can't have zero rows on a column";

      columnWidth = (int) Math.max(columnWidth, item.getWidth());
      columnHeight += item.getHeight() + 5;
      ++rowsOccupied;

      if (rowsOccupied >= columnRows) {
        columnWidths.add(columnWidth);
        columnHeights.add(columnHeight);
        width += columnWidth + 10;

        rowsOccupied = 0;
        columnWidth = 0;
        columnHeight = -5;
        ++column;
      }
    }

    assert rowsOccupied != 0 : "Too many rows/columns for available menu items.";

    CCSize winSize = Director.sharedDirector().winSize();

    column = 0;
    columnWidth = 0;
    columnRows = 0;
    float x = -width / 2, y = 0;
    for (int i = 0; i < children.size(); i++) {
      MenuItem item = (MenuItem) children.get(i);
      if (columnRows == 0) {
        columnRows = columns.get(column);
        y = columnHeights.get(column) + winSize.height / 2;
      }

      columnWidth = (int) Math.max(columnWidth, item.getWidth());
      item.setPosition(x + columnWidths.get(column) / 2, y - winSize.height / 2);

      y -= item.getHeight() + 10;
      ++rowsOccupied;

      if (rowsOccupied >= columnRows) {
        x += columnWidth + 5;

        rowsOccupied = 0;
        columnRows = 0;
        columnWidth = 0;
        ++column;
      }
    }
  }