Jump to content

Change the global settings through the GUI.


photo

Recommended Posts

Hi 908317477,

So, you want to use UI widgets to control some global settings (e.g. rendering) and load nodes stored in .node files to the current world, please correct me if I'm wrong.

Below is an example of doing this.

Add the following lines to your AppWorldLogic.h file:

// AppWorldLogic.h

//... 
#include <UnigineLights.h>// <-- header for light sources
#include <UnigineWidgets.h>// <-- header for widgets
#include <UnigineConsole.h>// <-- header for the Console
#include <UnigineRender.h>// <-- header for the Render
#include <UnigineEditor.h>// <-- header for the Editor

class AppWorldLogic : public Unigine::WorldLogic
{
	//... 
public:
	//... 
	
  	// declaring pointers to widgets
	Unigine::WidgetSliderPtr sun_slider;
	Unigine::WidgetSliderPtr haze_slider;
	Unigine::WidgetLabelPtr slider1_label;
	Unigine::WidgetLabelPtr slider2_label;
	Unigine::WidgetCheckBoxPtr check_box;
	Unigine::WidgetButtonPtr load_button;
	
    // callbacks for UI widgets
	void onSunSliderChanged();
	void onHazeSliderChanged();
	void OnCheckBoxChanged();
	void onButtonClicked();
};

And the following ones to your AppWorldLogic.cpp file:

// AppWorldLogic.cpp

// ...

// injecting namespaces
using namespace Unigine;
using namespace Math;

// ...

// callback to be fired on changing the state of the sun control slider
void AppWorldLogic::onSunSliderChanged()
{
	// trying to get a node named "sun"
	NodePtr node = Editor::get()->getNodeByName("sun");
	// if such node is found changing its position using the new angle determined by the slider value
	if (node)
		LightWorld::cast(node)->setWorldRotation(Math::quat(Math::vec3(0.0f, 1.0f, 0.0f), 180.0f - sun_slider->getValue()));
}

// callback to be fired on changing the state of the haze control slider
void AppWorldLogic::onHazeSliderChanged()
{
	// changing global rendering settings via the Render class
	Render::get()->getEnvironmentPreset(0)->setHazeMaxDistance(haze_slider->getValue());
}

// callback to be fired on changing the state of the checkbox
void AppWorldLogic::OnCheckBoxChanged()
{
	// changing global rendering settings via the Console
	String command = String::format("render_environment %d", check_box->isChecked());
	Console::get()->run(command.get());
}
// callback to be fired on clicking the button
void AppWorldLogic::onButtonClicked()
{
	// loading a node to the current world
	World::get()->loadNode("Cylinder.node");
}

int AppWorldLogic::init()
{
  	// getting a pointer to the GUI
	GuiPtr gui = Gui::get();

  	// creating a checkbox widget for environment rendering control and adding it to GUI
	check_box = WidgetCheckBox::create(gui, "Environment Rendering");
	check_box->setToolTip("Toggle environment rendering on and off");
	check_box->setPosition(10, 30);
	check_box->setChecked(1);
	gui->addChild(check_box->getWidget(), Gui::ALIGN_OVERLAP | Gui::ALIGN_FIXED);
	check_box->addCallback(Gui::CHANGED, MakeCallback(this, &AppWorldLogic::OnCheckBoxChanged));

	// creating a slider widget for sun position control and adding it to GUI
	sun_slider = WidgetSlider::create(gui, 0, 360, 90);
	sun_slider->setWidth(360);
	sun_slider->setPosition(150, 50);
	gui->addChild(sun_slider->getWidget(), Gui::ALIGN_OVERLAP | Gui::ALIGN_FIXED);
	sun_slider->addCallback(Gui::CHANGED, MakeCallback(this, &AppWorldLogic::onSunSliderChanged));

	// creating a slider widget for haze distance control and adding it to GUI
	haze_slider = WidgetSlider::create(gui, 0, 360, 10);
	haze_slider->setWidth(360);
	haze_slider->setPosition(150, 70);
	gui->addChild(haze_slider->getWidget(), Gui::ALIGN_OVERLAP | Gui::ALIGN_FIXED);
	haze_slider->addCallback(Gui::CHANGED, MakeCallback(this, &AppWorldLogic::onHazeSliderChanged));
	
	// creating two label widgets for sliders and adding them to GUI
	slider1_label = WidgetLabel::create(gui, "Sun position:");
	slider1_label->setToolTip("Change sun position");
	slider1_label->setPosition(10, 50);
	gui->addChild(slider1_label->getWidget(), Gui::ALIGN_OVERLAP | Gui::ALIGN_FIXED);
	slider2_label = WidgetLabel::create(gui, "Haze Distance:");
	slider2_label->setToolTip("Change maximum haze distance");
	slider2_label->setPosition(10, 70);
	gui->addChild(slider2_label->getWidget(), Gui::ALIGN_OVERLAP | Gui::ALIGN_FIXED);

	// creating a button widget and adding it to GUI
	load_button = WidgetButton::create(gui, "Load Node");
	load_button->setToolTip("Loads some node");
	load_button->setPosition(10, 90);
	load_button->addCallback(Gui::CLICKED, MakeCallback(this, &AppWorldLogic::onButtonClicked));
	gui->addChild(load_button->getWidget(), Gui::ALIGN_OVERLAP | Gui::ALIGN_FIXED);
  
	// ...
}

Hope this helps!

Thank you!

  • Like 1
Link to comment
×
×
  • Create New...