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
This commit is contained in:
unitraxx 2013-08-01 01:02:35 +00:00
parent 9a4c93f2de
commit a4c5c4b8ec
6 changed files with 117 additions and 56 deletions

View File

@ -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<s32> 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);

View File

@ -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 <string.h>
@ -23,6 +24,7 @@
#include <IGUIEnvironment.h>
#include <IGUIElement.h>
#include <IGUIButton.h>
#include <cmath>
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<s32> widget_size = rect<s32>(m_x, m_y, m_x + m_w, m_y + m_h);
irr::core::rect<s32> widget_size = rect<s32>(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<s32> & 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) );
}
}

View File

@ -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<int> 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<s32> & mouse_position);
};
}

View File

@ -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))

View File

@ -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(); }

View File

@ -20,7 +20,6 @@
#include <IGUIEnvironment.h>
#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<RatingBarWidget>("rating");
assert(m_rating_widget != NULL);
m_rating_widget->setRating(0);
m_rating_widget->setStarNumber(3);
m_options_widget = getWidget<RibbonWidget>("options");
assert(m_options_widget != NULL);
m_submit_widget = getWidget<IconButtonWidget>("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();
}