ConsoleRuleFn.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <console/Console.h>
00022 #include <console/ConsoleRuleFn.h>
00023 #include <common/Defines.h>
00024 #include <stdio.h>
00025 
00026 ConsoleRuleFnI::~ConsoleRuleFnI()
00027 {
00028 }
00029 
00030 static std::vector<ConsoleRuleParam> generateFnParams(
00031         ConsoleRuleType type, bool write)
00032 {
00033         std::vector<ConsoleRuleParam> params;
00034         if (!write)
00035         {
00036                 switch(type)
00037                 {
00038                 case ConsoleRuleTypeBoolean:
00039                         params.push_back(ConsoleRuleParam("<boolean>", ConsoleRuleTypeBoolean));
00040                         break;
00041                 case ConsoleRuleTypeNumber:
00042                         params.push_back(ConsoleRuleParam("<number>", ConsoleRuleTypeNumber));
00043                         break;
00044                 default:
00045                         params.push_back(ConsoleRuleParam("<string>", ConsoleRuleTypeString));
00046                         break;
00047                 }
00048         }
00049         return params;
00050 }
00051 
00052 ConsoleRuleFn::ConsoleRuleFn(const char *name, 
00053         ConsoleRuleFnI *user, 
00054         ConsoleRuleType type, 
00055         bool write) :
00056         ConsoleRule(name, generateFnParams(type, write)), 
00057         user_(user), 
00058         type_(type)
00059 {
00060 
00061 }
00062 
00063 ConsoleRuleFn::~ConsoleRuleFn()
00064 {
00065 
00066 }
00067 
00068 void ConsoleRuleFn::runRule(
00069         Console *console,
00070         const char *wholeLine,
00071         std::vector<ConsoleRuleValue> &values)
00072 {
00073         if (values.size() == 2)
00074         {
00075                 ConsoleRuleValue &value = values[1];
00076                 setValue(value);
00077         }
00078         console->addLine(false, getValue());
00079 }
00080 
00081 void ConsoleRuleFn::setValue(ConsoleRuleValue &split)
00082 {
00083         switch (type_)
00084         {
00085         case ConsoleRuleTypeBoolean:
00086                 user_->setBoolParam(name_.c_str(), split.valueBool);
00087                 break;
00088         case ConsoleRuleTypeNumber:
00089                 user_->setNumberParam(name_.c_str(), split.valueNumber);
00090                 break;
00091         case ConsoleRuleTypeString:
00092                 user_->setStringParam(name_.c_str(), split.valueString.c_str());
00093         }
00094 }
00095 
00096 const char *ConsoleRuleFn::getValue()
00097 {
00098         static std::string value;
00099         switch (type_)
00100         {
00101         case ConsoleRuleTypeBoolean:
00102                 value = (user_->getBoolParam(name_.c_str())?"on":"off");
00103                 break;
00104         case ConsoleRuleTypeNumber:
00105                 static char buffer[10];
00106                 snprintf(buffer, 10, "%.2f", user_->getNumberParam(name_.c_str()));
00107                 value = buffer;
00108                 break;
00109         case ConsoleRuleTypeString:
00110                 value = user_->getStringParam(name_.c_str());
00111                 break;
00112         default:
00113                 value = "None";
00114         }
00115 
00116         static std::string result;
00117         result = name_ + "=" + value;
00118 
00119         return result.c_str();
00120 }
00121 

Generated on Mon Feb 16 15:14:37 2009 for Scorched3D by  doxygen 1.5.3