Fix some memory leaks and code style issues

Maximum line length is 95 characters.
Some members' name are changed.
Struct initialisations use brace syntax; eliminating the usage of the memset function.
Iterations use for-each-loop instead of while-loop+iterator.
char * -> std::string
button_info * -> std::shared_ptr<button_info>
This commit is contained in:
Muhammad Rifqi Priyo Susanto 2023-06-28 14:00:00 +07:00
parent 8e09077de8
commit 3a47559e86
3 changed files with 380 additions and 422 deletions

@ -104,7 +104,7 @@ bool MyEventReceiver::OnEvent(const SEvent &event)
if (isMenuActive()) {
#ifdef HAVE_TOUCHSCREENGUI
if (m_touchscreengui) {
m_touchscreengui->Toggle(false);
m_touchscreengui->setVisible(false);
}
#endif
return g_menumgr.preprocessEvent(event);

File diff suppressed because it is too large Load Diff

@ -60,15 +60,6 @@ typedef enum
joystick_center_id
} touch_gui_button_id;
typedef enum
{
j_forward = 0,
j_backward,
j_left,
j_right,
j_aux1
} touch_gui_joystick_move_id;
typedef enum
{
AHBB_Dir_Top_Bottom,
@ -82,24 +73,24 @@ typedef enum
#define SETTINGS_BAR_Y_OFFSET 5
#define RARE_CONTROLS_BAR_Y_OFFSET 5
// Very slow button repeat frequency
#define SLOW_BUTTON_REPEAT 1.0f
extern const char *button_imagenames[];
extern const char *joystick_imagenames[];
extern const std::string button_image_names[];
extern const std::string joystick_image_names[];
struct button_info
{
float repeatcounter;
float repeatdelay;
irr::EKEY_CODE keycode;
float repeat_counter;
float repeat_delay;
EKEY_CODE keycode;
std::vector<size_t> ids;
IGUIButton *guibutton = nullptr;
IGUIButton *gui_button = nullptr;
bool immediate_release;
// 0: false, 1: (true) first texture, 2: (true) second texture
int togglable = 0;
std::vector<const char *> textures;
enum {
NOT_TOGGLEABLE,
FIRST_TEXTURE,
SECOND_TEXTURE
} toggleable = NOT_TOGGLEABLE;
std::vector<const std::string> textures;
};
class AutoHideButtonBar
@ -107,7 +98,7 @@ class AutoHideButtonBar
public:
AutoHideButtonBar(IrrlichtDevice *device, IEventReceiver *receiver);
void init(ISimpleTextureSource *tsrc, const char *starter_img, int button_id,
void init(ISimpleTextureSource *tsrc, const std::string &starter_img, int button_id,
const v2s32 &UpperLeft, const v2s32 &LowerRight,
autohide_button_bar_dir dir, float timeout);
@ -115,13 +106,13 @@ public:
// add button to be shown
void addButton(touch_gui_button_id id, const wchar_t *caption,
const char *btn_image);
const std::string &btn_image);
// add toggle button to be shown
void addToggleButton(touch_gui_button_id id, const wchar_t *caption,
const char *btn_image_1, const char *btn_image_2);
const std::string &btn_image_1, const std::string &btn_image_2);
// detect settings bar button events
// detect button bar button events
bool isButton(const SEvent &event);
// step handler
@ -130,13 +121,13 @@ public:
// return whether the button bar is active
bool active() { return m_active; }
// deactivate button bar
// deactivate the button bar
void deactivate();
// hide the whole buttonbar
// hide the whole button bar
void hide();
// unhide the buttonbar
// unhide the button bar
void show();
private:
@ -145,17 +136,16 @@ private:
IGUIEnvironment *m_guienv;
IEventReceiver *m_receiver;
button_info m_starter;
std::vector<button_info *> m_buttons;
std::vector<std::shared_ptr<button_info>> m_buttons;
v2s32 m_upper_left;
v2s32 m_lower_right;
// show settings bar
// show button bar
bool m_active = false;
bool m_visible = true;
// settings bar timeout
// button bar timeout
float m_timeout = 0.0f;
float m_timeout_value = 3.0f;
bool m_initialized = false;
@ -181,7 +171,7 @@ public:
double getPitch() { return m_camera_pitch; }
/*
/**
* Returns a line which describes what the player is pointing at.
* The starting point and looking direction are significant,
* the line should be scaled to match its length to the actual distance
@ -198,8 +188,8 @@ public:
void resetHud();
void registerHudItem(int index, const rect<s32> &rect);
inline void setUseCrosshair(bool use_crosshair) { m_draw_crosshair = use_crosshair; }
void Toggle(bool visible);
void setVisible(bool visible);
void hide();
void show();
@ -213,16 +203,15 @@ private:
s32 button_size;
double m_touchscreen_threshold;
std::map<int, rect<s32>> m_hud_rects;
std::map<size_t, irr::EKEY_CODE> m_hud_ids;
bool m_visible; // is the gui visible
std::map<size_t, EKEY_CODE> m_hud_ids;
bool m_visible; // is the whole touch screen gui visible
// value in degree
double m_camera_yaw_change = 0.0;
double m_camera_pitch = 0.0;
/*
* A line starting at the camera and pointing towards the
* selected object.
/**
* A line starting at the camera and pointing towards the selected object.
* The line ends on the camera's far plane.
* The coordinates do not contain the camera offset.
*/
@ -244,9 +233,9 @@ private:
bool m_fixed_joystick = false;
bool m_joystick_triggers_aux1 = false;
bool m_draw_crosshair = false;
button_info *m_joystick_btn_off = nullptr;
button_info *m_joystick_btn_bg = nullptr;
button_info *m_joystick_btn_center = nullptr;
std::shared_ptr<button_info> m_joystick_btn_off = nullptr;
std::shared_ptr<button_info> m_joystick_btn_bg = nullptr;
std::shared_ptr<button_info> m_joystick_btn_center = nullptr;
button_info m_buttons[after_last_element_id];
@ -265,7 +254,7 @@ private:
float repeat_delay = BUTTON_REPEAT_DELAY);
// initialize a joystick button
button_info *initJoystickButton(touch_gui_button_id id,
std::shared_ptr<button_info> initJoystickButton(touch_gui_button_id id,
const rect<s32> &button_rect, int texture_id,
bool visible = true);
@ -295,13 +284,13 @@ private:
void applyJoystickStatus();
// array for saving last known position of a pointer
std::map<size_t, v2s32> m_pointerpos;
std::map<size_t, v2s32> m_pointer_pos;
// settings bar
AutoHideButtonBar m_settingsbar;
AutoHideButtonBar m_settings_bar;
// rare controls bar
AutoHideButtonBar m_rarecontrolsbar;
AutoHideButtonBar m_rare_controls_bar;
};
extern TouchScreenGUI *g_touchscreengui;