DirectGUI

Since DirectGUI is implemented in Python, it is useless to C++ programmers. However, there is another GUI system, PGui, that forms the foundation of DirectGUI and is in fact implemented in C++.

Let’s take a look at some PGui functions.

The PGui C++ modules

[PGButton]

#include "pgButton.h"

with PGButton you can create a freely definable clickable button.

It can hold four different states:

ready

The default state of an untouched button

pressed

The pressed state (the user clicked on it)

roll over

When the user hovers over the button without clicking

inactive

Disabled state

Example usage, creating a texture for each state:

PT(PGButton) my_button;
my_button = new PGButton("MyButton");
my_button->setup("Button", 0.1);
PT(Texture) button_ready = TexturePool::load_texture("button.png");
PT(Texture) button_rollover = TexturePool::load_texture("button_active.png");
PT(Texture) button_pressed = TexturePool::load_texture("button_pressed.png");
PT(Texture) button_inactive = TexturePool::load_texture("button_inactive.png");

// PGFrameStyle is a powerful way to change the appearance of the button:
PGFrameStyle MyStyle=my_button->get_frame_style(0); // frame_style(0): ready state
MyStyle.set_type(PGFrameStyle::T_flat);

MyStyle.set_texture(button_ready);    my_button->set_frame_style(0, MyStyle);
MyStyle.set_texture(button_rollover); my_button->set_frame_style(1, MyStyle);
MyStyle.set_texture(button_pressed);  my_button->set_frame_style(2, MyStyle);
MyStyle.set_texture(button_inactive); my_button->set_frame_style(3, MyStyle);

NodePath defbutNP = window->get_aspect_2d().attach_new_node(my_button);
defbutNP.set_scale(0.1);

// Setup callback function
framework.define_key(my_button->get_click_event(MouseButton::one()), "button press", &GUI_Callback_Button_Clicked, my_button);

An example of a callback function:

static void GUI_Callback_Button_Clicked(const Event *ev, void *data) {
  PGButton *button = (PGButton *)data;
  // Your action here
  printf("%s has been pressed.\n", button->get_name().c_str());
}

[PGSliderBar]

#include "pgSliderBar.h"

A simple sliderbar.

This slider basically has four components:

  • The thumb button. This is the sliding part.

  • The slider. This is the ‘rail’ the thumb button slides along.

  • The left button. User can click on it to slide the thumb button to the left.

  • The right button. User can click on it to slide the thumb button to the right.

Using default initialisation, none of these components are textured.

PT(PGSliderBar) Slider = new PGSliderBar("MySliderBar");

// Setup, feeding the constructor with (bool vertical,float length,float width,float bevel)
Slider->setup_slider(false, 0.1, 0.1, 0); // 'rail' properties
Slider->set_range(0,1);
Slider->set_value(0.5);

// Setup scroll bar (the 'moving thumb button' including left and right button)
Slider->setup_scroll_bar(false, 0.35, 0.05, false);
NodePath SliderNP=window->get_aspect_2d().attach_new_node(Slider);
SliderNP.set_pos(0, 0, 0);

(work in progress, more to come soon.)