//TODO change it so with a key it can be skiped and not with mouse and so. void TextBox::handleEvent(sf::Event e) { float delayx, delayy; delayx = sprite.getOrigin().x*sprite.getScale().x; delayy = sprite.getOrigin().y*sprite.getScale().y; sprite.move(-delayx, -delayy); if(e.type == sf::Event::MouseButtonPressed) { } if(e.type == sf::Event::MouseButtonReleased) { if (e.mouseButton.button == sf::Mouse::Left) { is_clicked = false; } } if(e.type == sf::Event::KeyPressed) { if(e.key.code == sf::Keyboard::Space) { clicked = true; is_clicked = true; } } if(e.type == sf::Event::KeyReleased) { if(e.key.code == sf::Keyboard::Space && !textFinished) { is_clicked = false; setText(getCharacterSize()); } } sprite.move(delayx, delayy); }
void FeTextPrimative::fit_string( const std::basic_string<sf::Uint32> &s, int position, int &first_char, int &len ) { if ( position < 0 ) position = 0; else if ( position > (int)s.size() ) position = s.size(); const sf::Font *font = getFont(); unsigned int charsize = getCharacterSize(); float width = m_bgRect.getLocalBounds().width / m_texts[0].getScale().x; int running_total( charsize * 2 ); // measure of line's pixel width // start from "position", "i" measures to right, "j" to the left int i( (position == (int)s.size()) ? position : position + 1 ); int j( position ); int last_space( i ); bool found_space( false ); while (( running_total < width ) && (( i < (int)s.size() ) || ( !m_wrap && ( j > 0 )))) { if ( i < (int)s.size() ) { sf::Glyph g = font->getGlyph( s[i], charsize, false ); running_total += g.advance; if ( s[i] == L' ' ) { found_space = true; last_space = i; } i++; } if (!m_wrap && ( j > 0 ) && ( running_total < width )) { sf::Glyph g = font->getGlyph( s[j], charsize, false ); running_total += g.advance; j--; } } first_char = j; // // If we are word wrapping and found a space, then break at the space. // Otherwise, fit as much on this line as we can // if ( m_wrap && found_space && ( i != (int)s.size() )) len = last_space - j + 1; else len = i - j + 1; }
size_t IFont::getStringWidth(const std::string & data) const { size_t width = 0; for(size_t i=0; i<data.size(); i += getCharacterSize(data[i])) { width += getGlyphWidth(data.data() + i); } return width; }
sf::Vector2f FeTextPrimative::setString( const std::basic_string<sf::Uint32> &t, int position ) { // // Cut string if it is too big to fit our dimension // int first_char, len, disp_cpos; if ( m_wrap ) position = 0; fit_string( t, position, first_char, len ); m_texts[0].setString( t.substr( first_char, len ) ); disp_cpos = position - first_char; if ( m_texts.size() > 1 ) m_texts.resize( 1 ); if (( m_wrap ) && ( len < (int)t.size() )) { // // Calculate the number of lines we can fit in our RectShape // unsigned int spacing = getCharacterSize() * m_texts[0].getScale().y; const sf::Font *font = getFont(); if ( font ) spacing = font->getLineSpacing( spacing ); sf::FloatRect rectSize = m_bgRect.getLocalBounds(); int line_count = rectSize.height / spacing; // // Create the wrapped lines // position = len; int i=0; while (( position < (int)t.size() - 1 ) && ( i < line_count )) { fit_string( t, position, first_char, len ); sf::Text new_text( m_texts[0] ); new_text.setString( t.substr( first_char, len ) ); position += len; m_texts.push_back( new_text ); i++; } } set_positions(); // we need to set the positions now for findCharacterPos() to work below return m_texts[0].findCharacterPos( disp_cpos ); }
void FeTextPrimative::set_positions() const { int spacing = getCharacterSize() * m_texts[0].getScale().y; const sf::Font *font = getFont(); if (( font ) && ( font->getLineSpacing( spacing ) > spacing )) spacing = font->getLineSpacing( spacing ); sf::Vector2f rectPos = m_bgRect.getPosition(); sf::FloatRect rectSize = m_bgRect.getLocalBounds(); for ( unsigned int i=0; i < m_texts.size(); i++ ) { sf::Vector2f textPos; // we need to account for the scaling that we have applied to our text... sf::FloatRect textSize = m_texts[i].getLocalBounds(); textSize.width *= m_texts[i].getScale().x; textSize.height *= m_texts[i].getScale().y; textPos.y = rectPos.y + spacing * i + ( rectSize.height - ( spacing * m_texts.size() )) / 2; // set x position switch ( m_align ) { case Left: textPos.x = rectPos.x + spacing/2; break; case Centre: textPos.x = rectPos.x + ( (rectSize.width - textSize.width) / 2 ); break; case Right: textPos.x = rectPos.x + rectSize.width - textSize.width - spacing/2; break; } sf::Transform trans; trans.rotate( m_bgRect.getRotation(), rectPos.x, rectPos.y ); m_texts[i].setPosition( trans.transformPoint( textPos ) ); m_texts[i].setRotation( m_bgRect.getRotation() ); } m_needs_pos_set = false; }
void CBitmapHanFont::renderText(SDL_Surface * surface, const std::string & data, const SDL_Color & color, const Point & pos) const { int posX = pos.x; int posY = pos.y; SDL_LockSurface(surface); for(size_t i=0; i<data.size(); i += getCharacterSize(data[i])) { if (ui8(data[i]) < 0x80) renderCharacter(surface, getCharacterIndex(0xa3, data[i] + 0x80), color, posX, posY); else renderCharacter(surface, getCharacterIndex(data[i], data[i+1]), color, posX, posY); } SDL_UnlockSurface(surface); }
unsigned int sfText_getCharacterSize(const sfText* text) { CSFML_CALL_RETURN(text, getCharacterSize(), 0); }
sf::Vector2f FeTextPrimative::setString( const std::basic_string<sf::Uint32> &t, int position ) { // // Cut string if it is too big to fit our dimension // int first_char, len, disp_cpos; std::vector< int > fc_list; std::vector< int > len_list; if ( m_first_line >= 0 ) position = 0; fit_string( t, position, first_char, len ); fc_list.push_back( first_char ); len_list.push_back( len ); disp_cpos = position - first_char; if ( m_texts.size() > 1 ) m_texts.resize( 1 ); if (( m_first_line >= 0 ) && ( len < (int)t.size() )) { // // Calculate the number of lines we can fit in our RectShape // unsigned int spacing = getCharacterSize() * m_texts[0].getScale().y; const sf::Font *font = getFont(); if ( font ) spacing = font->getLineSpacing( spacing ); sf::FloatRect rectSize = m_bgRect.getLocalBounds(); int line_count = rectSize.height / spacing; // // Calculate the wrapped lines // position += len; int i=0; int actual_first_line=0; while (( position < (int)t.size() - 1 ) && ( i < line_count + m_first_line )) { if ( i >= line_count ) actual_first_line++; fit_string( t, position, first_char, len ); fc_list.push_back( first_char ); len_list.push_back( len ); position += len; i++; } m_first_line = actual_first_line; int actual_lines = ( (int)fc_list.size() < line_count ) ? fc_list.size() : line_count; int first_fc = fc_list.size() - actual_lines; // // Now create the wrapped lines // m_texts[0].setString( t.substr( fc_list[first_fc], len_list[first_fc] ) ); for ( i = first_fc + 1; i < (int)fc_list.size(); i++ ) { m_texts.push_back( m_texts[0] ); m_texts.back().setString( t.substr( fc_list[i], len_list[i] ) ); } } else // create the no-wrap or single non-clipped line m_texts[0].setString( t.substr( fc_list.front(), len_list.front() ) ); set_positions(); // we need to set the positions now for findCharacterPos() to work below return m_texts[0].findCharacterPos( disp_cpos ); }