Torque3D Documentation / _generateds / typeValidators.cpp

typeValidators.cpp

Engine/source/console/typeValidators.cpp

More...

Namespaces:

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/platform.h"
 25#include "console/console.h"
 26#include "console/consoleObject.h"
 27#include "console/typeValidators.h"
 28#include "console/simBase.h"
 29#include "math/mPoint3.h"
 30#include <stdarg.h>
 31
 32void TypeValidator::consoleError(SimObject *object, const char *format, ...)
 33{
 34   char buffer[1024];
 35   va_list argptr;
 36   va_start(argptr, format);
 37   dVsprintf(buffer, sizeof(buffer), format, argptr);
 38   va_end(argptr);
 39
 40   AbstractClassRep *rep = object->getClassRep();
 41   AbstractClassRep::Field &fld = rep->mFieldList[fieldIndex];
 42   const char *objectName = object->getName();
 43   if(!objectName)
 44      objectName = "unnamed";
 45
 46
 47   Con::warnf("%s - %s(%d) - invalid value for %s: %s",
 48      rep->getClassName(), objectName, object->getId(), fld.pFieldname, buffer);
 49}
 50
 51void FRangeValidator::validateType(SimObject *object, void *typePtr)
 52{
 53   F32 *v = (F32 *) typePtr;
 54   if(*v < minV || *v > maxV)
 55   {
 56      consoleError(object, "Must be between %g and %g", minV, maxV);
 57      if(*v < minV)
 58         *v = minV;
 59      else if(*v > maxV)
 60         *v = maxV;
 61   }
 62}
 63
 64void IRangeValidator::validateType(SimObject *object, void *typePtr)
 65{
 66   S32 *v = (S32 *) typePtr;
 67   if(*v < minV || *v > maxV)
 68   {
 69      consoleError(object, "Must be between %d and %d", minV, maxV);
 70      if(*v < minV)
 71         *v = minV;
 72      else if(*v > maxV)
 73         *v = maxV;
 74   }
 75}
 76
 77void IRangeValidatorScaled::validateType(SimObject *object, void *typePtr)
 78{
 79   S32 *v = (S32 *) typePtr;
 80   *v /= factor;
 81   if(*v < minV || *v > maxV)
 82   {
 83      consoleError(object, "Scaled value must be between %d and %d", minV, maxV);
 84      if(*v < minV)
 85         *v = minV;
 86      else if(*v > maxV)
 87         *v = maxV;
 88   }
 89}
 90
 91void Point3NormalizeValidator::validateType(SimObject *object, void *typePtr)
 92{
 93   Point3F *v = (Point3F *) typePtr;
 94   const F32 len = v->len();
 95   if(!mIsEqual(len, 1.0f))
 96   {
 97      consoleError(object, "Vector length must be %g", length);
 98      *v *= length / len;
 99   }
100}
101
102namespace CommonValidators
103{
104   FRangeValidator PositiveFloat(0.0f, F32_MAX);
105   FRangeValidator PositiveNonZeroFloat((F32)POINT_EPSILON, F32_MAX);
106   FRangeValidator NormalizedFloat(0.0f, 1.0f);
107   Point3NormalizeValidator NormalizedPoint3(1.0f);
108};
109
110