Files
Editor2D/include/TestGame/TestGameApp.hpp
2025-06-23 21:12:07 -05:00

165 lines
4.1 KiB
C++

#include "GameEntity.hpp"
#include "Player.hpp"
#include "App/EditorCamera.hpp"
#include "Data/Level.hpp"
#include "JUI/Widgets/Scene.hpp"
#include "ReWindow/types/Window.h"
#include <JUI/Widgets/Slider.hpp>
#include <JUI/Base/TextBase.hpp>
#include <ranges>
namespace TestGame
{
constexpr int GL_MAJOR = 2;
constexpr int GL_MINOR = 1;
constexpr int START_APP_WIDTH = 1024;
constexpr int START_APP_HEIGHT = 768;
static const std::string START_APP_TITLE = "Redacted Software 2D Level Editor - Map Testing Game";
// Implemented in next version of JUI.
class LabeledSlider : public JUI::Slider, public JUI::TextBase {
public:
LabeledSlider() : Slider(), TextBase() {
}
explicit LabeledSlider(Widget* parent) : LabeledSlider()
{
this->Parent(parent);
}
void Draw() override {
Slider::Draw();
auto abs_pos = this->GetAbsolutePosition();
auto abs_size = this->GetAbsoluteSize();
auto pos_pad = GetAbsolutePaddingTopLeft();
auto size_pad = GetAbsolutePaddingBottomRight();
TextBase::Draw(abs_pos + pos_pad, abs_size - size_pad);
}
void Update(float elapsed) override {
Slider::Update(elapsed);
TextBase::Update(elapsed);
}
protected:
private:
};
class TestGameAppWindow : public ReWindow::OpenGLWindow
{
public:
EditorCamera camera;
Level* loaded_level = nullptr;
Tileset* loaded_tileset = nullptr;
JGL::Texture* loaded_tilesheet = nullptr;
JUI::Scene* scene = nullptr;
Player* player = nullptr;
bool data_ready = false;
std::vector<GameEntity*> entities;
TestGameAppWindow();
~TestGameAppWindow() override;
/// Initializes and styles all the JUI widgets used by the application.
/// This function is called from Load().
void CreateUI();
/// Load all resources.
/// This is called after the application window is initially opened, and the OpenGL context is created.
/// But is always called before the first pass of the gameloop (Update, Draw, Step, etc).
void Load();
bool Open() override;
/// Performs collision testing and response between entities and tiles.
void CollisionSolve(float elapsed);
void CameraUpdate(float elapsed);
void Update(float elapsed);
void DrawLayer(const Layer* layer) const;
/// Translates the coordinate-space origin {0,0} to be at the center of the screen.
void ApplyOriginTransformation();
/// Applies the camera's rotation, scale, and translation to the coordinate system.
/// This is performed after translating the origin to the center of the screen.
void ApplyCameraTransformation();
void DrawLevel(const Level* level) const;
/// Draw objects with the camera projection, as if they are in world-space.
void DrawWorldSpace();
void Render();
#pragma region ReWindow Overrides
void OnRefresh(float elapsed) override
{
Update(elapsed);
Render();
SwapBuffers();
}
enum JUI::MouseButton ToJUIEnum(const MouseButton& btn) {
if (btn == MouseButtons::Left) return JUI::MouseButton::Left;
if (btn == MouseButtons::Middle) return JUI::MouseButton::Middle;
if (btn == MouseButtons::Right) return JUI::MouseButton::Right;
// Default condition.
return JUI::MouseButton::Left;
}
void OnMouseButtonDown(const ReWindow::MouseButtonDownEvent &e) override {
auto btn = ToJUIEnum(e.Button);
if (scene->ObserveMouseInput(btn, true)) return;
}
void OnMouseButtonUp(const ReWindow::MouseButtonUpEvent &e) override {
auto btn = ToJUIEnum(e.Button);
if (scene->ObserveMouseInput(btn, false)) return;
}
void OnMouseMove(const ReWindow::MouseMoveEvent &e) override {
Vector2 mposv2(e.Position.x, e.Position.y);
if (scene->ObserveMouseMovement(mposv2)) return;
}
void OnKeyDown(const ReWindow::KeyDownEvent &e) override {
if (scene->ObserveKeyInput(e.key, true)) return;
}
void OnKeyUp(const ReWindow::KeyUpEvent &e) override {
if (scene->ObserveKeyInput(e.key, false)) return;
}
void OnFocusGain(const ReWindow::RWindowEvent &e) override {
focused = true;
}
void OnFocusLost(const ReWindow::RWindowEvent &e) override {
focused = false;
}
#pragma endregion
};
}