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(); } } } }
/// <summary> /// how to execute an onClick action /// </summary> /// <param name="scope"></param> public void OnClick() { //Show.Log(debugMetaData); //Show.Log("onClick " + scope + "." + script.Stringify()); TokenErrorLog tok = new TokenErrorLog(); if (script.meta != null) { object r = script.Resolve(tok, scope); } if (tok.HasError()) { Show.Warning(tok.GetErrorString()); } }
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(); }
public void SetColumnHeader(ColumnHeader columnHeader, UnityDataSheet uds, int column) { // unregister listeners before values change, since values are about to change. ClearInputTextBoxListeners(); this.uds = uds; this.column = column; cHeader = columnHeader; TokenErrorLog errLog = new TokenErrorLog(); // setup script value Token t = cHeader.columnSetting.fieldToken; //string textA = t.GetAsSmallText(); //string textB = t.Stringify(); //string textC = t.StringifySmall(); //string textD = t.ToString(); string text = t.GetAsBasicToken(); //Show.Log("A: "+textA+"\nB:" + textB + "\nC:" + textC + "\nD:" + textD + "\nE:" + text); scriptValue.text = text; EventBind.On(scriptValue.onValueChanged, this, OnScriptValueEdit); // implicitly setup value types dropdown OnScriptValueEdit(text); // setup column label object labelText = cHeader.columnSetting.data.label.Resolve(errLog, uds.data); if (errLog.HasError()) { popup.Set("err", defaultValue.gameObject, errLog.GetErrorString() + Proc.Now); return; } columnLabel.text = labelText.StringifySmall(); EventBind.On(columnLabel.onValueChanged, this, OnLabelEdit); // setup column width columnWidth.text = cHeader.columnSetting.data.widthOfColumn.ToString(); EventBind.On(columnWidth.onValueChanged, this, OnColumnWidthEdit); // setup column index columnIndex.text = column.ToString(); EventBind.On(columnIndex.onValueChanged, this, OnIndexEdit); // setup column type List <ModalConfirmation.Entry> entries = columnTypes.ConvertAll(c => { string dropdownLabel; if (c.uiField != null && !string.IsNullOrEmpty(c.name)) { dropdownLabel = "/*" + c.name + "*/ " + c.uiField.name; } else { dropdownLabel = c.name; } return(new ModalConfirmation.Entry(dropdownLabel, null)); }); t = cHeader.columnSetting.data.columnUi; string fieldTypeText = t.ToString(); //cHeader.columnSetting.data.columnUi.GetAsBasicToken();//ResolveString(errLog, null); int currentIndex = columnTypes.FindIndex(c => fieldTypeText.StartsWith(c.uiField.name)) + 1; //Show.Log(currentIndex+" field " + fieldTypeText); DropDownEvent.PopulateDropdown(fieldType, entries, this, SetFieldType, currentIndex, true); if (currentIndex == 0) { DropDownEvent.SetCustomValue(fieldType.gameObject, fieldTypeText); } TMP_InputField elementUiInputField = fieldType.GetComponentInChildren <TMP_InputField>(); if (elementUiInputField != null) { elementUiInputField.onValueChanged.RemoveAllListeners(); //Show.Log("bind to "+elementUiInputField.name); EventBind.On(elementUiInputField.onValueChanged, this, OnSetFieldTypeText); } // setup default value object defVal = cHeader.columnSetting.defaultValue; if (defVal != null) { defaultValue.text = defVal.ToString(); } else { defaultValue.text = ""; } EventBind.On(defaultValue.onValueChanged, this, OnSetDefaultValue); // setup column destroy option EventBind.On(trashColumn.onClick, this, ColumnRemove); popup.Hide(); }