Ein mächtige Fähigkeit von RapaGUI ist die Möglichkeit, komplette Hollywood-Displays mit der Hollywood-Klasse in Ihre GUIs einzubetten. Immer wenn Sie etwas auf ein Hollywood-Display zeichnen, das mit der Hollywood-Klasse verbunden ist, wird es automatisch auch auf Ihrem GUI-Widget gezeichnet. Sie können sogar das Hollywood-Display ausblenden, so dass der Benutzer nicht einmal merkt, dass Hollywood im Hintergrund läuft. Darüber hinaus werden alle Mausklicks und Tastenanschläge, die innerhalb der Hollywood-Klasse stattfinden, als normale Hollywood-Ereignisse an das entsprechende Hollywood-Display weitergeleitet. So können Sie mit der Hollywood-Klasse fast alle leistungsstarken Funktionen von Hollywood auch in einem GUI-Widget nutzen.
Schauen wir uns ein Beispiel an. Der folgende Code verwendet die Hollywood-Klasse zum Einbetten von einer spielenden Animation der Größe 320x200 in einem GUI-Widget. Dafür wird die Größe des Hollywood-Displays auf 320x200 geändert und dann in die GUI als ein MOAI-Objekt vom Typ Hollywood-Klasse eingebettet. Hier zuerst die Definition der XML-GUI:
<?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> |
Beachten Sie, dass hier MOAI-Objekte der Rectangle-Klasse (Rechteck) verwendet wurde, um das nicht größenveränderliche Hollywood-Objekt aufzufüllen. Dies ist notwendig, damit die GUI in der Größe veränderbar bleibt. Hier ist der Code, der Ihnen zeigt, wie Sie Ihr GUI-Widget mit Hollywood verbinden können:
@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 |
Mit etwas mehr Arbeit können wir die Animation auch mit der Maus bewegen. Der Benutzer kann dann in das Hollywood-Objekt klicken und die Animation mit der Maus umherziehen. Hier ist der Code dafür:
@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 |
Schließlich ist es auch möglich, das Hollywood-Objekt in der
Größe zu verändern, indem man die Attribute Area.FixWidth
und Area.FixHeight auf False
setzt. Immer wenn der
Benutzer die Fenstergröße ändert, erhält Ihr Hollywood-Display
ein SizeWindow
-Ereignis, das Sie mit dem Hollywood-Befehl
InstallEventHandler()
überwachen können. Wenn wir
ein in der Größe veränderbares Hollywood-Objekt verwenden, können
wir die beiden Objekte <rectangle>
entfernen, die
ausschließlich als Füllraum verwendet werden. Der XML-Code sieht
dann so aus:
<?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> |
Unser Code ist so ziemlich derselbe wie vorher, mit der Ausnahme,
dass wir jetzt das Ereignis SizeWindow
überwachen
müssen, um auf die Größenänderungen der GUI zu reagieren. Hier
ist der angepasste Code von oben:
@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 |
Bei diesem Beispiel können Sie sehen, dass die Hollywood-Klasse wirklich mächtig ist und dazu verwendet werden kann, viele innovative GUI-Ideen zu verwirklichen, die nur durch Ihre Kreativität begrenzt sind!