If you have implemented some features that need usage of buttons like custom camera, various formation and tactics codes, shield bashing, iwill be better if platyer can customize the buttons configuration. That's the code about.
I made it simplified so it will be ready to use with minimum extra coding. And there's no need for aditional trigger at mission templates either.
All we need is just copy the code to the relevant files, and adding some constants and strings according to our mod need.
It can support up to 30 keys. If there're more than 20 keys, then the buttons arranged by 3 columns, otherwise there will be 2 columns.
Usage :
Just call at any menu, camp menu will be fit :
Modify constants and strings marked by :
#-- Parts to modify as your mod need --------------
I made it simplified so it will be ready to use with minimum extra coding. And there's no need for aditional trigger at mission templates either.
All we need is just copy the code to the relevant files, and adding some constants and strings according to our mod need.
It can support up to 30 keys. If there're more than 20 keys, then the buttons arranged by 3 columns, otherwise there will be 2 columns.
Code:
#-- Dunde's Key Config BEGIN
# Slots Initilizations (Key Defs & Default Values)
def set_key_config():
key_config = []
for i in xrange(len(keys_list)):
key_config.append((troop_set_slot, key_config_data, slot_default_keys_begin+i, keys_list[i][1]))
for i in xrange(len(all_keys_list)):
key_config.append((troop_set_slot, key_config_data, slot_key_defs_begin+i, all_keys_list[i][0]))
return key_config[:]
# Global Variables -> Slots
def read_key_config():
global_key = []
for i in xrange(len(keys_list)):
global_key.append((troop_set_slot, key_config_data, slot_keys_begin+i, keys_list[i][0]))
return global_key[:]
# Slots -> Global Variables
def write_key_config():
global_key = []
for i in xrange(len(keys_list)):
global_key.append((troop_get_slot, keys_list[i][0], key_config_data, slot_keys_begin+i))
return global_key[:]
#-- Dunde's Key Config END
Code:
#-- Dunde's Key Config BEGIN
("update_key_config_buttons",
[(try_for_range, ":no_key", slot_keys_begin, slot_keys_begin+number_of_keys),
#(store_add, ":off", ":no_key", number_of_keys),
(store_add, ":off_overlay", ":no_key", number_of_keys),
(troop_get_slot, ":ovr", key_config_data, ":off_overlay"),
(troop_get_slot, ":key", key_config_data, ":no_key"),
(assign, ":found", 0), (assign, ":upper_limit", number_of_all_keys),
(try_for_range, ":no", 0, ":upper_limit"),
(store_add, ":off_all_keys", ":no", slot_key_defs_begin),
(troop_slot_eq, key_config_data, ":off_all_keys", ":key"),
(store_add, ":off_strings", ":no", key_label_begin),
(overlay_set_text, ":ovr", ":off_strings", 0),
(overlay_set_color, ":ovr", 0x0),
(assign, ":found", ":no"),
(assign, ":upper_limit", ":no"),
(try_end),
(try_begin),
(neq, ":found", 0),
(else_try),
(eq, ":key", 0xff),
(overlay_set_text, ":ovr", "@Disabled"),
(overlay_set_color, ":ovr", 0x800000),
(else_try),
(overlay_set_text, ":ovr", "@Undefined!"),
(overlay_set_color, ":ovr", 0xFF0000),
(try_end),
(try_end), ]),
("init_key_config", set_key_config()), # Slots Initilizations (Key Defs & Default Values)
("set_config_slot_key_config", read_key_config()), # Global Variables -> Slots
("set_global_var_key_config", write_key_config()), # Slots -> Global Variables
("reset_to_default_keys", # Default Slots -> Working Slots
[(try_for_range, ":no", 0, number_of_keys),
(store_add, ":off", ":no", slot_default_keys_begin),
(troop_get_slot, ":key", key_config_data, ":off"),
(val_add, ":off", number_of_keys),
(troop_set_slot, key_config_data, ":off", ":key"),
(try_end), ]),
("init_all_keys",
[(call_script, "script_init_key_config"),
(call_script, "script_reset_to_default_keys"),
(call_script, "script_set_global_var_key_config"), ]),
#-- Dunde's Key Config END
Code:
(call_script, "script_init_all_keys"),
Code:
# Key configuration
("key_config", 0, mesh_load_window,
[(ti_on_presentation_load,
[(presentation_set_duration, 999999),
(set_fixed_point_multiplier, 1000),
(assign, "$key_config_button_down", 0),
(init_position, pos1),
# Tittle
(position_set_y, pos1, 650), (position_set_x, pos1, 500), # Tittle Position
(position_set_x, pos2, 2000), (position_set_y, pos2, 2000), # Tittle Size
(create_text_overlay, reg0, "str_keyconfig", tf_center_justify), (overlay_set_color, reg0, 0x000000),
(overlay_set_position, reg0, pos1), (overlay_set_size, reg0, pos2),
(position_set_y, pos1, 649), (position_set_x, pos1, 499), # Tittle Position
(create_text_overlay, reg0, "str_keyconfig", tf_center_justify), (overlay_set_color, reg0, 0xFF0000),
(overlay_set_position, reg0, pos1), (overlay_set_size, reg0, pos2),
# Notes
(position_set_y, pos1, 25), (position_set_x, pos1, 50), # Notes' Position
(position_set_x, pos2, 750), (position_set_y, pos2, 750), # Note's Size
(create_text_overlay, "$g_presentation_obj_2", "str_keyesc", tf_left_align),
(overlay_set_position, "$g_presentation_obj_2", pos1), (overlay_set_size, "$g_presentation_obj_2", pos2),
(overlay_set_color, "$g_presentation_obj_2", 0x678000), # Note's Color
(overlay_set_alpha, "$g_presentation_obj_2", 0x00), # Note's invisible
(position_set_y, pos1, 600), # First KeyConfig's Label Position
(try_begin),
(le, number_of_keys, two_columns_limit), # KeyConfig's Labels Size
(position_set_x, pos2, 1000), (position_set_y, pos2, 1000),
(position_set_x, pos1, 50),
(else_try),
(position_set_x, pos2, 750), (position_set_y, pos2, 750),
(position_set_x, pos1, 25),
(try_end),
(position_set_x, pos3, 120), (position_set_y, pos3, 28), # KeyConfig's Buttons Size
(try_for_range, ":no", 0, number_of_keys),
(store_add, ":off_string", ":no", key_names_begin), # Offset for Label String
(store_add, ":off_overlay2", ":no", slot_key_overlay_begin), # Offset for Buttons Overlay
(store_add, ":off_overlay1", ":off_overlay2", number_of_keys), # Offset for Labels Overlay
(create_text_overlay, reg0, ":off_string", tf_left_align), # Creating,
(troop_set_slot, key_config_data, ":off_overlay1", reg0), # Saving,
(overlay_set_position, reg0, pos1), (overlay_set_size, reg0, pos2), # Positioning, and Resizing KeyConfig Labels
(try_begin),
(le, number_of_keys, two_columns_limit),
(position_move_x, pos1, 35), # Move Right to give spave between labels and buttons
(else_try),
(position_move_x, pos1, 24),
(try_end),
(create_game_button_overlay, reg0, "str_no_string", tf_center_justify), # Creating,
(troop_set_slot, key_config_data, ":off_overlay2", reg0), # Saving,
(overlay_set_position, reg0, pos1), (overlay_set_size, reg0, pos3), # Positioning, and Resizing KeyConfig Buttons
(try_begin),
(le, number_of_keys, two_columns_limit),
(position_move_x, pos1, -35), # Move Left
(else_try),
(position_move_x, pos1, -24),
(try_end),
(position_move_y, pos1, -4), # Move left, and move down for next keyconfig's label and button
(try_begin),
(le, number_of_keys, two_columns_limit),
(store_mod, ":off_string", number_of_keys, 2),
(try_begin),
(eq, ":no", number_of_keys/2), # Half of keyconfigs positions are moved up and right
(eq, ":off_string", 1),
(assign, ":move_y", number_of_keys/2+1), (val_mul, ":move_y", 4),
(position_move_y, pos1, ":move_y"),
(position_move_x, pos1, 50),
(else_try),
(eq, ":no", number_of_keys/2 - 1), # Half of keyconfigs positions are moved up and right
(eq, ":off_string", 0),
(assign, ":move_y", number_of_keys/2), (val_mul, ":move_y", 4),
(position_move_y, pos1, ":move_y"),
(position_move_x, pos1, 50),
(try_end),
(else_try),
(store_mod, ":off_string", number_of_keys, 3),
(try_begin),
(eq, ":off_string", 0),
(try_begin),
(eq, ":no", number_of_keys/3 - 1), # 2nd 1/3 part of keyconfigs positions are moved up and right
(assign, ":move_y", number_of_keys/3), (val_mul, ":move_y", 4),
(position_move_y, pos1, ":move_y"),
(position_move_x, pos1, 33),
(else_try),
(eq, ":no", number_of_keys/3 + number_of_keys/3 - 1), # 3nd 2/3 part of keyconfigs positions are moved up and right
(assign, ":move_y", number_of_keys/3), (val_mul, ":move_y", 4),
(position_move_y, pos1, ":move_y"),
(position_move_x, pos1, 33),
(try_end),
(else_try),
(eq, ":off_string", 1),
(try_begin),
(eq, ":no", number_of_keys/3), # 2nd 1/3 part of keyconfigs positions are moved up and right
(assign, ":move_y", number_of_keys/3+1), (val_mul, ":move_y", 4),
(position_move_y, pos1, ":move_y"),
(position_move_x, pos1, 33),
(else_try),
(eq, ":no", number_of_keys/3 + number_of_keys/3), # 3nd 2/3 part of keyconfigs positions are moved up and right
(assign, ":move_y", number_of_keys/3), (val_mul, ":move_y", 4),
(position_move_y, pos1, ":move_y"),
(position_move_x, pos1, 33),
(try_end),
(else_try),
(eq, ":off_string", 2),
(try_begin),
(eq, ":no", number_of_keys/3) , # 2nd 1/3 part of keyconfigs positions are moved up and right
(assign, ":move_y", number_of_keys/3+1), (val_mul, ":move_y", 4),
(position_move_y, pos1, ":move_y"),
(position_move_x, pos1, 33),
(else_try),
(eq, ":no", number_of_keys/3 + number_of_keys/3 + 1), # 3nd 2/3 part of keyconfigs positions are moved up and right
(assign, ":move_y", number_of_keys/3+1), (val_mul, ":move_y", 4),
(position_move_y, pos1, ":move_y"),
(position_move_x, pos1, 33),
(try_end),
(try_end),
(try_end),
(try_end),
(call_script, "script_set_config_slot_key_config"), # Get Current KeyConfig
(call_script, "script_update_key_config_buttons"), # Writting the Captions
# Other Buttons
(create_game_button_overlay, "$g_presentation_obj_3", "str_ok", tf_center_justify), # OK Button
(create_game_button_overlay, "$g_presentation_obj_4", "str_reset", tf_center_justify), # RESET Button
(create_game_button_overlay, "$g_presentation_obj_5", "str_default", tf_center_justify), # DEFAULT Button
(create_game_button_overlay, "$g_presentation_obj_6", "str_disable_all", tf_center_justify), # DISABLE ALL Button
# Rearrange Buttons
(position_set_y, pos1, 25),
(position_set_x, pos1, 900), (overlay_set_position, "$g_presentation_obj_3", pos1),
(position_set_x, pos1, 900 - 1*160), (overlay_set_position, "$g_presentation_obj_4", pos1),
(position_set_x, pos1, 900 - 2*160), (overlay_set_position, "$g_presentation_obj_5", pos1),
(position_set_x, pos1, 900 - 3*160), (overlay_set_position, "$g_presentation_obj_6", pos1), ]),
(ti_on_presentation_event_state_change,
[(store_trigger_param_1, ":object"),
(assign, ":found", 0),
(store_add, ":upper_limit", slot_key_overlay_begin, number_of_keys), # Checking
(try_for_range, ":slot", slot_key_overlay_begin, ":upper_limit"), # for all KeyConfigs Button Overlay
(troop_slot_eq, key_config_data, ":slot", ":object"), # Found the Right Button Overlay?
(assign, "$key_config_button_down", 1), # Flag the event
(assign, "$ovr_no", ":object"), # Get The Overlay
(overlay_set_text, "$ovr_no", "str_press_any", tf_center_justify), # Change it's caption & color
(overlay_set_color, "$ovr_no", 0x0000FF),
(overlay_set_hilight_color, "$ovr_no", 0x0000FF),
(store_sub, "$key_no", ":slot", number_of_keys),
(assign, ":found", ":slot"), (assign, ":upper_limit", ":slot"), # Break the Loops
(try_end),
(try_begin),
(gt, ":found", 0), # Skip, the Object is a keyconfig button
(else_try),
(eq, ":object", "$g_presentation_obj_3"), # OK pressed?
(call_script, "script_set_global_var_key_config"), # Write the Config to Global Variable
(call_script, "script_update_key_config_buttons"), # it's not neccessary, just incase
(presentation_set_duration, 0),
(change_screen_return),
(else_try),
(eq, ":object", "$g_presentation_obj_4"), # RESET pressed?
(call_script, "script_set_config_slot_key_config"), # Reload Current Config
(call_script, "script_update_key_config_buttons"), # Update Captions
(else_try),
(eq, ":object", "$g_presentation_obj_5"), # DEFAULT pressed?
(call_script, "script_reset_to_default_keys"), # Reload Default Config
(call_script, "script_update_key_config_buttons"), # Update Captions
(else_try),
(eq, ":object", "$g_presentation_obj_6"), # DISABLE ALL pressed?
(try_for_range, ":no", slot_keys_begin, slot_keys_begin + number_of_keys),
(troop_set_slot, key_config_data, ":no", 0xff), # Disable all keys
(try_end),
(call_script, "script_update_key_config_buttons"), # Update Captions
(try_end),
(try_begin),
(eq, "$key_config_button_down", 1), # if a Button's waiting to set
(overlay_set_alpha, "$g_presentation_obj_2", 0xFF), # then make The Note visible
(try_end), ]),
(ti_on_presentation_run,
[(eq, "$key_config_button_down", 1), # if a Button's waiting to set
(assign, "$key_config_button_down", 0), #
(assign, ":found", 0),
(store_add, ":upper_limit", slot_key_defs_begin, number_of_all_keys), # Checking
(try_for_range, ":no", slot_key_defs_begin, ":upper_limit"), # for all keys
(troop_get_slot, ":key", key_config_data, ":no"), # reload the key from slot
(key_clicked, ":key"), # if that key's clicked?
(troop_set_slot, key_config_data, "$key_no", ":key"), # Yes, then save the new config for the button
(assign, ":found", ":key"), # Flag we found it
(assign, ":upper_limit", ":no"), # Break the Loops
(try_end),
(try_begin),
(gt, ":found", 0), # We Found the key
(else_try),
(key_clicked, key_escape), # ESC clicked?
(troop_set_slot, key_config_data, "$key_no", 0xff), # Disable the button
(else_try),
(assign, "$key_config_button_down", 1), # else? do nothing. The Button's still waiting
(try_end),
(try_begin),
(eq, "$key_config_button_down", 0), # if there's new keyconfig for the button
(try_for_range, ":no", slot_keys_begin, slot_keys_begin + number_of_keys), # Check if there is another button use the same key
(neq, ":no", "$key_no"),
(troop_slot_eq, key_config_data, ":no", ":found"), # if there is, then disable the button
(troop_set_slot, key_config_data, ":no", 0xff),
(try_end),
(overlay_set_hilight_color, "$ovr_no", 0x00ff00), # Set the button color back to normal
(overlay_set_alpha, "$g_presentation_obj_2", 0x00), # The Note must be dissapear again
(call_script, "script_update_key_config_buttons"), # Update Captions
(try_end), ]), ]),
Code:
####################################################################################################################
# Troops before this point are hardwired into the game and their order should not be changed!
####################################################################################################################
["find_item_cheat","find_item_cheat","find_item_cheat",tf_hero|tf_is_merchant,no_scene,reserved,fac_commoners,[],def_attrib,0,knows_common|knows_inventory_management_10,0],
["random_town_sequence","Random Town Sequence","Random Town Sequence",tf_hero,no_scene,reserved,fac_commoners,[],def_attrib,0,knows_common|knows_inventory_management_10,0],
["tournament_participants","Tournament Participants","Tournament Participants",tf_hero,no_scene,reserved,fac_commoners,[],def_attrib,0,knows_common|knows_inventory_management_10,0],
#####################################
# ARRAYs
["key_config","Key Config","Key Configs",tf_hero,no_scene,reserved,fac_commoners,[],def_attrib|str_14,0,knows_common|knows_power_draw_2,0],
#####################################
Code:
#-- Dunde's Key Config BEGIN
from header_triggers import *
#-- Parts to modify as your mod need --------------
keys_list = [("$key_crouch", key_caps_lock, "Crouching/Standing"),
("$key_crouch_command", key_comma, "Command to Stand"),
("$key_stand_command", key_period, "Command to Crouch"),
] # end of keys_list
#--------------------------------------------------
all_keys_list = [
(key_1, "1"), (key_2, "2"), (key_3, "3"), (key_4, "4"), (key_5, "5"), (key_6, "6"), (key_7, "7"), (key_8, "8"), (key_9, "9"), (key_0, "0"),
(key_a, "A"), (key_b, "B"), (key_c, "C"), (key_d, "D"), (key_e, "E"), (key_f, "F"), (key_g, "G"), (key_h, "H"), (key_i, "I"), (key_j, "J"),
(key_k, "K"), (key_l, "L"), (key_m, "M"), (key_n, "N"), (key_o, "O"), (key_p, "P"), (key_q, "Q"), (key_r, "R"), (key_s, "S"), (key_t, "T"),
(key_u, "U"), (key_v, "V"), (key_w, "W"), (key_x, "X"), (key_y, "Y"), (key_z, "Z"),
(key_numpad_0, "Numpad 0"), (key_numpad_1, "Numpad 1"), (key_numpad_2, "Numpad 2"), (key_numpad_3, "Numpad 3"), (key_numpad_4, "Numpad 4"),
(key_numpad_5, "Numpad 5"), (key_numpad_6, "Numpad 6"), (key_numpad_7, "Numpad 7"), (key_numpad_8, "Numpad 8"), (key_numpad_9, "Numpad 9"),
(key_num_lock, "Num Lock"), (key_numpad_slash, "Numpad DIV"), (key_numpad_multiply, "Numpad MUL"),
(key_numpad_minus, "Numpad MIN"), (key_numpad_plus, "Numpad PLUS"), (key_numpad_enter, "Numpad ENTER"), (key_numpad_period, "Numpad DEL)"),
(key_insert, "Insert"), (key_delete, "Delete"), (key_home, "Home"), (key_end, "End"), (key_page_up, "Page Up"), (key_page_down, "Page Down"),
(key_up, "Up"), (key_down, "Down"), (key_left, "Left"), (key_right, "Right"),
(key_f1, "F1"), (key_f1, "F2"), (key_f3, "F3"), (key_f4, "F4"), (key_f5, "F5"), (key_f6, "F6"),
(key_f7, "F7"), (key_f8, "F8"), (key_f9, "F9"), (key_f10, "F10"), (key_f11, "F11"), (key_f12, "F12"),
(key_space, "Space Bar"), (key_enter, "Enter"), (key_tab, "Tab"), (key_back_space, "Backspace"),
(key_open_braces, "[ "), (key_close_braces, " ] "), (key_comma, " < "), (key_period, " > "), (key_slash, " ? "), (key_back_slash, "\\"),
(key_equals, " = "), (key_minus, " -- "), (key_semicolon, "Semicolon"), (key_apostrophe, "Apostrophe"), (key_tilde, "Tilde"), (key_caps_lock, "Caps Lock"),
(key_left_shift, "Left Shift"), (key_right_shift, "Right Shift"), (key_left_control, "Left Ctrl"), (key_right_control, "Right Ctrl"), (key_left_alt, "Left Alt"), (key_right_alt, "Right Alt"),
(key_left_mouse_button, "Left Click"), (key_right_mouse_button, "Right Click"), (key_middle_mouse_button, "Middle Mouse Button"),
(key_mouse_button_4, "Mouse Button 4"), (key_mouse_button_5, "Mouse Button 5"), (key_mouse_button_6, "Mouse Button 6"),
(key_mouse_button_7, "Mouse Button 7"), (key_mouse_button_8, "Mouse Button 8"), (key_mouse_scroll_up, "Mouse Scroll Up"), (key_mouse_scroll_down, "Mouse Scroll Down"), ]
number_of_keys = len(keys_list)
number_of_all_keys = len(all_keys_list)
two_columns_limit = 10
slot_default_keys_begin = 0
slot_keys_begin = slot_default_keys_begin + number_of_keys
slot_key_overlay_begin = slot_keys_begin + number_of_keys
slot_key_defs_begin = slot_key_overlay_begin + number_of_keys + number_of_keys
key_config_data = "trp_key_config"
key_names_begin = "str_key_no1"
key_label_begin = "str_key_1"
#-- Dunde's Key Config END#-- Dunde's Key Config END
Code:
#-- Dunde's Key Config BEGIN
from module_constants import keys_list, all_keys_list
def get_key_strings():
key_strings = []
for i_key in xrange(len(all_keys_list)):
key_strings.append(("key_"+str(i_key+1), all_keys_list[i_key][1]))
for i_key in xrange(len(keys_list)):
key_strings.append(("key_no"+str(i_key+1), keys_list[i_key][2]))
return key_strings[:]
#-- Dunde's Key Config END
Code:
("keyconfig", "Keys Configuration"),
("keyesc", "Press any key to assign to^^or Esc to disable"),
("press_any","Press a key"),
("ok", "Ok"),
("reset", "Reset"),
("default", "Default"),
("disable_all", "Disable All"),
Code:
] + get_key_strings()
Usage :
Just call at any menu, camp menu will be fit :
Code:
("options_key",[],"Control Settings.", [(start_presentation, "prsnt_key_config"),]),
#-- Parts to modify as your mod need --------------