summaryrefslogtreecommitdiff
path: root/scene/gui
diff options
context:
space:
mode:
Diffstat (limited to 'scene/gui')
-rw-r--r--scene/gui/rich_text_label.cpp113
-rw-r--r--scene/gui/rich_text_label.h17
-rw-r--r--scene/gui/scroll_container.cpp121
-rw-r--r--scene/gui/scroll_container.h1
4 files changed, 192 insertions, 60 deletions
diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp
index 5258aae80c..82f2106432 100644
--- a/scene/gui/rich_text_label.cpp
+++ b/scene/gui/rich_text_label.cpp
@@ -404,6 +404,18 @@ void RichTextLabel::_shape_line(ItemFrame *p_frame, int p_line, const Ref<Font>
break;
}
switch (it->type) {
+ case ITEM_DROPCAP: {
+ // Add dropcap.
+ const ItemDropcap *dc = (ItemDropcap *)it;
+ if (dc != nullptr) {
+ l.text_buf->set_dropcap(dc->text, dc->font, dc->font_size, dc->dropcap_margins);
+ l.dc_color = dc->color;
+ l.dc_ol_size = dc->ol_size;
+ l.dc_ol_color = dc->ol_color;
+ } else {
+ l.text_buf->clear_dropcap();
+ }
+ } break;
case ITEM_NEWLINE: {
Ref<Font> font = _find_font(it);
if (font.is_null()) {
@@ -679,6 +691,14 @@ void RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_
}
}
+ // Draw dropcap.
+ int dc_lines = l.text_buf->get_dropcap_lines();
+ float h_off = l.text_buf->get_dropcap_size().x;
+ if (l.dc_ol_size > 0) {
+ l.text_buf->draw_dropcap_outline(ci, p_ofs + ((rtl) ? Vector2() : Vector2(l.offset.x, 0)), l.dc_ol_size, l.dc_ol_color);
+ }
+ l.text_buf->draw_dropcap(ci, p_ofs + ((rtl) ? Vector2() : Vector2(l.offset.x, 0)), l.dc_color);
+
// Draw text.
for (int line = 0; line < l.text_buf->get_line_count(); line++) {
RID rid = l.text_buf->get_line_rid(line);
@@ -718,6 +738,14 @@ void RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_
} break;
}
+ if (line <= dc_lines) {
+ if (rtl) {
+ off.x -= h_off;
+ } else {
+ off.x += h_off;
+ }
+ }
+
//draw_rect(Rect2(p_ofs + off, TS->shaped_text_get_size(rid)), Color(1,0,0), false, 2); //DEBUG_RECTS
off.y += TS->shaped_text_get_ascent(rid);
@@ -1742,6 +1770,19 @@ Dictionary RichTextLabel::_find_font_features(Item *p_item) {
return Dictionary();
}
+RichTextLabel::ItemDropcap *RichTextLabel::_find_dc_item(Item *p_item) {
+ Item *item = p_item;
+
+ while (item) {
+ if (item->type == ITEM_DROPCAP) {
+ return static_cast<ItemDropcap *>(item);
+ }
+ item = item->parent;
+ }
+
+ return nullptr;
+}
+
RichTextLabel::ItemList *RichTextLabel::_find_list_item(Item *p_item) {
Item *item = p_item;
@@ -2316,6 +2357,24 @@ bool RichTextLabel::remove_line(const int p_line) {
return true;
}
+void RichTextLabel::push_dropcap(const String &p_string, const Ref<Font> &p_font, int p_size, const Rect2 &p_dropcap_margins, const Color &p_color, int p_ol_size, const Color &p_ol_color) {
+ ERR_FAIL_COND(current->type == ITEM_TABLE);
+ ERR_FAIL_COND(p_string.is_empty());
+ ERR_FAIL_COND(p_font.is_null());
+ ERR_FAIL_COND(p_size <= 0);
+
+ ItemDropcap *item = memnew(ItemDropcap);
+
+ item->text = p_string;
+ item->font = p_font;
+ item->font_size = p_size;
+ item->color = p_color;
+ item->ol_size = p_ol_size;
+ item->ol_color = p_ol_color;
+ item->dropcap_margins = p_dropcap_margins;
+ _add_item(item, false);
+}
+
void RichTextLabel::push_font(const Ref<Font> &p_font) {
ERR_FAIL_COND(current->type == ITEM_TABLE);
ERR_FAIL_COND(p_font.is_null());
@@ -2765,7 +2824,7 @@ Error RichTextLabel::append_bbcode(const String &p_bbcode) {
tag_stack.pop_front();
pos = brk_end + 1;
- if (tag != "/img") {
+ if (tag != "/img" && tag != "/dropcap") {
pop();
}
@@ -3041,6 +3100,54 @@ Error RichTextLabel::append_bbcode(const String &p_bbcode) {
push_meta(url);
pos = brk_end + 1;
tag_stack.push_front("url");
+ } else if (tag.begins_with("dropcap")) {
+ Vector<String> subtag = tag.substr(5, tag.length()).split(" ");
+ Ref<Font> f = get_theme_font("normal_font");
+ int fs = get_theme_font_size("normal_font_size") * 3;
+ Color color = get_theme_color("default_color");
+ Color outline_color = get_theme_color("outline_color");
+ int outline_size = get_theme_constant("outline_size");
+ Rect2 dropcap_margins = Rect2();
+
+ for (int i = 0; i < subtag.size(); i++) {
+ Vector<String> subtag_a = subtag[i].split("=");
+ if (subtag_a.size() == 2) {
+ if (subtag_a[0] == "font" || subtag_a[0] == "f") {
+ String fnt = subtag_a[1];
+ Ref<Font> font = ResourceLoader::load(fnt, "Font");
+ if (font.is_valid()) {
+ f = font;
+ }
+ } else if (subtag_a[0] == "font_size") {
+ fs = subtag_a[1].to_int();
+ } else if (subtag_a[0] == "margins") {
+ Vector<String> subtag_b = subtag_a[1].split(",");
+ if (subtag_b.size() == 4) {
+ dropcap_margins.position.x = subtag_b[0].to_float();
+ dropcap_margins.position.y = subtag_b[1].to_float();
+ dropcap_margins.size.x = subtag_b[2].to_float();
+ dropcap_margins.size.y = subtag_b[3].to_float();
+ }
+ } else if (subtag_a[0] == "outline_size") {
+ outline_size = subtag_a[1].to_int();
+ } else if (subtag_a[0] == "color") {
+ color = _get_color_from_string(subtag_a[1], color);
+ } else if (subtag_a[0] == "outline_color") {
+ outline_color = _get_color_from_string(subtag_a[1], outline_color);
+ }
+ }
+ }
+ int end = p_bbcode.find("[", brk_end);
+ if (end == -1) {
+ end = p_bbcode.length();
+ }
+
+ String txt = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
+
+ push_dropcap(txt, f, fs, dropcap_margins, color, outline_size, outline_color);
+
+ pos = end;
+ tag_stack.push_front(bbcode_name);
} else if (tag.begins_with("img")) {
VAlign align = VALIGN_TOP;
if (tag.begins_with("img=")) {
@@ -3679,6 +3786,7 @@ void RichTextLabel::_bind_methods() {
ClassDB::bind_method(D_METHOD("push_underline"), &RichTextLabel::push_underline);
ClassDB::bind_method(D_METHOD("push_strikethrough"), &RichTextLabel::push_strikethrough);
ClassDB::bind_method(D_METHOD("push_table", "columns", "inline_align"), &RichTextLabel::push_table, DEFVAL(VALIGN_TOP));
+ ClassDB::bind_method(D_METHOD("push_dropcap", "string", "font", "size", "dropcap_margins", "color", "outline_size", "outline_color"), &RichTextLabel::push_dropcap, DEFVAL(Rect2()), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(0, 0, 0, 0)));
ClassDB::bind_method(D_METHOD("set_table_column_expand", "column", "expand", "ratio"), &RichTextLabel::set_table_column_expand);
ClassDB::bind_method(D_METHOD("set_cell_row_background_color", "odd_row_bg", "even_row_bg"), &RichTextLabel::set_cell_row_background_color);
ClassDB::bind_method(D_METHOD("set_cell_border_color", "color"), &RichTextLabel::set_cell_border_color);
@@ -3814,8 +3922,9 @@ void RichTextLabel::_bind_methods() {
BIND_ENUM_CONSTANT(ITEM_WAVE);
BIND_ENUM_CONSTANT(ITEM_TORNADO);
BIND_ENUM_CONSTANT(ITEM_RAINBOW);
- BIND_ENUM_CONSTANT(ITEM_CUSTOMFX);
BIND_ENUM_CONSTANT(ITEM_META);
+ BIND_ENUM_CONSTANT(ITEM_DROPCAP);
+ BIND_ENUM_CONSTANT(ITEM_CUSTOMFX);
}
void RichTextLabel::set_visible_characters(int p_visible) {
diff --git a/scene/gui/rich_text_label.h b/scene/gui/rich_text_label.h
index f1dac69dce..99d8cb5863 100644
--- a/scene/gui/rich_text_label.h
+++ b/scene/gui/rich_text_label.h
@@ -76,6 +76,7 @@ public:
ITEM_TORNADO,
ITEM_RAINBOW,
ITEM_META,
+ ITEM_DROPCAP,
ITEM_CUSTOMFX
};
@@ -89,6 +90,9 @@ private:
Item *from = nullptr;
Ref<TextParagraph> text_buf;
+ Color dc_color;
+ int dc_ol_size = 0;
+ Color dc_ol_color;
Vector2 offset;
int char_offset = 0;
@@ -140,6 +144,17 @@ private:
ItemText() { type = ITEM_TEXT; }
};
+ struct ItemDropcap : public Item {
+ String text;
+ Ref<Font> font;
+ int font_size;
+ Color color;
+ int ol_size;
+ Color ol_color;
+ Rect2 dropcap_margins;
+ ItemDropcap() { type = ITEM_DROPCAP; }
+ };
+
struct ItemImage : public Item {
Ref<Texture2D> image;
VAlign inline_align = VALIGN_TOP;
@@ -391,6 +406,7 @@ private:
Dictionary _find_font_features(Item *p_item);
int _find_outline_size(Item *p_item);
ItemList *_find_list_item(Item *p_item);
+ ItemDropcap *_find_dc_item(Item *p_item);
int _find_list(Item *p_item, Vector<int> &r_index, Vector<ItemList *> &r_list);
int _find_margin(Item *p_item, const Ref<Font> &p_base_font, int p_base_font_size);
Align _find_align(Item *p_item);
@@ -435,6 +451,7 @@ public:
void add_image(const Ref<Texture2D> &p_image, const int p_width = 0, const int p_height = 0, const Color &p_color = Color(1.0, 1.0, 1.0), VAlign p_align = VALIGN_TOP);
void add_newline();
bool remove_line(const int p_line);
+ void push_dropcap(const String &p_string, const Ref<Font> &p_font, int p_size, const Rect2 &p_dropcap_margins = Rect2(), const Color &p_color = Color(1, 1, 1), int p_ol_size = 0, const Color &p_ol_color = Color(0, 0, 0, 0));
void push_font(const Ref<Font> &p_font);
void push_font_size(int p_font_size);
void push_font_features(const Dictionary &p_features);
diff --git a/scene/gui/scroll_container.cpp b/scene/gui/scroll_container.cpp
index 35f504812d..af9c5cb0c5 100644
--- a/scene/gui/scroll_container.cpp
+++ b/scene/gui/scroll_container.cpp
@@ -264,6 +264,67 @@ void ScrollContainer::_ensure_focused_visible(Control *p_control) {
}
}
+void ScrollContainer::_update_dimensions() {
+ child_max_size = Size2(0, 0);
+ Size2 size = get_size();
+ Point2 ofs;
+
+ Ref<StyleBox> sb = get_theme_stylebox("bg");
+ size -= sb->get_minimum_size();
+ ofs += sb->get_offset();
+ bool rtl = is_layout_rtl();
+
+ if (h_scroll->is_visible_in_tree() && h_scroll->get_parent() == this) { //scrolls may have been moved out for reasons
+ size.y -= h_scroll->get_minimum_size().y;
+ }
+
+ if (v_scroll->is_visible_in_tree() && v_scroll->get_parent() == this) { //scrolls may have been moved out for reasons
+ size.x -= v_scroll->get_minimum_size().x;
+ }
+
+ for (int i = 0; i < get_child_count(); i++) {
+ Control *c = Object::cast_to<Control>(get_child(i));
+ if (!c) {
+ continue;
+ }
+ if (c->is_set_as_top_level()) {
+ continue;
+ }
+ if (c == h_scroll || c == v_scroll) {
+ continue;
+ }
+ Size2 minsize = c->get_combined_minimum_size();
+ child_max_size.x = MAX(child_max_size.x, minsize.x);
+ child_max_size.y = MAX(child_max_size.y, minsize.y);
+
+ Rect2 r = Rect2(-scroll, minsize);
+ if (!scroll_h || (!h_scroll->is_visible_in_tree() && c->get_h_size_flags() & SIZE_EXPAND)) {
+ r.position.x = 0;
+ if (c->get_h_size_flags() & SIZE_EXPAND) {
+ r.size.width = MAX(size.width, minsize.width);
+ } else {
+ r.size.width = minsize.width;
+ }
+ }
+ if (!scroll_v || (!v_scroll->is_visible_in_tree() && c->get_v_size_flags() & SIZE_EXPAND)) {
+ r.position.y = 0;
+ if (c->get_v_size_flags() & SIZE_EXPAND) {
+ r.size.height = MAX(size.height, minsize.height);
+ } else {
+ r.size.height = minsize.height;
+ }
+ }
+ r.position += ofs;
+ if (rtl && v_scroll->is_visible_in_tree() && v_scroll->get_parent() == this) {
+ r.position.x += v_scroll->get_minimum_size().x;
+ }
+ r.position = r.position.floor();
+ fit_child_in_rect(c, r);
+ }
+
+ update();
+}
+
void ScrollContainer::_notification(int p_what) {
if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_LAYOUT_DIRECTION_CHANGED || p_what == NOTIFICATION_TRANSLATION_CHANGED) {
_updating_scrollbars = true;
@@ -272,67 +333,11 @@ void ScrollContainer::_notification(int p_what) {
if (p_what == NOTIFICATION_READY) {
get_viewport()->connect("gui_focus_changed", callable_mp(this, &ScrollContainer::_ensure_focused_visible));
+ _update_dimensions();
}
if (p_what == NOTIFICATION_SORT_CHILDREN) {
- child_max_size = Size2(0, 0);
- Size2 size = get_size();
- Point2 ofs;
-
- Ref<StyleBox> sb = get_theme_stylebox("bg");
- size -= sb->get_minimum_size();
- ofs += sb->get_offset();
- bool rtl = is_layout_rtl();
-
- if (h_scroll->is_visible_in_tree() && h_scroll->get_parent() == this) { //scrolls may have been moved out for reasons
- size.y -= h_scroll->get_minimum_size().y;
- }
-
- if (v_scroll->is_visible_in_tree() && v_scroll->get_parent() == this) { //scrolls may have been moved out for reasons
- size.x -= v_scroll->get_minimum_size().x;
- }
-
- for (int i = 0; i < get_child_count(); i++) {
- Control *c = Object::cast_to<Control>(get_child(i));
- if (!c) {
- continue;
- }
- if (c->is_set_as_top_level()) {
- continue;
- }
- if (c == h_scroll || c == v_scroll) {
- continue;
- }
- Size2 minsize = c->get_combined_minimum_size();
- child_max_size.x = MAX(child_max_size.x, minsize.x);
- child_max_size.y = MAX(child_max_size.y, minsize.y);
-
- Rect2 r = Rect2(-scroll, minsize);
- if (!scroll_h || (!h_scroll->is_visible_in_tree() && c->get_h_size_flags() & SIZE_EXPAND)) {
- r.position.x = 0;
- if (c->get_h_size_flags() & SIZE_EXPAND) {
- r.size.width = MAX(size.width, minsize.width);
- } else {
- r.size.width = minsize.width;
- }
- }
- if (!scroll_v || (!v_scroll->is_visible_in_tree() && c->get_v_size_flags() & SIZE_EXPAND)) {
- r.position.y = 0;
- if (c->get_v_size_flags() & SIZE_EXPAND) {
- r.size.height = MAX(size.height, minsize.height);
- } else {
- r.size.height = minsize.height;
- }
- }
- r.position += ofs;
- if (rtl && v_scroll->is_visible_in_tree() && v_scroll->get_parent() == this) {
- r.position.x += v_scroll->get_minimum_size().x;
- }
- r.position = r.position.floor();
- fit_child_in_rect(c, r);
- }
-
- update();
+ _update_dimensions();
};
if (p_what == NOTIFICATION_DRAW) {
diff --git a/scene/gui/scroll_container.h b/scene/gui/scroll_container.h
index 4bf200009e..d2f5e87822 100644
--- a/scene/gui/scroll_container.h
+++ b/scene/gui/scroll_container.h
@@ -69,6 +69,7 @@ protected:
Size2 get_minimum_size() const override;
void _gui_input(const Ref<InputEvent> &p_gui_input);
+ void _update_dimensions();
void _notification(int p_what);
void _scroll_moved(float);