vexe / VFW

MIT License
492 stars 67 forks source link

Custom Drawer #9

Closed Igneom closed 9 years ago

Igneom commented 9 years ago

Upon inspecting the code of the Vector3Drawer, I gave my shot on making a custom drawer for a struct IntVector3 I use. On 1.3 it worked fine, on 1.3.1 it doesn't work anymore.

The following is the code I'm using for the drawer

public class IntVector3Drawer : BasicDrawer<IntVector3>
    {
        protected override IntVector3 DoField(string text, IntVector3 value)
        {
            using (gui.Horizontal())
            {
                gui.Prefix(text);
                using (gui.LabelWidth(13f))
                {
                    value.x = gui.Int("X", value.x);
                    value.y = gui.Int("Y", value.y);
                    value.z = gui.Int("Z", value.z);
                }
            }

            return value;
        }
    }

Edit: Found out About the new ObjectDrawer, code now looks like this;

public class IntVector3Drawer : ObjectDrawer<IntVector3>
    {
        public override void OnGUI()
        {
            if (memberValue.IsObjectNull())
                memberValue = new IntVector3();

            using (gui.Horizontal())
            {
                gui.Prefix(member.Name);
                using (gui.LabelWidth(13f))
                {
                    var value = new IntVector3
                    {
                        x = gui.Int("X", memberValue.x),
                        y = gui.Int("Y", memberValue.y),
                        z = gui.Int("Z", memberValue.z)
                    };
                    memberValue = value;
                }
            }
        }
    }

It works but I'm not 100% sure if I will not have problems with serialization since I couldn't change the fields directly as can be seen, also what are the benefits of the change in how it works?

vexe commented 9 years ago

Have you read the commit regarding the refactoring I did for the type-drawer mapping system? :)

It doesn't seem to me that you mapped your type to your drawer. Mapping is now done explicitly and not 'automagically' via the generic arguments of the drawer. Take a look at RegisterCustomDrawerExample.cs

If you apply that to your BasicDrawer it should work. (Doesn't matter if you're using BasicDrawer or ObjectDrawer, cause BasicDrawer 'is a' ObjectDrawer at the end) Note: ObjectDrawer is not something new, it's been around since the very beginning of Vfw. In fact I wonder how you said 'it works', it shouldn't if it's not registered/mapped ^^

vexe commented 9 years ago

I've added a third example featuring a structure similar to yours: https://github.com/vexe/VFW/commit/a386fb273968f1a91cf00db2fdf2de208ba77cb9 - does it help?

Igneom commented 9 years ago

Forgot to say I mapped it after looking at your post on unity forums and checking the examples. What i found weird is that I looked again at how the BasicDrawer did the Vector3 drawing and it was still the same, then I assumed it could've been broken, which is why I created the issue without checking anything else.