1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#include <ClanLib/core.h>
#include <ClanLib/display.h>
#include "stylemanager_opengl.h"
#include "inputbox_opengl.h"
#define BORDER_SIZE 12
CL_InputBox_OpenGL::CL_InputBox_OpenGL(
CL_InputBox *inputbox,
CL_StyleManager_OpenGL *style)
: CL_ComponentStyle(inputbox), inputbox(inputbox)
{
this->style = style;
resources = style->get_resources();
font = CL_Font::load("InputBox/font", resources);
if(inputbox->get_height() == 35)
{
int width = inputbox->get_width() / 16;
inputbox->set_width(width * 16);
surface_left = new CL_Surface("Inputbox/left", resources);
surface_center = new CL_Surface("Inputbox/center", resources);
surface_right = new CL_Surface("Inputbox/right", resources);
}
show_cursor = false;
character_offset = 0;
cursor_blink_time = CL_System::get_time();
slot_paint = inputbox->sig_paint().connect(this, &CL_InputBox_OpenGL::on_paint);
}
CL_InputBox_OpenGL::~CL_InputBox_OpenGL()
{
// delete font;
}
void CL_InputBox_OpenGL::on_paint()
{
if(surface_left)
on_paint_texture();
else
on_paint_default();
}
void CL_InputBox_OpenGL::on_paint_texture()
{
surface_left->put_screen(0, 0);
int x;
for(x = 16; x <= inputbox->get_width() - 32; x += 16)
surface_center->put_screen(x, 0);
surface_right->put_screen(inputbox->get_width() - 16, 0);
on_paint_input();
}
void CL_InputBox_OpenGL::on_paint_default()
{
// Draw frames
CL_Display::draw_rect(0, 0, inputbox->get_width(), inputbox->get_height(), 0.0f, 0.0f, 0.0f, 0.7f);
CL_Display::fill_rect(0, 0, inputbox->get_width(), inputbox->get_height(), 0.1f, 1.0f, 0.0f, 0.1f);
CL_Display::fill_rect(1, 1, inputbox->get_width() - 1, inputbox->get_height() - 1, 0.1f, 1.0f, 0.0f, 0.1f);
on_paint_input();
}
void CL_InputBox_OpenGL::on_paint_input()
{
// For easy reference, put inputfield-text into local variable
const char *text = inputbox->get_text().c_str();
// int pixel_y_offset = (inputbox->get_height() - font->get_height()) / 2;
int pixel_y_offset = 6;
// Calculate scroll offset
int character_offset = 0;
int pixel_offset = 0;
int width = (int)font->get_text_width(text);
if (width > inputbox->get_width() - BORDER_SIZE * 2)
{
while (
width >= inputbox->get_width() - BORDER_SIZE * 2 &&
character_offset + 1 < inputbox->get_cursor_position())
{
int w = font->get_char_width(text[character_offset]);
character_offset++;
pixel_offset += w;
width -= w;
}
}
// Display marked text, if any
if (inputbox->has_marked_text())
{
int start = inputbox->get_selection_start();
int end = start + inputbox->get_selection_length();
int mark_x1 = 0, mark_x2 = 0;
int i;
for(i = 0; i < start; i++)
mark_x1 += font->get_char_width(text[i]);
for(i = start; i < end; i++)
mark_x2 += font->get_char_width(text[i]);
CL_Display::fill_rect(
BORDER_SIZE + mark_x1 - 1,
pixel_y_offset,
BORDER_SIZE + mark_x1 + mark_x2,
pixel_y_offset + font->get_height() + 1,
0.0f, 0.0f, 0.0f, 0.5f);
}
// Display text
font->print_left(BORDER_SIZE, pixel_y_offset, &text[character_offset]);
// Show blinking cursor
if(inputbox->has_focus())
{
if (show_cursor)
{
int cursor_x = BORDER_SIZE - pixel_offset;
for(int i = 0; i < inputbox->get_cursor_position(); i++)
cursor_x += font->get_char_width(text[i]);
style->draw_line(cursor_x, pixel_y_offset, cursor_x, pixel_y_offset + font->get_height() + 1, GUICOLOR_CARET);
}
unsigned int cur_time = CL_System::get_time();
if (cur_time >= cursor_blink_time)
{
cursor_blink_time = cur_time + 400;
show_cursor = !show_cursor;
}
}
}
|