diff options
-rw-r--r-- | include/Body.hpp | 5 | ||||
-rw-r--r-- | include/QuickMedia.hpp | 1 | ||||
-rw-r--r-- | include/SearchBar.hpp | 13 | ||||
-rw-r--r-- | src/Body.cpp | 60 | ||||
-rw-r--r-- | src/QuickMedia.cpp | 234 | ||||
-rw-r--r-- | src/SearchBar.cpp | 64 |
6 files changed, 189 insertions, 188 deletions
diff --git a/include/Body.hpp b/include/Body.hpp index 39ad19d..b50b695 100644 --- a/include/Body.hpp +++ b/include/Body.hpp @@ -218,7 +218,7 @@ namespace QuickMedia { void clamp_selection(); // Returns true if the event was handled - bool on_event(const sf::RenderWindow &window, const sf::Event &event); + bool on_event(const sf::RenderWindow &window, const sf::Event &event, bool keyboard_navigation = true); void draw(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size); void draw(sf::RenderWindow &window, sf::Vector2f pos, sf::Vector2f size, const Json::Value &content_progress); // |size| is the clip size, another outside this will be cut off. @@ -262,6 +262,9 @@ namespace QuickMedia { sf::Shader *thumbnail_mask_shader; AttachSide attach_side = AttachSide::TOP; bool title_mark_urls = false; + + std::function<void()> on_top_reached = nullptr; + std::function<void()> on_bottom_reached = nullptr; private: void draw_item(sf::RenderWindow &window, BodyItem *item, const sf::Vector2f &pos, const sf::Vector2f &size, const float item_height, const int item_index, const Json::Value &content_progress, bool include_embedded_item = true, bool merge_with_previous = false); void update_dirty_state(BodyItem *body_item, float width); diff --git a/include/QuickMedia.hpp b/include/QuickMedia.hpp index 5d5f613..30836aa 100644 --- a/include/QuickMedia.hpp +++ b/include/QuickMedia.hpp @@ -66,6 +66,7 @@ namespace QuickMedia { FetchType fetch_type; bool typing = false; bool fetching_next_page_running = false; + bool fetching_next_page_failed = false; int fetched_page = 0; sf::Text search_result_text; AsyncTask<FetchResult> fetch_future; diff --git a/include/SearchBar.hpp b/include/SearchBar.hpp index 06c4c98..cad897f 100644 --- a/include/SearchBar.hpp +++ b/include/SearchBar.hpp @@ -23,7 +23,7 @@ namespace QuickMedia { class SearchBar { public: SearchBar(sf::Texture *plugin_logo, sf::Shader *rounded_rectangle_shader, const std::string &placeholder, bool input_masked = false); - void draw(sf::RenderWindow &window, bool draw_shadow = true); + void draw(sf::RenderWindow &window, sf::Vector2f size, bool draw_background); void on_event(sf::Event &event); void update(); void onWindowResize(const sf::Vector2f &window_size); @@ -32,7 +32,7 @@ namespace QuickMedia { bool is_cursor_at_start_of_line() const; void set_to_autocomplete(); void set_autocomplete_text(const std::string &text); - void set_vertical_position(float vertical_pos); + void set_position(sf::Vector2f pos); float getBottom() const; float getBottomWithoutShadow() const; @@ -46,6 +46,10 @@ namespace QuickMedia { int text_autosearch_delay; int autocomplete_search_delay; bool caret_visible; + + float padding_top = 0.0f; + float padding_bottom = 0.0f; + float padding_x = 10.0f; private: void onTextEntered(sf::Uint32 codepoint); void clear_autocomplete_if_text_not_substring(); @@ -54,7 +58,6 @@ namespace QuickMedia { sf::Text text; sf::Text autocomplete_text; RoundedRectangle background; - sf::RectangleShape background_shadow; sf::RectangleShape shade; sf::RectangleShape caret; sf::Sprite plugin_logo_sprite; @@ -68,8 +71,8 @@ namespace QuickMedia { bool typing; bool backspace_pressed; bool mouse_left_inside; - float vertical_pos; + sf::Vector2f pos; sf::Clock time_since_search_update; - sf::Vector2u prev_window_size; + sf::Vector2f prev_size; }; }
\ No newline at end of file diff --git a/src/Body.cpp b/src/Body.cpp index be9d1fd..98f08e8 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -399,7 +399,41 @@ namespace QuickMedia { } } - bool Body::on_event(const sf::RenderWindow &window, const sf::Event &event) { + bool Body::on_event(const sf::RenderWindow &window, const sf::Event &event, bool keyboard_navigation) { + if(keyboard_navigation && event.type == sf::Event::KeyPressed) { + if(event.key.code == sf::Keyboard::Up || (event.key.control && event.key.code == sf::Keyboard::K)) { + bool top_reached = select_previous_item(); + if(!top_reached && on_top_reached) + on_top_reached(); + return true; + } else if(event.key.code == sf::Keyboard::Down || (event.key.control && event.key.code == sf::Keyboard::J)) { + bool bottom_reached = select_next_item(); + if(!bottom_reached && on_bottom_reached) + on_bottom_reached(); + return true; + } else if(event.key.code == sf::Keyboard::Home) { + select_first_item(); + if(on_top_reached) + on_top_reached(); + return true; + } else if(event.key.code == sf::Keyboard::End) { + select_last_item(); + if(on_bottom_reached) + on_bottom_reached(); + return true; + } else if(event.key.code == sf::Keyboard::PageUp) { + bool top_reached = select_previous_page(); + if(!top_reached && on_top_reached) + on_top_reached(); + return true; + } else if(event.key.code == sf::Keyboard::PageDown) { + bool bottom_reached = select_next_page(); + if(!bottom_reached && on_bottom_reached) + on_bottom_reached(); + return true; + } + } + if(event.type == sf::Event::Resized) clamp_selected_item_to_body_count = 1; @@ -488,9 +522,33 @@ namespace QuickMedia { if(mouse_scroll_accel.y > 0.1 && first_fully_visible_item != -1) { selected_item = first_fully_visible_item; clamp_selection(); + // TODO: Cache this + if(on_top_reached) { + int first_visible_item = -1; + for(int i = 0; i <= selected_item; ++i) { + if(items[i]->visible) { + first_visible_item = i; + break; + } + } + if(first_visible_item == first_fully_visible_item) + on_top_reached(); + } } else if(mouse_scroll_accel.y < -0.1 && last_fully_visible_item != -1) { selected_item = last_fully_visible_item; clamp_selection(); + // TODO: Cache this + if(on_bottom_reached) { + int last_visible_item = -1; + for(int i = items.size() - 1; i >= selected_item; --i) { + if(items[i]->visible) { + last_visible_item = i; + break; + } + } + if(last_visible_item == last_fully_visible_item) + on_bottom_reached(); + } } if(!mouse_left_pressed) { diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index abfbc7c..6a7bc5f 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -598,6 +598,9 @@ namespace QuickMedia { show_room_side_panel = true; main_thread_id = std::this_thread::get_id(); + auto window_size_u = window.getSize(); + window_size.x = window_size_u.x; + window_size.y = window_size_u.y; } // Returns size_t(-1) if not found @@ -1077,19 +1080,7 @@ namespace QuickMedia { sf::FloatRect visible_area(0, 0, window_size.x, window_size.y); window.setView(sf::View(visible_area)); } else if(handle_keypress && event.type == sf::Event::KeyPressed) { - if(event.key.code == sf::Keyboard::Up || (event.key.control && event.key.code == sf::Keyboard::K)) { - body->select_previous_item(); - } else if(event.key.code == sf::Keyboard::Down || (event.key.control && event.key.code == sf::Keyboard::J)) { - body->select_next_item(); - } else if(event.key.code == sf::Keyboard::PageUp) { - body->select_previous_page(); - } else if(event.key.code == sf::Keyboard::PageDown) { - body->select_next_page(); - } else if(event.key.code == sf::Keyboard::Home) { - body->select_first_item(); - } else if(event.key.code == sf::Keyboard::End) { - body->select_last_item(); - } else if(event.key.code == sf::Keyboard::Escape) { + if(event.key.code == sf::Keyboard::Escape) { current_page = previous_page; } } else if(handle_searchbar) { @@ -1344,7 +1335,7 @@ namespace QuickMedia { } void Program::page_loop_render(sf::RenderWindow &window, std::vector<Tab> &tabs, int selected_tab, TabAssociatedData &tab_associated_data, const Json::Value *json_chapters, Tabs &ui_tabs) { - if(tabs[selected_tab].search_bar) tabs[selected_tab].search_bar->draw(window, false); + if(tabs[selected_tab].search_bar) tabs[selected_tab].search_bar->draw(window, window_size, true); float shade_extra_height = 0.0f; if(!tabs[selected_tab].search_bar) { @@ -1436,9 +1427,7 @@ namespace QuickMedia { window_size.x = window_size_u.x; window_size.y = window_size_u.y; - std::function<void(const std::string&)> submit_handler; - - submit_handler = [this, &submit_handler, &after_submit_handler, &tabs, &tab_associated_data, &ui_tabs, &loop_running, &redraw](const std::string &search_text) { + std::function<void(const std::string&)> submit_handler = [this, &submit_handler, &after_submit_handler, &tabs, &tab_associated_data, &ui_tabs, &loop_running, &redraw](const std::string &search_text) { const int selected_tab = ui_tabs.get_selected(); auto selected_item = tabs[selected_tab].body->get_selected_shared(); if(!selected_item && !tabs[selected_tab].page->allow_submit_no_selection()) @@ -1591,6 +1580,28 @@ namespace QuickMedia { hide_virtual_keyboard(); }; + std::function<void()> on_bottom_reached = [&ui_tabs, &tabs, &tab_associated_data, &gradient_inc] { + const int selected_tab = ui_tabs.get_selected(); + if(tab_associated_data[selected_tab].fetch_status == FetchStatus::NONE + && !tab_associated_data[selected_tab].fetching_next_page_running + && !tab_associated_data[selected_tab].fetching_next_page_failed + && tabs[selected_tab].page + && (!tabs[selected_tab].page->is_lazy_fetch_page() || tab_associated_data[selected_tab].lazy_fetch_finished)) + { + gradient_inc = 0.0; + tab_associated_data[selected_tab].fetching_next_page_running = true; + int next_page = tab_associated_data[selected_tab].fetched_page + 1; + Page *page = tabs[selected_tab].page.get(); + std::string update_search_text = tab_associated_data[selected_tab].update_search_text; + tab_associated_data[selected_tab].next_page_future = AsyncTask<BodyItems>([update_search_text, next_page, page]() { + BodyItems result_items; + if(page->get_page(update_search_text, next_page, result_items) != PluginResult::OK) + fprintf(stderr, "Failed to get next page (page %d)\n", next_page); + return result_items; + }); + } + }; + for(size_t i = 0; i < tabs.size(); ++i) { Tab &tab = tabs[i]; tab.body->body_item_select_callback = [&submit_handler](BodyItem *body_item) { @@ -1623,6 +1634,8 @@ namespace QuickMedia { return; submit_handler(search_text); }; + + tab.body->on_bottom_reached = on_bottom_reached; } sf::Event event; @@ -1655,51 +1668,7 @@ namespace QuickMedia { if(event.type == sf::Event::Resized || event.type == sf::Event::GainedFocus) redraw = true; else if(event.type == sf::Event::KeyPressed) { - if(event.key.code == sf::Keyboard::Down || event.key.code == sf::Keyboard::PageDown || event.key.code == sf::Keyboard::End || (event.key.control && event.key.code == sf::Keyboard::J)) { - bool hit_bottom = false; - switch(event.key.code) { - case sf::Keyboard::Down: - hit_bottom = !tabs[selected_tab].body->select_next_item(); - break; - case sf::Keyboard::J: - hit_bottom = !tabs[selected_tab].body->select_next_item(); - break; - case sf::Keyboard::PageDown: - hit_bottom = !tabs[selected_tab].body->select_next_page(); - break; - case sf::Keyboard::End: - tabs[selected_tab].body->select_last_item(); - hit_bottom = true; - break; - default: - hit_bottom = false; - break; - } - if(hit_bottom - && tab_associated_data[selected_tab].fetch_status == FetchStatus::NONE - && !tab_associated_data[selected_tab].fetching_next_page_running - && tabs[selected_tab].page - && (!tabs[selected_tab].page->is_lazy_fetch_page() || tab_associated_data[selected_tab].lazy_fetch_finished)) - { - gradient_inc = 0.0; - tab_associated_data[selected_tab].fetching_next_page_running = true; - int next_page = tab_associated_data[selected_tab].fetched_page + 1; - Page *page = tabs[selected_tab].page.get(); - std::string update_search_text = tab_associated_data[selected_tab].update_search_text; - tab_associated_data[selected_tab].next_page_future = AsyncTask<BodyItems>([update_search_text, next_page, page]() { - BodyItems result_items; - if(page->get_page(update_search_text, next_page, result_items) != PluginResult::OK) - fprintf(stderr, "Failed to get next page (page %d)\n", next_page); - return result_items; - }); - } - } else if(event.key.code == sf::Keyboard::Up || (event.key.control && event.key.code == sf::Keyboard::K)) { - tabs[selected_tab].body->select_previous_item(); - } else if(event.key.code == sf::Keyboard::PageUp) { - tabs[selected_tab].body->select_previous_page(); - } else if(event.key.code == sf::Keyboard::Home) { - tabs[selected_tab].body->select_first_item(); - } else if(event.key.code == sf::Keyboard::Escape) { + if(event.key.code == sf::Keyboard::Escape) { goto page_end; } else if(event.key.code == sf::Keyboard::Tab) { if(tabs[selected_tab].search_bar) tabs[selected_tab].search_bar->set_to_autocomplete(); @@ -1796,6 +1765,8 @@ namespace QuickMedia { if(num_new_messages > 0) { tabs[i].body->append_items(std::move(new_body_items)); associated_data.fetched_page++; + } else { + associated_data.fetching_next_page_failed = true; } associated_data.fetching_next_page_running = false; } @@ -3136,19 +3107,7 @@ namespace QuickMedia { if(event.type == sf::Event::Resized || event.type == sf::Event::GainedFocus) redraw = true; else if(navigation_stage == NavigationStage::VIEWING_COMMENTS && event.type == sf::Event::KeyPressed) { - if(event.key.code == sf::Keyboard::Up || (event.key.control && event.key.code == sf::Keyboard::K)) { - thread_body->select_previous_item(); - } else if(event.key.code == sf::Keyboard::Down || (event.key.control && event.key.code == sf::Keyboard::J)) { - thread_body->select_next_item(); - } else if(event.key.code == sf::Keyboard::PageUp) { - thread_body->select_previous_page(); - } else if(event.key.code == sf::Keyboard::PageDown) { - thread_body->select_next_page(); - } else if(event.key.code == sf::Keyboard::Home) { - thread_body->select_first_item(); - } else if(event.key.code == sf::Keyboard::End) { - thread_body->select_last_item(); - } else if(event.key.code == sf::Keyboard::Escape) { + if(event.key.code == sf::Keyboard::Escape) { current_page = pop_page_stack(); } else if(event.key.code == sf::Keyboard::P) { BodyItem *selected_item = thread_body->get_selected(); @@ -3513,35 +3472,43 @@ namespace QuickMedia { SearchBar password_input(nullptr, &rounded_rectangle_shader, "Password", true); SearchBar homeserver_input(nullptr, &rounded_rectangle_shader, "Homeserver"); - sf::Text status_text("", *FontLoader::get_font(FontLoader::FontType::LATIN), std::floor(18 * get_ui_scale())); - const int num_inputs = 3; SearchBar *inputs[num_inputs] = { &login_input, &password_input, &homeserver_input }; int focused_input = 0; - auto text_submit_callback = [this, inputs, &status_text](const sf::String&) { + RoundedRectangle background(sf::Vector2f(1.0f, 1.0f), 10.0f, sf::Color(33, 37, 44), &rounded_rectangle_shader); + + auto text_submit_callback = [this, inputs](const sf::String&) { for(int i = 0; i < num_inputs; ++i) { if(inputs[i]->get_text().empty()) { - status_text.setString("All fields need to be filled in"); + show_notification("QuickMedia", "All fields need to be filled in", Urgency::CRITICAL); return; } } - std::string err_msg; - // TODO: Make asynchronous - if(matrix->login(inputs[0]->get_text(), inputs[1]->get_text(), inputs[2]->get_text(), err_msg) == PluginResult::OK) { - current_page = PageType::CHAT; - } else { - status_text.setString("Failed to login, error: " + err_msg); - } - return; + run_task_with_loading_screen([this, inputs](){ + std::string err_msg; + if(matrix->login(inputs[0]->get_text(), inputs[1]->get_text(), inputs[2]->get_text(), err_msg) == PluginResult::OK) { + current_page = PageType::CHAT; + return true; + } else { + show_notification("QuickMedia", "Failed to login, error: " + err_msg, Urgency::CRITICAL); + return false; + } + }); }; for(int i = 0; i < num_inputs; ++i) { + inputs[i]->padding_top = 0.0f; + inputs[i]->padding_bottom = 0.0f; + inputs[i]->padding_x = 0.0f; inputs[i]->caret_visible = false; inputs[i]->onTextSubmitCallback = text_submit_callback; } inputs[focused_input]->caret_visible = true; + const float padding_x = std::floor(20.0f * get_ui_scale()); + const float padding_y = std::floor(20.0f * get_ui_scale()); + const float spacing_y = std::floor(20.0f * get_ui_scale()); sf::Vector2f body_pos; sf::Vector2f body_size; @@ -3581,19 +3548,25 @@ namespace QuickMedia { if(redraw) { redraw = false; get_body_dimensions(window_size, nullptr, body_pos, body_size); + background.set_size(sf::Vector2f( + std::min(window_size.x, std::max(640.0f, window_size.x * 0.5f)), + num_inputs * inputs[0]->getBottomWithoutShadow() + padding_y * 2.0f + spacing_y * std::max(0, num_inputs - 1))); + background.set_position(window_size * 0.5f - background.get_size() * 0.5f); + + sf::Vector2f pos = background.get_position() + sf::Vector2f(padding_x, padding_y); + for(int i = 0; i < num_inputs; ++i) { + inputs[i]->set_position(pos); + pos.y += inputs[i]->getBottomWithoutShadow() + spacing_y; + } } window.clear(back_color); body->draw(window, body_pos, body_size); - float y = 0.0f; + background.draw(window); for(int i = 0; i < num_inputs; ++i) { - inputs[i]->set_vertical_position(y); inputs[i]->update(); - inputs[i]->draw(window); - y += inputs[i]->getBottomWithoutShadow(); + inputs[i]->draw(window, background.get_size() - sf::Vector2f(padding_x * 2.0f, 0.0f), false); } - status_text.setPosition(0.0f, y + 10.0f); - window.draw(status_text); window.display(); } } @@ -4781,6 +4754,24 @@ namespace QuickMedia { move_room = true; }; + bool fetching_prev_failed = false; + std::function<void()> on_top_reached = [this, &previous_messages_future, &ui_tabs, &MESSAGES_TAB_INDEX, &gradient_inc, current_room, &fetching_prev_failed] { + const int selected_tab = ui_tabs.get_selected(); + if(!fetching_prev_failed && !previous_messages_future.valid() && selected_tab == MESSAGES_TAB_INDEX) { + gradient_inc = 0; + previous_messages_future = AsyncTask<Messages>([this, current_room]() { + Messages messages; + if(matrix->get_previous_room_messages(current_room, messages) != PluginResult::OK) + fprintf(stderr, "Failed to get previous matrix messages in room: %s\n", current_room->id.c_str()); + return messages; + }); + } + }; + + for(size_t i = 0; i < tabs.size(); ++i) { + tabs[i].body->on_top_reached = on_top_reached; + } + while (current_page == PageType::CHAT && window.isOpen() && !move_room) { sf::Int32 frame_time_ms = frame_timer.restart().asMilliseconds(); while (window.pollEvent(event)) { @@ -4803,7 +4794,7 @@ namespace QuickMedia { chat_input.process_event(event); if(draw_room_list) { - if(room_tabs[room_selected_tab].body->on_event(window, event)) + if(room_tabs[room_selected_tab].body->on_event(window, event, false)) idle_active_handler(); } @@ -4850,42 +4841,7 @@ namespace QuickMedia { } else if(event.type == sf::Event::GainedFocus) { redraw = true; } else if(event.type == sf::Event::KeyPressed && chat_state == ChatState::NAVIGATING) { - if(event.key.code == sf::Keyboard::Up || event.key.code == sf::Keyboard::PageUp || event.key.code == sf::Keyboard::Home || (event.key.control && event.key.code == sf::Keyboard::K)) { - bool hit_top = false; - switch(event.key.code) { - case sf::Keyboard::Up: - hit_top = !tabs[selected_tab].body.get()->select_previous_item(); - break; - case sf::Keyboard::K: - hit_top = !tabs[selected_tab].body.get()->select_previous_item(); - break; - case sf::Keyboard::PageUp: - hit_top = !tabs[selected_tab].body.get()->select_previous_page(); - break; - case sf::Keyboard::Home: - tabs[selected_tab].body.get()->select_first_item(); - hit_top = true; - break; - default: - hit_top = false; - break; - } - if(hit_top && !previous_messages_future.valid() && selected_tab == MESSAGES_TAB_INDEX && current_room) { - gradient_inc = 0; - previous_messages_future = AsyncTask<Messages>([this, ¤t_room]() { - Messages messages; - if(matrix->get_previous_room_messages(current_room, messages) != PluginResult::OK) - fprintf(stderr, "Failed to get previous matrix messages in room: %s\n", current_room->id.c_str()); - return messages; - }); - } - } else if(event.key.code == sf::Keyboard::Down || (event.key.control && event.key.code == sf::Keyboard::J)) { - tabs[selected_tab].body.get()->select_next_item(); - } else if(event.key.code == sf::Keyboard::PageDown) { - tabs[selected_tab].body.get()->select_next_page(); - } else if(event.key.code == sf::Keyboard::End) { - tabs[selected_tab].body.get()->select_last_item(); - } else if(event.key.code == sf::Keyboard::Escape) { + if(event.key.code == sf::Keyboard::Escape) { goto chat_page_end; } else if(event.key.code == sf::Keyboard::I && event.key.control) { BodyItem *selected_item = tabs[selected_tab].body->get_selected(); @@ -5028,19 +4984,7 @@ namespace QuickMedia { } } } else if(event.type == sf::Event::KeyPressed && chat_state == ChatState::URL_SELECTION) { - if(event.key.code == sf::Keyboard::Up || (event.key.control && event.key.code == sf::Keyboard::K)) { - url_selection_body.select_previous_item(); - } else if(event.key.code == sf::Keyboard::Down || (event.key.control && event.key.code == sf::Keyboard::J)) { - url_selection_body.select_next_item(); - } else if(event.key.code == sf::Keyboard::PageUp) { - url_selection_body.select_previous_page(); - } else if(event.key.code == sf::Keyboard::PageDown) { - url_selection_body.select_next_page(); - } else if(event.key.code == sf::Keyboard::Home) { - url_selection_body.select_first_item(); - } else if(event.key.code == sf::Keyboard::End) { - url_selection_body.select_last_item(); - } else if(event.key.code == sf::Keyboard::Escape) { + if(event.key.code == sf::Keyboard::Escape) { url_selection_body.clear_items(); chat_state = ChatState::NAVIGATING; } else if(event.key.code == sf::Keyboard::Enter) { @@ -5257,8 +5201,10 @@ namespace QuickMedia { if(previous_messages_future.ready()) { Messages new_messages = previous_messages_future.get(); all_messages.insert(all_messages.end(), new_messages.begin(), new_messages.end()); - if(new_messages.empty()) + if(new_messages.empty()) { fetched_enough_messages = true; + fetching_prev_failed = true; + } filter_sent_messages(new_messages); filter_existing_messages(new_messages); fprintf(stderr, "Finished fetching older messages, num new messages: %zu\n", new_messages.size()); diff --git a/src/SearchBar.cpp b/src/SearchBar.cpp index 059d028..8362864 100644 --- a/src/SearchBar.cpp +++ b/src/SearchBar.cpp @@ -13,9 +13,8 @@ static const sf::Color text_placeholder_color(255, 255, 255, 100); static const sf::Color front_color(55, 60, 68); static const float background_margin_horizontal = 10.0f + std::floor(5.0f * QuickMedia::get_ui_scale()); -static const float PADDING_HORIZONTAL = std::floor(25.0f * QuickMedia::get_ui_scale()); -static const float padding_top = std::floor(10.0f * QuickMedia::get_ui_scale()); -static const float padding_bottom = std::floor(15.0f * QuickMedia::get_ui_scale()); +static const float padding_top_default = std::floor(10.0f * QuickMedia::get_ui_scale()); +static const float padding_bottom_default = std::floor(15.0f * QuickMedia::get_ui_scale()); static const float background_margin_vertical = std::floor(4.0f * QuickMedia::get_ui_scale()); namespace QuickMedia { @@ -40,36 +39,32 @@ namespace QuickMedia { typing(false), backspace_pressed(false), mouse_left_inside(false), - vertical_pos(0.0f) + pos(0.0f, 0.0f), + prev_size(0.0f, 0.0f) { + padding_top = padding_top_default; + padding_bottom = padding_bottom_default; text.setFillColor(text_placeholder_color); autocomplete_text.setFillColor(text_placeholder_color); - //background.setCornersRadius(5); - background_shadow.setFillColor(sf::Color(23, 25, 27)); - //background_shadow.setPosition(background.getPosition() + sf::Vector2f(5.0f, 5.0f)); shade.setFillColor(sf::Color(33, 37, 44)); - //background.setOutlineThickness(1.0f); - //background.setOutlineColor(sf::Color(13, 15, 17)); if(plugin_logo && plugin_logo->getNativeHandle() != 0) plugin_logo_sprite.setTexture(*plugin_logo, true); } - void SearchBar::draw(sf::RenderWindow &window, bool draw_shadow) { - sf::Vector2u window_size = window.getSize(); - if(window_size.x != prev_window_size.x || window_size.y != prev_window_size.y) { + void SearchBar::draw(sf::RenderWindow &window, sf::Vector2f size, bool draw_background) { + if(std::abs(size.x - prev_size.x) > 1.0f || std::abs(size.y - prev_size.y) > 1.0f) { needs_update = true; - prev_window_size = window_size; + prev_size = size; } if(needs_update) { needs_update = false; - onWindowResize(sf::Vector2f(window_size.x, window_size.y)); + onWindowResize(size); } - (void)draw_shadow; - //if(draw_shadow) - // window.draw(background_shadow); - window.draw(shade); + if(draw_background) + window.draw(shade); + background.draw(window); // TODO: Render starting from the character after text length window.draw(autocomplete_text); @@ -154,41 +149,36 @@ namespace QuickMedia { } } - void SearchBar::onWindowResize(const sf::Vector2f &window_size) { + void SearchBar::onWindowResize(const sf::Vector2f &size) { draw_logo = plugin_logo_sprite.getTexture() != nullptr; - float padding_horizontal = PADDING_HORIZONTAL; - if(window_size.x - padding_horizontal * 2.0f < 400.0f) { - padding_horizontal = 0.0f; + if(size.x * 2.0f < 400.0f) draw_logo = false; - } float font_height = text.getCharacterSize() + 7.0f; float rect_height = std::floor(font_height + background_margin_vertical * 2.0f); - float offset_x = padding_horizontal; + float offset_x; if(draw_logo) { float one_line_height = std::floor(text.getCharacterSize() + 8.0f + background_margin_vertical * 2.0f); auto texture_size = plugin_logo_sprite.getTexture()->getSize(); sf::Vector2f texture_size_f(texture_size.x, texture_size.y); sf::Vector2f new_size = wrap_to_size(texture_size_f, sf::Vector2f(200.0f, one_line_height)); plugin_logo_sprite.setScale(get_ratio(texture_size_f, new_size)); - plugin_logo_sprite.setPosition(10.0f, padding_top + vertical_pos + rect_height * 0.5f - plugin_logo_sprite.getTexture()->getSize().y * plugin_logo_sprite.getScale().y * 0.5f); - offset_x = 10.0f + new_size.x + 10.0f; + plugin_logo_sprite.setPosition(pos.x + padding_x, pos.y + padding_top + rect_height * 0.5f - plugin_logo_sprite.getTexture()->getSize().y * plugin_logo_sprite.getScale().y * 0.5f); + offset_x = padding_x + new_size.x + 10.0f; } else { - offset_x = 10.0f; + offset_x = padding_x; } - const float width = std::floor(window_size.x - offset_x - 10.0f); + const float width = std::floor(size.x - offset_x - padding_x); background.set_size(sf::Vector2f(width, rect_height)); - shade.setSize(sf::Vector2f(window_size.x, padding_top + rect_height + padding_bottom)); + shade.setSize(sf::Vector2f(size.x, padding_top + rect_height + padding_bottom)); caret.setSize(sf::Vector2f(std::floor(2.0f * get_ui_scale()), text.getCharacterSize() + std::floor(2.0f * get_ui_scale()))); - background_shadow.setSize(sf::Vector2f(window_size.x, 5.0f)); - background.set_position(sf::Vector2f(offset_x, padding_top + vertical_pos)); - shade.setPosition(0.0f, vertical_pos); - background_shadow.setPosition(0.0f, std::floor(shade.getSize().y + vertical_pos)); - sf::Vector2f font_position(std::floor(offset_x + background_margin_horizontal), std::floor(padding_top + background_margin_vertical + vertical_pos)); + background.set_position(sf::Vector2f(pos.x + offset_x, pos.y + padding_top)); + shade.setPosition(pos); + sf::Vector2f font_position(std::floor(pos.x + offset_x + background_margin_horizontal), std::floor(pos.y + padding_top + background_margin_vertical)); autocomplete_text.setPosition(font_position); text.setPosition(font_position); } @@ -306,9 +296,9 @@ namespace QuickMedia { autocomplete_text.setString(text); } - void SearchBar::set_vertical_position(float vertical_pos) { - if(std::abs(this->vertical_pos - vertical_pos) > 1.0f) { - this->vertical_pos = vertical_pos; + void SearchBar::set_position(sf::Vector2f pos) { + if(std::abs(this->pos.x - pos.x) > 1.0f || std::abs(this->pos.y - pos.y) > 1.0f) { + this->pos = pos; needs_update = true; } } |