Additions

This commit is contained in:
2025-04-03 13:34:51 -04:00
parent 90272e35f4
commit 86d01099e2
9 changed files with 128 additions and 43 deletions

View File

@@ -4,6 +4,22 @@
namespace JUI
{
class JUILogger : public jlog::GenericLogger {
public:
Event<std::string, Color4> OnLog;
JUILogger(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 = jlog::Timestamp())
};
extern jlog::GenericLogger UILogs;
/// An enumeration for mouse buttons, used by JUI to decouple from external systems.

View File

@@ -5,9 +5,7 @@
#include <JUI/Widgets/ScrollingRect.hpp>
#include <JUI/Widgets/ListLayout.hpp>
namespace JUI
{
namespace JUI {
// TODO: Implement "Rich Text" message support.
// TODO: Implement command autocomplete helper API.
@@ -34,30 +32,18 @@ namespace JUI
/// Constructs a command line by specifying the widget it should be parented to.
explicit CommandLine(Widget* parent);
void Log(const std::string& message, const Color4& color = Colors::White)
{
const Color4 light {60, 60, 60};
const Color4 dark {80, 80, 80};
auto* entry = new JUI::TextRect(message_log_list);
entry->Size({100_percent, UDim(message_height, 0)});
entry->SetContent(message);
}
void Log(const std::string& message, const Color4& color = Colors::White);
[[nodiscard]] bool Opened() const;
[[nodiscard]] bool Closed() const;
void Open(bool open = true);
void Close(bool open = false);
void Open(bool openVal = true);
void Close(bool openVal = false);
void SetOpen(bool openVal);
bool ObserveKeyInput(Key key, bool pressed) override;
virtual void OnInputBoxSend(const std::string message)
{
OnInput.Invoke(message);
input_box->SetContent("");
input_box->SetAutoCompleteText("");
}
virtual void OnInputBoxSend(const std::string& message);
protected:
TextInputForm* input_box;

View File

@@ -33,11 +33,9 @@
// TODO: Simpler "One-liner" Rich text, which is a horizontal sequence of textlabels automatically generated from a format.
namespace RichTextFormat
{
namespace RichTextFormat {
class RichTextToken
{
class RichTextToken {
public:
std::string font_face_name;
std::string content;

View File

@@ -65,8 +65,9 @@ JUI::Scene* CreateScene() {
nineslice_demo_window->TitlebarHeight(12);
console = new JUI::CommandLine(root);
console
console->Close();
JUI::UILogs += [] (){ console};
auto* topbar = new UtilityBar(root);
topbar->ZIndex(3);
auto* file = topbar->AddButton("Demos");
@@ -91,9 +92,9 @@ JUI::Scene* CreateScene() {
auto* open_console = ctx_menu->AddItem("Command Line");
open_console->OnClickEvent += [&] (Vector2 pos, JUI::MouseButton btn) {
}
open_console->OnClickEvent += [&] (Vector2 pos, JUI::MouseButton btn)mutable {
console->Open();
};
ctx_menu->ZIndex(3);
};

View File

@@ -341,7 +341,10 @@ bool Widget::ObserveMouseMovement(const Vector2 &latest_known_pos) {
bool Widget::ObserveKeyInput(Key key, bool pressed) {
for (Widget* child : children)
child->ObserveKeyInput(key, pressed);
if (child->ObserveKeyInput(key, pressed))
return true;
return false;
}

View File

@@ -2,8 +2,14 @@
namespace JUI
{
JUILogger::JUILogger(const std::string &context, std::ofstream &file, Color4 contextColor, Color4 timestampColor,
Color4 locationColor, Color4 pointerColor, Color4 messageColor)
: jlog::GenericLogger(context, file, contextColor, timestampColor, locationColor, pointerColor, messageColor) { }
std::ofstream GlobalLogFile("latest.log", std::ios_base::app);
jlog::GenericLogger UILogs {"JUI", GlobalLogFile, Colors::Green, Colors::Gray, Colors::Gray, Colors::Green, Colors::White};
JUILogger UILogs {"JUI", GlobalLogFile, Colors::Green, Colors::Gray, Colors::Gray, Colors::Green, Colors::White};
}

View File

@@ -2,6 +2,20 @@
namespace JUI
{
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
const std::string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
// for more information about date/time format
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
return buf;
}
CommandLine::CommandLine() {
this->SetTitle("Console");
@@ -14,6 +28,7 @@ namespace JUI
input_box = new JUI::TextInputForm(this->ViewportInstance());
input_box->SetAutoCompleteText(">Type Commands Here");
input_box->SetContent("");
input_box->Size(JUI::UDim2(0, 20, 1, 0));
input_box->Position(JUI::UDim2(0, -20, 0, 1));
input_box->BGColor(Colors::Grays::DarkSlateGray);
@@ -23,10 +38,50 @@ namespace JUI
input_box->OnReturn += [this] (const std::string& msg) {
OnInputBoxSend(msg);
Log(std::format("[{}] >{}", currentDateTime(), msg));
};
}
CommandLine::CommandLine(Widget *parent): CommandLine() {
this->Parent(parent);
}
bool CommandLine::Opened() const { return this->open == true;}
bool CommandLine::Closed() const { return this->open == false;}
void CommandLine::Open(bool openVal) { SetOpen(openVal); }
void CommandLine::Close(bool openVal) { SetOpen(openVal);}
void CommandLine::SetOpen(bool openVal) {
this->Visible(openVal);
this->open = openVal;
}
void CommandLine::Log(const std::string &message, const Color4 &color) {
const Color4 light {60, 60, 60};
const Color4 dark {80, 80, 80};
auto* entry = new JUI::TextRect(message_log_list);
entry->Size({100_percent, UDim(message_height, 0)});
entry->SetContent(message);
entry->LayoutOrder(index++);
entry->BGColor(index%2 == 0 ? light : dark);
entry->SetTextColor(color);
entry->SetTextSize(14);
}
bool CommandLine::ObserveKeyInput(Key key, bool pressed) {
if (!IsVisible())
return false;
return Widget::ObserveKeyInput(key, pressed);
}
void CommandLine::OnInputBoxSend(const std::string &message) {
OnInput.Invoke(message);
input_box->SetContent("");
input_box->SetAutoCompleteText("");
}
}

View File

@@ -87,13 +87,18 @@ void ScrollingRect::Update(float delta) {
}
bool ScrollingRect::ObserveKeyInput(Key key, bool pressed) {
// TODO: Forego this in favor of mouse scrolling and clicking.
if (key == Keys::UpArrow && pressed)
{
scroll -= 10;
return true;
}
if (key == Keys::DownArrow && pressed)
{
scroll += 10;
return true;
}
return false;
}

View File

@@ -186,21 +186,36 @@ namespace JUI {
bool TextInputForm::ObserveKeyInput(Key key, bool pressed) {
Widget::ObserveKeyInput(key, pressed);
if (!pressed || !focused) return;
if (key == Keys::Return || key == Keys::NumPadReturn)
return SendInput(clear_text_on_return);
if (key == Keys::LeftArrow) return MoveCursorLeft();
if (key == Keys::RightArrow) return MoveCursorRight();
if (InputService::IsKeyDown(Keys::LeftControl)) {
if (key == Keys::C) return Copy();
if (key == Keys::V) return Paste();
if (key == Keys::X) return Cut();
if (!pressed || !focused) return false;
if (key == Keys::Return || key == Keys::NumPadReturn) {
SendInput(clear_text_on_return);
return true;
}
if (IsNonAlphanumerosymbolic(key)) return;
if (blacklist.contains(key.Mnemonic)) return;
if (key == Keys::Backspace) return Backspace();
if (key == Keys::LeftArrow) {
MoveCursorLeft();
return true;
}
if (key == Keys::RightArrow) {
MoveCursorRight();
return true;
}
if (InputService::IsKeyDown(Keys::LeftControl)) {
if (key == Keys::C) { Copy(); return true; }
if (key == Keys::V) { Paste(); return true; }
if (key == Keys::X) { Cut(); return true; }
}
if (IsNonAlphanumerosymbolic(key)) return false;
if (blacklist.contains(key.Mnemonic)) return false;
if (key == Keys::Backspace) {Backspace(); return true; }
// No other condition met, so we assume we should push this key into the input buffer.
PushKeyToCurrentPlaceInInputBuffer(key);
return true;
}
bool TextInputForm::HasFocus() const { return focused; }