Esempio n. 1
0
void TextBox::_ReformatText() {
	// (1): Go through the text ustring and determine where the newline characters can be found, examining one line at a time and adding it to the _text vector.
	size_t start_pos = 0;
	size_t newline_pos;
	ustring temp_str = _text_save;
	const size_t temp_length = temp_str.length();
	_text.clear();
	_num_chars = 0;

	// If font not set, return (leave _text vector empty)
	if (_font_properties == NULL) {
		IF_PRINT_WARNING(VIDEO_DEBUG) << "textbox font is invalid" << endl;
		return;
	}

	while (start_pos < temp_length) {
		newline_pos = temp_str.find(NEWLINE_CHARACTER, start_pos);

		// If the end of the string has been reached, add the new line and exit
		if (newline_pos == ustring::npos) {
			_AddLine(temp_str.substr(start_pos, temp_length - start_pos));
			break;
		}
		// Otherwise, add the new line segment and proceed to find the next
		else {
			_AddLine(temp_str.substr(start_pos, temp_length - newline_pos));
			start_pos = newline_pos + 1;
		}
	}

	// (2): Calculate the height of the text and check it against the height of the textbox.
	int32 text_height = CalculateTextHeight();

	if (text_height > _height) {
		IF_PRINT_WARNING(VIDEO_DEBUG) << "tried to display text of height (" << text_height
			<< ") in a window of lower height (" << _height << ")" << endl;
	}
} // void TextBox::_ReformatText()
Esempio n. 2
0
void TextBox::_ReformatText()
{
    // (1): Go through the text ustring and determine where the newline characters can be found, examining one line at a time and adding it to the _text vector.
    size_t newline_pos;
    size_t startline_pos = 0;
    const size_t temp_length = _text_save.length();
    _text.clear();
    _num_chars = 0;


    // If font not set, return (leave _text vector empty)
    if(!_font_properties) {
        IF_PRINT_WARNING(VIDEO_DEBUG) << "textbox font is invalid" << std::endl;
        return;
    }

    while(startline_pos < temp_length)
    {
        newline_pos = _text_save.find(NEWLINE_CHARACTER,startline_pos);
        // If the end of the string has been reached, add the new line and exit
        if(newline_pos == ustring::npos) {
            _AddLine(_text_save.substr(startline_pos, temp_length - startline_pos));
            break;
        }
        // Otherwise, add the new line segment and proceed to find the next
        else {
            ustring tmp = _text_save.substr(startline_pos, newline_pos - startline_pos);
            _AddLine(_text_save.substr(startline_pos, newline_pos - startline_pos));
            startline_pos = newline_pos + 1;

        }
    }

    // Update the scissor cache
    // Stores the positions of the four sides of the rectangle
    float left   = 0.0f;
    float right  = _width;
    float bottom = 0.0f;
    float top    = _height;

    VideoManager->PushState();

    VideoManager->SetDrawFlags(_xalign, _yalign, VIDEO_BLEND, 0);
    CalculateAlignedRect(left, right, bottom, top);
    VideoManager->PopState();

    // Create a screen rectangle for the position and apply any scissoring
    int32 x, y, w, h;

    x = static_cast<int32>(left < right ? left : right);
    y = static_cast<int32>(top < bottom ? top : bottom);
    w = static_cast<int32>(right - left);
    h = static_cast<int32>(top - bottom);

    if(w < 0)
        w = -w;
    if(h < 0)
        h = -h;

    _scissor_rect.Set(x, y, w, h);

    // Update the text height
    _text_height = static_cast<float>(CalculateTextHeight());
    // Calculate the height of the text and check it against the height of the textbox.
    if(_text_height > _height) {
        IF_PRINT_WARNING(VIDEO_DEBUG) << "tried to display text of height (" << _text_height
                                      << ") in a window of lower height (" << _height << ")" << std::endl;
    }

    // Determine the vertical position of the text based on the alignment
    if(_text_yalign == VIDEO_Y_TOP) {
        _text_ypos = top;
    } else if(_text_yalign == VIDEO_Y_CENTER) {
        _text_ypos = top - (VideoManager->_current_context.coordinate_system.GetVerticalDirection() * (_height - _text_height) * 0.5f);
    } else { // (_yalign == VIDEO_Y_BOTTOM)
        _text_ypos = top - (VideoManager->_current_context.coordinate_system.GetVerticalDirection() * (_height - _text_height));
    }

    // Determine the horizontal position of the text based on the alignment
    if(_text_xalign == VIDEO_X_LEFT) {
        _text_xpos = left;
    } else if(_text_xalign == VIDEO_X_CENTER) {
        _text_xpos = (left + right) * 0.5f; // * 0.5 equals /2.
    } else { // (_text_xalign == VIDEO_X_RIGHT)
        _text_xpos = right;
    }
} // void TextBox::_ReformatText()
Esempio n. 3
0
void TextBox::Draw() {
	if (_text.empty())
		return;

	if (_initialized == false) {
		IF_PRINT_WARNING(VIDEO_DEBUG) << "function failed because the textbox was not initialized:\n" << _initialization_errors << endl;
		return;
	}

	// Don't draw text window if parent window is hidden
	if (_owner && _owner->GetState() == VIDEO_MENU_STATE_HIDDEN)
		return;

	// Take the following steps to draw the text:
	//  (1): Save the video engine's context and enable appropriate draw settings
	//  (2): Determine the coordinates of the textbox rectangle to draw
	//  (3): Create a ScreenRect for the textbox area and apply scissoring if its enabled
	//  (4): Determine the text draw position from the alignment flags
	//  (5): Draw each line of text to the screen
	//  (6): Restore the original video engine context
	VideoManager->PushState();

	VideoManager->SetDrawFlags(_xalign, _yalign, VIDEO_BLEND, 0);

	// Stores the positions of the four sides of the rectangle
	float left   = 0.0f;
	float right  = _width;
	float bottom = 0.0f;
	float top    = _height;

	CalculateAlignedRect(left, right, bottom, top);

	// Create a screen rectangle for the position and apply any scissoring
	int32 x, y, w, h;

	x = static_cast<int32>(left < right ? left : right);
	y = static_cast<int32>(top < bottom ? top : bottom);
	w = static_cast<int32>(right - left);
	h = static_cast<int32>(top - bottom);

	if (w < 0)
		w = -w;
	if (h < 0)
		h = -h;

	ScreenRect rect(x, y, w, h);

	// TODO: this block of code (scissoring for textboxes) does not work properly
	/*
	if (_owner) {
		rect.Intersect(_owner->GetScissorRect());
	}
	rect.Intersect(VideoManager->GetScissorRect());
	VideoManager->EnableScissoring(_owner || VideoManager->IsScissoringEnabled());
	if (VideoManager->IsScissoringEnabled()) {
		VideoManager->SetScissorRect(rect);
	}
	*/

	// Holds the height of the text to be drawn
	float text_height = static_cast<float>(CalculateTextHeight());
	// Holds the x and y position where the text should be drawn
	float text_xpos, text_ypos;

	// Determine the vertical position of the text based on the alignment
	if (_text_yalign == VIDEO_Y_TOP) {
		text_ypos = top;
	}
	else if (_text_yalign == VIDEO_Y_CENTER) {
		text_ypos = top - (VideoManager->_current_context.coordinate_system.GetVerticalDirection() * (_height - text_height) * 0.5f);
	}
	else { // (_yalign == VIDEO_Y_BOTTOM)
		text_ypos = top - (VideoManager->_current_context.coordinate_system.GetVerticalDirection() * (_height - text_height));
	}

	// Determine the horizontal position of the text based on the alignment
	if (_text_xalign == VIDEO_X_LEFT) {
		text_xpos = left;
	}
	else if (_text_xalign == VIDEO_X_CENTER) {
		text_xpos = (left + right) * 0.5f; // TODO: Is this logic right? It doesn't seem so...
	}
	else { // (_text_xalign == VIDEO_X_RIGHT)
		text_xpos = right;
	}

	// Set the draw cursor, draw flags, and draw the text
	VideoManager->Move(0.0f, text_ypos);
	VideoManager->SetDrawFlags(VIDEO_X_LEFT, VIDEO_Y_TOP, VIDEO_BLEND, 0);
	_DrawTextLines(text_xpos, text_ypos, rect);

	if (GUIManager->DEBUG_DrawOutlines() == true)
		_DEBUG_DrawOutline(text_ypos);

	VideoManager->PopState();
} // void TextBox::Draw()