From a4c5c4b8ec2bb7cc39c8c17ef3b815f03d906e38 Mon Sep 17 00:00:00 2001 From: unitraxx Date: Thu, 1 Aug 2013 01:02:35 +0000 Subject: [PATCH] Some advancements with the addon voting git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/uni@13396 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/guiengine/skin.cpp | 8 +- src/guiengine/widgets/rating_bar_widget.cpp | 91 +++++++++++++-------- src/guiengine/widgets/rating_bar_widget.hpp | 23 +++--- src/online/current_user.cpp | 30 +++++++ src/online/current_user.hpp | 3 + src/states_screens/dialogs/vote_dialog.cpp | 18 ++-- 6 files changed, 117 insertions(+), 56 deletions(-) diff --git a/src/guiengine/skin.cpp b/src/guiengine/skin.cpp index 2fc79572f..c16b51bf2 100644 --- a/src/guiengine/skin.cpp +++ b/src/guiengine/skin.cpp @@ -813,14 +813,16 @@ void Skin::drawProgress(Widget* w, const core::recti &rect, void Skin::drawRatingBar(Widget *w, const core::recti &rect, const bool pressed, const bool focused) { - static const int step_number = 3; // Harcoded number of step. + RatingBarWidget *ratingBar = (RatingBarWidget*)w; + + core::position2d mouse_position = irr_driver->getDevice()->getCursorControl()->getPosition(); + ratingBar->setStepValuesByMouse(mouse_position); const ITexture *texture = SkinConfig::m_render_params["rating::neutral"].getImage(); const int texture_w = texture->getSize().Width / 4; const int texture_h = texture->getSize().Height; const float aspect_ratio = 1.0f; - RatingBarWidget *ratingBar = (RatingBarWidget*)w; const int star_number = ratingBar->getStarNumber(); int star_h = rect.getHeight(); @@ -846,7 +848,7 @@ void Skin::drawRatingBar(Widget *w, const core::recti &rect, star_rect.LowerRightCorner.X = x_from + (i + 1) * star_w; star_rect.LowerRightCorner.Y = y_from + star_h; - int step = ratingBar->getStepOfStar(i, step_number); + int step = ratingBar->getStepsOfStar(i); const core::recti source_area(texture_w * step, 0, texture_w * (step + 1), texture_h); diff --git a/src/guiengine/widgets/rating_bar_widget.cpp b/src/guiengine/widgets/rating_bar_widget.cpp index 88c1b0d6d..02e9ea569 100644 --- a/src/guiengine/widgets/rating_bar_widget.cpp +++ b/src/guiengine/widgets/rating_bar_widget.cpp @@ -16,6 +16,7 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "guiengine/engine.hpp" +#include "guiengine/modaldialog.hpp" #include "guiengine/widgets/rating_bar_widget.hpp" #include "utils/string_utils.hpp" #include @@ -23,6 +24,7 @@ #include #include #include +#include using namespace GUIEngine; using namespace irr::core; @@ -32,15 +34,17 @@ using namespace irr; RatingBarWidget::RatingBarWidget() : Widget(WTYPE_RATINGBAR) { m_rating = 0; - m_star_number = 0; + m_stars = 3; + m_steps = 3; + for(int i = 0; i < m_stars; i++) + m_star_values.push_back(0); } // ----------------------------------------------------------------------------- void RatingBarWidget::add() { - rect widget_size = rect(m_x, m_y, m_x + m_w, m_y + m_h); + irr::core::rect widget_size = rect(m_x, m_y, m_x + m_w, m_y + m_h); m_element = GUIEngine::getGUIEnv()->addButton(widget_size, m_parent, getNewNoFocusID(), NULL, L""); - m_id = m_element->getID(); m_element->setTabStop(false); m_element->setTabGroup(false); @@ -50,43 +54,62 @@ void RatingBarWidget::add() /** Get the current step of the star * * \param index The index of the star. - * \param max_step The number of different steps that a star can display. Two - * step are obligatory: full and empty. * \return The current step of the star. */ -int RatingBarWidget::getStepOfStar(int index, int max_step) +int RatingBarWidget::getStepsOfStar(int index) { - assert(index >= 0 && index < m_star_number); // Index must be between 0 and m_star_number - 1. - assert(max_step >= 2); // The maximun number of step must be larger or equal to 2. + assert(index >= 0 && index < m_stars); // Index must be between 0 and m_star_number - 1. - if (m_rating < index) - { - return 0; - } - else if (m_rating > index + 1) - { - return max_step - 1; - } - else - { - float step_size = 1 / (float)(max_step - 1); - - for (int i = 0; i < max_step; i++) - { - if (m_rating > index + step_size * (i - 0.5) - && m_rating < index + step_size * (i + 0.5)) - return i; - } - } - - return 0; - // TODO: Assert or throws a exception, what type? + return m_star_values[index]; } // getStepOfStar -EventPropagation RatingBarWidget::mouseHovered(Widget* child, const int playerID) { - Log::info("RatingBarWidget::mouseHovered",""); - //m_rating++; - return EVENT_BLOCK; + +void RatingBarWidget::setStepValues(float float_rating) +{ + int rating = round(float_rating); + float step_size = 1 / (float)(m_steps - 1); + + for (int star = 0; star < m_stars; star++) + { + if (rating < star) + m_star_values[star] = 0; + else if (rating > star + 1) + m_star_values[star] = m_steps-1; + else + { + for (int step = 0; step < m_steps; step++) + { + if (rating > star + step_size * (step - 0.5) && rating < star + step_size * (step + 0.5)) + { + m_star_values[star] = step; + break; + } + } + } + } } +// ----------------------------------------------------------------------------- + +void RatingBarWidget::setRating(float rating) +{ + m_rating = rating; + setStepValues(m_rating); +} + +// ----------------------------------------------------------------------------- + +void RatingBarWidget::setStepValuesByMouse(const core::position2d & mouse_position) +{ + if(m_element->getAbsolutePosition().isPointInside(mouse_position)) + { + + float value = (float)(mouse_position.X - m_element->getAbsolutePosition().UpperLeftCorner.X); + setStepValues( (float)( value / (float)m_w * (float)m_stars) ); + + } +} + + + diff --git a/src/guiengine/widgets/rating_bar_widget.hpp b/src/guiengine/widgets/rating_bar_widget.hpp index 60bfef974..f92441672 100644 --- a/src/guiengine/widgets/rating_bar_widget.hpp +++ b/src/guiengine/widgets/rating_bar_widget.hpp @@ -1,5 +1,6 @@ // SuperTuxKart - a fun racing game with go-kart // Copyright (C) 2009 Marianne Gagnon +// 2013 Glenn De Jonghe // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -34,13 +35,13 @@ namespace GUIEngine */ class RatingBarWidget : public Widget { - - float m_rating; - int m_star_number; - - protected: + private: + float m_rating; + int m_stars; + int m_steps; + std::vector m_star_values; - virtual EventPropagation mouseHovered(Widget* child, const int playerID); + void setStepValues(float rating); public: @@ -54,18 +55,20 @@ namespace GUIEngine void add(); /** Change the rating value of the widget. */ - void setRating(float rating) { m_rating = rating; }; + void setRating(float rating); /** Get the current value of the widget. */ float getRating() {return m_rating; }; /** Change the number of stars of the widget. */ - void setStarNumber(int star_number) { m_star_number = star_number; }; + void setStarNumber(int star_number) { m_stars = star_number; }; /** Get the current number of stars of the widget. */ - int getStarNumber() {return m_star_number; }; + int getStarNumber() {return m_stars; }; - int getStepOfStar(int index, int max_step); + int getStepsOfStar(int index); + + void setStepValuesByMouse(const core::position2d & mouse_position); }; } diff --git a/src/online/current_user.cpp b/src/online/current_user.cpp index fd53934d1..6845b3ef1 100644 --- a/src/online/current_user.cpp +++ b/src/online/current_user.cpp @@ -246,6 +246,36 @@ namespace Online{ // ============================================================================ + const XMLRequest * CurrentUser::requestGetAddonVote( uint32_t addon_id) + { + assert(isRegisteredUser()); + XMLRequest * request = new XMLRequest(); + request->setURL((std::string)UserConfigParams::m_server_multiplayer + "client-user.php"); + request->setParameter("action", std::string("get-addon-vote")); + request->setParameter("token", getToken()); + request->setParameter("id", getUserID()); + request->setParameter("addon-id", addon_id); + HTTPManager::get()->addRequest(request); + return request; + } + + // ============================================================================ + + const XMLRequest * CurrentUser::requestSetAddonVote( uint32_t addon_id) + { + assert(isRegisteredUser()); + XMLRequest * request = new XMLRequest(); + request->setURL((std::string)UserConfigParams::m_server_multiplayer + "client-user.php"); + request->setParameter("action", std::string("set-addon-vote")); + request->setParameter("token", getToken()); + request->setParameter("id", getUserID()); + request->setParameter("addon-id", addon_id); + HTTPManager::get()->addRequest(request); + return request; + } + + // ============================================================================ + const irr::core::stringw CurrentUser::getUserName() const { if((getUserState() == US_SIGNED_IN ) || (getUserState() == US_GUEST)) diff --git a/src/online/current_user.hpp b/src/online/current_user.hpp index 466fc415a..5db093070 100644 --- a/src/online/current_user.hpp +++ b/src/online/current_user.hpp @@ -129,6 +129,9 @@ namespace Online{ const XMLRequest * requestRecovery(const irr::core::stringw &username, const irr::core::stringw &email); + const XMLRequest * requestGetAddonVote(uint32_t addon_id); + const XMLRequest * requestSetAddonVote(uint32_t addon_id); + /** Returns the username if signed in. */ const irr::core::stringw getUserName() const; const UserState getUserState() const { return m_state.getAtomic(); } diff --git a/src/states_screens/dialogs/vote_dialog.cpp b/src/states_screens/dialogs/vote_dialog.cpp index 4f3109985..5d2cb9916 100644 --- a/src/states_screens/dialogs/vote_dialog.cpp +++ b/src/states_screens/dialogs/vote_dialog.cpp @@ -20,7 +20,6 @@ #include #include "audio/sfx_manager.hpp" -//#include "config/player.hpp" #include "guiengine/engine.hpp" #include "states_screens/state_manager.hpp" #include "utils/translation.hpp" @@ -46,8 +45,6 @@ VoteDialog::VoteDialog(const std::string & addon_id) m_rating_widget = getWidget("rating"); assert(m_rating_widget != NULL); m_rating_widget->setRating(0); - m_rating_widget->setStarNumber(3); - m_options_widget = getWidget("options"); assert(m_options_widget != NULL); m_submit_widget = getWidget("submit"); @@ -56,6 +53,7 @@ VoteDialog::VoteDialog(const std::string & addon_id) assert(m_cancel_widget != NULL); m_options_widget->setFocusForPlayer(PLAYER_ID_GAME_MASTER); + } // ----------------------------------------------------------------------------- @@ -68,14 +66,15 @@ VoteDialog::~VoteDialog() GUIEngine::EventPropagation VoteDialog::processEvent(const std::string& eventSource) { + if (eventSource == m_rating_widget->m_properties[PROP_ID]) + { + m_self_destroy = true; + return GUIEngine::EVENT_BLOCK; + } + if (eventSource == m_options_widget->m_properties[PROP_ID]) { const std::string& selection = m_options_widget->getSelectionIDString(PLAYER_ID_GAME_MASTER); - if (selection == m_rating_widget->m_properties[PROP_ID]) - { - m_self_destroy = true; - return GUIEngine::EVENT_BLOCK; - } } return GUIEngine::EVENT_LET; } @@ -84,5 +83,6 @@ GUIEngine::EventPropagation VoteDialog::processEvent(const std::string& eventSou void VoteDialog::onUpdate(float dt) { - + if (m_self_destroy) + ModalDialog::dismiss(); }