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();
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// used when the path is known, having been compiled already by <see cref="GetValueFromRawPath(object, IList{object}, object, List{object})"/>
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="path"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool TrySetValueCompiled(object scope, object path, object value)
        {
            bool conversionWorks = true;

            switch (path)
            {
            case FieldInfo fi:
                if (value != null && !fi.FieldType.IsAssignableFrom(value.GetType()))
                {
                    conversionWorks = CodeConvert.Convert(ref value, fi.FieldType);
                }
                if (conversionWorks)
                {
                    //object oldValue = fi.GetValue(scope); Show.Log("old value is " + oldValue);
                    fi.SetValue(scope, value);
                    //Show.Log("set "+fi.Name+" to "+value);
                    //object newValue = fi.GetValue(scope); Show.Log("new value is " + newValue);
                }
                return(conversionWorks);

            case PropertyInfo pi:
                if (!pi.CanWrite)
                {
                    return(false);
                }
                if (value != null && !pi.PropertyType.IsAssignableFrom(value.GetType()))
                {
                    conversionWorks = CodeConvert.Convert(ref value, pi.PropertyType);
                }
                if (conversionWorks)
                {
                    pi.SetValue(scope, value);
                    //Show.Log("set " + pi.Name + " to " + value);
                }
                return(conversionWorks);

            case string s: return(TrySetValue_Dictionary(scope, ref path, value));
            }
            return(false);
        }