mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-09-01 04:01:56 +02:00
fix bugs with previous commit using FloatType
This commit is contained in:
@@ -26,10 +26,22 @@ AnyType::AnyType(const AnyType & v):
|
|||||||
|
|
||||||
AnyType::operator NumberType()
|
AnyType::operator NumberType()
|
||||||
{
|
{
|
||||||
if(type != TypeNumber)
|
if (type == TypeNumber)
|
||||||
throw InvalidConversionException(type, TypeNumber);
|
|
||||||
else
|
|
||||||
return NumberType(value.num);
|
return NumberType(value.num);
|
||||||
|
else if (type == TypeFloat)
|
||||||
|
return NumberType(value.numf);
|
||||||
|
else
|
||||||
|
throw InvalidConversionException(type, TypeNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
AnyType::operator FloatType()
|
||||||
|
{
|
||||||
|
if (type == TypeNumber)
|
||||||
|
return FloatType(value.num);
|
||||||
|
else if (type == TypeFloat)
|
||||||
|
return FloatType(value.numf);
|
||||||
|
else
|
||||||
|
throw InvalidConversionException(type, TypeFloat);
|
||||||
}
|
}
|
||||||
|
|
||||||
AnyType::operator StringType()
|
AnyType::operator StringType()
|
||||||
@@ -86,16 +98,28 @@ AnyType::~AnyType()
|
|||||||
|
|
||||||
//Number Type
|
//Number Type
|
||||||
|
|
||||||
NumberType::NumberType(float number): AnyType(TypeNumber, ValueValue())
|
NumberType::NumberType(int number): AnyType(TypeNumber, ValueValue())
|
||||||
{
|
{
|
||||||
value.num = number;
|
value.num = number;
|
||||||
}
|
}
|
||||||
|
|
||||||
float NumberType::Value()
|
int NumberType::Value()
|
||||||
{
|
{
|
||||||
return value.num;
|
return value.num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Float Type
|
||||||
|
|
||||||
|
FloatType::FloatType(float number): AnyType(TypeFloat, ValueValue())
|
||||||
|
{
|
||||||
|
value.numf = number;
|
||||||
|
}
|
||||||
|
|
||||||
|
float FloatType::Value()
|
||||||
|
{
|
||||||
|
return value.numf;
|
||||||
|
}
|
||||||
|
|
||||||
//String type
|
//String type
|
||||||
|
|
||||||
StringType::StringType(std::string string): AnyType(TypeString, ValueValue())
|
StringType::StringType(std::string string): AnyType(TypeString, ValueValue())
|
||||||
|
@@ -5,8 +5,8 @@
|
|||||||
#include <typeinfo>
|
#include <typeinfo>
|
||||||
#include "gui/interface/Point.h"
|
#include "gui/interface/Point.h"
|
||||||
|
|
||||||
enum ValueType { TypeNumber, TypePoint, TypeString, TypeNull, TypeFunction };
|
enum ValueType { TypeNumber, TypeFloat, TypePoint, TypeString, TypeNull, TypeFunction };
|
||||||
typedef union { float num; std::string* str; ui::Point* pt; } ValueValue;
|
typedef union { int num; float numf; std::string* str; ui::Point* pt; } ValueValue;
|
||||||
|
|
||||||
class GeneralException
|
class GeneralException
|
||||||
{
|
{
|
||||||
@@ -23,6 +23,7 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
class NumberType;
|
class NumberType;
|
||||||
|
class FloatType;
|
||||||
class StringType;
|
class StringType;
|
||||||
class PointType;
|
class PointType;
|
||||||
|
|
||||||
@@ -35,6 +36,7 @@ public:
|
|||||||
AnyType(ValueType type_, ValueValue value_);
|
AnyType(ValueType type_, ValueValue value_);
|
||||||
AnyType(const AnyType & v);
|
AnyType(const AnyType & v);
|
||||||
operator NumberType();
|
operator NumberType();
|
||||||
|
operator FloatType();
|
||||||
operator StringType();
|
operator StringType();
|
||||||
operator PointType();
|
operator PointType();
|
||||||
ValueType GetType();
|
ValueType GetType();
|
||||||
@@ -44,6 +46,8 @@ public:
|
|||||||
{
|
{
|
||||||
case TypeNumber:
|
case TypeNumber:
|
||||||
return "Number";
|
return "Number";
|
||||||
|
case TypeFloat:
|
||||||
|
return "Float";
|
||||||
case TypePoint:
|
case TypePoint:
|
||||||
return "Point";
|
return "Point";
|
||||||
case TypeString:
|
case TypeString:
|
||||||
@@ -62,6 +66,8 @@ public:
|
|||||||
{
|
{
|
||||||
case TypeNumber:
|
case TypeNumber:
|
||||||
return "Number";
|
return "Number";
|
||||||
|
case TypeFloat:
|
||||||
|
return "Float";
|
||||||
case TypePoint:
|
case TypePoint:
|
||||||
return "Point";
|
return "Point";
|
||||||
case TypeString:
|
case TypeString:
|
||||||
@@ -91,7 +97,14 @@ public:
|
|||||||
class NumberType: public AnyType
|
class NumberType: public AnyType
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NumberType(float number);
|
NumberType(int number);
|
||||||
|
int Value();
|
||||||
|
};
|
||||||
|
|
||||||
|
class FloatType: public AnyType
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
FloatType(float number);
|
||||||
float Value();
|
float Value();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -84,11 +84,13 @@ ValueType TPTScriptInterface::testType(std::string word)
|
|||||||
parseNumber:
|
parseNumber:
|
||||||
for(i = 0; i < word.length(); i++)
|
for(i = 0; i < word.length(); i++)
|
||||||
{
|
{
|
||||||
if(!(rawWord[i] >= '0' && rawWord[i] <= '9') && rawWord[i] != '.' && !(rawWord[i] == '-' && !i))
|
if (!(rawWord[i] >= '0' && rawWord[i] <= '9') && !(rawWord[i] == '-' && !i))
|
||||||
{
|
{
|
||||||
if(rawWord[i] == ',' && rawWord[i+1] >= '0' && rawWord[i+1] <= '9')
|
if (rawWord[i] == '.' && i)
|
||||||
|
return TypeFloat;
|
||||||
|
else if (rawWord[i] == ',' && rawWord[i+1] >= '0' && rawWord[i+1] <= '9')
|
||||||
goto parsePoint;
|
goto parsePoint;
|
||||||
else if((rawWord[i] == '#' || rawWord[i] == 'x') &&
|
else if ((rawWord[i] == '#' || rawWord[i] == 'x') &&
|
||||||
((rawWord[i+1] >= '0' && rawWord[i+1] <= '9')
|
((rawWord[i+1] >= '0' && rawWord[i+1] <= '9')
|
||||||
|| (rawWord[i+1] >= 'a' && rawWord[i+1] <= 'f')
|
|| (rawWord[i+1] >= 'a' && rawWord[i+1] <= 'f')
|
||||||
|| (rawWord[i+1] >= 'A' && rawWord[i+1] <= 'F')))
|
|| (rawWord[i+1] >= 'A' && rawWord[i+1] <= 'F')))
|
||||||
@@ -99,7 +101,7 @@ ValueType TPTScriptInterface::testType(std::string word)
|
|||||||
}
|
}
|
||||||
parseNumberHex:
|
parseNumberHex:
|
||||||
i++;
|
i++;
|
||||||
for(; i < word.length(); i++)
|
for (; i < word.length(); i++)
|
||||||
if(!((rawWord[i] >= '0' && rawWord[i] <= '9') || (rawWord[i] >= 'a' && rawWord[i] <= 'f') || (rawWord[i] >= 'A' && rawWord[i] <= 'F')))
|
if(!((rawWord[i] >= '0' && rawWord[i] <= '9') || (rawWord[i] >= 'a' && rawWord[i] <= 'f') || (rawWord[i] >= 'A' && rawWord[i] <= 'F')))
|
||||||
{
|
{
|
||||||
goto parseString;
|
goto parseString;
|
||||||
@@ -107,7 +109,7 @@ ValueType TPTScriptInterface::testType(std::string word)
|
|||||||
return TypeNumber;
|
return TypeNumber;
|
||||||
parsePoint:
|
parsePoint:
|
||||||
i++;
|
i++;
|
||||||
for(; i < word.length(); i++)
|
for (; i < word.length(); i++)
|
||||||
if(!(rawWord[i] >= '0' && rawWord[i] <= '9'))
|
if(!(rawWord[i] >= '0' && rawWord[i] <= '9'))
|
||||||
{
|
{
|
||||||
goto parseString;
|
goto parseString;
|
||||||
@@ -122,17 +124,17 @@ float TPTScriptInterface::parseNumber(char * stringData)
|
|||||||
char cc;
|
char cc;
|
||||||
int base = 10;
|
int base = 10;
|
||||||
int currentNumber = 0;
|
int currentNumber = 0;
|
||||||
if(stringData[0] == '#')
|
if (stringData[0] == '#')
|
||||||
{
|
{
|
||||||
stringData++;
|
stringData++;
|
||||||
base = 16;
|
base = 16;
|
||||||
}
|
}
|
||||||
else if(stringData[0] == '0' && stringData[1] == 'x')
|
else if (stringData[0] == '0' && stringData[1] == 'x')
|
||||||
{
|
{
|
||||||
stringData+=2;
|
stringData+=2;
|
||||||
base = 16;
|
base = 16;
|
||||||
}
|
}
|
||||||
if(base == 16)
|
if (base == 16)
|
||||||
{
|
{
|
||||||
while(cc = *(stringData++))
|
while(cc = *(stringData++))
|
||||||
{
|
{
|
||||||
@@ -149,7 +151,7 @@ float TPTScriptInterface::parseNumber(char * stringData)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return atof(stringData);
|
return atoi(stringData);
|
||||||
}
|
}
|
||||||
return currentNumber;
|
return currentNumber;
|
||||||
}
|
}
|
||||||
@@ -181,6 +183,8 @@ AnyType TPTScriptInterface::eval(std::deque<std::string> * words)
|
|||||||
break;
|
break;
|
||||||
case TypeNumber:
|
case TypeNumber:
|
||||||
return NumberType(parseNumber(rawWord));
|
return NumberType(parseNumber(rawWord));
|
||||||
|
case TypeFloat:
|
||||||
|
return FloatType(atof(rawWord));
|
||||||
case TypePoint:
|
case TypePoint:
|
||||||
{
|
{
|
||||||
int pointX, pointY;
|
int pointX, pointY;
|
||||||
@@ -257,10 +261,15 @@ AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words)
|
|||||||
throw GeneralException("Invalid property");
|
throw GeneralException("Invalid property");
|
||||||
|
|
||||||
//Selector
|
//Selector
|
||||||
float newValue;
|
int newValue;
|
||||||
if(value.GetType() == TypeNumber)
|
float newValuef;
|
||||||
|
if (value.GetType() == TypeNumber)
|
||||||
{
|
{
|
||||||
newValue = ((NumberType)value).Value();
|
newValue = newValuef = ((NumberType)value).Value();
|
||||||
|
}
|
||||||
|
else if (value.GetType() == TypeFloat)
|
||||||
|
{
|
||||||
|
newValue = newValuef = ((FloatType)value).Value();
|
||||||
}
|
}
|
||||||
else if(value.GetType() == TypeString)
|
else if(value.GetType() == TypeString)
|
||||||
{
|
{
|
||||||
@@ -268,9 +277,11 @@ AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words)
|
|||||||
{
|
{
|
||||||
std::string newString = ((StringType)value).Value();
|
std::string newString = ((StringType)value).Value();
|
||||||
if (newString.at(newString.length()-1) == 'C')
|
if (newString.at(newString.length()-1) == 'C')
|
||||||
newValue = atof(newString.substr(0, newString.length()-1).c_str())+273.15;
|
newValuef = atof(newString.substr(0, newString.length()-1).c_str())+273.15;
|
||||||
else if (newString.at(newString.length()-1) == 'F')
|
else if (newString.at(newString.length()-1) == 'F')
|
||||||
newValue = (atof(newString.substr(0, newString.length()-1).c_str())-32.0f)*5/9+273.15f;
|
newValuef = (atof(newString.substr(0, newString.length()-1).c_str())-32.0f)*5/9+273.15f;
|
||||||
|
else
|
||||||
|
throw GeneralException("Invalid value for assignment");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -310,7 +321,7 @@ AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words)
|
|||||||
*((int*)(partsBlock+(partIndex*sizeof(Particle))+propertyOffset)) = newValue;
|
*((int*)(partsBlock+(partIndex*sizeof(Particle))+propertyOffset)) = newValue;
|
||||||
break;
|
break;
|
||||||
case FormatFloat:
|
case FormatFloat:
|
||||||
*((float*)(partsBlock+(partIndex*sizeof(Particle))+propertyOffset)) = newValue;
|
*((float*)(partsBlock+(partIndex*sizeof(Particle))+propertyOffset)) = newValuef;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
returnValue = 1;
|
returnValue = 1;
|
||||||
@@ -335,7 +346,7 @@ AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words)
|
|||||||
if(sim->parts[j].type)
|
if(sim->parts[j].type)
|
||||||
{
|
{
|
||||||
returnValue++;
|
returnValue++;
|
||||||
*((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValue;
|
*((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValuef;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -372,7 +383,7 @@ AnyType TPTScriptInterface::tptS_set(std::deque<std::string> * words)
|
|||||||
if(sim->parts[j].type == type)
|
if(sim->parts[j].type == type)
|
||||||
{
|
{
|
||||||
returnValue++;
|
returnValue++;
|
||||||
*((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValue;
|
*((float*)(partsBlock+(j*sizeof(Particle))+propertyOffset)) = newValuef;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
Reference in New Issue
Block a user