A powerful feature of RapaGUI is that it allows you to embed complete Hollywood displays inside your GUIs using Hollywood class. Whenever you draw something to a Hollywood display that is attached to Hollywood class, it will automatically be drawn to your widget as well. You can even hide the Hollywood display so that the user does not even notice that Hollywood is running in the background. Furthermore, all mouse clicks and key strokes that happen inside Hollywood class will be forwarded to the corresponding Hollywood display as normal Hollywood events. Thus, Hollywood class allows you to use almost all of Hollywood's powerful features inside a GUI widget as well.
Let's have a look at an example. The following code uses Hollywood class to embed a playing animation of size 320x200 inside a widget. For this, the Hollywood display's size is changed to 320x200 and then it is embedded in the GUI using a MOAI object of type Hollywood class. Here is the XML GUI declaration first:
<?xml version="1.0" encoding="iso-8859-1"?> <application> <window title="Hollywood bridge"> <vgroup> <hgroup> <rectangle/> <hollywood display="1"/> <rectangle/> </hgroup> <hgroup> <button id="play">Play</button> <button id="stop">Stop</button> </hgroup> </vgroup> </window> </application> |
Note that we use MOAI objects of Rectangle class to pad the non-resizable Hollywood object. This is necessary for the GUI to stay resizable. Here is the code now that shows you to connect your GUI widget and Hollywood:
@REQUIRE "RapaGUI" @ANIM 1, "amy_walks.anim" @DISPLAY {Width = 320, Height = 200} Function p_AnimFunc() Local numframes = GetAttribute(#ANIM, 1, #ATTRNUMFRAMES) curframe = Wrap(curframe + 1, 1, numframes + 1) DisplayAnimFrame(1, 0, 0, curframe) EndFunction Function p_EventFunc(msg) Switch msg.Class Case "Button": Switch msg.Attribute Case "Pressed": Switch msg.ID Case "play": SetInterval(1, p_AnimFunc, 50) Case "stop": ClearInterval(1) EndSwitch EndSwitch EndSwitch EndFunction InstallEventHandler({RapaGUI = p_EventFunc}) moai.CreateApp(FileToString("GUI.xml")) Repeat WaitEvent Forever |
With a little bit more work we can also make the anim movable with the mouse. The user can then click into the Hollywood object and drag the anim around using his mouse. Here is the code for that:
@REQUIRE "RapaGUI" @ANIM 1, "amy_walks.anim" @DISPLAY {Width = 320, Height = 200} Function p_AnimFunc() Local numframes = GetAttribute(#ANIM, 1, #ATTRNUMFRAMES) curframe = Wrap(curframe + 1, 1, numframes + 1) SelectBrush(1) Cls DisplayAnimFrame(1, offx, offy, curframe) EndSelect DisplayBrush(1, 0, 0) EndFunction Function p_MouseFunc() If IsLeftMouse() = True Local mx, my = MouseX(), MouseY() If (grabx = -1) And (graby = -1) Then grabx, graby = mx - offx, my - offy offx = mx - grabx offy = my - graby Else grabx, graby = -1, -1 EndIf EndFunction Function p_EventFunc(msg) Switch msg.Class Case "Button": Switch msg.Attribute Case "Pressed": Switch msg.ID Case "play": SetInterval(1, p_AnimFunc, 50) SetInterval(2, p_MouseFunc, 20) Case "stop": ClearInterval(1) ClearInterval(2) EndSwitch EndSwitch EndSwitch EndFunction CreateBrush(1, 320, 200) InstallEventHandler({RapaGUI = p_EventFunc}) moai.CreateApp(FileToString("GUI.xml")) Repeat WaitEvent Forever |
Finally, it is also possible to make the Hollywood object resizable by
setting the attributes Area.FixWidth and Area.FixHeight to
False
. Whenever the user resizes the window, your Hollywood display will
receive a SizeWindow
event that you can listen to using the
InstallEventHandler()
Hollywood function. When using a resizable
Hollywood object, we can remove the two <rectangle>
objects
used solely as padding space. The XML code looks like this then:
<?xml version="1.0" encoding="iso-8859-1"?> <application> <window title="Hollywood bridge"> <vgroup> <hollywood display="1" fixwidth="false" fixheight="false"/> <hgroup> <button id="play">Play</button> <button id="stop">Stop</button> </hgroup> </vgroup> </window> </application> |
Our code is pretty much the same as before with the exception that we
now have to handle the SizeWindow
event to take care of
GUI resize events. Here is the adapted code from above:
@REQUIRE "RapaGUI" @ANIM 1, "amy_walks.anim" @DISPLAY {Width = 320, Height = 200} Function p_AnimFunc() Local numframes = GetAttribute(#ANIM, 1, #ATTRNUMFRAMES) curframe = Wrap(curframe + 1, 1, numframes + 1) SelectBrush(1) Cls DisplayAnimFrame(1, offx, offy, curframe, {Width = swidth, Height = sheight}) EndSelect DisplayBrush(1, 0, 0) EndFunction Function p_MouseFunc() If IsLeftMouse() = True Local mx, my = MouseX(), MouseY() If (grabx = -1) And (graby = -1) Then grabx, graby = mx - offx, my - offy offx = mx - grabx offy = my - graby Else grabx, graby = -1, -1 EndIf EndFunction Function p_EventFunc(msg) If msg.Action = "SizeWindow" swidth = msg.Width sheight = msg.Height CreateBrush(1, swidth, sheight) Return EndIf Switch msg.Class Case "Button": Switch msg.Attribute Case "Pressed": Switch msg.ID Case "play": SetInterval(1, p_AnimFunc, 50) SetInterval(2, p_MouseFunc, 20) Case "stop": ClearInterval(1) ClearInterval(2) EndSwitch EndSwitch EndSwitch EndFunction swidth, sheight = 320, 200 CreateBrush(1, swidth, sheight) InstallEventHandler({RapaGUI = p_EventFunc, SizeWindow = p_EventFunc}) moai.CreateApp(FileToString("GUI.xml")) Repeat WaitEvent Forever |
From this example you can see that Hollywood class is really powerful and can be used to achieve lots of innovative GUI ideas only limited by your creativity!