2012-06-26 20:23:05 +00:00
|
|
|
// * This file is part of the COLOBOT source code
|
|
|
|
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
|
|
|
|
// *
|
|
|
|
// * This program is free software: you can redistribute it and/or modify
|
|
|
|
// * it under the terms of the GNU General Public License as published by
|
|
|
|
// * the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// * (at your option) any later version.
|
|
|
|
// *
|
|
|
|
// * This program is distributed in the hope that it will be useful,
|
|
|
|
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// * GNU General Public License for more details.
|
|
|
|
// *
|
|
|
|
// * You should have received a copy of the GNU General Public License
|
|
|
|
// * along with this program. If not, see http://www.gnu.org/licenses/.// restext.cpp
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
#include "common/restext.h"
|
|
|
|
|
|
|
|
#include "common/global.h"
|
2012-06-26 20:23:05 +00:00
|
|
|
#include "common/event.h"
|
2012-09-16 08:38:08 +00:00
|
|
|
#include "common/logger.h"
|
2012-09-20 22:01:03 +00:00
|
|
|
#include "common/stringutils.h"
|
2012-06-26 20:23:05 +00:00
|
|
|
#include "CBot/resource.h"
|
2012-09-09 15:51:10 +00:00
|
|
|
#include "object/object.h"
|
2012-09-19 16:32:18 +00:00
|
|
|
#include "object/robotmain.h"
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
#include <libintl.h>
|
|
|
|
#include <SDL/SDL_keyboard.h>
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
2012-08-25 10:02:10 +00:00
|
|
|
static char g_gamerName[100];
|
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
void SetGlobalGamerName(char *name)
|
|
|
|
{
|
|
|
|
strcpy(g_gamerName, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct KeyDesc
|
|
|
|
{
|
2012-09-19 16:32:18 +00:00
|
|
|
InputSlot key;
|
2012-06-26 20:23:05 +00:00
|
|
|
char name[20];
|
|
|
|
};
|
|
|
|
|
|
|
|
static KeyDesc keyTable[22] =
|
|
|
|
{
|
2012-09-19 16:32:18 +00:00
|
|
|
{ INPUT_SLOT_LEFT, "left;" },
|
|
|
|
{ INPUT_SLOT_RIGHT, "right;" },
|
|
|
|
{ INPUT_SLOT_UP, "up;" },
|
|
|
|
{ INPUT_SLOT_DOWN, "down;" },
|
|
|
|
{ INPUT_SLOT_GUP, "gup;" },
|
|
|
|
{ INPUT_SLOT_GDOWN, "gdown;" },
|
|
|
|
{ INPUT_SLOT_CAMERA, "camera;" },
|
|
|
|
{ INPUT_SLOT_DESEL, "desel;" },
|
|
|
|
{ INPUT_SLOT_ACTION, "action;" },
|
|
|
|
{ INPUT_SLOT_NEAR, "near;" },
|
|
|
|
{ INPUT_SLOT_AWAY, "away;" },
|
|
|
|
{ INPUT_SLOT_NEXT, "next;" },
|
|
|
|
{ INPUT_SLOT_HUMAN, "human;" },
|
|
|
|
{ INPUT_SLOT_QUIT, "quit;" },
|
|
|
|
{ INPUT_SLOT_HELP, "help;" },
|
|
|
|
{ INPUT_SLOT_PROG, "prog;" },
|
|
|
|
{ INPUT_SLOT_CBOT, "cbot;" },
|
|
|
|
{ INPUT_SLOT_VISIT, "visit;" },
|
|
|
|
{ INPUT_SLOT_SPEED10, "speed10;" },
|
|
|
|
{ INPUT_SLOT_SPEED15, "speed15;" },
|
|
|
|
{ INPUT_SLOT_SPEED20, "speed20;" }
|
2012-06-26 20:23:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Seeks a key.
|
|
|
|
|
2012-09-19 16:32:18 +00:00
|
|
|
bool SearchKey(const char *cmd, InputSlot &key)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for ( i=0 ; i<22 ; i++ )
|
|
|
|
{
|
|
|
|
if ( strstr(cmd, keyTable[i].name) == cmd )
|
|
|
|
{
|
|
|
|
key = keyTable[i].key;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replaces the commands "\key name;" in a text.
|
|
|
|
|
2012-08-25 10:02:10 +00:00
|
|
|
static void PutKeyName(char* dst, const char* src)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-19 16:32:18 +00:00
|
|
|
InputSlot key;
|
2012-06-26 20:23:05 +00:00
|
|
|
char name[50];
|
2012-09-19 16:32:18 +00:00
|
|
|
int s, d, n;
|
|
|
|
unsigned int res;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
s = d = 0;
|
|
|
|
while ( src[s] != 0 )
|
|
|
|
{
|
|
|
|
if ( src[s+0] == '\\' &&
|
|
|
|
src[s+1] == 'k' &&
|
|
|
|
src[s+2] == 'e' &&
|
|
|
|
src[s+3] == 'y' &&
|
|
|
|
src[s+4] == ' ' )
|
|
|
|
{
|
|
|
|
if ( SearchKey(src+s+5, key) )
|
|
|
|
{
|
2012-09-19 16:32:18 +00:00
|
|
|
res = CRobotMain::GetInstancePointer()->GetInputBinding(key).key;
|
|
|
|
if (res != KEY_INVALID)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
if ( GetResource(RES_KEY, res, name) )
|
|
|
|
{
|
|
|
|
n = 0;
|
|
|
|
while ( name[n] != 0 )
|
|
|
|
{
|
|
|
|
dst[d++] = name[n++];
|
|
|
|
}
|
|
|
|
while ( src[s++] != ';' );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dst[d++] = src[s++];
|
|
|
|
}
|
|
|
|
dst[d++] = 0;
|
|
|
|
}
|
|
|
|
|
2012-08-25 10:02:10 +00:00
|
|
|
// Returns the translated text of a resource that needs key substitution
|
|
|
|
|
|
|
|
static const char* GetResourceBase(ResType type, int num)
|
|
|
|
{
|
|
|
|
const char *str = NULL;
|
|
|
|
|
|
|
|
assert(num >= 0);
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
2012-09-20 22:01:03 +00:00
|
|
|
case RES_TEXT:
|
|
|
|
assert(num < strings_text_len);
|
|
|
|
str = strings_text[num];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RES_EVENT:
|
|
|
|
// assert(num < strings_event_len);
|
|
|
|
if (num >= strings_event_len)
|
|
|
|
{
|
|
|
|
GetLogger()->Trace("GetResource event num out of range: %d\n", num); // TODO: fix later
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
str = strings_event[num];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RES_OBJECT:
|
|
|
|
assert(num < strings_object_len);
|
|
|
|
if (num == OBJECT_HUMAN)
|
|
|
|
return g_gamerName;
|
|
|
|
|
|
|
|
str = strings_object[num];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RES_ERR:
|
|
|
|
assert(num < strings_err_len);
|
|
|
|
str = strings_err[num];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RES_CBOT:
|
|
|
|
assert(num < strings_cbot_len);
|
|
|
|
str = strings_cbot[num];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RES_KEY:
|
|
|
|
|
|
|
|
if (num == VIRTUAL_KMOD_CTRL)
|
|
|
|
return "Ctrl";
|
|
|
|
else if (num == VIRTUAL_KMOD_SHIFT)
|
|
|
|
return "Shift";
|
|
|
|
else if (num == VIRTUAL_KMOD_ALT)
|
|
|
|
return "Alt";
|
|
|
|
else if (num == VIRTUAL_KMOD_META)
|
|
|
|
return "Win";
|
|
|
|
else if (num > VIRTUAL_JOY(0))
|
|
|
|
{
|
|
|
|
// TODO: temporary fix
|
|
|
|
static std::string sstr;
|
|
|
|
sstr = gettext("Button %1");
|
|
|
|
StrUtils::Replace(sstr, "%1", StrUtils::ToString<int>(1 + num - VIRTUAL_JOY(0)));
|
|
|
|
return sstr.c_str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
str = SDL_GetKeyName(static_cast<SDLKey>(num));
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false);
|
2012-08-25 10:02:10 +00:00
|
|
|
}
|
2012-09-20 22:01:03 +00:00
|
|
|
|
2012-08-25 10:02:10 +00:00
|
|
|
return gettext(str);
|
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
// Returns the text of a resource.
|
|
|
|
|
|
|
|
bool GetResource(ResType type, int num, char* text)
|
|
|
|
{
|
2012-08-25 10:02:10 +00:00
|
|
|
const char *tmpl = GetResourceBase(type, num);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-08-25 10:02:10 +00:00
|
|
|
if (!tmpl)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
text[0] = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-25 10:02:10 +00:00
|
|
|
PutKeyName(text, tmpl);
|
2012-06-26 20:23:05 +00:00
|
|
|
return true;
|
|
|
|
}
|