Skip to content
Half People edited this page Nov 8, 2023 · 21 revisions

CreateWidget

You can go to Object/DefaultWidget.h in the main project or PluginMain/HPlugin.hin the PluginExample project to see more examples :)

1.create your WidgetClass

/*Your Widget Calss Name*/
class MyWidget :public HWidget
{
public:
	MyWidget/*Your Widget Calss Name*/()
	{
	}

	virtual void DrawIconForControlPanel()override
	{
		return;
	}
	virtual std::string Export(std::string Offset) override
	{
	}
	virtual void Draw()override
	{
	}
	virtual void DetailPanelWidget()override
	{
                //The details panel draws content when this Widget is selected
		return;
	}
	virtual HWidget* CreateSelfClass()override { /*Return self class e.g. return new MyWidget();*/ }

	virtual json Copy()override {
		json J;
		PreCopy(J);
                // Save all variables used by Widget       e.g. J["Index"] = index;
		return J;
	}
	virtual void Paste(json Data)override {
		PrePaste(Data);
                // Load all variables Save used by Widget  e.g. index = Data["Index"];   HVFloat * Value;
		return;
	}
private:
//What variables this Widget needs to input or use can be added here.    e.g. int index = 0;
};

2.Add basic information

//Required  Since the project contains a translation function, it needs to have two Widget names.
static std::string MyWidgetName = "MyWidget";
static std::string MyWidgetID = "MyWidget";
//Optional
//Can be used alone
static std::string MyWidgetInculdPath = "/*Need to use local path*/";
//Can be used alone
static std::string MyWidgetLibPath = "/*Need to use local path*/";
//Can be used alone
static std::vector<std::string> MyWidgetInculd = {
    .../*.h file*/
};
//Can be used alone
static std::vector<std::string> MyWidgetRequestCompileFile = {
    .../*.cpp file*/
};
//Need to cooperate MyWidgetInculd for use
static std::string MyWidgetLib = 
"... /*single lib : e.g. #pragma comment(lib,"Test.lib")     multiple lib : #pragma comment(lib,"Test.lib") \n #pragma comment(lib,"Test1.lib") */";
//Can be used alone
//Some small Widgets that can be implemented with just one function can add the function here
static std::string MyWidgetFunction = R("
...
//e.g. namespace HEditor {
//    void DrawHButton(){
//        ImGui::Button("Hello Wrold");
//        ImGui::Text("(●''●)");
//    }
//}
");
class MyWidget :public HWidget
{
    MyWidget()
    {
        //Required
        WidgetName = &MyWidgetName;
        WidgetNameID = &MyWidgetID;
        // Modify available movement methods
        HArrowFlag = HArrow_SizeFlag_Default; //e.g. HArrow_SizeFlag_NotResize  HArrow_SizeFlag_OnlyX  HArrow_SizeFlag_Default
        //Optional  Configure according to the variables you configured above
        Inculd = &MyWidgetInculd ;
        RequestCompileFile = &MyWidgetRequestCompileFile ;
        Lib = &MyWidgetLib ;
        Function = &MyWidgetFunction ;
        LibPath = &MyWidgetLibPath ;
        InculdPath = &MyWidgetInculdPath ;

        //Latest variable system  (Use only in the following situations : The data in this ImGui Widget is an item, not a variable in the editor. )
        DragBuffer= new HVInt(""/*ValueName*/);
        //Since this variable must output a variable, it needs to be modified. The next line is the modified result. If it is not necessary to output the variable, just do it like the previous line.
        DragBuffer= new HVInt(""/*ValueName*/,0/*init value*/,true/*ExposedAsVariable*/,false/*can choose can Exposed As Variable*/);
        HValues.AddHValue(DragBuffer); // Add to data set
    }
private:
    //Latest variable system  (Use only in the following situations : The data in this ImGui Widget is an item, not a variable in the editor. )
    HVInt * DragBuffer;
}

3.Add Widget icon

	virtual void DrawIconForControlPanel()override
	{
                //The icon does not need to be interactive, so if it needs a value, you can create a variable here
                //e.g. int DemoInt = 0;
                //ImGui::DragInt("DragInt",&DemoInt);
		return;
	}

4.Create own class

virtual HWidget* CreateSelfClass()override { return new MyWidget()/*return new Your Class();*/; }

5.Define required variables

class MyWidget :public HWidget
{
   ...
private:
	char Title[200] = { "DragInt" };
	Int DragBuffer;
	float Speed = 1;
	int Mmin = 0;
	int Mmax = 10;
	char Mformat[200] = { "% .3f" };
	ImGuiSliderFlags Mflage;
};

6.Drawing details panel editable content

	virtual void DetailPanelWidget()override
	{

		ImGui::InputText("Title", Title, sizeof(Title));
		ImGui::DragFloat("Speed", &Speed, 0.1, 0.01, 100000);
		ImGui::DragInt("min", &Mmin, 0.1, -100000);
		ImGui::DragFloat("max", &Mmax, 0.1, 0, 100000);
		ImGui::InputText("format", Mformat, sizeof(Mformat));
                
                ImGui::DragInt("Value",&DragBuffer);//old variable system
                DragBuffer->Draw("Value",this)//New variable system

		if (ImGui::TreeNode("Flags"))
		{
			ImGui::CheckboxFlags("ImGuiSliderFlags_None           ", &Mflage, ImGuiSliderFlags_None);
			ImGui::CheckboxFlags("ImGuiSliderFlags_AlwaysClamp    ", &Mflage, ImGuiSliderFlags_AlwaysClamp);
			ImGui::CheckboxFlags("ImGuiSliderFlags_Logarithmic    ", &Mflage, ImGuiSliderFlags_Logarithmic);
			ImGui::CheckboxFlags("ImGuiSliderFlags_NoRoundToFormat", &Mflage, ImGuiSliderFlags_NoRoundToFormat);
			ImGui::CheckboxFlags("ImGuiSliderFlags_NoInput        ", &Mflage, ImGuiSliderFlags_NoInput);
			ImGui::CheckboxFlags("ImGuiSliderFlags_InvalidMask_   ", &Mflage, ImGuiSliderFlags_InvalidMask_);
			ImGui::TreePop();
		}

		return;
	}

7. save data

	virtual json Copy()override {
		json J;
		PreCopy(J);
		J["WidgetTitle"] = Title;
		J["Buff"] = DragBuff;//old variable system
                                     //The new variable system does not require
		J["MSpeed"] = Speed;
		J["SMin"] = Mmin;
		J["SMax"] = Mmax;
		J["Format"] = Mformat;
		J["Flage"] = Mflage;
		return J;
	}

8.load data

	virtual void Paste(json Data)override {
		PrePaste(Data);
		std::string ST = Data["WidgetTitle"];
		strcpy_s(Title, 200, ST.c_str());

		DragBuff = Data["Buff"];//old variable system
                                        //The new variable system does not require
		Speed = Data["MSpeed"];
		Mmin = Data["SMin"];
		Mmax = Data["SMax"];
		ST = Data["Format"];
		strcpy_s(Mformat, ST.c_str());

		Mflage = Data["Flage"];

		return;
	}

9.Draw in the main window

	virtual void Draw()override
	{
		DrawPreLogic();
		ImGui::SetNextItemWidth(WidgetSize.x);
		ImGui::DragInt(std::string(Title).append("###").append(std::to_string((int)this)).c_str()/*You can also use "GetID"function (Deprecated)*/, &DragBuff/*old variable system*/    DragBuff->Get()/*New variable system*/, Speed, Mmin, Mmax, Mformat, Mflage);
		DrawLogicTick();
		return;
	}

If your Widget can place content, you can refer to the following code This code is Drawfunction in Child Widget

	virtual void Draw()override
	{
		DrawPreLogic();
		if (ImGui::BeginChild(GetID().c_str(), *WidgetSize->Get(), border, ChildFlag))
		{
			HWidgetDragSpace(Content);// << -- Add the ability to place content   The "Content" variable has been defined in "HWidget"
		}
		ImGui::EndChild();
		DrawLogicTick();
		return;
	}

10.output code

If you have content, you can use std::string GetItemsExport(std::string Offset) this function to obtain the output code of the content.

	virtual std::string Export(std::string Offset) override
	{       
                 
                //Get variable names that will not be repeated  old variable system
                // (Of course it is also applicable now for saving.You want to control higher variables)
                // The new variable system does not need to be added here. It will be automatically completed after the data is uploaded.
		std::string RandText = GetRandText((int)this);
                HVariableExport HV;
                HV.VariableCode.append("\nstatic int DragInt_").append(RandText).append(" = ").append(std::to_string(DragBuff)).append(";");
                //Added to the global variables in the variable file  (If you do not use EVariable, you can also use ECacheVariable.
ECacheVariable is designed to save global data using binary saves such as fonts or images.)
                EVariable.push_back(HV);
                

                //You can add an initializer definition here if needed
                //Normally it should be used, for example, to initialize image variable data, etc. The following is the simplest way to use it.
                //e.g. InitializationCodes.push_back(std::string("\nDragInt_").append(RandText).append(" = 25;");

                std::string SaveExportText;
               //old variable system		 
              SaveExportText.append("\n").append(Offset).append("if(ImGui::DragInt(\"").append(TextData).append("\",&DragInt_").append(RandText).append(" ,").append(std::to_string(Speed)).append(",").append(std::to_string(Mmin)).append(",").append(std::to_string(Mmax)).append(",\"").append(Mformat).append("\",").append(std::to_string(Mflage)).append("))");
               //New variable system
              SaveExportText.append("\n").append(Offset).append("if(ImGui::DragInt(\"").append(TextData).append("\",&").append(DragBuff->AutoGetOutputValue(this)).append(" ,").append(std::to_string(Speed)).append(",").append(std::to_string(Mmin)).append(",").append(std::to_string(Mmax)).append(",\"").append(Mformat).append("\",").append(std::to_string(Mflage)).append("))");
		SaveExportText.append("\n").append(Offset).append("{");
		SaveExportText.append("\n").append(Offset).append("}");
		return SaveExportText;
	}

This function will output variable.h

static int DragInt_OtkCP = 0;

Widget.cpp

if(DragInt("DragInt",&DragInt_OtkCP,1,-1000,10000,"% .3f",0))
{
}