[2066] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Christian Meyer |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | #include "command_node.h" |
---|
| 18 | #include "keynames.h" |
---|
| 19 | #include "ini_parser.h" |
---|
| 20 | |
---|
| 21 | #include <stdio.h> |
---|
| 22 | #include <strings.h> |
---|
| 23 | |
---|
| 24 | using namespace std; |
---|
| 25 | |
---|
| 26 | CommandNode::CommandNode (int ID) |
---|
| 27 | { |
---|
| 28 | bound = new List<WorldEntity>(); |
---|
| 29 | aliases = NULL; |
---|
| 30 | netID = ID; |
---|
| 31 | bLocalInput = false; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | CommandNode::CommandNode (char* filename = DEFAULT_KEYBIND_FILE) |
---|
| 35 | { |
---|
| 36 | bLocalInput = true; |
---|
| 37 | netID = 0; |
---|
| 38 | bound = new List<WorldEntity>(); |
---|
| 39 | load_bindings (filename); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | CommandNode::~CommandNode () |
---|
| 43 | { |
---|
| 44 | if( aliases != NULL) free (aliases); |
---|
| 45 | if( bound != NULL) delete bound; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | void CommandNode::load_bindings (char* filename) |
---|
| 49 | { |
---|
| 50 | FILE* stream; |
---|
| 51 | |
---|
| 52 | if( filename == NULL) filename = DEFAULT_KEYBIND_FILE; |
---|
| 53 | |
---|
| 54 | // remove old bindings if present |
---|
| 55 | if( aliases != NULL) |
---|
| 56 | { |
---|
| 57 | free (aliases); |
---|
| 58 | aliases = NULL; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | // create parser |
---|
| 62 | IniParser parser (filename); |
---|
| 63 | if( parser.get_section ("Bindings") == -1) |
---|
| 64 | { |
---|
| 65 | printf("Could not find key bindings in %s\n", filename); |
---|
| 66 | return; |
---|
| 67 | } |
---|
| 68 | // allocate empty lookup table |
---|
| 69 | aliases = (KeyBindings*) calloc (1, sizeof (KeyBindings)); |
---|
| 70 | |
---|
| 71 | char namebuf[256]; |
---|
| 72 | char valuebuf[256]; |
---|
| 73 | int* index; |
---|
| 74 | |
---|
| 75 | while( parser.next_var (namebuf, valuebuf) != -1) |
---|
| 76 | { |
---|
| 77 | index = name_to_index (namebuf); |
---|
| 78 | switch( index[0]) |
---|
| 79 | { |
---|
| 80 | case 0: |
---|
| 81 | strcpy (aliases->keys[index[1]], valuebuf); |
---|
| 82 | break; |
---|
| 83 | case 1: |
---|
| 84 | strcpy (aliases->buttons[index[1]], valuebuf); |
---|
| 85 | break; |
---|
| 86 | default: |
---|
| 87 | break; |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | void CommandNode::bind (WorldEntity* entity) |
---|
| 93 | { |
---|
| 94 | bound->add (entity, LIST_ADD_NEXT, true); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void CommandNode::unbind (WorldEntity* entity) |
---|
| 98 | { |
---|
| 99 | bound->remove (entity, LIST_FIND_FW); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | int* CommandNode::name_to_index (char* name) |
---|
| 103 | { |
---|
| 104 | coord[0] = -1; |
---|
| 105 | if( (coord[1] = keyname_to_SDLK (name)) != -1) coord[0] = 0; |
---|
| 106 | if( (coord[1] = buttonname_to_SDLB (name)) != -1) coord[0] = 1; |
---|
| 107 | return coord; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void CommandNode::process () |
---|
| 111 | { |
---|
| 112 | if( bLocalInput) process_local (); |
---|
| 113 | else process_network (); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void CommandNode::process_local () |
---|
| 117 | { |
---|
| 118 | SDL_Event event; |
---|
| 119 | Command cmd; |
---|
| 120 | |
---|
| 121 | while( SDL_PollEvent (&event)) |
---|
| 122 | { |
---|
| 123 | switch( event.type) |
---|
| 124 | { |
---|
| 125 | case SDL_KEYDOWN: |
---|
| 126 | strcpy (cmd.cmd, aliases->keys[event.key.keysym.sym]); |
---|
| 127 | cmd.bUp = false; |
---|
| 128 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
| 129 | break; |
---|
| 130 | case SDL_KEYUP: |
---|
| 131 | strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]); |
---|
| 132 | cmd.bUp = true; |
---|
| 133 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
| 134 | break; |
---|
| 135 | case SDL_MOUSEMOTION: |
---|
| 136 | strcpy( cmd.cmd, "cursor"); |
---|
| 137 | cmd.x = event.motion.x; |
---|
| 138 | cmd.y = event.motion.y; |
---|
| 139 | cmd.xrel = event.motion.xrel; |
---|
| 140 | cmd.yrel = event.motion.yrel; |
---|
| 141 | break; |
---|
| 142 | case SDL_MOUSEBUTTONUP: |
---|
| 143 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
| 144 | cmd.bUp = true; |
---|
| 145 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
| 146 | break; |
---|
| 147 | case SDL_MOUSEBUTTONDOWN: |
---|
| 148 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
| 149 | cmd.bUp = false; |
---|
| 150 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
| 151 | break; |
---|
| 152 | case SDL_JOYAXISMOTION: |
---|
| 153 | case SDL_JOYBALLMOTION: |
---|
| 154 | case SDL_JOYHATMOTION: |
---|
| 155 | case SDL_JOYBUTTONDOWN: |
---|
| 156 | case SDL_JOYBUTTONUP: |
---|
| 157 | break; |
---|
| 158 | default: |
---|
[2068] | 159 | Orxonox *orx = Orxonox::getInstance(); |
---|
| 160 | orx->event_handler (&event); |
---|
[2066] | 161 | break; |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | void CommandNode::process_network () |
---|
| 167 | { |
---|
| 168 | |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | void CommandNode::relay (Command* cmd) |
---|
| 172 | { |
---|
| 173 | List<WorldEntity>* plist = bound; |
---|
| 174 | |
---|
| 175 | while( (plist = plist->get_next()) != NULL) |
---|
| 176 | { |
---|
| 177 | (plist->get_object())->command (cmd); |
---|
| 178 | } |
---|
| 179 | } |
---|