~unity-team/nuxplayground/nuxplayground.fix-nstring

« back to all changes in this revision

Viewing changes to src/view-canvas/ViewCanvas.cpp

* View Canvas

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "Nux/Nux.h"
 
2
#include "ViewCanvas.h"
 
3
 
 
4
const int CAPTION_X_MARGIN = 6;
 
5
const int X_MARGIN = 4;
 
6
const int Y_MARGIN = 4;
 
7
 
 
8
static const int XMARGIN = 20;
 
9
static const int YMARGIN = 20;
 
10
 
 
11
static const int NODE_HANDLE_WIDTH = 8;
 
12
static const int NODE_HANDLE_HEIGHT = 5;
 
13
static const int NODE_HANDLE_COLOR = 0xFFFFFFFF;
 
14
//static const NODE_GRID_SPACING = 32;
 
15
 
 
16
namespace nux
 
17
{
 
18
NUX_IMPLEMENT_OBJECT_TYPE(Canvas);
 
19
 
 
20
Canvas::Canvas(NUX_FILE_LINE_DECL)
 
21
  : View(NUX_FILE_LINE_PARAM)
 
22
  , head_view_(NULL)
 
23
  , selected_view_(NULL)
 
24
  , grid_snapping_(1)
 
25
{
 
26
  NTextureData bitmap_data;
 
27
  bitmap_data.AllocateCheckBoardTexture(16, 16, 1, Color(0xFF444444), Color(0xFF666666), 8, 8);
 
28
  node_background_texture_.Update(&bitmap_data);
 
29
 
 
30
  SetMinimumSize(DEFAULT_WIDGET_WIDTH+5, PRACTICAL_WIDGET_HEIGHT+5);
 
31
  SetBaseSize(DEFAULT_WIDGET_WIDTH+5, PRACTICAL_WIDGET_HEIGHT+5);
 
32
}
 
33
 
 
34
Canvas::~Canvas()
 
35
{
 
36
  std::list<View*>::iterator it;
 
37
  for (it = node_list_.begin(); it != node_list_.end(); it++) // Traverse from front to back.
 
38
  {
 
39
    (*it)->UnReference();
 
40
  }
 
41
}
 
42
 
 
43
void Canvas::Draw(GraphicsEngine& graphics_engine, bool force_draw)
 
44
{
 
45
  Geometry base = GetGeometry();
 
46
  graphics_engine.PushClippingRectangle(base);
 
47
 
 
48
  GetPainter().PaintShape(graphics_engine, base, Color(0xFF191919), eSHAPE_CORNER_ROUND10, false);
 
49
 
 
50
  graphics_engine.PopClippingRectangle();
 
51
}
 
52
 
 
53
void Canvas::DrawContent(GraphicsEngine& graphics_engine, bool force_draw)
 
54
{
 
55
  graphics_engine.PushClippingRectangle(GetGeometry());
 
56
 
 
57
  // Draw from Back to front
 
58
  std::list<View*>::reverse_iterator rev_iter;
 
59
  for(rev_iter = node_list_.rbegin(); rev_iter != node_list_.rend(); rev_iter++)
 
60
  {
 
61
    (*rev_iter)->ProcessDraw(graphics_engine, force_draw);
 
62
  }
 
63
 
 
64
  graphics_engine.PopClippingRectangle();
 
65
}
 
66
 
 
67
void Canvas::PreLayoutManagement()
 
68
{
 
69
 
 
70
}
 
71
 
 
72
long Canvas::PostLayoutManagement(long LayoutResult)
 
73
{
 
74
  long ret = 0;
 
75
  Geometry base = GetGeometry();
 
76
 
 
77
  std::list<View*>::iterator it;
 
78
  for(it = node_list_.begin(); it != node_list_.end(); it++)
 
79
  {
 
80
      (*it)->ComputeContentSize();
 
81
  }
 
82
 
 
83
  ret = eCompliantHeight | eCompliantWidth;
 
84
  return ret;
 
85
}
 
86
 
 
87
void Canvas::ComputeContentPosition(float offsetX, float offsetY)
 
88
{
 
89
  Geometry base = GetGeometry();
 
90
 
 
91
  std::list<View*>::iterator it;
 
92
  for(it = node_list_.begin(); it != node_list_.end(); it++)
 
93
  {
 
94
      (*it)->ComputeContentPosition(0, 0);
 
95
  }
 
96
}
 
97
 
 
98
void Canvas::OnChildQueueDraw(Area* area)
 
99
{
 
100
  QueueDraw();
 
101
}
 
102
 
 
103
void Canvas::AddView(View* node)
 
104
{
 
105
  // Add a unique View in the list, at the top of the stack.
 
106
  std::list<View*>::iterator iter;
 
107
  iter = std::find(node_list_.begin(), node_list_.end(), node);
 
108
  if(iter == node_list_.end())
 
109
  {
 
110
    node->Reference();
 
111
    node_list_.push_front(node);
 
112
    node->SetReconfigureParentLayoutOnGeometryChange(false);
 
113
 
 
114
    node->queue_draw.connect(sigc::mem_fun(this, &Canvas::OnChildQueueDraw));
 
115
    node->child_queue_draw.connect(sigc::mem_fun(this, &Canvas::OnChildQueueDraw));
 
116
  }
 
117
}
 
118
 
 
119
void Canvas::RemoveView( View* node)
 
120
{
 
121
  // Remove a View from the list.
 
122
  std::list<View*>::iterator iter;
 
123
  iter = std::find(node_list_.begin(), node_list_.end(), node);
 
124
  if(iter != node_list_.end())
 
125
  {
 
126
    node_list_.erase(iter);
 
127
  }
 
128
}
 
129
 
 
130
void Canvas::SetGridSnaping(unsigned int GridSpacing)
 
131
{
 
132
  grid_snapping_ = (GridSpacing > 0) ? GridSpacing : 1;
 
133
}
 
134
 
 
135
void Canvas::RecvViewMouseDown(int x, int y, long button_flags, long key_flags, View* view)
 
136
{
 
137
  // Make sure the view is a child of the canvas view
 
138
  std::list<View*>::iterator it;
 
139
  it = std::find(node_list_.begin(), node_list_.end(), view);
 
140
 
 
141
  if (it == node_list_.end())
 
142
  {
 
143
    return;
 
144
  }
 
145
 
 
146
  hit_point_ = Point(x, y);
 
147
 
 
148
  //   // head_view_ holds the node that just received a mouse down. This node will be pushed at the top of the stack;
 
149
  //   head_view_ = view;
 
150
  //   selected_view_ = view;
 
151
}
 
152
 
 
153
void Canvas::RecvViewMouseDrag(int x, int y, int dx, int dy, long button_flags, long key_flags, View* view)
 
154
{
 
155
  // Make sure the view is a child of the canvas view
 
156
  std::list<View*>::iterator it;
 
157
  it = std::find(node_list_.begin(), node_list_.end(), view);
 
158
 
 
159
  if (it == node_list_.end())
 
160
  {
 
161
    return;
 
162
  }
 
163
 
 
164
  int ddx, ddy;
 
165
  ddx = ddy = 0;
 
166
 
 
167
  if(dx > 0)
 
168
  {
 
169
    if(x - hit_point_.x > 0 )
 
170
      ddx = x - hit_point_.x;
 
171
  }
 
172
  if(dx < 0)
 
173
  {
 
174
    if(x - hit_point_.x < 0 )
 
175
      ddx = x - hit_point_.x;
 
176
  }
 
177
 
 
178
  if(dy > 0)
 
179
  {
 
180
    if(y - hit_point_.y > 0)
 
181
      ddy = y - hit_point_.y;
 
182
  }
 
183
 
 
184
  if(dy < 0)
 
185
  {
 
186
    if(y - hit_point_.y < 0)
 
187
      ddy = y - hit_point_.y;
 
188
  }
 
189
 
 
190
 
 
191
  Geometry geo = view->GetGeometry();
 
192
  geo.OffsetPosition(ddx, ddy);
 
193
 
 
194
  if(geo.x < 0)
 
195
    geo.SetX(0);
 
196
  if(geo.y < 0)
 
197
    geo.SetY(0);
 
198
 
 
199
  int X = (geo.x/1) * 1;
 
200
  int Y = (geo.y/1) * 1;
 
201
  geo.SetX(X);
 
202
  geo.SetY(Y);
 
203
 
 
204
  view->SetGeometry(geo);
 
205
 
 
206
  QueueDraw();
 
207
}
 
208
 
 
209
void Canvas::CanvasNeedRedraw()
 
210
{
 
211
  QueueDraw();
 
212
  // All nodes need to be redrawn.
 
213
  std::list<View*>::iterator iter;
 
214
  for(iter = node_list_.begin(); iter != node_list_.end(); iter++)
 
215
  {
 
216
    (*iter)->QueueDraw();
 
217
  }
 
218
}
 
219
 
 
220
Area* Canvas::FindAreaUnderMouse(const Point& mouse_position, NuxEventType event_type)
 
221
{
 
222
  std::list<View*>::iterator it;
 
223
  bool mouse_inside = TestMousePointerInclusionFilterMouseWheel(mouse_position, event_type);
 
224
 
 
225
  if (mouse_inside == false)
 
226
    return NULL;
 
227
 
 
228
  if (node_list_.size() == 0)
 
229
  {
 
230
    selected_view_ = NULL;
 
231
    head_view_ = NULL;
 
232
    return NULL;
 
233
  }
 
234
 
 
235
  if (event_type == NUX_MOUSE_PRESSED)
 
236
  {
 
237
    selected_view_ = NULL;
 
238
    head_view_ = NULL;
 
239
  }
 
240
 
 
241
  if (event_type == NUX_MOUSE_MOVE)
 
242
  {
 
243
    int i = 4;
 
244
  }
 
245
 
 
246
  Area* target_area = NULL;
 
247
  bool mouse_inside_child_view = false;
 
248
  
 
249
  const Point relative_mouse_position = Point(mouse_position.x - GetX(), mouse_position.y - GetY());
 
250
 
 
251
  for (it = node_list_.begin(); it != node_list_.end(); it++) // Traverse from front to back.
 
252
  {
 
253
    mouse_inside_child_view = (*it)->TestMousePointerInclusionFilterMouseWheel(relative_mouse_position, event_type);
 
254
    if (mouse_inside_child_view)
 
255
    {
 
256
      target_area = (*it)->FindAreaUnderMouse(relative_mouse_position, event_type);
 
257
      break;
 
258
    }
 
259
  }
 
260
 
 
261
  if (mouse_inside_child_view && node_list_.size())
 
262
  {
 
263
    // The mouse has landed on a child view. bring that child view to the front.
 
264
 
 
265
    if (event_type == NUX_MOUSE_PRESSED)
 
266
    {
 
267
      head_view_ = (*it);
 
268
      selected_view_ = head_view_;
 
269
 
 
270
      if(it != node_list_.end())
 
271
      {
 
272
        std::list<View*>::iterator iter;
 
273
        iter = std::find(node_list_.begin(), node_list_.end(), head_view_);
 
274
        if (iter != node_list_.end())
 
275
        {
 
276
          node_list_.erase(iter);
 
277
          node_list_.push_front(head_view_);
 
278
        }
 
279
      }
 
280
    }
 
281
 
 
282
//     // All nodes need to be redrawn.
 
283
//     for (iter = node_list_.begin(); iter != node_list_.end(); iter++)
 
284
//     {
 
285
//       (*iter)->QueueDraw();
 
286
//     }
 
287
    return target_area;
 
288
  }
 
289
 
 
290
  if ((event_type == NUX_MOUSE_WHEEL) && (!AcceptMouseWheelEvent()))
 
291
    return NULL;
 
292
  return this;
 
293
}
 
294
}
 
295