Cleaned up PaintVisualGrid.

This commit is contained in:
Relintai 2022-11-19 02:31:39 +01:00
parent d7ccaff144
commit 71a455687d
3 changed files with 41 additions and 69 deletions

View File

@ -602,7 +602,7 @@ PaintCanvasOld::PaintCanvasOld() {
add_child(tool_preview_layer_rect); add_child(tool_preview_layer_rect);
grid = memnew(PaintVisualGrid); grid = memnew(PaintVisualGrid);
grid->size = 4; grid->set_grid_size(4);
grid->set_mouse_filter(Control::MOUSE_FILTER_IGNORE); grid->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
add_child(grid); add_child(grid);

View File

@ -24,90 +24,59 @@ SOFTWARE.
#include "paint_visual_grid.h" #include "paint_visual_grid.h"
int PaintVisualGrid::get_grid_size() {
return _grid_size;
}
void PaintVisualGrid::set_grid_size(const int gs) { void PaintVisualGrid::set_grid_size(const int gs) {
size = gs; _grid_size = gs;
update();
}
Color PaintVisualGrid::get_grid_color() {
return _grid_color;
}
void PaintVisualGrid::set_grid_color(const Color &val) {
_grid_color = val;
update(); update();
} }
void PaintVisualGrid::_draw() { void PaintVisualGrid::_draw() {
if (size == 0) {
size = 1;
}
Size2 rect_size = get_size();
int sx = rect_size.x;
int sy = rect_size.y;
for (int x = 0; x < sx; x += size) {
draw_line(Vector2(x, 0), Vector2(x, sy), color, 1);
}
for (int y = 0; y < sy; y += size) {
draw_line(Vector2(0, y), Vector2(sx, y), color, 1);
}
/*
float temp_size = size + zoom;
Vector2 wrap_offset = Vector2(Math::wrapf(offset.x, 0, temp_size), Math::wrapf(offset.y, 0, temp_size));
Size2 rect_size = get_size();
float ceil_x = Math::ceil(rect_size.x / temp_size);
float ceil_y = Math::ceil(rect_size.y / temp_size);
for (int i = 0; i < ceil_y; ++i) {
Point2 start_x = Vector2(0, (i * temp_size) + wrap_offset.y);
Point2 end_x = Vector2(rect_size.x, (i * temp_size) + wrap_offset.y);
//var end_x = Vector2(int(rect_size.x) + size - int(rect_size.x) % size, (i * temp_size) + wrap_offset.y);
draw_line(start_x, end_x, color, 1);
}
for (int i = 0; i < ceil_x; ++i) {
Point2 start_y = Vector2((i * temp_size) + wrap_offset.x, 0);
Point2 end_y = Vector2((i * temp_size) + (wrap_offset.x), rect_size.y);
//var end_y = Vector2((i * temp_size) + (wrap_offset.x), int(rect_size.y) + size - int(rect_size.y) % size);
draw_line(start_y, end_y, color, 1);
}
*/
} }
void PaintVisualGrid::_notification(int p_what) { void PaintVisualGrid::_notification(int p_what) {
/*
func _enter_tree():
set_process(true)
func _process(delta):
if not is_visible_in_tree():
return
update()
*/
switch (p_what) { switch (p_what) {
/* case NOTIFICATION_DRAW: {
case NOTIFICATION_PROCESS: { ERR_FAIL_COND(_grid_size <= 0);
if (!is_visible_in_tree()) {
return; Size2 rect_size = get_size();
int sx = rect_size.x;
int sy = rect_size.y;
for (int x = 0; x < sx; x += _grid_size) {
draw_line(Vector2(x, 0), Vector2(x, sy), _grid_color, 1);
} }
update(); for (int y = 0; y < sy; y += _grid_size) {
} break; draw_line(Vector2(0, y), Vector2(sx, y), _grid_color, 1);
*/ }
case NOTIFICATION_DRAW: {
_draw();
} break; } break;
} }
} }
PaintVisualGrid::PaintVisualGrid() { PaintVisualGrid::PaintVisualGrid() {
color = Color(1, 1, 1, 0.42); _grid_color = Color(1, 1, 1, 0.42);
size = 16; _grid_size = 2;
//zoom = 0;
} }
PaintVisualGrid::~PaintVisualGrid() { PaintVisualGrid::~PaintVisualGrid() {
} }
void PaintVisualGrid::_bind_methods() { void PaintVisualGrid::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_grid_size"), &PaintVisualGrid::get_grid_size);
ClassDB::bind_method(D_METHOD("set_grid_size", "size"), &PaintVisualGrid::set_grid_size); ClassDB::bind_method(D_METHOD("set_grid_size", "size"), &PaintVisualGrid::set_grid_size);
ADD_PROPERTY(PropertyInfo(Variant::INT, "grid_size"), "set_grid_size", "get_grid_size");
ClassDB::bind_method(D_METHOD("get_grid_color"), &PaintVisualGrid::get_grid_color);
ClassDB::bind_method(D_METHOD("set_grid_color", "color"), &PaintVisualGrid::set_grid_color);
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "grid_color"), "set_grid_color", "get_grid_color");
} }

View File

@ -31,21 +31,24 @@ class PaintVisualGrid : public Control {
GDCLASS(PaintVisualGrid, Control); GDCLASS(PaintVisualGrid, Control);
public: public:
int get_grid_size();
void set_grid_size(const int gs); void set_grid_size(const int gs);
Color get_grid_color();
void set_grid_color(const Color &val);
void _draw(); void _draw();
PaintVisualGrid(); PaintVisualGrid();
~PaintVisualGrid(); ~PaintVisualGrid();
Color color;
int size;
//float zoom;
//Vector2 offset;
protected: protected:
void _notification(int p_what); void _notification(int p_what);
static void _bind_methods(); static void _bind_methods();
Color _grid_color;
int _grid_size;
}; };
#endif #endif