Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
JoyDlg.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: JoyDlg.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12 */
13 
14 #include "MemDebug.h"
15 #include "JoyDlg.h"
16 #include "KeyMap.h"
17 #include "MenuScreen.h"
18 #include "Starshatter.h"
19 #include "FormatUtil.h"
20 
21 #include "Game.h"
22 #include "ListBox.h"
23 #include "ComboBox.h"
24 #include "Button.h"
25 #include "Joystick.h"
26 
27 // +--------------------------------------------------------------------+
28 // DECLARE MAPPING FUNCTIONS:
29 
30 DEF_MAP_CLIENT(JoyDlg, OnApply);
31 DEF_MAP_CLIENT(JoyDlg, OnCancel);
32 DEF_MAP_CLIENT(JoyDlg, OnAxis);
33 
34 static const char* joy_axis_names[] = {
35  "JoyDlg.axis.0",
36  "JoyDlg.axis.1",
37  "JoyDlg.axis.2",
38  "JoyDlg.axis.3",
39  "JoyDlg.axis.4",
40  "JoyDlg.axis.5",
41  "JoyDlg.axis.6",
42  "JoyDlg.axis.7"
43 };
44 
45 static int selected_axis = -1;
46 static int sample_axis = -1;
47 static int samples[8];
48 
49 static int map_axis[4];
50 
51 // +--------------------------------------------------------------------+
52 
54 : FormWindow(s, 0, 0, s->Width(), s->Height()), manager(mgr),
55 apply(0), cancel(0), message(0)
56 {
57  Init(def);
58 }
59 
61 {
62 }
63 
64 void
66 {
67  if (apply)
68  return;
69 
70  for (int i = 0; i < 4; i++) {
71  axis_button[i] = (Button*) FindControl(201 + i);
72  invert_checkbox[i] = (Button*) FindControl(301 + i);
73 
74  if (axis_button[i])
76  }
77 
78  message = FindControl(11);
79 
80  apply = (Button*) FindControl(1);
82 
83  cancel = (Button*) FindControl(2);
85 }
86 
87 // +--------------------------------------------------------------------+
88 
89 void
91 {
92  if (selected_axis >= 0 && selected_axis < 4) {
93  Joystick* joystick = Joystick::GetInstance();
94  if (joystick) {
95  joystick->Acquire();
96 
97  int delta = 1000;
98 
99  for (int i = 0; i < 8; i++) {
101 
102  int d = a - samples[i];
103  if (d < 0) d = -d;
104 
105  if (d > delta && samples[i] < 1e6) {
106  delta = d;
107  sample_axis = i;
108  }
109 
110  samples[i] = a;
111  }
112 
113  Button* b = axis_button[selected_axis];
114 
115  if (sample_axis >= 0) {
116  b->SetText(Game::GetText(joy_axis_names[sample_axis]));
117  map_axis[selected_axis] = sample_axis;
118  }
119 
120  else
121  b->SetText(Game::GetText("JoyDlg.select"));
122  }
123  }
124 }
125 
126 // +--------------------------------------------------------------------+
127 
128 void
130 {
132 
133  for (int i = 0; i < 4; i++) {
134  Button* b = axis_button[i];
135  if (b) {
136  int map = Joystick::GetAxisMap(i) - KEY_JOY_AXIS_X;
137  int inv = Joystick::GetAxisInv(i);
138 
139  if (map >= 0 && map < 8) {
140  b->SetText(Game::GetText(joy_axis_names[map]));
141  map_axis[i] = map;
142  }
143  else {
144  b->SetText(Game::GetText("JoyDlg.unmapped"));
145  }
146 
147  b->SetButtonState(0);
148 
149  invert_checkbox[i]->SetButtonState(inv ? 1 : 0);
150  }
151  }
152 
153  SetFocus();
154 }
155 
156 // +--------------------------------------------------------------------+
157 
158 void
160 {
161  for (int i = 0; i < 4; i++) {
162  int map = map_axis[i];
163  Text name = Game::GetText("JoyDlg.unmapped");
164  Button* b = axis_button[i];
165 
166  if (map >= 0 && map < 8)
167  name = Game::GetText(joy_axis_names[map]);
168 
169  if (b) {
170  if (b == event->window) {
171  if (selected_axis == i) {
172  b->SetText(name);
173  b->SetButtonState(0);
174  selected_axis = -1;
175  }
176  else {
177  b->SetText(Game::GetText("JoyDlg.select"));
178  b->SetButtonState(1);
179  selected_axis = i;
180  }
181  }
182  else {
183  b->SetText(name);
184  b->SetButtonState(0);
185  }
186  }
187  }
188 
189  for (int i = 0; i < 8; i++) {
190  samples[i] = 10000000;
191  }
192 }
193 
194 // +--------------------------------------------------------------------+
195 
196 void
198 {
200 
201  if (stars) {
202  KeyMap& keymap = stars->GetKeyMap();
203 
204  keymap.Bind(KEY_AXIS_YAW, map_axis[0]+KEY_JOY_AXIS_X, 0);
205  keymap.Bind(KEY_AXIS_PITCH, map_axis[1]+KEY_JOY_AXIS_X, 0);
206  keymap.Bind(KEY_AXIS_ROLL, map_axis[2]+KEY_JOY_AXIS_X, 0);
207  keymap.Bind(KEY_AXIS_THROTTLE, map_axis[3]+KEY_JOY_AXIS_X, 0);
208 
209  keymap.Bind(KEY_AXIS_YAW_INVERT, invert_checkbox[0]->GetButtonState(), 0);
210  keymap.Bind(KEY_AXIS_PITCH_INVERT, invert_checkbox[1]->GetButtonState(), 0);
211  keymap.Bind(KEY_AXIS_ROLL_INVERT, invert_checkbox[2]->GetButtonState(), 0);
212  keymap.Bind(KEY_AXIS_THROTTLE_INVERT, invert_checkbox[3]->GetButtonState(), 0);
213 
214  keymap.SaveKeyMap("key.cfg", 256);
215 
216  stars->MapKeys();
217  }
218 
219  if (manager)
220  manager->ShowCtlDlg();
221 }
222 
223 void
225 {
226  if (manager)
227  manager->ShowCtlDlg();
228 }
229 
230 // +--------------------------------------------------------------------+