#pragma once #if defined(__cplusplus) #include #include "MiniFB.h" template void mfb_set_active_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, bool)); template void mfb_set_resize_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, int, int)); template void mfb_set_keyboard_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_key, mfb_key_mod, bool)); template void mfb_set_char_input_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, unsigned int)); template void mfb_set_mouse_button_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_mouse_button, mfb_key_mod, bool)); template void mfb_set_mouse_move_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, int, int)); template void mfb_set_mouse_scroll_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_key_mod, float, float)); //------------------------------------- // To avoid clumsy hands //------------------------------------- class mfb_stub { mfb_stub() : m_window(0x0) {} template friend void mfb_set_active_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, bool)); template friend void mfb_set_resize_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, int, int)); template friend void mfb_set_mouse_button_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_mouse_button, mfb_key_mod, bool)); template friend void mfb_set_keyboard_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_key, mfb_key_mod, bool)); template friend void mfb_set_char_input_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, unsigned int)); template friend void mfb_set_mouse_button_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_mouse_button, mfb_key_mod, bool)); template friend void mfb_set_mouse_move_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, int, int)); template friend void mfb_set_mouse_scroll_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *, mfb_key_mod, float, float)); static mfb_stub *GetInstance(struct mfb_window *window); static void active_stub(struct mfb_window *window, bool isActive); static void resize_stub(struct mfb_window *window, int width, int height); static void keyboard_stub(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed); static void char_input_stub(struct mfb_window *window, unsigned int); static void mouse_btn_stub(struct mfb_window *window, mfb_mouse_button button, mfb_key_mod mod, bool isPressed); static void mouse_move_stub(struct mfb_window *window, int x, int y); static void scroll_stub(struct mfb_window *window, mfb_key_mod mod, float deltaX, float deltaY); struct mfb_window *m_window; std::function m_active; std::function m_resize; std::function m_keyboard; std::function m_char_input; std::function m_mouse_btn; std::function m_mouse_move; std::function m_scroll; }; //------------------------------------- template inline void mfb_set_active_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, bool)) { using namespace std::placeholders; mfb_stub *stub = mfb_stub::GetInstance(window); stub->m_active = std::bind(method, obj, _1, _2); mfb_set_active_callback(window, mfb_stub::active_stub); } template inline void mfb_set_resize_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, int, int)) { using namespace std::placeholders; mfb_stub *stub = mfb_stub::GetInstance(window); stub->m_resize = std::bind(method, obj, _1, _2, _3); mfb_set_resize_callback(window, mfb_stub::resize_stub); } template inline void mfb_set_keyboard_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, mfb_key, mfb_key_mod, bool)) { using namespace std::placeholders; mfb_stub *stub = mfb_stub::GetInstance(window); stub->m_keyboard = std::bind(method, obj, _1, _2, _3, _4); mfb_set_keyboard_callback(window, mfb_stub::keyboard_stub); } template inline void mfb_set_char_input_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, unsigned int)) { using namespace std::placeholders; mfb_stub *stub = mfb_stub::GetInstance(window); stub->m_char_input = std::bind(method, obj, _1, _2); mfb_set_char_input_callback(window, mfb_stub::char_input_stub); } template inline void mfb_set_mouse_button_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, mfb_mouse_button, mfb_key_mod, bool)) { using namespace std::placeholders; mfb_stub *stub = mfb_stub::GetInstance(window); stub->m_mouse_btn = std::bind(method, obj, _1, _2, _3, _4); mfb_set_mouse_button_callback(window, mfb_stub::mouse_btn_stub); } template inline void mfb_set_mouse_move_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, int, int)) { using namespace std::placeholders; mfb_stub *stub = mfb_stub::GetInstance(window); stub->m_mouse_move = std::bind(method, obj, _1, _2, _3); mfb_set_mouse_move_callback(window, mfb_stub::mouse_move_stub); } template inline void mfb_set_mouse_scroll_callback(struct mfb_window *window, T *obj, void (T::*method)(struct mfb_window *window, mfb_key_mod, float, float)) { using namespace std::placeholders; mfb_stub *stub = mfb_stub::GetInstance(window); stub->m_scroll = std::bind(method, obj, _1, _2, _3, _4); mfb_set_mouse_scroll_callback(window, mfb_stub::scroll_stub); } #endif