ronantakizawa/codereview-bench huggingface dataset:
Repository: godotengine/godot
File: editor/plugins/texture_3d_editor_plugin.cpp (C++)

Code before:

#include "scene/gui/label.h"

void Texture3DEditor::_texture_rect_draw() {
	texture_rect->draw_rect(Rect2(Point2(), texture_rect->get_size()), Color(1, 1, 1, 1));
}

void Texture3DEditor::_notification(int p_what) {
	switch (p_what) {
		case NOTIFICATION_RESIZED: {
			_texture_rect_update_area();
		} break;

		case NOTIFICATION_DRAW: {
			Ref<Texture2D> checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
			Size2 size = get_size();

			draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
		} break;
	}
}

void Texture3DEditor::_texture_changed() {
	if (!is_visible()) {
		return;
	}
	queue_redraw();
}

void Texture3DEditor::_update_material() {
	material->set_shader_parameter("layer", (layer->get_value() + 0.5) / texture->get_depth());
	material->set_shader_parameter("tex", texture->get_rid());

	String format = Image::get_format_name(texture->get_format());

	String text;
	text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + "x" + itos(texture->get_depth()) + " " + format;

	info->set_text(text);
}

void Texture3DEditor::_make_shaders() {
	shader.instantiate();
	shader->set_code(R"(
// Texture3DEditor preview shader.

shader_type canvas_item;

uniform sampler3D tex;
uniform float layer;


Review comment (suggestion):
```suggestion
				info->add_theme_font_override(SceneStringName(font), metadata_label_font);
```
New constant

Code after:

#include "editor/editor_string_names.h"
#include "editor/themes/editor_scale.h"
#include "scene/gui/label.h"

// Shader sources.

constexpr const char *texture_3d_shader = R"(
	// Texture3DEditor preview shader.

	shader_type canvas_item;

	uniform sampler3D tex;
	uniform float layer;

	void fragment() {
		COLOR = textureLod(tex, vec3(UV, layer), 0.0);
	}
)";

void Texture3DEditor::_texture_rect_draw() {
	texture_rect->draw_rect(Rect2(Point2(), texture_rect->get_size()), Color(1, 1, 1, 1));
}

void Texture3DEditor::_notification(int p_what) {
	switch (p_what) {
		case NOTIFICATION_RESIZED: {
			_texture_rect_update_area();
		} break;

		case NOTIFICATION_DRAW: {
			Ref<Texture2D> checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
			Size2 size = get_size();

			draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
		} break;

		case NOTIFICATION_THEME_CHANGED: {
			if (info) {
				Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts));
				info->add_theme_font_override(SceneStringName(font), metadata_label_font);
			}
		} break;
	}
}

void Texture3DEditor::_texture_changed() {
	if (!is_visible()) {
		return;
	}
Repository: godotengine/godot
File: editor/plugins/texture_layered_editor_plugin.cpp (C++)

Code before:

uniform sampler2DArray tex;
uniform float layer;

void fragment() {
	COLOR = textureLod(tex, vec3(UV, layer), 0.0);
}
)");

	shaders[1].instantiate();
	shaders[1]->set_code(R"(
// TextureLayeredEditor preview shader (cubemap).

shader_type canvas_item;

uniform samplerCube tex;
uniform vec3 normal;
uniform mat3 rot;

void fragment() {
	vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z));
	COLOR = textureLod(tex, n, 0.0);
}
)");

	shaders[2].instantiate();
	shaders[2]->set_code(R"(
// TextureLayeredEditor preview shader (cubemap array).

shader_type canvas_item;

uniform samplerCubeArray tex;
uniform vec3 normal;
uniform mat3 rot;
uniform float layer;

void fragment() {
	vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z));
	COLOR = textureLod(tex, vec4(n, layer), 0.0);
}
)");

	for (int i = 0; i < 3; i++) {
		materials[i].instantiate();
		materials[i]->set_shader(shaders[i]);
	}
}

void TextureLayeredEditor::_texture_rect_update_area() {
	Size2 size = get_size();
	int tex_width = texture->get_width() * size.height / texture->get_height();

Review comment (suggestion):
```suggestion
				info->add_theme_font_override(SceneStringName(font), metadata_label_font);
```

Code after:
		case TextureLayered::LAYERED_TYPE_2D_ARRAY: {
			layer->set_max(texture->get_layers() - 1);

			texture_info = vformat(String::utf8("%d×%d (×%d) %s\n"),
					texture->get_width(),
					texture->get_height(),
					texture->get_layers(),
					format);

		} break;
		case TextureLayered::LAYERED_TYPE_CUBEMAP: {
			layer->hide();

			texture_info = vformat(String::utf8("%d×%d %s\n"),
					texture->get_width(),
					texture->get_height(),
					format);

		} break;
		case TextureLayered::LAYERED_TYPE_CUBEMAP_ARRAY: {
			layer->set_max(texture->get_layers() / 6 - 1);

			texture_info = vformat(String::utf8("%d×%d (×%d) %s\n"),
					texture->get_width(),
					texture->get_height(),
					texture->get_layers() / 6,
					format);

		} break;

		default: {
		}
	}

	if (texture->has_mipmaps()) {
		const int mip_count = Image::get_image_required_mipmaps(texture->get_width(), texture->get_height(), texture->get_format());
		const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), texture->get_format(), true) * texture->get_layers();

		texture_info += vformat(TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"),
				mip_count,
				String::humanize_size(memory));

	} else {
		const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), texture->get_format(), false) * texture->get_layers();

		texture_info += vformat(TTR("No Mipmaps") + "\n" + TTR("Memory: %s"),
				String::humanize_size(memory));
	}

	info->set_text(texture_info);
}
Repository: godotengine/godot
File: editor/plugins/texture_layered_editor_plugin.h (C/C++)

Code before:
#define TEXTURE_LAYERED_EDITOR_PLUGIN_H

#include "editor/editor_inspector.h"
#include "editor/plugins/editor_plugin.h"
#include "scene/gui/spin_box.h"
#include "scene/resources/shader.h"
#include "scene/resources/texture.h"

class TextureLayeredEditor : public Control {
	GDCLASS(TextureLayeredEditor, Control);

	SpinBox *layer = nullptr;
	Label *info = nullptr;
	Ref<TextureLayered> texture;

	Ref<Shader> shaders[3];
	Ref<ShaderMaterial> materials[3];

	float x_rot = 0;
	float y_rot = 0;
	Control *texture_rect = nullptr;

	bool setting = false;

	void _make_shaders();
	void _update_material();

	void _layer_changed(double) {
		if (!setting) {
			_update_material();
		}
	}
	void _texture_changed();

	void _texture_rect_update_area();
	void _texture_rect_draw();

protected:
	void _notification(int p_what);
	virtual void gui_input(const Ref<InputEvent> &p_event) override;

public:
	void edit(Ref<TextureLayered> p_texture);
	TextureLayeredEditor();
	~TextureLayeredEditor();
};

class EditorInspectorPluginLayeredTexture : public EditorInspectorPlugin {
	GDCLASS(EditorInspectorPluginLayeredTexture, EditorInspectorPlugin);

public:

Review comment (suggestion):
```suggestion
	void _update_material(bool p_texture_changed);
```

Code after:
#define TEXTURE_LAYERED_EDITOR_PLUGIN_H

#include "editor/editor_inspector.h"
#include "editor/plugins/editor_plugin.h"
#include "scene/gui/spin_box.h"
#include "scene/resources/shader.h"
#include "scene/resources/texture.h"

class TextureLayeredEditor : public Control {
	GDCLASS(TextureLayeredEditor, Control);

	SpinBox *layer = nullptr;
	Label *info = nullptr;
	Ref<TextureLayered> texture;

	Ref<Shader> shaders[3];
	Ref<ShaderMaterial> materials[3];

	float x_rot = 0;
	float y_rot = 0;
	Control *texture_rect = nullptr;

	bool setting = false;

	void _make_shaders();
	void _update_material(bool p_texture_changed);

	void _layer_changed(double) {
		if (!setting) {
			_update_material(false);
		}
	}

	void _texture_changed();

	void _texture_rect_update_area();
	void _texture_rect_draw();

	void _update_gui();

protected:
	void _notification(int p_what);
	virtual void gui_input(const Ref<InputEvent> &p_event) override;

public:
	void edit(Ref<TextureLayered> p_texture);

	TextureLayeredEditor();
	~TextureLayeredEditor();
};
Repository: godotengine/godot
File: editor/plugins/texture_layered_editor_plugin.cpp (C++)

Code before:
void fragment() {
	vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z));
	COLOR = textureLod(tex, n, 0.0);
}
)");

	shaders[2].instantiate();
	shaders[2]->set_code(R"(
// TextureLayeredEditor preview shader (cubemap array).

shader_type canvas_item;

uniform samplerCubeArray tex;
uniform vec3 normal;
uniform mat3 rot;
uniform float layer;

void fragment() {
	vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z));
	COLOR = textureLod(tex, vec4(n, layer), 0.0);
}
)");

	for (int i = 0; i < 3; i++) {
		materials[i].instantiate();
		materials[i]->set_shader(shaders[i]);
	}
}

void TextureLayeredEditor::_texture_rect_update_area() {
	Size2 size = get_size();
	int tex_width = texture->get_width() * size.height / texture->get_height();
	int tex_height = size.height;

	if (tex_width > size.width) {
		tex_width = size.width;
		tex_height = texture->get_height() * tex_width / texture->get_width();
	}

	// Prevent the texture from being unpreviewable after the rescale, so that we can still see something
	if (tex_height <= 0) {
		tex_height = 1;
	}
	if (tex_width <= 0) {
		tex_width = 1;
	}

	int ofs_x = (size.width - tex_width) / 2;
	int ofs_y = (size.height - tex_height) / 2;

	texture_rect->set_position(Vector2(ofs_x, ofs_y));

Review comment (suggestion):
```suggestion
void TextureLayeredEditor::_update_material(bool p_texture_changed) {
```

Code after:
		case TextureLayered::LAYERED_TYPE_CUBEMAP_ARRAY: {
			layer->set_max(texture->get_layers() / 6 - 1);

			texture_info = vformat(String::utf8("%d×%d (×%d) %s\n"),
					texture->get_width(),
					texture->get_height(),
					texture->get_layers() / 6,
					format);

		} break;

		default: {
		}
	}

	if (texture->has_mipmaps()) {
		const int mip_count = Image::get_image_required_mipmaps(texture->get_width(), texture->get_height(), texture->get_format());
		const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), texture->get_format(), true) * texture->get_layers();

		texture_info += vformat(TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"),
				mip_count,
				String::humanize_size(memory));

	} else {
		const int memory = Image::get_image_data_size(texture->get_width(), texture->get_height(), texture->get_format(), false) * texture->get_layers();

		texture_info += vformat(TTR("No Mipmaps") + "\n" + TTR("Memory: %s"),
				String::humanize_size(memory));
	}

	info->set_text(texture_info);
}

void TextureLayeredEditor::_notification(int p_what) {
	switch (p_what) {
		case NOTIFICATION_RESIZED: {
			_texture_rect_update_area();
		} break;

		case NOTIFICATION_DRAW: {
			Ref<Texture2D> checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
			Size2 size = get_size();

			draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
		} break;

		case NOTIFICATION_THEME_CHANGED: {
			if (info) {
				Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts));
				info->add_theme_font_override(SceneStringName(font), metadata_label_font);
			}
Repository: godotengine/godot
File: editor/plugins/texture_3d_editor_plugin.h (C/C++)

Code before:
	GDCLASS(Texture3DEditor, Control);

	SpinBox *layer = nullptr;
	Label *info = nullptr;
	Ref<Texture3D> texture;

	Ref<Shader> shader;
	Ref<ShaderMaterial> material;

	Control *texture_rect = nullptr;

	bool setting = false;

	void _make_shaders();
	void _update_material();

	void _layer_changed(double) {
		if (!setting) {
			_update_material();
		}
	}
	void _texture_changed();

	void _texture_rect_update_area();
	void _texture_rect_draw();

protected:
	void _notification(int p_what);

public:
	void edit(Ref<Texture3D> p_texture);
	Texture3DEditor();
	~Texture3DEditor();
};

class EditorInspectorPlugin3DTexture : public EditorInspectorPlugin {
	GDCLASS(EditorInspectorPlugin3DTexture, EditorInspectorPlugin);

public:
	virtual bool can_handle(Object *p_object) override;
	virtual void parse_begin(Object *p_object) override;
};

class Texture3DEditorPlugin : public EditorPlugin {
	GDCLASS(Texture3DEditorPlugin, EditorPlugin);

public:
	virtual String get_name() const override { return "Texture3D"; }

	Texture3DEditorPlugin();
};

Review comment (suggestion):
```suggestion
	void _update_material(bool p_texture_changed);
```

Code after:
	GDCLASS(Texture3DEditor, Control);

	SpinBox *layer = nullptr;
	Label *info = nullptr;
	Ref<Texture3D> texture;

	Ref<Shader> shader;
	Ref<ShaderMaterial> material;

	Control *texture_rect = nullptr;

	bool setting = false;

	void _make_shaders();

	void _layer_changed(double) {
		if (!setting) {
			_update_material(false);
		}
	}

	void _texture_changed();

	void _texture_rect_update_area();
	void _texture_rect_draw();

	void _update_material(bool p_texture_changed);
	void _update_gui();

protected:
	void _notification(int p_what);

public:
	void edit(Ref<Texture3D> p_texture);

	Texture3DEditor();
	~Texture3DEditor();
};

class EditorInspectorPlugin3DTexture : public EditorInspectorPlugin {
	GDCLASS(EditorInspectorPlugin3DTexture, EditorInspectorPlugin);

public:
	virtual bool can_handle(Object *p_object) override;
	virtual void parse_begin(Object *p_object) override;
};

class Texture3DEditorPlugin : public EditorPlugin {
	GDCLASS(Texture3DEditorPlugin, EditorPlugin);

public:
Repository: godotengine/godot
File: editor/plugins/texture_3d_editor_plugin.cpp (C++)

Code before:
	}
}

void Texture3DEditor::_texture_changed() {
	if (!is_visible()) {
		return;
	}
	queue_redraw();
}

void Texture3DEditor::_update_material() {
	material->set_shader_parameter("layer", (layer->get_value() + 0.5) / texture->get_depth());
	material->set_shader_parameter("tex", texture->get_rid());

	String format = Image::get_format_name(texture->get_format());

	String text;
	text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + "x" + itos(texture->get_depth()) + " " + format;

	info->set_text(text);
}

void Texture3DEditor::_make_shaders() {
	shader.instantiate();
	shader->set_code(R"(
// Texture3DEditor preview shader.

shader_type canvas_item;

uniform sampler3D tex;
uniform float layer;

void fragment() {
	COLOR = textureLod(tex, vec3(UV, layer), 0.0);
}
)");
	material.instantiate();
	material->set_shader(shader);
}

void Texture3DEditor::_texture_rect_update_area() {
	Size2 size = get_size();
	int tex_width = texture->get_width() * size.height / texture->get_height();
	int tex_height = size.height;

	if (tex_width > size.width) {
		tex_width = size.width;
		tex_height = texture->get_height() * tex_width / texture->get_width();
	}

	// Prevent the texture from being unpreviewable after the rescale, so that we can still see something

Review comment (suggestion):
```suggestion
void Texture3DEditor::_update_material(bool p_texture_changed) {
```

Code after:

void Texture3DEditor::_texture_rect_draw() {
	texture_rect->draw_rect(Rect2(Point2(), texture_rect->get_size()), Color(1, 1, 1, 1));
}

void Texture3DEditor::_notification(int p_what) {
	switch (p_what) {
		case NOTIFICATION_RESIZED: {
			_texture_rect_update_area();
		} break;

		case NOTIFICATION_DRAW: {
			Ref<Texture2D> checkerboard = get_editor_theme_icon(SNAME("Checkerboard"));
			Size2 size = get_size();

			draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
		} break;

		case NOTIFICATION_THEME_CHANGED: {
			if (info) {
				Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts));
				info->add_theme_font_override(SceneStringName(font), metadata_label_font);
			}
		} break;
	}
}

void Texture3DEditor::_texture_changed() {
	if (!is_visible()) {
		return;
	}

	setting = true;
	_update_gui();
	setting = false;

	_update_material(true);
	queue_redraw();
}

void Texture3DEditor::_update_material(bool p_texture_changed) {
	material->set_shader_parameter("layer", (layer->get_value() + 0.5) / texture->get_depth());

	if (p_texture_changed) {
		material->set_shader_parameter("tex", texture->get_rid());
	}
}

void Texture3DEditor::_make_shaders() {
	shader.instantiate();
	shader->set_code(texture_3d_shader);
Repository: godotengine/godot
File: scene/3d/navigation_agent_3d.cpp (C++)

Code before:
void NavigationAgent3D::set_debug_path_custom_point_size(float p_point_size) {
#ifdef DEBUG_ENABLED
	if (Math::is_equal_approx(debug_path_custom_point_size, p_point_size)) {
		return;
	}

	debug_path_custom_point_size = MAX(0.0, p_point_size);
	debug_path_dirty = true;
#endif // DEBUG_ENABLED
}

float NavigationAgent3D::get_debug_path_custom_point_size() const {
	return debug_path_custom_point_size;
}

#ifdef DEBUG_ENABLED
void NavigationAgent3D::_navigation_debug_changed() {
	debug_path_dirty = true;
}

void NavigationAgent3D::_update_debug_path() {
	if (!debug_path_dirty) {
		return;
	}
	debug_path_dirty = false;

	if (!debug_path_instance.is_valid()) {
		debug_path_instance = RenderingServer::get_singleton()->instance_create();
	}

	if (!debug_path_mesh.is_valid()) {
		debug_path_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
	}

	debug_path_mesh->clear_surfaces();

	if (!(debug_enabled && NavigationServer3D::get_singleton()->get_debug_navigation_enable_agent_paths())) {
		return;
	}

	if (!(agent_parent && agent_parent->is_inside_tree())) {
		return;
	}

	const Vector<Vector3> &navigation_path = navigation_result->get_path();

	if (navigation_path.size() <= 1) {
		return;
	}

	Vector<Vector3> debug_path_lines_vertex_array;

Review comment (bug):
```suggestion
	if (debug_path_mesh.is_null()) {
```

Code after:
void NavigationAgent3D::set_debug_path_custom_point_size(float p_point_size) {
#ifdef DEBUG_ENABLED
	if (Math::is_equal_approx(debug_path_custom_point_size, p_point_size)) {
		return;
	}

	debug_path_custom_point_size = MAX(0.0, p_point_size);
	debug_path_dirty = true;
#endif // DEBUG_ENABLED
}

float NavigationAgent3D::get_debug_path_custom_point_size() const {
	return debug_path_custom_point_size;
}

#ifdef DEBUG_ENABLED
void NavigationAgent3D::_navigation_debug_changed() {
	debug_path_dirty = true;
}

void NavigationAgent3D::_update_debug_path() {
	if (!debug_path_dirty) {
		return;
	}
	debug_path_dirty = false;

	if (!debug_path_instance.is_valid()) {
		debug_path_instance = RenderingServer::get_singleton()->instance_create();
	}

	if (debug_path_mesh.is_null()) {
		debug_path_mesh.instantiate();
	}

	debug_path_mesh->clear_surfaces();

	if (!(debug_enabled && NavigationServer3D::get_singleton()->get_debug_navigation_enable_agent_paths())) {
		return;
	}

	if (!(agent_parent && agent_parent->is_inside_tree())) {
		return;
	}

	const Vector<Vector3> &navigation_path = navigation_result->get_path();

	if (navigation_path.size() <= 1) {
		return;
	}

	Vector<Vector3> debug_path_lines_vertex_array;
Repository: godotengine/godot
File: editor/plugins/canvas_item_editor_plugin.cpp (C++)

Code before:
		}
	}

	if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true) && drag_type == DRAG_NONE) {
		// Unselect everything
		editor_selection->clear();
		viewport->queue_redraw();
	}
	return false;
}

bool CanvasItemEditor::_gui_input_ruler_tool(const Ref<InputEvent> &p_event) {
	if (tool != TOOL_RULER) {
		ruler_tool_active = false;
		return false;
	}

	Ref<InputEventMouseButton> b = p_event;
	Ref<InputEventMouseMotion> m = p_event;

	Point2 previous_origin = ruler_tool_origin;
	if (!ruler_tool_active) {
		ruler_tool_origin = snap_point(viewport->get_local_mouse_position() / zoom + view_offset);
	}

	if (b.is_valid() && b->get_button_index() == MouseButton::LEFT) {
		if (b->is_pressed()) {
			ruler_tool_active = true;
		} else {
			ruler_tool_active = false;
		}

		viewport->queue_redraw();
		return true;
	}

	if (m.is_valid() && (ruler_tool_active || (grid_snap_active && previous_origin != ruler_tool_origin))) {
		viewport->queue_redraw();
		return true;
	}

	return false;
}

bool CanvasItemEditor::_gui_input_hover(const Ref<InputEvent> &p_event) {
	Ref<InputEventMouseMotion> m = p_event;
	if (m.is_valid()) {
		Point2 click = transform.affine_inverse().xform(m->get_position());

		// Checks if the hovered items changed, redraw the viewport if so
		Vector<_SelectResult> hovering_results_items;

Review comment (suggestion):
```suggestion
	if (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && ruler_tool_active) {
```
No need for comparison with `bool`

Code after:
		}
	}

	if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true) && drag_type == DRAG_NONE) {
		// Unselect everything
		editor_selection->clear();
		viewport->queue_redraw();
	}
	return false;
}

bool CanvasItemEditor::_gui_input_ruler_tool(const Ref<InputEvent> &p_event) {
	if (tool != TOOL_RULER) {
		ruler_tool_active = false;
		return false;
	}

	Ref<InputEventMouseButton> b = p_event;
	Ref<InputEventMouseMotion> m = p_event;

	Point2 previous_origin = ruler_tool_origin;
	if (!ruler_tool_active) {
		ruler_tool_origin = snap_point(viewport->get_local_mouse_position() / zoom + view_offset);
	}

	if (ruler_tool_active && b.is_valid() && b->get_button_index() == MouseButton::RIGHT) {
		ruler_tool_active = false;
		viewport->queue_redraw();
		return true;
	}

	if (b.is_valid() && b->get_button_index() == MouseButton::LEFT) {
		if (b->is_pressed()) {
			ruler_tool_active = true;
		} else {
			ruler_tool_active = false;
		}

		viewport->queue_redraw();
		return true;
	}

	if (m.is_valid() && (ruler_tool_active || (grid_snap_active && previous_origin != ruler_tool_origin))) {
		viewport->queue_redraw();
		return true;
	}

	return false;
}

bool CanvasItemEditor::_gui_input_hover(const Ref<InputEvent> &p_event) {
Repository: godotengine/godot
File: editor/plugins/canvas_item_editor_plugin.cpp (C++)

Code before:
		}
	}

	if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true) && drag_type == DRAG_NONE) {
		// Unselect everything
		editor_selection->clear();
		viewport->queue_redraw();
	}
	return false;
}

bool CanvasItemEditor::_gui_input_ruler_tool(const Ref<InputEvent> &p_event) {
	if (tool != TOOL_RULER) {
		ruler_tool_active = false;
		return false;
	}

	Ref<InputEventMouseButton> b = p_event;
	Ref<InputEventMouseMotion> m = p_event;

	Point2 previous_origin = ruler_tool_origin;
	if (!ruler_tool_active) {
		ruler_tool_origin = snap_point(viewport->get_local_mouse_position() / zoom + view_offset);
	}

	if (b.is_valid() && b->get_button_index() == MouseButton::LEFT) {
		if (b->is_pressed()) {
			ruler_tool_active = true;
		} else {
			ruler_tool_active = false;
		}

		viewport->queue_redraw();
		return true;
	}

	if (m.is_valid() && (ruler_tool_active || (grid_snap_active && previous_origin != ruler_tool_origin))) {
		viewport->queue_redraw();
		return true;
	}

	return false;
}

bool CanvasItemEditor::_gui_input_hover(const Ref<InputEvent> &p_event) {
	Ref<InputEventMouseMotion> m = p_event;
	if (m.is_valid()) {
		Point2 click = transform.affine_inverse().xform(m->get_position());

		// Checks if the hovered items changed, redraw the viewport if so
		Vector<_SelectResult> hovering_results_items;

Review comment (suggestion):
```suggestion
	if (ruler_tool_active && b.is_valid() && b->get_button_index() == MouseButton::RIGHT) {
```
Cheaper checks first.

Code after:
		}
	}

	if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true) && drag_type == DRAG_NONE) {
		// Unselect everything
		editor_selection->clear();
		viewport->queue_redraw();
	}
	return false;
}

bool CanvasItemEditor::_gui_input_ruler_tool(const Ref<InputEvent> &p_event) {
	if (tool != TOOL_RULER) {
		ruler_tool_active = false;
		return false;
	}

	Ref<InputEventMouseButton> b = p_event;
	Ref<InputEventMouseMotion> m = p_event;

	Point2 previous_origin = ruler_tool_origin;
	if (!ruler_tool_active) {
		ruler_tool_origin = snap_point(viewport->get_local_mouse_position() / zoom + view_offset);
	}

	if (ruler_tool_active && b.is_valid() && b->get_button_index() == MouseButton::RIGHT) {
		ruler_tool_active = false;
		viewport->queue_redraw();
		return true;
	}

	if (b.is_valid() && b->get_button_index() == MouseButton::LEFT) {
		if (b->is_pressed()) {
			ruler_tool_active = true;
		} else {
			ruler_tool_active = false;
		}

		viewport->queue_redraw();
		return true;
	}

	if (m.is_valid() && (ruler_tool_active || (grid_snap_active && previous_origin != ruler_tool_origin))) {
		viewport->queue_redraw();
		return true;
	}

	return false;
}

bool CanvasItemEditor::_gui_input_hover(const Ref<InputEvent> &p_event) {