Skip to content

Commit

Permalink
Merge pull request #76265 from ThePotatomancer/spell-info-scroll
Browse files Browse the repository at this point in the history
allow scrolling of spell info again
  • Loading branch information
Maleclypse authored Sep 8, 2024
2 parents 772b0e9 + 89ef7e3 commit 057c145
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 50 deletions.
34 changes: 34 additions & 0 deletions src/cata_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,40 @@ static void PushOrPopColor( const std::string_view seg, int minimumColorStackSiz
}
}

/**
* Scrolls the current ImGui window by a scroll action
*
* Setting scroll needs to happen before drawing contents for page scroll to work properly
* @param s an enum for the currently pending scroll action
*/
void cataimgui::set_scroll( scroll &s )
{
int scroll_px = 0;
int line_height = ImGui::GetTextLineHeightWithSpacing();
int page_height = ImGui::GetContentRegionAvail().y;

switch( s ) {
case scroll::none:
break;
case scroll::line_up:
scroll_px = -line_height;
break;
case scroll::line_down:
scroll_px = line_height;
break;
case scroll::page_up:
scroll_px = -page_height;
break;
case scroll::page_down:
scroll_px = page_height;
break;
}

ImGui::SetScrollY( ImGui::GetScrollY() + scroll_px );

s = scroll::none;
}

void cataimgui::draw_colored_text( std::string const &text, const nc_color &color,
float wrap_width, bool *is_selected, bool *is_focused, bool *is_hovered )
{
Expand Down
10 changes: 10 additions & 0 deletions src/cata_imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ enum class dialog_result {
NoClicked
};

enum class scroll : int {
none = 0,
line_up,
line_down,
page_up,
page_down
};

class client
{
std::vector<int> cata_input_trail;
Expand Down Expand Up @@ -87,6 +95,8 @@ void imvec2_to_point( ImVec2 *src, point *dest );

ImVec4 imvec4_from_color( nc_color &color );

void set_scroll( scroll &s );

void draw_colored_text( std::string const &text, const nc_color &color,
float wrap_width = 0.0F, bool *is_selected = nullptr,
bool *is_focused = nullptr, bool *is_hovered = nullptr );
Expand Down
13 changes: 8 additions & 5 deletions src/magic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2405,8 +2405,8 @@ static void reflesh_favorite( uilist *menu, std::vector<spell *> known_spells )
class spellcasting_callback : public uilist_callback
{
private:
int scroll_pos = 0;
std::vector<spell *> known_spells;
cataimgui::scroll spell_info_scroll = cataimgui::scroll::none;
void display_spell_info( size_t index );
public:
// invlets reserved for special functions
Expand Down Expand Up @@ -2442,11 +2442,13 @@ class spellcasting_callback : public uilist_callback
get_player_character().magic->rem_invlet( known_spells[entnum]->id() );
}
return true;
} else if( action == "SCROLL_UP_SPELL_MENU" || action == "SCROLL_DOWN_SPELL_MENU" ) {
scroll_pos += action == "SCROLL_DOWN_SPELL_MENU" ? 1 : -1;
} else if( action == "SCROLL_FAVORITE" ) {
get_player_character().magic->toggle_favorite( known_spells[entnum]->id() );
reflesh_favorite( menu, known_spells );
} else if( action == "SCROLL_UP_SPELL_MENU" ) {
spell_info_scroll = cataimgui::scroll::line_up;
} else if( action == "SCROLL_DOWN_SPELL_MENU" ) {
spell_info_scroll = cataimgui::scroll::line_down;
}
return false;
}
Expand Down Expand Up @@ -2568,11 +2570,12 @@ void spellcasting_callback::display_spell_info( size_t index )
const spell &sp = *known_spells[ index ];
Character &pc = get_player_character();

cataimgui::set_scroll( spell_info_scroll );
ImGui::TextColored( c_yellow, "%s", sp.spell_class() == trait_NONE ? _( "Classless" ) :
sp.spell_class()->name().c_str() );
// we remove 6 characteres from the width because there seems to be issues with wrapping in this menu (even with TextWrapped)
// TODO(thePotatomancer): investigate and fix the strange wrapping issues in this menu as well as other imgui menus
float spell_info_width = ImGui::GetContentRegionAvail().x - ( ImGui::CalcTextSize( " " ).x * 6 );
// TODO(thePotatomancer): investigate and fix the strange wrapping issues in this menu as well as oth er imgui menus
float spell_info_width = ImGui::GetContentRegionAvail().x - ( ImGui::CalcTextSize( " " ).x * 16 );
cataimgui::draw_colored_text( sp.description(), spell_info_width );
ImGui::NewLine();
cataimgui::draw_colored_text( sp.enumerate_spell_data( pc ), spell_info_width );
Expand Down
42 changes: 7 additions & 35 deletions src/ui_iteminfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,10 @@
#include "imgui/imgui_internal.h"


static void set_scroll( scroll &s )
void draw_item_info_imgui( cataimgui::window &window, item_info_data &data, int width,
cataimgui::scroll &s )
{
int scroll_px = 0;
int line_height = ImGui::GetTextLineHeightWithSpacing();
int page_height = ImGui::GetContentRegionAvail().y;

switch( s ) {
case scroll::none:
break;
case scroll::line_up:
scroll_px = -line_height;
break;
case scroll::line_down:
scroll_px = line_height;
break;
case scroll::page_up:
scroll_px = -page_height;
break;
case scroll::page_down:
scroll_px = page_height;
break;
}

ImGui::SetScrollY( ImGui::GetScrollY() + scroll_px );

s = scroll::none;
}

void draw_item_info_imgui( cataimgui::window &window, item_info_data &data, int width, scroll &s )
{
// Setting scroll needs to happen before drawing contents for page scroll to work properly
set_scroll( s );
cataimgui::set_scroll( s );

float wrap_width = window.str_width_to_pixels( width - 2 );
nc_color base_color = c_light_gray;
Expand Down Expand Up @@ -174,13 +146,13 @@ void iteminfo_window::execute()
std::string action = ctxt.handle_input();

if( data.handle_scrolling && data.arrow_scrolling && action == "UP" ) {
s = scroll::line_up;
s = cataimgui::scroll::line_up;
} else if( data.handle_scrolling && data.arrow_scrolling && action == "DOWN" ) {
s = scroll::line_down;
s = cataimgui::scroll::line_down;
} else if( data.handle_scrolling && action == "PAGE_UP" ) {
s = scroll::page_up;
s = cataimgui::scroll::page_up;
} else if( data.handle_scrolling && action == "PAGE_DOWN" ) {
s = scroll::page_down;
s = cataimgui::scroll::page_down;
} else if( action == "CONFIRM" || action == "QUIT" ||
( data.any_input && action == "ANY_INPUT" && !ctxt.get_raw_input().sequence.empty() ) ) {
break;
Expand Down
12 changes: 2 additions & 10 deletions src/ui_iteminfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@
#include "output.h"
#include "imgui/imgui.h"

enum class scroll : int {
none = 0,
line_up,
line_down,
page_up,
page_down
};

void draw_item_info_imgui( cataimgui::window &window, item_info_data &data, int width,
scroll &s );
cataimgui::scroll &s );

class iteminfo_window : public cataimgui::window
{
Expand All @@ -36,7 +28,7 @@ class iteminfo_window : public cataimgui::window
int height;
input_context ctxt;

scroll s;
cataimgui::scroll s;
};

#endif // CATA_SRC_UI_ITEMINFO_H

0 comments on commit 057c145

Please sign in to comment.