mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-06 22:28:56 +00:00
(svn r15366) -Add [NoAI]: Add AddLabels() where you can define labels for the values of the settings in info.nut
This commit is contained in:
parent
8f270af124
commit
cb3784d8b1
@ -261,9 +261,12 @@ struct AISettingsWindow : public Window {
|
||||
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)));
|
||||
|
@ -210,6 +210,12 @@ AIInfo::~AIInfo()
|
||||
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 @@ SQInteger AIInfo::AddSetting(HSQUIRRELVM vm)
|
||||
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;
|
||||
|
@ -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 @@ enum AIConfigFlags {
|
||||
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 @@ struct AIConfigItem {
|
||||
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;
|
||||
@ -140,6 +144,11 @@ public:
|
||||
*/
|
||||
SQInteger AddSetting(HSQUIRRELVM vm);
|
||||
|
||||
/**
|
||||
* Add labels for a setting.
|
||||
*/
|
||||
SQInteger AddLabels(HSQUIRRELVM vm);
|
||||
|
||||
/**
|
||||
* Get the default value for a setting.
|
||||
*/
|
||||
|
@ -138,6 +138,7 @@ AIScanner::AIScanner() :
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user