Esempio n. 1
0
        /// <summary>
        /// Bind control to the auto layout handler.
        /// </summary>
        /// <param name="control">Control that would be binded.</param>
        /// <param name="descriptor">Source descriptor.</param>
        /// <param name="member">Member from the descriptor.</param>
        public static void ToBindControl(IGUIField control, UIDescriptor descriptor, MemberInfo member)
        {
            // Drop control sign up in case if member not shared.
            if (descriptor == null)
            {
                throw new NullReferenceException("Instance of the UIDescriptor not shared with @args");
            }
            if (member == null)
            {
                throw new NullReferenceException("Instance of the MemberInfo not shared with @args");
            }

            // Detecting default value seted up into descriptor.
            object defaultValue;

            // Getting from property member.
            if (member is PropertyInfo pi)
            {
                defaultValue = pi.GetValue(descriptor);
            }
            // Getting from field memeber.
            else if (member is FieldInfo fi)
            {
                defaultValue = fi.GetValue(descriptor);
            }
            // Member cast is invalid and not supported intor that operation.
            else
            {
                throw new InvalidCastException("@member must inherit PropertyInfo of FieldInfo");
            }

            // Sing up this control on desctiptor events.
            descriptor.ControlSignUp(control, member, defaultValue);
        }
Esempio n. 2
0
        /// <summary>
        /// Registrating bool property into auto layout ui.
        /// </summary>
        /// <param name="control">Instiniated layout control.</param>
        /// <param name="descriptor">Descriptor that hold fields or properties.</param>
        /// <param name="member">Member in descriptor instance that will be used as target for value update.</param>
        /// <param name="defautltValue">Value that will be setted by default.</param>
        public static void RegistrateField(this IGUIField control, UIDescriptor descriptor, MemberInfo member, object defautltValue)
        {
            #region Declaretion & Initializtion
            // Apply default value.
            control.Value = defautltValue;

            // Declaring registration params.
            Action <IGUIField> handler     = null;
            PropertyInfo       propMember  = null;
            FieldInfo          fieldMember = null;
            #endregion

            #region Handlers
            // Instiniate UI field update callback for property members.
            void PropChangeCallback(IGUIField _)
            {
                // Try to set value.
                try { propMember.SetValue(descriptor, control.Value); } catch (Exception ex)
                { MessageBox.Show("Backward member binding corupted.\n\nDetails:\n" + ex.Message); }
            }

            // Instiniate UI field update callback for fields members.
            void FieldChangeCallback(IGUIField _)
            {
                // Try to set value.
                try { fieldMember.SetValue(descriptor, control.Value); } catch (Exception ex)
                { MessageBox.Show("Backward member binding corupted.\n\nDetails:\n" + ex.Message); }
            }

            #endregion

            #region Configuration
            // Configurating registration params and handlers.
            if (member is PropertyInfo prop)
            {
                propMember = prop;
                handler    = PropChangeCallback;
            }
            else if (member is FieldInfo field)
            {
                fieldMember = field;
                handler     = FieldChangeCallback;
            }
            #endregion

            #region Registration
            // To to registrate control into handler.
            try { RegistredCallbacks.Add(control, handler); }
            catch { throw new NotSupportedException("Instance of the ILayoutControl could be registred only once."); }

            // Subscribe on value change.
            control.ValueChanged += handler;
            #endregion
        }
Esempio n. 3
0
        /// <summary>
        /// Safely init layout control element and sighn up on internal hadler events.
        /// </summary>
        /// <param name="control">The GUI control that will be instiniated.</param>
        /// <param name="member">The member that will be binded to the GUI.</param>
        /// <param name="value">Value that will applied as default.</param>
        public void ControlSignUp(IGUIField control, MemberInfo member, object value)
        {
            try
            {
                // Registrate member in auto layout handler.
                control.RegistrateField(this, member, value);

                // Adding instiniated element to the layout.
                activeLayer?.ApplyControl(control as FrameworkElement);
            }
            catch (Exception ex)
            { MessageBox.Show("Constrol sign up failed.\n\nDetails:\n" + ex.Message); }
        }
Esempio n. 4
0
 /// <summary>
 /// Unbind layout control from auto layout handler.
 /// </summary>
 /// <param name="control">Target layout control.</param>
 public static void UnregistrateField(this IGUIField control)
 {
     try
     {
         // Unregistreting of registred callback.
         control.ValueChanged -= (Action <IGUIField>)RegistredCallbacks[control];
     }
     catch
     {
         // Log error.
         //MessageBox.Show("You trying to unregistred layout control " +
         //    "that was not registred into the auto layout handler.\n" +
         //    "Use `LayoutHandler.RegistrateField` before.");
     }
 }
Esempio n. 5
0
        /// <summary>
        /// Trying to bind control to the auto layout handler.
        /// </summary>
        /// <param name="control">Control that would be binded.</param>
        /// <param name="args">Must contains <see cref="UIDescriptor"/>
        /// and <see cref="MemberInfo"/> for success performing.</param>
        /// <returns>Is control was binded?</returns>
        public static bool TryToBindControl(IGUIField control, params object[] args)
        {
            try
            {
                // Trying to bind.
                ToBindControl(control, args);

                // Success if esception not occured
                return(true);
            }
            catch
            {
                // Inform about binding fail.
                return(false);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Occurs when language changed.
        /// </summary>
        /// <param name="arg1"></param>
        /// <param name="arg2"></param>
        private void OnLanguageChanged(IGUIField arg1, object[] arg2)
        {
            switch (lang)
            {
            case LangOptions.En:
                LocalizationHandler.LoadDictionaries(
                    langDictsPath,
                    new CultureInfo("en-US"));
                break;

            case LangOptions.Ru:
                LocalizationHandler.LoadDictionaries(
                    langDictsPath,
                    new CultureInfo("ru-RU"));
                break;
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Safely init layout control element and sighn up on internal hadler events.
        /// </summary>
        /// <param name="control">The GUI control that will be instiniated.</param>
        /// <param name="member">The member that will be binded to the GUI.</param>
        /// <param name="value">Value that will applied as default.</param>
        public void ControlSignUp(IGUIField control, MemberInfo member, object value)
        {
            try
            {
                // Registrate member in auto layout handler.
                control.RegistrateField(this, member, value);

                // Subscribe the global event handler.
                control.ValueChanged += OnValueChangedCallback;
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    "Control sign up failed.\n\n" +
                    "Member: " + member.Name + "\n" +
                    "Details: " + ex.Message);
            }
        }
Esempio n. 8
0
        private void Descriptor_ValueChanged(UIDescriptor sender, IGUIField changedElement, object[] args)
        {
            if (!(sender is ControlPanel controlPanel))
            {
                throw new NotSupportedException();
            }

            ellipseShape.Visibility = Visibility.Collapsed;
            rectShape.Visibility    = Visibility.Collapsed;

            switch (controlPanel.shapeType)
            {
            case ShapeType.Ellipse: ellipseShape.Visibility = Visibility.Visible; break;

            case ShapeType.Rectangle: rectShape.Visibility = Visibility.Visible; break;
            }

            shapesHolder.Width  = controlPanel.width;
            shapesHolder.Height = controlPanel.height;
        }
Esempio n. 9
0
        /// <summary>
        /// Occurs when user decide to change the current app's language.
        /// </summary>
        /// <param name="obj"></param>
        private void LangPanel_ValueChanged(IGUIField obj, object[] args)
        {
            var togglePanel = obj as FlatTogglesGroup;

            switch (togglePanel.Index)
            {
            case 0:
                LocalizationHandler.LoadDictionaries(langDictsPath, new CultureInfo("en-US"));
                break;

            case 1:
                LocalizationHandler.LoadDictionaries(langDictsPath, new CultureInfo("ru-RU"));
                break;

            default:
                LocalizationHandler.UnloadDictionaries();
                break;
            }

            // Buferizing current index.
            LangIndex = togglePanel.Index;
        }
Esempio n. 10
0
        /// <summary>
        /// Bind control to the auto layout handler.
        /// </summary>
        /// <param name="control">Control that would be binded.</param>
        /// <param name="args">Must contains <see cref="UIDescriptor"/> and <see cref="MemberInfo"/>.</param>
        public static void ToBindControl(IGUIField control, params object[] args)
        {
            // Find required referendes.
            UIDescriptor desc   = null;
            MemberInfo   member = null;

            // Trying to get shared properties.
            foreach (object obj in args)
            {
                if (obj is UIDescriptor)
                {
                    desc = (UIDescriptor)obj;
                }
                if (obj is MemberInfo)
                {
                    member = (MemberInfo)obj;
                }
            }

            // Request binding.
            ToBindControl(control, desc, member);
        }
Esempio n. 11
0
        private void FlatTogglesGroup_ValueChanged(IGUIField obj, object[] _)
        {
            var group = obj as FlatTogglesGroup;

            MessageBox.Show("\"" + group.Value + "\" is selected.");
        }
Esempio n. 12
0
 // Occurs when `flexibleCollection` changed from UI.
 private void Field_ValueChanged(IGUIField obj, object[] args)
 {
     // Do something.
 }
Esempio n. 13
0
 /// <summary>
 /// Handels the UIDescriptor.ValueChanged event and forward to the ipper one.
 /// </summary>
 /// <param name="arg1">Descriptor initiated the event initiated the event.</param>
 /// <param name="arg2">Fieled initiated the event.</param>
 /// <param name="args">Shared arguments</param>
 protected void OnValueChangedCallback(UIDescriptor arg1, IGUIField arg2, object[] args)
 {
     // Inform subscribers.
     ValueChanged?.Invoke(arg1, arg2, args);
 }
Esempio n. 14
0
 /// <summary>
 /// Handels the IGuiField.ValueChanged event and forward to the global one.
 /// </summary>
 /// <param name="sender">Fieled initiated the event.</param>
 /// <param name="args">Shared arguments.</param>
 protected virtual void OnValueChangedCallback(IGUIField sender, object[] args)
 {
     // Inform subscribers.
     ValueChanged?.Invoke(this, sender, args);
 }
Esempio n. 15
0
        /// <summary>
        /// Registrating bool property into auto layout ui.
        /// </summary>
        /// <param name="control">Instiniated layout control.</param>
        /// <param name="descriptor">Descriptor that hold fields or properties.</param>
        /// <param name="member">Member in descriptor instance that will be used as target for value update.</param>
        /// <param name="defautltValue">Value that will be setted by default.</param>
        public static void RegistrateField(
            this IGUIField control, UIDescriptor descriptor,
            MemberInfo member, object defautltValue)
        {
            #region Declaretion & Initializtion
            // Preventing the null field.
            if (defautltValue == null)
            {
                try
                {
                    var type = UIDescriptor.MembersHandler.GetSpecifiedMemberType(member);

                    if (type.GetConstructor(new Type[0]) != null)
                    {
                        // Instiniating the default value.
                        defautltValue = Activator.CreateInstance(type);

                        UIDescriptor.MembersHandler.SetValue(member, descriptor, defautltValue);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Custructor crash!" +
                                                  "\nMember: {0}\n" +
                                                  "Descriptor: {1}\n\n" +
                                                  "Details: {2}",
                                                  member.Name,
                                                  descriptor.GetType().FullName,
                                                  ex.Message));
                };
            }

            // Apply default value.
            control.Value = defautltValue;

            // Declaring registration params.
            Action <IGUIField, object[]> handler = null;
            PropertyInfo propMember  = null;
            FieldInfo    fieldMember = null;
            #endregion

            #region Handlers
            // Instiniate UI field update callback for property members.
            void PropChangeCallback(IGUIField _, object[] __)
            {
                // Try to set value.
                try { propMember.SetValue(descriptor, control.Value); }
                catch (NotSupportedException) { }
                catch (Exception ex)
                { MessageBox.Show("Backward member binding corupted.\n\nDetails:\n" + ex.Message); }
            }

            // Instiniate UI field update callback for fields members.
            void FieldChangeCallback(IGUIField _, object[] __)
            {
                // Try to set value.
                try { fieldMember.SetValue(descriptor, control.Value); }
                catch (NotSupportedException) { }
                catch (Exception ex)
                { MessageBox.Show("Backward member binding corupted.\n\nDetails:\n" + ex.Message); }
            }

            #endregion

            #region Configuration
            // Configurating registration params and handlers.
            if (member is PropertyInfo prop)
            {
                propMember = prop;
                handler    = PropChangeCallback;
            }
            else if (member is FieldInfo field)
            {
                fieldMember = field;
                handler     = FieldChangeCallback;
            }
            #endregion

            #region Registration
            // To to registrate control into handler.
            try { RegistredCallbacks.Add(control, handler); }
            catch { throw new NotSupportedException("Instance of the ILayoutControl could be registred only once."); }

            // Subscribe on value change.
            control.ValueChanged += handler;
            #endregion
        }