This commit is contained in:
scientiist
2023-10-26 23:26:06 -05:00
parent 05c160c3de
commit 2cac765c52
19 changed files with 242 additions and 67 deletions

View File

@@ -8,7 +8,15 @@
#include <vector>
#include "JUI/jui.h"
#include <glm/glm
#include <glm/glm.hpp>
class Transform2D
{
public:
protected:
private:
};
namespace JUI {
@@ -17,7 +25,6 @@ namespace JUI {
using uint = unsigned int;
class Camera2D
{
#pragma region Interface

View File

@@ -95,7 +95,6 @@ namespace JUI {
// Make sure last segment of curve is drawn
SDL_RenderDrawLine(renderer, last.x, last.y, first.x, first.y);
// Draw Control Points on top of curve
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderDrawPoint(renderer, p0.x, p0.y);
@@ -104,7 +103,6 @@ namespace JUI {
SDL_RenderDrawPoint(renderer, p3.x, p3.y);
}
void DrawCurve(SDL_Renderer* renderer, SDL_Color col, const curve& c, int subdivisions) {
JUI::DrawCurve(renderer, col, c.p0, c.p1, c.p2, c.p3, subdivisions);
}

View File

@@ -1,19 +1,24 @@
#pragma once
#include <JUI/common.h>
#include <JUI/Widgets/Widget.hpp>
#include <JUI/Widgets/Rect.hpp>
namespace JUI {
class Checkbox : public Widget {
class Checkbox : public Rect {
public:
Checkbox(const std::string& name)
: Widget(), name(name)
{
Checkbox();
explicit Checkbox(Widget* parent);
~Checkbox() override;
void Update(double delta) override;
void Draw(SDL_Renderer* target) override;
}
protected:
bool checked = false;
private:
const std::string &name;
};
}

View File

@@ -10,24 +10,33 @@
#pragma once
#include <JUI/Widgets/Widget.hpp>
#include "Rect.hpp"
#include <SDL_image.h>
namespace JUI {
// Texture Widget - Rect Wrapper an SDL_Texture
class Texture : public Rect {
// Image Widget - container for SDL_Texture
class Image : public Widget {
#pragma region Interface
public:
#pragma region Constructors
Texture(SDL_Renderer*); // Loads Dev Texture
Texture(SDL_Renderer*, std::string path);
Texture(SDL_Renderer*, SDL_Texture& tex);
Texture(SDL_Renderer*, SDL_Surface* surface);
Image(); // Loads Dev Texture
Image(Widget* parent);
Image(Widget* parent, SDL_Texture* texture);
#pragma endregion
#pragma region Member Methods
void Draw(SDL_Renderer* r) override;
void Update(double delta) override;
#pragma region Getters
v2f GetAbsoluteSize() const override;
v2f GetAbsolutePosition() const override;
SDL_Texture* GetTexture() const;
#pragma endregion
#pragma region Setters
void SetTexture(SDL_Texture* t);
#pragma endregion
#pragma endregion
#pragma region Events

View File

@@ -9,6 +9,7 @@ namespace JUI {
class LayoutContainerWidget : public Widget {
public:
LayoutContainerWidget();
LayoutContainerWidget(Widget* parent);
~LayoutContainerWidget() override;
@@ -28,7 +29,11 @@ namespace JUI {
#pragma region Interface
#pragma region Constructors
VerticalListLayout() {}
VerticalListLayout() : LayoutContainerWidget() {}
VerticalListLayout(Widget* parent) : VerticalListLayout()
{
this->SetParent(parent);
}
~VerticalListLayout() override {}

View File

@@ -20,6 +20,7 @@ namespace JUI {
public:
#pragma region Constructors
Rect();
Rect(Widget* parent);
~Rect() override {}
#pragma endregion
#pragma region Event

View File

@@ -27,11 +27,19 @@ namespace JUI {
public:
#pragma region Constructors
Text();
explicit Text(Widget* parent);
Text(Widget* parent, const std::string& text);
~Text() override;
#pragma endregion
#pragma region Member Methods
void Center()
{
SetHorizontalTextAlignment(TextAlignment::Horizontal::CENTER);
SetVerticalTextAlignment(TextAlignment::Vertical::CENTER);
}
#pragma region Getters
std::string GetText() const;
SDL_Color GetTextColor() const;

View File

@@ -34,8 +34,7 @@ namespace JUI {
public:
#pragma region Constructors
Widget();
Widget(const_ref<std::string> _name);
Widget(Widget* parent);
virtual ~Widget() {}
#pragma endregion

View File

@@ -5,6 +5,7 @@
#include <SDL2_gfxPrimitives.h>
#include "JUI/Types/Font.hpp"
namespace JUI {
// Aliases for GLM types
@@ -19,6 +20,7 @@ namespace JUI {
// Is there a better approach?
JUI::Font* GetDefaultFont();
SDL_Texture* GetCheckmark();
void Load(SDL_Renderer* renderer);
@@ -53,6 +55,8 @@ namespace JUI {
v2i GetMousePosition();
v2i GetTextureDimensions(SDL_Texture* tex);
void draw_simple_text(SDL_Renderer* gfx, std::string text, v2i coords);

View File

@@ -17,7 +17,7 @@
#include <JUI/Widgets/Scene.hpp>
#include <JUI/Widgets/Rect.hpp>
#include <JUI/Widgets/Texture.hpp>
#include <JUI/Widgets/Image.hpp>
#include <JUI/Widgets/Text.hpp>
#include <JUI/Widgets/LayoutContainers.hpp>
#include <JUI/Widgets/Checkbox.hpp>

View File

@@ -3,7 +3,6 @@
#include <SDL2/SDL.h>
#include <SDL_image.h>
#include <SDL_video.h>
#include <SDL2_framerate.h>
#include <JUI/jui.h>
#include "Tools/gizmo.hpp"
#include "JUI/Bezier.hpp"
@@ -72,17 +71,26 @@ int main(int argc, char** argv) {
#pragma endregion
#pragma region Scene Construction
JUI::Load(renderer);
auto *demo_scene = new JUI::Scene(window, renderer);
auto *rect = new JUI::Rect();
rect->SetParent(demo_scene);
auto *rect = new JUI::Rect(demo_scene);
//rect->SetParent(demo_scene);
rect->SetBackgroundColor({128, 128, 128, 128});
rect->SetPosition({25_px, 25_percent});
rect->SetBorderRadius(12.f);
rect->SetBorderWidth(1.f);
rect->SetBorderColor({0, 255, 255, 255});
auto enable_bg_btn = rect->Add(new JUI::Checkbox(""));
auto *list = new JUI::VerticalListLayout(rect);
auto *label = new JUI::Text(list, "Sample Text");
label->SetTextColor({255, 255, 255, 255});
label->Center();
auto *chkbox = new JUI::Checkbox(list);
//chkbox->SetBackgroundColor({128, 0, 128, 255});
#pragma endregion
@@ -112,7 +120,7 @@ int main(int argc, char** argv) {
}
}
#pragma region Update
// Calculate deltatime
// Logic Tick
demo_scene->Update(delta_time.count());
p0.Update();
@@ -135,11 +143,13 @@ int main(int argc, char** argv) {
demo_scene->Draw(renderer);
SDL_Color red {255, 0, 0, 255};
SDL_Color blu {128, 128, 255, 255};
for (int i = 1; i < 10; i++)
{
JUI::DrawCurve(renderer, red, p0.position, p1.position, p2.position, p3.position, i);
}
//for (int i = 1; i < 10; i++)
//{
JUI::DrawCurve(renderer, red, p0.position, p1.position, p2.position, p3.position, 3);
JUI::DrawCurve(renderer, blu, p0.position, p1.position, p2.position, p3.position, 30);
//}
p0.Draw(renderer);
p1.Draw(renderer);
@@ -163,7 +173,7 @@ int main(int argc, char** argv) {
#pragma endregion
#pragma endregion
std::this_thread::sleep_for(1ms);
//std::this_thread::sleep_for(2ms);
end = precision_clock::now();
delta_time = std::chrono::duration_cast<seconds_d>(end - start);
@@ -171,5 +181,8 @@ int main(int argc, char** argv) {
}
std::cout << "DONE!" << std::endl;
// Call this upon gameloop exit to dispose of assets
JUI::Cleanup();
#pragma endregion
}

View File

@@ -4,4 +4,54 @@
namespace JUI
{
Checkbox::Checkbox() : Rect() {
this->SetSize({25_px, 25_px});
this->SetBorderRadius(10.f);
this->MouseReleased += [this] (MouseButtonID mousebtn)
{
if (mousebtn == MouseButtonID::LMB)
{
this->checked = !this->checked;
}
};
}
Checkbox::Checkbox(Widget *parent) : Checkbox()
{
this->SetParent(parent);
}
void Checkbox::Update(double delta) {
Rect::Update(delta);
if (IsMouseInside())
{
}
}
void Checkbox::Draw(SDL_Renderer* target) {
Rect::Draw(target);
auto* texture = JUI::GetCheckmark();
auto image_dimensions = JUI::GetTextureDimensions(texture);
auto abs_pos = GetAbsolutePosition();
auto abs_size = GetAbsoluteSize();
if (checked)
{
SDL_Rect source_rect = {0, 0, image_dimensions.x, image_dimensions.y};
SDL_Rect output_rect = {
static_cast<int>(abs_pos.x),
static_cast<int>(abs_pos.y),
static_cast<int>(abs_size.x),
static_cast<int>(abs_size.y)};
SDL_RenderCopy(target, texture, &source_rect, &output_rect);
}
}
Checkbox::~Checkbox() = default;
}

58
src/JUI/Widgets/Image.cpp Normal file
View File

@@ -0,0 +1,58 @@
#include <JUI/Widgets/Image.hpp>
#include <SDL.h>
#include <SDL_image.h>
namespace JUI {
Image::Image() : Widget()
{
}
Image::Image(Widget* parent) : Image()
{
this->SetParent(parent);
}
Image::Image(Widget* parent, SDL_Texture* texture) : Image(parent)
{
this->texture = texture;
}
void Image::Update(double delta)
{
Widget::Update(delta);
}
v2f Image::GetAbsoluteSize() const
{ return this->GetParent()->GetAbsoluteSize(); }
v2f Image::GetAbsolutePosition() const { return this->GetParent()->GetAbsolutePosition(); }
void Image::Draw(SDL_Renderer *renderer) {
Widget::Draw(renderer);
/*auto image_dimensions = GetDimensions();
auto abs_pos = GetAbsolutePosition();
auto abs_size = GetAbsoluteSize();
//SDL_Rect source_rect = {0, 0, image_dimensions.x, image_dimensions.y};
SDL_Rect output_rect = {
static_cast<int>(abs_pos.x),
static_cast<int>(abs_pos.y),
static_cast<int>(abs_size.x),
static_cast<int>(abs_size.y)};
SDL_RenderCopy(renderer, this->texture, &source_rect, &output_rect);
*/
}
//Image::Image(SDL_Renderer* renderer, std::string path) {
//this->texture = IMG_LoadTexture(renderer, path.c_str());
//}
}; // UTF8 Causes Methamphetamine Addiction

View File

@@ -5,6 +5,11 @@ namespace JUI {
JUI::LayoutContainerWidget::LayoutContainerWidget() {}
JUI::LayoutContainerWidget::LayoutContainerWidget(Widget* parent) : LayoutContainerWidget() {
this->SetParent(parent);
}
JUI::LayoutContainerWidget::~LayoutContainerWidget() {}
void JUI::LayoutContainerWidget::Update(double delta) {

View File

@@ -8,7 +8,14 @@
namespace JUI {
Rect::Rect() : Widget() {}
Rect::Rect() : Widget() {
}
Rect::Rect(Widget* parent) : Rect()
{
this->SetParent(parent);
}
void Rect::Draw(SDL_Renderer* target) {
if (!visible)
@@ -31,7 +38,6 @@ namespace JUI {
SDL_RenderSetClipRect(target, &box);
}
// Draw Backgrounc
roundedBoxRGBA(target, box.x, box.y, box.x+box.w, box.y+box.h, border_radius, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
@@ -69,14 +75,11 @@ namespace JUI {
MouseButtonState state {};
state.LMB = mouse_btn_bitmask & SDL_BUTTON(1);
state.MMB = mouse_btn_bitmask & SDL_BUTTON(2);
state.RMB = mouse_btn_bitmask & SDL_BUTTON(3);
// std::cout << state << std::endl;
// TODO: Pull this out into some kind of InputService singleton
if (!prev_state.LMB && state.LMB && mouse_hover) {
was_clicked = true;
MousePressed.Invoke(MouseButtonID::LMB);
@@ -114,14 +117,13 @@ namespace JUI {
prev_state = state;
SDL_Rect bounds{
static_cast<int>(abs_pos.x),
static_cast<int>(abs_pos.y),
static_cast<int>(abs_size.x),
static_cast<int>(abs_size.y)};
if (SDL_PointInRect(&mouse_coords, &bounds)) {
if (IsMouseInside()) { //(SDL_PointInRect(&mouse_coords, &bounds)) {
if (!mouse_hover) {
const v2f fcoords = {mouse_coords.x, mouse_coords.y};
MouseEnter.Invoke(fcoords);
@@ -174,14 +176,14 @@ namespace JUI {
// TODO: FIX
bool Rect::IsMouseInside() const {
int *x, *y;
SDL_GetMouseState(x, y);
int x, y;
SDL_GetMouseState(&x, &y);
auto pos = GetAbsolutePosition();
auto size = GetAbsoluteSize();
if (*x > pos.x && *y > pos.y && *x < pos.x + size.x && *y < pos.y + size.y) {
if (x > pos.x && y > pos.y && x < pos.x + size.x && y < pos.y + size.y) {
return true;
}
return false;

View File

@@ -4,10 +4,23 @@
namespace JUI
{
Text::Text() : Widget() {
text_color = {255, 255, 255, 255};
text_outline = 1.f;
this->font = JUI::GetDefaultFont();
}
Text::~Text() {}
Text::Text(Widget* parent) : Text()
{
this->SetParent(parent);
}
Text::Text(Widget* parent, const std::string& text) : Text(parent)
{
this->SetText(text);
}
Text::~Text() = default;
v2f Text::GetAbsoluteSize() const
{ return this->GetParent()->GetAbsoluteSize(); }

View File

@@ -1,19 +0,0 @@
#include <JUI/Widgets/Texture.hpp>
#include <SDL_image.h>
namespace JUI {
Texture::Texture(SDL_Renderer*) {}
Texture::Texture(SDL_Renderer* renderer, std::string path) {
this->texture = IMG_LoadTexture(renderer, path.c_str());
}
}; // UTF8 Causes Methamphetamine Addiction

View File

@@ -29,9 +29,9 @@ namespace JUI {
}
Widget::Widget(const_ref<std::string> _name) : Widget() {
name = _name;
Widget::Widget(Widget *parent) : Widget()
{
this->SetParent(parent);
}
//Widget::~Widget() {}
@@ -309,4 +309,6 @@ namespace JUI {
return newChild;
}
}

View File

@@ -1,16 +1,25 @@
#include <JUI/common.h>
#include <SDL_image.h>
JUI::Font* default_font;
SDL_Texture* icon_checkmark;
JUI::Font* JUI::GetDefaultFont() {
return default_font;
}
SDL_Texture* JUI::GetCheckmark() {
return icon_checkmark;
}
void JUI::Load(SDL_Renderer *renderer) {
default_font = new Font(renderer, "FreeSans.ttf", 12);
icon_checkmark = IMG_LoadTexture(renderer, "check.png");
}
void JUI::Cleanup() {
SDL_DestroyTexture(icon_checkmark);
delete default_font;
}
@@ -38,3 +47,9 @@ JUI::v2i JUI::GetWindowSize(SDL_Window *window) {
SDL_GetWindowSize(window, &x, &y);
return {x, y};
}
JUI::v2i JUI::GetTextureDimensions(SDL_Texture *tex) {
v2i size{0, 0};
SDL_QueryTexture(tex, NULL, NULL, &size.x, &size.y);
return size;
}