src/Graphics/MessageView.cc
author Tero Marttila <terom@fixme.fi>
Wed, 21 Jan 2009 23:07:22 +0200
branchnew_graphics
changeset 412 721c60072091
parent 411 106aaf6eadfe
permissions -rw-r--r--
new graphics code compiles... no, it doesn't work yet

#include "MessageView.hh"
#include "Graphics.hh"

namespace graphics
{

MessageView::MessageView (const PixelArea &area) :
    View(area), messages()
{
    
}

void MessageView::add_message (CL_Color color, std::string message) {
    Message msg (color, message);

    messages.push_back(msg);
}

void MessageView::draw (Display &display) {
    // get font
    CL_Font &font = graphics->fonts.getSimpleFont(); 

    // remember color
    CL_Color font_color = font.get_color();
    
    // maximum width
    CL_Size max_size = CL_Size(area.right - area.left, 0);

    // starting point for drawing
    PixelDimension offset_prev = area.bottom;

    // render messages, bottom up
    for (std::vector<Message>::reverse_iterator it = messages.rbegin(); it != messages.rend(); it++) {
        // set message color
        font.set_color(it->color);

        // calculate height
        PixelDimension height = font.get_height(it->message, max_size) + MESSAGE_VIEW_LINE_OFFSET;
       
        // new draw_point
        PixelDimension offset_this = offset_prev - height;

        // break if it doesn't fit anymore
        if (offset_this < area.top)
            break;
 
        // draw text
        font.draw(CL_Rect(area.left, offset_this, area.right, offset_prev), it->message, display.get_gc());
        
        // advance offset
        offset_prev = offset_this;
    }

    // restore font color
    font.set_color(font_color);
}

}