Browse Source

Cleaned up the Font:getWrap code a bit.

Alex Szpakowski 10 years ago
parent
commit
153bd6f4cb
2 changed files with 22 additions and 20 deletions
  1. 21 19
      src/modules/graphics/opengl/Font.cpp
  2. 1 1
      src/modules/system/wrap_System.cpp

+ 21 - 19
src/modules/graphics/opengl/Font.cpp

@@ -671,38 +671,37 @@ int Font::getWidth(char character)
 
 
 void Font::getWrap(const std::string &text, float wrap, std::vector<std::string> &lines, std::vector<int> *linewidths, std::vector<bool> *wrappedlines)
 void Font::getWrap(const std::string &text, float wrap, std::vector<std::string> &lines, std::vector<int> *linewidths, std::vector<bool> *wrappedlines)
 {
 {
-	using namespace std;
 	const float width_space = (float) getWidth(' ');
 	const float width_space = (float) getWidth(' ');
 
 
-	//split text at newlines
-	istringstream iss(text);
-	string line;
-	ostringstream string_builder;
-	while (getline(iss, line, '\n'))
+	std::istringstream iss(text);
+	std::string line;
+	std::ostringstream string_builder;
+
+	// Split text at newlines.
+	while (std::getline(iss, line, '\n'))
 	{
 	{
+		std::vector<std::string> words;
+		std::istringstream word_iss(line);
+
 		// split line into words
 		// split line into words
-		vector<string> words;
-		istringstream word_iss(line);
-		copy(istream_iterator<string>(word_iss), istream_iterator<string>(),
-			 back_inserter< vector<string> >(words));
+		std::copy(std::istream_iterator<std::string>(word_iss), std::istream_iterator<std::string>(), std::back_inserter(words));
 
 
-		// put words back together until a wrap occurs
 		float width = 0.0f;
 		float width = 0.0f;
 		float oldwidth = 0.0f;
 		float oldwidth = 0.0f;
 		string_builder.str("");
 		string_builder.str("");
-		vector<string>::const_iterator word_iter, wend = words.end();
-		for (word_iter = words.begin(); word_iter != wend; ++word_iter)
+
+		// Put words back together until a wrap occurs.
+		for (const std::string &word : words)
 		{
 		{
-			const string &word = *word_iter;
 			width += getWidth(word);
 			width += getWidth(word);
 
 
-			// on wordwrap, push line to line buffer and clear string builder
+			// On wordwrap, push line to line buffer and clear string builder.
 			if (width > wrap && oldwidth > 0)
 			if (width > wrap && oldwidth > 0)
 			{
 			{
 				int realw = (int) width;
 				int realw = (int) width;
 
 
-				// remove trailing space
-				string tmp = string_builder.str();
+				// Remove trailing space.
+				std::string tmp = string_builder.str();
 				lines.push_back(tmp.substr(0,tmp.size()-1));
 				lines.push_back(tmp.substr(0,tmp.size()-1));
 				string_builder.str("");
 				string_builder.str("");
 				width = static_cast<float>(getWidth(word));
 				width = static_cast<float>(getWidth(word));
@@ -715,15 +714,18 @@ void Font::getWrap(const std::string &text, float wrap, std::vector<std::string>
 				if (wrappedlines)
 				if (wrappedlines)
 					wrappedlines->push_back(true);
 					wrappedlines->push_back(true);
 			}
 			}
+
 			string_builder << word << " ";
 			string_builder << word << " ";
+
 			width += width_space;
 			width += width_space;
 			oldwidth = width;
 			oldwidth = width;
 		}
 		}
-		// push last line
+
+		// Push last line.
 		if (linewidths)
 		if (linewidths)
 			linewidths->push_back(width);
 			linewidths->push_back(width);
 
 
-		string tmp = string_builder.str();
+		std::string tmp = string_builder.str();
 		lines.push_back(tmp.substr(0,tmp.size()-1));
 		lines.push_back(tmp.substr(0,tmp.size()-1));
 
 
 		// Indicate that this line was not automatically wrapped.
 		// Indicate that this line was not automatically wrapped.

+ 1 - 1
src/modules/system/wrap_System.cpp

@@ -88,7 +88,7 @@ int w_openURL(lua_State *L)
 
 
 int w_vibrate(lua_State *L)
 int w_vibrate(lua_State *L)
 {
 {
-	double seconds = luaL_checknumber(L, 1);
+	double seconds = luaL_optnumber(L, 1, 0.5);
 	instance()->vibrate(seconds);
 	instance()->vibrate(seconds);
 	return 0;
 	return 0;
 }
 }