Add "mul_round" op to template parsing to support multiplying-then-rounding.

This allows using arbitrary 'mul' values with Integer types, instead of
having to switch them to Decimal types.
The ParamNode is not aware of validation (thus types) so a better
solution is incredibly non-trivial.

Differential Revision: https://code.wildfiregames.com/D268
This was SVN commit r22003.
This commit is contained in:
wraitii 2019-01-02 14:46:17 +00:00
parent d91702a16b
commit 19f600cfa2
2 changed files with 8 additions and 14 deletions

View file

@ -1104,28 +1104,16 @@ void CComponentManager::SendGlobalMessage(entity_id_t ent, const CMessage& msg)
std::string CComponentManager::GenerateSchema() const std::string CComponentManager::GenerateSchema() const
{ {
std::string numericOperation =
"<optional>"
"<attribute name='op'>"
"<choice>"
"<value>add</value>"
"<value>mul</value>"
"</choice>"
"</attribute>"
"</optional>";
std::string schema = std::string schema =
"<grammar xmlns='http://relaxng.org/ns/structure/1.0' xmlns:a='http://ns.wildfiregames.com/entity' datatypeLibrary='http://www.w3.org/2001/XMLSchema-datatypes'>" "<grammar xmlns='http://relaxng.org/ns/structure/1.0' xmlns:a='http://ns.wildfiregames.com/entity' datatypeLibrary='http://www.w3.org/2001/XMLSchema-datatypes'>"
"<define name='decimal'>" "<define name='decimal'>"
"<data type='decimal'/>" "<data type='decimal'/>"
+ numericOperation +
"</define>" "</define>"
"<define name='nonNegativeDecimal'>" "<define name='nonNegativeDecimal'>"
"<data type='decimal'><param name='minInclusive'>0</param></data>" "<data type='decimal'><param name='minInclusive'>0</param></data>"
+ numericOperation +
"</define>" "</define>"
"<define name='positiveDecimal'>" "<define name='positiveDecimal'>"
"<data type='decimal'><param name='minExclusive'>0</param></data>" "<data type='decimal'><param name='minExclusive'>0</param></data>"
+ numericOperation +
"</define>" "</define>"
"<define name='anything'>" "<define name='anything'>"
"<zeroOrMore>" "<zeroOrMore>"

View file

@ -83,7 +83,8 @@ void CParamNode::ApplyLayer(const XMBFile& xmb, const XMBElement& element, const
enum op { enum op {
INVALID, INVALID,
ADD, ADD,
MUL MUL,
MUL_ROUND
} op = INVALID; } op = INVALID;
bool replacing = false; bool replacing = false;
bool filtering = false; bool filtering = false;
@ -117,6 +118,8 @@ void CParamNode::ApplyLayer(const XMBFile& xmb, const XMBElement& element, const
op = ADD; op = ADD;
else if (std::wstring(attr.Value.begin(), attr.Value.end()) == L"mul") else if (std::wstring(attr.Value.begin(), attr.Value.end()) == L"mul")
op = MUL; op = MUL;
else if (std::wstring(attr.Value.begin(), attr.Value.end()) == L"mul_round")
op = MUL_ROUND;
else else
LOGWARNING("Invalid op '%ls'", attr.Value); LOGWARNING("Invalid op '%ls'", attr.Value);
} }
@ -178,7 +181,10 @@ void CParamNode::ApplyLayer(const XMBFile& xmb, const XMBElement& element, const
node.m_Value = (oldval + mod).ToString().FromUTF8(); node.m_Value = (oldval + mod).ToString().FromUTF8();
break; break;
case MUL: case MUL:
node.m_Value = (oldval.Multiply(mod)).ToString().FromUTF8(); node.m_Value = oldval.Multiply(mod).ToString().FromUTF8();
break;
case MUL_ROUND:
node.m_Value = fixed::FromInt(oldval.Multiply(mod).ToInt_RoundToNearest()).ToString().FromUTF8();
break; break;
} }
hasSetValue = true; hasSetValue = true;