Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] MultiRect - Fix flushing in TextEdit #79498

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions scene/gui/text_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,11 @@ void TextEdit::_notification(int p_what) {
line_drawing_cache[line] = cache_entry;
}

// Flush out any text in the drawer BEFORE
// drawing the completion box, as we want the completion
// box to overwrite the underlying text.
drawer.flush();

bool completion_below = false;
if (completion_active && is_cursor_visible && completion_options.size() > 0) {
// Completion panel
Expand Down
9 changes: 6 additions & 3 deletions scene/resources/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,15 +542,18 @@ FontDrawer::FontDrawer(const Ref<Font> &p_font, const Color &p_outline_color) :
font(p_font),
outline_color(p_outline_color) {
has_outline = p_font->has_outline();
multirect.begin();
}

FontDrawer::~FontDrawer() {
void FontDrawer::flush() {
for (int i = 0; i < pending_draws.size(); ++i) {
const PendingDraw &draw = pending_draws[i];
font->draw_char_ex(draw.canvas_item, draw.pos, draw.chr, draw.next, draw.modulate, false, &multirect);
}
multirect.end();
multirect.flush();
}

FontDrawer::~FontDrawer() {
flush();
}

void BitmapFont::set_fallback(const Ref<BitmapFont> &p_fallback) {
Expand Down
1 change: 1 addition & 0 deletions scene/resources/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class FontDrawer {
return font->draw_char_ex(p_canvas_item, p_pos, p_char, p_next, has_outline ? outline_color : p_modulate, has_outline, &multirect);
}
MultiRect &get_multirect() { return multirect; }
void flush();

~FontDrawer();
};
Expand Down
30 changes: 11 additions & 19 deletions servers/visual/visual_server_canvas_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,6 @@ Mutex VisualServerCanvasHelper::_tilemap_mutex;

bool VisualServerCanvasHelper::_multirect_enabled = true;

MultiRect::MultiRect() {
begin();
}
MultiRect::~MultiRect() {
end();
}

void MultiRect::begin() {
DEV_CHECK_ONCE(!rects.size());
rects.clear();
sources.clear();

state.flags = 0;
state_set = false;
}

void MultiRect::add_rect(RID p_canvas_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, RID p_normal_map, bool p_clip_uv) {
bool new_common_data = true;

Expand Down Expand Up @@ -101,7 +85,7 @@ void MultiRect::add_rect(RID p_canvas_item, const Rect2 &p_rect, RID p_texture,
if (!is_empty()) {
if ((state != s) ||
(rects.size() >= MAX_RECTS)) {
end();
flush();
} else {
new_common_data = false;
}
Expand All @@ -113,6 +97,11 @@ void MultiRect::add_rect(RID p_canvas_item, const Rect2 &p_rect, RID p_texture,

rects.push_back(rect);
sources.push_back(source);

// Legacy path
if (!VisualServerCanvasHelper::_multirect_enabled) {
flush();
}
}

void MultiRect::begin(const VisualServerCanvasHelper::State &p_state) {
Expand Down Expand Up @@ -184,7 +173,7 @@ bool MultiRect::add(const Rect2 &p_rect, const Rect2 &p_src_rect, bool p_commit_
return true;
}

void MultiRect::end() {
void MultiRect::flush() {
if (!is_empty()) {
if (VisualServerCanvasHelper::_multirect_enabled) {
VisualServer::get_singleton()->canvas_item_add_texture_multirect_region(state.item, rects, state.texture, sources, state.modulate, state.flags, state.normal_map);
Expand All @@ -203,6 +192,9 @@ void MultiRect::end() {
sources.clear();
}
state_set = false;

// This may not be necessary (if needing to eek out maximum speed).
state.flags = 0;
}

void VisualServerCanvasHelper::tilemap_begin() {
Expand Down Expand Up @@ -272,7 +264,7 @@ void VisualServerCanvasHelper::tilemap_end() {
}

for (uint32_t n = 0; n < _tilemap_multirects.size(); n++) {
_tilemap_multirects[n].end();
_tilemap_multirects[n].flush();
}

_tilemap_multirects.clear();
Expand Down
9 changes: 5 additions & 4 deletions servers/visual/visual_server_canvas_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,19 @@ class MultiRect {

public:
// Simple API
void begin();
void add_rect(RID p_canvas_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, RID p_normal_map = RID(), bool p_clip_uv = false);

// Efficient API
void begin(const VisualServerCanvasHelper::State &p_state);
bool add(const Rect2 &p_rect, const Rect2 &p_src_rect, bool p_commit_on_flip_change = true);
bool is_empty() const { return rects.is_empty(); }
bool is_full() const { return rects.is_full(); }
void end();

MultiRect();
~MultiRect();
// Always called in destructor, but can be used explicitly
// for multiple draws, or to time the flush.
void flush();

~MultiRect() { flush(); }
};

#endif // VISUAL_SERVER_CANVAS_HELPER_H