Back to index

nux  3.0.0
TestView.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012 Inalogic Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License version 3, as published
00006  * by the  Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * version 3 along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>
00016  *
00017  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00018  *
00019  */
00020 
00021 #include "Nux/Nux.h"
00022 #include "TestView.h"
00023 
00024 /*
00025   TestView:
00026     This class is a Nux view that processes all mouse events that it receives. It is meant for testing simulated
00027     mouse events. 
00028 */
00029 namespace nux {
00030 NUX_IMPLEMENT_OBJECT_TYPE(TestView);
00031 
00032 TestView::TestView(NUX_FILE_LINE_DECL)
00033 : nux::View(NUX_FILE_LINE_PARAM)
00034 , can_focus_(true)
00035 , calls_to_queue_draw_(0)
00036 {
00037   ResetEvents();
00038   ResetKeyFocusEvents();
00039 
00040   key_nav_direction_ = nux::KEY_NAV_NONE;
00041   normal_color_ = nux::color::Green;
00042   mouse_down_color_ = nux::color::Red;
00043   mouse_drag_color_ = nux::color::Yellow;
00044   mouse_in_color_ = nux::color::Blue;
00045   current_color_ = normal_color_;
00046   with_key_focus_color_ = nux::color::White;
00047   without_key_focus_color_ = normal_color_;
00048 
00049   has_focus_ = false;
00050   mouse_in_ = false;
00051   mouse_mouse_drag_ = false;
00052   mouse_mouse_down_ = false;
00053 
00054   mouse_down.connect(sigc::mem_fun(this, &TestView::OnMouseDown));
00055   mouse_up.connect(sigc::mem_fun(this, &TestView::OnMouseUp));
00056   mouse_drag.connect(sigc::mem_fun(this, &TestView::OnMouseDrag));
00057   mouse_click.connect(sigc::mem_fun(this, &TestView::OnMouseClick));
00058   mouse_double_click.connect(sigc::mem_fun(this, &TestView::OnMouseDoubleClick));
00059   mouse_move.connect(sigc::mem_fun(this, &TestView::OnMouseMove));
00060   mouse_enter.connect(sigc::mem_fun(this, &TestView::OnMouseEnter));
00061   mouse_leave.connect(sigc::mem_fun(this, &TestView::OnMouseLeave));
00062   key_nav_focus_change.connect(sigc::mem_fun(this, &TestView::OnKeyNavFocusChange));
00063   begin_key_focus.connect(sigc::mem_fun(this, &TestView::OnBeginKeyFocus));
00064   end_key_focus.connect(sigc::mem_fun(this, &TestView::OnEndKeyFocus));
00065   object_destroyed.connect(sigc::mem_fun(this, &TestView::OnObjectDestroyed));
00066 
00067 }
00068 
00069 TestView::~TestView()
00070 {
00071 
00072 }
00073 
00074 void TestView::ResetEvents()
00075 {
00076   registered_mouse_down_          = false;
00077   registered_mouse_up_            = false;
00078   registered_mouse_drag_          = false;
00079   registered_mouse_enter_         = false;
00080   registered_mouse_leave_         = false;
00081   registered_mouse_click_         = false;
00082   registered_mouse_double_click_  = false;
00083   registered_mouse_move_          = false;
00084   registered_mouse_enter_         = false;
00085   registered_mouse_leave_         = false;
00086   registered_object_destroyed_    = false;
00087     
00088 }
00089 
00090 void TestView::ResetKeyFocusEvents()
00091 {
00092   registered_begin_keynav_focus_ = false;
00093   registered_end_keynav_focus_ = false;
00094 }
00095 
00096 void TestView::QueueDraw()
00097 {
00098   ++calls_to_queue_draw_;
00099   nux::View::QueueDraw();
00100 }
00101 
00102 nux::Color TestView::GetColor() const
00103 {
00104   return current_color_;
00105 }
00106 
00107 void TestView::Draw(nux::GraphicsEngine& graphics_engine, bool force_draw)
00108 {
00109   nux::Geometry geo = GetGeometry();
00110   graphics_engine.QRP_Color(geo.x, geo.y, geo.width, geo.height, current_color_);
00111 
00112 if (has_focus_)
00113   graphics_engine.QRP_Color(geo.x, geo.y, 20, 20, with_key_focus_color_);  
00114 }
00115 
00116 void TestView::OnMouseDown(int x, int y, unsigned long button_flags, unsigned long key_flags)
00117 {
00118   registered_mouse_down_ = true;
00119   current_color_ = mouse_down_color_;
00120   mouse_mouse_down_ = true;
00121   QueueDraw();
00122 }
00123 
00124 void TestView::OnMouseUp(int x, int y, unsigned long button_flags, unsigned long key_flags)
00125 {
00126   registered_mouse_up_ = true;
00127   if (mouse_in_)
00128     current_color_ = mouse_in_color_;
00129   else
00130     current_color_ = normal_color_;
00131 
00132   mouse_mouse_down_ = false;
00133   mouse_mouse_drag_ = false;
00134   QueueDraw();
00135 }
00136 
00137 void TestView::OnMouseDrag(int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
00138 {
00139   registered_mouse_drag_ = true;
00140   current_color_ = mouse_drag_color_;
00141   mouse_mouse_drag_ = true;
00142   QueueDraw();
00143 }
00144 
00145 void TestView::OnMouseClick(int x, int y, unsigned long button_flags, unsigned long key_flags)
00146 {
00147   registered_mouse_click_ = true;
00148   QueueDraw();
00149 }
00150 
00151 void TestView::OnMouseDoubleClick(int x, int y, unsigned long button_flags, unsigned long key_flags)
00152 {
00153   registered_mouse_double_click_ = true;
00154   current_color_ = mouse_down_color_;
00155   mouse_mouse_down_ = true;
00156   QueueDraw();
00157 }
00158 
00159 void TestView::OnMouseMove(int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
00160 {
00161   registered_mouse_move_ = true;
00162   QueueDraw();
00163 }
00164 
00165 void TestView::OnMouseEnter(int x, int y, unsigned long button_flags, unsigned long key_flags)
00166 {
00167   registered_mouse_enter_ = true;
00168   mouse_in_ = true;
00169   current_color_ = mouse_in_color_;
00170   QueueDraw();
00171 }
00172 
00173 void TestView::OnMouseLeave(int x, int y, unsigned long button_flags, unsigned long key_flags)
00174 {
00175   registered_mouse_leave_ = true;
00176   mouse_in_ = false;
00177   current_color_ = normal_color_;
00178   QueueDraw();
00179 }
00180 
00181 void TestView::OnKeyNavFocusChange(nux::Area* area, bool has_focus, nux::KeyNavDirection direction)
00182 {
00183   has_focus_ = HasKeyFocus();
00184   
00185   has_focus_ = has_focus;
00186   key_nav_direction_ = direction;
00187   QueueDraw();
00188 }
00189 
00190 void TestView::OnBeginKeyFocus()
00191 {
00192   registered_begin_keynav_focus_ = true;
00193 }
00194 
00195 void TestView::OnEndKeyFocus()
00196 {
00197   registered_end_keynav_focus_ = true;
00198 }
00199 
00200 void TestView::OnObjectDestroyed(nux::Object* object)
00201 {
00202   registered_object_destroyed_ = true;
00203 }
00204 
00205 }