Best practice for adding children to a component from outside that component?


#1

First off, hello!

I'm a newcomer to the world of JUCE and C++ programming, so apologies in advance if my phrasing is a bit imprecise. I've begun a process of creating a series of progressivley more complex applications using the JUCE framework to learn its ins and outs and really learn C++ in a more hands-on way, and my latest project is a clone of the tile-based web game '2048' (http://gabrielecirulli.github.io/2048/). I realize that JUCE isn't necessarily designed with this kind of application in mind, but I'd really like to familiarize myself with the JUCE framework and its best practices while having a bit of fun along the way!

Right now, I have a MainContentComponent which contains an instance of a custom TileGrid class, which contains an OwnedContentArray of 'Tile' components and methods for the manipulation of these tiles during gameplay. My MainContentComponent contains a KeyListener for handling user input and a ComponentAnimator for animating my Tile components. I'm currently doing this by iterating through each component in my TileGrid's OwnedContentArray of Tile components, displaying them with the addAndMakeVisible method of my MainContentComponent, and animating them with my MainContentComponent's ComponentAnimator depending on behaviors specified in each Tile component class.

I've attached a little diagram of my layout to help illustrate how my application works:

My application works perfectly, but I'd like to make it a bit more elegant and compartmentalized by putting all of my animation functionality outside of my MainContentComponent. 

My question is, what is the best way to add and manipulate child components of my MainContentComponent from outside of my MainContentComponent? Or, more generally, what is the best way to add child components to another component from outside of the (to-be) parent component? 

Right now, I'm planning on writing an Animations class that contains all of my animation methods, and constructing it with a pointer to the Component that instantiated it (my MainContentComponent, in this case). Theoretically, it can then add child components to its instantiating component by dereferencing the pointer to the component that instantiated it and calling its addAndMakeVisible method. 

Again, apologies if I'm not articulating this very well. These forums have been immensely useful thus far, and I've been able to understand a number of specific hangups I've encountered through perusing the forums. However, as a C++ noob it's been a little bit difficult to find answers to some of my questions involving some of the bigger-picture layout and best practice-type questions I've encountered.

 

Thanks in advance!


#2

(If I had time, I'd been thinking that adding a 2048 game to the juce demo as an example would be fun!)

It doesn't really make any difference who adds a component to another one. The only thing to worry about is ownership of the child component, but that depends entirely on how you want to structure your objects - there are no fixed rules, it's up to whatever works best in your app.


#3

Thanks Jules!

It's indeed been pretty fun to make a quick 2048 game with JUCE, but mainly because I got to play it for an even more unreasonable amount of time. The game's up and running great, but if I were to do it all over again I think I'd do quite a few things differently, although I think that will always be the case. On to the next learning project...

As an aside question, I'm really finding JUCE to be a very rewarding environment for working on getting my C++ chops up to speed, but I'd like to know the best way to answer some more JUCE-specific general C++ questions.

For instance, I couldn't figure out how to inherit from the Timer class for quite a long time. After digging through the Class API Reference for quite a while, I finally realized that I hadn't been declaring the virtual timerCallback function. It was an easy mistake to see once I spotted it, but I didn't quite know where to look to find the answer other than digging through (to my inexperienced eyes) fairly thick API documentation. 

I've picked up a copy of "Getting Started With JUCE", although I've found it to really only skim the surface of what I'd like to learn. Are there any other good places to go to learn about some of the richness of JUCE from a little bit more of a ground-floor level? Or should I just suck it up and take the ridicule that comes from asking "easy" questions on the forum?

Thanks again, it's been a pleasure to see how thought-out this library is!


#4

For instance, I couldn't figure out how to inherit from the Timer class for quite a long time. After digging through the Class API Reference for quite a while, I finally realized that I hadn't been declaring the virtual timerCallback function. It was an easy mistake to see once I spotted it, but I didn't quite know where to look to find the answer other than digging through (to my inexperienced eyes) fairly thick API documentation.

Well, that's just unfamiliarity with the C++ language rather than a juce-specific thing.

But where would you expect to find docs that would have helped you? It'd be redundant and repetitive to explain basic C++ inheritance syntax in every class that has a virtual function, and I'm not sure what else I could have done..


#5

Yeah, I'm more just curious what other learning references there are out there for learning JUCE from a bit more of a beginner's perspective. Not really expecting this from the API, but maybe something along the lines of a collection of simple demo apps that illustrate a really simple use of the most important classes. I've certainly learned a great deal from the JUCE demo app, but it's a pretty complex thing and can be a bit difficult to wade through exactly how the various classes are being implemented.

I mentioned my hangup with the virtual function in the Timer class because I think it would have been a much more easy thing to spot if I had been able to see it implemented in a simple way. Maybe it's just my unfamiliarity but seeing a virtual function defined in a simple application (with a comment, ideally!) rather than catching that elusive keyword in the API would have been really helpful!

Anyway, don't take this as anything but wishful thinking - I can't explain how exciting it is, as a relative beginner, to jump into the world of JUCE and make functional, beautiful applications so quickly and elegantly. Maybe this idea is something that I can contribute to some day once I really get my feet wet!

And thanks for the replies, they truly do mean a lot! 

 


#6

Well, in the demo app there's a "Timers and Events" demo that has a simple timer class to flash a colour. But I understand - it's hard to find basic things like this because it's hard to know what to search for!