ValueSource to represent custom class [solved]


#1

Hi all

I have a class representing Rational Numbers (RationalNum). It contains 2 ints and a float. I now want to update the GUI with user value changes. So I'm looking at making it a Value::ValueSource (its already a ReferenceCountedObject). 

However I'm confused at how to set the 'var' value. 

Do I assume that the incoming 'var' points to a valid RationalNum object, and copy its data in? 

Or do I need to create a wrapper class that uses my ReferenceCountedObjectPtr to create the var object?  

Or would I be better off redesigning the class using var objects around the primitives? One of my goals is to be able to easily save and load these objects as JSON. 

On a related note what happened to the great tutorials on Value, var and ValueTrees? They seem to have dissappeared... 


#2

So,  I think I got this wrong and worked out that I need to subclass DynamicObject. 
I made a wrapper for my RationalNum class, and after working with it a bit I see this is really neat stuff. I now have JSON output of my class, and a way to use it in Value objects so I can listen for changes etc. 


#3

For this kind of thing I'd advise to not use a custom DynamicObject, as it won't serialise to JSON or handle undo/redo. Much better to model your data with a couple of var properties if possible.


#4

Ok, I was taking that approach previously  - however, my RationalNum class handles conversion between decimal and ratio depending on which is updated. And I want to avoid maintaining an Array and ValueTree. I've subclassed DynamicObject and I get JSON output of the properties. However, I may rethink it again if this prevents me using ValueTree's Undo/Redo functionality.  

Thanks for the heads up. 

 

EDIT: My subclass does have var properties but they are taken from the wrapped RationalNum class.