Jump to content

How to organize complex programs?


Vida Marcell
 Share

Recommended Posts

I use this as my base class and make derived classes for each major part of a program, usually a window or some panel with a lot of controls on it. You can listen for a specific widget and evaluate everything that happens in ProcessEvent():

UIElement.hUIElement.cpp

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Here is an example of a derived class. I create a panel for the area of the interface this is creating, and add all the widgets it uses. For any widgets it uses, I call Listen() so those events will get evaluated by ProcessEvent(), where they are all handled:

AboutWindow.h

#pragma once
#include "UltraEngine.h"
#include "../Editor.h"

class AboutWindow : public UIElement
{
public:
	shared_ptr<Window> window;
	shared_ptr<Widget> closebutton;
	virtual bool Initialize(shared_ptr<Window> parent);
	virtual bool ProcessEvent(const Event& e);
};

AboutWindow.cpp

#pragma once
#include "UltraEngine.h"
#include "../Editor.h"

bool AboutWindow::Initialize(shared_ptr<Window> parent)
{
	int statusheight = 40;
	iVec2 size = iVec2(600,380);
	window = CreateWindow("",0,0,size.x,size.y,parent,WINDOW_CENTER | WINDOW_TITLEBAR | WINDOW_HIDDEN);
	auto ui = CreateInterface(window);
	//ui->root->SetColor(0, 0, 0, 1);
	auto panel = CreatePanel(0,0,ui->root->size.x,ui->root->size.y-statusheight,ui->root, PANEL_BORDER);
	panel->SetColor(panel->color[WIDGETCOLOR_SUNKEN]);
	panel->SetLayout(1, 1, 1, 1);
	WString apptitle, appversion;
	apptitle = JSONGetString(editor->config["appTitle"]);
	appversion = JSONGetString(editor->config["appVersion"]);
	auto label = CreateLabel(apptitle + " " + appversion + "\nCopyright Ultra Software 2021", 4, 0 + ui->root->ClientSize().y - statusheight, panel->ClientSize().x, statusheight, ui->root, LABEL_MIDDLE);
	label->SetLayout(1, 0, 0, 1);
	label->SetFontScale(editor->smallfontscale);
	label->SetColor(editor->smallfontcolor, WIDGETCOLOR_FOREGROUND);
	auto logopanel = CreatePanel(0, panel->size.y / 2 - 40, panel->size.x*0.6, 60, panel);
	auto logo = LoadIcon("Icons/UltraEngine.svg");
	auto logop = logo->Rasterize(0.4);
	logopanel->SetPixmap(logop);
	auto splash = LoadPixmap("splashscreen.dds",0,0,LOAD_QUIET);
	panel->SetPixmap(splash,PIXMAP_COVER);
	logopanel->SetColor(1, 1, 1, 0);
	auto link = CreateHyperlink(JSONGetString(editor->config["url"]), ui->root->size.x - 150 - 4, ui->root->size.y - statusheight, 150, statusheight, ui->root, LABEL_MIDDLE | LABEL_RIGHT);
	link->SetLayout(0, 1, 0, 1);
	link->SetFontScale(editor->smallfontscale);
	link->SetColor(editor->smallfontcolor, WIDGETCOLOR_FOREGROUND);
	closebutton = CreateButton("Close",-200,-200,100,100,ui->root,BUTTON_CANCEL);
  
  	//Listen for events
	Listen(EVENT_WIDGETACTION, closebutton);
	Listen(EVENT_WINDOWCLOSE, window);
  
  	//Rescale interface
	if (parent->display->scale != 1.0f)
	{
		size.x = window->size.x * parent->display->scale;
		size.y = window->size.y * parent->display->scale;
		window->SetShape((parent->display->size.x - size.x)/2, (parent->display->size.y - size.y)/2,size.x,size.y);
		ui->SetScale(parent->display->scale);
	}
	return true;
}

bool AboutWindow::ProcessEvent(const Event& e)
{
	switch (e.id)
	{
	case EVENT_WIDGETACTION:
		if (e.source == closebutton)
		{
			EmitEvent(EVENT_WINDOWCLOSE, window);
		}
		break;
	case EVENT_WINDOWCLOSE:
		if (e.source == window)
		{
			window->GetParent()->Activate();
			window->SetHidden(true);
		}
		break;
	}
	return true;
}

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...