changeset 11026:2e46cdff4e8b draft

(svn r15366) -Add [NoAI]: Add AddLabels() where you can define labels for the values of the settings in info.nut
author Yexo <Yexo@openttd.org>
date Fri, 06 Feb 2009 00:25:37 +0000
parents 73735b278bed
children d2965f7acc26
files src/ai/ai_gui.cpp src/ai/ai_info.cpp src/ai/ai_info.hpp src/ai/ai_scanner.cpp
diffstat 4 files changed, 65 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/ai_gui.cpp
+++ b/src/ai/ai_gui.cpp
@@ -261,9 +261,12 @@
 				DrawFrameRect(4, y  + 2, 23, y + 10, (current_value != 0) ? 6 : 4, (current_value != 0) ? FR_LOWERED : FR_NONE);
 			} else {
 				DrawArrowButtons(4, y + 2, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + !!this->clicked_increase : 0, current_value > (*it).min_value, current_value < (*it).max_value);
-				static char buf[8];
-				sprintf(buf, "%d", current_value);
-				x = DoDrawStringTruncated(buf, 28, y + 3, TC_ORANGE, this->width - 32);
+				if (it->labels != NULL && it->labels->Find(current_value) != it->labels->End()) {
+					x = DoDrawStringTruncated(it->labels->Find(current_value)->second, 28, y + 3, TC_ORANGE, this->width - 32);
+				} else {
+					SetDParam(0, current_value);
+					x = DrawStringTruncated(28, y + 3, STR_JUST_INT, TC_ORANGE, this->width - 32);
+				}
 			}
 
 			DoDrawStringTruncated((*it).description, max(x + 3, 54), y + 3, TC_LIGHT_BLUE, this->width - (4 + max(x + 3, 54)));
--- a/src/ai/ai_info.cpp
+++ b/src/ai/ai_info.cpp
@@ -210,6 +210,12 @@
 	for (AIConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
 		free((char *)(*it).name);
 		free((char *)(*it).description);
+		if (it->labels != NULL) {
+			for (LabelMapping::iterator it2 = (*it).labels->Begin(); it2 != (*it).labels->End(); it2++) {
+				free(it2->second);
+			}
+			delete it->labels;
+		}
 	}
 	this->config_list.clear();
 }
@@ -323,6 +329,49 @@
 	return 0;
 }
 
+SQInteger AIInfo::AddLabels(HSQUIRRELVM vm)
+{
+	const SQChar *sq_setting_name;
+	sq_getstring(vm, -2, &sq_setting_name);
+	const char *setting_name = FS2OTTD(sq_setting_name);
+
+	AIConfigItem *config = NULL;
+	for (AIConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
+		if (strcmp((*it).name, setting_name) == 0) config = &(*it);
+	}
+
+	if (config == NULL) {
+		char error[1024];
+		snprintf(error, sizeof(error), "Trying to add labels for non-defined setting '%s'", setting_name);
+		this->engine->ThrowError(error);
+		return SQ_ERROR;
+	}
+	if (config->labels != NULL) return SQ_ERROR;
+
+	config->labels = new LabelMapping;
+
+	/* Read the table and find all labels */
+	sq_pushnull(vm);
+	while (SQ_SUCCEEDED(sq_next(vm, -2))) {
+		const SQChar *sq_key;
+		const SQChar *sq_label;
+		sq_getstring(vm, -2, &sq_key);
+		sq_getstring(vm, -1, &sq_label);
+		/* Because squirrel doesn't support identifiers starting with a digit,
+		 * we skip the first character. */
+		const char *key_string = FS2OTTD(sq_key);
+		int key = atoi(key_string + 1);
+		const char *label = FS2OTTD(sq_label);
+
+		if (config->labels->Find(key) == config->labels->End()) config->labels->Insert(key, strdup(label));
+
+		sq_pop(vm, 2);
+	}
+	sq_pop(vm, 1);
+
+	return 0;
+}
+
 const AIConfigItemList *AIInfo::GetConfigList()
 {
 	return &this->config_list;
--- a/src/ai/ai_info.hpp
+++ b/src/ai/ai_info.hpp
@@ -6,6 +6,7 @@
 #define AI_INFO
 
 #include <list>
+#include "../core/smallmap_type.hpp"
 #include "api/ai_object.hpp"
 
 enum AIConfigFlags {
@@ -14,6 +15,8 @@
 	AICONFIG_BOOLEAN = 0x2, //!< This value is a boolean (either 0 (false) or 1 (true) ).
 };
 
+typedef SmallMap<int, char *> LabelMapping;
+
 struct AIConfigItem {
 	const char *name;        //!< The name of the configuration setting.
 	const char *description; //!< The description of the configuration setting.
@@ -26,6 +29,7 @@
 	int random_deviation;    //!< The maximum random deviation from the default value.
 	int step_size;           //!< The step size in the gui.
 	AIConfigFlags flags;     //!< Flags for the configuration setting.
+	LabelMapping *labels;    //!< Text labels for the integer values.
 };
 
 extern AIConfigItem _start_date_config;
@@ -141,6 +145,11 @@
 	SQInteger AddSetting(HSQUIRRELVM vm);
 
 	/**
+	 * Add labels for a setting.
+	 */
+	SQInteger AddLabels(HSQUIRRELVM vm);
+
+	/**
 	 * Get the default value for a setting.
 	 */
 	int GetSettingDefaultValue(const char *name);
--- a/src/ai/ai_scanner.cpp
+++ b/src/ai/ai_scanner.cpp
@@ -138,6 +138,7 @@
 	SQAIInfo.PreRegister(engine);
 	SQAIInfo.AddConstructor<void (AIInfo::*)(), 1>(engine, "x");
 	SQAIInfo.DefSQAdvancedMethod(this->engine, &AIInfo::AddSetting, "AddSetting");
+	SQAIInfo.DefSQAdvancedMethod(this->engine, &AIInfo::AddLabels, "AddLabels");
 	SQAIInfo.DefSQConst(engine, AICONFIG_RANDOM, "AICONFIG_RANDOM");
 	SQAIInfo.DefSQConst(engine, AICONFIG_BOOLEAN, "AICONFIG_BOOLEAN");
 	SQAIInfo.PostRegister(engine);