Merge pull request #2449 from leezh/label-valign-fix

reworked Label class
This commit is contained in:
Juan Linietsky 2015-09-25 11:29:49 -03:00
commit 52eebdc001
3 changed files with 246 additions and 134 deletions

View File

@ -14976,26 +14976,28 @@
<argument index="0" name="align" type="int"> <argument index="0" name="align" type="int">
</argument> </argument>
<description> <description>
Set the alignmend mode to any of the ALIGN_* enumeration values. Sets the alignment mode to any of the ALIGN_* enumeration values.
</description> </description>
</method> </method>
<method name="get_align" qualifiers="const"> <method name="get_align" qualifiers="const">
<return type="int"> <return type="int">
</return> </return>
<description> <description>
Return the alignmend mode (any of the ALIGN_* enumeration values). Return the alignment mode (any of the ALIGN_* enumeration values).
</description> </description>
</method> </method>
<method name="set_valign"> <method name="set_valign">
<argument index="0" name="valign" type="int"> <argument index="0" name="valign" type="int">
</argument> </argument>
<description> <description>
Sets the vertical alignment mode to any of the VALIGN_* enumeration values.
</description> </description>
</method> </method>
<method name="get_valign" qualifiers="const"> <method name="get_valign" qualifiers="const">
<return type="int"> <return type="int">
</return> </return>
<description> <description>
Return the vertical alignment mode (any of the VALIGN_* enumeration values).
</description> </description>
</method> </method>
<method name="set_text"> <method name="set_text">
@ -15026,16 +15028,32 @@
Return the state of the [i]autowrap[/i] mode (see [method set_autowrap]). Return the state of the [i]autowrap[/i] mode (see [method set_autowrap]).
</description> </description>
</method> </method>
<method name="set_clip_text">
<argument index="0" name="enable" type="bool">
</argument>
<description>
Cuts off the rest of the text if it is too wide.
</description>
</method>
<method name="is_clipping_text" qualifiers="const">
<return type="bool">
</return>
<description>
Return true if text would be cut off if it is too wide.
</description>
</method>
<method name="set_uppercase"> <method name="set_uppercase">
<argument index="0" name="enable" type="bool"> <argument index="0" name="enable" type="bool">
</argument> </argument>
<description> <description>
Display text in all capitals.
</description> </description>
</method> </method>
<method name="is_uppercase" qualifiers="const"> <method name="is_uppercase" qualifiers="const">
<return type="bool"> <return type="bool">
</return> </return>
<description> <description>
Return true if text is displayed in all capitals.
</description> </description>
</method> </method>
<method name="get_line_height" qualifiers="const"> <method name="get_line_height" qualifiers="const">
@ -15056,24 +15074,63 @@
<return type="int"> <return type="int">
</return> </return>
<description> <description>
Return the total length of the text.
</description> </description>
</method> </method>
<method name="set_visible_characters"> <method name="set_visible_characters">
<argument index="0" name="arg0" type="int"> <argument index="0" name="amount" type="int">
</argument> </argument>
<description> <description>
Restricts the number of characters to display. Set to -1 to disable.
</description>
</method>
<method name="get_visible_characters" qualifiers="const">
<return type="int">
</return>
<description>
Return the restricted number of characters to display. Returns -1 if unrestricted.
</description> </description>
</method> </method>
<method name="set_percent_visible"> <method name="set_percent_visible">
<argument index="0" name="percent_visible" type="float"> <argument index="0" name="percent_visible" type="float">
</argument> </argument>
<description> <description>
Restricts the number of characters to display (as a percentage of the total text).
</description> </description>
</method> </method>
<method name="get_percent_visible" qualifiers="const"> <method name="get_percent_visible" qualifiers="const">
<return type="float"> <return type="float">
</return> </return>
<description> <description>
Return the restricted number of characters to display (as a percentage of the total text).
</description>
</method>
<method name="set_max_lines_visible">
<argument index="0" name="lines_visible" type="int">
</argument>
<description>
Restricts the number of lines to display. Set to -1 to disable.
</description>
</method>
<method name="get_max_lines_visible" qualifiers="const">
<return type="int">
</return>
<description>
Return the restricted number of lines to display. Returns -1 if unrestricted.
</description>
</method>
<method name="set_lines_skipped">
<argument index="0" name="lines_skipped" type="int">
</argument>
<description>
Sets the number of lines to skip before displaying. Useful for scrolling text.
</description>
</method>
<method name="get_lines_skipped" qualifiers="const">
<return type="int">
</return>
<description>
Return the the number of lines to skipped before displaying.
</description> </description>
</method> </method>
</methods> </methods>

View File

@ -37,7 +37,7 @@ void Label::set_autowrap(bool p_autowrap) {
autowrap=p_autowrap; autowrap=p_autowrap;
word_cache_dirty=true; word_cache_dirty=true;
minimum_size_changed(); minimum_size_changed();
update();
} }
bool Label::has_autowrap() const { bool Label::has_autowrap() const {
@ -51,6 +51,7 @@ void Label::set_uppercase(bool p_uppercase) {
uppercase=p_uppercase; uppercase=p_uppercase;
word_cache_dirty=true; word_cache_dirty=true;
minimum_size_changed(); minimum_size_changed();
update();
} }
bool Label::is_uppercase() const { bool Label::is_uppercase() const {
@ -69,10 +70,9 @@ void Label::_notification(int p_what) {
if (p_what==NOTIFICATION_DRAW) { if (p_what==NOTIFICATION_DRAW) {
if (clip && !autowrap) if (clip || autowrap)
VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(),true); VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(),true);
if (word_cache_dirty) if (word_cache_dirty)
regenerate_word_cache(); regenerate_word_cache();
@ -91,38 +91,43 @@ void Label::_notification(int p_what) {
VisualServer::get_singleton()->canvas_item_set_distance_field_mode(get_canvas_item(),font.is_valid() && font->is_distance_field_hint()); VisualServer::get_singleton()->canvas_item_set_distance_field_mode(get_canvas_item(),font.is_valid() && font->is_distance_field_hint());
int font_h = font->get_height(); int font_h = font->get_height();
int line_from=(int)get_val(); // + p_exposed.pos.y / font_h;
int lines_visible = size.y/font_h; int lines_visible = size.y/font_h;
int line_to=(int)get_val() + lines_visible; //p_exposed.pos.y+p_exposed.size.height / font_h;
int space_w=font->get_char_size(' ').width; int space_w=font->get_char_size(' ').width;
int lines_total = get_max();
int chars_total=0; int chars_total=0;
int vbegin=0,vsep=0; int vbegin=0,vsep=0;
if (lines_total && lines_total < lines_visible) { if (lines_visible > line_count) {
lines_visible = line_count;
}
if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
lines_visible = max_lines_visible;
}
if (lines_visible > 0) {
switch(valign) { switch(valign) {
case VALIGN_TOP: { case VALIGN_TOP: {
//nothing //nothing
} break; } break;
case VALIGN_CENTER: { case VALIGN_CENTER: {
vbegin=(size.y - lines_visible * font_h) / 2;
vbegin=(lines_visible-lines_total) * font_h / 2;
vsep=0; vsep=0;
} break; } break;
case VALIGN_BOTTOM: { case VALIGN_BOTTOM: {
vbegin=(lines_visible-lines_total) * font_h; vbegin=size.y - lines_visible * font_h;
vsep=0; vsep=0;
} break; } break;
case VALIGN_FILL: { case VALIGN_FILL: {
vbegin=0; vbegin=0;
if (lines_total>1) { if (lines_visible>1) {
vsep=(lines_visible-lines_total) * font_h / (lines_total-1); vsep=(size.y - lines_visible * font_h) / (lines_visible - 1);
} else { } else {
vsep=0; vsep=0;
} }
@ -138,11 +143,12 @@ void Label::_notification(int p_what) {
int c = 0; int c = 0;
int line=0; int line=0;
int line_to=lines_skipped + (lines_visible>0?lines_visible:1);
while(wc) { while(wc) {
/* handle lines not meant to be drawn quickly */ /* handle lines not meant to be drawn quickly */
if (line>line_to) if (line>=line_to)
break; break;
if (line<line_from) { if (line<lines_skipped) {
while (wc && wc->char_pos>=0) while (wc && wc->char_pos>=0)
wc=wc->next; wc=wc->next;
@ -199,7 +205,7 @@ void Label::_notification(int p_what) {
} break; } break;
} }
int y_ofs=(line-(int)get_val())*font_h + font->get_ascent(); int y_ofs=(line-lines_skipped)*font_h + font->get_ascent();
y_ofs+=vbegin + line*vsep; y_ofs+=vbegin + line*vsep;
while(from!=to) { while(from!=to) {
@ -310,9 +316,9 @@ int Label::get_longest_line_width() const {
int max_line_width=0; int max_line_width=0;
int line_width=0; int line_width=0;
for (int i=0;i<text.size()+1;i++) { for (int i=0;i<text.size();i++) {
CharType current=i<text.length()?text[i]:' '; //always a space at the end, so the algo works CharType current=text[i];
if (uppercase) if (uppercase)
current=String::char_uppercase(current); current=String::char_uppercase(current);
@ -477,26 +483,19 @@ void Label::regenerate_word_cache() {
} }
//total_char_cache -= line_count + 1; // do not count new lines (including the first one)
if (!autowrap) { if (!autowrap) {
minsize.width=width; minsize.width=width;
minsize.height=font->get_height()*line_count; if (max_lines_visible > 0 && line_count > max_lines_visible) {
set_page( line_count ); minsize.height=font->get_height()*max_lines_visible;
} else { } else {
minsize.height=font->get_height()*line_count;
set_page( get_size().height / font->get_height() ); }
} }
set_max(line_count);
word_cache_dirty=false; word_cache_dirty=false;
} }
void Label::set_align(Align p_align) { void Label::set_align(Align p_align) {
ERR_FAIL_INDEX(p_align,4); ERR_FAIL_INDEX(p_align,4);
@ -530,16 +529,15 @@ void Label::set_text(const String& p_string) {
text=str; text=str;
word_cache_dirty=true; word_cache_dirty=true;
if (percent_visible<1)
visible_chars=get_total_character_count()*percent_visible;
update(); update();
if (!autowrap)
minimum_size_changed(); minimum_size_changed();
} }
void Label::set_clip_text(bool p_clip) { void Label::set_clip_text(bool p_clip) {
if (clip==p_clip)
return;
clip=p_clip; clip=p_clip;
update(); update();
minimum_size_changed(); minimum_size_changed();
@ -558,16 +556,32 @@ String Label::get_text() const {
void Label::set_visible_characters(int p_amount) { void Label::set_visible_characters(int p_amount) {
visible_chars=p_amount; visible_chars=p_amount;
if (get_total_character_count() > 0) {
percent_visible=(float)p_amount/(float)total_char_cache;
}
update(); update();
} }
int Label::get_visible_characters() const {
return visible_chars;
}
void Label::set_percent_visible(float p_percent) { void Label::set_percent_visible(float p_percent) {
if (p_percent<0) if (p_percent<0 || p_percent>=1) {
set_visible_characters(-1);
else visible_chars=-1;
set_visible_characters(get_total_character_count()*p_percent); percent_visible=1;
} else {
visible_chars=get_total_character_count()*p_percent;
percent_visible=p_percent; percent_visible=p_percent;
}
update();
} }
float Label::get_percent_visible() const{ float Label::get_percent_visible() const{
@ -575,6 +589,27 @@ float Label::get_percent_visible() const{
return percent_visible; return percent_visible;
} }
void Label::set_lines_skipped(int p_lines) {
lines_skipped=p_lines;
update();
}
int Label::get_lines_skipped() const{
return lines_skipped;
}
void Label::set_max_lines_visible(int p_lines) {
max_lines_visible=p_lines;
update();
}
int Label::get_max_lines_visible() const{
return max_lines_visible;
}
int Label::get_total_character_count() const { int Label::get_total_character_count() const {
@ -594,14 +629,21 @@ void Label::_bind_methods() {
ObjectTypeDB::bind_method(_MD("get_text"),&Label::get_text); ObjectTypeDB::bind_method(_MD("get_text"),&Label::get_text);
ObjectTypeDB::bind_method(_MD("set_autowrap","enable"),&Label::set_autowrap); ObjectTypeDB::bind_method(_MD("set_autowrap","enable"),&Label::set_autowrap);
ObjectTypeDB::bind_method(_MD("has_autowrap"),&Label::has_autowrap); ObjectTypeDB::bind_method(_MD("has_autowrap"),&Label::has_autowrap);
ObjectTypeDB::bind_method(_MD("set_clip_text","enable"),&Label::set_clip_text);
ObjectTypeDB::bind_method(_MD("is_clipping_text"),&Label::is_clipping_text);
ObjectTypeDB::bind_method(_MD("set_uppercase","enable"),&Label::set_uppercase); ObjectTypeDB::bind_method(_MD("set_uppercase","enable"),&Label::set_uppercase);
ObjectTypeDB::bind_method(_MD("is_uppercase"),&Label::is_uppercase); ObjectTypeDB::bind_method(_MD("is_uppercase"),&Label::is_uppercase);
ObjectTypeDB::bind_method(_MD("get_line_height"),&Label::get_line_height); ObjectTypeDB::bind_method(_MD("get_line_height"),&Label::get_line_height);
ObjectTypeDB::bind_method(_MD("get_line_count"),&Label::get_line_count); ObjectTypeDB::bind_method(_MD("get_line_count"),&Label::get_line_count);
ObjectTypeDB::bind_method(_MD("get_total_character_count"),&Label::get_total_character_count); ObjectTypeDB::bind_method(_MD("get_total_character_count"),&Label::get_total_character_count);
ObjectTypeDB::bind_method(_MD("set_visible_characters"),&Label::set_visible_characters); ObjectTypeDB::bind_method(_MD("set_visible_characters","amount"),&Label::set_visible_characters);
ObjectTypeDB::bind_method(_MD("get_visible_characters"),&Label::get_visible_characters);
ObjectTypeDB::bind_method(_MD("set_percent_visible","percent_visible"),&Label::set_percent_visible); ObjectTypeDB::bind_method(_MD("set_percent_visible","percent_visible"),&Label::set_percent_visible);
ObjectTypeDB::bind_method(_MD("get_percent_visible"),&Label::get_percent_visible); ObjectTypeDB::bind_method(_MD("get_percent_visible"),&Label::get_percent_visible);
ObjectTypeDB::bind_method(_MD("set_lines_skipped","lines_skipped"),&Label::set_lines_skipped);
ObjectTypeDB::bind_method(_MD("get_lines_skipped"),&Label::get_lines_skipped);
ObjectTypeDB::bind_method(_MD("set_max_lines_visible","lines_visible"),&Label::set_max_lines_visible);
ObjectTypeDB::bind_method(_MD("get_max_lines_visible"),&Label::get_max_lines_visible);
BIND_CONSTANT( ALIGN_LEFT ); BIND_CONSTANT( ALIGN_LEFT );
BIND_CONSTANT( ALIGN_CENTER ); BIND_CONSTANT( ALIGN_CENTER );
@ -617,8 +659,11 @@ void Label::_bind_methods() {
ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "align", PROPERTY_HINT_ENUM,"Left,Center,Right,Fill" ),_SCS("set_align"),_SCS("get_align") ); ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "align", PROPERTY_HINT_ENUM,"Left,Center,Right,Fill" ),_SCS("set_align"),_SCS("get_align") );
ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "valign", PROPERTY_HINT_ENUM,"Top,Center,Bottom,Fill" ),_SCS("set_valign"),_SCS("get_valign") ); ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "valign", PROPERTY_HINT_ENUM,"Top,Center,Bottom,Fill" ),_SCS("set_valign"),_SCS("get_valign") );
ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "autowrap"),_SCS("set_autowrap"),_SCS("has_autowrap") ); ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "autowrap"),_SCS("set_autowrap"),_SCS("has_autowrap") );
ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "clip_text"),_SCS("set_clip_text"),_SCS("is_clipping_text") );
ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "uppercase"),_SCS("set_uppercase"),_SCS("is_uppercase") ); ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "uppercase"),_SCS("set_uppercase"),_SCS("is_uppercase") );
ADD_PROPERTY( PropertyInfo( Variant::REAL, "percent_visible", PROPERTY_HINT_RANGE,"0,1,0.001"),_SCS("set_percent_visible"),_SCS("get_percent_visible") ); ADD_PROPERTY( PropertyInfo( Variant::REAL, "percent_visible", PROPERTY_HINT_RANGE,"0,1,0.001"),_SCS("set_percent_visible"),_SCS("get_percent_visible") );
ADD_PROPERTY( PropertyInfo( Variant::INT, "lines_skipped", PROPERTY_HINT_RANGE,"0,999,1"),_SCS("set_lines_skipped"),_SCS("get_lines_skipped") );
ADD_PROPERTY( PropertyInfo( Variant::INT, "max_lines_visible", PROPERTY_HINT_RANGE,"-1,999,1"),_SCS("set_max_lines_visible"),_SCS("get_max_lines_visible") );
} }
@ -636,7 +681,9 @@ Label::Label(const String &p_text) {
set_ignore_mouse(true); set_ignore_mouse(true);
total_char_cache=0; total_char_cache=0;
visible_chars=-1; visible_chars=-1;
percent_visible=-1; percent_visible=1;
lines_skipped=0;
max_lines_visible=-1;
set_text(p_text); set_text(p_text);
uppercase=false; uppercase=false;
} }

View File

@ -29,13 +29,13 @@
#ifndef LABEL_H #ifndef LABEL_H
#define LABEL_H #define LABEL_H
#include "scene/gui/range.h" #include "scene/gui/control.h"
/** /**
@author Juan Linietsky <reduzio@gmail.com> @author Juan Linietsky <reduzio@gmail.com>
*/ */
class Label : public Range { class Label : public Control {
OBJ_TYPE( Label, Range ); OBJ_TYPE( Label, Control );
public: public:
enum Align { enum Align {
@ -88,6 +88,8 @@ private:
WordCache *word_cache; WordCache *word_cache;
int total_char_cache; int total_char_cache;
int visible_chars; int visible_chars;
int lines_skipped;
int max_lines_visible;
protected: protected:
void _notification(int p_what); void _notification(int p_what);
@ -113,6 +115,7 @@ public:
bool is_uppercase() const; bool is_uppercase() const;
void set_visible_characters(int p_amount); void set_visible_characters(int p_amount);
int get_visible_characters() const;
int get_total_character_count() const; int get_total_character_count() const;
void set_clip_text(bool p_clip); void set_clip_text(bool p_clip);
@ -121,6 +124,11 @@ public:
void set_percent_visible(float p_percent); void set_percent_visible(float p_percent);
float get_percent_visible() const; float get_percent_visible() const;
void set_lines_skipped(int p_lines);
int get_lines_skipped() const;
void set_max_lines_visible(int p_lines);
int get_max_lines_visible() const;
int get_line_height() const; int get_line_height() const;
int get_line_count() const; int get_line_count() const;