From 78ddbedae90fd2e7c3bbc236f9324fb4d02b30e1 Mon Sep 17 00:00:00 2001 From: Kate M Date: Mon, 18 Dec 2023 23:31:32 -0500 Subject: [PATCH] Dealt with some suggestions. re-astyled some files --- src/cata_imgui.cpp | 84 +++++++++--------- src/cata_imgui.h | 206 ++++++++++++++++++++++---------------------- src/color.cpp | 2 +- src/main.cpp | 4 +- src/ncurses_def.cpp | 4 +- src/sdltiles.cpp | 6 +- src/ui_manager.cpp | 4 +- src/ui_manager.h | 2 +- 8 files changed, 156 insertions(+), 156 deletions(-) diff --git a/src/cata_imgui.cpp b/src/cata_imgui.cpp index 713508dedb571..04b39eb713a86 100644 --- a/src/cata_imgui.cpp +++ b/src/cata_imgui.cpp @@ -93,26 +93,26 @@ void cataimgui::client::process_input( void *input ) new_mouse_event.bstate = 0; for( int input_raw_key : curses_input->sequence ) { switch( static_cast( input_raw_key ) ) { - case MouseInput::LeftButtonPressed: - new_mouse_event.bstate |= BUTTON1_PRESSED; - break; - case MouseInput::LeftButtonReleased: - new_mouse_event.bstate |= BUTTON1_RELEASED; - break; - case MouseInput::RightButtonPressed: - new_mouse_event.bstate |= BUTTON3_PRESSED; - break; - case MouseInput::RightButtonReleased: - new_mouse_event.bstate |= BUTTON3_RELEASED; - break; - case MouseInput::ScrollWheelUp: - new_mouse_event.bstate |= BUTTON4_PRESSED; - break; - case MouseInput::ScrollWheelDown: - new_mouse_event.bstate |= BUTTON5_PRESSED; - break; - default: - break; + case MouseInput::LeftButtonPressed: + new_mouse_event.bstate |= BUTTON1_PRESSED; + break; + case MouseInput::LeftButtonReleased: + new_mouse_event.bstate |= BUTTON1_RELEASED; + break; + case MouseInput::RightButtonPressed: + new_mouse_event.bstate |= BUTTON3_PRESSED; + break; + case MouseInput::RightButtonReleased: + new_mouse_event.bstate |= BUTTON3_RELEASED; + break; + case MouseInput::ScrollWheelUp: + new_mouse_event.bstate |= BUTTON4_PRESSED; + break; + case MouseInput::ScrollWheelDown: + new_mouse_event.bstate |= BUTTON5_PRESSED; + break; + default: + break; } } imtui_events.push_back( std::pair( KEY_MOUSE, new_mouse_event ) ); @@ -316,7 +316,7 @@ int cataimgui::window::draw_item_info_data( item_info_data &data ) } // If type name is set, and not already contained in item name, output it too if( !data.get_type_name().empty() && - data.get_item_name().find( data.get_type_name() ) == std::string::npos ) { + data.get_item_name().find( data.get_type_name() ) == std::string::npos ) { buffer += data.get_type_name() + "\n"; } for( unsigned int i = 0; i < data.padding; i++ ) { @@ -373,8 +373,8 @@ int cataimgui::window::draw_item_info_data( item_info_data &data ) action = ctxt.handle_input(); if( action == "CONFIRM" || action == "QUIT" || - ( data.any_input && action == "ANY_INPUT" && - !ctxt.get_raw_input().sequence.empty() ) ) { + ( data.any_input && action == "ANY_INPUT" && + !ctxt.get_raw_input().sequence.empty() ) ) { break; } } @@ -408,23 +408,23 @@ bool cataimgui::window::is_child_window_navigated() class cataimgui::window_impl : public ui_adaptor { - friend class cataimgui::window; - cataimgui::window *win_base; - bool is_resized; - std::unique_ptr window_adaptor; -public: - explicit window_impl( cataimgui::window *win ) { - win_base = win; - is_resized = true; - window_adaptor.reset( new ui_adaptor() ); - window_adaptor->is_imgui = true; - window_adaptor->on_redraw( [this]( ui_adaptor & ) { - win_base->draw(); - } ); - window_adaptor->on_screen_resize( [this]( ui_adaptor & ) { + friend class cataimgui::window; + cataimgui::window *win_base; + bool is_resized; + std::unique_ptr window_adaptor; + public: + explicit window_impl( cataimgui::window *win ) { + win_base = win; is_resized = true; - } ); - } + window_adaptor.reset( new ui_adaptor() ); + window_adaptor->is_imgui = true; + window_adaptor->on_redraw( [this]( ui_adaptor & ) { + win_base->draw(); + } ); + window_adaptor->on_screen_resize( [this]( ui_adaptor & ) { + is_resized = true; + } ); + } }; cataimgui::window::window( int window_flags ) @@ -633,14 +633,14 @@ void cataimgui::popup::draw() { ImGui::PushOverrideID( popup_id ); auto bounds = get_bounds(); - if(bounds.x >=0 || bounds.y >= 0) { + if( bounds.x >= 0 || bounds.y >= 0 ) { ImGui::SetNextWindowPos( {bounds.x, bounds.y} ); } else { ImGui::SetNextWindowPos( ImVec2( ImGui::GetIO().DisplaySize.x * 0.5f, ImGui::GetIO().DisplaySize.y * 0.5f ), ImGuiCond_Always, ImVec2( 0.5f, 0.5f ) ); } - if(bounds.h >= 0 || bounds.w >= 0) { - ImGui::SetNextWindowSize({bounds.h, bounds.w}); + if( bounds.h >= 0 || bounds.w >= 0 ) { + ImGui::SetNextWindowSize( {bounds.h, bounds.w} ); } if( is_modal ) { if( ImGui::BeginPopupModal( id.c_str(), &is_open, ImGuiWindowFlags_AlwaysAutoResize ) ) { diff --git a/src/cata_imgui.h b/src/cata_imgui.h index d9d79f2c94fd3..c4701b58d0423 100644 --- a/src/cata_imgui.h +++ b/src/cata_imgui.h @@ -45,65 +45,65 @@ enum class text_align { class client { -public: - client(); - ~client(); + public: + client(); + ~client(); - void new_frame(); - void end_frame(); - void process_input( void *input ); + void new_frame(); + void end_frame(); + void process_input( void *input ); #if !(defined(TILES) || defined(WIN32)) - void upload_color_pair( int p, int f, int b ); - void set_alloced_pair_count( short count ); + void upload_color_pair( int p, int f, int b ); + void set_alloced_pair_count( short count ); #else - static struct SDL_Renderer *sdl_renderer; - static struct SDL_Window *sdl_window; + static struct SDL_Renderer *sdl_renderer; + static struct SDL_Window *sdl_window; #endif }; class window { - friend class child_window; - class window_impl *p_impl; - std::shared_ptr active_popup; - std::vector children; - window *parent; - bool open_popup_requested; - dialog_result last_popup_result; - bounds cached_bounds; -protected: - explicit window( int window_flags = 0 ); - explicit window( window *parent, int window_flags = 0 ); -public: - explicit window( const std::string &title, int window_flags = 0 ); - virtual ~window(); - void draw_colored_text( std::string const &text, const nc_color &color, - text_align alignment = text_align::Left, float max_width = 0.0F, bool *is_selected = nullptr, - bool *is_focused = nullptr, bool *is_hovered = nullptr ); - void draw_colored_text( std::string const &text, nc_color &color, - text_align alignment = text_align::Left, float max_width = 0.0F, bool *is_selected = nullptr, - bool *is_focused = nullptr, bool *is_hovered = nullptr ); - bool action_button( const std::string &action, const std::string &text ); - void draw_header( std::string const &text ); - bool get_is_open() const; - void set_title( const std::string &title ); - bool is_child_window_navigated(); - void show_popup_async( popup *next_popup ); - dialog_result show_popup( popup *next_popup ); - void show_popup_async( const std::shared_ptr &next_popup ); - dialog_result show_popup( const std::shared_ptr &next_popup ); - virtual void draw(); - bool is_resized(); - -protected: - bool is_open; - std::string id; - int window_flags; - virtual bounds get_bounds(); - virtual void draw_controls() = 0; - int draw_item_info_data( item_info_data &data ); - - void add_child( window *child ); + friend class child_window; + class window_impl *p_impl; + std::shared_ptr active_popup; + std::vector children; + window *parent; + bool open_popup_requested; + dialog_result last_popup_result; + bounds cached_bounds; + protected: + explicit window( int window_flags = 0 ); + explicit window( window *parent, int window_flags = 0 ); + public: + explicit window( const std::string &title, int window_flags = 0 ); + virtual ~window(); + void draw_colored_text( std::string const &text, const nc_color &color, + text_align alignment = text_align::Left, float max_width = 0.0F, bool *is_selected = nullptr, + bool *is_focused = nullptr, bool *is_hovered = nullptr ); + void draw_colored_text( std::string const &text, nc_color &color, + text_align alignment = text_align::Left, float max_width = 0.0F, bool *is_selected = nullptr, + bool *is_focused = nullptr, bool *is_hovered = nullptr ); + bool action_button( const std::string &action, const std::string &text ); + void draw_header( std::string const &text ); + bool get_is_open() const; + void set_title( const std::string &title ); + bool is_child_window_navigated(); + void show_popup_async( popup *next_popup ); + dialog_result show_popup( popup *next_popup ); + void show_popup_async( const std::shared_ptr &next_popup ); + dialog_result show_popup( const std::shared_ptr &next_popup ); + virtual void draw(); + bool is_resized(); + + protected: + bool is_open; + std::string id; + int window_flags; + virtual bounds get_bounds(); + virtual void draw_controls() = 0; + int draw_item_info_data( item_info_data &data ); + + void add_child( window *child ); }; #if !(defined(TILES) || defined(WIN32)) @@ -114,69 +114,69 @@ bool is_drag_drop_active(); class popup : public window { - friend class window; - class popup_impl *p_impl; - bool is_modal; - std::function on_draw_callback; -public: - popup( const std::string &id, bool is_modal ); - popup( const std::string &id, bool is_modal, const std::function &on_draw_callback ); - ~popup() override; - - void draw() override; - void set_draw_callback( const std::function &callback ); - void close(); - dialog_result get_result(); - bool is_draw_callback_set(); - -protected: - dialog_result result; - void open(); + friend class window; + class popup_impl *p_impl; + bool is_modal; + std::function on_draw_callback; + public: + popup( const std::string &id, bool is_modal ); + popup( const std::string &id, bool is_modal, const std::function &on_draw_callback ); + ~popup() override; + + void draw() override; + void set_draw_callback( const std::function &callback ); + void close(); + dialog_result get_result(); + bool is_draw_callback_set(); + + protected: + dialog_result result; + void open(); }; class message_box : public popup { - mbox_btn buttons; - std::string prompt; -public: - message_box( const std::string &title, const std::string &prompt, - mbox_btn buttons = mbox_btn::BT_OK ); - static dialog_result show( const std::string &title, const std::string &text ); -protected: - void draw_mbox_btn( const std::string &text, dialog_result result_if_clicked ); - void draw_controls() override; - bounds get_bounds() override; + mbox_btn buttons; + std::string prompt; + public: + message_box( const std::string &title, const std::string &prompt, + mbox_btn buttons = mbox_btn::BT_OK ); + static dialog_result show( const std::string &title, const std::string &text ); + protected: + void draw_mbox_btn( const std::string &text, dialog_result result_if_clicked ); + void draw_controls() override; + bounds get_bounds() override; }; class string_input_box : public popup { - std::string prompt; - std::array input; -public: - string_input_box( const std::string &title, const std::string &prompt ); - static dialog_result show( const std::string &prompt, std::string &input ); - std::string get_input(); -protected: - void draw_controls() override; + std::string prompt; + std::array input; + public: + string_input_box( const std::string &title, const std::string &prompt ); + static dialog_result show( const std::string &prompt, std::string &input ); + std::string get_input(); + protected: + void draw_controls() override; }; class list_selector : public popup { - std::string prompt; - int selected_index; -public: - struct litem { - std::string text; - bool is_enabled; - bool is_selected; - }; - - explicit list_selector( const std::string &id ); - void add( const litem &it ); - void add( std::initializer_list &items ); - int get_selected_index() const; -protected: - void draw_controls() override; - std::vector items; + std::string prompt; + int selected_index; + public: + struct litem { + std::string text; + bool is_enabled; + bool is_selected; + }; + + explicit list_selector( const std::string &id ); + void add( const litem &it ); + void add( std::initializer_list &items ); + int get_selected_index() const; + protected: + void draw_controls() override; + std::vector items; }; } // namespace cataimgui diff --git a/src/color.cpp b/src/color.cpp index 318ebd3f7fa7c..c9200935ea6b5 100644 --- a/src/color.cpp +++ b/src/color.cpp @@ -212,7 +212,7 @@ nc_color color_manager::highlight_from_names( const std::string &name, } #if !(defined(TILES) || defined(WIN32)) -extern cataimgui::client *imclient; +extern std::unique_ptr imclient; #endif void color_manager::load_default() diff --git a/src/main.cpp b/src/main.cpp index c9d6301f18921..ef4e9627010a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -60,7 +60,7 @@ # include // getpid() #endif -extern cataimgui::client *imclient; +extern std::unique_ptr imclient; #if defined(PREFIX) # undef PREFIX @@ -166,7 +166,7 @@ void exit_handler( int s ) } else #endif { - delete imclient; + imclient.reset(); exit( exit_status ); } } diff --git a/src/ncurses_def.cpp b/src/ncurses_def.cpp index 02bfef884142f..2dcb0de069754 100644 --- a/src/ncurses_def.cpp +++ b/src/ncurses_def.cpp @@ -36,7 +36,7 @@ #include "output.h" #include "ui_manager.h" -cataimgui::client *imclient = nullptr; +std::unique_ptr imclient; static void curses_check_result( const int result, const int expected, const char *const /*name*/ ) { @@ -336,7 +336,7 @@ void catacurses::init_interface() set_escdelay( 10 ); // Make Escape actually responsive // TODO: error checking start_color(); - imclient = new cataimgui::client(); + imclient = std::make_unique(); init_colors(); #if !defined(__CYGWIN__) // ncurses mouse registration diff --git a/src/sdltiles.cpp b/src/sdltiles.cpp index 336c987bf77c1..b9b6e908cb1f8 100644 --- a/src/sdltiles.cpp +++ b/src/sdltiles.cpp @@ -74,7 +74,7 @@ #include "wcwidth.h" #include "cata_imgui.h" -cataimgui::client *imclient = nullptr; +std::unique_ptr imclient; #if defined(__linux__) # include // getenv()/setenv() @@ -433,7 +433,7 @@ static void WinCreate() cataimgui::client::sdl_renderer = renderer.get(); cataimgui::client::sdl_window = window.get(); - imclient = new cataimgui::client(); + imclient = std::make_unique(); //io.Fonts->AddFontDefault(); //io.Fonts->Build(); @@ -444,7 +444,7 @@ static void WinDestroy() #if defined(__ANDROID__) touch_joystick.reset(); #endif - delete imclient; + imclient.reset(); shutdown_sound(); tilecontext.reset(); gamepad::quit(); diff --git a/src/ui_manager.cpp b/src/ui_manager.cpp index d3335d1cecf72..fb4a59bb4da90 100644 --- a/src/ui_manager.cpp +++ b/src/ui_manager.cpp @@ -335,7 +335,7 @@ void ui_adaptor::redraw() redraw_invalidated(); } -extern cataimgui::client *imclient; +extern std::unique_ptr imclient; void ui_adaptor::redraw_invalidated( bool draw_imgui ) { @@ -440,7 +440,7 @@ void ui_adaptor::redraw_invalidated( bool draw_imgui ) ui.invalidated = false; } } - if( ui.is_imgui && draw_imgui && ( !ui.is_imgui || imgui_is_on_top ) ) { + if( ui.is_imgui && draw_imgui && imgui_is_on_top ) { if( ui.redraw_cb ) { ui.redraw_cb( ui ); } diff --git a/src/ui_manager.h b/src/ui_manager.h index 51562c5e3734e..36faef3090a1c 100644 --- a/src/ui_manager.h +++ b/src/ui_manager.h @@ -98,7 +98,7 @@ class ui_adaptor explicit ui_adaptor( debug_message_ui ); ui_adaptor( const ui_adaptor &rhs ) = delete; ui_adaptor( ui_adaptor &&rhs ) = delete; - virtual ~ui_adaptor(); + ~ui_adaptor(); ui_adaptor &operator=( const ui_adaptor &rhs ) = delete; ui_adaptor &operator=( ui_adaptor &&rhs ) = delete;