forked from vpinball/vpinball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Debugger.cpp
276 lines (238 loc) · 10.3 KB
/
Debugger.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "stdafx.h"
#include "resource.h"
#include <windowsx.h>
#include "Debugger.h"
#include <string>
#define RECOMPUTEBUTTONCHECK WM_USER+100
#define RESIZE_FROM_EXPAND WM_USER+101
DebuggerDialog::DebuggerDialog() : CDialog(IDD_DEBUGGER)
{
}
BOOL DebuggerDialog::IsSubDialogMessage(MSG& msg) const
{
return IsDialogMessage(msg);
}
BOOL DebuggerDialog::OnInitDialog()
{
AttachItem(IDC_PLAY, m_playButton);
AttachItem(IDC_PAUSE, m_pauseButton);
AttachItem(IDC_STEP, m_stepButton);
AttachItem(IDC_STEPAMOUNT, m_stepAmountEdit);
m_hThrowBallsInPlayerCheck = ::GetDlgItem(GetHwnd(), IDC_BALL_THROWING);
m_hBallControlCheck = ::GetDlgItem(GetHwnd(), IDC_BALL_CONTROL);
AttachItem(IDC_THROW_BALL_SIZE_EDIT2, m_ballSizeEdit);
AttachItem(IDC_THROW_BALL_MASS_EDIT2, m_ballMassEdit);
AttachItem(IDC_EDITSIZE, m_notesEdit);
HANDLE hIcon = ::LoadImage(GetModuleHandle(nullptr), MAKEINTRESOURCE(IDI_PLAY), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
m_playButton.SetIcon((HICON)hIcon);
hIcon = ::LoadImage(GetModuleHandle(nullptr), MAKEINTRESOURCE(IDI_PAUSE), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
m_pauseButton.SetIcon((HICON)hIcon);
hIcon = ::LoadImage(GetModuleHandle(nullptr), MAKEINTRESOURCE(IDI_STEP), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
m_stepButton.SetIcon((HICON)hIcon);
SendMessage(RECOMPUTEBUTTONCHECK, 0, 0);
RECT rcEditSize;
::GetWindowRect(GetDlgItem(IDC_EDITSIZE), &rcEditSize);
::ScreenToClient(GetHwnd(), (POINT*)&rcEditSize);
::ScreenToClient(GetHwnd(), &((POINT*)&rcEditSize)[1]);
g_pplayer->m_hwndDebugOutput = CreateWindowEx(0, "Scintilla", "",
WS_CHILD | ES_NOHIDESEL | WS_VISIBLE | ES_SUNKEN | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | ES_WANTRETURN | WS_BORDER,
rcEditSize.left, rcEditSize.top, rcEditSize.right - rcEditSize.left, rcEditSize.bottom - rcEditSize.top, GetHwnd(), nullptr, g_pvp->theInstance, 0);
SendMessage(g_pplayer->m_hwndDebugOutput, SCI_STYLESETSIZE, 32, 10);
SendMessage(g_pplayer->m_hwndDebugOutput, SCI_STYLESETFONT, 32, (LPARAM)"Courier");
SendMessage(g_pplayer->m_hwndDebugOutput, SCI_SETMARGINWIDTHN, 1, 0);
SendMessage(g_pplayer->m_hwndDebugOutput, SCI_SETTABWIDTH, 4, 0);
SendMessage(m_hThrowBallsInPlayerCheck, BM_SETCHECK, g_pplayer->m_throwBalls ? BST_CHECKED : BST_UNCHECKED, 0);
SendMessage(m_hBallControlCheck, BM_SETCHECK, g_pplayer->m_ballControl ? BST_CHECKED : BST_UNCHECKED, 0);
m_ballSizeEdit.SetWindowText(std::to_string(g_pplayer->m_debugBallSize).c_str());
m_ballMassEdit.SetWindowText(f2sz(g_pplayer->m_debugBallMass).c_str());
m_resizer.Initialize(*this, GetWindowRect());
m_resizer.AddChild(m_notesEdit.GetHwnd(), CResizer::bottomright, RD_STRETCH_HEIGHT | RD_STRETCH_WIDTH);
m_resizer.AddChild(g_pplayer->m_hwndDebugOutput, CResizer::bottomright, RD_STRETCH_WIDTH | RD_STRETCH_HEIGHT);
m_resizer.AddChild(GetDlgItem(IDC_GUIDE1).GetHwnd(), CResizer::topleft, 0);
m_resizer.AddChild(GetDlgItem(IDC_GUIDE2).GetHwnd(), CResizer::bottomright, 0);
LoadPosition();
return TRUE;
}
BOOL DebuggerDialog::OnCommand(WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (LOWORD(wParam))
{
case IDC_PLAY:
{
#ifdef STEPPING
g_pplayer->m_pauseTimeTarget = 0;
#endif
g_pplayer->m_userDebugPaused = false;
g_pplayer->RecomputePseudoPauseState();
SendMessage(RECOMPUTEBUTTONCHECK, 0, 0);
return TRUE;
}
case IDC_PAUSE:
{
#ifdef STEPPING
g_pplayer->m_pauseTimeTarget = 0;
#endif
g_pplayer->m_userDebugPaused = true;
g_pplayer->RecomputePseudoPauseState();
SendMessage(RECOMPUTEBUTTONCHECK, 0, 0);
return TRUE;
}
case IDC_STEP:
{
#ifdef STEPPING
const int ms = GetDlgItemInt(IDC_STEPAMOUNT, FALSE);
g_pplayer->m_pauseTimeTarget = g_pplayer->m_time_msec + ms;
#endif
g_pplayer->m_userDebugPaused = false;
g_pplayer->RecomputePseudoPauseState();
SendMessage(RECOMPUTEBUTTONCHECK, 0, 0);
return TRUE;
}
case IDC_EXPAND:
{
SendMessage(RESIZE_FROM_EXPAND, 0, 0);
return TRUE;
}
case IDC_BALL_THROWING:
{
const size_t checked = SendMessage(m_hThrowBallsInPlayerCheck, BM_GETCHECK, 0, 0);
g_pplayer->m_throwBalls = !!checked;
return TRUE;
}
case IDC_BALL_CONTROL:
{
const size_t checked = SendMessage(m_hBallControlCheck, BM_GETCHECK, 0, 0);
g_pplayer->m_ballControl = !!checked;
return TRUE;
}
}
return FALSE;
}
void DebuggerDialog::OnClose()
{
#ifdef STEPPING
g_pplayer->m_pauseTimeTarget = 0;
#endif
g_pplayer->m_userDebugPaused = false;
g_pplayer->RecomputePseudoPauseState();
g_pplayer->m_debugBallSize = GetDlgItemInt(IDC_THROW_BALL_SIZE_EDIT2, FALSE);
const float fv = sz2f(GetDlgItemText(IDC_THROW_BALL_MASS_EDIT2).c_str());
g_pplayer->m_debugBallMass = fv;
g_pplayer->m_debugMode = false;
g_pplayer->m_showDebugger = false;
ShowWindow(SW_HIDE);
if ((g_pplayer->m_fullScreen || (g_pplayer->m_wnd_width == g_pplayer->m_screenwidth && g_pplayer->m_wnd_height == g_pplayer->m_screenheight)) // detect windowed fullscreen
&& !(g_pplayer->m_throwBalls || g_pplayer->m_ballControl || g_pplayer->m_liveUI->IsOpened()))
{
while (ShowCursor(TRUE)<0) ;
while (ShowCursor(FALSE)>=0) ;
}
SavePosition();
}
void DebuggerDialog::LoadPosition()
{
const CRect rcMain = GetParent().GetWindowRect();
const CRect rcDialog = GetWindowRect();
const int x = g_pvp->m_settings.LoadValueWithDefault(Settings::Editor, "DebuggerPosX"s, (int) ((rcMain.right + rcMain.left) / 2 - (rcDialog.right - rcDialog.left) / 2));
const int y = g_pvp->m_settings.LoadValueWithDefault(Settings::Editor, "DebuggerPosY"s, (int) ((rcMain.bottom + rcMain.top) / 2 - (rcDialog.bottom - rcDialog.top) / 2));
const int w = g_pvp->m_settings.LoadValueWithDefault(Settings::Editor, "DebuggerWidth"s, 1000);
const int h = g_pvp->m_settings.LoadValueWithDefault(Settings::Editor, "DebuggerHeight"s, 800);
POINT p { x, y };
if (MonitorFromPoint(p, MONITOR_DEFAULTTONULL) != NULL) // Do not apply if point is offscreen
SetWindowPos(nullptr, x, y, w, h, SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOACTIVATE);
}
void DebuggerDialog::SavePosition()
{
const CRect rect = GetWindowRect();
g_pvp->m_settings.SaveValue(Settings::Editor, "DebuggerPosX"s, (int)rect.left);
g_pvp->m_settings.SaveValue(Settings::Editor, "DebuggerPosY"s, (int)rect.top);
const int w = rect.right - rect.left;
g_pvp->m_settings.SaveValue(Settings::Editor, "DebuggerWidth"s, w);
const int h = rect.bottom - rect.top;
g_pvp->m_settings.SaveValue(Settings::Editor, "DebuggerHeight"s, h);
}
LRESULT DebuggerDialog::OnNotify(WPARAM wparam, LPARAM lparam)
{
const NMHDR* const pnmh = (LPNMHDR)lparam;
//HWND hwndRE = pnmh->hwndFrom;
const int code = pnmh->code;
switch (code)
{
case SCN_CHARADDED:
{
const SCNotification* const pscnmh = (SCNotification*)lparam;
if (pscnmh->ch == '\n') // execute code
{
SendMessage(pnmh->hwndFrom, SCI_DELETEBACK, 0, 0);
const size_t curpos = SendMessage(pnmh->hwndFrom, SCI_GETCURRENTPOS, 0, 0);
const size_t line = SendMessage(pnmh->hwndFrom, SCI_LINEFROMPOSITION, curpos, 0);
const size_t lineStart = SendMessage(pnmh->hwndFrom, SCI_POSITIONFROMLINE, line, 0);
const size_t lineEnd = SendMessage(pnmh->hwndFrom, SCI_GETLINEENDPOSITION, line, 0);
char* const szText = new char[lineEnd - lineStart + 1];
Sci_TextRange tr;
tr.chrg.cpMin = (Sci_PositionCR)lineStart;
tr.chrg.cpMax = (Sci_PositionCR)lineEnd;
tr.lpstrText = szText;
SendMessage(pnmh->hwndFrom, SCI_GETTEXTRANGE, 0, (LPARAM)&tr);
const size_t maxlines = SendMessage(pnmh->hwndFrom, SCI_GETLINECOUNT, 0, 0);
if (maxlines == line + 1)
{
// need to add a new line to the end
SendMessage(pnmh->hwndFrom, SCI_DOCUMENTEND, 0, 0);
SendMessage(pnmh->hwndFrom, SCI_ADDTEXT, 1, (LPARAM)"\n");
}
else
{
const size_t pos = SendMessage(pnmh->hwndFrom, SCI_POSITIONFROMLINE, line + 1, 0);
SendMessage(pnmh->hwndFrom, SCI_SETCURRENTPOS, pos, 0);
}
g_pplayer->m_ptable->m_pcv->EvaluateScriptStatement(szText);
delete[] szText;
}
break;
}
}
return CDialog::OnNotify(wparam, lparam);
}
INT_PTR DebuggerDialog::DialogProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
m_resizer.HandleMessage(uMsg, wParam, lParam);
switch (uMsg)
{
case RECOMPUTEBUTTONCHECK:
{
int PlayDown = BST_UNCHECKED;
int PauseDown = BST_UNCHECKED;
int StepDown = BST_UNCHECKED;
if (g_pplayer->m_userDebugPaused)
{
PauseDown = BST_CHECKED;
}
#ifdef STEPPING
else if (g_pplayer->m_pauseTimeTarget > 0)
{
StepDown = BST_CHECKED;
}
#endif
else
{
PlayDown = BST_CHECKED;
}
SendDlgItemMessage(IDC_PLAY, BM_SETCHECK, PlayDown, 0);
SendDlgItemMessage(IDC_PAUSE, BM_SETCHECK, PauseDown, 0);
SendDlgItemMessage(IDC_STEP, BM_SETCHECK, StepDown, 0);
SendMessage(TB_CHECKBUTTON, IDC_PLAY, PlayDown);
SendMessage(TB_CHECKBUTTON, IDC_PAUSE, PauseDown);
SendMessage(TB_CHECKBUTTON, IDC_STEP, StepDown);
return TRUE;
}
case WM_SIZE:
{
const CRect rc = m_notesEdit.GetClientRect();
::SetWindowPos(g_pplayer->m_hwndDebugOutput, nullptr,
0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
}
}
return DialogProcDefault(uMsg, wParam, lParam);
}