Lit reorganizing and adding command parser.

This commit is contained in:
2025-06-21 01:34:13 -05:00
parent 4252febd3a
commit abb1ba35c3
3 changed files with 83 additions and 50 deletions

View File

@@ -19,6 +19,8 @@
#include <App/LayerView.hpp>
#include <App/TilesetView.hpp>
#include "JUI/Widgets/FpsGraph.hpp"
#define GL_VER_MAJOR 2
#define GL_VER_MINOR 1
@@ -81,7 +83,7 @@ public:
JUI::Window* bg_color_tool_window = nullptr;
JUI::ColorPicker* bg_color_tool = nullptr;
JUI::UtilityBar* topbar = nullptr;
JUI::FpsGraph* fps_graph = nullptr;
JUI::Window* app_info_dialog = nullptr;
JUI::Text* topbar_stats = nullptr;
#pragma endregion
@@ -169,6 +171,9 @@ public:
/// Creates a JUI widget that displays the Tileset, and lets you select a tile from it.
JUI::Window* CreateTilesetViewerWindow(JUI::Widget* parent);
void ParseCmdLineMessage(const std::string& message);
void LogHelpInfo();
void BindConsoleCallbacks();
/// Create all JUI elements required for this program.

View File

@@ -4,8 +4,6 @@
class TilesetView : public JUI::Window {
public:
int tileset_width;
int tileset_height;
Tileset* stored_tileset;
Texture* stored_texture;
Event<int> TileSelected;
@@ -14,21 +12,23 @@ public:
Title("Tileset View");
SetResizable(false);
SetDraggable(true);
// TODO: Allow changing the background color of the tileset viewer.
Content()->BGColor(Colors::Black);
tilesheet = new JUI::Image(Content());
tilesheet->FitImageToParent(false);
}
TilesetView(Tileset* tileset, JGL::Texture* texture) {
TilesetView(Tileset* tileset, JGL::Texture* texture) : TilesetView() {
stored_tileset = tileset;
stored_texture = texture;
Title(std::format("Tileset View : {}", tileset->name));
Size(JUI::UDim2::FromPixels(texture->GetDimensions().x, texture->GetDimensions().y));
Size(JUI::UDim2::FromPixels(texture->GetDimensions().x, texture->GetDimensions().y+20));
tilesheet = new JUI::Image(Content());
tilesheet->Content(texture);
tilesheet->FitImageToParent(false);
auto grid_overlay_target = new JGL::RenderTarget(texture->GetDimensions());
J2D::Begin(grid_overlay_target);
@@ -41,15 +41,17 @@ public:
auto* overlay = new JUI::Image(this->Content());
overlay->Content(grid_overlay);
overlay->ZIndex(2);
overlay->FitImageToParent(false);
cell_indicator = new JUI::Rect(Content());
cell_indicator->BorderMode(JUI::BorderMode::Outline);
cell_indicator->BorderColor(Colors::Blues::CornflowerBlue);
cell_indicator->BorderWidth(2);
cell_indicator->BGColor(Colors::Transparent);
SetCellSizeIndicatorToTilesetSize();
}
TilesetView(Widget* parent) {
TilesetView(Widget* parent) : TilesetView() {
Parent(parent);
}
TilesetView(Tileset* tileset, JGL::Texture* texture, Widget* parent) : TilesetView(tileset, texture) {
@@ -80,10 +82,13 @@ public:
is_focusing = true;
cell_indicator->Visible(true);
Vector2 rel = Vector2(GetTilesetCellFromMouse(last_known_mouse_pos));
auto ipair = InputService::GetMousePosition();
Vector2 mpos(ipair.x, ipair.y);
rel.x = tileset_width;
rel.y = tileset_height;
Vector2 rel = Vector2(GetTilesetCellFromMouse(mpos));
rel.x *= stored_tileset->tile_width;
rel.y *= stored_tileset->tile_height;
cell_indicator->Position(JUI::UDim2::FromPixels(rel.x, rel.y));
@@ -94,16 +99,17 @@ public:
}
bool ObserveMouseInput(JUI::MouseButton btn, bool pressed) override {
if (pressed == false) {
if (btn == JUI::MouseButton::Left) {
if (Content()->IsMouseInside()) {
Vector2i cell = GetTilesetCellFromMouse(last_known_mouse_pos);
int index = CellToIndex(cell, stored_tileset->rows);
TileSelected.Invoke(index);
if (Window::ObserveMouseInput(btn, pressed)) return true;
}
if (IsMouseInside()) {
if (Content()->IsMouseInside() && pressed == false && btn == JUI::MouseButton::Left) {
Vector2i cell = GetTilesetCellFromMouse(last_known_mouse_pos);
int index = CellToIndex(cell, stored_tileset->rows);
TileSelected.Invoke(index);
}
return true;
}
return false;
}
bool IsFocusing() {

View File

@@ -237,7 +237,13 @@ void EditorApp::LoadLevel(const std::filesystem::path& level_meta_path)
layer->Load();
tileset_view = new TilesetView(loaded_tileset, loaded_tilesheet);
tileset_view = new TilesetView(loaded_tileset, loaded_tilesheet, scene);
tileset_view->TileSelected += [this](int id) mutable {
selected_quad = id;
};
layer_view->UpdateComponents(loaded_level);
data_ready = true;
}
@@ -357,31 +363,37 @@ namespace misc {
}
void EditorApp::ParseCmdLineMessage(const std::string &message) {
auto tokens = misc::string_expand(message);
struct Arg {
std::string name;
std::string type;
bool optional;
};
if (tokens.size() == 0) {
console->Log("No command input!", Colors::Red);
return;
}
struct Command {
std::string name;
std::vector<std::string> aliases;
std::vector<Arg> args;
std::string err_msg = "ERROR: Invalid command syntax!";
};
std::string cmd = tokens[0];
// Remove 0th element from tokens before passing to command delegates.
tokens.erase(tokens.begin());
if (misc::string_matches(cmd, {"reset"})) {
}
console->Log(std::format("No such command: {}", cmd), Colors::Red);
void EditorApp::LogHelpInfo() {
console->Log("Redacted Software 2D Level Editor - Command Line");
console->Log("Type `cmds` to see a list of all commands!");
}
std::string tolower(const std::string& s) {
std::string copy = "";
for (auto elem : s) {
copy += std::tolower(elem);
}
return copy;
}
void EditorApp::BindConsoleCallbacks()
{
// TODO: This parsing pattern is duplicated between at least 2 other projects at this point.
// TODO: Move up into JUI or a separate package.
console->OnInput += [this] (const std::string& input)
@@ -390,21 +402,36 @@ void EditorApp::BindConsoleCallbacks()
std::string cmd = tokens[0];
tokens.erase(tokens.begin());
if (cmd == "help") {
return LogHelpInfo();
}
if (cmd == "cmds" || cmd == "commandlist") {
}
if (cmd == "q" || cmd == "quit") { return this->Close(); }
if (cmd == "framegraph") {
// TODO: Add FpsGraph::Toggle().
fps_graph->Visible(!fps_graph->IsVisible());
}
if (cmd == "load") {
LoadLevel(tokens[1]);
if (tokens.size() >= 1) {
return LoadLevel(tokens[0]);
}
return console->Log("ERROR: Must provide file name!");
}
if (cmd == "save") {
SaveCurrentLevel();
}
if (cmd == "save-as") {
if (tokens.size() >= 2)
SaveCurrentLevelAs(tokens[1]);
if (tokens.size() >= 1)
return SaveCurrentLevelAs(tokens[0]);
else
console->Log(std::format("ERROR: Must provide file name: {}", cmd));
return console->Log(std::format("ERROR: Must provide file name: {}", cmd));
}
if (cmd == "new") {
SaveCurrentLevel();
}
if (cmd == "info") {
@@ -416,7 +443,7 @@ void EditorApp::BindConsoleCallbacks()
if (cmd == "tileset") {
}
console->Log(std::format("No such command: {}", cmd), Colors::Red);
};
}
@@ -426,7 +453,7 @@ void EditorApp::CreateWidgets()
app_info_dialog = CreateAppInfoDialogWindow(scene);
auto* fps_graph = new JUI::FpsGraph(scene);
fps_graph = new JUI::FpsGraph(scene);
// TODO: Revise to use PseudoDockedElementAtBottomOfViewport coming in the next JUI release.
fps_graph->Size({100_percent, 50_px});
fps_graph->AnchorPoint({1, 1});
@@ -891,11 +918,6 @@ void EditorApp::Draw()
}
glPopMatrix();
if (data_ready) {