~ted/unity/menu-ref

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright 2011 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3, as
 * published by the  Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License version 3 along with this program.  If not, see
 * <http://www.gnu.org/licenses/>
 *
 * Authored by: Gordon Allott <gord.allott@canonical.com>
 *
 */

#include <math.h>

#include <Nux/Nux.h>
#include <NuxCore/Logger.h>
#include <UnityCore/Variant.h>

#include "RatingsButton.h"
#include "DashStyle.h"

namespace
{
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)
  , editable_(true)
  , rating_(0.0)
  , focused_star_(-1)
  , star_size_(star_size)
  , star_gap_(star_gap)
{
  SetAcceptKeyNavFocusOnMouseDown(false);
  SetAcceptKeyNavFocusOnMouseEnter(true);

  mouse_up.connect(sigc::mem_fun(this, &RatingsButton::RecvMouseUp));
  mouse_move.connect(sigc::mem_fun(this, &RatingsButton::RecvMouseMove));
  mouse_drag.connect(sigc::mem_fun(this, &RatingsButton::RecvMouseDrag));

  key_nav_focus_change.connect([&](nux::Area* area, bool has_focus, nux::KeyNavDirection direction)
  {
    if (has_focus && direction != nux::KEY_NAV_NONE)
      focused_star_ = 0;
    else if (!has_focus)
      focused_star_ = -1;

    QueueDraw();
  });
  key_nav_focus_activate.connect([&](nux::Area*) { SetRating(static_cast<float>(focused_star_+1)/num_stars); });
  key_down.connect(sigc::mem_fun(this, &RatingsButton::OnKeyDown));
}

RatingsButton::~RatingsButton()
{
}

void RatingsButton::SetEditable(bool editable)
{
  editable_ = editable;
  if (!editable_)
    focused_star_ = -1;
  QueueDraw();
}

void RatingsButton::SetRating(float rating)
{
  rating_  = rating;
  QueueDraw();
}

float RatingsButton::GetRating() const
{
  return rating_;
}

void RatingsButton::Draw(nux::GraphicsEngine& GfxContext, bool force_draw)
{
  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
  // the moment is the applications lens which according to design
  // (Bug #839759) shouldn't. So for now just force rounding.
  //    int total_half_stars = rating % 2;
  //    int total_full_stars = rating / 2;
  int total_full_stars = rating;

  nux::Geometry const& geo = GetGeometry();
  nux::Geometry geo_star(geo);
  geo_star.width = star_size_;
  geo_star.height = star_size_;

  gPainter.PaintBackground(GfxContext, geo);
  // set up our texture mode
  nux::TexCoordXForm texxform;
  texxform.SetWrap(nux::TEXWRAP_CLAMP_TO_BORDER, nux::TEXWRAP_CLAMP_TO_BORDER);
  texxform.SetTexCoordType(nux::TexCoordXForm::OFFSET_SCALE_COORD);
  texxform.SetFilter(nux::TEXFILTER_LINEAR, nux::TEXFILTER_LINEAR);

  // clear what is behind us
  unsigned int alpha = 0, src = 0, dest = 0;

  GfxContext.GetRenderStates().GetBlend(alpha, src, dest);
  GfxContext.GetRenderStates().SetBlend(true, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

  nux::Color col = nux::color::Black;
  col.alpha = 0;
  GfxContext.QRP_Color(geo.x,
                       geo.y,
                       geo.width,
                       geo.height,
                       col);

  for (int index = 0; index < num_stars; ++index)
  {
    dash::Style& style = dash::Style::Instance();
    nux::BaseTexture* texture = style.GetStarSelectedIcon();
    if (index < total_full_stars)
    {
      if (GetVisualState() == nux::ButtonVisualState::VISUAL_STATE_NORMAL)
        texture = style.GetStarSelectedIcon();
      else if (GetVisualState() == nux::ButtonVisualState::VISUAL_STATE_PRELIGHT)
        texture = style.GetStarSelectedIcon();
      else if (GetVisualState() == nux::ButtonVisualState::VISUAL_STATE_PRESSED)
        texture = style.GetStarSelectedIcon();
    }
    else
    {
      if (GetVisualState() == nux::ButtonVisualState::VISUAL_STATE_NORMAL)
        texture = style.GetStarDeselectedIcon();
      else if (GetVisualState() == nux::ButtonVisualState::VISUAL_STATE_PRELIGHT)
        texture = style.GetStarDeselectedIcon();
      else if (GetVisualState() == nux::ButtonVisualState::VISUAL_STATE_PRESSED)
        texture = style.GetStarDeselectedIcon();
    }

    GfxContext.QRP_1Tex(geo_star.x,
                        geo_star.y,
                        geo_star.width,
                        geo_star.height,
                        texture->GetDeviceTexture(),
                        texxform,
                        nux::Color(1.0f, 1.0f, 1.0f, 1.0f));

    if (focused_star_ == index)
    {
      GfxContext.QRP_1Tex(geo_star.x,
                          geo_star.y,
                          geo_star.width,
                          geo_star.height,
                          style.GetStarHighlightIcon()->GetDeviceTexture(),
                          texxform,
                          nux::Color(1.0f, 1.0f, 1.0f, 0.5f));
    }

    geo_star.x += geo_star.width + star_gap_;

  }

  GfxContext.GetRenderStates().SetBlend(alpha, src, dest);

}

void RatingsButton::UpdateRatingToMouse(int x)
{
  int width = num_stars*star_size_ + (num_stars-1)*star_gap_;
  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 = (new_rating > 1) ? 1 : ((new_rating < 0) ? 0 : new_rating);

  SetRating(new_rating);
}

void RatingsButton::RecvMouseUp(int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  if (!editable_)
    return;

  UpdateRatingToMouse(x);
}

void RatingsButton::RecvMouseDrag(int x, int y, int dx, int dy,
                                        unsigned long button_flags,
                                        unsigned long key_flags)
{
  if (!editable_)
    return;

  UpdateRatingToMouse(x);
}

void RatingsButton::RecvMouseMove(int x, int y, int dx, int dy,
                                        unsigned long button_flags,
                                        unsigned long key_flags)
{
  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));

  if (!HasKeyFocus())
    nux::GetWindowCompositor().SetKeyFocusArea(this);

  QueueDraw();
}

bool RatingsButton::InspectKeyEvent(unsigned int eventType, unsigned int keysym, const char* character)
{
  nux::KeyNavDirection direction = nux::KEY_NAV_NONE;

  switch (keysym)
  {
    case NUX_VK_LEFT:
      direction = nux::KeyNavDirection::KEY_NAV_LEFT;
      break;
    case NUX_VK_RIGHT:
      direction = nux::KeyNavDirection::KEY_NAV_RIGHT;
      break;
    default:
      direction = nux::KeyNavDirection::KEY_NAV_NONE;
      break;
  }

  if (direction == nux::KeyNavDirection::KEY_NAV_NONE)
    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))
    return false;
  else
   return true;
}


void RatingsButton::OnKeyDown(unsigned long event_type, unsigned long event_keysym,
                                    unsigned long event_state, const TCHAR* character,
                                    unsigned short key_repeat_count)
{
  if (!editable_)
    return;

  switch (event_keysym)
  {
    case NUX_VK_LEFT:
      --focused_star_;
      break;
    case NUX_VK_RIGHT:
      ++focused_star_;
      break;
    default:
      return;
  }

  QueueDraw();
}

bool RatingsButton::AcceptKeyNavFocus()
{
  return editable_;
}

std::string RatingsButton::GetName() const
{
  return "RatingsButton";
}

void RatingsButton::AddProperties(GVariantBuilder* builder)
{
  variant::BuilderWrapper(builder)
    .add(GetAbsoluteGeometry())
    .add("rating", rating_)
    .add("focused-star", focused_star_)
    .add("editable", editable_);
}

} // namespace unity