TORCS  1.3.9
The Open Racing Car Simulator
mouseconfig.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  file : mouseconfig.cpp
4  created : Thu Mar 13 21:27:03 CET 2003
5  copyright : (C) 2003 by Eric EspiĆ©
6  email : eric.espie@torcs.org
7  version : $Id$
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
26 #include <stdio.h>
27 #include <stdlib.h>
28 
29 #include <tgfclient.h>
30 #include <track.h>
31 #include <robot.h>
32 #include <playerpref.h>
33 #include <plib/js.h>
34 
35 #include "controlconfig.h"
36 #include "mouseconfig.h"
37 
38 static void *scrHandle2 = NULL;
39 
41 
42 // TODO: refactor, this is horrible, depends on the order in controlconfig.cpp, static tCmdInfo Cmd[]
43 #define CMD_OFFSET 7
44 
45 /*
46  * Mouse calibration
47  */
48 static int InstId;
49 static int CalState;
50 static int scrw, scrh;
51 
52 static tCmdInfo *Cmd;
53 static int maxCmd;
54 
55 static const char *Instructions[] = {
56  "Move Mouse for maximum left steer then press a button",
57  "Move Mouse for maximum right steer then press a button",
58  "Move Mouse for full throttle then press a button",
59  "Move Mouse for full brake then press a button",
60  "Calibration terminated",
61  "Calibration failed"
62 };
63 
64 static void Idle2(void);
65 
66 static int
68 {
69  int i;
70 
71  for (i = CalState; i < 4; i++) {
72  if (Cmd[CMD_OFFSET + i].ref.type == GFCTRL_TYPE_MOUSE_AXIS) {
73  return i;
74  }
75  }
76  return i;
77 }
78 
79 
80 static void
82 {
83  float axv;
84 
85  switch (CalState) {
86  case 0:
87  case 1:
90  if (fabs(axv) < 0.01) {
91  return; /* ignore no move input */
92  }
93  Cmd[CMD_OFFSET + CalState].max = axv;
94  Cmd[CMD_OFFSET + CalState].pow = 1.0 / axv;
95  break;
96 
97  case 2:
98  case 3:
101  if (fabs(axv) < 0.01) {
102  return; /* ignore no move input */
103  }
104  Cmd[CMD_OFFSET + CalState].max = axv;
105  Cmd[CMD_OFFSET + CalState].pow = 1.0 / axv;
106  break;
107  }
108 
109  CalState++;
110  CalState = GetNextAxis();
112  if (CalState < 4) {
113  glutIdleFunc(Idle2);
114  } else {
115  glutIdleFunc(GfuiIdle);
116  }
117 }
118 
119 static void
120 Idle2(void)
121 {
122  int i;
123 
125 
126  /* Check for a mouse button pressed */
127  for (i = 0; i < 3; i++) {
128  if (mouseInfo.edgedn[i]) {
130  return;
131  }
132  }
133 }
134 
135 static void
137 {
138  /* Get the center mouse position */
139  memset(&mouseInfo, 0, sizeof(mouseInfo));
142  glutIdleFunc(Idle2);
143 }
144 
145 static void
146 onActivate2(void * /* dummy */)
147 {
148  int dummy;
149 
150  GfScrGetSize(&scrw, &scrh, &dummy, &dummy);
151  CalState = 0;
152  GetNextAxis();
154  if (CalState < 4) {
155  glutIdleFunc(IdleMouseInit);
157  }
158 }
159 
160 void *
161 MouseCalMenuInit(void *prevMenu, tCmdInfo *cmd, int maxcmd)
162 {
163  //int x, y, dy;
164 
165  Cmd = cmd;
166  maxCmd = maxcmd;
167 
168  if (scrHandle2) {
169  return scrHandle2;
170  }
171 
172  scrHandle2 = GfuiScreenCreateEx(NULL, NULL, onActivate2, NULL, NULL, 1);
173  GfuiTitleCreate(scrHandle2, "Mouse Calibration", 0);
175 
176  GfuiScreenAddBgImg(scrHandle2, "data/img/splash-mousecal.png");
177 
178  //x = 128;
179  //y = 300;
180  //dy = 50;
181 
183 
185  prevMenu, GfuiScreenActivate, NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
186 
188  NULL, onActivate2, NULL, (tfuiCallback)NULL, (tfuiCallback)NULL);
189 
190  return scrHandle2;
191 }
int GfctrlMouseGetCurrent(tCtrlMouseInfo *mouseInfo)
Get the mouse current values.
Definition: control.cpp:427
#define GFUI_MOUSE_UP
Definition: tgfclient.h:80
static int CalState
Definition: mouseconfig.cpp:49
static int scrh
Definition: mouseconfig.cpp:50
int GfuiButtonCreate(void *scr, const char *text, int font, int x, int y, int width, int align, int mouse, void *userDataOnPush, tfuiCallback onPush, void *userDataOnFocus, tfuiCallback onFocus, tfuiCallback onFocusLost)
Add a button to a screen.
Definition: guibutton.cpp:248
static int InstId
Definition: mouseconfig.cpp:48
int edgedn[3]
Button transition from up to down.
Definition: tgfclient.h:297
static int maxCmd
Definition: mouseconfig.cpp:53
Robot Module Interface Definition.
int GfuiLabelCreate(void *scr, const char *text, int font, int x, int y, int align, int maxlen)
Add a label to a screen.
Definition: guilabel.cpp:142
#define CMD_OFFSET
Definition: mouseconfig.cpp:43
void GfuiScreenAddBgImg(void *scr, const char *filename)
Add an image background to a screen.
Definition: gui.cpp:961
void GfuiIdle(void)
Idle function for the GUI to be called during Idle loop of glut.
Definition: gui.cpp:116
static void IdleMouseInit(void)
The Gaming Framework API (client part).
static tCmdInfo * Cmd
Definition: mouseconfig.cpp:52
#define GFUI_FONT_LARGE
Definition: tgfclient.h:168
void * GfuiScreenCreateEx(float *bgColor, void *userDataOnActivate, tfuiCallback onActivate, void *userDataOnDeactivate, tfuiCallback onDeactivate, int mouseAllowed)
Create a screen.
Definition: gui.cpp:578
static int GetNextAxis(void)
Definition: mouseconfig.cpp:67
void GfuiLabelSetText(void *scr, int id, const char *text)
Change the text of a label.
Definition: guilabel.cpp:212
static int scrw
Definition: mouseconfig.cpp:50
static void MouseCalAutomaton(void)
Definition: mouseconfig.cpp:81
void GfuiMenuDefaultKeysAdd(void *scr)
Add the default menu keyboard callback to a screen.
Definition: guimenu.cpp:55
void * MouseCalMenuInit(void *prevMenu, tCmdInfo *cmd, int maxcmd)
float max
Definition: controlconfig.h:40
void(* tfuiCallback)(void *)
Definition: tgfclient.h:105
void GfctrlMouseInitCenter(void)
Get the reference position.
Definition: control.cpp:489
void GfuiScreenActivate(void *screen)
Activate a screen and make it current.
Definition: gui.cpp:467
tCtrlRef ref
Definition: controlconfig.h:35
#define GFUI_ALIGN_HC_VB
Definition: tgfclient.h:72
void GfScrGetSize(int *scrw, int *scrh, int *vieww, int *viewh)
Get the screen and viewport sizes.
Definition: screen.cpp:471
int GfuiTitleCreate(void *scr, const char *text, int maxlen)
Add a Title to the screen.
Definition: guilabel.cpp:170
void GfctrlMouseCenter(void)
Recentre the mouse on the screen.
Definition: control.cpp:476
Mouse information structure.
Definition: tgfclient.h:294
static tCtrlMouseInfo mouseInfo
Definition: mouseconfig.cpp:40
#define GFUI_FONT_MEDIUM
Definition: tgfclient.h:169
static void onActivate2(void *)
static void * scrHandle2
Definition: mouseconfig.cpp:38
static void Idle2(void)
float ax[4]
mouse axis position (mouse considered as a joystick)
Definition: tgfclient.h:299
int index
Definition: tgfclient.h:266
Track Structure and Track Loader Module Definition.
static const char * Instructions[]
Definition: mouseconfig.cpp:55
float pow
Definition: controlconfig.h:42