implement onTap and remove memory leaks
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -231,11 +231,7 @@ void Device::ReleaseTap(const SDL_MouseButtonEvent *event) const
|
|||||||
|
|
||||||
for (const auto &child : m_children)
|
for (const auto &child : m_children)
|
||||||
{
|
{
|
||||||
if (child->IsHit(static_cast<int>(event->x), static_cast<int>(event->y)))
|
child->ReleaseTap(static_cast<int>(event->x), static_cast<int>(event->y));
|
||||||
{
|
|
||||||
child->ReleaseTap(static_cast<int>(event->x), static_cast<int>(event->y));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -4,8 +4,6 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
auto pressed = false;
|
|
||||||
|
|
||||||
Button::Button(void *appState, const float x, const float y, const float width, std::function<void()> callback)
|
Button::Button(void *appState, const float x, const float y, const float width, std::function<void()> callback)
|
||||||
: UIWidget(appState), m_x(x), m_y(y), m_width(width), m_callback(std::move(callback))
|
: UIWidget(appState), m_x(x), m_y(y), m_width(width), m_callback(std::move(callback))
|
||||||
{
|
{
|
||||||
@@ -15,14 +13,16 @@ void Button::Render() const
|
|||||||
{
|
{
|
||||||
const auto button =
|
const auto button =
|
||||||
ResourceManager::Instance().GetTextureByName(GetContext()->MainRenderer(), "button_normal.png");
|
ResourceManager::Instance().GetTextureByName(GetContext()->MainRenderer(), "button_normal.png");
|
||||||
const auto overlay =
|
|
||||||
ResourceManager::Instance().GetTextureByName(GetContext()->MainRenderer(), "button_pressed_overlay.png");
|
|
||||||
|
|
||||||
const auto dst = SDL_FRect(m_x, m_y, m_width, m_width);
|
const auto dst = SDL_FRect(m_x, m_y, m_width, m_width);
|
||||||
SDL_RenderTexture(GetContext()->MainRenderer(), button, nullptr, &dst);
|
SDL_RenderTexture(GetContext()->MainRenderer(), button, nullptr, &dst);
|
||||||
|
SDL_DestroyTexture(button);
|
||||||
|
|
||||||
if (m_isPressed)
|
if (m_isPressed)
|
||||||
{
|
{
|
||||||
|
const auto overlay =
|
||||||
|
ResourceManager::Instance().GetTextureByName(GetContext()->MainRenderer(), "button_pressed_overlay.png");
|
||||||
SDL_RenderTexture(GetContext()->MainRenderer(), overlay, nullptr, &dst);
|
SDL_RenderTexture(GetContext()->MainRenderer(), overlay, nullptr, &dst);
|
||||||
|
SDL_DestroyTexture(overlay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -13,8 +13,38 @@ void D_Pad::Render() const
|
|||||||
const auto dPad =
|
const auto dPad =
|
||||||
ResourceManager::Instance().GetTextureByName(GetContext()->MainRenderer(), "d-pad_normal.png");
|
ResourceManager::Instance().GetTextureByName(GetContext()->MainRenderer(), "d-pad_normal.png");
|
||||||
|
|
||||||
const auto dst = SDL_FRect(m_x, m_y, m_width, m_width);
|
auto dst = SDL_FRect(m_x, m_y, m_width, m_width);
|
||||||
SDL_RenderTexture(GetContext()->MainRenderer(), dPad, nullptr, &dst);
|
SDL_RenderTexture(GetContext()->MainRenderer(), dPad, nullptr, &dst);
|
||||||
|
SDL_DestroyTexture(dPad);
|
||||||
|
|
||||||
|
if (m_isPressed != Direction::NONE)
|
||||||
|
{
|
||||||
|
const auto overlay =
|
||||||
|
ResourceManager::Instance().GetTextureByName(GetContext()->MainRenderer(), "button_pressed_overlay.png");
|
||||||
|
switch (m_isPressed)
|
||||||
|
{
|
||||||
|
case Direction::UP:
|
||||||
|
dst = SDL_FRect(m_x + m_width / 3.0f, m_y, m_width / 3.0f, m_width / 3.0f);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Direction::DOWN:
|
||||||
|
dst = SDL_FRect(m_x + m_width / 3.0f, m_y + 2 * m_width / 3.0f, m_width / 3.0f, m_width / 3.0f);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Direction::LEFT:
|
||||||
|
dst = SDL_FRect(m_x, m_y + m_width / 3.0f, m_width / 3.0f, m_width / 3.0f);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Direction::RIGHT:
|
||||||
|
dst = SDL_FRect(m_x + 2 * m_width / 3.0f, m_y + m_width / 3.0f, m_width / 3.0f, m_width / 3.0f);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Direction::NONE:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
SDL_RenderTexture(GetContext()->MainRenderer(), overlay, nullptr, &dst);
|
||||||
|
SDL_DestroyTexture(overlay);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool D_Pad::IsHit(const int mouse_x, const int mouse_y) const
|
bool D_Pad::IsHit(const int mouse_x, const int mouse_y) const
|
||||||
@@ -66,17 +96,17 @@ void D_Pad::OnTap(const int mouse_x, const int mouse_y)
|
|||||||
if (m_callback)
|
if (m_callback)
|
||||||
{
|
{
|
||||||
const auto local_x = static_cast<float>(mouse_x) - m_x;
|
const auto local_x = static_cast<float>(mouse_x) - m_x;
|
||||||
const auto local_y = static_cast<float>(mouse_y) - m_y;
|
|
||||||
|
|
||||||
if (local_x >= 0 && local_x <= m_width && local_y >= 0 && local_y <= m_width)
|
if (const auto local_y = static_cast<float>(mouse_y) - m_y;
|
||||||
|
local_x >= 0 && local_x <= m_width && local_y >= 0 && local_y <= m_width)
|
||||||
{
|
{
|
||||||
const auto dir = GetDirectionFromTap(local_x, local_y);
|
m_isPressed = GetDirectionFromTap(local_x, local_y);
|
||||||
m_callback(dir);
|
m_callback(m_isPressed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void D_Pad::ReleaseTap(const int mouse_x, const int mouse_y)
|
void D_Pad::ReleaseTap(const int mouse_x, const int mouse_y)
|
||||||
{
|
{
|
||||||
///
|
m_isPressed = Direction::NONE;
|
||||||
}
|
}
|
@@ -24,6 +24,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
float m_x, m_y, m_width;
|
float m_x, m_y, m_width;
|
||||||
std::function<void(Direction)> m_callback;
|
std::function<void(Direction)> m_callback;
|
||||||
|
Direction m_isPressed = Direction::NONE;
|
||||||
|
|
||||||
[[nodiscard]] Direction GetDirectionFromTap(float local_x, float local_y) const;
|
[[nodiscard]] Direction GetDirectionFromTap(float local_x, float local_y) const;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user