Added text editor for ASM files

This commit is contained in:
Justin Scofield
2022-06-11 17:36:20 -04:00
parent a6fb58dba1
commit b887563407
6 changed files with 197 additions and 70 deletions

View File

@@ -9,62 +9,68 @@ bool Controller::isActive() const { return active; }
void Controller::onEntry() noexcept(false) {
window.Create();
renderer.Create(window.Get());
ImGuiIO &io = ImGui::GetIO();
io.KeyMap[ImGuiKey_Backspace] = SDL_GetScancodeFromKey(SDLK_BACKSPACE);
io.KeyMap[ImGuiKey_Enter] = SDL_GetScancodeFromKey(SDLK_RETURN);
io.KeyMap[ImGuiKey_UpArrow] = SDL_GetScancodeFromKey(SDLK_UP);
io.KeyMap[ImGuiKey_DownArrow] = SDL_GetScancodeFromKey(SDLK_DOWN);
io.KeyMap[ImGuiKey_Tab] = SDL_GetScancodeFromKey(SDLK_TAB);
active = true;
}
void Controller::onInput() {
int wheel = 0;
SDL_Event event;
ImGuiIO& io = ImGui::GetIO();
ImGuiIO &io = ImGui::GetIO();
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_UP:
case SDLK_DOWN:
case SDLK_RETURN:
case SDLK_BACKSPACE:
case SDLK_TAB:
io.KeysDown[event.key.keysym.scancode] =
(event.type == SDL_KEYDOWN);
break;
default:
break;
}
break;
case SDL_KEYUP: {
int key = event.key.keysym.scancode;
IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
io.KeysDown[key] = (event.type == SDL_KEYDOWN);
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
break;
}
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_CLOSE:
active = false;
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
io.DisplaySize.x = static_cast<float>(event.window.data1);
io.DisplaySize.y = static_cast<float>(event.window.data2);
break;
default:
break;
}
break;
case SDL_TEXTINPUT:
io.AddInputCharactersUTF8(event.text.text);
break;
case SDL_MOUSEWHEEL:
wheel = event.wheel.y;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_UP:
case SDLK_DOWN:
case SDLK_RETURN:
case SDLK_BACKSPACE:
case SDLK_TAB:
io.KeysDown[event.key.keysym.scancode] = (event.type == SDL_KEYDOWN);
break;
default:
break;
}
break;
case SDL_KEYUP: {
int key = event.key.keysym.scancode;
IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
io.KeysDown[key] = (event.type == SDL_KEYDOWN);
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
break;
}
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_CLOSE:
active = false;
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
io.DisplaySize.x = static_cast<float>(event.window.data1);
io.DisplaySize.y = static_cast<float>(event.window.data2);
break;
default:
break;
}
break;
case SDL_TEXTINPUT:
io.AddInputCharactersUTF8(event.text.text);
break;
case SDL_MOUSEWHEEL:
wheel = event.wheel.y;
break;
default:
break;
}
}
@@ -78,13 +84,9 @@ void Controller::onInput() {
io.MouseWheel = static_cast<float>(wheel);
}
void Controller::onLoad() {
editor.UpdateScreen();
}
void Controller::onLoad() { editor.UpdateScreen(); }
void Controller::doRender() {
renderer.Render();
}
void Controller::doRender() { renderer.Render(); }
void Controller::onExit() {
ImGui_ImplSDLRenderer_Shutdown();
@@ -95,6 +97,6 @@ void Controller::onExit() {
SDL_Quit();
}
} // namespace Core
} // namespace Application
} // namespace yaze
} // namespace Core
} // namespace Application
} // namespace yaze

View File

@@ -4,6 +4,87 @@ namespace yaze {
namespace Application {
namespace Editor {
Editor::Editor() {
static bool inited = false;
if (!inited) {
static const char *const keywords[] = {
"ADC", "AND", "ASL", "BCC", "BCS", "BEQ", "BIT", "BMI", "BNE",
"BPL", "BRA", "BRL", "BVC", "BVS", "CLC", "CLD", "CLI", "CLV",
"CMP", "CPX", "CPY", "DEC", "DEX", "DEY", "EOR", "INC", "INX",
"INY", "JMP", "JSR", "JSL", "LDA", "LDX", "LDY", "LSR", "MVN", "NOP",
"ORA", "PEA", "PER", "PHA", "PHB", "PHD", "PHP", "PHX", "PHY",
"PLA", "PLB", "PLD", "PLP", "PLX", "PLY", "REP", "ROL", "ROR",
"RTI", "RTL", "RTS", "SBC", "SEC", "SEI", "SEP", "STA", "STP",
"STX", "STY", "STZ", "TAX", "TAY", "TCD", "TCS", "TDC", "TRB",
"TSB", "TSC", "TSX", "TXA", "TXS", "TXY", "TYA", "TYX", "WAI",
"WDM", "XBA", "XCE", "ORG", "LOROM", "HIROM", "NAMESPACE", "DB" };
for (auto &k : keywords)
language65816Def.mKeywords.insert(k);
static const char *const identifiers[] = {
"abort", "abs", "acos", "asin", "atan", "atexit",
"atof", "atoi", "atol", "ceil", "clock", "cosh",
"ctime", "div", "exit", "fabs", "floor", "fmod",
"getchar", "getenv", "isalnum", "isalpha", "isdigit", "isgraph",
"ispunct", "isspace", "isupper", "kbhit", "log10", "log2",
"log", "memcmp", "modf", "pow", "putchar", "putenv",
"puts", "rand", "remove", "rename", "sinh", "sqrt",
"srand", "strcat", "strcmp", "strerror", "time", "tolower",
"toupper" };
for (auto &k : identifiers) {
TextEditor::Identifier id;
id.mDeclaration = "Built-in function";
language65816Def.mIdentifiers.insert(std::make_pair(std::string(k), id));
}
language65816Def.mTokenRegexStrings.push_back(
std::make_pair<std::string, TextEditor::PaletteIndex>(
"[ \\t]*#[ \\t]*[a-zA-Z_]+",
TextEditor::PaletteIndex::Preprocessor));
language65816Def.mTokenRegexStrings.push_back(
std::make_pair<std::string, TextEditor::PaletteIndex>(
"L?\\\"(\\\\.|[^\\\"])*\\\"", TextEditor::PaletteIndex::String));
language65816Def.mTokenRegexStrings.push_back(
std::make_pair<std::string, TextEditor::PaletteIndex>(
"\\'\\\\?[^\\']\\'", TextEditor::PaletteIndex::CharLiteral));
language65816Def.mTokenRegexStrings.push_back(
std::make_pair<std::string, TextEditor::PaletteIndex>(
"[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)([eE][+-]?[0-9]+)?[fF]?",
TextEditor::PaletteIndex::Number));
language65816Def.mTokenRegexStrings.push_back(
std::make_pair<std::string, TextEditor::PaletteIndex>(
"[+-]?[0-9]+[Uu]?[lL]?[lL]?", TextEditor::PaletteIndex::Number));
language65816Def.mTokenRegexStrings.push_back(
std::make_pair<std::string, TextEditor::PaletteIndex>(
"0[0-7]+[Uu]?[lL]?[lL]?", TextEditor::PaletteIndex::Number));
language65816Def.mTokenRegexStrings.push_back(
std::make_pair<std::string, TextEditor::PaletteIndex>(
"0[xX][0-9a-fA-F]+[uU]?[lL]?[lL]?",
TextEditor::PaletteIndex::Number));
language65816Def.mTokenRegexStrings.push_back(
std::make_pair<std::string, TextEditor::PaletteIndex>(
"[a-zA-Z_][a-zA-Z0-9_]*", TextEditor::PaletteIndex::Identifier));
language65816Def.mTokenRegexStrings.push_back(
std::make_pair<std::string, TextEditor::PaletteIndex>(
"[\\[\\]\\{\\}\\!\\%\\^\\&\\*\\(\\)\\-\\+\\=\\~\\|\\<\\>\\?\\/"
"\\;\\,\\.]",
TextEditor::PaletteIndex::Punctuation));
language65816Def.mCommentStart = "/*";
language65816Def.mCommentEnd = "*/";
language65816Def.mSingleLineComment = ";";
language65816Def.mCaseSensitive = false;
language65816Def.mAutoIndentation = true;
language65816Def.mName = "65816";
inited = true;
}
asm_editor_.SetLanguageDefinition(language65816Def);
asm_editor_.SetPalette(TextEditor::GetDarkPalette());
}
void Editor::UpdateScreen() {
const ImGuiIO &io = ImGui::GetIO();
ImGui::NewFrame();
@@ -51,7 +132,7 @@ void Editor::DrawYazeMenu() {
std::string filePathName = ImGuiFileDialog::Instance()->GetFilePathName();
std::string filePath = ImGuiFileDialog::Instance()->GetCurrentPath();
rom.LoadFromFile(filePathName);
owEditor.SetRom(rom);
overworld_editor_.SetRom(rom);
rom_data_ = (void *)rom.GetRawData();
}
@@ -126,10 +207,12 @@ void Editor::DrawEditMenu() const {
}
}
void Editor::DrawViewMenu() const {
void Editor::DrawViewMenu() {
static bool show_imgui_metrics = false;
static bool show_imgui_style_editor = false;
static bool show_memory_editor = false;
static bool show_asm_editor = false;
if (show_imgui_metrics) {
ImGui::ShowMetricsWindow(&show_imgui_metrics);
}
@@ -139,6 +222,31 @@ void Editor::DrawViewMenu() const {
mem_edit.DrawWindow("Memory Editor", rom_data_, rom.getSize());
}
if (show_asm_editor) {
static bool asm_is_loaded = false;
auto cpos = asm_editor_.GetCursorPosition();
static const char *fileToEdit = "assets/bunnyhood.asm";
if (!asm_is_loaded) {
std::ifstream t(fileToEdit);
if (t.good()) {
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
asm_editor_.SetText(str);
}
asm_is_loaded = true;
}
ImGui::Begin("ASM Editor", &show_asm_editor);
ImGui::Text("%6d/%-6d %6d lines | %s | %s | %s | %s", cpos.mLine + 1,
cpos.mColumn + 1, asm_editor_.GetTotalLines(),
asm_editor_.IsOverwrite() ? "Ovr" : "Ins",
asm_editor_.CanUndo() ? "*" : " ",
asm_editor_.GetLanguageDefinition().mName.c_str(), fileToEdit);
asm_editor_.Render(fileToEdit);
ImGui::End();
}
if (show_imgui_style_editor) {
ImGui::Begin("Style Editor (ImGui)", &show_imgui_style_editor);
ImGui::ShowStyleEditor();
@@ -153,6 +261,7 @@ void Editor::DrawViewMenu() const {
}
ImGui::MenuItem("HEX Editor", nullptr, &show_memory_editor);
ImGui::MenuItem("ASM Editor", nullptr, &show_asm_editor);
ImGui::Separator();
if (ImGui::BeginMenu("GUI Tools")) {
@@ -193,7 +302,7 @@ void Editor::DrawHelpMenu() const {
// understand the data quickly
void Editor::DrawOverworldEditor() {
if (ImGui::BeginTabItem("Overworld")) {
owEditor.Update();
overworld_editor_.Update();
ImGui::EndTabItem();
}
}

View File

@@ -4,29 +4,31 @@
#include <memory>
#include "Core/Icons.h"
#include "OverworldEditor.h"
#include "ImGuiColorTextEdit/TextEditor.h"
#include "ImGuiFileDialog/ImGuiFileDialog.h"
#include "OverworldEditor.h"
#include "Utils/ROM.h"
#include "imgui/backends/imgui_impl_sdl.h"
#include "imgui/backends/imgui_impl_sdlrenderer.h"
#include "imgui/imgui.h"
#include "imgui/imgui_internal.h"
#include "imgui/misc/cpp/imgui_stdlib.h"
#include "imgui/imgui_memory_editor.h"
#include "imgui/misc/cpp/imgui_stdlib.h"
namespace yaze {
namespace Application {
namespace Editor {
class Editor {
public:
public:
Editor();
void UpdateScreen();
private:
private:
void DrawYazeMenu();
void DrawFileMenu() const;
void DrawEditMenu() const;
void DrawViewMenu() const;
void DrawViewMenu();
void DrawHelpMenu() const;
void DrawOverworldEditor();
@@ -34,18 +36,20 @@ class Editor {
void DrawSpriteEditor();
void DrawScreenEditor();
void DrawROMInfo();
OverworldEditor owEditor;
void *rom_data_;
bool isLoaded = true;
Utils::ROM rom;
TextEditor asm_editor_;
TextEditor::LanguageDefinition language65816Def;
OverworldEditor overworld_editor_;
void* rom_data_;
bool isLoaded = true;
ImGuiTableFlags toolset_table_flags = ImGuiTableFlags_SizingFixedFit;
};
} // namespace View
} // namespace Application
} // namespace yaze
} // namespace Editor
} // namespace Application
} // namespace yaze
#endif // YAZE_APPLICATION_VIEW_EDITOR_H
#endif // YAZE_APPLICATION_VIEW_EDITOR_H