This commit is contained in:
hippoz 2022-07-06 03:43:19 +03:00
parent 72720d216d
commit 2423645222
No known key found for this signature in database
GPG key ID: 7C52899193467641
2 changed files with 66 additions and 0 deletions

33
src/ScrollContainer.cpp Normal file
View file

@ -0,0 +1,33 @@
#include "ScrollContainer.hpp"
#include "src/Styles.hpp"
#include <iostream>
#include <memory>
namespace Raven {
void ScrollContainer::on_layout() {
if (!m_target)
return;
m_target->rect().set_x(-m_scroll.x());
m_target->rect().set_y(-m_scroll.y());
}
std::shared_ptr<Widget> ScrollContainer::make_target() {
m_target = add<Raven::Widget>();
m_target->set_style(style());
m_target->rect().update();
return m_target;
}
void ScrollContainer::on_mouse_button(MouseButtonEvent &event) {
if (event.did_scroll_down()) {
set_scroll(Point(m_scroll.x(), m_scroll.y() + m_scroll_step));
} else if (event.did_scroll_up()) {
set_scroll(Point(m_scroll.x(), m_scroll.y() - m_scroll_step));
}
}
}

33
src/ScrollContainer.hpp Normal file
View file

@ -0,0 +1,33 @@
#pragma once
#include "Widget.hpp"
#include "src/Point.hpp"
#include <memory>
namespace Raven {
class ScrollContainer : public Widget {
public:
ScrollContainer()
: Widget() {}
void set_scroll(Point scroll) { m_scroll = scroll; reflow(); }
Point &scroll() { return m_scroll; }
std::shared_ptr<Widget> make_target();
void set_target(std::shared_ptr<Widget> target) { m_target = target; }
std::shared_ptr<Widget> target() { return m_target; }
void set_scroll_step(double scroll_step) { m_scroll_step = scroll_step; }
double scroll_step() { return m_scroll_step; }
protected:
void on_layout();
void on_mouse_button(MouseButtonEvent &event);
private:
double m_scroll_step { 5.0 };
Point m_scroll { 0, 0 };
std::shared_ptr<Widget> m_target { nullptr };
};
}