public CLayout BuildTable(string message, TextAlign textAlign, int tableWidth) { var tableLayout = new CLayout(0, 0, CLayout.Orientation.Vertical, false); // split lines var lines = message.Split('\n'); var lineCount = lines.Length; // get column count var columnCount = 0; for (var lineNo = 0; lineNo < lineCount; lineNo++) { columnCount = Math.Max(columnCount, lines[lineNo].Split('§').Length); } // define column widths var columnWidth = new int[columnCount]; var totalWidth = 0; for (var lineNo = 0; lineNo < lineCount; lineNo++) { var curLine = lines[lineNo]; if (curLine.Contains("|")) { curLine = curLine.Split('|')[1]; } // skip empty lines if (string.IsNullOrEmpty(curLine)) { continue; } // split row in columns var line = curLine.Split('§'); totalWidth = 0; for (var colNo = 0; colNo < line.Length; colNo++) { // track column width if current row has no "colspan" if (line.Length == columnCount) { columnWidth[colNo] = Math.Max(columnWidth[colNo], (int)(menu_sprite_font.MeasureString(Utils.StringCleanup(line[colNo])).X + 8)); totalWidth += columnWidth[colNo]; } } } // if table width supplied, resize all columns if (tableWidth > 0 && totalWidth != tableWidth) { for (var colNo = 0; colNo < columnWidth.Length; colNo++) { columnWidth[colNo] = (int)((float)columnWidth[colNo] / (float)totalWidth * (float)tableWidth); } } // find message width var messageWidth = 0; for (var i = 0; i < columnCount; i++) { messageWidth += columnWidth[i]; } var messageHeight = lineCount * menu_item_height; for (var lineNo = 0; lineNo < lineCount; lineNo++) { var row = new CLayout(0, 0, CLayout.Orientation.Horizontal, true); var curLine = lines[lineNo]; if (curLine.Contains("|")) { row.KeyName = curLine.Split('|')[0]; row.IsFocusable = curLine.Split('|').Length < 3; curLine = curLine.Split('|')[1]; } var line = curLine.Split('§'); row.Width = messageWidth; row.Gravity = LayoutGravity.Left; for (var colNo = 0; colNo < line.Length; colNo++) { CLabel lbl; lbl = new CLabel(0, 0, columnWidth[colNo], menu_item_height, line[colNo], menu_font_name, menu_font_size, menu_font_style, menu_font_color, Color.TransparentWhite, textAlign, false) { Scrolling = false }; // first row is table header //if (lineNo == 0) //{ // lbl = new CLabel(0, 0, // columnWidth[colNo], (int)(menu_item_height * 1), line[colNo], menu_font_name, // menu_font_size + 1, menu_font_style, menu_selected_font_color, menu_selected_backcolor, textAlign, false); // lbl.BackColor = menu_selected_backcolor; // lbl.BorderColor = menu_font_color; //} //else // lbl = new CLabel(0, 0, // columnWidth[colNo], menu_item_height, line[colNo], menu_font_name, // menu_font_size, menu_font_style, menu_font_color, Color.TransparentWhite, textAlign, false); row.AddItem(lbl); } tableLayout.AddItem(row); } tableLayout.Update(); return(tableLayout); }
public int ShowMessage(string message, TextAlign textAlign, int previousMessageHeight, int maxRows) { // split lines var lines = message.Split('\n'); var lineCount = Math.Min(lines.Length, maxRows); // get column count var columnCount = 0; for (var lineNo = 0; lineNo < lineCount; lineNo++) { columnCount = Math.Max(columnCount, lines[lineNo].Split('§').Length); } // define column widths var columnWidth = new int[columnCount]; for (var lineNo = 0; lineNo < lineCount; lineNo++) { // skip empty lines if (string.IsNullOrEmpty(lines[lineNo])) { continue; } // split row in columns var line = lines[lineNo].Split('§'); for (var colNo = 0; colNo < line.Length; colNo++) { // track column width if current row has no "colspan" if (line.Length == columnCount) { columnWidth[colNo] = Math.Max(columnWidth[colNo], (int)(menu_sprite_font.MeasureString(Utils.StringCleanup(line[colNo])).X *(lineNo == 0 ? 1.2 : 1) + 8)); } } } // find message width var messageWidth = 0; for (var i = 0; i < columnCount; i++) { messageWidth += columnWidth[i]; } var messageHeight = lineCount * menu_item_height; if (previousMessageHeight == 0) { // first message: clear messageItems.Clear(); } else { // appended message: re-position previous message foreach (var item in messageItems) { item.Y -= ((messageHeight + (menu_item_height / 2)) / 2); } } var lblX = (screenResX - messageWidth) / 2; var lblY = (previousMessageHeight / 2) + (screenResY - messageHeight + (menu_item_height / 2)) / 2; var column = new CLayout(lblX, lblY, CLayout.Orientation.Vertical, true); for (var lineNo = 0; lineNo < lineCount; lineNo++) { if (!string.IsNullOrEmpty(lines[lineNo])) { var line = lines[lineNo].Split('§'); var row = new CLayout(lblX, lblY, CLayout.Orientation.Horizontal, true); row.Width = messageWidth; for (var colNo = 0; colNo < line.Length; colNo++) { CLabel lbl; // first row is table header if (lineNo == 0) { lbl = new CLabel(0, 0, columnWidth[colNo], (int)(menu_item_height * 1.2), line[colNo], menu_font_name, menu_font_size + 1, menu_font_style, menu_selected_font_color, menu_selected_backcolor, textAlign, false); lbl.BorderColor = menu_font_color; } else { lbl = new CLabel(0, 0, columnWidth[colNo], menu_item_height, line[colNo], menu_font_name, menu_font_size, menu_font_style, menu_font_color, menu_backcolor, textAlign, false); } lbl.FillParent = line.Length != columnCount; row.AddItem(lbl); } lblY += row.Height; column.AddItem(row); } } messageItems.Add(column); column.Update(); _showMessage = true; return(messageHeight); }