Skip to content

Create HValue (Create a new variable system variable class)

Half People edited this page Oct 22, 2023 · 3 revisions
  1. Open HValue.h image
  2. Select copy type
  • If your type cannot be linearly transitioned, please select HVBool and copy and add a one.
  • If you change the data, you can linearly transition,Please select HVImVec2 and copy and add a one.
  1. Modify name
class HVFloat/*<- Rename this*/ : public HValue
{
public:
	HVFloat/*<- Rename this*/(const char* Name, float init = 0, bool ExposedAsVariable_ = false, bool CanExposedAsVariable_ = true)

4.Modify variable type (Although there are many functions as you read, please don’t worry about the brief introduction.) Below I will modify it to ImVec4 .And where I have modified it, I will add /*<-*/

class HVImVec4 /*<-*/: public HValue
{
public:
	bool usecoloredit = false;
	HVImVec4/*<-*/(const char* Name, ImVec4 /*<-*/init = ImVec4()/*<-*/, bool UseColorEdit = false, bool ExposedAsVariable_ = false, bool CanExposedAsVariable_ = true)
	{
		ExposedAsVariable = ExposedAsVariable_;
		CanExposedAsVariable = CanExposedAsVariable_;
		Name_ = Name;
		InitData = new ImVec4/*<-*/(init);
		Data = new ImVec4/*<-*/(init);
		HaveFlagMode = true; // If your variable does not have linear interpolation function, please change it to false here.
		usecoloredit = UseColorEdit;
                //CanAddToTimeLine             //There are two variables here that can be modified. Please modify them as needed.
                //ExporsedVariableToDefault    //
	}
	virtual std::string GetType()override {
		return	"ImVec4"/*<-*/;
	};
	virtual void DrawWidgetCallBack(const char* label) override
	{
		if (usecoloredit)/*<-*/
			ImGui::ColorEdit4(label, (float*)Data,ImGuiColorEditFlags_::ImGuiColorEditFlags_AlphaBar);/*<-*/
		else/*<-*/
			ImGui::DragFloat4(label, (float*)Data);/*<-*/
	}
	virtual void TimeLineUpdataValueCallBack(std::vector<void*> datas, std::vector<int>keys, HAnimation::KeyFlag flag) override
	{
		if (flag == HAnimation::KeyFlag_None)
		{
			void* buffer = UseKeyFindData(datas, keys, HAnimation::CurrentSequenFrame);
			if (Data && buffer != 0)
			{
				*(ImVec4*)/*<-*/Data = *(ImVec4*)/*<-*/buffer;
			}
		}
                //In this function, only the data required for linear interpolation requires the following code.
		else if (flag == HAnimation::KeyFlag_LinearInterpolation)
		{
			HAnimation::InterpolationInfo info = UseKeyFindT(keys, HAnimation::CurrentSequenFrame);


			(*(ImVec4*)/*<-*/Data).x = HAnimation::LinearInterpolation((*(ImVec4*)/*<-*/datas[info.PreviousKey]).x, (*(ImVec4*)/*<-*/datas[info.LastOneKey]).x, info.alpha);
			(*(ImVec4*)/*<-*/Data).y = HAnimation::LinearInterpolation((*(ImVec4*)/*<-*/datas[info.PreviousKey]).y, (*(ImVec4*)/*<-*/datas[info.LastOneKey]).y, info.alpha);
			(*(ImVec4*)/*<-*/Data).z = HAnimation::LinearInterpolation((*(ImVec4*)/*<-*/datas[info.PreviousKey]).z, (*(ImVec4*)/*<-*/datas[info.LastOneKey]).z, info.alpha);
			(*(ImVec4*)/*<-*/Data).w = HAnimation::LinearInterpolation((*(ImVec4*)/*<-*/datas[info.PreviousKey]).w, (*(ImVec4*)/*<-*/datas[info.LastOneKey]).w, info.alpha);
		}
		else if (flag == HAnimation::KeyFlag_SimpleBezierInterpolation)
		{
			HAnimation::InterpolationInfo info = UseKeyFindT(keys, HAnimation::CurrentSequenFrame);


			(*(ImVec4*)/*<-*/Data).x = HAnimation::SimpleBezierInterpolation((*(ImVec4*)/*<-*/datas[info.PreviousKey]).x, (*(ImVec4*)/*<-*/datas[info.LastOneKey]).x, info.alpha);
			(*(ImVec4*)/*<-*/Data).y = HAnimation::SimpleBezierInterpolation((*(ImVec4*)/*<-*/datas[info.PreviousKey]).y, (*(ImVec4*)/*<-*/datas[info.LastOneKey]).y, info.alpha);
			(*(ImVec4*)/*<-*/Data).z = HAnimation::SimpleBezierInterpolation((*(ImVec4*)/*<-*/datas[info.PreviousKey]).z, (*(ImVec4*)/*<-*/datas[info.LastOneKey]).z, info.alpha);
			(*(ImVec4*)/*<-*/Data).w = HAnimation::SimpleBezierInterpolation((*(ImVec4*)/*<-*/datas[info.PreviousKey]).w, (*(ImVec4*)/*<-*/datas[info.LastOneKey]).w, info.alpha);
		}
	}
	virtual HValue* CreateSelf(const char* Name) override {
		return new HVImVec4/*<-*/(Name);
	};
	virtual std::string GetValue(void* TargetData) override
	{
		return std::string("ImVec4(").append(std::to_string((*(ImVec4*)TargetData).x)).append(",").append(std::to_string((*(ImVec4*)TargetData).y)).append(",").append(std::to_string((*(ImVec4*)TargetData).z)).append(",").append(std::to_string((*(ImVec4*)TargetData).w)).append(")");/*<-*/     //Here you need to know how to create a value to other variables when outputting this variable into a variable.
	}
	virtual HVariableExport GetVariableExport(HWidget* Widget, bool InitVariable = true, bool UseStatic = true) override
	{
		//Example code :
		HVariableExport ExportBuffer;
		ExportBuffer.Comment = "--ImVec4--";/*<-*/
		ExportBuffer.VariableCode = std::string("\n	"/*\n <-This have a TAB*/);
		if (UseStatic)
			ExportBuffer.VariableCode.append("static ");
		ExportBuffer.VariableCode.append("ImVec4");//<-----your value type   /*<-*/
		ExportBuffer.VariableCode.append(" ").append(GetVariableName(Widget));
		if (InitVariable && Data)
			ExportBuffer.VariableCode.append(" = ").append(GetValue(Data));
		ExportBuffer.VariableCode.append(";");
		return ExportBuffer;
	}
	virtual json Save() override
	{
		json J, v;
		v["x"] = (*(ImVec4*)Data).x;
		v["y"] = (*(ImVec4*)Data).y;
		v["z"] = (*(ImVec4*)Data).z;
		v["w"] = (*(ImVec4*)Data).w;
		J["Value"] = v;
		return J;
                /*<-*/ //Here you need to modify it according to what can be saved in your variables.
	}
	virtual void Load(json J) override
	{
		json v = J["Value"];
		(*(ImVec4*)Data).x = v["x"];
		(*(ImVec4*)Data).y = v["y"];
		(*(ImVec4*)Data).y = v["z"];
		(*(ImVec4*)Data).y = v["w"];
                /*<-*/ //Here you need to modify it according to what can be load in your variables.
	}
	virtual json OnlySaveData(void* data)override
	{
		json J, v;
		v["x"] = (*(ImVec4*)Data).x;
		v["y"] = (*(ImVec4*)Data).y;
		v["z"] = (*(ImVec4*)Data).z;
		v["w"] = (*(ImVec4*)Data).w;
		J["Value"] = v;
		return J;
                /*<-*/ //Here you need to modify it according to what can be saved in your variables.
	}
	virtual void* OnlyLoadData(json J)override
	{
		json v = J["Value"];
		return new ImVec4(v["x"], v["y"],v["z"],v["w"]); /*<-*/ //Load the store and create a variable of your own target type
	}
	virtual std::string ExportTimeLineCode(std::vector<int>& keys, std::vector<void*>& datas, HAnimation::KeyFlag flag, HWidget* Widget) override
	{
		std::string ExportBuffer, buffer;
		switch (flag)
		{
		case HAnimation::KeyFlag_None:
			return HAnimation::CodeExportTool::DataSwitching(keys, datas, this, Widget);/*<-*/  //If your variables do not require linear interpolation, you only need this code
			break;
		case HAnimation::KeyFlag_LinearInterpolation:
			ExportBuffer += HAnimation::CodeExportTool::Interpolation::DataInterpolation("ImVec4"/*<-*/, keys, datas, this, Widget);

                        //According to the linear interpolation variables among your variables, .x, .y, and .z need to be modified as needed to become the Value of your variables.
			ExportBuffer += "\n	";
			ExportBuffer += GetVariableName(Widget);
			ExportBuffer += ".x = ";
			ExportBuffer += HAnimation::CodeExportTool::Interpolation::GetLinearInterpolationCode(".x", this, Widget);/*<-*/

			ExportBuffer += "\n	";
			ExportBuffer += GetVariableName(Widget);
			ExportBuffer += ".y = ";
			ExportBuffer += HAnimation::CodeExportTool::Interpolation::GetLinearInterpolationCode(".y", this, Widget);/*<-*/

			ExportBuffer += "\n	";
			ExportBuffer += GetVariableName(Widget);
			ExportBuffer += ".z = ";
			ExportBuffer += HAnimation::CodeExportTool::Interpolation::GetLinearInterpolationCode(".z", this, Widget);/*<-*/

			ExportBuffer += "\n	";
			ExportBuffer += GetVariableName(Widget);
			ExportBuffer += ".w = ";
			ExportBuffer += HAnimation::CodeExportTool::Interpolation::GetLinearInterpolationCode(".w", this, Widget);/*<-*/
			break;
		case HAnimation::KeyFlag_SimpleBezierInterpolation:
			ExportBuffer += HAnimation::CodeExportTool::Interpolation::DataInterpolation("ImVec4"/*<-*/, keys, datas, this, Widget);

                        //According to the linear interpolation variables among your variables, .x, .y, and .z need to be modified as needed to become the Value of your variables.
			ExportBuffer += "\n	";
			ExportBuffer += GetVariableName(Widget);
			ExportBuffer += ".x = ";
			ExportBuffer += HAnimation::CodeExportTool::Interpolation::GetSimpleBezierInterpolationCode(".x", this, Widget);/*<-*/

			ExportBuffer += "\n	";
			ExportBuffer += GetVariableName(Widget);
			ExportBuffer += ".y = ";
			ExportBuffer += HAnimation::CodeExportTool::Interpolation::GetSimpleBezierInterpolationCode(".y", this, Widget);/*<-*/

			ExportBuffer += "\n	";
			ExportBuffer += GetVariableName(Widget);
			ExportBuffer += ".z = ";
			ExportBuffer += HAnimation::CodeExportTool::Interpolation::GetSimpleBezierInterpolationCode(".z", this, Widget);/*<-*/

			ExportBuffer += "\n	";
			ExportBuffer += GetVariableName(Widget);
			ExportBuffer += ".w = ";
			ExportBuffer += HAnimation::CodeExportTool::Interpolation::GetSimpleBezierInterpolationCode(".w", this, Widget);/*<-*/
			break;
		default:
			break;
		}
		return ExportBuffer;
	}


	ImVec4* Get()/*<-*/
	{
		return (ImVec4*)/*<-*/Data;
	}
	//Here you can optionally refer to the definition like this .It will be more convenient to use if defined in this way 
        ImVec4& Get()/*<-*/
	{
		return &*(ImVec4*)/*<-*/Data;
	}
private:
};