fixed chaining of console commands, allows snatching updates of console variables. cleaned up some code

This commit is contained in:
Magnus Auvinen 2009-06-15 14:01:36 +00:00
parent 0160f26514
commit 7b68ff4277
6 changed files with 80 additions and 41 deletions

View file

@ -417,6 +417,10 @@ int client_send_msg()
if(!info)
return -1;
if(client_state() == CLIENTSTATE_OFFLINE)
return 0;
mem_zero(&packet, sizeof(NETCHUNK));

View file

@ -428,6 +428,27 @@ static void str_variable_command(void *result, void *user_data)
}
}
static void console_chain(void *result, void *user_data)
{
COMMANDCHAIN *info = (COMMANDCHAIN *)user_data;
info->chain_callback(result, info->user_data, info->callback, info->callback_user_data);
}
void console_chain_command(const char *cmd, COMMANDCHAIN *chaininfo, CONSOLE_CHAIN_CALLBACK cb, void *user)
{
COMMAND *command = console_get_command(cmd);
/* store info */
chaininfo->chain_callback = cb;
chaininfo->callback = command->callback;
chaininfo->callback_user_data = command->user_data;
chaininfo->user_data = user;
/* chain */
command->callback = console_chain;
command->user_data = chaininfo;
}
void console_init()
{
MACRO_REGISTER_COMMAND("echo", "r", CFGFLAG_SERVER|CFGFLAG_CLIENT, con_echo, 0x0, "Echo the text");

View file

@ -6,6 +6,7 @@ extern "C"{
#endif
typedef void (*CONSOLE_CALLBACK)(void *result, void *user_data);
typedef void (*CONSOLE_CHAIN_CALLBACK)(void *result, void *user_data, CONSOLE_CALLBACK cb, void *cbuser);
typedef struct COMMAND_t
{
@ -18,6 +19,14 @@ typedef struct COMMAND_t
struct COMMAND_t *next;
} COMMAND;
typedef struct COMMANDCHAIN_t
{
CONSOLE_CHAIN_CALLBACK chain_callback;
CONSOLE_CALLBACK callback;
void *callback_user_data;
void *user_data;
} COMMANDCHAIN;
void console_init();
void console_register(COMMAND *cmd);
void console_execute_line(const char *str);
@ -25,6 +34,7 @@ void console_execute_line_stroked(int stroke, const char *str);
void console_execute_file(const char *filename);
void console_possible_commands(const char *str, int flagmask, void (*callback)(const char *cmd, void *user), void *user);
COMMAND *console_get_command(const char *cmd);
void console_chain_command(const char *cmd, COMMANDCHAIN *chaininfo, CONSOLE_CHAIN_CALLBACK cb, void *user);
void console_print(const char *str);
void console_register_print_callback(void (*callback)(const char *, void *user_data), void *user_data);

View file

@ -178,6 +178,8 @@ static float listbox_rowheight;
static int listbox_itemindex;
static int listbox_selected_index;
static int listbox_new_selected;
static int listbox_doneevents;
static int listbox_numitems;
void MENUS::ui_do_listbox_start(void *id, const RECT *rect, float row_height, const char *title, int num_items, int selected_index)
{
@ -207,6 +209,8 @@ void MENUS::ui_do_listbox_start(void *id, const RECT *rect, float row_height, co
listbox_new_selected = selected_index;
listbox_itemindex = 0;
listbox_rowheight = row_height;
listbox_numitems = num_items;
listbox_doneevents = 0;
//int num_servers = client_serverbrowse_sorted_num();
@ -270,6 +274,25 @@ MENUS::LISTBOXITEM MENUS::ui_do_listbox_nextitem(void *id)
if(listbox_selected_index==listbox_itemindex)
{
if(!listbox_doneevents)
{
listbox_doneevents = 1;
for(int i = 0; i < num_inputevents; i++)
{
if(inputevents[i].flags&INPFLAG_PRESS)
{
if(inputevents[i].key == KEY_DOWN) listbox_new_selected++;
if(inputevents[i].key == KEY_UP) listbox_new_selected--;
}
}
if(listbox_new_selected >= listbox_numitems)
listbox_new_selected = listbox_numitems-1;
if(listbox_new_selected < 0)
listbox_new_selected = 0;
}
//selected_index = i;
RECT r = row;
ui_margin(&r, 1.5f, &r);
@ -289,28 +312,6 @@ int MENUS::ui_do_listbox_end()
return listbox_new_selected;
}
/*
void MENUS::demolist_listdir_callback(const char *name, int is_dir, void *user)
{
(*count)++;
LISTBOXITEM item = ui_do_listbox_nextitem((void*)(10+*count));
if(item.visible)
ui_do_label(&item.rect, name, item.rect.h*fontmod_height, -1);
}
DEMOITEM *demos;
int num_demos;
*/
/*void MENUS::demolist_count_callback(const char *name, int is_dir, void *user)
{
if(is_dir || name[0] == '.')
return;
(*(int *)user)++;
}*/
struct FETCH_CALLBACKINFO
{
MENUS *self;
@ -329,32 +330,14 @@ void MENUS::demolist_fetch_callback(const char *name, int is_dir, void *user)
str_format(item.filename, sizeof(item.filename), "%s/%s", info->prefix, name);
str_copy(item.name, name, sizeof(item.name));
info->self->demos.add(item);
/*
if(info->count == info->self->num_demos)
return;
info->count++;
*/
}
void MENUS::demolist_populate()
{
demos.clear();
/*if(demos)
mem_free(demos);
demos = 0;
num_demos = 0;*/
char buf[512];
str_format(buf, sizeof(buf), "%s/demos", client_user_directory());
//fs_listdir(buf, demolist_count_callback, &num_demos);
//fs_listdir("demos", demolist_count_callback, &num_demos);
//demos = (DEMOITEM *)mem_alloc(sizeof(DEMOITEM)*num_demos, 1);
//mem_zero(demos, sizeof(DEMOITEM)*num_demos);
FETCH_CALLBACKINFO info = {this, buf, 0};
fs_listdir(buf, demolist_fetch_callback, &info);

View file

@ -248,7 +248,18 @@ void GAMECLIENT::on_console_init()
// let all the other components register their console commands
for(int i = 0; i < all.num; i++)
all.components[i]->on_console_init();
//
{ static COMMANDCHAIN chain; console_chain_command("player_name", &chain, conchain_special_infoupdate, this); }
{ static COMMANDCHAIN chain; console_chain_command("player_use_custom_color", &chain, conchain_special_infoupdate, this); }
{ static COMMANDCHAIN chain; console_chain_command("player_color_body", &chain, conchain_special_infoupdate, this); }
{ static COMMANDCHAIN chain; console_chain_command("player_color_feet", &chain, conchain_special_infoupdate, this); }
{ static COMMANDCHAIN chain; console_chain_command("player_skin", &chain, conchain_special_infoupdate, this); }
//
suppress_events = false;
}
@ -954,3 +965,10 @@ void GAMECLIENT::con_kill(void *result, void *user_data)
{
((GAMECLIENT*)user_data)->send_kill(-1);
}
void GAMECLIENT::conchain_special_infoupdate(void *result, void *user_data, CONSOLE_CALLBACK cb, void *cbuser)
{
cb(result, cbuser);
if(console_arg_num(result))
((GAMECLIENT*)user_data)->send_info(false);
}

View file

@ -1,5 +1,6 @@
#include <base/vmath.hpp>
#include <engine/e_console.h>
#include <game/gamecore.hpp>
#include "render.hpp"
@ -34,6 +35,8 @@ class GAMECLIENT
static void con_team(void *result, void *user_data);
static void con_kill(void *result, void *user_data);
static void conchain_special_infoupdate(void *result, void *user_data, CONSOLE_CALLBACK cb, void *cbuser);
public:
bool suppress_events;
bool new_tick;