Hacky logger integration, other fixes.

This commit is contained in:
2025-02-12 14:02:38 -05:00
parent f20dec85ce
commit dd6a284b6f
7 changed files with 194 additions and 110 deletions

View File

@@ -25,68 +25,15 @@ namespace CaveGame::Client {
Event<std::string> OnInput;
void OnInputEvent(const std::string& msg)
{
Log(std::format(">{}", msg));
OnInput.Invoke(msg);
// TODO: Handle within JUI
input_box->SetContent("");
}
void OnInputEvent(const std::string& msg);
explicit Console(Widget* parent) : JUI::Window(parent)
{
this->SetTitle("Console");
explicit Console(Widget* parent);
message_log_box = new JUI::ScrollingRect(this->ViewportInstance());
message_log_box->Size(JUI::UDim2(0, -20, 1, 1));
void Log(const std::string& message, const Color4& color = Colors::White);
message_log_layout = new JUI::VerticalListLayout(message_log_box);
message_log_layout->LayoutOrder(JUI::LayoutOrder::V::BOTTOM);
//message_log_view->Position(JUI::UDim2());
input_box = new JUI::TextInputForm(this->ViewportInstance());
input_box->Size(JUI::UDim2(0, 20, 1, 0));
input_box->Position(JUI::UDim2(0, -20, 0, 1));
input_box->BGColor(Colors::Grays::DarkSlateGray);
input_box->SetTextSize(16);
input_box->SetAutoCompleteText("");
input_box->OnReturn += [this] (const std::string& msg) {
OnInputEvent(msg);
};
}
void Log(const std::string& message)
{
auto* entry = new JUI::TextRect(message_log_layout);
entry->Size({100_percent, 16_px});
entry->SetContent(message);
entry->LayoutOrder(index++);
entry->SetTextColor(Colors::Black);
entry->BGColor(index%2==0 ? Colors::Grays::LightGray : Colors::Grays::LightSlateGray);
entry->SetTextSize(14);
}
void ObserveKeyInput(Key key, bool pressed) override
{
if (!IsVisible())
return;
Widget::ObserveKeyInput(key, pressed);
}
bool IsOpen() const { return console_open;}
void SetOpen(bool open) {
console_open = open;
if (open)
input_box->GrabFocus();
else
input_box->DropFocus();
Visible(open);
}
void ObserveKeyInput(Key key, bool pressed) override;
bool IsOpen() const;
void SetOpen(bool open);
protected:
JUI::TextInputForm* input_box;
JUI::ScrollingRect* message_log_box;

View File

@@ -1 +1,82 @@
#include <Client/Console.hpp>
#include <Client/Console.hpp>
#include <Core/Loggers.hpp>
void CaveGame::Client::Console::OnInputEvent(const std::string &msg) {
Log(std::format(">{}", msg));
OnInput.Invoke(msg);
// TODO: Handle within JUI
input_box->SetContent("");
}
CaveGame::Client::Console::Console(JUI::Widget *parent) : JUI::Window(parent)
{
this->SetTitle("Console");
message_log_box = new JUI::ScrollingRect(this->ViewportInstance());
message_log_box->Size(JUI::UDim2(0, -20, 1, 1));
message_log_layout = new JUI::VerticalListLayout(message_log_box);
message_log_layout->LayoutOrder(JUI::LayoutOrder::V::BOTTOM);
//message_log_view->Position(JUI::UDim2());
input_box = new JUI::TextInputForm(this->ViewportInstance());
input_box->Size(JUI::UDim2(0, 20, 1, 0));
input_box->Position(JUI::UDim2(0, -20, 0, 1));
input_box->BGColor(Colors::Grays::DarkSlateGray);
input_box->SetTextSize(16);
input_box->SetAutoCompleteText("");
input_box->OnReturn += [this] (const std::string& msg) {
OnInputEvent(msg);
};
CaveGame::Logs::Info.OnLog += [this](std::string m, Color4 c) {Log(m, c); };
CaveGame::Logs::Debug.OnLog += [this](std::string m, Color4 c) {Log(m, c); };
CaveGame::Logs::Warning.OnLog += [this](std::string m, Color4 c) {Log(m, c); };
CaveGame::Logs::Error.OnLog += [this](std::string m, Color4 c) {Log(m, c); };
CaveGame::Logs::Fatal.OnLog += [this](std::string m, Color4 c) {Log(m, c); };
CaveGame::Logs::Client.OnLog += [this](std::string m, Color4 c) {Log(m, c); };
CaveGame::Logs::Server.OnLog += [this](std::string m, Color4 c) {Log(m, c); };
CaveGame::Logs::Generator.OnLog += [this](std::string m, Color4 c) {Log(m, c); };
CaveGame::Logs::Lighting.OnLog += [this](std::string m, Color4 c) {Log(m, c); };
// TODO: Integrate loggers into game console.
}
void CaveGame::Client::Console::Log(const std::string &message, const Color4& color) {
Color4 col_a = {64, 64, 64};
Color4 col_b = {92, 92, 92};
auto* entry = new JUI::TextRect(message_log_layout);
entry->Size({100_percent, 16_px});
entry->SetContent(message);
entry->LayoutOrder(index++);
entry->SetTextColor(Colors::Black);
entry->BGColor(index%2==0 ? col_a : col_b);
entry->SetTextSize(14);
entry->SetTextColor(color);
}
void CaveGame::Client::Console::ObserveKeyInput(Key key, bool pressed) {
if (!IsVisible())
return;
Widget::ObserveKeyInput(key, pressed);
}
void CaveGame::Client::Console::SetOpen(bool open) {
console_open = open;
if (open)
input_box->GrabFocus();
else
input_box->DropFocus();
Visible(open);
}
bool CaveGame::Client::Console::IsOpen() const { return console_open;}

View File

@@ -1,4 +1,5 @@
#include <Client/TileTool.hpp>
#include <Core/Loggers.hpp>
CaveGame::Client::TileTool::TileTool(JUI::Widget *parent) : JUI::Window(parent)
{
@@ -85,6 +86,7 @@ void CaveGame::Client::TileTool::SetBrushSize(float size) {
void CaveGame::Client::TileTool::Enable(bool value) {
this->enabled = value;
std::cout << "TileTool Enabled " << value << std::endl;
CaveGame::Logs::Info("Tile Editor Tool Toggled");
this->Visible(enabled);
}

View File

@@ -113,5 +113,6 @@ namespace CaveGame::ClientApp {
float tool_radius = 8.f;
float tool_percent = 100.f;
bool wanna_die = false; // This field indicates the program is ready to close.
void DrawTileToolOverlayCursor();
};
}

View File

@@ -292,6 +292,34 @@ namespace CaveGame::ClientApp
}
void CaveGameWindow::DrawTileToolOverlayCursor() {
JGL::J2D::Begin();
auto camera = game_ctx->world->camera;
// TODO: The following Translation, Rotation, Scale transformation code is duplicated between here and LocalWorld.cpp:Draw.
// Shift the origin to the center of the screen.
glTranslatef(camera.HalfSizeOffset().x, camera.HalfSizeOffset().y, 0);
// Apply rotation, zoom, and translation.
glRotatef(camera.Rotation(), 0, 0, 1);
glScalef(camera.Zoom(), camera.Zoom(), 1);
glTranslatef(-camera.Position().x, -camera.Position().y, 0);
auto mpos = Vector2(currentMouse.Position.x, currentMouse.Position.y);
auto pos = camera.ScreenToWorld(mpos);
JGL::J2D::OutlineCircle(Colors::Red, pos, tool_radius);
JGL::J2D::DrawPoint({128, 128, 128, 128}, pos, 1.1f);
JGL::J2D::End();
}
void CaveGameWindow::Draw()
{
auto isize = GetSize();
@@ -336,33 +364,7 @@ namespace CaveGame::ClientApp
draw_debug_info(debug_lines);
if (current_scene == game_ctx) {
JGL::J2D::Begin();
auto camera = game_ctx->world->camera;
// TODO: The following Translation, Rotation, Scale transformation code is duplicated between here and LocalWorld.cpp:Draw.
// Shift the origin to the center of the screen.
glTranslatef(camera.HalfSizeOffset().x, camera.HalfSizeOffset().y, 0);
//DrawSky();
// Apply rotation, zoom, and translation.
glRotatef(camera.Rotation(), 0, 0, 1);
glScalef(camera.Zoom(), camera.Zoom(), 1);
glTranslatef(-camera.Position().x, -camera.Position().y, 0);
auto mpos = Vector2(currentMouse.Position.x, currentMouse.Position.y);
auto pos = camera.ScreenToWorld(mpos);
JGL::J2D::OutlineCircle(Colors::Red, pos, tool_radius);
JGL::J2D::DrawPoint({128, 128, 128, 128}, pos, 1.1f);
JGL::J2D::End();
DrawTileToolOverlayCursor();
}
}
@@ -394,7 +396,7 @@ namespace CaveGame::ClientApp
if (ev.key == Keys::Grave) {
// TODO: Key Input Hierarchy: Determine and handle priority of where certain keys go.
console_window->Visible(!console_window->IsVisible());
console_window->SetOpen(!console_window->IsOpen());
return;
}

View File

@@ -1,23 +1,43 @@
#pragma once
#include <jlog/Logger.hpp>
#include <Event.h>
namespace CaveGame::Logs
{
using namespace jlog;
using Logger = GenericLogger;
extern Logger Info;
extern Logger Debug;
extern Logger Warning;
extern Logger Error;
extern Logger Fatal;
std::string format_timestamp(Timestamp ts);
extern Logger Client;
extern Logger Server;
extern Logger Generator;
extern Logger Lighting;
std::string format_source_location(const std::source_location & location);
class CaveGameLogger : public GenericLogger
{
public:
Event<std::string, Color4> OnLog;
CaveGameLogger(const std::string &context,
std::ofstream &file,
Color4 contextColor = Colors::White,
Color4 timestampColor = Colors::Purples::Fuchsia,
Color4 locationColor = Colors::Pinks::Pink,
Color4 pointerColor = Colors::Pinks::LightPink,
Color4 messageColor = Colors::Greens::LightGreen);
void Log(const std::string &message, const std::source_location &location = std::source_location::current(), const jlog::Timestamp &ts = Timestamp()) override;
};
extern CaveGameLogger Info;
extern CaveGameLogger Debug;
extern CaveGameLogger Warning;
extern CaveGameLogger Error;
extern CaveGameLogger Fatal;
extern CaveGameLogger Client;
extern CaveGameLogger Server;
extern CaveGameLogger Generator;
extern CaveGameLogger Lighting;
void SetAllEnableConsole(bool log_to_console);
void SetAllEnableFile(bool log_to_file);

View File

@@ -2,18 +2,17 @@
namespace CaveGame::Logs {
using namespace jlog;
using Logger = GenericLogger;
GenericLogger Info {"CaveGame", GlobalLogFile,Colors::Gray, Colors::Gray, Colors::Gray, Colors::Gray};
GenericLogger Debug {"CaveGame", GlobalLogFile,Colors::Greens::Lime, Colors::Gray, Colors::Gray, Colors::Gray};
GenericLogger Warning {"CaveGame", GlobalLogFile,Colors::Oranges::Gold, Colors::Gray, Colors::Gray, Colors::Gray};
GenericLogger Error {"CaveGame", GlobalLogFile,Colors::Reds::Salmon, Colors::Gray, Colors::Gray, Colors::Gray};
GenericLogger Fatal {"CaveGame", GlobalLogFile, Colors::Reds::DarkRed, Colors::Reds::DarkRed, Colors::Reds::DarkRed, Colors::Reds::DarkRed};
CaveGameLogger Info {"CaveGame", GlobalLogFile,Colors::Gray, Colors::Gray, Colors::Gray, Colors::Gray};
CaveGameLogger Debug {"CaveGame", GlobalLogFile,Colors::Greens::Lime, Colors::Gray, Colors::Gray, Colors::Gray};
CaveGameLogger Warning {"CaveGame", GlobalLogFile,Colors::Oranges::Gold, Colors::Gray, Colors::Gray, Colors::Gray};
CaveGameLogger Error {"CaveGame", GlobalLogFile,Colors::Reds::Salmon, Colors::Gray, Colors::Gray, Colors::Gray};
CaveGameLogger Fatal {"CaveGame", GlobalLogFile, Colors::Reds::DarkRed, Colors::Reds::DarkRed, Colors::Reds::DarkRed, Colors::Reds::DarkRed};
GenericLogger Client {"CaveGame::Client", GlobalLogFile,Colors::Greens::Lime, Colors::Gray, Colors::Gray, Colors::Gray};
GenericLogger Server {"CaveGame::Server", GlobalLogFile,Colors::Greens::Lime, Colors::Gray, Colors::Gray, Colors::Gray};
GenericLogger Generator {"CaveGame::Generator", GlobalLogFile,Colors::Greens::Lime, Colors::Gray, Colors::Gray, Colors::Gray};
GenericLogger Lighting {"CaveGame::Generator", GlobalLogFile,Colors::Greens::Lime, Colors::Gray, Colors::Gray, Colors::Gray};
CaveGameLogger Client {"CaveGame::Client", GlobalLogFile,Colors::Greens::Lime, Colors::Gray, Colors::Gray, Colors::Gray};
CaveGameLogger Server {"CaveGame::Server", GlobalLogFile,Colors::Greens::Lime, Colors::Gray, Colors::Gray, Colors::Gray};
CaveGameLogger Generator {"CaveGame::Generator", GlobalLogFile,Colors::Greens::Lime, Colors::Gray, Colors::Gray, Colors::Gray};
CaveGameLogger Lighting {"CaveGame::Generator", GlobalLogFile,Colors::Greens::Lime, Colors::Gray, Colors::Gray, Colors::Gray};
void SetAllEnableConsole(bool log_to_console) {
Info.EnableConsole(log_to_console);
@@ -109,6 +108,38 @@ namespace CaveGame::Logs {
SetIncludeTimestampAll(false);
}
std::string format_timestamp(Timestamp ts) {
return std::format(
"{}-{}-{} {}:{}:{}.{}",
ts.Year(),
ts.Month(),
ts.Day(),
ts.Hour().count(),
ts.Minute().count(),
ts.Second().count(),
ts.Millisecond().count());
}
std::string format_source_location(const std::source_location &location) {
return std::format("{} @ {}:{}", location.function_name(), location.file_name(), location.line());
}
GenericLogger Network {"CaveGame::Generator", GlobalLogFile,Colors::Greens::Lime, Colors::Gray, Colors::Gray, Colors::Gray};
CaveGameLogger::CaveGameLogger(const std::string &context, std::ofstream &file, Color4 contextColor,
Color4 timestampColor, Color4 locationColor, Color4 pointerColor,
Color4 messageColor)
: GenericLogger(context, file, contextColor, timestampColor, locationColor, pointerColor, messageColor) { }
void CaveGameLogger::Log(const std::string &message, const std::source_location &location, const Timestamp &ts) {
GenericLogger::Log(message, location, ts);
std::string formatted_timestamp = format_timestamp(ts);
std::string formatted_source_location = format_source_location(location);
std::string final_result = std::format("[{}] [{}] {}", formatted_timestamp, this->ConsoleLogger::context, message);
OnLog.Invoke(final_result, messageColor);
}
}