public void AssignFromText(string text) { //TMPro.TMP_InputField input = GetComponent<TMPro.TMP_InputField>(); //Show.Log("assign "+text+" instead of "+input.text); UnityDataSheet uds = GetComponentInParent <UnityDataSheet>(); if (uds == null) { // this happens the first instant that the input field is created, before it is connected to the UI properly //Show.Log("missing "+nameof(UnityDataSheet)+" for "+transform.HierarchyPath()); return; } int col = transform.GetSiblingIndex(); int row = uds.GetRowIndex(transform.parent.gameObject); Udash.ColumnSetting column = uds.GetColumn(col); if (column.canEdit) { object value = text; bool validAssignment = true; if (column.type != null) { if (!CodeConvert.Convert(ref value, column.type)) { errorMessage = "could not assign \"" + text + "\" to " + column.type; uds.errLog.AddError(-1, errorMessage); validAssignment = false; uds.popup.Set("err", gameObject, errorMessage); } } if (validAssignment) { ITokenErrLog errLog = new TokenErrorLog(); validAssignment = column.SetValue(uds.GetItem(row), value, errLog); if (errLog.HasError()) { errorMessage = errLog.GetErrorString(); validAssignment = false; uds.popup.Set("err", gameObject, errorMessage); } } if (validAssignment) { uds.data.Set(row, col, value); if (errorMessage == uds.popup.Message) { uds.popup.Hide(); } } } }
public void ColumnRemove() { ModalConfirmation ui = confirmRemoveUi; if (ui == null) { ui = Global.GetComponent <ModalConfirmation>(); } Udash.ColumnSetting cS = uds.GetColumn(column); ui.OkCancel("Are you sure you want to delete column \"" + cS.data.label + "\"?", () => { uds.RemoveColumn(column); Close(); }); uds.RefreshUi(); }
public Udash.ColumnSetting AddColumn() { Udash.ColumnSetting column = new Udash.ColumnSetting(data) { fieldToken = new Token(""), data = new UnityColumnData { label = new Token("new data"), columnUi = new Token("input"), headerUi = new Token("collabel"), widthOfColumn = -1, }, type = typeof(string), defaultValue = "" }; data.AddColumn(column); MakeSureColumnsMarkedLastAreLast(); RefreshUi(); return(column); }
void InitColumnSettings(string columnSetup) { //Show.Log(columnSetup); Tokenizer tokenizer = new Tokenizer(); CodeConvert.TryParse(columnSetup, out UnityColumnData[] columns, null, tokenizer); if (tokenizer.HasError()) { Show.Error("error parsing column structure: " + tokenizer.GetErrorString()); return; } int index = 0; //data.AddRange(list, tokenizer); for (int i = 0; i < columns.Length; ++i) { UnityColumnData c = columns[i]; c.typeOfValue = c.defaultValue != null?c.defaultValue.GetType() : null; Udash.ColumnSetting columnSetting = new Udash.ColumnSetting(data) { //fieldToken = c.valueScript, data = new UnityColumnData { label = c.label, columnUi = c.columnUi, headerUi = c.headerUi, widthOfColumn = -1, onClick = c.onClick, alwaysLast = c.alwaysLast }, type = c.typeOfValue, defaultValue = c.defaultValue }; columnSetting.SetFieldToken(c.valueScript, tokenizer); data.SetColumn(index, columnSetting); if (c.widthOfColumn > 0) { data.columnSettings[index].data.widthOfColumn = c.widthOfColumn; } ++index; } RefreshHeaders(); }
public GameObject UpdateRowData(DataSheetRow rObj, RowData rowData, float yPosition = float.NaN) { object[] columns = rowData.columns; Vector2 rowCursor = Vector2.zero; RectTransform rect; // remove all columns from the row (probably temporarily) List <GameObject> unusedColumns = new List <GameObject>(); for (int i = 0; i < rObj.transform.childCount; ++i) { GameObject fieldUi = rObj.transform.GetChild(i).gameObject; if (fieldUi == null) { throw new Exception("a null child in the row? wat"); } unusedColumns.Add(fieldUi); } while (rObj.transform.childCount > 0) { rObj.transform.GetChild(rObj.transform.childCount - 1).SetParent(null, false); } TokenErrorLog errLog = new TokenErrorLog(); for (int c = 0; c < data.columnSettings.Count; ++c) { Udash.ColumnSetting colS = data.columnSettings[c]; GameObject fieldUi = null; string columnUiName = colS.data.columnUi.ResolveString(errLog, rowData.obj); if (columnUiName == null) { string errorMessage = "could not resolve column UI name from " + colS.data.columnUi + "\n" + errLog.GetErrorString(); Show.Log(errorMessage); columnUiName = colS.data.columnUi.ResolveString(errLog, rowData.obj); throw new Exception(errorMessage); } // check if there's a version of it from earlier for (int i = 0; i < unusedColumns.Count; ++i) { if (unusedColumns[i].name.StartsWith(columnUiName)) { fieldUi = unusedColumns[i]; unusedColumns.RemoveAt(i); break; } } // otherwise create it if (fieldUi == null) { GameObject prefab = uiPrototypes.GetElement(columnUiName); if (prefab == null) { columnUiName = colS.data.columnUi.ResolveString(errLog, rowData.obj); throw new Exception("no such prefab \"" + columnUiName + "\" in data sheet initialization script. valid values: [" + uiPrototypes.transform.JoinToString() + "]\n---\n" + colS.data.columnUi + "\n---\n" + columnSetup); } fieldUi = Instantiate(prefab); } if (colS.data.onClick.IsSyntax) { ClickableScriptedCell clickable = fieldUi.GetComponent <ClickableScriptedCell>(); UiClick.ClearOnClick(fieldUi); if (fieldUi != null) { Destroy(clickable); } clickable = fieldUi.AddComponent <ClickableScriptedCell>(); clickable.Set(rowData.obj, colS.data.onClick); clickable.debugMetaData = colS.data.onClick.StringifySmall(); if (!UiClick.AddOnButtonClickIfNotAlready(fieldUi, clickable, clickable.OnClick)) { UiClick.AddOnPanelClickIfNotAlready(fieldUi, clickable, clickable.OnClick); } } fieldUi.SetActive(true); fieldUi.transform.SetParent(rObj.transform, false); fieldUi.transform.SetSiblingIndex(c); object value = columns[c]; if (value != null) { UiText.SetText(fieldUi, value.ToString()); } else { UiText.SetText(fieldUi, ""); } rect = fieldUi.GetComponent <RectTransform>(); rect.anchoredPosition = rowCursor; float w = rect.sizeDelta.x; if (colS.data.widthOfColumn > 0) { w = colS.data.widthOfColumn; rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, w); } rowCursor.x += w * rt.localScale.x; } for (int i = 0; i < unusedColumns.Count; ++i) { Destroy(unusedColumns[i]); } unusedColumns.Clear(); rect = rObj.GetComponent <RectTransform>(); rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, rowCursor.x); rect.transform.SetParent(contentRectangle, false); if (!float.IsNaN(yPosition)) { //rect.anchoredPosition = new Vector2(0, -yPosition); //rect.localPosition = new Vector2(0, -yPosition); rObj.LocalPosition = new Vector2(0, -yPosition); } return(rObj.gameObject); }
public void RefreshHeaders() { if (headerRectangle == null) { return; } Vector2 cursor = Vector2.zero; // put old headers aside. they may be reused. List <GameObject> unusedHeaders = new List <GameObject>(); for (int i = 0; i < headerRectangle.childCount; ++i) { GameObject header = headerRectangle.GetChild(i).gameObject; if (header != null) { unusedHeaders.Add(header); } } while (headerRectangle.childCount > 0) { Transform t = headerRectangle.GetChild(headerRectangle.childCount - 1); t.SetParent(null, false); } errLog.ClearErrors(); for (int i = 0; i < data.columnSettings.Count; ++i) { Udash.ColumnSetting colS = data.columnSettings[i]; GameObject header = null; string headerObjName = colS.data.headerUi.ResolveString(errLog, this); // check if the header we need is in the old header list object headerTextResult = colS.data.label.Resolve(errLog, data); if (errLog.HasError()) { popup.Set("err", null, errLog.GetErrorString()); return; } string headerTextString = headerTextResult?.ToString() ?? null; for (int h = 0; h < unusedHeaders.Count; ++h) { GameObject hdr = unusedHeaders[h]; if (hdr.name.StartsWith(headerObjName) && UiText.GetText(hdr) == headerTextString) { header = hdr; unusedHeaders.RemoveAt(h); break; } } if (header == null) { GameObject headerObjectPrefab = uiPrototypes.GetElement(headerObjName); header = Instantiate(headerObjectPrefab); header.name = header.name.SubstringBeforeFirst("(", headerObjName.Length) + "(" + colS.data.label + ")"; UiText.SetText(header, headerTextString); } ColumnHeader ch = header.GetComponent <ColumnHeader>(); if (ch != null) { ch.columnSetting = colS; } header.SetActive(true); header.transform.SetParent(headerRectangle, false); header.transform.SetSiblingIndex(i); RectTransform rect = header.GetComponent <RectTransform>(); rect.anchoredPosition = cursor; float w = rect.sizeDelta.x; if (colS.data.widthOfColumn > 0) { w = colS.data.widthOfColumn; rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, w); } else { colS.data.widthOfColumn = w; // if the width isn't set, use the default width of the column header } cursor.x += w * rt.localScale.x; } contentAreaSize.x = cursor.x; for (int i = 0; i < unusedHeaders.Count; ++i) { GameObject header = unusedHeaders[i]; Destroy(header); } unusedHeaders.Clear(); contentRectangle.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, cursor.x); popup.Hide(); }