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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "backends/imgui/IconsMaterialSymbols.h"
#include "director/director.h"
#include "director/lingo/lingodec/context.h"
#include "director/lingo/lingodec/script.h"
#include "director/cast.h"
#include "director/castmember/bitmap.h"
#include "director/debugger.h"
#include "director/movie.h"
#include "director/picture.h"
#include "director/debugger/debugtools.h"
#include "director/debugger/dt-internal.h"
namespace Director {
namespace DT {
ImGuiState *_state = nullptr;
const LingoDec::Handler *getHandler(const Cast *cast, CastMemberID id, const Common::String &handlerId) {
if (!cast)
return nullptr;
const ScriptContext *ctx = cast->_lingoArchive->findScriptContext(id.member);
if (!ctx || !ctx->_functionHandlers.contains(handlerId))
return nullptr;
// for the moment it's happening with Director version < 4
if (!cast->_lingodec)
return nullptr;
for (auto p : cast->_lingodec->scripts) {
if ((p.second->castID & 0xFFFF) != id.member)
continue;
;
for (const LingoDec::Handler &handler : p.second->handlers) {
if (handler.name == handlerId) {
return &handler;
}
}
}
return nullptr;
}
const LingoDec::Handler *getHandler(CastMemberID id, const Common::String &handlerId) {
const Director::Movie *movie = g_director->getCurrentMovie();
for (const auto it : *movie->getCasts()) {
const Cast *cast = it._value;
const LingoDec::Handler *handler = getHandler(cast, id, handlerId);
if (handler)
return handler;
}
return getHandler(movie->getSharedCast(), id, handlerId);
}
ImGuiScript toImGuiScript(ScriptType scriptType, CastMemberID id, const Common::String &handlerId) {
ImGuiScript result;
result.id = id;
result.handlerId = handlerId;
result.type = scriptType;
const LingoDec::Handler *handler = getHandler(id, handlerId);
if (!handler) {
const ScriptContext *ctx;
if (id.castLib == SHARED_CAST_LIB) {
ctx = g_director->getCurrentMovie()->getSharedCast()->_lingoArchive->getScriptContext(scriptType, id.member);
} else {
ctx = g_director->getCurrentMovie()->getScriptContext(scriptType, id);
}
if (!ctx) return result;
result.oldAst = ctx->_assemblyAST;
return result;
}
result.bytecodeArray = handler->bytecodeArray;
result.root = handler->ast.root;
result.isGenericEvent = handler->isGenericEvent;
result.argumentNames = handler->argumentNames;
result.propertyNames = handler->script->propertyNames;
result.globalNames = handler->globalNames;
LingoDec::Script *script = handler->script;
if (!script)
return result;
result.isMethod = script->isFactory();
return result;
}
Director::Breakpoint *getBreakpoint(const Common::String &handlerName, uint16 scriptId, uint pc) {
auto &bps = g_lingo->getBreakpoints();
for (uint i = 0; i < bps.size(); i++) {
if (bps[i].type == kBreakpointFunction && bps[i].scriptId == scriptId && bps[i].funcName == handlerName && bps[i].funcOffset == pc) {
return &bps[i];
}
}
return nullptr;
}
ImGuiImage getImageID(CastMember *castMember) {
if (castMember->_type != CastType::kCastBitmap)
return {};
BitmapCastMember *bmpMember = (BitmapCastMember *)castMember;
Common::Rect bbox(bmpMember->getBbox());
Graphics::Surface *bmp = bmpMember->getMatte(bbox);
if (!bmp)
return {};
if (_state->_cast._textures.contains(bmp))
return _state->_cast._textures[bmp];
Picture *pic = bmpMember->_picture;
if (!pic)
return {};
ImTextureID textureID = g_system->getImGuiTexture(pic->_surface, pic->_palette, pic->_paletteColors);
_state->_cast._textures[bmp] = {textureID, pic->_surface.w, pic->_surface.h};
return _state->_cast._textures[bmp];
}
static void setToolTipImage(const ImGuiImage &image, const char *name) {
if (ImGui::IsItemHovered() && ImGui::BeginTooltip()) {
ImGui::Text("%s", name);
ImGui::Image(image.id, ImVec2(image.width, image.height), ImVec2(0, 0), ImVec2(1, 1), ImVec4(1, 1, 1, 1), ImVec4(1, 1, 1, 1));
ImGui::EndTooltip();
}
}
void showImage(const ImGuiImage &image, const char *name, float thumbnailSize) {
ImVec2 size;
if (image.width > image.height) {
size = {thumbnailSize - 2, (thumbnailSize - 2) * image.height / image.width};
} else {
size = {(thumbnailSize - 2) * image.width / image.height, thumbnailSize - 2};
}
ImGui::BeginGroup();
ImVec2 screenPos = ImGui::GetCursorScreenPos();
ImGui::GetWindowDrawList()->AddRect(screenPos, screenPos + ImVec2(thumbnailSize, thumbnailSize), 0xFFFFFFFF);
ImVec2 pos = ImGui::GetCursorPos();
ImVec2 imgPos = pos + ImVec2(1 + (thumbnailSize - 2 - size.x) * 0.5f, 1 + (thumbnailSize - 2 - size.y) * 0.5f);
ImGui::SetCursorPos(imgPos);
ImGui::Image(image.id, size);
ImGui::EndGroup();
setToolTipImage(image, name);
}
void displayVariable(const Common::String &name, bool changed) {
ImU32 var_color = ImGui::GetColorU32(_state->_colors._var_ref);
ImU32 color;
color = ImGui::GetColorU32(_state->_colors._bp_color_disabled);
if (_state->_variables.contains(name))
color = ImGui::GetColorU32(_state->_colors._bp_color_enabled);
ImDrawList *dl = ImGui::GetWindowDrawList();
ImVec2 pos = ImGui::GetCursorScreenPos();
ImVec2 eyeSize = ImGui::CalcTextSize(ICON_MS_VISIBILITY " ");
ImVec2 textSize = ImGui::CalcTextSize(name.c_str());
ImGui::InvisibleButton("Line", ImVec2(textSize.x + eyeSize.x, textSize.y));
if (ImGui::IsItemClicked(0)) {
if (color == ImGui::GetColorU32(_state->_colors._bp_color_enabled)) {
_state->_variables.erase(name);
color = ImGui::GetColorU32(_state->_colors._bp_color_disabled);
} else {
_state->_variables[name] = true;
color = ImGui::GetColorU32(_state->_colors._bp_color_enabled);
}
}
if (changed)
var_color = ImGui::GetColorU32(_state->_colors._var_ref_changed);
if (color == ImGui::GetColorU32(_state->_colors._bp_color_disabled) && ImGui::IsItemHovered()) {
color = ImGui::GetColorU32(_state->_colors._bp_color_hover);
}
dl->AddText(pos, color, ICON_MS_VISIBILITY " ");
dl->AddText(ImVec2(pos.x + eyeSize.x, pos.y), var_color, name.c_str());
}
ImVec4 convertColor(uint32 color) {
if (g_director->_colorDepth <= 8) {
float r = g_director->getPalette()[color * 3 + 0] * 1.0 / 255.0;
float g = g_director->getPalette()[color * 3 + 1] * 1.0 / 255.0;
float b = g_director->getPalette()[color * 3 + 2] * 1.0 / 255.0;
return ImVec4(r, g, b, 1.0);
}
return ImGui::ColorConvertU32ToFloat4(color);
}
static void addScriptCastToDisplay(CastMemberID &id) {
_state->_scriptCasts.remove(id);
_state->_scriptCasts.push_back(id);
}
void setScriptToDisplay(const ImGuiScript &script) {
uint index = _state->_functions._scripts.size();
if (index && _state->_functions._scripts[index - 1] == script) {
_state->_functions._showScript = true;
return;
}
_state->_functions._scripts.push_back(script);
_state->_functions._current = index;
_state->_functions._showScript = true;
}
void displayScriptRef(CastMemberID &scriptId) {
if (scriptId.member) {
ImGui::TextColored(_state->_colors._script_ref, "%d", scriptId.member);
ImGui::SetItemTooltip(scriptId.asString().c_str());
if (ImGui::IsItemClicked(0))
addScriptCastToDisplay(scriptId);
} else {
ImGui::Selectable(" ");
}
}
static void showSettings() {
if (!_state->_w.settings)
return;
ImGui::SetNextWindowPos(ImVec2(20, 20), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(480, 240), ImGuiCond_FirstUseEver);
if (ImGui::Begin("Settings", &_state->_w.settings)) {
ImGui::ColorEdit4("Breakpoint disabled", &_state->_colors._bp_color_disabled.x);
ImGui::ColorEdit4("Breakpoint enabled", &_state->_colors._bp_color_enabled.x);
ImGui::ColorEdit4("Breakpoint hover", &_state->_colors._bp_color_hover.x);
ImGui::SeparatorText("Lingo syntax");
ImGui::ColorEdit4("Line", &_state->_colors._line_color.x);
ImGui::ColorEdit4("Call", &_state->_colors._call_color.x);
ImGui::ColorEdit4("Builtin", &_state->_colors._builtin_color.x);
ImGui::ColorEdit4("Variable", &_state->_colors._var_color.x);
ImGui::ColorEdit4("Literal", &_state->_colors._literal_color.x);
ImGui::ColorEdit4("Comment", &_state->_colors._comment_color.x);
ImGui::ColorEdit4("Type", &_state->_colors._type_color.x);
ImGui::ColorEdit4("Keyword", &_state->_colors._keyword_color.x);
ImGui::ColorEdit4("The entity", &_state->_colors._the_color.x);
ImGui::SeparatorText("References");
ImGui::ColorEdit4("Script", &_state->_colors._script_ref.x);
ImGui::ColorEdit4("Variable", &_state->_colors._var_ref.x);
ImGui::ColorEdit4("Variable changed", &_state->_colors._var_ref_changed.x);
_state->_logger->drawColorOptions();
}
ImGui::End();
}
void onLog(LogMessageType::Type type, int level, uint32 debugChannels, const char *message) {
switch (type) {
case LogMessageType::kError:
_state->_logger->addLog("[error]%s", message);
break;
case LogMessageType::kWarning:
_state->_logger->addLog("[warn]%s", message);
break;
case LogMessageType::kInfo:
_state->_logger->addLog("%s", message);
break;
case LogMessageType::kDebug:
_state->_logger->addLog("[debug]%s", message);
break;
}
}
void onImGuiInit() {
ImGuiIO &io = ImGui::GetIO();
io.Fonts->AddFontDefault();
ImFontConfig icons_config;
icons_config.MergeMode = true;
icons_config.PixelSnapH = false;
icons_config.OversampleH = 3;
icons_config.OversampleV = 3;
icons_config.GlyphOffset = {0, 4};
static const ImWchar icons_ranges[] = {ICON_MIN_MS, ICON_MAX_MS, 0};
ImGui::addTTFFontFromArchive("MaterialSymbolsSharp.ttf", 16.f, &icons_config, icons_ranges);
_state = new ImGuiState();
_state->_tinyFont = ImGui::addTTFFontFromArchive("FreeSans.ttf", 10.0f, nullptr, nullptr);
_state->_archive.memEdit.ReadOnly = true;
_state->_logger = new ImGuiEx::ImGuiLogger;
Common::setLogWatcher(onLog);
}
void onImGuiRender() {
if (!debugChannelSet(-1, kDebugImGui)) {
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange | ImGuiConfigFlags_NoMouse;
return;
}
if (!_state)
return;
ImGuiIO &io = ImGui::GetIO();
io.ConfigFlags &= ~(ImGuiConfigFlags_NoMouseCursorChange | ImGuiConfigFlags_NoMouse);
ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport(), ImGuiDockNodeFlags_PassthruCentralNode);
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("View")) {
ImGui::SeparatorText("Windows");
if (ImGui::MenuItem("View All")) {
if (_state->_wasHidden)
_state->_w = _state->_savedW;
_state->_wasHidden = false;
}
if (ImGui::MenuItem("Hide All")) {
if (!_state->_wasHidden) {
_state->_savedW = _state->_w;
memset((void *)&_state->_w, 0, sizeof(_state->_w));
}
_state->_wasHidden = true;
}
ImGui::MenuItem("Control Panel", NULL, &_state->_w.controlPanel);
ImGui::MenuItem("Score", NULL, &_state->_w.score);
ImGui::MenuItem("Functions", NULL, &_state->_w.funcList);
ImGui::MenuItem("Cast", NULL, &_state->_w.cast);
ImGui::MenuItem("Channels", NULL, &_state->_w.channels);
ImGui::MenuItem("CallStack", NULL, &_state->_w.callStack);
ImGui::MenuItem("Breakpoints", NULL, &_state->_w.bpList);
ImGui::MenuItem("Vars", NULL, &_state->_w.vars);
ImGui::MenuItem("Watched Vars", NULL, &_state->_w.watchedVars);
ImGui::MenuItem("Logger", NULL, &_state->_w.logger);
ImGui::MenuItem("Archive", NULL, &_state->_w.archive);
ImGui::SeparatorText("Misc");
if (ImGui::MenuItem("Save state")) {
}
if (ImGui::MenuItem("Load state")) {
}
ImGui::Separator();
ImGui::MenuItem("Settings", NULL, &_state->_w.settings);
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
showScriptCasts();
showScripts();
showControlPanel();
showVars();
showCallStack();
showChannels();
showCast();
showFuncList();
showScore();
showBreakpointList();
showSettings();
showArchive();
showWatchedVars();
_state->_logger->draw("Logger", &_state->_w.logger);
}
void onImGuiCleanup() {
Common::setLogWatcher(nullptr);
if (_state) {
free(_state->_archive.data);
delete _state->_logger;
}
delete _state;
_state = nullptr;
}
} // namespace DT
} // namespace Director
|