bool FStructureEditorUtils::RemoveVariable(UUserDefinedStruct* Struct, FGuid VarGuid)
{
    if(Struct)
    {
        const auto OldNum = GetVarDesc(Struct).Num();
        const bool bAllowToMakeEmpty = false;
        if (bAllowToMakeEmpty || (OldNum > 1))
        {
            const FScopedTransaction Transaction(LOCTEXT("RemoveVariable", "Remove Variable"));
            ModifyStructData(Struct);

            GetVarDesc(Struct).RemoveAll(FFindByGuidHelper<FStructVariableDescription>(VarGuid));
            if (OldNum != GetVarDesc(Struct).Num())
            {
                OnStructureChanged(Struct);
                return true;
            }
        }
        else
        {
            UE_LOG(LogBlueprint, Log, TEXT("Member variable cannot be removed. User Defined Structure cannot be empty"));
        }
    }
    return false;
}
bool FStructureEditorUtils::AddVariable(UUserDefinedStruct* Struct, const FEdGraphPinType& VarType)
{
    if (Struct)
    {
        const FScopedTransaction Transaction( LOCTEXT("AddVariable", "Add Variable") );
        ModifyStructData(Struct);

        FString ErrorMessage;
        if (!CanHaveAMemberVariableOfType(Struct, VarType, &ErrorMessage))
        {
            UE_LOG(LogBlueprint, Warning, TEXT("%s"), *ErrorMessage);
            return false;
        }

        const FGuid Guid = FGuid::NewGuid();
        FString DisplayName;
        const FName VarName = FMemberVariableNameHelper::Generate(Struct, FString(), Guid, &DisplayName);
        check(NULL == GetVarDesc(Struct).FindByPredicate(FStructureEditorUtils::FFindByNameHelper<FStructVariableDescription>(VarName)));
        check(IsUniqueVariableDisplayName(Struct, DisplayName));

        FStructVariableDescription NewVar;
        NewVar.VarName = VarName;
        NewVar.FriendlyName = DisplayName;
        NewVar.SetPinType(VarType);
        NewVar.VarGuid = Guid;
        NewVar.bDontEditoOnInstance = false;
        NewVar.bInvalidMember = false;
        GetVarDesc(Struct).Add(NewVar);

        OnStructureChanged(Struct);
        return true;
    }
    return false;
}
bool FStructureEditorUtils::RenameVariable(UUserDefinedStruct* Struct, FGuid VarGuid, const FString& NewDisplayNameStr)
{
    if (Struct)
    {
        auto VarDesc = GetVarDescByGuid(Struct, VarGuid);
        if (VarDesc
                && !NewDisplayNameStr.IsEmpty()
                && FName(*NewDisplayNameStr).IsValidXName(INVALID_OBJECTNAME_CHARACTERS)
                && IsUniqueVariableDisplayName(Struct, NewDisplayNameStr))
        {
            const FScopedTransaction Transaction(LOCTEXT("RenameVariable", "Rename Variable"));
            ModifyStructData(Struct);

            VarDesc->FriendlyName = NewDisplayNameStr;
            //>>> TEMPORARY it's more important to prevent changes in structs instances, than to have consistent names
            if (GetGuidFromPropertyName(VarDesc->VarName).IsValid())
                //<<< TEMPORARY
            {
                const FName NewName = FMemberVariableNameHelper::Generate(Struct, NewDisplayNameStr, VarGuid);
                check(NULL == GetVarDesc(Struct).FindByPredicate(FFindByNameHelper<FStructVariableDescription>(NewName)))
                VarDesc->VarName = NewName;
            }
            OnStructureChanged(Struct);
            return true;
        }
    }
    return false;
}
bool FStructureEditorUtils::IsUniqueVariableDisplayName(const UUserDefinedStruct* Struct, const FString& DisplayName)
{
    if(Struct)
    {
        for (auto& VarDesc : GetVarDesc(Struct))
        {
            if (VarDesc.FriendlyName == DisplayName)
            {
                return false;
            }
        }
        return true;
    }
    return false;
}
bool FStructureEditorUtils::MoveVariable(UUserDefinedStruct* Struct, FGuid VarGuid, EMoveDirection MoveDirection)
{
    if (Struct)
    {
        const bool bMoveUp = (EMoveDirection::MD_Up == MoveDirection);
        auto& DescArray = GetVarDesc(Struct);
        const int32 InitialIndex = bMoveUp ? 1 : 0;
        const int32 IndexLimit = DescArray.Num() - (bMoveUp ? 0 : 1);
        for (int32 Index = InitialIndex; Index < IndexLimit; ++Index)
        {
            if (DescArray[Index].VarGuid == VarGuid)
            {
                const FScopedTransaction Transaction(LOCTEXT("ReorderVariables", "Varaibles reordered"));
                ModifyStructData(Struct);

                DescArray.Swap(Index, Index + (bMoveUp ? -1 : 1));
                OnStructureChanged(Struct);
                return true;
            }
        }
    }
    return false;
}
FGuid FStructureEditorUtils::GetGuidForProperty(const UProperty* Property)
{
    auto UDStruct = Property ? Cast<const UUserDefinedStruct>(Property->GetOwnerStruct()) : NULL;
    auto VarDesc = UDStruct ? GetVarDesc(UDStruct).FindByPredicate(FFindByNameHelper<FStructVariableDescription>(Property->GetFName())) : NULL;
    return VarDesc ? VarDesc->VarGuid : FGuid();
}
Esempio n. 7
0
short DisplayVar(short handle,short varNo,TDataKind varKind,unsigned short
                                                                  dataSection)
/* Function to display all information for a file or DS variable
                                                    Return 1 if ok 0 if not */
{
    short    handleNo;
    short    procNo;
    short    errNo;                                   /* for error handling */
    TVarDesc var;                        /*to hold the variable description */
    short    ret;                                       /* for return value */
    char     onebyte;                           /* for INT1 and WRD1 values */
    short    twobyte;                           /* for INT2 and WRD2 values */
    long     fourbyte;                                   /* for INT4 values */
    float    realv;                                       /* for RL4 values */
    double   doubv;                                       /* for RL8 values */
    char     s[256];                                   /* for string values */

    ret = 1;                                      /* return value if all ok */
                            /* use function to look at varaible description */
    GetVarDesc(handle,                                   /* CFS file handle */
               varNo,                                    /* variable number */
               varKind,                                 /* FILEVAR or DSVAR */ 
               &var.vSize,                  /* for return of variables size */
               &var.vType,     /* for return of variable type INT1,WRD1 etc.*/
               var.varUnits,                /* for return of variable units */
               var.varDesc); /* for return of users description of variable */
    if (FileError(&handleNo,&procNo,&errNo)!=0)         /* check for errors */
    {
        printf("\nError %d %d %d",handleNo,procNo,errNo);
        ret = 0;                                  /* return value if not ok */
    }
    else               /* report details and get and display variable value */
    {
        if (varKind==DSVAR)
            printf("\nData Section %u data section ",dataSection);
        else
            printf("\nFile ");               /* start with kind of variable */
        printf("variable %d",varNo);                      /* and its number */
        printf("\nUnits %s",var.varUnits);                  /* report units */
        printf("\nDescription %s",var.varDesc); /* report users description */
     /* space needed for return value depends on type so print type, get value
                                         and print value all in case satement.
             NB errors in GetVarVal are not tested until after value report */

        switch (var.vType)
        {
            case INT1 : printf("\nType INT1");
                        GetVarVal(handle,varNo,varKind,dataSection,&onebyte);
                        printf("\nValue %d\n",(int)onebyte);
                        break;
            case WRD1 : printf("\nType WRD1");
                        GetVarVal(handle,varNo,varKind,dataSection,&onebyte);
                        printf("\nValue %u\n",(int)onebyte);
                        break;
            case INT2 : printf("\nType INT2");
                        GetVarVal(handle,varNo,varKind,dataSection,&twobyte);
                        printf("\nValue %d\n",(int)twobyte);
                        break;
            case WRD2 : printf("\nType WRD2");
                        GetVarVal(handle,varNo,varKind,dataSection,&twobyte);
                        printf("\nValue %u\n",(int)twobyte);
                        break;
            case INT4 : printf("\nType INT4");
                        GetVarVal(handle,varNo,varKind,dataSection,&fourbyte);
                        printf("\nValue %ld\n",fourbyte);
                        break;
            case RL4  : printf("\nType RL4");
                        GetVarVal(handle,varNo,varKind,dataSection,&realv);
                        printf("\nValue %f\n",(double)realv);
                        break;
            case RL8  : printf("\nType RL8");
                        GetVarVal(handle,varNo,varKind,dataSection,&doubv);
                        printf("\nValue %f\n",doubv);
                        break;
            case LSTR : printf("\nType LSTR");
                        GetVarVal(handle,varNo,varKind,dataSection,s);
                        printf("\nValue %s\n",s);
                        break;
            default   : printf("\nUndocumented error\n");
                        ret=0;
                        break;
        };
        if (FileError(&handleNo,&procNo,&errNo)!=0)     /* check for errors */
        {
            printf("\nError %d %d %d",handleNo,procNo,errNo);
            ret = 0;                              /* return value if not ok */
        }
    }
   return ret;
};                                                     /* end of DisplayVar */