SDL文本框突出显示文本

时间:2016-03-24 07:23:48

标签: c++ text textbox sdl highlight

我在C ++中创建一个使用SDL显示文本框的程序。我已经制作了文本框的基本功能,例如输入文字,限制,复制/粘贴,等等。但是我坚持使其能够突出显示文字。

以下是我绘制文本框的方法:

void draw_text_box(SDL_Surface *screan, char *message, struct textbox *text_box, int r, int g, int b)
{
    int temp_width;
    int temp_frame;
    SDL_Surface *text;
    SDL_Color textcolor = {0,0,0,0};
    SDL_Rect temp_location, clr_box;

    temp_width = text_box->location.w; //preserve the correct width

    temp_location = text_box->location;
    temp_location.y += 4;
    temp_location.h = 12;

    clr_box = text_box->location;

    //colors!
    textcolor.r = r;
    textcolor.g = g;
    textcolor.b = b;

    //text = TTF_RenderText_Solid(ttf_font, text_box->prev_message, textcolor);
    //SDL_BlitSurface( text, NULL, screan, &text_box->location);//<-changes "location" width to width of text

    //clear
    SDL_FillRect(screan, &clr_box, SDL_MapRGB(screen->format, 0, 0, 0));

    //strcpy(text_box->prev_message, message);

    //set the text
    text = TTF_RenderText_Solid(ttf_font, message, textcolor);

    if(!text) return;

    //set the offset
    temp_location.x = text_box->last_shift;
    temp_location.y = 0;
    temp_location.h = text->h;

    if(text_box->cursor_loc - text_box->last_shift > temp_location.w)
    temp_location.x = text_box->cursor_loc - temp_location.w;

    if(text_box->cursor_loc - 3 < text_box->last_shift)
    temp_location.x = text_box->cursor_loc - 3;

    if(temp_location.x < 0) temp_location.x = 0;
    text_box->last_shift = temp_location.x;

    //draw
    SDL_BlitSurface( text, &temp_location, screan, &text_box->location);//<-changes "location" width to width of text
    text_box->location.w = temp_width;
}

设置文字:

void set_text_box_text(SDL_Surface *screan, struct textbox *text_box, char *message)
{
    if(text_box->message != message)
        strcpy(text_box->message, message);

    text_box->char_amt = strlen(message);
    text_box->cursor = text_box->char_amt;
    text_box->last_shift = 0;

    if (screan) //if not NULL
    {
        if (text_box->selected)
            select_text_box(screan,text_box);
        else
            unselect_text_box(screan,text_box);
    }
    else
    {
        ;//text_box->prev_message[0] = '\0';
    }
}

选择并取消选中文本框:

void select_text_box(SDL_Surface *screan, struct textbox *text_box)
{
    char temp_message[405] = "";
    char temp_left[405];
    int i;

    text_box->selected = 1;

    if (text_box->pass_char == '\0')
    {
        if (text_box->cursor != 0)
        {
            left(temp_left, text_box->message, text_box->cursor);
            strcat(temp_message, temp_left);
        }

        temp_message[text_box->cursor] = '|';
        temp_message[text_box->cursor + 1] = '\0';

        right(temp_left, text_box->message, text_box->cursor);
        strcat(temp_message, temp_left);
    }
    else
    {
        for(i=0;i<text_box->cursor;i++)
            temp_message[i] = text_box->pass_char;

        temp_message[i] = '|';

        for(i++;i<text_box->char_amt + 1;i++)
            temp_message[i] = text_box->pass_char;

        temp_message[i] = '\0';
    }

    draw_text_box(screan, temp_message, text_box, 250, 250, 250);

}

void unselect_text_box(SDL_Surface *screan, struct textbox *text_box)
{
    char temp_message[405];
    int i;

    text_box->selected = 0;

    if (text_box->pass_char == '\0')
        draw_text_box(screan, text_box->message, text_box, 250, 250, 250);
    else
    {
        for(i=0;i<text_box->char_amt;i++)
            temp_message[i] = text_box->pass_char;

        temp_message[i] = '\0';
        draw_text_box(screan, temp_message, text_box, 250, 250, 250);
    }
}

0 个答案:

没有答案