Skip to content

ExampleGUI

da772 edited this page Feb 29, 2020 · 5 revisions

Creating GUI

GUIs, or Graphical User Interfaces, are created to allow for visual user input into an application. GUIs are components which are added to actors to display on the screen. GUIs can also have children which allows for bundling multiple GUI components into one single component.

Elements

Creating a simple Textured GUI

TestScene.java

@Override
public void OnBegin() {
   Actor myActor = new Actor("myActor"); // Creates new actor called my actor on this scene, takes in identifier string
   // Create a blank white quad that is 25% of the screen size in the middle of the screen
   GUIQuad texturedGUI = new GUIQuad(
           "myGUI", // Identifying string
           new Transform( // Transform (position, rotation, scale)
               new Vector3f(0f,0f,0f), // Position -1 to 1, -1 being far left of screen 1 being far right and 0 being 
                                       //  center, the Z position indicates the order it should be drawn, higher on top
               new Vector3f(0f,0f,0f), // Rotation
               new Vector3f(.25f,.25f,1f) // Scale, based on percentage of screen, ex 1 = entire screen, .5 = half of 
          ),                              // screen
          "Images/blankTexture.png", // Texture image, blankTexture.png is an empty square
          new Vector4f(1f,1f,1f,1f)  // Color of the quad (red, green, blue, alpha)
          );
   myActor.AddComponent(texturedGUI);
}

Creating a Text GUI element

TestScene.java

@Override
public void OnBegin() {
   Actor myActor = new Actor("myActor"); // Creates new actor called my actor on this scene, takes in identifier string  
   // Creates red text that takes up 25% of the width of the screen.
   GUIText guiText = new GUIText(
        "myGUI", // Identifying string
        new Transform( // Transform (position, rotation, scale)
        new Vector3f(0f,0f,1f),// Position -1 to 1, -1 being far left of screen 1 being far right 
                               // and 0 being center, the Z position indicates the order it should be drawn, higher on top
        new Vector3f(0f,0f,0f), // Rotation
        new Vector3f(1f,1f,1f) // Scale, based on percentage of screen, ex 1 = entire screen, .5 = half of screen
	),
        "Fonts/BebasNeue", // Name and location of font to use
        "This is a GUI element which writes text to the screen!", // String to draw
        new Vector4f(1f,0f,0f,1f),  // Color of the quad (red, green, blue, alpha)
        .25f, // Max Width of each line, 1 is entire screen .5 is half etc.
         2f, // Font size
         true); // Center text?

   myActor.AddComponent(guiText);
}

Creating a Button Gui Element

TestScene.java

@Override
public void OnBegin() {
   Actor myActor = new Actor("myActor"); // Creates new actor called my actor on this scene, takes in identifier string  
   // Creates button that takes up 25% of the screen
   GUIButton guiButton = new GUIButton(
        "myGUI", // Identifying string
        new Transform( // Transform (position, rotation, scale)
        new Vector3f(0f,0f,1f),// Position -1 to 1, -1 being far left of screen 1 being far right 
                               // and 0 being center, the Z position indicates the order it should be drawn, higher on top
        new Vector3f(0f,0f,0f), // Rotation
        new Vector3f(.25f,.25f,1f) // Scale, based on percentage of screen, ex 1 = entire screen, .5 = half of screen
	),
        "Images/Buttons/mainMenuButtonUp.png", // Normal button image 
        "Images/Buttons/mainMenuButtonDown.png", // Pressed button image
        new Vector4f(1f,0f,0f,1f)  // Color of the quad (red, green, blue, alpha)
         ) {
             @Override
             protected void OnSelect() {
               // When the mouse is over the button
             }

             @Override
             protected void OnDeselect() {
               // When the mouse leaves the button
             }

             @Override
             protected void OnMousePressed() {
               // When the mouse is down on the button
             }

             @Override
             protected void OnMouseReleased() {
               // When the mouse is released on the buttton
             }


           };

   myActor.AddComponent(guiButton);
}

Creating a Slider Gui Element

TestScene.java

@Override
public void OnBegin() {
   Actor myActor = new Actor("myActor"); // Creates new actor called my actor on this scene, takes in identifier string  
   // Creates slider background
   GUISlider guiSlider = new GUISlider(
        "myGUI", // Identifying string
        new Transform( // Transform (position, rotation, scale)
        new Vector3f(0f,0f,1f),// Position -1 to 1, -1 being far left of screen 1 being far right 
                               // and 0 being center, the Z position indicates the order it should be drawn, higher on top
        new Vector3f(0f,0f,0f), // Rotation
        new Vector3f(.20f,.02f,1f) // Scale, based on percentage of screen, ex 1 = entire screen, .5 = half of screen
	),
        "Images/blankTexture.png", // Texture of slider
        new Vector4f(1f,0f,0f,1f),  // Color of the quad (red, green, blue, alpha)
        0f, // Min value
        50f // Max Value
         ) {
             @Override
             protected void OnValueChanged(float value) {
               // Called when slider moves
             }
           };
   // Creates the slider's draggable bar
   GUISliderBar sliderBar = new GUISliderBar( "mySliderBar", // Identifying string
        new Transform( // Transform (position, rotation, scale)
        new Vector3f(0f,0f,1f),// Position -1 to 1, -1 being far left of screen 1 being far right 
                               // and 0 being center, the Z position indicates the order it should be drawn, higher on top
        new Vector3f(0f,0f,0f), // Rotation
        new Vector3f(.035f,.0175f,1f) // Scale, based on percentage of screen, ex 1 = entire screen, .5 = half of screen
	),
        "Images/blankTexture.png", // Texture of slider
        new Vector4f(1f,0f,0f,1f),  // Color of the quad (red, green, blue, alpha)
        .5f, // Position of bar, 0 - 1
        false // vertical slide?
        );
   guiSlider.AddChild(sliderBar);
   myActor.AddComponent(guiSlider);
}

GUI Interaction

To add interaction to GUI elements you must override the OnMouseEnter function and OnMouseExit function. If the mouse enters or exits the dimensions of the GUI these functions will be called automatically. Most GUI elements have Collision enabled by default, however GUI elements like Text have their collision disabled by default. To disable or enable a GUI elements collision use the SetGUICollision() function and pass either true or false. If an element requires window event information like mouse position, mouse buttons, or key presses they can be marked as selected. Only one GUI can be selected at once, GUI elements are selected by calling the SelectGUI function, and deselected calling the DeselctGUI function. Once selected, all of the window events are passed to the GUI's SelectedOnEvent function. This function allows for the creation of buttons, draggable GUIs, and much much more.

TestScene.java

@Override
public void OnBegin() {
   Actor myActor = new Actor("myActor"); // Creates new actor called my actor on this scene, takes in identifier string
   // Create a blank white quad that is 25% of the screen size in the middle of the screen
   // this GUI will also print a message when left clicked
   GUIQuad texturedGUI = new GUIQuad(
           "myGUI", // Identifying string
           new Transform( // Transform (position, rotation, scale)
               new Vector3f(0f,0f,0f), // Position -1 to 1, -1 being far left of screen 1 being far right and 0 being 
                                       //  center, the Z position indicates the order it should be drawn, higher on top
               new Vector3f(0f,0f,0f), // Rotation
               new Vector3f(.25f,.25f,1f) // Scale, based on percentage of screen, ex 1 = entire screen, .5 = half of 
          ),                              // screen
          "Images/blankTexture.png", // Texture image, blankTexture.png is an empty square
          new Vector4f(1f,1f,1f,1f)  // Color of the quad (red, green, blue, alpha)
          )
          {
	     @Override
	     protected void OnMouseEnter() {
	       // Mouse Entered. Set it as selected GUI
               SelectGUI();
	     }
	     @Override
	     protected void OnMouseExit() {
	       // Mouse Exited. Set it as not selected
               DeselectGUI();
	     }
             @Override
             public void SelectedOnEvent(Event e) {
               //When selected capture events 
               // Determine if mouse has been pressed
               if (e instanceof Events.MouseButtonPressedEvent) { 
                  // Cast event into mouse pressed event
                  Events.MouseButtonPressedEvent mousePressed = (Events.MouseButtonPressedEvent)e;
                  // Get which button was pressed
                  if (mousePressed.GetKeyCode() == KeyCodes.MOUSE_LEFT) {
                    // Print information
                    System.out.println("GUI clicked!");
                  }
                  
               }
             }
          };
   myActor.AddComponent(texturedGUI);
}

GUI Parenting

When adding a child to a GUI the position of the child will change. It will no longer be position based on the screen, but instead be positioned based on the position of its parent. For example, if the parent GUI is 25% to the right and 25% up and the child GUI is 0% to the left and 0% up, it will not be in the center of the screen. It will instead be at the center of the parent's position so child's position will actually be 25% to the right and 25% up.

TestScene.java

@Override
public void OnBegin() {
   Actor myActor = new Actor("myActor"); // Creates new actor called my actor on this scene, takes in identifier string  
   GUIQuad texturedGUI = new GUIQuad(
           "myGUI", // Identifying string
           new Transform( // Transform (position, rotation, scale)
               new Vector3f(0f,0f,0f), // Position -1 to 1, -1 being far left of screen 1 being far right and 0 being 
                                       //  center, the Z position indicates the order it should be drawn, higher on top
               new Vector3f(0f,0f,0f), // Rotation
               new Vector3f(.25f,.25f,1f) // Scale, based on percentage of screen, ex 1 = entire screen, .5 = half of 
          ),                              // screen
          "Images/blankTexture.png", // Texture image, blankTexture.png is an empty square
          new Vector4f(1f,1f,1f,1f)  // Color of the quad (red, green, blue, alpha)
          );
   GUIText guiText = new GUIText(
        "myGUI", // Identifying string
        new Transform( // Transform (position, rotation, scale)
        new Vector3f(0f,0f,1f),// Position -1 to 1, -1 being far left of screen 1 being far right 
                               // and 0 being center, the Z position indicates the order it should be drawn, higher on top
        new Vector3f(0f,0f,0f), // Rotation
        new Vector3f(1f,1f,1f) // Scale, based on percentage of screen, ex 1 = entire screen, .5 = half of screen
	),
        "Fonts/BebasNeue", // Name and location of font to use
        "This is a GUI element which writes text to the screen!", // String to draw
        new Vector4f(1f,0f,0f,1f),  // Color of the quad (red, green, blue, alpha)
        .25f, // Max Width of each line, 1 is entire screen .5 is half etc.
         2f, // Font size
         true); // Center text?
  

 // Adds the text as a child to the quad, in order to have the text over the quad the z position of the text must be 
 // greater than 0. The child's z position will always be added to the parent's z position so as long as the child has a z 
 // position that's higher than 0 it will be on top
   texturedGUI .AddChild(guiText);
// Adds the gui to the actor
   myActor.AddComponent(texturedGUI);

}

Previous | Next

Clone this wiki locally