Moved mouse delta code to be more async

This commit is contained in:
Rebekah 2022-04-22 22:53:19 -04:00
parent 631f3b931d
commit 5f3c03a8e8
Signed by: oneechanhax
GPG Key ID: 183EB7902964DAE5
12 changed files with 36 additions and 31 deletions

View File

@ -40,8 +40,8 @@ public:
bool m_bKeysInit = false; bool m_bKeysInit = false;
int m_iMouseX; int m_iMouseX;
int m_iMouseY; int m_iMouseY;
int mouse_dx; [[deprecated]] int mouse_dx;
int mouse_dy; [[deprecated]] int mouse_dy;
bool fake_scroll = false; bool fake_scroll = false;
glez::rgba GetColor(); glez::rgba GetColor();

View File

@ -52,6 +52,7 @@ public:
virtual void OnKeyPress(CatKey key, bool repeat) override; virtual void OnKeyPress(CatKey key, bool repeat) override;
virtual void OnMouseEnter() override; virtual void OnMouseEnter() override;
virtual void OnMouseLeave() override; virtual void OnMouseLeave() override;
virtual void OnMouseMove(std::pair<int, int>) override;
virtual void Draw(int x, int y) override; virtual void Draw(int x, int y) override;
virtual void Update() override; virtual void Update() override;
virtual void MoveChildren() override; virtual void MoveChildren() override;

View File

@ -30,7 +30,7 @@ public:
Logo(IWidget*); Logo(IWidget*);
virtual bool AlwaysVisible() override; virtual bool AlwaysVisible() override;
virtual void Draw(int x, int y) override; virtual void Draw(int x, int y) override;
virtual void Update() override; virtual void OnMouseMove(std::pair<int, int>) override;
glez::texture texture; glez::texture texture;
}; };

View File

@ -49,6 +49,7 @@ public:
virtual void OnMouseLeave(); virtual void OnMouseLeave();
virtual void OnMousePress(); virtual void OnMousePress();
virtual void OnMouseRelease(); virtual void OnMouseRelease();
virtual void OnMouseMove(std::pair<int, int>);
virtual void Update(); virtual void Update();
virtual void HandleCustomEvent(std::string_view event); virtual void HandleCustomEvent(std::string_view event);

View File

@ -45,6 +45,7 @@ public:
inline virtual void OnMouseLeave() { this->hover = false; } inline virtual void OnMouseLeave() { this->hover = false; }
inline virtual void OnMousePress() { this->press = true; } inline virtual void OnMousePress() { this->press = true; }
inline virtual void OnMouseRelease() { this->press = false; } inline virtual void OnMouseRelease() { this->press = false; }
inline virtual void OnMouseMove(std::pair<int, int>) { }
inline virtual void OnKeyPress(CatKey key, bool repeat) {}; inline virtual void OnKeyPress(CatKey key, bool repeat) {};
inline virtual void OnKeyRelease(CatKey key) {}; inline virtual void OnKeyRelease(CatKey key) {};
inline virtual void OnFocusGain() { this->focus = true; } inline virtual void OnFocusGain() { this->focus = true; }

View File

@ -48,6 +48,7 @@ public:
virtual void OnMouseLeave() = 0; virtual void OnMouseLeave() = 0;
virtual void OnMousePress() = 0; virtual void OnMousePress() = 0;
virtual void OnMouseRelease() = 0; virtual void OnMouseRelease() = 0;
virtual void OnMouseMove(std::pair<int, int>) = 0;
virtual void OnKeyPress(CatKey key, bool repeat) = 0; virtual void OnKeyPress(CatKey key, bool repeat) = 0;
virtual void OnKeyRelease(CatKey key) = 0; virtual void OnKeyRelease(CatKey key) = 0;
virtual void OnFocusGain() = 0; virtual void OnFocusGain() = 0;

View File

@ -29,10 +29,8 @@ public:
CTitleBar(IWidget* parent, std::string title); CTitleBar(IWidget* parent, std::string title);
virtual void Draw(int x, int y); virtual void Draw(int x, int y);
virtual void OnMouseMove(std::pair<int, int>);
virtual void Update(); virtual void Update();
std::string m_strTitle; std::string m_strTitle;
int m_iDraggingStage;
int m_nLastX;
int m_nLastY;
}; };

View File

@ -152,8 +152,12 @@ void Canvas::Update() {
auto nmouse = input::GetMouse(); auto nmouse = input::GetMouse();
mouse_dx = nmouse.first - m_iMouseX; {
mouse_dy = nmouse.second - m_iMouseY; auto mouse_delta = std::make_pair(nmouse.first - m_iMouseX, nmouse.second - m_iMouseY);
mouse_dx = mouse_delta.first, mouse_dy = mouse_delta.second;
if (mouse_delta.first || mouse_delta.second)
this->OnMouseMove(mouse_delta);
}
m_iMouseX = nmouse.first; m_iMouseX = nmouse.first;
m_iMouseY = nmouse.second; m_iMouseY = nmouse.second;

View File

@ -267,14 +267,17 @@ void List::SetParent(IWidget* parent) {
root_list = this; root_list = this;
} }
void List::Update() { void List::OnMouseMove(std::pair<int, int> delta) {
CBaseContainer::Update();
if (IsPressed() && root_list == this) { if (IsPressed() && root_list == this) {
const auto& offset = root_list->GetOffset(); const auto& offset = root_list->GetOffset();
root_list->SetOffset(offset.first + this->GetCanvas()->mouse_dx, offset.second + this->GetCanvas()->mouse_dy); root_list->SetOffset(offset.first + delta.first, offset.second + delta.second);
} }
} }
void List::Update() {
CBaseContainer::Update();
}
void List::MoveChildren() { void List::MoveChildren() {
int accy = 2; int accy = 2;
int j = 0; int j = 0;

View File

@ -44,11 +44,11 @@ void Logo::Draw(int x, int y) {
glez::draw::rect_textured(x, y, embeded_logo_png_rgba.width, embeded_logo_png_rgba.height, this->GetCanvas()->GetColor(), this->texture, 0, 0, embeded_logo_png_rgba.width, embeded_logo_png_rgba.height, 0.0f); glez::draw::rect_textured(x, y, embeded_logo_png_rgba.width, embeded_logo_png_rgba.height, this->GetCanvas()->GetColor(), this->texture, 0, 0, embeded_logo_png_rgba.width, embeded_logo_png_rgba.height, 0.0f);
} }
void Logo::Update() { void Logo::OnMouseMove(std::pair<int, int> delta) {
if (this->IsPressed()) { if (this->IsPressed()) {
auto offset = GetOffset(); auto offset = GetOffset();
offset.first += this->GetCanvas()->mouse_dx; offset.first += delta.first;
offset.second += this->GetCanvas()->mouse_dy; offset.second += delta.second;
SetOffset(offset.first, offset.second); SetOffset(offset.first, offset.second);
} }
} }

View File

@ -185,6 +185,12 @@ void CBaseContainer::OnMouseRelease() {
GetPressedChild()->OnMouseRelease(); GetPressedChild()->OnMouseRelease();
} }
void CBaseContainer::OnMouseMove(std::pair<int, int> delta) {
CBaseWidget::OnMouseMove(delta);
for (auto* i : this->m_children)
i->OnMouseMove(delta);
}
void CBaseContainer::PressOn(IWidget* child) { void CBaseContainer::PressOn(IWidget* child) {
m_pPressedChild = child; m_pPressedChild = child;
if (child) { if (child) {

View File

@ -26,9 +26,6 @@
CTitleBar::CTitleBar(IWidget* parent, std::string title) CTitleBar::CTitleBar(IWidget* parent, std::string title)
: CBaseWidget("titlebar", parent) { : CBaseWidget("titlebar", parent) {
m_strTitle = title; m_strTitle = title;
m_iDraggingStage = 0;
m_nLastX = 0;
m_nLastY = 0;
SetPositionMode(ABSOLUTE); SetPositionMode(ABSOLUTE);
} }
@ -40,23 +37,16 @@ void CTitleBar::Draw(int x, int y) {
glez::draw::string(x + (size.first - l) / 2, y + TITLEBAR_PADDING_H, m_strTitle, this->GetCanvas()->GetFont(), glez::color::white, nullptr, nullptr); glez::draw::string(x + (size.first - l) / 2, y + TITLEBAR_PADDING_H, m_strTitle, this->GetCanvas()->GetFont(), glez::color::white, nullptr, nullptr);
} }
void CTitleBar::OnMouseMove(std::pair<int, int> delta) {
if (this->IsPressed()) {
auto offset = GetParent()->GetOffset();
GetParent()->SetOffset(offset.first + delta.first, offset.second + delta.second);
}
}
void CTitleBar::Update() { void CTitleBar::Update() {
auto psize = GetParent()->GetSize(); auto psize = GetParent()->GetSize();
float l, h; float l, h;
this->GetCanvas()->GetFont().stringSize(m_strTitle, &l, &h); this->GetCanvas()->GetFont().stringSize(m_strTitle, &l, &h);
SetSize(psize.first, 2 * TITLEBAR_PADDING_H + h); SetSize(psize.first, 2 * TITLEBAR_PADDING_H + h);
if (!IsPressed()) {
m_iDraggingStage = 0;
return;
}
if (m_iDraggingStage == 0) {
m_iDraggingStage = 1;
} else {
int dx = this->GetCanvas()->m_iMouseX - m_nLastX;
int dy = this->GetCanvas()->m_iMouseY - m_nLastY;
auto offset = GetParent()->GetOffset();
GetParent()->SetOffset(offset.first + dx, offset.second + dy);
}
m_nLastX = this->GetCanvas()->m_iMouseX;
m_nLastY = this->GetCanvas()->m_iMouseY;
} }