Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions code/controlconfig/controlsconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ static UI_BUTTON List_buttons[LIST_BUTTONS_MAX]; // buttons for each line of te
static UI_WINDOW Ui_window;
static unsigned int Defaults_cycle_pos = 0; // the controls preset that was last selected
static UI_TIMESTAMP bound_timestamp;
static char bound_string[40];
constexpr size_t bound_string_len = 255;
static char bound_string[bound_string_len + 1];

int Control_config_overlay_id;

Expand Down Expand Up @@ -1917,7 +1918,8 @@ int control_config_draw_list(int select_tease_line) {
int x; // x coordinate of text anchor
int y; // y coordinate of text anchor
int z; // cc_index of the line being drawn
char buf[256]; // c_str buffer
constexpr size_t buf_len = 255;
char buf[buf_len + 1]; // c_str buffer
int font_height = gr_get_font_height();

for (line = Scroll_offset; cc_line_query_visible(line) && (line - Scroll_offset < LIST_BUTTONS_MAX); ++line) {
Expand Down Expand Up @@ -1945,7 +1947,7 @@ int control_config_draw_list(int select_tease_line) {
gr_set_color_fast(c);
if (Cc_lines[line].label) {
strcpy_s(buf, Cc_lines[line].label);
font::force_fit_string(buf, 255, Control_list_ctrl_w[gr_screen.res]);
font::force_fit_string(buf, buf_len, Control_list_ctrl_w[gr_screen.res]);
gr_printf_menu(Control_list_coords[gr_screen.res][CONTROL_X_COORD], y, "%s", buf);
}

Expand All @@ -1960,7 +1962,7 @@ int control_config_draw_list(int select_tease_line) {
x = Control_list_first_x[gr_screen.res];
*buf = 0;
strcpy_s(buf, first.c_str());
font::force_fit_string(buf, 255, Control_list_first_w[gr_screen.res]);
font::force_fit_string(buf, buf_len, Control_list_first_w[gr_screen.res]);
gr_printf_menu(x, y, "%s", buf);

Cc_lines[line].kx = x - Control_list_coords[gr_screen.res][CONTROL_X_COORD];
Expand All @@ -1973,7 +1975,7 @@ int control_config_draw_list(int select_tease_line) {
x = Control_list_second_x[gr_screen.res];
*buf = 0;
strcpy_s(buf, second.c_str());
font::force_fit_string(buf, 255, Control_list_first_w[gr_screen.res]);
font::force_fit_string(buf, buf_len, Control_list_first_w[gr_screen.res]);
gr_printf_menu(x, y, "%s", buf);

Cc_lines[line].jx = x - Control_list_coords[gr_screen.res][CONTROL_X_COORD];
Expand Down Expand Up @@ -2365,7 +2367,8 @@ int control_config_search_key_on_frame(bool API_Access)
void control_config_do_frame(float frametime)
{
const char *str;
char buf[256];
constexpr size_t buf_len = 255;
char buf[buf_len + 1];
int i; // generic index
int w, x, y, conflict;
int k; // polled key. Can be masked with SHIFT and/or ALT
Expand All @@ -2383,7 +2386,7 @@ void control_config_do_frame(float frametime)

if (done) {
// done with binding mode, clean up and prepare for display
font::force_fit_string(bound_string, 39, Conflict_wnd_coords[gr_screen.res][CONTROL_W_COORD]);
font::force_fit_string(bound_string, bound_string_len, Conflict_wnd_coords[gr_screen.res][CONTROL_W_COORD]);
bound_timestamp = ui_timestamp(2500);
control_config_conflict_check();
control_config_list_prepare();
Expand Down Expand Up @@ -2720,7 +2723,7 @@ void control_config_do_frame(float frametime)
strcpy_s(buf, Control_config[i].text.c_str());
}

w = font::force_fit_string(buf, 255, Conflict_wnd_coords[gr_screen.res][CONTROL_W_COORD]);
w = font::force_fit_string(buf, buf_len, Conflict_wnd_coords[gr_screen.res][CONTROL_W_COORD]);
gr_printf_menu(x - w / 2, y, "%s", buf);

} else if (*bound_string) {
Expand Down
14 changes: 5 additions & 9 deletions code/controlconfig/controlsconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -899,20 +899,16 @@ int translate_key_to_index(const char *key, bool find_override=true);
* @brief Given the system default key 'key', return the current control input(s) that is bound to that function.
*
* @param[in] key The default key combo (as cstring) to a certain control
* @param[out] out The key combo currently bound to the control, or "None" if there is nothing bound to it
*
* @return The key combo (as cstring) currently bound to the control, or
* @return nullptr if the given key is not a system default key, or
* @return "None" if there is nothing bound to the control
* @return true if 'key' is a system default key, in which case 'out' is set, or
* @return false otherwise, in which case 'out' is unchanged
*
* @details Both the 'key' and the return value are descriptive strings that can be displayed
* directly to the user. If 'key' isn't a real key, is not normally bound to anything,
* or there is no key currently bound to the function, nullptr is returned.
*
* @note Not thread safe. Has an internal buffer for the return value which is overwritten on each call.
* @details Both the 'key' and 'out' are descriptive strings that can be displayed directly to the user.
*
* @note Uses CC_bind::textify for translations
*/
const char *translate_key(char *key);
bool translate_key(const char *key, SCP_string &out);

/**
* @brief Converts the specified key code to a human readable string, according to the selected locale and keyboard layout
Expand Down
23 changes: 11 additions & 12 deletions code/controlconfig/controlsconfigcommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,14 +614,13 @@ int translate_key_to_index(const char *key, bool find_override)
return -1;
}

const char *translate_key(char *key)
bool translate_key(const char *key, SCP_string &out)
{
int index = -1;
static char text[BTN_MSG_LEN] = {""};

index = translate_key_to_index(key, false);
if (index < 0) {
return nullptr;
return false;
}

const CC_bind &first = Control_config[index].first;
Expand All @@ -630,23 +629,23 @@ const char *translate_key(char *key)
Failed_key_index = index;

if (!first.empty() && !second.empty()) {
strcpy_s(text, first.textify().c_str());
strcat_s(text, " ");
strcat_s(text, XSTR("or", 1672));
strcat_s(text, " ");
strcat_s(text, second.textify().c_str());
out = first.textify();
out += " ";
out += XSTR("or", 1672);
out += " ";
out += second.textify();

} else if (!first.empty()) {
strcpy_s(text, first.textify().c_str());
out = first.textify();

} else if (!second.empty()) {
strcpy_s(text, second.textify().c_str());
out = second.textify();

} else {
strcpy_s(text, XSTR("None", 1673));
out = XSTR("None", 1673);
}

return text;
return true;
}

const char *textify_scancode(int code)
Expand Down
14 changes: 6 additions & 8 deletions code/gamehelp/gameplayhelp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ SCP_vector<gameplay_help_section> Help_text;

void gameplay_help_init_control_line(int id, gameplay_help_section &thisHelp)
{
char buf[256];
SCP_string buf;

auto ci = &Control_config[id];

Expand All @@ -135,20 +135,18 @@ void gameplay_help_init_control_line(int id, gameplay_help_section &thisHelp)
return;
}

buf[0] = 0;

strcpy_s(buf, ci->first.textify().c_str());
buf = ci->first.textify();

if (!ci->second.empty()) {
strcat_s(buf, XSTR(", ", 129));
strcat_s(buf, ci->second.textify().c_str());
buf += XSTR(", ", 129);
buf += ci->second.textify();
}

if (ci->empty()) {
strcpy_s(buf, XSTR("no binding", 130));
buf = XSTR("no binding", 130);
}

thisHelp.key.push_back(buf);
thisHelp.key.push_back(std::move(buf));
thisHelp.text.push_back(XSTR(ci->text.c_str(), CONTROL_CONFIG_XSTR + id));
}

Expand Down
9 changes: 4 additions & 5 deletions code/mission/missiontraining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ SCP_string message_translate_tokens(const char *text)
char temp[40];
const char *ptr, *toke1, *toke2;
int r;
SCP_string buf;
SCP_string buf, translated;

toke1 = strchr(text, '$');
toke2 = strchr(text, '#');
Expand All @@ -758,9 +758,8 @@ SCP_string message_translate_tokens(const char *text)
if ( (toke2 > text) && ((toke2 - text) < (ptr_s)sizeof(temp)) ) {
strncpy(temp, text, toke2 - text); // isolate token into seperate buffer
temp[toke2 - text] = 0; // null terminate string
ptr = translate_key(temp); // try and translate key
if (ptr != nullptr) { // was key translated properly?
if (!stricmp(ptr, NOX("none")) && (Training_bind_warning != Missiontime)) {
if (translate_key(temp, translated)) { // was key translated properly?
if (!stricmp(translated.c_str(), NOX("none")) && (Training_bind_warning != Missiontime)) {
// check if a warning message should be displayed if the key is unbound
if ( (The_mission.game_type & MISSION_TYPE_TRAINING) || (Always_warn_player_about_unbound_keys && (The_mission.game_type & MISSION_TYPE_SINGLE)) ) {
int xstr_index;
Expand All @@ -784,7 +783,7 @@ SCP_string message_translate_tokens(const char *text)
}

buf.pop_back(); // erase the $
buf += ptr; // put translated key in place of token
buf += translated; // put translated key in place of token
text = toke2 + 1;
}
}
Expand Down
Loading