More Scroller Testing
This commit is contained in:
@@ -16,6 +16,7 @@ namespace JUI {
|
||||
}
|
||||
|
||||
/// A Rectangle Widget which has a larger renderable area than the visible area.
|
||||
/// This allows user-controlled scrolling of the viewable content.
|
||||
class JUI::ScrollingRect : public Rect {
|
||||
protected:
|
||||
bool vertical_scrollbar_enabled = true;
|
||||
@@ -24,15 +25,16 @@ protected:
|
||||
float scrollbar_width = 12;
|
||||
Color4 scrollbar_color = Colors::Whites::Azure;
|
||||
float scroll = 0;
|
||||
Vector2i canvas_size {256, 256};
|
||||
JGL::RenderTarget* canvas = nullptr;
|
||||
|
||||
protected:
|
||||
/* This isn't public because nothing should ever
|
||||
* have to do this from the outside. -Redacted */
|
||||
void RecomputeRenderTarget();
|
||||
public:
|
||||
JGL::RenderTarget* GetCanvas() { return canvas; }
|
||||
[[nodiscard]] Vector2i CanvasSize() const { return canvas_size; }
|
||||
float scroll_size = 0;
|
||||
JGL::RenderTarget* GetCanvas();
|
||||
[[nodiscard]] Vector2i CanvasSize() const;
|
||||
[[nodiscard]] float ScrollPos() const { return scroll; }
|
||||
[[nodiscard]] Vector2 CanvasPosition() const;
|
||||
// TODO scrolling in either direction. Assuming vertical scroll for now.
|
||||
@@ -46,16 +48,25 @@ public:
|
||||
void Update(float delta) override
|
||||
{
|
||||
//scroll += delta*5;
|
||||
canvas->Resize(Vector2i(GetAbsoluteSize().x, GetAbsoluteSize().y));
|
||||
//canvas->Resize(Vector2i(GetAbsoluteSize().x, GetAbsoluteSize().y));
|
||||
Rect::Update(delta);
|
||||
}
|
||||
|
||||
void ObserveKeyInput(Key key, bool pressed) override
|
||||
{
|
||||
if (key == Keys::UpArrow && pressed)
|
||||
{
|
||||
scroll -= 10;
|
||||
}
|
||||
if (key == Keys::DownArrow && pressed)
|
||||
{
|
||||
scroll += 10;
|
||||
}
|
||||
|
||||
}
|
||||
public:
|
||||
~ScrollingRect() override { delete canvas; }
|
||||
// TODO: ClearColor is no longer required to specify with new mcolor.
|
||||
ScrollingRect() : canvas(new RenderTarget(canvas_size, {0, 0, 0, 0})) {
|
||||
bool success = canvas->SetMSAAEnabled(JGL::MSAA_SAMPLE_RATE::MSAA_8X);
|
||||
};
|
||||
explicit ScrollingRect(Widget* parent) : Rect(parent), canvas(new RenderTarget(canvas_size, {0, 0, 0, 0})) {
|
||||
bool success = canvas->SetMSAAEnabled(JGL::MSAA_SAMPLE_RATE::MSAA_8X);
|
||||
};
|
||||
~ScrollingRect() override;
|
||||
|
||||
ScrollingRect();
|
||||
explicit ScrollingRect(Widget* parent);;
|
||||
};
|
23
main.cpp
23
main.cpp
@@ -32,6 +32,7 @@ JUI::Scene* scene;
|
||||
JGL::Texture* sample_texture;
|
||||
JGL::Texture* slicer;
|
||||
JUI::VerticalListLayout* list;
|
||||
JUI::ScrollingRect* scroller;
|
||||
|
||||
JUI::Scene* CreateScene() {
|
||||
using namespace JUI;
|
||||
@@ -132,7 +133,7 @@ JUI::Scene* CreateScene() {
|
||||
other_window->SetTitle("Another Window");
|
||||
|
||||
|
||||
auto* scroller = new JUI::ScrollingRect(other_window->GetViewportInstance());
|
||||
scroller = new JUI::ScrollingRect(other_window->GetViewportInstance());
|
||||
scroller->Size({100_percent, 100_percent});
|
||||
scroller->BGColor(Colors::Reds::LightCoral);
|
||||
|
||||
@@ -192,6 +193,9 @@ JUI::Scene* CreateScene() {
|
||||
return root;
|
||||
}
|
||||
|
||||
float accum = 0;
|
||||
int iter = 0;
|
||||
|
||||
class JUIDevelopmentTestWindow : public ReWindow::OpenGLWindow {
|
||||
public:
|
||||
|
||||
@@ -209,11 +213,22 @@ public:
|
||||
void Update(float elapsed)
|
||||
{
|
||||
using namespace JUI::UDimLiterals;
|
||||
|
||||
accum += elapsed;
|
||||
|
||||
scene->Update(elapsed);
|
||||
|
||||
auto* text = new JUI::TextRect(list);
|
||||
text->Size({50_percent, 20_px});
|
||||
text->SetContent(std::format("Elapsed: {}ms", Math::Floor(elapsed*1000.f)));
|
||||
if (accum > 1.f)
|
||||
{
|
||||
iter--;
|
||||
accum = 0.f;
|
||||
auto* text = new JUI::TextRect(list);
|
||||
text->Size({50_percent, 20_px});
|
||||
text->ZIndex(iter);
|
||||
text->SetContent(std::format("{} Sampled Delta: {}ms", -iter, Math::Floor(elapsed*1000.f)));
|
||||
scroller->scroll_size += 20;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@@ -11,20 +11,35 @@ void ScrollingRect::RecomputeRenderTarget() {
|
||||
}
|
||||
|
||||
void ScrollingRect::Draw() {
|
||||
//std::cout << "ScrollingRect::RecomputeRenderTarget " << scroll << std::endl;
|
||||
|
||||
// TODO: Only recompute when something has changed.
|
||||
// Q: How do we know when something has changed?
|
||||
RecomputeRenderTarget();
|
||||
//ScrollingRect::InnerDraw();
|
||||
//J2D::Begin();
|
||||
|
||||
//J2D::DrawRenderTarget(canvas, CanvasPosition());
|
||||
// TODO sub_texture_size would be the size of the visible area.
|
||||
J2D::DrawPartialRenderTarget(canvas, GetAbsolutePosition(), {0, scroll}, Vector2(canvas->GetDimensions()));
|
||||
//J2D::DrawRenderTarget(canvas, GetAbsolutePosition());
|
||||
J2D::DrawPartialRenderTarget(canvas, GetAbsolutePosition(), {0, 0}, Vector2(GetAbsoluteSize().x, GetAbsoluteSize().y));
|
||||
|
||||
bool canvas_larger_than_widget = scroll_size > GetAbsoluteSize().y;
|
||||
float ratio = scroll_size / GetAbsoluteSize().y;
|
||||
|
||||
if (vertical_scrollbar_enabled && canvas_larger_than_widget)
|
||||
{
|
||||
//std::cout << "Ratio " << ratio << std::endl;
|
||||
Vector2 vscroll_pos = GetAbsolutePosition() + Vector2(GetAbsoluteSize().x-scrollbar_width, 0);
|
||||
Vector2 vscroll_size = {scrollbar_width, GetAbsoluteSize().y-scrollbar_width};
|
||||
J2D::FillRect(Colors::LightGray, vscroll_pos, vscroll_size);
|
||||
J2D::FillRoundedRect(scrollbar_color, {vscroll_pos.x, vscroll_pos.y + scroll}, {vscroll_size.x, (vscroll_size.y / ratio)}, 4);
|
||||
|
||||
// Draw little square box at the point where the vertical and horizontal scrollbars would meet.
|
||||
Vector2 lil_box_size = {scrollbar_width, scrollbar_width};
|
||||
Vector2 lil_box_pos = GetAbsolutePosition() + GetAbsoluteSize() - lil_box_size;
|
||||
J2D::FillRect(Colors::DarkGray, lil_box_pos, lil_box_size);
|
||||
}
|
||||
|
||||
//J2D::End();
|
||||
}
|
||||
|
||||
Vector2 ScrollingRect::CanvasPosition() const {
|
||||
return {GetAbsolutePosition().x, GetAbsolutePosition().y + scroll};
|
||||
return {GetAbsolutePosition().x, GetAbsolutePosition().y - scroll};
|
||||
}
|
||||
|
||||
// Wouldn't inner draw actually render everything onto the RenderTarget?
|
||||
@@ -32,33 +47,41 @@ void ScrollingRect::InnerDraw() {
|
||||
|
||||
J2D::Begin(canvas, true);
|
||||
auto saved_pos = position;
|
||||
auto* saved_parent = parent;
|
||||
auto saved_size = size;
|
||||
|
||||
// OH MY GOD
|
||||
//Parent(nullptr);
|
||||
Position(UDim2::FromPixels(-GetAbsolutePosition().x, -GetAbsolutePosition().y));
|
||||
//Size({512_px, 512_px});
|
||||
|
||||
//std::cout << canvas.Get << std::endl;
|
||||
|
||||
// Kind of a hack to get the child widgets onto the rendertarget at the correct position.
|
||||
// We basically set the position for just long enough to lie to the child widgets.
|
||||
// The amount we offset by is the absolute position.
|
||||
// If you work the formulas out, the child widgets will believe their parent's AbsolutePosition is {0,0}.
|
||||
|
||||
Position(UDim2::FromPixels(-GetAbsolutePosition().x, -GetAbsolutePosition().y + scroll));
|
||||
Size({200_percent, 500_percent});
|
||||
|
||||
Rect::InnerDraw();
|
||||
DrawChildWidgets();
|
||||
|
||||
J2D::DrawPoint(Colors::Red, {1,1});
|
||||
J2D::DrawPoint(Colors::Red, Vector2(canvas_size));
|
||||
|
||||
//Parent(saved_parent);
|
||||
J2D::DrawPoint(Colors::Blue, {1,1}, 2);
|
||||
J2D::DrawPoint(Colors::Blue, Vector2(GetAbsoluteSize()), 2);
|
||||
|
||||
// Set our position back once we're done.
|
||||
Size(saved_size);
|
||||
Position(saved_pos);
|
||||
J2D::End();
|
||||
|
||||
//J2D::FillRect(bg_color, {0, 0}, {64, 64});
|
||||
//RectBase::Draw(canvas,bg_color, border_color, {0, 0}, {64, 64});
|
||||
//RectBase::Draw(canvas,bg_color, border_color, {0, 70}, {64, 64});
|
||||
//Widget::Draw();
|
||||
//J2D::End();
|
||||
}
|
||||
|
||||
const Vector2i default_initialize_canvas_size {1024, 4096};
|
||||
|
||||
ScrollingRect::~ScrollingRect() { delete canvas; }
|
||||
|
||||
ScrollingRect::ScrollingRect() : canvas(new RenderTarget(default_initialize_canvas_size)) {
|
||||
bool success = canvas->SetMSAAEnabled(JGL::MSAA_SAMPLE_RATE::MSAA_8X);
|
||||
}
|
||||
|
||||
ScrollingRect::ScrollingRect(Widget *parent) : Rect(parent), canvas(new RenderTarget(default_initialize_canvas_size)) {
|
||||
bool success = canvas->SetMSAAEnabled(JGL::MSAA_SAMPLE_RATE::MSAA_8X);
|
||||
}
|
||||
|
||||
JGL::RenderTarget *ScrollingRect::GetCanvas() { return canvas; }
|
||||
|
||||
Vector2i ScrollingRect::CanvasSize() const { return canvas->GetDimensions(); }
|
||||
|
Reference in New Issue
Block a user