From 59ea9fffec01a6cc3fbf147aa311bfaa9abaa933 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 22 Jul 2008 13:14:35 +0000 Subject: list_actions, renamed thruster actions --- src/client/Makefile.am | 8 ++--- src/client/input.cc | 96 +++++++++++++++++++++++++++++++++----------------- src/client/key.h | 5 +++ src/client/keyboard.cc | 78 ++++++++++++++++++++++++++++++++++++---- src/client/keyboard.h | 11 ++++++ 5 files changed, 154 insertions(+), 44 deletions(-) (limited to 'src/client') diff --git a/src/client/Makefile.am b/src/client/Makefile.am index 39b53cf..46b8e21 100644 --- a/src/client/Makefile.am +++ b/src/client/Makefile.am @@ -7,12 +7,12 @@ else noinst_LTLIBRARIES = libclient.la endif -libclient_la_SOURCES = chat.cc client.cc console.cc hud.cc input.cc key.cc \ - keyboard.cc targets.cc video.cc view.cc +libclient_la_SOURCES = action.cc chat.cc client.cc console.cc hud.cc input.cc \ + key.cc keyboard.cc targets.cc video.cc view.cc libclient_la_CFLAGS = $(LIBSDL_CFLAGS) $(GL_CFLAGS) libclient_la_LDFLAGS = -avoid-version -no-undefined $(GL_LIBS) $(LIBSDL_LIBS) -noinst_HEADERS = chat.h client.h console.h input.h key.h keyboard.h targets.h \ - video.h view.h +noinst_HEADERS = action.h chat.h client.h console.h input.h key.h keyboard.h \ + targets.h video.h view.h libclient_la_LIBADD = $(top_builddir)/src/render/librender.la \ $(top_builddir)/src/core/libcore.la diff --git a/src/client/input.cc b/src/client/input.cc index dc40b8f..775f03a 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -59,9 +59,6 @@ bool mouse_control = false; const float thruster_offset = 0.05f; -// small hack to prevent two sounds triggered by the scrollwheel -bool singleclick = true; - //--- engine functions -------------------------------------------- void func_screenshot(std::string const & args) @@ -114,16 +111,42 @@ void func_ui_view(std::string const &args) } } -void func_list_keys(std::string const &args) +void func_list_actions(std::string const &args) { if (keyboard) - keyboard->list_keys(); + keyboard->list_actions(); + else + con_warn << "Keyboard handler not installed!" << std::endl; +} + +void func_list_keys(std::string const &args) +{ + if (keyboard) { + std::stringstream argstr(args); + std::string keyname; + if (argstr >> keyname) { + keyboard->list_bind(keyname); + } else { + keyboard->list_keys(); + } + } else { + con_warn << "Keyboard handler not installed!" << std::endl; + } } void func_list_binds(std::string const &args) { - if (keyboard) - keyboard->list_binds(); + if (keyboard) { + std::stringstream argstr(args); + std::string keyname; + if (argstr >> keyname) { + keyboard->list_bind(keyname); + } else { + keyboard->list_binds(); + } + } else { + con_warn << "Keyboard handler not installed!" << std::endl; + } } void func_bind(std::string const &args) @@ -134,28 +157,36 @@ void func_bind(std::string const &args) if (argstr >> keyname) { if (args.size() > keyname.size()+1) { keyboard->bind(keyname, args.substr(keyname.size()+1)); - return; + } else { + keyboard->list_bind(keyname); } + return; } - } + core::Func *func = core::Func::find("bind"); + con_print << "bind " << func->info() << std::endl; - core::Func *func = core::Func::find("bind"); - con_print << "bind " << func->info() << std::endl; + } else { + con_warn << "Keyboard handler not installed!" << std::endl; + } } void func_unbind(std::string const &args) { if (keyboard) { + std::stringstream argstr(args); std::string keyname; if (argstr >> keyname) { keyboard->unbind(keyname); return; } - } - core::Func *func = core::Func::find("unbind"); - con_print << "unbind " << func->info() << std::endl; + core::Func *func = core::Func::find("unbind"); + con_print << "unbind " << func->info() << std::endl; + + } else { + con_warn << "Keyboard handler not installed!" << std::endl; + } } //--- input functions --------------------------------------------- @@ -189,11 +220,14 @@ void init() func = core::Func::add("ui_control",func_ui_control); func->set_info("toggle mouse control"); + func = core::Func::add("list_actions", func_list_actions); + func->set_info("list key action names"); + func = core::Func::add("list_keys", func_list_keys); - func->set_info("list keyboard key names"); + func->set_info("list key names"); func = core::Func::add("list_binds",func_list_binds); - func->set_info("list keyboard binds"); + func->set_info("list keyb binds"); func = core::Func::add("bind", (core::FuncPtr) func_bind); func->set_info("[key] [str] bind a command to a key"); @@ -214,6 +248,7 @@ void shutdown() { con_print << "^BShutting down input..." << std::endl; + core::Func::remove("list_actions"); core::Func::remove("list_keys"); core::Func::remove("list_binds"); @@ -241,19 +276,11 @@ void shutdown() void action_press(std::string const &action) { /* -- thruster ------------------------------------ */ - if (action.compare("+thrust") == 0) { - if (local_thrust < 1.0f && singleclick) { - //audio::play("ui/thruster"); - singleclick = false; - } + if (action.compare("+thrustup") == 0) { local_thrust += thruster_offset; - } else if (action.compare("-thrust") == 0) { - if (local_thrust > 0.0f && singleclick) { - //audio::play("ui/thruster"); - singleclick = false; - } + } else if (action.compare("+thrustdown") == 0) { local_thrust -= 2.0f * thruster_offset; /* -- mouse control ------------------------------- */ @@ -298,10 +325,10 @@ void action_press(std::string const &action) void action_release(std::string const &action) { /* -- thruster ------------------------------------ */ - if (action.compare("+thrust") == 0) { + if (action.compare("+thrustup") == 0) { - } else if (action.compare("-thrust") == 0) { + } else if (action.compare("+thrustdown") == 0) { /* -- mouse control ------------------------------- */ @@ -372,7 +399,6 @@ void frame(float seconds) SDL_Event event; Key *key = 0; bool pressed = false; - singleclick = true; while (SDL_PollEvent(&event)) { pressed = false; @@ -450,7 +476,7 @@ void frame(float seconds) } else { // FIXME modifier char c = key->bind(Key::None).c_str()[0]; - if (c == '+' || c == '-') { + if (c == '+') { // action bind action_press(key->bind(Key::None)); @@ -464,7 +490,7 @@ void frame(float seconds) // FIXME modifier char c = key->bind(Key::None).c_str()[0]; - if (c && c != '+' && c != '-') { + if (c && c != '+') { // normal bind core::cmd() << key->bind(Key::None) << "\n"; } @@ -475,7 +501,7 @@ void frame(float seconds) if (core::application()->connected() && core::localcontrol() && !console()->visible() && !chat::visible()) { // FIXME modifier (note: mmmmm, modifier could be released by now) char c = key->bind(Key::None).c_str()[0]; - if (c == '+' || c == '-') { + if (c == '+') { // action bind action_release(key->bind(Key::None)); } @@ -586,8 +612,12 @@ void frame(float seconds) math::clamp(local_direction, -1.0f, 1.0f); math::clamp(local_pitch, -1.0f, 1.0f); - math::clamp(local_thrust, 0.0f, 1.0f); math::clamp(local_roll, -1.0f, 1.0f); + math::clamp(local_thrust, 0.0f, 1.0f); + if (local_thrust < 0.01f) + local_thrust = 0.0f; + else if (local_thrust > 0.99f) + local_thrust = 1.0f; core::localcontrol()->set_thrust(local_thrust); core::localcontrol()->set_direction(local_direction); diff --git a/src/client/key.h b/src/client/key.h index 344e1fa..bd6f6bd 100644 --- a/src/client/key.h +++ b/src/client/key.h @@ -11,6 +11,11 @@ namespace client { +/// a pressable key +/** + * a Key instance can contain any kind of 'key' like a keyboard key, + * a mouse button, or a joystick button + */ class Key { public: diff --git a/src/client/keyboard.cc b/src/client/keyboard.cc index c569499..da93814 100644 --- a/src/client/keyboard.cc +++ b/src/client/keyboard.cc @@ -28,6 +28,31 @@ Keyboard::Keyboard() numlock = false; capslock = false; + // ------------------ ACTIONS + + // note: actions should be state keys and not use key repeat + // FIXME: thruster should be a state key + + add_action("+left", Action::None, "rotate left"); + add_action("+right", Action::None, "rotate right"); + add_action("+up", Action::None, "rotate up"); + add_action("+down", Action::None, "rotate down"); + + add_action("+rollleft", Action::None, "roll left"); + add_action("+rollright", Action::None, "roll right"); + + add_action("+camleft", Action::None, "rotate camera left"); + add_action("+camright", Action::None, "rotate camera right"); + add_action("+camup", Action::None, "rotate camera up"); + add_action("+camdown", Action::None, "rotate camera down"); + + add_action("+thrustup", Action::None, "increase thruster"); + add_action("+thrustdown", Action::None, "decrease thruster"); + + add_action("+control", Action::None, "enable mouse control while pressed"); + + // ------------------ KEYS + add_key("backspace", SDLK_BACKSPACE); add_key("tab", SDLK_TAB); add_key("clear", SDLK_CLEAR); @@ -117,8 +142,8 @@ Keyboard::Keyboard() add_key("kpperiod", SDLK_KP_PERIOD, '.'); add_key("kpdiv", SDLK_KP_DIVIDE, '/', "+rollleft"); add_key("kpmul", SDLK_KP_MULTIPLY, '*', "+rollright"); - add_key("kpmin", SDLK_KP_MINUS, '-', "-thrust"); - add_key("kpplus", SDLK_KP_PLUS, '+', "+thrust"); + add_key("kpmin", SDLK_KP_MINUS, '-', "+thrustdown"); + add_key("kpplus", SDLK_KP_PLUS, '+', "+thrustup"); add_key("kpenter", SDLK_KP_ENTER, '\n'); add_key("kpequal", SDLK_KP_EQUALS, '='); @@ -181,16 +206,21 @@ Keyboard::Keyboard() add_key("mouse2", 512 + SDL_BUTTON_RIGHT); add_key("mouse3", 512 + SDL_BUTTON_MIDDLE); - add_key("mouse4", 512 + SDL_BUTTON_WHEELUP, 0, "+thrust"); - add_key("mouse5", 512 + SDL_BUTTON_WHEELDOWN, 0, "-thrust"); + add_key("mouse4", 512 + SDL_BUTTON_WHEELUP, 0, "+thrustup"); + add_key("mouse5", 512 + SDL_BUTTON_WHEELDOWN, 0, "+thrustdown"); } Keyboard::~Keyboard() { + // clear key map for (iterator it = begin(); it != end(); it++) delete(*it).second; - keys.clear(); + + // clear action list + for(std::list::iterator ait = actions.begin(); ait != actions.end(); ait++) + delete (*ait); + actions.clear(); } void Keyboard::save_binds() @@ -323,12 +353,46 @@ void Keyboard::add_key(const char *name, const unsigned int keysym, const char a keys[keysym] = new Key(name, keysym, ascii, bind); } +void Keyboard::add_action(const char *name, Action::Identifier action, const char *info) +{ + actions.push_back(new Action(name, action, info)); +} + +void Keyboard::list_actions() +{ + for (std::list::iterator it = actions.begin(); it != actions.end(); it++) { + con_print << " " << (*it)->name() << " " << (*it)->info() << std::endl; + } + con_print << actions.size() << " registered actions" << std::endl; +} + void Keyboard::list_keys() { for (iterator it = begin(); it != end(); it++) { con_print << " " << aux::pad_left((*it).second->name(), 6) << " " << (*it).second->bind(Key::None) << std::endl; } - con_print << keys.size() << " keys" << std::endl; + con_print << keys.size() << " registered keys" << std::endl; +} + +void Keyboard::list_bind(std::string const &name) +{ + Key *key = find(name); + if (key) { + if (key->bind(Key::None).size()) { + con_print << " " << aux::pad_left(key->name(), 6) << " " << key->bind(Key::None) << std::endl; + } + if (key->bind(Key::Shift).size()) { + con_print << " shift+" << aux::pad_left(key->name(), 6) << " " << key->bind(Key::Shift) << std::endl; + } + if (key->bind(Key::Ctrl).size()) { + con_print << " ctrl+" << aux::pad_left(key->name(), 6) << " " << key->bind(Key::Ctrl) << std::endl; + } + if (key->bind(Key::Alt).size()) { + con_print << " alt+" << aux::pad_left(key->name(), 6) << " " << key->bind(Key::Alt) << std::endl; + } + } else { + con_print << "key '" << name << "' not found" << std::endl; + } } void Keyboard::list_binds() @@ -353,7 +417,7 @@ void Keyboard::list_binds() } } - con_print << n << " binds" << std::endl; + con_print << n << " registered binds" << std::endl; } void setkeyboardmode(bool input) diff --git a/src/client/keyboard.h b/src/client/keyboard.h index 8275b1a..1c70179 100644 --- a/src/client/keyboard.h +++ b/src/client/keyboard.h @@ -9,9 +9,11 @@ #include #include +#include #include "SDL/SDL.h" +#include "client/action.h" #include "client/key.h" namespace client { @@ -40,9 +42,15 @@ public: /// list keyboard key names void list_keys(); + /// list actions + void list_actions(); + /// list keyboard binds void list_binds(); + /// list binds for a singe key + void list_bind(std::string const &name); + /// load keyboard binds void load_binds(); @@ -64,7 +72,10 @@ private: void add_key(const char *name, const unsigned int keysym, const char ascii=0, const char *bind=0); + void add_action(const char *name, Action::Identifier action, const char *info = 0); + std::map keys; + std::list actions; bool numlock; bool capslock; -- cgit v1.2.3