/** 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; } } }
/** align items vertically with padding */ public void alignItemsVertically(float padding) { float height = -padding; for (int i = 0; i < children.size(); i++) { MenuItem item = (MenuItem) children.get(i); height += item.getHeight() * item.getScaleY() + padding; } float y = height / 2.0f; for (int i = 0; i < children.size(); i++) { MenuItem item = (MenuItem) children.get(i); item.setPosition(0, y - item.getHeight() * item.getScaleY() / 2.0f); y -= item.getHeight() * item.getScaleY() + padding; } }
/** 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; } } }