summaryrefslogtreecommitdiff
path: root/unity-shared
diff options
authorMarco Trevisan (Treviño) <mail@3v1n0.net>2014-07-10 14:00:57 +0200
committerMarco Trevisan (Treviño) <mail@3v1n0.net>2014-07-10 14:00:57 +0200
commitc38ffff5e20f346f2841879f5a35d2ba9a309cdb (patch)
treee09ee92d26226cd19452bcb467bd99cc2e6cd971 /unity-shared
parent4de6736c3dacd415d0fa016f50d99ca0350ef803 (diff)
RatingsButton: correctly scale the ratings button
(bzr r3830.5.14)
Diffstat (limited to 'unity-shared')
-rw-r--r--unity-shared/RatingsButton.cpp31
-rw-r--r--unity-shared/RatingsButton.h9
2 files changed, 21 insertions, 19 deletions
diff --git a/unity-shared/RatingsButton.cpp b/unity-shared/RatingsButton.cpp
index f4954c782..eae9c517c 100644
--- a/unity-shared/RatingsButton.cpp
+++ b/unity-shared/RatingsButton.cpp
@@ -29,13 +29,14 @@
namespace
{
-const int num_stars = 5;
+const int NUM_STARS = 5;
}
namespace unity
{
RatingsButton::RatingsButton(int star_size, int star_gap, NUX_FILE_LINE_DECL)
: nux::ToggleButton(NUX_FILE_LINE_PARAM)
+ , scale(1.0)
, star_size_(star_size)
, star_gap_(star_gap)
, editable_(true)
@@ -58,12 +59,12 @@ RatingsButton::RatingsButton(int star_size, int star_gap, NUX_FILE_LINE_DECL)
QueueDraw();
});
- key_nav_focus_activate.connect([this](nux::Area*) { SetRating(static_cast<float>(focused_star_+1)/num_stars); });
+ key_nav_focus_activate.connect([this](nux::Area*) { SetRating(static_cast<float>(focused_star_+1)/NUM_STARS); });
key_down.connect(sigc::mem_fun(this, &RatingsButton::OnKeyDown));
-}
-RatingsButton::~RatingsButton()
-{
+ scale.changed.connect([this] (double scale) {
+ QueueDraw();
+ });
}
void RatingsButton::SetEditable(bool editable)
@@ -87,7 +88,7 @@ float RatingsButton::GetRating() const
void RatingsButton::Draw(nux::GraphicsEngine& GfxContext, bool force_draw)
{
- int rating = static_cast<int>(rating_ * num_stars);
+ int rating = static_cast<int>(rating_ * NUM_STARS);
// FIXME: 9/26/2011
// We should probably support an API for saying whether the ratings
// should or shouldn't support half stars...but our only consumer at
@@ -99,8 +100,8 @@ void RatingsButton::Draw(nux::GraphicsEngine& GfxContext, bool force_draw)
nux::Geometry const& geo = GetGeometry();
nux::Geometry geo_star(geo);
- geo_star.width = star_size_;
- geo_star.height = star_size_;
+ geo_star.width = star_size_.CP(scale);
+ geo_star.height = star_size_.CP(scale);
gPainter.PaintBackground(GfxContext, geo);
// set up our texture mode
@@ -123,7 +124,7 @@ void RatingsButton::Draw(nux::GraphicsEngine& GfxContext, bool force_draw)
geo.height,
col);
- for (int index = 0; index < num_stars; ++index)
+ for (int index = 0; index < NUM_STARS; ++index)
{
dash::Style& style = dash::Style::Instance();
nux::BaseTexture* texture = style.GetStarSelectedIcon();
@@ -165,7 +166,7 @@ void RatingsButton::Draw(nux::GraphicsEngine& GfxContext, bool force_draw)
nux::Color(1.0f, 1.0f, 1.0f, 0.5f));
}
- geo_star.x += geo_star.width + star_gap_;
+ geo_star.x += geo_star.width + star_gap_.CP(scale);
}
@@ -175,11 +176,11 @@ void RatingsButton::Draw(nux::GraphicsEngine& GfxContext, bool force_draw)
void RatingsButton::UpdateRatingToMouse(int x)
{
- int width = num_stars*star_size_ + (num_stars-1)*star_gap_;
+ int width = NUM_STARS*star_size_.CP(scale) + (NUM_STARS-1)*star_gap_.CP(scale);
float new_rating = (static_cast<float>(x) / width);
// FIXME: change to * 2 once we decide to support also half-stars
- new_rating = ceil((num_stars * 1) * new_rating) / (num_stars * 1);
+ new_rating = ceil((NUM_STARS * 1) * new_rating) / (NUM_STARS * 1);
new_rating = (new_rating > 1) ? 1 : ((new_rating < 0) ? 0 : new_rating);
SetRating(new_rating);
@@ -210,8 +211,8 @@ void RatingsButton::RecvMouseMove(int x, int y, int dx, int dy,
if (!editable_)
return;
- int width = num_stars*star_size_+ (num_stars-1)*star_gap_;
- focused_star_ = std::max(0, std::min(static_cast<int>(ceil((static_cast<float>(x) / width) * num_stars) - 1), num_stars - 1));
+ int width = NUM_STARS*star_size_.CP(scale)+ (NUM_STARS-1)*star_gap_.CP(scale);
+ focused_star_ = std::max(0, std::min(static_cast<int>(ceil((static_cast<float>(x) / width) * NUM_STARS) - 1), NUM_STARS - 1));
if (!HasKeyFocus())
nux::GetWindowCompositor().SetKeyFocusArea(this);
@@ -240,7 +241,7 @@ bool RatingsButton::InspectKeyEvent(unsigned int eventType, unsigned int keysym,
return false;
else if (direction == nux::KEY_NAV_LEFT && (focused_star_ <= 0))
return false;
- else if (direction == nux::KEY_NAV_RIGHT && (focused_star_ >= num_stars - 1))
+ else if (direction == nux::KEY_NAV_RIGHT && (focused_star_ >= NUM_STARS - 1))
return false;
else
return true;
diff --git a/unity-shared/RatingsButton.h b/unity-shared/RatingsButton.h
index 9fef67f7e..a0cf138d4 100644
--- a/unity-shared/RatingsButton.h
+++ b/unity-shared/RatingsButton.h
@@ -27,6 +27,7 @@
#include <Nux/Nux.h>
#include <Nux/ToggleButton.h>
#include "unity-shared/Introspectable.h"
+#include "unity-shared/RawPixel.h"
namespace unity
{
@@ -35,15 +36,13 @@ class RatingsButton : public unity::debug::Introspectable, public nux::ToggleBut
{
public:
RatingsButton(int star_size, int star_gap, NUX_FILE_LINE_PROTO);
- virtual ~RatingsButton();
+
+ nux::Property<double> scale;
void SetEditable(bool editable);
virtual void SetRating(float rating);
virtual float GetRating() const;
- int star_size_;
- int star_gap_;
-
protected:
virtual void Draw(nux::GraphicsEngine& GfxContext, bool force_draw);
@@ -67,6 +66,8 @@ private:
protected:
+ RawPixel star_size_;
+ RawPixel star_gap_;
bool editable_;
float rating_;
int focused_star_;