Removing splashes: intro to object oriented GUI programming - acdop100/Matlab-2048 GitHub Wiki

Now open up the file removeSplash.m. This is the simplest file to illustrate the object-oriented nature of Matlab's GUI system.

If you recall, in play2048.m we had a button that called back the function "removeSplash" with an input array named "arr". it looked like "start.Callback = {@removeSplash, arr};"

Looking at the actual removeSplash.m file, we see it actually has 3 inputs: "f", "event", and "arr". If you only called the function with 1 input, where did the other 2 come from?

When a Matlab figure calls a function, the 2 inputs are automatically input into the function. We don't need to explicitly call the function with these inputs but to access them we need to put them in the input line in our function header. The input "f" just lets us access any uicontrol objects we created, and "event" gives us the type of event that triggered the function and use that as an input to the function if needed.

My function serves three purposes: it removes the initial splash screen when you launch the game, removes the splash after you pause the game, and removes the splash after you restart the game. It is easy to implement this in 20 lines (probably less but I am lazy :P)

so line 4: "if strcmp(event.Source.String, 'Start!')" What this line does if check to see whether the string of the button you pressed contained the string "Start!". This tells Matlab you pressed the Start button, and removes the start screen splashes appropriately. Event.Source.String is the object of the button you pressed that called the function. Event is what called the function, .Source denotes you are looking for an object in the source of the event, and .String means you only want the string that the source object contained. If you only called event.Source, you would get a (quite large) array filled with all of the information about the source object. Adding the .String lets us pull only the info we want and makes it compatible with strcmp.

I know that was a lot but I hope it made sense haha. Lets continue.

  • Let's assume that the strcmp passed and we are removing the splash screen. The next line says "cf = gcf". What does this do? The command "gcf" means "get current figure" and grabs all of the objects within the input figure "f". "gcf" is not dependent on the name of your figure variable name.
  • Next we grab all of the child objects within the current figure with "allchild(cf)". This gives us an array of every uicontrol "child" object we made in out figure. This includes text boxes, buttons, etc.
  • Children are listed from bottom to top of the figure, and since out start splash screen were created last in out play2048.m file, they appear as the first two children in our variable "childs"
  • The next 2 lines removes those two children from the figure. They don't delete the code obviously, just removes them from view in the figure.
  • Last (for this section of the if statement, anyways), we run the myTileGenerator function with the inputs of f, event, and arr. Why do we input f and event here, but not when we called the removeSplash program? It's because we are calling the figure from within an already running function and not a figure call (i.e. from pressing a button).

The rest is basically the same as above. On to myTileGenerator.m!