Files
blueprintLibrary/Source/GraffModule/Private/cppFuncLibrary.cpp
T

326 lines
8.2 KiB
C++

// Fill outLvl your copyright notice in the Description page of Project Settings.
#include "cppFuncLibrary.h"
#include "Kismet/KismetStringLibrary.h"
#include "Kismet/KismetmathLibrary.h"
TArray<FString> UcppFuncLibrary::cppUniversalParser(FString string, TArray<FString> keys, FString delimiter, bool caseSensitive)
{
TArray<FString> arr;
TArray<FString> out;
out.SetNum(keys.Num());
string.ParseIntoArray(arr, *delimiter, false);
for (const auto& str : arr) {
int n = 0;
for (const auto& str1 : keys) {
if (str1.IsEmpty()) continue;
FString left_;
FString right_;
if (str.Split(str1, &left_, &right_, caseSensitive ? ESearchCase::CaseSensitive : ESearchCase::IgnoreCase)) {
if (out[n].IsEmpty()) out[n] = right_;
}
n++;
}
}
return out;
}
FString UcppFuncLibrary::cppIntToStrPad(int32 int_, int32 minDigits)
{
FString temp = FString::FromInt(int_);
for (int32 i = temp.Len(); i < minDigits; i++) {
temp = L"0" + temp;
}
return temp;
}
TSoftObjectPtr<UTexture2D> UcppFuncLibrary::castSoftTex2D(TSoftObjectPtr<UObject> ptr)
{
return TSoftObjectPtr<UTexture2D>(ptr.ToSoftObjectPath());//somewhy cant construct from another <T> and cant cast
}
double UcppFuncLibrary::NegateF(double in)
{
return in * -1;
}
int32 UcppFuncLibrary::NegateI(int32 in)
{
return in * -1;
}
double UcppFuncLibrary::OneMinus(double in)
{
return 1 - in;
}
double UcppFuncLibrary::AddRefF(UPARAM(ref) double& A, double B)
{
A += B;
return A;
}
int32 UcppFuncLibrary::AddRefI(UPARAM(ref) int32& A, int32 B)
{
A += B;
return A;
}
double UcppFuncLibrary::SubRefF(UPARAM(ref) double& A, double B)
{
A -= B;
return A;
}
int32 UcppFuncLibrary::SubRefI(UPARAM(ref) int32& A, int32 B)
{
A -= B;
return A;
}
double UcppFuncLibrary::saturate(double in)
{
return in < 0 ? 0 : (in > 1 ? 1 : in);
}
int32 UcppFuncLibrary::clamp0(int32 in)
{
return (in < 0) ? 0 : in;
}
void UcppFuncLibrary::cppssw(int32 int_, FString trueInts, bool& true_, bool& false_)
{
FJsonSerializableArray arr;
trueInts.ParseIntoArray(arr, L",");
for (auto& obj : arr) {
FString lft;
FString* lftp = &lft;
FString rht;
FString* rhtp = &rht;
if (obj.Split(L"-", &lft, &rht)) {
if ((int_ >= UKismetStringLibrary::Conv_StringToInt(lft)) && (int_ <= UKismetStringLibrary::Conv_StringToInt(rht))) {
true_ = true;
false_ = false;
return;
}
}
else {
if (int_ == UKismetStringLibrary::Conv_StringToInt(obj)) {
true_ = true;
false_ = false;
return;
}
}
}
true_ = false;
false_ = true;
return;
}
FString UcppFuncLibrary::stringAdd(UPARAM(ref)FString& str, FString add)
{
return str.Append(add);
}
void UcppFuncLibrary::parseOptions(FString SourceString, FString key, FString Delimiter, FString& value, bool& found)
{
FJsonSerializableArray arr;
SourceString.ParseIntoArray(arr, *Delimiter);
for (auto& ar : arr) {
FString lft;
FString rht;
if (ar.Split(L"=", &lft, &rht)) {
if (lft == key) {
value = rht;
found = true;
return;
}
}
}
value = FString();
found = false;
return;
}
int32 UcppFuncLibrary::customStringToInt(FString string)
{
return (string.IsEmpty() || !string.IsNumeric()) ? -1 : UKismetStringLibrary::Conv_StringToInt(string);
}
void UcppFuncLibrary::multiContains(FString source, UPARAM(ref)TArray<FString>& substrings, bool bAnd, bool bUseCase, bool& bTrue, bool& bFalse)
{
TArray<bool> temp;
for (auto& st : substrings) {
bool temp1 = source.Contains(st, bUseCase ? ESearchCase::CaseSensitive : ESearchCase::IgnoreCase);
temp.Add(temp1);
if (temp1) {
if (!bAnd) {
bTrue = true;
bFalse = false;
return;
}
}
}
bTrue = !temp.Contains(false);
bFalse = !bTrue;
return;
}
void UcppFuncLibrary::containStringSwitch(FString string, UPARAM(ref)TArray<FString>& keys, bool bUseCase, int32& bInt, TArray<bool>& bools)
{
bools.Empty();
bInt = -1;
bool lock = false;
for (auto& key : keys) {
bool temp2 = string.Contains(key, bUseCase ? ESearchCase::CaseSensitive : ESearchCase::IgnoreCase);
bools.Add(temp2);
if (temp2 && !lock) {
lock = true;
bInt = bools.Num() - 1;
}
}
}
FString UcppFuncLibrary::multiRemove(FString source, UPARAM(ref)TArray<FString>& substrings, bool bUseCase)
{
for (auto& ss : substrings) source.ReplaceInline(*ss, L"", bUseCase ? ESearchCase::CaseSensitive : ESearchCase::IgnoreCase);
return source;
}
FString UcppFuncLibrary::quoteString(FString string)
{
return FString("""").Append(string).Append("""");
}
void UcppFuncLibrary::getRange(double value, double minMult, double maxMult, double& bMin, double& bValue, double& bMax)
{
bMin = value * minMult;
bMax = value * maxMult;
bValue = value;
}
void UcppFuncLibrary::updateFilterList(TArray<FcppflatStruct> flats, int floorMin, int floorMax, float sqMin, float sqMax, int priceMin, int priceMax, TArray<bool> houses, TArray<bool> sections, TArray<bool> rooms, int sortType, TArray<FcppflatStruct>& filtered)
{
TArray<int> tempfloor, tempprice;
TArray<float> tempsq;
for (auto& flat : flats) {
if (inRange(flat.Floor, floorMin, floorMax)
&& inRange(flat.Price, priceMin, priceMax)
&& inRange(flat.Square, sqMin, sqMax)
&& boolGet(houses, flat.House)
&& boolGet(sections, flat.Section)
&& boolGet(rooms, flat.Rooms)) {
filtered.Add(flat);
tempfloor.Add(flat.Floor);
tempprice.Add(flat.Price);
tempsq.Add(flat.Square);
}
}
if (tempfloor.Num() > 1) {
switch (sortType) {
case 0:
case 1:
for (int i = 0; i <= (tempprice.Num() - 1) / 2; i++) {
int minid, maxid;
minmaxInRange(tempprice, i, tempprice.Num() - 1 - i, minid, maxid);
tempprice.Swap(sortType == 0 ? minid : maxid, i);
filtered.Swap(sortType == 0 ? minid : maxid, i);
minmaxInRange(tempprice, i, tempprice.Num() - 1 - i, minid, maxid);
tempprice.Swap(sortType == 1 ? minid : maxid, tempprice.Num() - 1 - i);
filtered.Swap(sortType == 1 ? minid : maxid, tempprice.Num() - 1 - i);
}
break;
case 2:
case 3:
for (int i = 0; i <= (tempsq.Num() - 1) / 2; i++) {
int minid, maxid;
minmaxInRange(tempsq, i, tempsq.Num() - 1 - i, minid, maxid);
tempsq.Swap(sortType == 2 ? minid : maxid, i);
filtered.Swap(sortType == 2 ? minid : maxid, i);
minmaxInRange(tempsq, i, tempsq.Num() - 1 - i, minid, maxid);
tempsq.Swap(sortType == 3 ? minid : maxid, tempsq.Num() - 1 - i);
filtered.Swap(sortType == 3 ? minid : maxid, tempsq.Num() - 1 - i);
}
break;
case 4:
case 5:
for (int i = 0; i <= (tempfloor.Num() - 1) / 2; i++) {
int minid, maxid;
minmaxInRange(tempfloor, i, tempfloor.Num() - 1 - i, minid, maxid);
tempfloor.Swap(sortType == 4 ? minid : maxid, i);
filtered.Swap(sortType == 4 ? minid : maxid, i);
minmaxInRange(tempfloor, i, tempfloor.Num() - 1 - i, minid, maxid);
tempfloor.Swap(sortType == 5 ? minid : maxid, tempfloor.Num() - 1 - i);
filtered.Swap(sortType == 5 ? minid : maxid, tempfloor.Num() - 1 - i);
}
break;
default:
break;
}
}
}
bool UcppFuncLibrary::inRange(float in, float min, float max)
{
return (in >= min) && (in <= max);
}
bool UcppFuncLibrary::inRange(int in, int min, int max)
{
return (in >= min) && (in <= max);
}
bool UcppFuncLibrary::boolGet(TArray<bool> arr, int index)
{
if (arr.Contains(true)) {
if (arr.IsValidIndex(index))return arr[index];
return false;
}
return true;
}
void UcppFuncLibrary::minmaxInRange(TArray<int> arr, int min, int max, int& minid, int& maxid)
{
int temp;
max = (max > (arr.Num() - 1)) ? arr.Num() - 1 : (max < 0 ? 0 : max);
min = (min > (arr.Num() - 1)) ? arr.Num() - 1 : (min < 0 ? 0 : min);
arr.SetNum(max + 1); //delete overmax ids
for (int i = 0; i < min; i++)arr.RemoveAt(0);// delete belowmin ids
UKismetMathLibrary::MinOfIntArray(arr, minid, temp);
minid += min; //return original order
UKismetMathLibrary::MaxOfIntArray(arr, maxid, temp);
maxid += min;
}
void UcppFuncLibrary::minmaxInRange(TArray<float> arr, int min, int max, int& minid, int& maxid)
{
float temp;
max = (max > (arr.Num() - 1)) ? arr.Num() - 1 : (max < 0 ? 0 : max);
min = (min > (arr.Num() - 1)) ? arr.Num() - 1 : (min < 0 ? 0 : min);
arr.SetNum(max + 1); //delete overmax ids
for (int i = 0; i < min; i++)arr.RemoveAt(0);// delete belowmin ids
UKismetMathLibrary::MinOfFloatArray(arr, minid, temp);
minid += min; //return original order
UKismetMathLibrary::MaxOfFloatArray(arr, maxid, temp);
maxid += min;
}