Created
September 26, 2016 08:52
-
-
Save usagi/e651d4761971f4165707b177fa1e49cd to your computer and use it in GitHub Desktop.
ImGui でタグクラウド風のボタンクラウドを実装する例 ref: http://qiita.com/usagi/items/f26be05cce73e4798d56
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace usagi | |
| { | |
| namespace imgui | |
| { | |
| class small_button_cloud | |
| : public std::multimap | |
| < std::string | |
| , std::function< auto () -> void > | |
| > | |
| { | |
| float _width = 0.0f; | |
| public: | |
| auto width() { return _width; } | |
| auto width( const decltype( _width ) in ) { return _width = in; } | |
| auto get_width_pointer() { return &_width; } | |
| auto operator()() | |
| { | |
| ImGui::BeginGroup(); | |
| const auto s = ImGui::GetStyle(); | |
| const auto iis2 = s.ItemInnerSpacing.x * 2; | |
| const auto is = s.ItemSpacing.x; | |
| auto current_line_width = 0.0f; | |
| for ( const auto& p : *this ) | |
| { | |
| const auto entity_width = ImGui::CalcTextSize( p.first.c_str() ).x; | |
| const auto tmp_line_width = current_line_width + is + iis2 + entity_width; | |
| if ( current_line_width > 0.0f and tmp_line_width < _width ) | |
| { | |
| ImGui::SameLine(); | |
| current_line_width = tmp_line_width; | |
| } | |
| else | |
| current_line_width = iis2 + entity_width; | |
| if ( ImGui::SmallButton( p.first.c_str() ) ) | |
| p.second(); | |
| } | |
| ImGui::EndGroup(); | |
| } | |
| }; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using usagi::imgui::small_button_cloud; | |
| static small_button_cloud c; | |
| ImGui::SliderFloat( "width", c.get_width_pointer(), 0.0f, 1024.0f ); | |
| if ( c.empty() ) | |
| for ( auto n = 0; n < 32; ++n ) | |
| c.emplace( "hoge-" + std::to_string( n ), [=]{ std::cerr << "hoge-" << n << '\n'; } ); | |
| c(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment