Skip to content

Commit

Permalink
Fixing size of popups so only message_box is fixed-size
Browse files Browse the repository at this point in the history
  • Loading branch information
katemonster33 committed Dec 19, 2023
1 parent 21fcdf4 commit c7c8209
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 149 deletions.
105 changes: 58 additions & 47 deletions src/cata_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MouseInput>( 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<int, ImTui::mouse_event>( KEY_MOUSE, new_mouse_event ) );
Expand Down Expand Up @@ -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++ ) {
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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<ui_adaptor> window_adaptor;
public:
explicit window_impl( cataimgui::window *win ) {
win_base = win;
friend class cataimgui::window;
cataimgui::window *win_base;
bool is_resized;
std::unique_ptr<ui_adaptor> 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;
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 )
Expand Down Expand Up @@ -632,14 +632,16 @@ void cataimgui::popup::set_draw_callback( const std::function<bool()> &callback
void cataimgui::popup::draw()
{
ImGui::PushOverrideID( popup_id );
#if defined(TILES) || defined(WIN32)
ImGui::SetNextWindowSize( { 400, 0 } );
#else
ImGui::SetNextWindowSize( { 50, 0 } );
#endif
ImGui::SetNextWindowPos( ImVec2( ImGui::GetIO().DisplaySize.x * 0.5f,
ImGui::GetIO().DisplaySize.y * 0.5f ), ImGuiCond_Always, ImVec2( 0.5f, 0.5f ) );

auto bounds = get_bounds();
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( is_modal ) {
if( ImGui::BeginPopupModal( id.c_str(), &is_open, ImGuiWindowFlags_AlwaysAutoResize ) ) {
draw_controls();
Expand Down Expand Up @@ -707,6 +709,15 @@ void cataimgui::message_box::draw_mbox_btn( const std::string &text,
}
}

cataimgui::bounds cataimgui::message_box::get_bounds()
{
#if defined(TILES) || defined(WIN32)
return { -1, -1, 400, 0 };
#else
return { -1, -1, 50, 0 };
#endif
}

void cataimgui::message_box::draw_controls()
{
ImGui::Indent( 1.0f );
Expand Down
205 changes: 103 additions & 102 deletions src/cata_imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<class popup> active_popup;
std::vector<window *> 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<popup> &next_popup );
dialog_result show_popup( const std::shared_ptr<popup> &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<class popup> active_popup;
std::vector<window *> 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<popup> &next_popup );
dialog_result show_popup( const std::shared_ptr<popup> &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))
Expand All @@ -114,68 +114,69 @@ bool is_drag_drop_active();

class popup : public window
{
friend class window;
class popup_impl *p_impl;
bool is_modal;
std::function<bool()> on_draw_callback;
public:
popup( const std::string &id, bool is_modal );
popup( const std::string &id, bool is_modal, const std::function<bool()> &on_draw_callback );
~popup() override;

void draw() override;
void set_draw_callback( const std::function<bool()> &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<bool()> on_draw_callback;
public:
popup( const std::string &id, bool is_modal );
popup( const std::string &id, bool is_modal, const std::function<bool()> &on_draw_callback );
~popup() override;

void draw() override;
void set_draw_callback( const std::function<bool()> &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;
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<char, 100> 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<char, 100> 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<litem> &items );
int get_selected_index() const;
protected:
void draw_controls() override;
std::vector<litem> 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<litem> &items );
int get_selected_index() const;
protected:
void draw_controls() override;
std::vector<litem> items;
};
} // namespace cataimgui

Expand Down

0 comments on commit c7c8209

Please sign in to comment.