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 | #include "world_entity.h" |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <strings.h> |
---|
24 | #include <stdlib.h> |
---|
25 | |
---|
26 | using namespace std; |
---|
27 | |
---|
28 | |
---|
29 | CommandNode::CommandNode (int ID) |
---|
30 | { |
---|
31 | bound = new List<WorldEntity>(); |
---|
32 | aliases = NULL; |
---|
33 | netID = ID; |
---|
34 | bLocalInput = false; |
---|
35 | } |
---|
36 | |
---|
37 | CommandNode::CommandNode (char* filename = DEFAULT_KEYBIND_FILE) |
---|
38 | { |
---|
39 | bLocalInput = true; |
---|
40 | netID = 0; |
---|
41 | bound = new List<WorldEntity>(); |
---|
42 | load_bindings (filename); |
---|
43 | } |
---|
44 | |
---|
45 | CommandNode::~CommandNode () |
---|
46 | { |
---|
47 | if( aliases != NULL) free (aliases); |
---|
48 | if( bound != NULL) delete bound; |
---|
49 | } |
---|
50 | |
---|
51 | void CommandNode::load_bindings (char* filename) |
---|
52 | { |
---|
53 | FILE* stream; |
---|
54 | |
---|
55 | printf("Loading key bindings from %s\n", filename); |
---|
56 | |
---|
57 | if( filename == NULL) filename = DEFAULT_KEYBIND_FILE; |
---|
58 | |
---|
59 | // remove old bindings if present |
---|
60 | if( aliases != NULL) |
---|
61 | { |
---|
62 | free (aliases); |
---|
63 | aliases = NULL; |
---|
64 | } |
---|
65 | |
---|
66 | // create parser |
---|
67 | IniParser parser (filename); |
---|
68 | if( parser.get_section ("Bindings") == -1) |
---|
69 | { |
---|
70 | printf("Could not find key bindings in %s\n", filename); |
---|
71 | return; |
---|
72 | } |
---|
73 | // allocate empty lookup table |
---|
74 | aliases = (KeyBindings*) calloc (1, sizeof (KeyBindings)); |
---|
75 | |
---|
76 | char namebuf[256]; |
---|
77 | char valuebuf[256]; |
---|
78 | memset (namebuf, 0, 256); |
---|
79 | memset (valuebuf, 0, 256); |
---|
80 | int* index; |
---|
81 | |
---|
82 | while( parser.next_var (namebuf, valuebuf) != -1) |
---|
83 | { |
---|
84 | index = name_to_index (namebuf); |
---|
85 | switch( index[0]) |
---|
86 | { |
---|
87 | case 0: |
---|
88 | printf("Key binding %d(%s) set to %s\n", index[1], SDLK_to_keyname( index[1]), valuebuf); |
---|
89 | strcpy (aliases->keys[index[1]], valuebuf); |
---|
90 | break; |
---|
91 | case 1: |
---|
92 | printf("Button binding %d(%s) set to %s\n", index[1], SDLB_to_buttonname( index[1]), valuebuf); |
---|
93 | strcpy (aliases->buttons[index[1]], valuebuf); |
---|
94 | break; |
---|
95 | default: |
---|
96 | break; |
---|
97 | } |
---|
98 | memset (namebuf, 0, 256); |
---|
99 | memset (valuebuf, 0, 256); |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | void CommandNode::bind (WorldEntity* entity) |
---|
104 | { |
---|
105 | bound->add (entity, LIST_ADD_NEXT, true); |
---|
106 | } |
---|
107 | |
---|
108 | void CommandNode::unbind (WorldEntity* entity) |
---|
109 | { |
---|
110 | bound->remove (entity, LIST_FIND_FW); |
---|
111 | } |
---|
112 | |
---|
113 | int* CommandNode::name_to_index (char* name) |
---|
114 | { |
---|
115 | coord[0] = -1; |
---|
116 | coord[1] = -1; |
---|
117 | int c; |
---|
118 | if( (c = keyname_to_SDLK (name)) != -1) |
---|
119 | { |
---|
120 | coord[1] = c; |
---|
121 | coord[0] = 0; |
---|
122 | } |
---|
123 | if( (c = buttonname_to_SDLB (name)) != -1) |
---|
124 | { |
---|
125 | coord[1] = c; |
---|
126 | coord[0] = 1; |
---|
127 | } |
---|
128 | return coord; |
---|
129 | } |
---|
130 | |
---|
131 | void CommandNode::process () |
---|
132 | { |
---|
133 | perror("CommandNode|process()"); |
---|
134 | if( bLocalInput) process_local (); |
---|
135 | else process_network (); |
---|
136 | } |
---|
137 | |
---|
138 | void CommandNode::process_local () |
---|
139 | { |
---|
140 | SDL_Event event; |
---|
141 | Command cmd; |
---|
142 | |
---|
143 | while( SDL_PollEvent (&event)) |
---|
144 | { |
---|
145 | memset (cmd.cmd, 0, CMD_LENGHT); |
---|
146 | switch( event.type) |
---|
147 | { |
---|
148 | case SDL_KEYDOWN: |
---|
149 | strcpy (cmd.cmd, aliases->keys[event.key.keysym.sym]); |
---|
150 | cmd.bUp = false; |
---|
151 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
152 | break; |
---|
153 | case SDL_KEYUP: |
---|
154 | strcpy( cmd.cmd, aliases->keys[event.key.keysym.sym]); |
---|
155 | cmd.bUp = true; |
---|
156 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
157 | break; |
---|
158 | case SDL_MOUSEMOTION: |
---|
159 | strcpy( cmd.cmd, "cursor"); |
---|
160 | cmd.x = event.motion.x; |
---|
161 | cmd.y = event.motion.y; |
---|
162 | cmd.xrel = event.motion.xrel; |
---|
163 | cmd.yrel = event.motion.yrel; |
---|
164 | break; |
---|
165 | case SDL_MOUSEBUTTONUP: |
---|
166 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
167 | cmd.bUp = true; |
---|
168 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
169 | break; |
---|
170 | case SDL_MOUSEBUTTONDOWN: |
---|
171 | strcpy( cmd.cmd, aliases->buttons[event.button.button]); |
---|
172 | cmd.bUp = false; |
---|
173 | if( strlen (cmd.cmd) > 0) relay (&cmd); |
---|
174 | break; |
---|
175 | case SDL_JOYAXISMOTION: |
---|
176 | case SDL_JOYBALLMOTION: |
---|
177 | case SDL_JOYHATMOTION: |
---|
178 | case SDL_JOYBUTTONDOWN: |
---|
179 | case SDL_JOYBUTTONUP: |
---|
180 | break; |
---|
181 | default: |
---|
182 | Orxonox *orx = Orxonox::getInstance(); |
---|
183 | orx->event_handler (&event); |
---|
184 | break; |
---|
185 | } |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | void CommandNode::process_network () |
---|
190 | { |
---|
191 | |
---|
192 | } |
---|
193 | |
---|
194 | void CommandNode::relay (Command* cmd) |
---|
195 | { |
---|
196 | perror("CommandNode|relay()"); |
---|
197 | List<WorldEntity>* plist = bound; |
---|
198 | |
---|
199 | Orxonox *orx = Orxonox::getInstance(); |
---|
200 | if( orx->system_command (cmd)) return; |
---|
201 | |
---|
202 | if( bLocalInput) send_over_network (cmd); |
---|
203 | |
---|
204 | while( (plist = plist->get_next()) != NULL) |
---|
205 | { |
---|
206 | (plist->get_object())->command (cmd); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | void CommandNode::set_netID (int ID) |
---|
211 | { |
---|
212 | } |
---|
213 | |
---|
214 | void CommandNode::send_over_network (Command* cmd) |
---|
215 | { |
---|
216 | } |
---|