Adam's Lair Forum

game development and casual madness
It is currently 2019/06/27, 07:11

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: 2013/07/19, 15:32 
Member
Member

Joined: 2013/05/20, 17:19
Posts: 77
Role: Professional
I'm trying to modify a resource editor so that it replaces a particular field with a string property editor with this code:

Code:
protected override PropertyEditor AutoCreateMemberEditor(System.Reflection.MemberInfo info)
{
   if (ReflectionHelper.MemberInfoEquals(info, typeof (Move).GetProperty("InputSequence")))
   {
      var editor = new StringPropertyEditor
         {
            Getter = () => GetValue().Cast<Move>().Select(m => m.InputSequence.ToString())
         };
      ParentGrid.ConfigureEditor(editor);
      return editor;
   }
         
   return base.AutoCreateMemberEditor(info);
}


but the Getter property is being overridden after the editor is returned. Is there somewhere else I should be attaching the getter?


Top
 Profile  
 
PostPosted: 2013/07/19, 17:18 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2073
Location: Germany
Role: Professional
What you want to do is entirely possible, but you shouldn't deal with the child editors Getter in your case. While the Getter is normally a perfectly valid option to set, it's not your duty to do so when directly or indirectly deriving from a MemberwiseProprtyEditor. What you are doing is overriding one of its methods, and it certainly doesn't expect you to interfere with its scope of responsibility - providing get and set methods for its child editors.

But there is another way. There are cases where a PropertyEditor needs to edit value types that weren't intended to be edited by that specific PropertyEditor - like using a numeric PropertyEditor for editing a string Property or similar. For those cases, there is a mechanism that allows you to inject a suitable code snippet for value conversion. Here is how it works:


Code:
// Editing a Fonts Size property using a string editor.
if (ReflectionHelper.MemberInfoEquals(info, typeof(Font).GetProperty("Size")))
{
   StringPropertyEditor e = new StringPropertyEditor();
   e.EditedType = typeof(string);
   e.ConverterGet = (floatSize => floatSize.ToString());
   e.ConverterSet = (strSize =>
   {
      float floatSize;
      if (!float.TryParse((string)strSize, out floatSize)) return 0.0f;
      return floatSize;
   });
   this.ParentGrid.ConfigureEditor(e);
   return e;
}


As far as I can see, this might also work for your case. :)

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2013/07/19, 17:50 
Member
Member

Joined: 2013/05/20, 17:19
Posts: 77
Role: Professional
Great, thanks. That sorted it. Property editors are definitely the most complex part of the editor code base for me right now, but I'm slowly getting my head around it:)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 15 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group