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