-Added table support to RichTextLabel

-Made help more readable, closes #2851
This commit is contained in:
reduz 2015-12-26 10:25:17 -03:00
parent b989d4f887
commit 13964f59c1
4 changed files with 467 additions and 159 deletions

View file

@ -30,7 +30,9 @@
#include "scene/scene_string_names.h" #include "scene/scene_string_names.h"
#include "os/keyboard.h" #include "os/keyboard.h"
#include "os/os.h" #include "os/os.h"
RichTextLabel::Item *RichTextLabel::_get_next_item(Item* p_item) { RichTextLabel::Item *RichTextLabel::_get_next_item(Item* p_item,bool p_free) {
if (p_free) {
if (p_item->subitems.size()) { if (p_item->subitems.size()) {
@ -47,18 +49,43 @@ RichTextLabel::Item *RichTextLabel::_get_next_item(Item* p_item) {
} }
if (p_item && p_item->parent) if (p_item->parent)
return p_item->E->next()->get(); return p_item->E->next()->get();
else else
return NULL; return NULL;
} }
} else {
if (p_item->subitems.size() && p_item->type!=ITEM_TABLE) {
return p_item->subitems.front()->get();
} else if (p_item->type==ITEM_FRAME) {
return NULL;
} else if (p_item->E->next()) {
return p_item->E->next()->get();
} else {
//go up until something with a next is found
while (p_item->type!=ITEM_FRAME && !p_item->E->next()) {
p_item=p_item->parent;
}
if (p_item->type!=ITEM_FRAME)
return p_item->E->next()->get();
else
return NULL;
}
}
return NULL; return NULL;
} }
void RichTextLabel::_process_line(int &y, int p_width, int p_line, ProcessMode p_mode,const Ref<Font> &p_base_font,const Color &p_base_color,const Point2i& p_click_pos,Item **r_click_item,int *r_click_char,bool *r_outside,int p_char_count) {
void RichTextLabel::_process_line(ItemFrame *p_frame,const Vector2& p_ofs,int &y, int p_width, int p_line, ProcessMode p_mode,const Ref<Font> &p_base_font,const Color &p_base_color,const Point2i& p_click_pos,Item **r_click_item,int *r_click_char,bool *r_outside,int p_char_count) {
RID ci; RID ci;
if (r_outside) if (r_outside)
@ -70,7 +97,7 @@ void RichTextLabel::_process_line(int &y, int p_width, int p_line, ProcessMode p
*r_click_item=NULL; *r_click_item=NULL;
} }
Line &l = lines[p_line]; Line &l = p_frame->lines[p_line];
Item *it = l.from; Item *it = l.from;
@ -91,6 +118,7 @@ void RichTextLabel::_process_line(int &y, int p_width, int p_line, ProcessMode p
l.offset_caches.clear(); l.offset_caches.clear();
l.height_caches.clear(); l.height_caches.clear();
l.char_count=0; l.char_count=0;
l.minimum_width=0;
} }
int wofs=margin; int wofs=margin;
@ -138,7 +166,7 @@ void RichTextLabel::_process_line(int &y, int p_width, int p_line, ProcessMode p
if (p_mode!=PROCESS_CACHE) {\ if (p_mode!=PROCESS_CACHE) {\
lh=line<l.height_caches.size()?l.height_caches[line]:1;\ lh=line<l.height_caches.size()?l.height_caches[line]:1;\
}\ }\
if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=y && p_click_pos.y<=y+lh && p_click_pos.x<wofs) {\ if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=p_ofs.y+y && p_click_pos.y<=p_ofs.y+y+lh && p_click_pos.x<p_ofs.x+wofs) {\
if (r_outside) *r_outside=true;\ if (r_outside) *r_outside=true;\
*r_click_item=it;\ *r_click_item=it;\
*r_click_char=rchar;\ *r_click_char=rchar;\
@ -148,12 +176,15 @@ void RichTextLabel::_process_line(int &y, int p_width, int p_line, ProcessMode p
#define ENSURE_WIDTH(m_width) \ #define ENSURE_WIDTH(m_width) \
if (p_mode==PROCESS_CACHE) { \
l.minimum_width=MAX(l.minimum_width,wofs+m_width);\
}\
if (wofs + m_width > p_width) {\ if (wofs + m_width > p_width) {\
if (p_mode==PROCESS_CACHE) {\ if (p_mode==PROCESS_CACHE) {\
if (spaces>0) \ if (spaces>0) \
spaces-=1;\ spaces-=1;\
}\ }\
if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=y && p_click_pos.y<=y+lh && p_click_pos.x>wofs) {\ if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=p_ofs.y+y && p_click_pos.y<=p_ofs.y+y+lh && p_click_pos.x>p_ofs.x+wofs) {\
if (r_outside) *r_outside=true; \ if (r_outside) *r_outside=true; \
*r_click_item=it;\ *r_click_item=it;\
*r_click_char=rchar;\ *r_click_char=rchar;\
@ -165,7 +196,7 @@ void RichTextLabel::_process_line(int &y, int p_width, int p_line, ProcessMode p
#define ADVANCE(m_width) \ #define ADVANCE(m_width) \
{\ {\
if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=y && p_click_pos.y<=y+lh && p_click_pos.x>=wofs && p_click_pos.x<wofs+m_width) {\ if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=p_ofs.y+y && p_click_pos.y<=p_ofs.y+y+lh && p_click_pos.x>=p_ofs.x+wofs && p_click_pos.x<p_ofs.x+wofs+m_width) {\
if (r_outside) *r_outside=false; \ if (r_outside) *r_outside=false; \
*r_click_item=it;\ *r_click_item=it;\
*r_click_char=rchar;\ *r_click_char=rchar;\
@ -218,8 +249,10 @@ if (m_height > line_height) {\
underline=true; underline=true;
} }
} else if (p_mode==PROCESS_CACHE) { } else if (p_mode==PROCESS_CACHE) {
l.char_count+=text->text.length(); l.char_count+=text->text.length();
} }
rchar=0; rchar=0;
@ -263,7 +296,6 @@ if (m_height > line_height) {\
} }
ENSURE_WIDTH(w); ENSURE_WIDTH(w);
@ -294,7 +326,7 @@ if (m_height > line_height) {\
if (p_mode==PROCESS_POINTER && r_click_char && p_click_pos.y>=y && p_click_pos.y<=y+lh) { if (p_mode==PROCESS_POINTER && r_click_char && p_click_pos.y>=p_ofs.y+y && p_click_pos.y<=p_ofs.y+y+lh) {
//int o = (wofs+w)-p_click_pos.x; //int o = (wofs+w)-p_click_pos.x;
@ -303,7 +335,7 @@ if (m_height > line_height) {\
cw=tab_size*font->get_char_size(' ').width; cw=tab_size*font->get_char_size(' ').width;
} }
if (p_click_pos.x-cw/2>pofs) { if (p_click_pos.x-cw/2>p_ofs.x+pofs) {
rchar=int((&c[i])-cf); rchar=int((&c[i])-cf);
//print_line("GOT: "+itos(rchar)); //print_line("GOT: "+itos(rchar));
@ -337,13 +369,13 @@ if (m_height > line_height) {\
if (selected) { if (selected) {
cw = font->get_char_size(c[i],c[i+1]).x; cw = font->get_char_size(c[i],c[i+1]).x;
draw_rect(Rect2(pofs,y,cw,lh),selection_bg); draw_rect(Rect2(p_ofs.x+pofs,p_ofs.y+y,cw,lh),selection_bg);
if (visible) if (visible)
font->draw_char(ci,Point2(pofs,y+lh-(fh-ascent)),c[i],c[i+1],selection_fg); font->draw_char(ci,p_ofs+Point2(pofs,y+lh-(fh-ascent)),c[i],c[i+1],selection_fg);
} else { } else {
if (visible) if (visible)
cw=font->draw_char(ci,Point2(pofs,y+lh-(fh-ascent)),c[i],c[i+1],color); cw=font->draw_char(ci,p_ofs+Point2(pofs,y+lh-(fh-ascent)),c[i],c[i+1],color);
} }
p_char_count++; p_char_count++;
@ -359,7 +391,7 @@ if (m_height > line_height) {\
uc.a*=0.5; uc.a*=0.5;
//VS::get_singleton()->canvas_item_add_line(ci,Point2(pofs,y+ascent+2),Point2(pofs+cw,y+ascent+2),uc); //VS::get_singleton()->canvas_item_add_line(ci,Point2(pofs,y+ascent+2),Point2(pofs+cw,y+ascent+2),uc);
int uy = y+lh-fh+ascent+2; int uy = y+lh-fh+ascent+2;
VS::get_singleton()->canvas_item_add_line(ci,Point2(pofs,uy),Point2(pofs+cw,uy),uc); VS::get_singleton()->canvas_item_add_line(ci,p_ofs+Point2(pofs,uy),p_ofs+Point2(pofs+cw,uy),uc);
} }
ofs+=cw; ofs+=cw;
} }
@ -397,7 +429,7 @@ if (m_height > line_height) {\
bool visible = visible_characters<0 || p_char_count<visible_characters; bool visible = visible_characters<0 || p_char_count<visible_characters;
if (p_mode==PROCESS_DRAW && visible) { if (p_mode==PROCESS_DRAW && visible) {
img->image->draw(ci,Point2(wofs,y+lh-font->get_descent()-img->image->get_height())); img->image->draw(ci,p_ofs+Point2(wofs,y+lh-font->get_descent()-img->image->get_height()));
} }
p_char_count++; p_char_count++;
@ -435,6 +467,138 @@ if (m_height > line_height) {\
#endif #endif
} break; } break;
case ITEM_TABLE: {
lh=0;
ItemTable *table = static_cast<ItemTable*>(it);
int hseparation=get_constant("table_hseparation");
int vseparation=get_constant("table_vseparation");
Color ccolor = _find_color(table,p_base_color);
Vector2 draw_ofs = Point2(wofs,y);
if (p_mode==PROCESS_CACHE) {
int idx=0;
//set minimums to zero
for(int i=0;i<table->columns.size();i++) {
table->columns[i].min_width=0;
table->columns[i].width=0;
}
//compute minimum width for each cell
for (List<Item*>::Element *E=table->subitems.front();E;E=E->next()) {
ERR_CONTINUE(E->get()->type!=ITEM_FRAME); //children should all be frames
ItemFrame *frame = static_cast<ItemFrame*>(E->get());
int column = idx % table->columns.size();
int ly=0;
for(int i=0;i<frame->lines.size();i++) {
_process_line(frame,Point2(),ly,p_width,i,PROCESS_CACHE,cfont,Color());
table->columns[column].min_width=MAX( table->columns[i].min_width, frame->lines[i].minimum_width );
}
idx++;
}
//compute available width and total radio (for expanders)
int total_ratio=0;
int available_width=p_width - hseparation * (table->columns.size() -1);
table->total_width=hseparation;
for(int i=0;i<table->columns.size();i++) {
available_width-=table->columns[i].min_width;
if (table->columns[i].expand)
total_ratio+=table->columns[i].expand_ratio;
}
//assign actual widths
for(int i=0;i<table->columns.size();i++) {
table->columns[i].width = table->columns[i].min_width;
if (table->columns[i].expand)
table->columns[i].width+=table->columns[i].expand_ratio*available_width/total_ratio;
table->total_width+=table->columns[i].width+hseparation;
}
//compute caches properly again with the right width
idx=0;
for (List<Item*>::Element *E=table->subitems.front();E;E=E->next()) {
ERR_CONTINUE(E->get()->type!=ITEM_FRAME); //children should all be frames
ItemFrame *frame = static_cast<ItemFrame*>(E->get());
int column = idx % table->columns.size();
for(int i=0;i<frame->lines.size();i++) {
int ly=0;
_process_line(frame,Point2(),ly,table->columns[column].width,i,PROCESS_CACHE,cfont,Color());
frame->lines[i].height_cache=ly; //actual height
frame->lines[i].height_accum_cache=ly; //actual height
}
idx++;
}
}
Point2 offset(hseparation,vseparation);
int row_height=0;
//draw using computed caches
int idx=0;
for (List<Item*>::Element *E=table->subitems.front();E;E=E->next()) {
ERR_CONTINUE(E->get()->type!=ITEM_FRAME); //children should all be frames
ItemFrame *frame = static_cast<ItemFrame*>(E->get());
int column = idx % table->columns.size();
int ly=0;
int yofs=0;
for(int i=0;i<frame->lines.size();i++) {
if (p_mode==PROCESS_DRAW) {
_process_line(frame,p_ofs+offset+draw_ofs+Vector2(0,yofs),ly,table->columns[column].width,i,PROCESS_DRAW,cfont,ccolor);
} else if (p_mode==PROCESS_POINTER) {
_process_line(frame,p_ofs+offset+draw_ofs+Vector2(0,yofs),ly,table->columns[column].width,i,PROCESS_POINTER,cfont,ccolor,p_click_pos,r_click_item,r_click_char,r_outside);
}
yofs+=frame->lines[i].height_cache;
if (p_mode==PROCESS_CACHE) {
frame->lines[i].height_accum_cache=offset.y+draw_ofs.y+frame->lines[i].height_cache;
}
}
row_height=MAX(yofs,row_height);
offset.x+=table->columns[column].width+hseparation;
if (column==table->columns.size()-1) {
offset.y+=row_height+vseparation;
offset.x=hseparation;
row_height=0;
}
idx++;
}
int total_height = offset.y;
if (row_height) {
total_height=row_height+vseparation;
}
ADVANCE( table->total_width );
CHECK_HEIGHT( total_height );
} break;
default: {} default: {}
@ -445,7 +609,7 @@ if (m_height > line_height) {\
it = _get_next_item(it); it = _get_next_item(it);
if (p_mode == PROCESS_POINTER && r_click_item && itp && !it && p_click_pos.y>y+lh) { if (p_mode == PROCESS_POINTER && r_click_item && itp && !it && p_click_pos.y>p_ofs.y+y+lh) {
//at the end of all, return this //at the end of all, return this
if (r_outside) *r_outside=true; if (r_outside) *r_outside=true;
*r_click_item=itp; *r_click_item=itp;
@ -453,9 +617,9 @@ if (m_height > line_height) {\
return; return;
} }
if (it && (p_line+1 < lines.size()) && lines[p_line+1].from==it) { if (it && (p_line+1 < p_frame->lines.size()) && p_frame->lines[p_line+1].from==it) {
if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=y && p_click_pos.y<=y+lh) { if (p_mode==PROCESS_POINTER && r_click_item && p_click_pos.y>=p_ofs.y+y && p_click_pos.y<=p_ofs.y+y+lh) {
//went to next line, but pointer was on the previous one //went to next line, but pointer was on the previous one
if (r_outside) *r_outside=true; if (r_outside) *r_outside=true;
*r_click_item=itp; *r_click_item=itp;
@ -493,8 +657,8 @@ void RichTextLabel::_scroll_changed(double) {
void RichTextLabel::_update_scroll() { void RichTextLabel::_update_scroll() {
int total_height=0; int total_height=0;
if (lines.size()) if (main->lines.size())
total_height=lines[lines.size()-1].height_accum_cache; total_height=main->lines[main->lines.size()-1].height_accum_cache;
bool exceeds = total_height > get_size().height && scroll_active; bool exceeds = total_height > get_size().height && scroll_active;
@ -503,18 +667,18 @@ void RichTextLabel::_update_scroll() {
if (exceeds) { if (exceeds) {
scroll_visible=true; scroll_visible=true;
first_invalid_line=0; main->first_invalid_line=0;
scroll_w=vscroll->get_combined_minimum_size().width; scroll_w=vscroll->get_combined_minimum_size().width;
vscroll->show(); vscroll->show();
vscroll->set_anchor_and_margin( MARGIN_LEFT, ANCHOR_END,scroll_w); vscroll->set_anchor_and_margin( MARGIN_LEFT, ANCHOR_END,scroll_w);
_validate_line_caches(); _validate_line_caches(main);
} else { } else {
scroll_visible=false; scroll_visible=false;
vscroll->hide(); vscroll->hide();
scroll_w=0; scroll_w=0;
_validate_line_caches(); _validate_line_caches(main);
} }
} }
@ -527,7 +691,7 @@ void RichTextLabel::_notification(int p_what) {
case NOTIFICATION_RESIZED: { case NOTIFICATION_RESIZED: {
first_invalid_line=0; //invalidate ALL main->first_invalid_line=0; //invalidate ALL
update(); update();
} break; } break;
@ -535,7 +699,7 @@ void RichTextLabel::_notification(int p_what) {
if (use_bbcode) if (use_bbcode)
parse_bbcode(bbcode); parse_bbcode(bbcode);
first_invalid_line=0; //invalidate ALL main->first_invalid_line=0; //invalidate ALL
update(); update();
} break; } break;
@ -550,7 +714,7 @@ void RichTextLabel::_notification(int p_what) {
} break; } break;
case NOTIFICATION_DRAW: { case NOTIFICATION_DRAW: {
_validate_line_caches(); _validate_line_caches(main);
_update_scroll(); _update_scroll();
@ -571,25 +735,25 @@ void RichTextLabel::_notification(int p_what) {
int from_line = 0; int from_line = 0;
int total_chars = 0; int total_chars = 0;
while (from_line<lines.size()) { while (from_line<main->lines.size()) {
if (lines[from_line].height_accum_cache>=ofs) if (main->lines[from_line].height_accum_cache>=ofs)
break; break;
from_line++; from_line++;
total_chars+=lines[from_line].char_count; total_chars+=main->lines[from_line].char_count;
} }
if (from_line>=lines.size()) if (from_line>=main->lines.size())
break; //nothing to draw break; //nothing to draw
int y = (lines[from_line].height_accum_cache - lines[from_line].height_cache) - ofs; int y = (main->lines[from_line].height_accum_cache - main->lines[from_line].height_cache) - ofs;
Ref<Font> base_font=get_font("normal_font"); Ref<Font> base_font=get_font("normal_font");
Color base_color=get_color("default_color"); Color base_color=get_color("default_color");
while (y<size.height && from_line<lines.size()) { while (y<size.height && from_line<main->lines.size()) {
_process_line(y,size.width-scroll_w,from_line,PROCESS_DRAW,base_font,base_color,Point2i(),NULL,NULL,NULL,total_chars); _process_line(main,Point2(),y,size.width-scroll_w,from_line,PROCESS_DRAW,base_font,base_color,Point2i(),NULL,NULL,NULL,total_chars);
total_chars+=lines[from_line].char_count; total_chars+=main->lines[from_line].char_count;
from_line++; from_line++;
} }
} }
@ -597,7 +761,7 @@ void RichTextLabel::_notification(int p_what) {
} }
void RichTextLabel::_find_click(const Point2i& p_click,Item **r_click_item,int *r_click_char,bool *r_outside) { void RichTextLabel::_find_click(ItemFrame* p_frame,const Point2i& p_click,Item **r_click_item,int *r_click_char,bool *r_outside) {
if (r_click_item) if (r_click_item)
*r_click_item=NULL; *r_click_item=NULL;
@ -609,26 +773,26 @@ void RichTextLabel::_find_click(const Point2i& p_click,Item **r_click_item,int *
//todo, change to binary search //todo, change to binary search
int from_line = 0; int from_line = 0;
while (from_line<lines.size()) { while (from_line<p_frame->lines.size()) {
if (lines[from_line].height_accum_cache>=ofs) if (p_frame->lines[from_line].height_accum_cache>=ofs)
break; break;
from_line++; from_line++;
} }
if (from_line>=lines.size()) if (from_line>=p_frame->lines.size())
return; return;
int y = (lines[from_line].height_accum_cache - lines[from_line].height_cache) - ofs; int y = (p_frame->lines[from_line].height_accum_cache - p_frame->lines[from_line].height_cache) - ofs;
Ref<Font> base_font=get_font("normal_font"); Ref<Font> base_font=get_font("normal_font");
Color base_color=get_color("default_color"); Color base_color=get_color("default_color");
while (y<size.height && from_line<lines.size()) { while (y<size.height && from_line<p_frame->lines.size()) {
_process_line(y,size.width-scroll_w,from_line,PROCESS_POINTER,base_font,base_color,p_click,r_click_item,r_click_char,r_outside); _process_line(p_frame,Point2(),y,size.width-scroll_w,from_line,PROCESS_POINTER,base_font,base_color,p_click,r_click_item,r_click_char,r_outside);
if (r_click_item && *r_click_item) if (r_click_item && *r_click_item)
return; return;
from_line++; from_line++;
@ -643,13 +807,13 @@ Control::CursorShape RichTextLabel::get_cursor_shape(const Point2& p_pos) const
if (!underline_meta || selection.click) if (!underline_meta || selection.click)
return CURSOR_ARROW; return CURSOR_ARROW;
if (first_invalid_line<lines.size()) if (main->first_invalid_line<main->lines.size())
return CURSOR_ARROW; //invalid return CURSOR_ARROW; //invalid
int line=0; int line=0;
Item *item=NULL; Item *item=NULL;
((RichTextLabel*)(this))->_find_click(p_pos,&item,&line); ((RichTextLabel*)(this))->_find_click(main,p_pos,&item,&line);
if (item && ((RichTextLabel*)(this))->_find_meta(item,NULL)) if (item && ((RichTextLabel*)(this))->_find_meta(item,NULL))
@ -665,7 +829,7 @@ void RichTextLabel::_input_event(InputEvent p_event) {
case InputEvent::MOUSE_BUTTON: { case InputEvent::MOUSE_BUTTON: {
if (first_invalid_line<lines.size()) if (main->first_invalid_line<main->lines.size())
return; return;
const InputEventMouseButton& b = p_event.mouse_button; const InputEventMouseButton& b = p_event.mouse_button;
@ -680,7 +844,7 @@ void RichTextLabel::_input_event(InputEvent p_event) {
Item *item=NULL; Item *item=NULL;
bool outside; bool outside;
_find_click(Point2i(b.x,b.y),&item,&line,&outside); _find_click(main,Point2i(b.x,b.y),&item,&line,&outside);
if (item) { if (item) {
@ -773,7 +937,7 @@ void RichTextLabel::_input_event(InputEvent p_event) {
} break; } break;
case InputEvent::MOUSE_MOTION: { case InputEvent::MOUSE_MOTION: {
if (first_invalid_line<lines.size()) if (main->first_invalid_line<main->lines.size())
return; return;
const InputEventMouseMotion& m = p_event.mouse_motion; const InputEventMouseMotion& m = p_event.mouse_motion;
@ -782,7 +946,7 @@ void RichTextLabel::_input_event(InputEvent p_event) {
int line=0; int line=0;
Item *item=NULL; Item *item=NULL;
_find_click(Point2i(m.x,m.y),&item,&line); _find_click(main,Point2i(m.x,m.y),&item,&line);
if (!item) if (!item)
return; // do not update return; // do not update
@ -950,9 +1114,9 @@ bool RichTextLabel::_find_meta(Item *p_item,Variant *r_meta) {
} }
void RichTextLabel::_validate_line_caches() { void RichTextLabel::_validate_line_caches(ItemFrame* p_frame) {
if (first_invalid_line==lines.size()) if (p_frame->first_invalid_line==p_frame->lines.size())
return; return;
//validate invalid lines!s //validate invalid lines!s
@ -960,24 +1124,24 @@ void RichTextLabel::_validate_line_caches() {
Ref<Font> base_font=get_font("normal_font"); Ref<Font> base_font=get_font("normal_font");
for(int i=first_invalid_line;i<lines.size();i++) { for(int i=p_frame->first_invalid_line;i<p_frame->lines.size();i++) {
int y=0; int y=0;
_process_line(y,size.width-scroll_w,i,PROCESS_CACHE,base_font,Color()); _process_line(p_frame,Point2(),y,size.width-scroll_w,i,PROCESS_CACHE,base_font,Color());
lines[i].height_cache=y; p_frame->lines[i].height_cache=y;
lines[i].height_accum_cache=y; p_frame->lines[i].height_accum_cache=y;
if (i>0) if (i>0)
lines[i].height_accum_cache+=lines[i-1].height_accum_cache; p_frame->lines[i].height_accum_cache+=p_frame->lines[i-1].height_accum_cache;
} }
int total_height=0; int total_height=0;
if (lines.size()) if (p_frame->lines.size())
total_height=lines[lines.size()-1].height_accum_cache; total_height=p_frame->lines[p_frame->lines.size()-1].height_accum_cache;
first_invalid_line=lines.size(); main->first_invalid_line=p_frame->lines.size();
updating_scroll=true; updating_scroll=true;
vscroll->set_max(total_height); vscroll->set_max(total_height);
@ -990,17 +1154,20 @@ void RichTextLabel::_validate_line_caches() {
} }
void RichTextLabel::_invalidate_current_line() { void RichTextLabel::_invalidate_current_line(ItemFrame* p_frame) {
if (lines.size()-1 <= first_invalid_line) { if (p_frame->lines.size()-1 <= p_frame->first_invalid_line) {
first_invalid_line=lines.size()-1; p_frame->first_invalid_line=p_frame->lines.size()-1;
update(); update();
} }
} }
void RichTextLabel::add_text(const String& p_text) { void RichTextLabel::add_text(const String& p_text) {
if (current->type==ITEM_TABLE)
return; //can't add anything here
int pos=0; int pos=0;
while (pos<p_text.length()) { while (pos<p_text.length()) {
@ -1027,7 +1194,7 @@ void RichTextLabel::add_text(const String& p_text) {
//append text condition! //append text condition!
ItemText *ti = static_cast<ItemText*>(current->subitems.back()->get()); ItemText *ti = static_cast<ItemText*>(current->subitems.back()->get());
ti->text+=line; ti->text+=line;
_invalidate_current_line(); _invalidate_current_line(main);
} else { } else {
//append item condition //append item condition
@ -1043,11 +1210,11 @@ void RichTextLabel::add_text(const String& p_text) {
if (eol) { if (eol) {
ItemNewline *item = memnew( ItemNewline ); ItemNewline *item = memnew( ItemNewline );
item->line=lines.size(); item->line=current_frame->lines.size();
_add_item(item,false); _add_item(item,false);
lines.resize(lines.size()+1); current_frame->lines.resize(current_frame->lines.size()+1);
lines[lines.size()-1].from=item; current_frame->lines[current_frame->lines.size()-1].from=item;
_invalidate_current_line(); _invalidate_current_line(current_frame);
} }
@ -1055,25 +1222,38 @@ void RichTextLabel::add_text(const String& p_text) {
} }
} }
void RichTextLabel::_add_item(Item *p_item, bool p_enter) { void RichTextLabel::_add_item(Item *p_item, bool p_enter, bool p_ensure_newline) {
p_item->parent=current; p_item->parent=current;
p_item->E=current->subitems.push_back(p_item); p_item->E=current->subitems.push_back(p_item);
p_item->index=current_idx++; p_item->index=current_idx++;
if (p_enter) if (p_enter)
current=p_item; current=p_item;
if (lines[lines.size()-1].from==NULL) { if (p_ensure_newline && current_frame->lines[current_frame->lines.size()-1].from) {
lines[lines.size()-1].from=p_item; _invalidate_current_line(current_frame);
current_frame->lines.resize( current_frame->lines.size() +1 );
} }
_invalidate_current_line(); if (current_frame->lines[current_frame->lines.size()-1].from==NULL) {
current_frame->lines[current_frame->lines.size()-1].from=p_item;
}
p_item->line=current_frame->lines.size()-1;
_invalidate_current_line(current_frame);
} }
void RichTextLabel::add_image(const Ref<Texture>& p_image) { void RichTextLabel::add_image(const Ref<Texture>& p_image) {
if (current->type==ITEM_TABLE)
return;
ERR_FAIL_COND(p_image.is_null()); ERR_FAIL_COND(p_image.is_null());
ItemImage *item = memnew( ItemImage ); ItemImage *item = memnew( ItemImage );
@ -1084,15 +1264,18 @@ void RichTextLabel::add_image(const Ref<Texture>& p_image) {
void RichTextLabel::add_newline() { void RichTextLabel::add_newline() {
if (current->type==ITEM_TABLE)
return;
ItemNewline *item = memnew( ItemNewline ); ItemNewline *item = memnew( ItemNewline );
item->line=lines.size(); item->line=current_frame->lines.size();
lines.resize(lines.size()+1); current_frame->lines.resize(current_frame->lines.size()+1);
_add_item(item,false); _add_item(item,false);
} }
void RichTextLabel::push_font(const Ref<Font>& p_font) { void RichTextLabel::push_font(const Ref<Font>& p_font) {
ERR_FAIL_COND(current->type==ITEM_TABLE);
ERR_FAIL_COND(p_font.is_null()); ERR_FAIL_COND(p_font.is_null());
ItemFont *item = memnew( ItemFont ); ItemFont *item = memnew( ItemFont );
@ -1102,6 +1285,7 @@ void RichTextLabel::push_font(const Ref<Font>& p_font) {
} }
void RichTextLabel::push_color(const Color& p_color) { void RichTextLabel::push_color(const Color& p_color) {
ERR_FAIL_COND(current->type==ITEM_TABLE);
ItemColor *item = memnew( ItemColor ); ItemColor *item = memnew( ItemColor );
item->color=p_color; item->color=p_color;
@ -1110,6 +1294,7 @@ void RichTextLabel::push_color(const Color& p_color) {
} }
void RichTextLabel::push_underline() { void RichTextLabel::push_underline() {
ERR_FAIL_COND(current->type==ITEM_TABLE);
ItemUnderline *item = memnew( ItemUnderline ); ItemUnderline *item = memnew( ItemUnderline );
_add_item(item,true); _add_item(item,true);
@ -1118,47 +1303,40 @@ void RichTextLabel::push_underline() {
void RichTextLabel::push_align(Align p_align) { void RichTextLabel::push_align(Align p_align) {
lines.resize(lines.size()+1); ERR_FAIL_COND(current->type==ITEM_TABLE);
ItemAlign *item = memnew( ItemAlign ); ItemAlign *item = memnew( ItemAlign );
item->align=p_align; item->align=p_align;
_add_item(item,true); _add_item(item,true,true);
ItemNewline *itemnl = memnew( ItemNewline );
itemnl->line=lines.size()-1;
_add_item(itemnl,false);
} }
void RichTextLabel::push_indent(int p_level) { void RichTextLabel::push_indent(int p_level) {
ERR_FAIL_COND(current->type==ITEM_TABLE);
ERR_FAIL_COND(p_level<0); ERR_FAIL_COND(p_level<0);
lines.resize(lines.size()+1);
ItemIndent *item = memnew( ItemIndent ); ItemIndent *item = memnew( ItemIndent );
item->level=p_level; item->level=p_level;
_add_item(item,true); _add_item(item,true,true);
ItemNewline *itemnl = memnew( ItemNewline );
itemnl->line=lines.size()-1;
_add_item(itemnl,false);
} }
void RichTextLabel::push_list(ListType p_list) { void RichTextLabel::push_list(ListType p_list) {
ERR_FAIL_COND(current->type==ITEM_TABLE);
ERR_FAIL_INDEX(p_list,3); ERR_FAIL_INDEX(p_list,3);
ItemList *item = memnew( ItemList ); ItemList *item = memnew( ItemList );
item->list_type=p_list; item->list_type=p_list;
_add_item(item,true); _add_item(item,true,true);
} }
void RichTextLabel::push_meta(const Variant& p_meta) { void RichTextLabel::push_meta(const Variant& p_meta) {
ERR_FAIL_COND(current->type==ITEM_TABLE);
ItemMeta *item = memnew( ItemMeta ); ItemMeta *item = memnew( ItemMeta );
item->meta=p_meta; item->meta=p_meta;
@ -1166,9 +1344,62 @@ void RichTextLabel::push_meta(const Variant& p_meta) {
} }
void RichTextLabel::push_table(int p_columns) {
ERR_FAIL_COND( p_columns < 1);
ItemTable *item = memnew( ItemTable );
item->columns.resize(p_columns);
item->total_width=0;
for(int i=0;i<item->columns.size();i++) {
item->columns[i].expand=false;
item->columns[i].expand_ratio=1;
}
_add_item(item,true,true);
}
void RichTextLabel::set_table_column_expand(int p_column,bool p_expand,int p_ratio) {
ERR_FAIL_COND(current->type!=ITEM_TABLE);
ItemTable *table = static_cast<ItemTable*>(current);
ERR_FAIL_INDEX(p_column,table->columns.size());
table->columns[p_column].expand=p_expand;
table->columns[p_column].expand_ratio=p_ratio;
}
void RichTextLabel::push_cell(){
ERR_FAIL_COND(current->type!=ITEM_TABLE);
ItemFrame *item = memnew( ItemFrame );
item->parent_frame=current_frame;
_add_item(item,true);
current_frame=item;
item->cell=true;
item->parent_line=item->parent_frame->lines.size()-1;
item->lines.resize(1);
item->lines[0].from=NULL;
item->first_invalid_line=0;
}
int RichTextLabel::get_current_table_column() const {
ERR_FAIL_COND_V(current->type!=ITEM_TABLE,-1);
ItemTable *table = static_cast<ItemTable*>(current);
return table->subitems.size() % table->columns.size();
}
void RichTextLabel::pop() { void RichTextLabel::pop() {
ERR_FAIL_COND(!current->parent); ERR_FAIL_COND(!current->parent);
if (current->type==ITEM_FRAME) {
current_frame = static_cast<ItemFrame*>(current)->parent_frame;
}
current=current->parent; current=current->parent;
} }
@ -1176,9 +1407,10 @@ void RichTextLabel::clear() {
main->_clear_children(); main->_clear_children();
current=main; current=main;
lines.clear(); current_frame=main;
lines.resize(1); main->lines.clear();
first_invalid_line=0; main->lines.resize(1);
main->first_invalid_line=0;
update(); update();
selection.click=NULL; selection.click=NULL;
selection.active=false; selection.active=false;
@ -1189,7 +1421,7 @@ void RichTextLabel::clear() {
void RichTextLabel::set_tab_size(int p_spaces) { void RichTextLabel::set_tab_size(int p_spaces) {
tab_size=p_spaces; tab_size=p_spaces;
first_invalid_line=0; main->first_invalid_line=0;
update(); update();
} }
@ -1345,6 +1577,30 @@ Error RichTextLabel::append_bbcode(const String& p_bbcode) {
push_font(mono_font); push_font(mono_font);
pos=brk_end+1; pos=brk_end+1;
tag_stack.push_front(tag); tag_stack.push_front(tag);
} else if (tag.begins_with("table=")) {
int columns = tag.substr(6,tag.length()).to_int();
if (columns<1)
columns=1;
//use monospace font
push_table(columns);
pos=brk_end+1;
tag_stack.push_front("table");
} else if (tag=="cell") {
push_cell();
pos=brk_end+1;
tag_stack.push_front(tag);
} else if (tag.begins_with("cell=")) {
int ratio = tag.substr(6,tag.length()).to_int();
if (ratio<1)
ratio=1;
//use monospace font
set_table_column_expand(get_current_table_column(),true,ratio);
push_cell();
pos=brk_end+1;
tag_stack.push_front("cell");
} else if (tag=="u") { } else if (tag=="u") {
//use underline //use underline
@ -1504,15 +1760,15 @@ Error RichTextLabel::append_bbcode(const String& p_bbcode) {
void RichTextLabel::scroll_to_line(int p_line) { void RichTextLabel::scroll_to_line(int p_line) {
ERR_FAIL_INDEX(p_line,lines.size()); ERR_FAIL_INDEX(p_line,main->lines.size());
_validate_line_caches(); _validate_line_caches(main);
vscroll->set_val(lines[p_line].height_accum_cache-lines[p_line].height_cache); vscroll->set_val(main->lines[p_line].height_accum_cache-main->lines[p_line].height_cache);
} }
int RichTextLabel::get_line_count() const { int RichTextLabel::get_line_count() const {
return lines.size(); return current_frame->lines.size();
} }
void RichTextLabel::set_selection_enabled(bool p_enabled) { void RichTextLabel::set_selection_enabled(bool p_enabled) {
@ -1556,29 +1812,31 @@ bool RichTextLabel::search(const String& p_string,bool p_from_selection) {
selection.active=true; selection.active=true;
update(); update();
if (line==-1) { _validate_line_caches(main);
while(it) { int fh = _find_font(t).is_valid()?_find_font(t)->get_height():get_font("normal_font")->get_height();
if (it->type==ITEM_NEWLINE) { float offset =0;
line=static_cast<ItemNewline*>(it)->line; int line = t->line;
break; Item *item =t;
while(item) {
if (item->type==ITEM_FRAME) {
ItemFrame *frame = static_cast<ItemFrame*>(item);
if (line>=0 && line<frame->lines.size()) {
offset+=frame->lines[line].height_accum_cache-frame->lines[line].height_cache;
line=frame->line;
} }
it=_get_next_item(it);
} }
item=item->parent;
} }
vscroll->set_val(offset-fh);
line-=2;
scroll_to_line(line<0?0:line);
return true; return true;
} }
} }
it=_get_next_item(it); it=_get_next_item(it,true);
charidx=0; charidx=0;
} }
@ -1617,7 +1875,7 @@ void RichTextLabel::selection_copy() {
if (item==selection.to) if (item==selection.to)
break; break;
item=_get_next_item(item); item=_get_next_item(item,true);
} }
if (text!="") { if (text!="") {
@ -1670,6 +1928,9 @@ void RichTextLabel::_bind_methods() {
ObjectTypeDB::bind_method(_MD("push_list","type"),&RichTextLabel::push_list); ObjectTypeDB::bind_method(_MD("push_list","type"),&RichTextLabel::push_list);
ObjectTypeDB::bind_method(_MD("push_meta","data"),&RichTextLabel::push_meta); ObjectTypeDB::bind_method(_MD("push_meta","data"),&RichTextLabel::push_meta);
ObjectTypeDB::bind_method(_MD("push_underline"),&RichTextLabel::push_underline); ObjectTypeDB::bind_method(_MD("push_underline"),&RichTextLabel::push_underline);
ObjectTypeDB::bind_method(_MD("push_table","columns"),&RichTextLabel::push_table);
ObjectTypeDB::bind_method(_MD("set_table_column_expand","column","expand"),&RichTextLabel::set_table_column_expand);
ObjectTypeDB::bind_method(_MD("push_cell"),&RichTextLabel::push_cell);
ObjectTypeDB::bind_method(_MD("pop"),&RichTextLabel::pop); ObjectTypeDB::bind_method(_MD("pop"),&RichTextLabel::pop);
ObjectTypeDB::bind_method(_MD("clear"),&RichTextLabel::clear); ObjectTypeDB::bind_method(_MD("clear"),&RichTextLabel::clear);
@ -1722,7 +1983,7 @@ void RichTextLabel::_bind_methods() {
BIND_CONSTANT( LIST_LETTERS ); BIND_CONSTANT( LIST_LETTERS );
BIND_CONSTANT( LIST_DOTS ); BIND_CONSTANT( LIST_DOTS );
BIND_CONSTANT( ITEM_MAIN ); BIND_CONSTANT( ITEM_FRAME );
BIND_CONSTANT( ITEM_TEXT ); BIND_CONSTANT( ITEM_TEXT );
BIND_CONSTANT( ITEM_IMAGE ); BIND_CONSTANT( ITEM_IMAGE );
BIND_CONSTANT( ITEM_NEWLINE ); BIND_CONSTANT( ITEM_NEWLINE );
@ -1750,8 +2011,8 @@ int RichTextLabel::get_visible_characters() const {
int RichTextLabel::get_total_character_count() const { int RichTextLabel::get_total_character_count() const {
int tc=0; int tc=0;
for(int i=0;i<lines.size();i++) for(int i=0;i<current_frame->lines.size();i++)
tc+=lines[i].char_count; tc+=current_frame->lines[i].char_count;
return tc; return tc;
} }
@ -1760,12 +2021,13 @@ int RichTextLabel::get_total_character_count() const {
RichTextLabel::RichTextLabel() { RichTextLabel::RichTextLabel() {
main = memnew( ItemMain ); main = memnew( ItemFrame );
main->index=0; main->index=0;
current=main; current=main;
lines.resize(1); main->lines.resize(1);
lines[0].from=main; main->lines[0].from=main;
first_invalid_line=0; main->first_invalid_line=0;
current_frame=main;
tab_size=4; tab_size=4;
default_align=ALIGN_LEFT; default_align=ALIGN_LEFT;
underline_meta=true; underline_meta=true;

View file

@ -54,7 +54,7 @@ public:
enum ItemType { enum ItemType {
ITEM_MAIN, ITEM_FRAME,
ITEM_TEXT, ITEM_TEXT,
ITEM_IMAGE, ITEM_IMAGE,
ITEM_NEWLINE, ITEM_NEWLINE,
@ -64,6 +64,7 @@ public:
ITEM_ALIGN, ITEM_ALIGN,
ITEM_INDENT, ITEM_INDENT,
ITEM_LIST, ITEM_LIST,
ITEM_TABLE,
ITEM_META ITEM_META
}; };
@ -72,6 +73,24 @@ protected:
static void _bind_methods(); static void _bind_methods();
private: private:
struct Item;
struct Line {
Item *from;
Vector<int> offset_caches;
Vector<int> height_caches;
Vector<int> space_caches;
int height_cache;
int height_accum_cache;
int char_count;
int minimum_width;
Line() { from=NULL; char_count=0; }
};
struct Item { struct Item {
int index; int index;
@ -79,18 +98,26 @@ private:
ItemType type; ItemType type;
List<Item*> subitems; List<Item*> subitems;
List<Item*>::Element *E; List<Item*>::Element *E;
int line;
void _clear_children() { while (subitems.size()) { memdelete(subitems.front()->get()); subitems.pop_front(); } } void _clear_children() { while (subitems.size()) { memdelete(subitems.front()->get()); subitems.pop_front(); } }
Item() { parent=NULL; E=NULL; } Item() { parent=NULL; E=NULL; line=0;}
virtual ~Item() { _clear_children(); } virtual ~Item() { _clear_children(); }
}; };
struct ItemMain : public Item { struct ItemFrame : public Item{
ItemMain() { type=ITEM_MAIN; } int parent_line;
bool cell;
Vector<Line> lines;
int first_invalid_line;
ItemFrame *parent_frame;
ItemFrame() { type=ITEM_FRAME; parent_frame=NULL; cell=false; parent_line=0; }
}; };
struct ItemText : public Item { struct ItemText : public Item {
String text; String text;
@ -150,11 +177,28 @@ private:
ItemNewline() { type=ITEM_NEWLINE; } ItemNewline() { type=ITEM_NEWLINE; }
}; };
ItemMain *main;
struct ItemTable : public Item{
struct Column {
bool expand;
int expand_ratio;
int min_width;
int width;
};
Vector<Column> columns;
int total_width;
ItemTable() { type=ITEM_TABLE; }
};
ItemFrame *main;
Item *current; Item *current;
ItemFrame *current_frame;
VScrollBar *vscroll; VScrollBar *vscroll;
bool scroll_visible; bool scroll_visible;
bool scroll_follow; bool scroll_follow;
bool scroll_following; bool scroll_following;
@ -163,34 +207,16 @@ private:
bool updating_scroll; bool updating_scroll;
int current_idx; int current_idx;
struct Line {
Item *from;
Vector<int> offset_caches;
Vector<int> height_caches;
Vector<int> space_caches;
int height_cache;
int height_accum_cache;
int char_count;
Line() { from=NULL; char_count=0; }
};
Vector<Line> lines;
int first_invalid_line;
int tab_size; int tab_size;
bool underline_meta; bool underline_meta;
Align default_align; Align default_align;
void _invalidate_current_line(); void _invalidate_current_line(ItemFrame *p_frame);
void _validate_line_caches(); void _validate_line_caches(ItemFrame *p_frame);
void _add_item(Item *p_item, bool p_enter=false); void _add_item(Item *p_item, bool p_enter=false,bool p_ensure_newline=false);
@ -227,8 +253,8 @@ private:
int visible_characters; int visible_characters;
void _process_line(int &y, int p_width, int p_line, ProcessMode p_mode,const Ref<Font> &p_base_font,const Color &p_base_color,const Point2i& p_click_pos=Point2i(),Item **r_click_item=NULL,int *r_click_char=NULL,bool *r_outside=NULL,int p_char_count=0); void _process_line(ItemFrame *p_frame,const Vector2& p_ofs,int &y, int p_width, int p_line, ProcessMode p_mode,const Ref<Font> &p_base_font,const Color &p_base_color,const Point2i& p_click_pos=Point2i(),Item **r_click_item=NULL,int *r_click_char=NULL,bool *r_outside=NULL,int p_char_count=0);
void _find_click(const Point2i& p_click,Item **r_click_item=NULL,int *r_click_char=NULL,bool *r_outside=NULL); void _find_click(ItemFrame *p_frame, const Point2i& p_click,Item **r_click_item=NULL,int *r_click_char=NULL,bool *r_outside=NULL);
Ref<Font> _find_font(Item *p_item); Ref<Font> _find_font(Item *p_item);
@ -242,12 +268,12 @@ private:
void _scroll_changed(double); void _scroll_changed(double);
void _input_event(InputEvent p_event); void _input_event(InputEvent p_event);
Item *_get_next_item(Item* p_item); Item *_get_next_item(Item* p_item, bool p_free=false);
bool use_bbcode; bool use_bbcode;
String bbcode; String bbcode;
void _update_all_lines();
protected: protected:
void _notification(int p_what); void _notification(int p_what);
@ -264,6 +290,10 @@ public:
void push_indent(int p_level); void push_indent(int p_level);
void push_list(ListType p_list); void push_list(ListType p_list);
void push_meta(const Variant& p_data); void push_meta(const Variant& p_data);
void push_table(int p_columns);
void set_table_column_expand(int p_column, bool p_expand, int p_ratio=1);
int get_current_table_column() const;
void push_cell();
void pop(); void pop();
void clear(); void clear();

View file

@ -797,6 +797,8 @@ void make_default_theme() {
t->set_color("selection_color","RichTextLabel", Color(0.1,0.1,1,0.8) ); t->set_color("selection_color","RichTextLabel", Color(0.1,0.1,1,0.8) );
t->set_constant("line_separation","RichTextLabel", 1 ); t->set_constant("line_separation","RichTextLabel", 1 );
t->set_constant("table_hseparation","RichTextLabel", 3 );
t->set_constant("table_vseparation","RichTextLabel", 3 );

View file

@ -690,16 +690,28 @@ Error EditorHelp::_goto_desc(const String& p_class,int p_vscr) {
class_desc->pop(); class_desc->pop();
//class_desc->add_newline(); //class_desc->add_newline();
class_desc->add_newline(); // class_desc->add_newline();
class_desc->push_indent(1); class_desc->push_indent(1);
class_desc->push_table(2);
class_desc->set_table_column_expand(1,1);
for(int i=0;i<cd.methods.size();i++) { for(int i=0;i<cd.methods.size();i++) {
class_desc->push_cell();
method_line[cd.methods[i].name]=class_desc->get_line_count()-2; //gets overriden if description method_line[cd.methods[i].name]=class_desc->get_line_count()-2; //gets overriden if description
class_desc->push_align(RichTextLabel::ALIGN_RIGHT);
class_desc->push_font(doc_code_font); class_desc->push_font(doc_code_font);
_add_type(cd.methods[i].return_type); _add_type(cd.methods[i].return_type);
class_desc->add_text(" "); //class_desc->add_text(" ");
class_desc->pop(); //align
class_desc->pop(); //font
class_desc->pop(); //cell
class_desc->push_cell();
class_desc->push_font(doc_code_font);
if (cd.methods[i].description!="") { if (cd.methods[i].description!="") {
method_descr=true; method_descr=true;
class_desc->push_meta("@"+cd.methods[i].name); class_desc->push_meta("@"+cd.methods[i].name);
@ -742,12 +754,14 @@ Error EditorHelp::_goto_desc(const String& p_class,int p_vscr) {
} }
class_desc->pop();//monofont class_desc->pop();//monofont
class_desc->add_newline(); // class_desc->add_newline();
class_desc->pop(); //cell
} }
class_desc->pop(); //table
class_desc->pop(); class_desc->pop();
class_desc->add_newline(); class_desc->add_newline();
class_desc->add_newline();
} }