-
Notifications
You must be signed in to change notification settings - Fork 1
/
hello.cpp
192 lines (168 loc) · 6.03 KB
/
hello.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#pragma warning(disable:4996)
#include "XPLMDisplay.h"
#include "XPLMGraphics.h"
/*
* Global Variables. We will store our single window globally. We also record
* whether the mouse is down from our mouse handler. The drawing handler looks
* at this information and draws the appropriate display.
*
*/
XPLMWindowID gWindow = NULL;
int gClicked = 0;
void MyDrawWindowCallback(
XPLMWindowID inWindowID,
void * inRefcon);
void MyHandleKeyCallback(
XPLMWindowID inWindowID,
char inKey,
XPLMKeyFlags inFlags,
char inVirtualKey,
void * inRefcon,
int losingFocus);
int MyHandleMouseClickCallback(
XPLMWindowID inWindowID,
int x,
int y,
XPLMMouseStatus inMouse,
void * inRefcon);
/*
* XPluginStart
*
* Our start routine registers our window and does any other initialization we
* must do.
*
*/
PLUGIN_API int XPluginStart(
char * outName,
char * outSig,
char * outDesc)
{
/* First we must fill in the passed in buffers to describe our
* plugin to the plugin-system. */
strcpy(outName, "HelloWorld");
strcpy(outSig, "xplanesdk.examples.helloworld");
strcpy(outDesc, "A plugin that makes a window.");
/* Now we create a window. We pass in a rectangle in left, top,
* right, bottom screen coordinates. We pass in three callbacks. */
gWindow = XPLMCreateWindow(
50, 600, 300, 200, /* Area of the window. */
1, /* Start visible. */
MyDrawWindowCallback, /* Callbacks */
MyHandleKeyCallback,
MyHandleMouseClickCallback,
NULL); /* Refcon - not used. */
/* We must return 1 to indicate successful initialization, otherwise we
* will not be called back again. */
return 1;
}
/*
* XPluginStop
*
* Our cleanup routine deallocates our window.
*
*/
PLUGIN_API void XPluginStop(void)
{
XPLMDestroyWindow(gWindow);
}
/*
* XPluginDisable
*
* We do not need to do anything when we are disabled, but we must provide the handler.
*
*/
PLUGIN_API void XPluginDisable(void)
{
}
/*
* XPluginEnable.
*
* We don't do any enable-specific initialization, but we must return 1 to indicate
* that we may be enabled at this time.
*
*/
PLUGIN_API int XPluginEnable(void)
{
return 1;
}
/*
* XPluginReceiveMessage
*
* We don't have to do anything in our receive message handler, but we must provide one.
*
*/
PLUGIN_API void XPluginReceiveMessage(
XPLMPluginID inFromWho,
long inMessage,
void * inParam)
{
}
/*
* MyDrawingWindowCallback
*
* This callback does the work of drawing our window once per sim cycle each time
* it is needed. It dynamically changes the text depending on the saved mouse
* status. Note that we don't have to tell X-Plane to redraw us when our text
* changes; we are redrawn by the sim continuously.
*
*/
void MyDrawWindowCallback(
XPLMWindowID inWindowID,
void * inRefcon)
{
int left, top, right, bottom;
float color[] = { 1.0, 1.0, 1.0 }; /* RGB White */
/* First we get the location of the window passed in to us. */
XPLMGetWindowGeometry(inWindowID, &left, &top, &right, &bottom);
/* We now use an XPLMGraphics routine to draw a translucent dark
* rectangle that is our window's shape. */
XPLMDrawTranslucentDarkBox(left, top, right, bottom);
/* Finally we draw the text into the window, also using XPLMGraphics
* routines. The NULL indicates no word wrapping. */
XPLMDrawString(color, left + 5, top - 20,
(char*)(gClicked ? "I'm a plugin" : "Hello world"), NULL, xplmFont_Basic);
}
/*
* MyHandleKeyCallback
*
* Our key handling callback does nothing in this plugin. This is ok;
* we simply don't use keyboard input.
*
*/
void MyHandleKeyCallback(
XPLMWindowID inWindowID,
char inKey,
XPLMKeyFlags inFlags,
char inVirtualKey,
void * inRefcon,
int losingFocus)
{
}
/*
* MyHandleMouseClickCallback
*
* Our mouse click callback toggles the status of our mouse variable
* as the mouse is clicked. We then update our text on the next sim
* cycle.
*
*/
int MyHandleMouseClickCallback(
XPLMWindowID inWindowID,
int x,
int y,
XPLMMouseStatus inMouse,
void * inRefcon)
{
/* If we get a down or up, toggle our status click. We will
* never get a down without an up if we accept the down. */
if ((inMouse == xplm_MouseDown) || (inMouse == xplm_MouseUp))
gClicked = 1 - gClicked;
/* Returning 1 tells X-Plane that we 'accepted' the click; otherwise
* it would be passed to the next window behind us. If we accept
* the click we get mouse moved and mouse up callbacks, if we don't
* we do not get any more callbacks. It is worth noting that we
* will receive mouse moved and mouse up even if the mouse is dragged
* out of our window's box as long as the click started in our window's
* box. */
return 1;
}