bool KrEncoder::EncodeSprite( SDL_Surface* surface, const AllInfo& allInfo, KrConsole* console ) { // gedString spriteName, actionName; // TiXmlElement* action; // TiXmlElement* frame; // // spriteName = "no_sprite_name"; // if ( sprite->Attribute( "name" ) ) // { // spriteName = *(sprite->Attribute( "name" )); // } // Create or locate the sprite. if ( !vault.GetSpriteResource( allInfo.name ) ) { vault.AddResource( new KrSpriteResource( allInfo.name ) ); } KrSpriteResource* spriteResource = vault.GetSpriteResource( allInfo.name ); GLASSERT( spriteResource ); GlDynArray< int > rotation; if ( allInfo.rotation.Count() ) rotation = allInfo.rotation; else rotation.PushBack( 0 ); for( int i=0; i<(int) rotation.Count(); ++i ) { // Get or create the action. // If using rotation, append to the name. gedString action = allInfo.action; if ( rotation.Count() > 1 ) { char buf[16]; sprintf( buf, ".ROT%03d", rotation[i] ); action += buf; } if ( !spriteResource->GetAction( action ) ) { spriteResource->AddAction( new KrAction( action ) ); } KrAction* actionRes = spriteResource->GetAction( action ); GLASSERT( actionRes ); int index = actionRes->NumFrames(); actionRes->AddFrame(); KrPaintInfo info( surface ); KrRle* rle = actionRes->GetFrame( index ); if ( allInfo.isoTargetWidth > 0 ) { CreateIsoTile( &info, console, allInfo.x, allInfo.y, allInfo.width, allInfo.height, rle, allInfo.isoTargetWidth, rotation[i] ); } else { rle->Create( &info, allInfo.x, allInfo.y, allInfo.width, allInfo.height, allInfo.hotx, allInfo.hoty, allInfo.deltax, allInfo.deltay ); } PrintSprite( console, allInfo.name, action, index, actionRes->GetFrame( index ) ); } return true; }
void KrEventManager::HandleEvent( const SDL_Event& event, KrEngine* engine ) { if ( event.type == SDL_KEYDOWN && keyFocus >= 0) //maks { // - the tab key changes key focus. // - accelerators are checked // - keys passed through to the handler. #ifdef DEBUG GLOUTPUT( "KeyDown mod=%d sym=%d, unicode=%d, name=%s\n", event.key.keysym.mod, event.key.keysym.sym, event.key.keysym.unicode, SDL_GetKeyName(event.key.keysym.sym)); //maks #endif if ( event.key.keysym.sym == SDLK_TAB && keyListeners.Count() > 1 ) { if ( event.key.keysym.mod & KMOD_SHIFT ) ChangeKeyFocus( keyFocus + keyListeners.Count() - 1 ); else ChangeKeyFocus( keyFocus + 1 ); return; } for( int i=0; i<accelListeners.Count(); ++i ) { int sym = accelListeners[i].keysym; int mod = accelListeners[i].keymod; if ( event.key.keysym.sym == sym && event.key.keysym.mod & mod && keyListeners.Count() && accelListeners[i].target == keyListeners[ keyFocus ]) //maks: send accelerators for key focus owners { accelListeners[i].target->Accelerate( true, mod, sym ); //maks return; } } if ( keyListeners.Count() > 0 ) { keyFocus = GlClamp( keyFocus, 0, int( keyListeners.Count()-1 ) ); KrWidget* widget = keyListeners[ keyFocus ]; // Go up the chain until handled. while( widget && !widget->KeyEvent( event ) ) { widget = widget->ParentWidget(); } } } else if ( event.type == SDL_KEYUP ) { // - only accelerates key up for( int i=0; i<accelListeners.Count(); ++i ) { if ( event.key.keysym.sym == accelListeners[i].keysym && event.key.keysym.mod & accelListeners[i].keymod && keyFocus >= 0 && keyListeners.Count() && accelListeners[i].target == keyListeners[ keyFocus ]) //maks { accelListeners[i].target->Accelerate( false, event.key.keysym.mod, event.key.keysym.sym ); //maks return; } } //Send shift key up if ( keyListeners.Count() > 0 && (event.key.keysym.sym == SDLK_LSHIFT || event.key.keysym.sym == SDLK_RSHIFT)) { keyFocus = GlClamp( keyFocus, 0, int( keyListeners.Count()-1 ) ); KrWidget* widget = keyListeners[ keyFocus ]; // Go up the chain until handled. while( widget && !widget->KeyEvent( event ) ) { widget = widget->ParentWidget(); } } } else if ( event.type == SDL_MOUSEMOTION ) { GlDynArray<KrImage*> hitArray; KrWidget* hit = 0; //int window = engine->GetWindowFromPoint( event.motion.x, event.motion.y ); KrVector2T< GlFixed > object; #ifndef _WIN32_WCE if(mouseDown && mouseFocus) //maks { //Don't change the focus if mouse button is down //Don't works on Pocket PC. In input.ged don't close the SIP keyboard (only send keyFocus = true) hit = mouseFocus; hit->ScreenToObject( event.motion.x, event.motion.y, &object/*, window*/ ); } else #endif { engine->Tree()->HitTest( event.motion.x, event.motion.y, KrImageTree::ALWAYS_INSIDE_BOX, //| GET_ALL_HITS, &hitArray/*, &window*/ ); for( int i=0; i<hitArray.Count(); ++i ) { KrImNode* parent = hitArray[i]->Parent(); while( parent ) { if ( parent->ToWidget() ) { hit = parent->ToWidget(); hit->ScreenToObject( event.motion.x, event.motion.y, &object/*, window*/ ); break; } parent = parent->Parent(); } } } // 1) Something has the focus. Nothing had it before. // 2) Something has the focus, something else had it before. // 3) Something loses the focus. // 5) The thing with focus gets a move. if ( hit && !mouseFocus ) { mouseFocus = hit; mouseFocus->MouseIn( mouseDown, true ); mouseFocus->MouseMove( mouseDown, object.x.ToIntRound(), object.y.ToIntRound() ); } else if ( hit && mouseFocus && mouseFocus != hit ) { mouseFocus->MouseIn( mouseDown, false ); mouseFocus = hit; mouseFocus->MouseIn( mouseDown, true ); mouseFocus->MouseMove( mouseDown, object.x.ToIntRound(), object.y.ToIntRound() ); } else if ( !hit && mouseFocus ) { mouseFocus->MouseIn( mouseDown, false ); mouseFocus = hit; } else if ( hit && hit == mouseFocus ) { GLASSERT( hit == mouseFocus ); mouseFocus->MouseMove( mouseDown, object.x.ToIntRound(), object.y.ToIntRound() ); } else if ( !hit && !mouseFocus ) { // nothing to do } else { GLASSERT( 0 ); } } else if ( event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP ) { if ( event.button.button == SDL_BUTTON_LEFT ) { bool down = event.button.state != 0; // & SDL_BUTTON_LMASK; if ( down != mouseDown ) { mouseDown = down; if ( mouseFocus ) { int window = engine->GetWindowFromPoint( event.motion.x, event.motion.y ); KrVector2T< GlFixed > object; mouseFocus->ScreenToObject( event.motion.x, event.motion.y, &object/*, window*/ ); mouseFocus->MouseClick( mouseDown ? KrWidget::LEFT_DOWN : KrWidget::LEFT_UP, object.x.ToIntRound(), object.y.ToIntRound() ); } else //maks { if( keyFocus >= 0 && keyFocus < keyListeners.Count() && !newListnerFocus //maks: Only remove the focus if the widget don't has changed the focus in this cycle (solve focus bug in Math for Kids.ged) ) { keyListeners[ keyFocus ]->KeyFocus( false ); keyFocus = -1; FocusState(false); } } } } else if ( event.button.button == SDL_BUTTON_RIGHT ) //maks { bool down = event.button.state != 0; if ( down != mouseDown ) { mouseDown = down; if ( mouseFocus ) { int window = engine->GetWindowFromPoint( event.motion.x, event.motion.y ); KrVector2T< GlFixed > object; mouseFocus->ScreenToObject( event.motion.x, event.motion.y, &object/*, window*/ ); mouseFocus->MouseClick( mouseDown ? KrWidget::RIGHT_DOWN : KrWidget::RIGHT_UP, object.x.ToIntRound(), object.y.ToIntRound() ); } } } } newListnerFocus = false; //maks }
void KrEncoder::CalcAllInfo( TiXmlNode* node, AllInfo* i, SDL_Surface* surface ) { TiXmlElement* ele = node->ToElement(); if ( !ele ) { GLASSERT( 0 ); return; } // Walk up the tree, get information as we go. TiXmlNode* parent = ele->Parent(); while( parent ) { TiXmlElement* parentEle = parent->ToElement(); if ( parentEle ) { if ( parentEle->Value() == "Definition" ) { // i->format = FORMAT_DEF; // // if ( parentEle->Attribute( "filename" ) ) // i->filename = *parentEle->Attribute( "filename" ); // We need go no higher. break; } else if ( parentEle->Value() == "Sprite" ) { i->type = TYPE_SPRITE; if ( parentEle->Attribute( "name" ) ) i->name = *parentEle->Attribute( "name" ); } else if ( parentEle->Value() == "Action" ) { if ( parentEle->Attribute( "name" ) ) i->action = *parentEle->Attribute( "name" ); } else if ( parentEle->Value() == "File" ) { // if ( parentEle->Attribute( "filename" ) ) // i->filename = *parentEle->Attribute( "filename" ); } else if ( parentEle->Value() == "Direct" ) { //i->format = FORMAT_DIRECT; // Go no higher. break; } } parent = parent->Parent(); } // Now interpret the element itself: if ( ele->Value() == "Image" ) { // Could be sprite or tile. i->useEntireImage = true; } else if ( ele->Value() == "ColorKey" ) { // Could be sprite on tile. //i->useEntireImage = false; } else if ( ele->Value() == "Frame" ) { i->type = TYPE_SPRITE; } else if ( ele->Value() == "Font" ) { i->type = TYPE_FONT; } else if ( ele->Value() == "Tile" ) { i->type = TYPE_TILE; } // And its attributes. They don't have different meanings in different // tags, so they can all be read in together. // ColorKey and Image attributes: if ( ele->Attribute( "tile" ) ) { GLASSERT( i->type == TYPE_NONE ); i->name = *ele->Attribute( "tile" ); i->type = TYPE_TILE; } if ( ele->Attribute( "sprite" ) ) { GLASSERT( i->type == TYPE_NONE ); i->name = *ele->Attribute( "sprite" ); i->type = TYPE_SPRITE; } if ( ele->Attribute( "color" ) ) { gedString c = *ele->Attribute( "color" ); i->keyColor.FromString( c.c_str() ); i->keyColor.c.alpha = KrRGBA::KR_OPAQUE; // alpha not used } if ( ele->Attribute( "frameCount" ) ) { ele->Attribute( "frameCount", &i->frameCount ); } if ( ele->Attribute( "action" ) ) { i->action = *ele->Attribute( "action" ); } if ( ele->Attribute( "sprite" ) ) { i->name = *ele->Attribute( "sprite" ); } // Used by tile and font: if ( ele->Attribute( "name" ) ) { i->name = *ele->Attribute( "name" ); } // Font attributes: if ( i->type == TYPE_FONT ) { if ( ele->Attribute( "start" ) ) { ele->Attribute( "start", &i->fontStart ); } if ( ele->Attribute( "space" ) ) { ele->Attribute( "space", &i->space ); } if ( ele->Attribute( "type" ) ) { if ( *ele->Attribute( "type" ) == "sfont" ) i->subType = SUBTYPE_SFONT; } } // Generic attributes: if ( ele->Attribute( "x" ) ) { ele->Attribute( "x", &i->x ); } if ( ele->Attribute( "y" ) ) { ele->Attribute( "y", &i->y ); } if ( ele->Attribute( "size" ) ) { GLASSERT( i->type == TYPE_TILE ); ele->Attribute( "size", &i->width ); i->height = i->width; // size is height and width for tiles. } if ( ele->Attribute( "width" ) ) { ele->Attribute( "width", &i->width ); } if ( ele->Attribute( "height" ) ) { ele->Attribute( "height", &i->height ); } if ( ele->Attribute( "hotspotx" ) ) { //i->hasHotspot = true; ele->Attribute( "hotspotx", &i->hotx); } if ( ele->Attribute( "hotspoty" ) ) { //i->hasHotspot = true; ele->Attribute( "hotspoty", &i->hoty ); } if ( ele->Attribute( "deltax" ) ) { //i->hasDelta = true; ele->Attribute( "deltax", &i->deltax ); } if ( ele->Attribute( "deltay" ) ) { //i->hasDelta = true; ele->Attribute( "deltay", &i->deltay ); } if ( ele->Attribute( "isotile" ) ) { i->isoTargetWidth = 0; ele->Attribute( "isotile", &i->isoTargetWidth ); } if ( ele->Attribute( "length" ) ) { ele->Attribute( "length", &i->fontLength ); } if ( ele->Attribute( "rotation" ) ) { gedString r = *ele->Attribute( "rotation" ); GlString::RemoveWhiteSpace( &r ); GlDynArray< gedString > strArray; GlString::Split( &strArray, r, ",", false ); for( int k=0; k<(int)strArray.Count(); ++k ) { if ( strArray[k].length() > 1 && strArray[k].at( 0 ) == 'd' ) { int division = strArray[k].at( 1 ) - '0'; //maks if ( division > 0 ) { int increment = 360 / division; if ( increment > 0 ) { for( int theta = 0; theta < 360; theta += increment ) { i->rotation.PushBack( theta ); } } } } else { int theta = atoi( strArray[k].c_str() ); i->rotation.PushBack( theta ); } } i->rotation.Sort(); } if ( i->useEntireImage ) { i->x = 0; i->y = 0; i->width = surface->w; i->height = surface->h; } }