/** align items horizontally with padding */ public void alignItemsHorizontally(float padding) { float width = -padding; for (int i = 0; i < children.size(); i++) { MenuItem item = (MenuItem) children.get(i); width += item.getWidth() * item.getScaleX() + padding; } float x = width / 2.0f; for (int i = 0; i < children.size(); i++) { MenuItem item = (MenuItem) children.get(i); item.setPosition(x - item.getWidth() * item.getScaleX() / 2.0f, 0); x -= item.getWidth() * item.getScaleX() + padding; } }
/** 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; } } }