Gui python module
From WiCWiki
|
This module contains everything related to the GUI
Types
Events
Those are the possible events coming from the GUI:
- OnButtonPressed
- Thrown when a Button is pressed. First argument will be the Gui that the Button is located in and the second argument will be the hashed name of the Button.
- OnWidgetLostFocus
- Thrown when the mouse leaves a Widget. First argument will be the Gui that the Widget is located in and the second argument will be the hashed name of the Widget.
- OnWidgetGainedFocus
- Thrown when the mouse is over a Widget. First argument will be the Gui that the Widget is located in and the second argument will be the hashed name of the Widget.
- OnGuiActivated
- Thrown when a Gui becomes activated. First argument will be the activated Gui and the second argument will be the hashed name of the Gui.
- OnGuiDeactivated
- Thrown when a Gui becomes deactivated. First argument will be the deactivated Gui and the second argument will be the hashed name of the Gui.
Constants
Widget Status
- WIDGETSTATUS_ENABLED
- The widget will be active and visible
- WIDGETSTATUS_DISABLED
- The widget will be visible. But won't produce any events
- WIDGETSTATUS_HIDDEN
- The widget will be hidden and won't produce any events
Functions
FindGui
- Syntax
import wic wic.player.gui.FindGui( aGuiScreenName )
- Description
- -
- Exceptions
- UnknownGuiException - aGuiScreenName didn't match any known Gui.
- See also
- Gui
- #FindWidget
- #FindTextLabel
- #FindButton
FindWidget
- Syntax
import wic wic.player.gui.FindWidget( aGuiName, aWidgetName )
- Exceptions
- UnknownGuiException - aGuiName didn't match any known Gui.
- UnknownWidgetException - aWidgetName didn't match any Widget in the given Gui.
- See also
- Gui
- Widget
- #FindTextLabel
- #FindButton
- #FindGui
FindButton
- Syntax
import wic wic.player.gui.FindButton( aGuiName, aButtonName )
- Exceptions
- UnknownGuiException - aGuiName didn't match any known Gui.
- UnknownButtonException - aButtonName didn't match any Button in the given Gui.
- See also
- Gui
- Button
- #FindWidget
- #FindTextLabel
- #FindGui
FindTextLabel
- Syntax
import wic wic.player.gui.FindTextLabel( aGuiName, aTextLabelName )
- Exceptions
- UnknownGuiException - aGuiName didn't match any known Gui.
- UnknownWidgetException - aTextLabelName didn't match any TextLabel in the given Gui.
- See also
- Gui
- TextLabel
- #FindWidget
- #FindButton
- #FindGui
Examples
How to open a GUI screen
To be able to open a Gui screen we need the name of it. Here I will use the request Gui which is conveniently named Request. To get a reference to the Gui I use the function FindGui in wic.player.gui. The open function of the Gui is Activate.
py import wic py requestGui = wic.player.gui.FindGui( "Request" ) py requestGui.Activate()
Change the text on a Button
Ok, first we need a button. After some searching I found a perfect victim. The Button named myButton_Sorry in the Gui Request.
py import wic py sorryButton = wic.player.gui.FindButton( "Request", "myButton_Sorry" ) py sorryButton.Text = "Ooopsidaisy!"
Well, a bit silly perhaps but that gives you a reason to freestyle a bit.
Reacting to Gui events
This part cannot be done from the ingame console so you will have to hack the client.py for an existing map. This is because we need functions to handle events and the console cannot handle multi line python statements. My suggestion is to use europe1 (or do_Ruins as it's also called). Remember to backup the original file before doing this. And don't forget to add -console to your command line options. In an empty client.py we add this:
import sys, wic def EventHandler( anEventName, someEventData ): """ This function will be registered as our generic event handler. Any client event without a specialised handler will be passed to this function. Unhandled events will just pass through silently. """ # If it's a GUI activation we print the name of the # GUI to stdout which we further down specify as our ingame # console. if anEventName == "OnGuiActivated": aGui = someEventData[0] print "GUI %s was activated." % aGui.Name def OnInit(): """ This specialised event handler will trigger when the wic.player module is loaded. We can use this to init our python environment. """ # This trick redirects python's output to the ingame # console. It's very helpful when playing around with # python in the console. sys.stdout = wic.player sys.stderr = wic.player # This will tell the game that we have a generic event handler # that can take care of client events that we have no specialised # handler for. In this case it's our EventHandler function. wic.ClientEventHandler = EventHandler
Now, after starting a game press § once to reveal a little of the console. Press escape twice to reveal the game menu and hide it again. You will see quite a lot of text scroll by in the console telling which GUI's opens. But we maybe want to see which GUI's close as well. Ok, lets try some runtime hacking. First ALT+Tab to your python editor and change the EventHandler function to look like this:
def EventHandler( anEventName, someEventData ): """ This function will be registered as our generic event handler. Any client event without a specialised handler will be passed to this function. Unhandled events will just pass through silently. """ # If it's a GUI activation we print the name of the # GUI to stdout which we further down specify as our ingame # console. if anEventName == "OnGuiActivated": aGui = someEventData[0] print "GUI %s was activated." % aGui.Name # And we do the same thing for GUI deactivation. elif anEventName == "OnGuiDeactivated": aGui = someEventData[0] print "GUI %s was dectivated." % aGui.Name
Go back to the game and open the console fully with another press of the § button. Then do this:
py import wic, client py reload( client ) py wic.ClientEventHandler = client.EventHandler
Now press § twice to make the console first disappear and then open it halfway. If you press esc a few times, revealing and hiding the game menu you will see even more text scroll by.
Reacting to Button events
So, lets say that we would like to react when the player presses a certain button. In preparation for this we start by adding a button named Button_CreateTank to the GUI screen named Combat in the ingame gui. This button will create an M1 Abrams tank at the players deployment zone. To do this we will need to alter the server.py for the map as well. But we start with the client.py:
import sys, wic def EventHandler( anEventName, someEventData ): """ This function will be registered as our generic event handler. Any client event without a specialized handler will be passed to this function. Events that isn't handled will just pass through silently. """ # See if it's a button that was pressed. if anEventName == "OnButtonPressed": aGui = someEventData[0] aHashedButtonName = someEventData[1] # Check if it was the button named Button_CreateTank if aHashedButtonName == wic.common.StringToInt( "Button_CreateTank" ): # Tell the server about the event wic.player.SendPlayerEvent( "CreateTankButtonPressed" ) def OnInit(): """ This specialised event handler will trigger when the wic.player module is loaded. We can use this to init our python environment. """ # This trick redirects python's output to the ingame # console. It's very helpful when playing around with # python in the console. sys.stdout = wic.player sys.stderr = wic.player # This will tell the game that we have a generic event handler # that can take care of client events that we have no specialised # handler for. In this case it's our EventHandler function. wic.ClientEventHandler = EventHandler
The in server.py we add a handler for the event. Some of the code below are taken from the Unit examples. :
import sys, wic from wic.common import StringToInt as s2i from wic.common.math import Vector3 def EventHandler( anEventName, someEventData ): """ This function will be registered as our generic event handler. Any server event without a specialized handler will be passed to this function. Events that isn't handled will just pass through silently. """ # See if it's a player event if anEventName == "OnPlayerEvent": anEvent = someEventData[0] aPlayer = someEventData[1] if anEvent == "CreateTankButtonPressed": dropzone = wic.game.GetDropzone() wic.game.CreateUnit( s2i( 'US_Tank_Abrahms' ), dropzone, 0.0, aPlayer.Id, aPlayer.Team ) def OnInit(): """ This specialised event handler will trigger when the wic.player module is loaded. We can use this to init our python environment. """ # This will tell the game that we have a generic event handler # that can take care of server events that we have no specialised # handler for. In this case it's our EventHandler function. wic.ServerEventHandler = EventHandler