mirror of
https://github.com/luanti-org/luanti.git
synced 2025-10-23 04:45:24 +02:00
TouchScreenGUI: Fix only 9 hotbar slots being usable (#13698)
Co-authored-by: Muhammad Rifqi Priyo Susanto <muhammadrifqipriyosusanto@gmail.com>
This commit is contained in:
@@ -583,25 +583,28 @@ touch_gui_button_id TouchScreenGUI::getButtonID(size_t eventID)
|
||||
return after_last_element_id;
|
||||
}
|
||||
|
||||
bool TouchScreenGUI::isHUDButton(const SEvent &event)
|
||||
bool TouchScreenGUI::isHotbarButton(const SEvent &event)
|
||||
{
|
||||
// check if hud item is pressed
|
||||
for (auto &hud_rect : m_hud_rects) {
|
||||
if (hud_rect.second.isPointInside(v2s32(event.TouchInput.X, event.TouchInput.Y))) {
|
||||
SEvent translated{};
|
||||
translated.EventType = irr::EET_KEY_INPUT_EVENT;
|
||||
translated.KeyInput.Key = (irr::EKEY_CODE) (KEY_KEY_1 + hud_rect.first);
|
||||
translated.KeyInput.Control = false;
|
||||
translated.KeyInput.Shift = false;
|
||||
translated.KeyInput.PressedDown = true;
|
||||
m_receiver->OnEvent(translated);
|
||||
m_hud_ids[event.TouchInput.ID] = translated.KeyInput.Key;
|
||||
const v2s32 touch_pos = v2s32(event.TouchInput.X, event.TouchInput.Y);
|
||||
// check if hotbar item is pressed
|
||||
for (auto &[index, rect] : m_hotbar_rects) {
|
||||
if (rect.isPointInside(touch_pos)) {
|
||||
// We can't just emit a keypress event because the number keys
|
||||
// range from 1 to 9, but there may be more hotbar items.
|
||||
m_hotbar_selection = index;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::optional<u16> TouchScreenGUI::getHotbarSelection()
|
||||
{
|
||||
auto selection = m_hotbar_selection;
|
||||
m_hotbar_selection = std::nullopt;
|
||||
return selection;
|
||||
}
|
||||
|
||||
void TouchScreenGUI::handleButtonEvent(touch_gui_button_id button,
|
||||
size_t eventID, bool action)
|
||||
{
|
||||
@@ -745,10 +748,10 @@ void TouchScreenGUI::translateEvent(const SEvent &event)
|
||||
handleButtonEvent(button, eventID, true);
|
||||
m_settings_bar.deactivate();
|
||||
m_rare_controls_bar.deactivate();
|
||||
} else if (isHUDButton(event)) {
|
||||
} else if (isHotbarButton(event)) {
|
||||
m_settings_bar.deactivate();
|
||||
m_rare_controls_bar.deactivate();
|
||||
// already handled in isHUDButton()
|
||||
// already handled in isHotbarButton()
|
||||
} else if (m_settings_bar.isButton(event)) {
|
||||
m_rare_controls_bar.deactivate();
|
||||
// already handled in isSettingsBarButton()
|
||||
@@ -1071,14 +1074,14 @@ void TouchScreenGUI::step(float dtime)
|
||||
m_rare_controls_bar.step(dtime);
|
||||
}
|
||||
|
||||
void TouchScreenGUI::resetHud()
|
||||
void TouchScreenGUI::resetHotbarRects()
|
||||
{
|
||||
m_hud_rects.clear();
|
||||
m_hotbar_rects.clear();
|
||||
}
|
||||
|
||||
void TouchScreenGUI::registerHudItem(int index, const rect<s32> &rect)
|
||||
void TouchScreenGUI::registerHotbarRect(u16 index, const rect<s32> &rect)
|
||||
{
|
||||
m_hud_rects[index] = rect;
|
||||
m_hotbar_rects[index] = rect;
|
||||
}
|
||||
|
||||
void TouchScreenGUI::setVisible(bool visible)
|
||||
|
@@ -24,8 +24,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
#include <IGUIEnvironment.h>
|
||||
#include <IrrlichtDevice.h>
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "client/tile.h"
|
||||
@@ -186,14 +187,16 @@ public:
|
||||
float getMovementSpeed() { return m_joystick_speed; }
|
||||
|
||||
void step(float dtime);
|
||||
void resetHud();
|
||||
void registerHudItem(int index, const rect<s32> &rect);
|
||||
inline void setUseCrosshair(bool use_crosshair) { m_draw_crosshair = use_crosshair; }
|
||||
|
||||
void setVisible(bool visible);
|
||||
void hide();
|
||||
void show();
|
||||
|
||||
void resetHotbarRects();
|
||||
void registerHotbarRect(u16 index, const rect<s32> &rect);
|
||||
std::optional<u16> getHotbarSelection();
|
||||
|
||||
private:
|
||||
bool m_initialized = false;
|
||||
IrrlichtDevice *m_device;
|
||||
@@ -203,10 +206,11 @@ private:
|
||||
v2u32 m_screensize;
|
||||
s32 button_size;
|
||||
double m_touchscreen_threshold;
|
||||
std::map<int, rect<s32>> m_hud_rects;
|
||||
std::map<size_t, EKEY_CODE> m_hud_ids;
|
||||
bool m_visible; // is the whole touch screen gui visible
|
||||
|
||||
std::unordered_map<u16, rect<s32>> m_hotbar_rects;
|
||||
std::optional<u16> m_hotbar_selection = std::nullopt;
|
||||
|
||||
// value in degree
|
||||
double m_camera_yaw_change = 0.0;
|
||||
double m_camera_pitch = 0.0;
|
||||
@@ -272,8 +276,8 @@ private:
|
||||
// handle a button event
|
||||
void handleButtonEvent(touch_gui_button_id bID, size_t eventID, bool action);
|
||||
|
||||
// handle pressed hud buttons
|
||||
bool isHUDButton(const SEvent &event);
|
||||
// handle pressing hotbar items
|
||||
bool isHotbarButton(const SEvent &event);
|
||||
|
||||
// do a right-click
|
||||
bool doRightClick();
|
||||
@@ -285,7 +289,7 @@ private:
|
||||
void applyJoystickStatus();
|
||||
|
||||
// array for saving last known position of a pointer
|
||||
std::map<size_t, v2s32> m_pointer_pos;
|
||||
std::unordered_map<size_t, v2s32> m_pointer_pos;
|
||||
|
||||
// settings bar
|
||||
AutoHideButtonBar m_settings_bar;
|
||||
|
Reference in New Issue
Block a user