Packet::Packet(string packetData){ // Parse the packet into the object stringstream deserializer(packetData); string type; deserializer >> type; string d; string t; string dateTime; deserializer >> d; deserializer >> t; stringstream dateTimeStream; dateTimeStream << d << " " << t; dateTime = dateTimeStream.str(); const char * timeBuffer =dateTime.c_str(); tm date; strptime(timeBuffer, "%Y-%m-%d %H:%M:%S", &date); time_t packetTime = mktime(&date); setTimeSent(packetTime); int life; deserializer >> life; setLifeTime(life); int pno; deserializer >> pno; setPacketNumber(pno); int hop; deserializer >> hop; hop++; setHopCount(hop); string src; deserializer >> src; setSourceAddress(src); string dest; deserializer >> dest; setDestinationAddress(dest); stringstream payloadStream; payloadStream << deserializer.rdbuf(); string pay = payloadStream.str(); pay = pay.substr(1, pay.length()); setPayload(pay); }
bool Card::handleEvent( SDL_Event* e ) { bool isEvent = false; float lDist, dDist, dTheta, dist; /*-----------------------------MOUSE_EVENT-------------------------------------------*/ if( (e->type == SDL_MOUSEMOTION || e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_MOUSEBUTTONUP || e->type == SDL_MOUSEWHEEL)) { //Get mouse position int x, y; SDL_GetMouseState( &x, &y ); double new_x = x - (pos[0] + width/2); double new_y = y - (pos[1] + height/2); x = cos((M_PI * angle)/180)*new_x + sin((M_PI * angle)/180)*new_y; y = cos((M_PI * angle)/180)*new_y - sin((M_PI * angle)/180)*new_x; if (isInside(x, y)) { //calculate new position for card //float newPosx = x - touchPos.x; //float newPosy = y - touchPos.y; //Set mouse over sprite switch( e->type ) { case SDL_MOUSEMOTION: /* if (isTrans) { SDL_GetMouseState( &x, &y ); pos.x = x - touchPos.x; pos.y = y - touchPos.y; setLifeTime(clock() + 10*CLOCKS_PER_SEC/2); } */ break; case SDL_MOUSEBUTTONDOWN: isEvent = true; setLifeTime(clock() + 10*CLOCKS_PER_SEC/2); int x2, y2; SDL_GetMouseState( &x2, &y2 ); touchPos.x = x2 - pos.x; touchPos.y = y2 - pos.y; isTrans = true; if (y < -height/2 + 9*width/50 && abs(x) < width/2 - width/25 ) { isReading = !isReading; } else if (isReading && y > height/2 - width/10 && abs(x) < width/2 - width/25 ) { isReading = false; } break; case SDL_MOUSEBUTTONUP: //currentEvent = "BUTTON_SPRITE_MOUSE_UP"; //std::cout << currentEvent << std::endl; touchPos = glm::vec2(-1.0f,-1.0f); isTrans = false; break; case SDL_MOUSEWHEEL: double scaleFactor = 1.0; setLifeTime(clock() + 10*CLOCKS_PER_SEC/2); if (e->wheel.y < 0 ) scaleFactor = 0.98; else if (e->wheel.y > 0) scaleFactor = 1.02; if (width*scaleFactor < SCREEN_WIDTH/2) { width *= scaleFactor; height *= scaleFactor; } isEvent = true; break; } } } /*---------------------------------TOUCH_EVENT------------------------------------------* if (e->type == SDL_FINGERMOTION || e->type == SDL_FINGERDOWN|| e->type == SDL_FINGERUP) { double x = e->tfinger.x * SCREEN_WIDTH; double y = e->tfinger.y * SCREEN_HEIGHT; glm::vec2 lastP; glm::vec2 lastCentroid; glm::vec2 newCentroid; double new_x = x - (pos[0] + width/2); double new_y = y - (pos[1] + height/2); x = cos((M_PI * angle)/180)*new_x + sin((M_PI * angle)/180)*new_y; y = cos((M_PI * angle)/180)*new_y - sin((M_PI * angle)/180)*new_x; if (isInside(x, y)) { isEvent = true; //save the touchfinger events in a variable SDL_TouchFingerEvent tf = e->tfinger; float newPosx = pos.x+e->tfinger.dx*SCREEN_WIDTH; float newPosy = pos.y+e->tfinger.dy*SCREEN_HEIGHT; switch( e->type ) { case SDL_FINGERDOWN: if (numberFingers == 0 || fingerEvents[0].x == -1) { fingerEvents[0] = tf; numberFingers++; } else if (numberFingers == 1 || fingerEvents[1].x == -1) { fingerEvents[1] = tf; numberFingers++; } // std::cout << "nf: " << numberFingers << std::endl; setLifeTime(time(0) + 10); touchPos.x = x - pos.x; touchPos.y = y - pos.y; isTrans = true; isReading = !isReading; // std::cout << isReading; // std::cout << "number fingers: " << numberFingers << std::endl; break; case SDL_FINGERMOTION: //if only 1 finger move card if (numberFingers < 2) { fingerEvents[0] = tf; // makes sure cards can't be forced outside of screen if (isTrans ) { //isEvent = true; pos.x = pos.x + tf.dx*SCREEN_WIDTH; pos.y = pos.y + tf.dy*SCREEN_HEIGHT; setLifeTime(time(0) + 10); //add time before death } } //otherwise it should be scaled and/or rotated else { SDL_TouchFingerEvent thatFinger; lastP.x = tf.x - tf.dx; //the fingers position lastP.y = tf.y - tf.dy; if (std::abs(fingerEvents[0].x - lastP.x) < EPSILON && std::abs(fingerEvents[0].y - lastP.y) < EPSILON) { thatFinger = fingerEvents[1]; fingerEvents[0] = tf; } else if (std::abs(fingerEvents[1].x - (tf.x - tf.dx)) < EPSILON && std::abs(fingerEvents[1].y - (tf.y - tf.dy)) < EPSILON) { thatFinger = fingerEvents[0]; fingerEvents[1] = tf; } lastCentroid.x = lastP.x - (lastP.x - thatFinger.x)/2; lastCentroid.y = lastP.y - (lastP.y - thatFinger.y)/2; newCentroid.x = lastCentroid.x + e->tfinger.dx/2; newCentroid.y = lastCentroid.y + e->tfinger.dy/2; glm::vec2 lv, v; lv.x = lastP.x - lastCentroid.x; //from center to last position lv.y = lastP.y - lastCentroid.y; lDist = sqrt(pow(lv.x,2) + pow(lv.y,2)); v.x = tf.x - newCentroid.x; //from center to current pos v.y = tf.y - newCentroid.y; dist = sqrt(pow(v.x,2) + pow(v.y,2)); dDist = (dist - lDist); //std::cout << "dDist " << dDist << std::endl; double fingerDist = sqrt(pow(thatFinger.x - lastP.x ,2) + pow(thatFinger.y - lastP.y ,2)); double scaleFactor = (fingerDist + dDist)/fingerDist; //std::cout << "skal: " << scaleFactor << std::endl; if (std::abs(scaleFactor - 1.0) < 0.1) { width *= scaleFactor; height *= scaleFactor; } glm::vec2 p1 = glm::vec2(lastP.x - thatFinger.x, lastP.y - thatFinger.y); glm::vec2 p2 = glm::vec2(tf.x - thatFinger.x, tf.y - thatFinger.y); // arctan(v X u/v * u) dTheta = atan2(lv.x*v.y - lv.y*v.x,lv.x*v.x + lv.y*v.y)*180/3.14; //dTheta = acos((v.x*lv.x + v.y*lv.y)/(sqrt(pow(v.x,2) + sqrt(pow(v.y,2)))*sqrt(pow(lv.x,2)+pow(lv.y,2)))); if (std::abs(dTheta) > 3.14/1080) { angle += dTheta; } } break; case SDL_FINGERUP: if (std::abs(tf.x - fingerEvents[0].x) < EPSILON && std::abs(tf.y - fingerEvents[0].y) < EPSILON) { fingerEvents[0].x = -1; std::cout << "finger 1 upp" << std::endl; } else if (std::abs(tf.x - fingerEvents[1].x) < EPSILON && std::abs(tf.y - fingerEvents[1].y) < EPSILON) { fingerEvents[1].x = -1; std::cout << "finger 2 upp" << std::endl; } touchPos = glm::vec2(-1.0f,-1.0f); if (isTrans) numberFingers--; if (numberFingers == 0) isTrans = false; // std::cout << "up: " << std::abs(tf.x + tf.dx - fingerEvents[0].x) << std::endl; std::cout << "nf : " << numberFingers << std::endl; break; } } }*/ return isEvent; }