MouseSenderAgent.cpp

Go to the documentation of this file.
00001 /*
00002  * This file is part of Micole Architecture
00003  *
00004  * Copyright (C) 2007 Micole partners
00005  *
00006  * Micole Architecture is free software: you can redistribute it 
00007  * and/or modify it under the terms of the GNU Lesser General 
00008  * Public License as published by the Free Software Foundation, 
00009  * either version 3 of the License, or (at your option) any 
00010  * later version.
00011  *
00012  * Micole Architecture is distributed in the hope that it will be 
00013  * useful, * but WITHOUT ANY WARRANTY; without even the implied 
00014  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00015  * PURPOSE.  See the GNU Lesser General Public License for more 
00016  * details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Micole Architecture.  If not, see
00020  * <http://www.gnu.org/licenses/>.
00021  */
00022 
00023 #include "stdafx.h"
00024 #include "MouseSenderAgent.h"
00025 #include "ReachinMicoleApplication.h"
00026 #include <MicoleStringStream.h>
00027 
00028 #include <Shape.h>
00029 #include <Appearance.h>
00030 #include <Material.h>
00031 #include <Box.h>
00032 /*#include <Sphere.h>*/
00033 
00034 MouseSenderAgent::MouseSenderAgent(int interval):
00035         MicoleAgentThread("MouseSenderAgent","SenderAgent"), 
00036         _mouseMoveListener(new MouseMoveListener), 
00037         _leftMouseButtonListener(new MouseButtonListener("BT1_")),
00038         _rightMouseButtonListener(new MouseButtonListener("BT2_")),
00039         _mouse(new Mouse()),
00040         mouse_coordinates(new SFVec3f())
00041 {
00042         _mouseMoveListener->_mssa=this;
00043         _leftMouseButtonListener->_mssa=this;
00044         _rightMouseButtonListener->_mssa=this;
00045         _mouse->position->route(_mouseMoveListener);
00046         _mouse->left_button->route(_leftMouseButtonListener);
00047         _mouse->right_button->route(_rightMouseButtonListener);
00048         _mouseMoveListener->route(_leftMouseButtonListener->mouse_coordinates);
00049         _mouseMoveListener->route(_rightMouseButtonListener->mouse_coordinates);
00050         sendMessage("MOUSE INPUT CREATED");
00051 //      ReachinMicoleApplication::getInstance()->sendIvyMessage("MOUSE INPUT CREATED");
00052 
00053         _coordUpdInterval=interval;
00054 
00055 /*
00056         Transform *xform1 = new Reachin::Transform();
00057         xform1->translation->set( Vec3f(0.05, 0.0, 0.0) );
00058         Shape *shape1 = new Shape();
00059         Shape *shape2 = new Shape();
00060         Appearance *app1 = new Appearance();
00061         Material *mat1 = new Material();
00062         Box *boxX = new Box();
00063         Box *boxY = new Box();
00064         boxX->size->set( Vec3f(0.005, 0.001, 0.001) );
00065         boxY->size->set( Vec3f(0.001, 0.005, 0.001) );
00066         mat1->diffuse_color->set( RGB(1.0, 0.0, 0.0) );
00067         app1->material->set( mat1 );
00068         shape1->appearance->set( app1 );
00069         shape2->appearance->set( app1 );
00070         shape1->geometry->set( boxX );
00071         shape2->geometry->set( boxY );
00072         xform1->children->add( shape1 );
00073         xform1->children->add( shape2 );
00074         ReachinMicoleApplication::getInstance()->getDisplay()->children->add( xform1 );
00075         mouse_coordinates->route(xform1->translation );*/
00076 
00077         start();
00078 }
00079 
00080 MouseSenderAgent::~MouseSenderAgent()
00081 {
00082 
00083 }
00084 
00085 //      : mouse_coordinates(new SFVec2f())
00086 MouseSenderAgent::MouseMoveListener::MouseMoveListener()  
00087 {
00088 }
00089 
00090 void MouseSenderAgent::run()
00091 {
00092         MicoleStringStream ss;
00093         Vec2f p;
00094 
00095         while(getState() == ACTIVE)
00096         {
00097                 if(_coordUpdInterval > 0)
00098                 {
00099                         p = _mouseMoveListener->get();
00100                 
00101 /*                      if(p.x < -0.16)
00102                                 p.x = -0.16;
00103                         else if (p.x > 0.16)
00104                                 p.x = 0.16;
00105 
00106                         if(p.y < -0.11)
00107                                 p.y = -0.11;
00108                         else if (p.y > 0.11)
00109                                 p.y = 0.11;
00110 */
00111                         ss << "IN MSE : pos=(";
00112                         ss << p.x << ", " << p.y << ");";
00113                 
00114                         sendMessage(ss.str().c_str());
00115         
00116                         ss.flush();
00117         
00118                         ::Sleep(_coordUpdInterval);
00119                 }
00120                 else
00121                         ::Sleep(50);
00122         }
00123 }
00124 
00125 void MouseSenderAgent::setCoordinateUpdateInterval(int interval)
00126 {
00127         _coordUpdInterval=interval;
00128 }
00129 
00130 
00131 void MouseSenderAgent::MouseMoveListener::evaluate(SFVec2f *coord)
00132 {
00133         MicoleStringStream ss;
00134         Vec2f in = coord->get();
00135         
00136         if(in.x < -0.15)
00137                 in.x = -0.15;
00138         else if (in.x > 0.15)
00139                 in.x = 0.15;
00140 
00141         if(in.y < -0.11)
00142                 in.y = -0.11;
00143         else if (in.y > 0.11)
00144                 in.y = 0.11;
00145 
00146         value =  Vec2f(in.x, in.y);
00147         _mssa->mouse_coordinates->set(Vec3f(in.x, in.y, 0));
00148         _mssa->mouse_coordinates->touch();
00149         
00150         /*ss << "IN MSE : pos=(";
00151         ss << mouse_coordinates->get().x << ", " << mouse_coordinates->get().y << ");";
00152         _mssa->sendMessage(ss.str().c_str());
00153 
00154         _mssa->sendMessage("coin");*/
00155 }
00156 
00157 MouseSenderAgent::MouseButtonListener::MouseButtonListener(string button)
00158 :m_button(new SFBool()), mouse_coordinates(new SFVec2f())
00159 {
00160         m_strButton=button;
00161 }
00162 
00163 void MouseSenderAgent::MouseButtonListener::evaluate(SFBool *mbutton)
00164 {
00165         value =  mbutton;
00166         m_button->set(value);
00167         if(mbutton->get()==true)
00168                 sendCoordinates(m_strButton + "PRESSED");
00169         else
00170                 sendCoordinates(m_strButton + "RELEASED");
00171         m_button->touch();
00172 }
00173 
00177 void MouseSenderAgent::MouseButtonListener::sendCoordinates(string state)
00178 {
00179         MicoleStringStream ss;
00180         ss << "IN MSE : pos=(";
00181         ss << mouse_coordinates->get().x << ", " << mouse_coordinates->get().y << ");";
00182         ss << " evt=(" << state << ");";
00183         _mssa->sendMessage(ss.str().c_str());
00184 }

Generated on Tue Oct 16 17:10:43 2007 for Micole by  doxygen 1.4.7