~didrocks/unity/fixes-natty-finale

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
/*
 * 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 "Nux/Nux.h"
#include "PlacesTile.h"
PlacesTile::PlacesTile (NUX_FILE_LINE_DECL) :
View (NUX_FILE_LINE_PARAM),
_hilight_background (NULL),
_hilight_layer (NULL),
_last_width (0),
_last_height (0)
{
  _state = STATE_DEFAULT;

  OnMouseDown.connect (sigc::mem_fun (this, &PlacesTile::RecvMouseDown));
  OnMouseUp.connect (sigc::mem_fun (this, &PlacesTile::RecvMouseUp));
  OnMouseClick.connect (sigc::mem_fun (this, &PlacesTile::RecvMouseClick));
  OnMouseMove.connect (sigc::mem_fun (this, &PlacesTile::RecvMouseMove));
  //OnMouseDrag.connect (sigc::mem_fun (this, &PlacesTile::RecvMouseDrag));
  OnMouseEnter.connect (sigc::mem_fun (this, &PlacesTile::RecvMouseEnter));
  OnMouseLeave.connect (sigc::mem_fun (this, &PlacesTile::RecvMouseLeave));
  _hilight_view = this;
}

PlacesTile::~PlacesTile ()
{
}

void
PlacesTile::UpdateBackground ()
{
  nux::Geometry base = GetGeometry ();

  if ((base.width == _last_width) && (base.height == _last_height))
    return;

  _last_width = base.width;
  _last_height = base.height;

  nux::CairoGraphics *cairo_graphics = new nux::CairoGraphics (CAIRO_FORMAT_ARGB32, base.width, base.height);
  cairo_t *cr = cairo_graphics->GetContext();

  cairo_scale (cr, 1.0f, 1.0f);

  cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0);
  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
  cairo_paint (cr);
  cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);

  DrawRoundedRectangle (cr, 1.0, 6.0, 6.0, 15.0, base.width - 12 , base.height - 12);
  cairo_set_source_rgba (cr, 0.25, 0.25, 0.25, 0.25);
  cairo_fill_preserve (cr);
  cairo_set_source_rgba (cr, 1, 1, 1, 1.0);
  cairo_set_line_width (cr, 1.0);
  cairo_stroke (cr);

  cairo_destroy (cr);

  nux::NBitmapData* bitmap =  cairo_graphics->GetBitmap();
  
  if (_hilight_background)
    _hilight_background->UnReference ();

  _hilight_background = nux::GetThreadGLDeviceFactory()->CreateSystemCapableTexture ();
  _hilight_background->Update (bitmap);
  delete bitmap;

  nux::ROPConfig rop; 
  rop.Blend = true;
  rop.SrcBlend = GL_ONE;
  rop.DstBlend = GL_ONE_MINUS_SRC_ALPHA;
  
  nux::TexCoordXForm texxform;
  texxform.SetTexCoordType (nux::TexCoordXForm::OFFSET_COORD);
  texxform.SetWrap (nux::TEXWRAP_REPEAT, nux::TEXWRAP_REPEAT);

  if (_hilight_layer)
    delete _hilight_layer;

  _hilight_layer = new nux::TextureLayer (_hilight_background->GetDeviceTexture(),
                                     texxform,
                                     nux::Color::White,
                                     true,
                                     rop);
  _hilight_background->UnReference ();
  _hilight_background = NULL;
  delete cairo_graphics;
}

static inline double
_align (double val)
{
  double fract = val - (int) val;
  if (fract != 0.5f)
    return (double) ((int) val + 0.5f);
  else
    return val;
}

void
PlacesTile::DrawRoundedRectangle (cairo_t* cr,
                                  double   aspect,
                                  double   x,
                                  double   y,
                                  double   cornerRadius,
                                  double   width,
                                  double   height)
{
  double radius = cornerRadius / aspect;

  // top-left, right of the corner
  cairo_move_to (cr, _align (x + radius), _align (y));

  // top-right, left of the corner
  cairo_line_to (cr, _align (x + width - radius), _align (y));

  // top-right, below the corner
  cairo_arc (cr,
             _align (x + width - radius),
             _align (y + radius),
             radius,
             -90.0f * G_PI / 180.0f,
             0.0f * G_PI / 180.0f);

  // bottom-right, above the corner
  cairo_line_to (cr, _align (x + width), _align (y + height - radius));

  // bottom-right, left of the corner
  cairo_arc (cr,
             _align (x + width - radius),
             _align (y + height - radius),
             radius,
             0.0f * G_PI / 180.0f,
             90.0f * G_PI / 180.0f);

  // bottom-left, right of the corner
  cairo_line_to (cr, _align (x + radius), _align (y + height));

  // bottom-left, above the corner
  cairo_arc (cr,
             _align (x + radius),
             _align (y + height - radius),
             radius,
             90.0f * G_PI / 180.0f,
             180.0f * G_PI / 180.0f);

  // top-left, right of the corner
  cairo_arc (cr,
             _align (x + radius),
             _align (y + radius),
             radius,
             180.0f * G_PI / 180.0f,
             270.0f * G_PI / 180.0f);
}


long PlacesTile::ProcessEvent (nux::IEvent &ievent, long TraverseInfo, long ProcessEventInfo)
{
  long ret = TraverseInfo;
  ret = PostProcessEvent2 (ievent, ret, ProcessEventInfo);
  return ret;
}

void PlacesTile::Draw (nux::GraphicsEngine& gfxContext,
                       bool                 forceDraw)
{
  UpdateBackground ();

  nux::Geometry base = GetGeometry ();
  gfxContext.PushClippingRectangle (base);

  nux::GetPainter ().PaintBackground (gfxContext, GetGeometry ());

  if (_state == STATE_HOVER)
  {
    gPainter.PushDrawLayer (gfxContext, GetGeometry (), _hilight_layer);
    gPainter.PopBackground ();
  }

  gfxContext.PopClippingRectangle ();
}

void PlacesTile::DrawContent (nux::GraphicsEngine &GfxContext, bool force_draw)
{
  UpdateBackground ();
  GfxContext.PushClippingRectangle (GetGeometry() );

  if (_state == STATE_HOVER)
    nux::GetPainter ().PushLayer (GfxContext, GetGeometry (), _hilight_layer);

  _layout->ProcessDraw (GfxContext, force_draw);
  
  if (_state == STATE_HOVER)
    nux::GetPainter ().PopBackground ();

  GfxContext.PopClippingRectangle();
}

void PlacesTile::PostDraw (nux::GraphicsEngine &GfxContext, bool force_draw)
{

}

void
PlacesTile::PreLayoutManagement ()
{
  nux::View::PreLayoutManagement ();
}

long
PlacesTile::PostLayoutManagement (long LayoutResult)
{
  return nux::View::PostLayoutManagement (LayoutResult);
}

void PlacesTile::SetState (TileState state)
{
  _state = state;
  NeedRedraw ();
}

PlacesTile::TileState PlacesTile::GetState ()
{
  return _state;
}

void PlacesTile::RecvMouseClick (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  sigClick.emit(this);

  NeedRedraw();
  _layout->NeedRedraw ();
}

void PlacesTile::RecvMouseUp (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  NeedRedraw();
  _layout->NeedRedraw ();
}

void PlacesTile::RecvMouseDown (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  _state = STATE_PRESSED;
  NeedRedraw();
  _layout->NeedRedraw ();
}

void PlacesTile::RecvMouseMove (int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
{

}

void PlacesTile::RecvMouseEnter (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  SetState (STATE_HOVER);
  NeedRedraw();
  _layout->NeedRedraw ();
}

void PlacesTile::RecvMouseLeave (int x, int y, unsigned long button_flags, unsigned long key_flags)
{
  SetState (STATE_DEFAULT);
  NeedRedraw();
  _layout->NeedRedraw ();
}