Files
ReJUI/main.cpp

296 lines
8.9 KiB
C++

/// Josh's User Interface Library
/// A C++20 Library for creating, styling, and rendering of a UI/UX widgets.
/// Developed and Maintained by Josh O'Leary @ Redacted Software.
/// Special Thanks to William Tomasine II and Maxine Hayes.
/// (c) 2024 Redacted Software
/// This work is dedicated to the public domain.
/// @file main.cpp
/// @desc Demo Program Entry Point
/// @edit 2024-07-11
#include <iostream>
#include <JGL/JGL.h>
#include <JUI/Widgets/Rect.hpp>
#include <JUI/Widgets/RadioButton.hpp>
#include <JUI/Widgets/Window.hpp>
#include <JUI/Widgets/Scene.hpp>
#include <JUI/Widgets/Text.hpp>
#include <JUI/Widgets/ListLayout.hpp>
#include <JUI/Widgets/TextRect.hpp>
#include <JUI/Widgets/Image.hpp>
#include <rewindow/types/window.h>
#include <jlog/Logger.hpp>
JGL::Font FreeSans;
JUI::Scene* scene;
JGL::Texture* sample_texture;
JUI::Scene* CreateScene() {
using namespace JUI;
Scene *root = new Scene();
auto* JUI = new TextRect(root);
JUI->SetFont(FreeSans);
JUI->SetTextSize(48);
JUI->SetTextColor({32, 48, 192});
JUI->SetContent("Josh User Interface");
JUI->AlignBottom();
JUI->AlignCenterHorizontally();
JUI->SetAnchorPoint(Vector2(0.5f, 0.5f));
JUI->SetPosition({50_percent, 50_percent});
//JUI->SetSize({30_percent, 10_percent});
JUI->FitText(true);
JUI->SetPadding(20_px);
JUI->BGColor({64, 64, 64, 128});
JUI->SetBorderWidth(2);
JUI->BorderColor({255, 255, 255, 128});
auto* Redacted = new TextRect(root);
Redacted->SetFont(FreeSans);
Redacted->SetTextSize(32);
Redacted->SetTextColor({255, 255, 255});
Redacted->SetContent("Redacted Software Group");
Redacted->SetPosition({50_percent, 60_percent});
//Redacted->SetSize({30_percent, 10_percent});
Redacted->AlignCenterHorizontally();
Redacted->AlignTop();
Redacted->SetAnchorPoint(Vector2(0.5f, .5f));
Redacted->FitText(true);
Redacted->SetPadding(10_px);
Redacted->BGColor({32, 48, 192});
Redacted->SetBorderWidth(1);
Redacted->BorderColor({64, 64, 64});
// Rect //
Rect *rect_element = new Rect(root);
rect_element->SetName("JimBob");
//Rect* element = new Rect(root);
//rect_element->SetName("JimBob");
//element->BGColor({0,255,0});
rect_element->BGColor({0, 64, 0});
rect_element->SetSize({0, 0, 0.1f, 0.2f});
rect_element->SetClipsDescendants(true);
rect_element->BorderColor({255, 255, 255});
rect_element->SetBorderWidth(2.f);
rect_element->MouseEnter += [rect_element](auto coords) {
rect_element->BGColor({0, 128, 0});
};
rect_element->MouseExit += [rect_element](auto coords) {
rect_element->BGColor({0, 64, 0});
};
// End Rect //
// Button //
RadioButton *button_element = new RadioButton(root);
button_element->SetName("BobJim");
button_element->BGColor({0, 0, 64});
button_element->SetSize({0, 0, 0.1f, 0.1f});
button_element->SetClipsDescendants(true);
button_element->BorderColor({255, 255, 255});
button_element->SetBorderWidth(2.f);
auto bpos = rect_element->Size();
//exit(1);
button_element->SetPosition(
{bpos.X.Pixels, bpos.Y.Pixels, bpos.X.Scale - 0.2f, 0}); // I don't know how to use sx and sy - maxine
button_element->OnToggleOnEvent += [rect_element] () {
rect_element->BGColor({64, 0, 0});
};
button_element->OnToggleOffEvent += [rect_element] () {
rect_element->BGColor({0, 64, 0});
};
button_element->OnToggleEvent += [button_element] () {
Color4 incbg = button_element->BGColor();
// Once an overflow occurs it will reset anyway
// Thanks computer science
if (incbg.b < 255)
incbg.b += 10;
button_element->BGColor(incbg);
};
Text* btntext = new Text(button_element);
btntext->SetContent("I AM BUTTON");
btntext->SetFont(FreeSans);
btntext->SetTextSize(8);
btntext->SetTextColor({255, 0, 0});
// End Button //
// Window //
JUI::Window* win_element = new JUI::Window(root);
win_element->SetTitleFont(FreeSans);
win_element->SetSize({50_percent, 50_percent});
win_element->SetTitle("JUI Example Window Widget");
//win_element->SetPadding(1_px);
// End Window //
auto list = new VerticalListLayout(win_element->GetViewportInstance());
list->SetPadding(10_px);
TextRect* a = new TextRect(list);
a->SetTextSize(16);
a->SetFont(FreeSans);
a->SetSize({0, 20, 1, 0});
//a->FitText(true);
a->Center();
a->SetContent("Greetings. This is the JUI demo program!");
TextRect* b = new TextRect(list);
b->SetTextSize(16);
b->SetContent("JUI is my home-coded game menu building toolkit.");
//b->FitText(true);
b->SetSize({0, 20, 1, 0});
b->Center();
b->SetFont(FreeSans);
TextRect* c = new TextRect(list);
c->SetTextSize(16);
c->SetContent("Settings");
//c->FitText(true);
c->SetSize({0, 20, 1, 0});
c->Center();
c->SetFont(FreeSans);
TextRect* d = new TextRect(list);
d->SetTextSize(16);
d->SetContent("Exit");
//d->FitText(true);
d->SetSize({0, 20, 1, 0});
d->Center();
d->SetFont(FreeSans);
Text *text = new Text(rect_element);
text->SetContent("YO MAMA");
text->SetFont(FreeSans);
text->SetTextSize(48);
text->SetTextColor({255, 0, 0});
auto darkie = new Image(win_element->GetViewportInstance(), sample_texture);
darkie->FitImageToParent(true);
darkie->Color({255,255,255,128});
root->SetViewportSize(800, 600);
return root;
}
class JUIDevelopmentTestWindow : public ReWindow::RWindow {
public:
void initGL() {
gladLoadGL();
JGL::Update(getSize());
JGL::InitTextEngine();
glClearColor(0.f, 0.f, 0.f, 0.f);
// TODO: Delete when we update to the next release of JGL
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // NOTE: This MUST be called for text rendering to work properly!!!
}
void Update()
{
scene->Update(0.f);
}
void Draw()
{
scene->Draw();
}
JUIDevelopmentTestWindow(const std::string& title, int w, int h) : ReWindow::RWindow(title, w, h) {}
void OnRefresh(float elapsed) override {
Update();
JGL::Update(getSize());
scene->SetViewportSize(getSize().x, getSize().y);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Draw();
this->glSwapBuffers();
}
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override {
std::cout << "RESIZED:" << e.Size.x << ", " << e.Size.y << std::endl;
return true;
}
//bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent &e) override {}
JUIDevelopmentTestWindow() : ReWindow::RWindow() {}
};
int main()
{
using namespace ReWindow;
// TODO: Find out new jlog api for silencing specific loggers.
auto* window = new JUIDevelopmentTestWindow("Test Window", 800, 600);
window->setRenderer(RenderingAPI::OPENGL);
window->Open();
window->initGL();
window->setFullscreen(false);
window->setVsyncEnabled(false);
window->setResizable(true);
JGL::Update({800, 600});
FreeSans = JGL::Font("assets/fonts/FreeSans.ttf");
sample_texture = new JGL::Texture("assets/ld.png");
scene = CreateScene();
window->OnResizeRequestEvent += [&] (ReWindow::WindowResizeRequestEvent e){
Vector2 size = e.Size;//window->getLastKnownResize();
scene->SetViewportSize(size.x, size.y);
std::cout << size.x << "," << size.y << std::endl;
JGL::Update(size);
};
window->OnMouseMoveEvent += [&] (MouseMoveEvent e)
{
scene->ObserveMouseMovement(e.Position);
};
window->OnMouseButtonUpEvent += [&] (MouseButtonUpEvent e) {
/// Invalid operands to binary expression 'MouseButton' and 'const MouseButton'
if (e.Button == MouseButtons::Left)
scene->ObserveMouseInput(JUI::MouseButton::Left, false);
if (e.Button == MouseButtons::Middle)
scene->ObserveMouseInput(JUI::MouseButton::Middle, false);
if (e.Button == MouseButtons::Right)
scene->ObserveMouseInput(JUI::MouseButton::Right, false);
};
window->OnMouseButtonDownEvent += [&] (MouseButtonDownEvent e) {
if (e.Button == MouseButtons::Left)
scene->ObserveMouseInput(JUI::MouseButton::Left, true);
if (e.Button == MouseButtons::Middle)
scene->ObserveMouseInput(JUI::MouseButton::Middle, true);
if (e.Button == MouseButtons::Right)
scene->ObserveMouseInput(JUI::MouseButton::Right, true);
};
window->OnMouseButtonDownEvent += [&] (MouseButtonDownEvent e) {};
window->OnKeyDownEvent += [&] (KeyDownEvent e) {};
window->OnKeyUpEvent += [&] (KeyUpEvent e) {};
while (window->isAlive()) {
window->pollEvents();
window->refresh();
}
return 0;
}