From d98f9746e89ed79337f10ff0ca05c950eb609a84 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 16 Apr 2022 01:25:02 +0200 Subject: [PATCH] Ported the logic for quite a few classes of the paint module. --- modules/paint/SCsub | 12 ++ modules/paint/actions/paint_action.cpp | 131 ++++++++--------- modules/paint/actions/paint_action.h | 15 +- modules/paint/paint_canvas_layer.cpp | 161 +++++++++++---------- modules/paint/paint_canvas_layer.h | 37 ++--- modules/paint/paint_canvas_outline.cpp | 58 +++++--- modules/paint/paint_canvas_outline.h | 4 +- modules/paint/paint_color_grid.cpp | 50 +++---- modules/paint/paint_color_grid.h | 5 +- modules/paint/paint_layer_button.cpp | 189 +++++++++++-------------- modules/paint/paint_layer_button.h | 10 ++ modules/paint/paint_utilities.cpp | 155 ++++++++++---------- modules/paint/paint_utilities.h | 14 +- 13 files changed, 428 insertions(+), 413 deletions(-) diff --git a/modules/paint/SCsub b/modules/paint/SCsub index d3cf7c94b..a76a30190 100644 --- a/modules/paint/SCsub +++ b/modules/paint/SCsub @@ -6,6 +6,18 @@ module_env = env.Clone() module_env.add_source_files(env.modules_sources,"register_types.cpp") +module_env.add_source_files(env.modules_sources,"paint_utilities.cpp") + +module_env.add_source_files(env.modules_sources,"actions/paint_action.cpp") + + +module_env.add_source_files(env.modules_sources,"paint_canvas_layer.cpp") + +module_env.add_source_files(env.modules_sources,"paint_canvas_outline.cpp") +module_env.add_source_files(env.modules_sources,"paint_color_grid.cpp") +module_env.add_source_files(env.modules_sources,"paint_layer_button.cpp") + + #module_env.add_source_files(env.modules_sources,"plugin/mdr_import_plugin_base.cpp") #if 'TOOLS_ENABLED' in env["CPPDEFINES"]: diff --git a/modules/paint/actions/paint_action.cpp b/modules/paint/actions/paint_action.cpp index 6badcc031..f42de09be 100644 --- a/modules/paint/actions/paint_action.cpp +++ b/modules/paint/actions/paint_action.cpp @@ -25,92 +25,86 @@ SOFTWARE. #include "paint_action.h" void PaintAction::do_action(PaintCanvas *canvas, Array data) { - /* - if not "cells" in action_data.redo: - action_data.redo["cells"] = [] - action_data.redo["colors"] = [] + if (!action_data_redo.has("cells")) { + action_data_redo["cells"] = Array(); + action_data_redo["colors"] = Array(); + } - if not "cells" in action_data.undo: - action_data.undo["cells"] = [] - action_data.undo["colors"] = [] + if (!action_data_undo.has("cells")) { + action_data_undo["cells"] = Array(); + action_data_undo["colors"] = Array(); + } - if not "cells" in action_data.preview: - action_data.preview["cells"] = [] - action_data.preview["colors"] = [] + if (!action_data_preview.has("cells")) { + action_data_preview["cells"] = Array(); + action_data_preview["colors"] = Array(); + } - if not "layer" in action_data: - action_data["layer"] = canvas.active_layer - */ + if (!action_data.has("layer")) { + //action_data["layer"] = canvas->get_active_layer(); + } } void PaintAction::commit_action(PaintCanvas *canvas) { - /* - print("NO IMPL commit_action ") - return [] - */ + ERR_PRINT("NO IMPL commit_action"); } void PaintAction::undo_action(PaintCanvas *canvas) { - /* - print("NO IMPL undo_action ") - */ + ERR_PRINT("NO IMPL undo_action"); } void PaintAction::redo_action(PaintCanvas *canvas) { - /* - print("NO IMPL redo_action ") - */ + ERR_PRINT("NO IMPL redo_action"); } bool PaintAction::can_commit() { - /* - return not action_data.redo.empty() - */ + return !action_data_redo.empty(); } -Array PaintAction::get_x_sym_points(int canvas_width, Vector2i pixel) { - /* - var p = int(canvas_width - pixel.x) - var all_points = [pixel, Vector2(p-1, pixel.y)] +PoolVector2iArray PaintAction::get_x_sym_points(int canvas_width, Vector2i pixel) { + int p = canvas_width - pixel.x; - var points :Array = [] - for point in all_points: - if point in points: - continue - points.append(point) - return points - */ + PoolVector2iArray points; + points.append(pixel); + points.append(Vector2i(p - 1, pixel.y)); + + return points; } -Array PaintAction::get_y_sym_points(int canvas_height, Vector2i pixel) { - /* - var p = int(canvas_height - pixel.y) - var all_points = [pixel, Vector2(pixel.x, p-1)] +PoolVector2iArray PaintAction::get_y_sym_points(int canvas_height, Vector2i pixel) { + int p = canvas_height - pixel.y; - var points :Array = [] - for point in all_points: - if point in points: - continue - points.append(point) - return points - */ + PoolVector2iArray points; + points.append(pixel); + points.append(Vector2i(pixel.x, p - 1)); + + return points; } -Array PaintAction::get_xy_sym_points(int canvas_width, int canvas_height, Vector2i pixel) { - /* - var all_points = [] - var xpoints = get_x_sym_points(canvas_width, pixel) +PoolVector2iArray PaintAction::get_xy_sym_points(int canvas_width, int canvas_height, Vector2i pixel) { + PoolVector2iArray all_points; + PoolVector2iArray xpoints = get_x_sym_points(canvas_width, pixel); - all_points += get_y_sym_points(canvas_height, xpoints[0]) - all_points += get_y_sym_points(canvas_height, xpoints[1]) + all_points.append_array(get_y_sym_points(canvas_height, xpoints[0])); + all_points.append_array(get_y_sym_points(canvas_height, xpoints[1])); - var points :Array = [] - for point in all_points: - if point in points: - continue - points.append(point) + PoolVector2iArray points; + for (int i = 0; i < all_points.size(); ++i) { //point in all_points: + Vector2i point = all_points[i]; + bool found = false; - return points - */ + for (int j = 0; j < points.size(); ++j) { + if (points[j] == point) { + found = true; + break; + } + } + + if (!found) { + points.append(point); + } + } + + return points; } -Array PaintAction::get_points(PaintCanvas *canvas, Vector2i pixel) { +PoolVector2iArray PaintAction::get_points(PaintCanvas *canvas, Vector2i pixel) { /* - var points = [] + PoolVector2iArray points; if canvas.symmetry_x and canvas.symmetry_y: var sym_points = get_xy_sym_points(canvas.canvas_width, canvas.canvas_height, pixel) for point in sym_points: @@ -142,18 +136,13 @@ Array PaintAction::get_points(PaintCanvas *canvas, Vector2i pixel) { return [] points.append(pixel) - return points + return points; */ + + return PoolVector2iArray(); } PaintAction::PaintAction() { - /* - - action_data["redo"] = {} - action_data["undo"] = {} - action_data["preview"] = {} - - */ } PaintAction::~PaintAction() { diff --git a/modules/paint/actions/paint_action.h b/modules/paint/actions/paint_action.h index 3b74bcd3a..4e62595dc 100644 --- a/modules/paint/actions/paint_action.h +++ b/modules/paint/actions/paint_action.h @@ -29,6 +29,7 @@ SOFTWARE. #include "core/pool_vector.h" #include "core/variant.h" +#include "core/dictionary.h" #include "core/array.h" #include "core/math/vector2i.h" @@ -47,15 +48,19 @@ public: virtual void redo_action(PaintCanvas *canvas); virtual bool can_commit(); - virtual Array get_x_sym_points(int canvas_width, Vector2i pixel); - virtual Array get_y_sym_points(int canvas_height, Vector2i pixel); - virtual Array get_xy_sym_points(int canvas_width, int canvas_height, Vector2i pixel); - virtual Array get_points(PaintCanvas *canvas, Vector2i pixel); + virtual PoolVector2iArray get_x_sym_points(int canvas_width, Vector2i pixel); + virtual PoolVector2iArray get_y_sym_points(int canvas_height, Vector2i pixel); + virtual PoolVector2iArray get_xy_sym_points(int canvas_width, int canvas_height, Vector2i pixel); + virtual PoolVector2iArray get_points(PaintCanvas *canvas, Vector2i pixel); PaintAction(); ~PaintAction(); - //var action_data = {} + Dictionary action_data_undo; + Dictionary action_data_redo; + Dictionary action_data_preview; + + Dictionary action_data; protected: static void _bind_methods(); diff --git a/modules/paint/paint_canvas_layer.cpp b/modules/paint/paint_canvas_layer.cpp index 0ff356409..5e29b7bd4 100644 --- a/modules/paint/paint_canvas_layer.cpp +++ b/modules/paint/paint_canvas_layer.cpp @@ -24,114 +24,113 @@ SOFTWARE. #include "paint_canvas_layer.h" -void PaintCanvasLayer::_init() { - /* - texture = ImageTexture.new() - */ +#include "paint_utilities.h" +#include "scene/gui/texture_rect.h" + +bool PaintCanvasLayer::get_visible() { + return _visible; +} +void PaintCanvasLayer::set_visible(bool vis) { + _visible = vis; + texture_rect_ref->set_visible(_visible); } -void PaintCanvasLayer::create(Node *texture_rect_ref, int width, int height) { - /* - self.texture_rect_ref = texture_rect_ref - layer_width = width - layer_height = height +void PaintCanvasLayer::create(TextureRect *p_texture_rect_ref, int width, int height) { + texture_rect_ref = p_texture_rect_ref; - image = Image.new() - image.create(width, height, false, Image.FORMAT_RGBA8) - image.fill(Color.transparent) - update_texture() - */ + layer_width = width; + layer_height = height; + + image.instance(); + image->create(width, height, false, Image::FORMAT_RGBA8); + //Color.transparent + image->fill(Color(1.00, 1.00, 1.00, 0.00)); + update_texture(); } void PaintCanvasLayer::resize(int width, int height) { - /* - var pixel_colors = [] - var prev_width = layer_width - var prev_height = layer_height + PoolColorArray pixel_colors; + int prev_width = layer_width; + int prev_height = layer_height; - image.lock() - for y in range(prev_height): - for x in range(prev_width): - pixel_colors.append(image.get_pixel(x, y)) - image.unlock() + image->lock(); - layer_width = width - layer_height = height + for (int y = 0; y < prev_height; ++y) { + for (int x = 0; x < prev_width; ++x) { + pixel_colors.append(image->get_pixel(x, y)); + } + } - image.create(width, height, false, Image.FORMAT_RGBA8) + image->unlock(); - image.lock() - for x in range(prev_width): - for y in range(prev_height): - if x >= width or y >= height: - continue - image.set_pixel(x, y, pixel_colors[GEUtils.to_1D(x, y, prev_width)]) - image.unlock() + layer_width = width; + layer_height = height; - update_texture() - */ + image->create(width, height, false, Image::FORMAT_RGBA8); + + image->lock(); + + for (int x = 0; x < prev_width; ++x) { + for (int y = 0; y < prev_height; ++y) { + if (x >= width or y >= height) { + continue; + } + + image->set_pixel(x, y, pixel_colors[PaintUtilities::to_1D(x, y, prev_width)]); + } + } + + image->unlock(); + + update_texture(); } void PaintCanvasLayer::set_pixel(int x, int y, Color color) { - /* - image.lock() - image.set_pixel(x, y, color) - image.unlock() - */ + image->lock(); + image->set_pixel(x, y, color); + image->unlock(); } Color PaintCanvasLayer::get_pixel(int x, int y) { - /* - if x < 0 or y < 0 or x >= image.get_width() or y >= image.get_height(): - return null - image.lock() - var pixel = image.get_pixel(x, y) - image.unlock() - return pixel - */ + if (x < 0 || y < 0 || x >= image->get_width() || y >= image->get_height()) { + return Color(); + } + + image->lock(); + Color pixel = image->get_pixel(x, y); + image->unlock(); + + return pixel; } void PaintCanvasLayer::clear() { - /* - image.fill(Color.transparent) - update_texture() - */ + //Color.transparent + image->fill(Color(1.00, 1.00, 1.00, 0.00)); + + update_texture(); } void PaintCanvasLayer::update_texture() { - /* - texture.create_from_image(image, 0) - texture_rect_ref.texture = texture - texture_rect_ref.margin_right = 0 - texture_rect_ref.margin_bottom = 0 - */ -} -void PaintCanvasLayer::set_visible(bool vis) { - /* - visible = vis - texture_rect_ref.visible = visible - */ + texture->create_from_image(image, 0); + + texture_rect_ref->set_texture(texture); + texture_rect_ref->set_margin(Margin::MARGIN_RIGHT, 0); + texture_rect_ref->set_margin(Margin::MARGIN_BOTTOM, 0); } + void PaintCanvasLayer::toggle_lock() { - /* - locked = not locked - */ + locked = !locked; } void PaintCanvasLayer::toggle_alpha_locked() { - /* - alpha_locked = not alpha_locked - */ + alpha_locked = !alpha_locked; } PaintCanvasLayer::PaintCanvasLayer() { - /* - var name - var layer_width - var layer_height - var visible = true setget set_visible - var locked = false - var alpha_locked = false + layer_width = 0; + layer_height = 0; + _visible = true; + locked = false; + alpha_locked = false; - var texture: ImageTexture - var image: Image - var texture_rect_ref - */ + texture_rect_ref = nullptr; + + texture.instance(); } PaintCanvasLayer::~PaintCanvasLayer() { diff --git a/modules/paint/paint_canvas_layer.h b/modules/paint/paint_canvas_layer.h index 178078b8a..23e5ee10e 100644 --- a/modules/paint/paint_canvas_layer.h +++ b/modules/paint/paint_canvas_layer.h @@ -27,22 +27,38 @@ SOFTWARE. #include "core/reference.h" -class Node; +#include "core/image.h" +#include "scene/resources/texture.h" + +class TextureRect; //class_name GELayer class PaintCanvasLayer : public Reference { GDCLASS(PaintCanvasLayer, Reference); public: - void _init(); - void create(Node *texture_rect_ref, int width, int height); + String name; + int layer_width; + int layer_height; + + bool locked = false; + bool alpha_locked = false; + + Ref texture; + Ref image; + TextureRect *texture_rect_ref; + + bool get_visible(); + void set_visible(bool vis); + + void create(TextureRect *p_texture_rect_ref, int width, int height); void resize(int width, int height); void set_pixel(int x, int y, Color color); Color get_pixel(int x, int y); void clear(); void update_texture(); - void set_visible(bool vis); + void toggle_lock(); void toggle_alpha_locked(); @@ -52,18 +68,7 @@ public: protected: static void _bind_methods(); - /* - var name - var layer_width - var layer_height - var visible = true setget set_visible - var locked = false - var alpha_locked = false - - var texture: ImageTexture - var image: Image - var texture_rect_ref - */ + bool _visible; }; #endif diff --git a/modules/paint/paint_canvas_outline.cpp b/modules/paint/paint_canvas_outline.cpp index 99ac80c14..e0f35afe1 100644 --- a/modules/paint/paint_canvas_outline.cpp +++ b/modules/paint/paint_canvas_outline.cpp @@ -24,33 +24,45 @@ SOFTWARE. #include "paint_canvas_outline.h" -void PaintCanvasOutline::_draw() { -/* - var size = get_parent().rect_size - var pos = Vector2.ZERO //get_parent().rect_global_position - draw_outline_box(pos, size, color, 1) -*/ -} void PaintCanvasOutline::draw_outline_box(Vector2 pos, Vector2 size, Color color, int width) { -/* - //Top line - draw_line(pos, pos + Vector2(size.x, 0), color, width) - //Left line - draw_line(pos, pos + Vector2(0, size.y), color, width) - //Bottom line - draw_line(pos + Vector2(0, size.y), pos + Vector2(size.x, size.y), color, width) - //Right line - draw_line(pos + Vector2(size.x, 0), pos + Vector2(size.x, size.y), color, width) -*/ + //Top line + draw_line(pos, pos + Vector2(size.x, 0), color, width); + //Left line + draw_line(pos, pos + Vector2(0, size.y), color, width); + //Bottom line + draw_line(pos + Vector2(0, size.y), pos + Vector2(size.x, size.y), color, width); + //Right line + draw_line(pos + Vector2(size.x, 0), pos + Vector2(size.x, size.y), color, width); +} + +void PaintCanvasOutline::_notification(int p_what) { + switch(p_what) { + /* + case NOTIFICATION_PROCESS: { + if (!is_visible_in_tree()) { + return; + } + + update(); + } break; + */ + case NOTIFICATION_DRAW: { + //Control *pc = get_parent_control(); + + //if (!pc) { + // return; + //} + + //Vector2 size = pc.rect_size; + + //var pos = Vector2(); //get_parent().rect_global_position + draw_outline_box(Vector2(), get_size(), color, 1); + } break; + } } -/* -func _process(delta): - if not is_visible_in_tree(): - return - update() -*/ PaintCanvasOutline::PaintCanvasOutline() { + color = Color(0, 1, 0, 1); } PaintCanvasOutline::~PaintCanvasOutline() { diff --git a/modules/paint/paint_canvas_outline.h b/modules/paint/paint_canvas_outline.h index ca643114d..3309dc226 100644 --- a/modules/paint/paint_canvas_outline.h +++ b/modules/paint/paint_canvas_outline.h @@ -31,16 +31,16 @@ class PaintCanvasOutline : public Control { GDCLASS(PaintCanvasOutline, Control); public: - void _draw(); void draw_outline_box(Vector2 pos, Vector2 size, Color color, int width); PaintCanvasOutline(); ~PaintCanvasOutline(); protected: + void _notification(int p_what); static void _bind_methods(); - //export var color = Color() + Color color; }; #endif diff --git a/modules/paint/paint_color_grid.cpp b/modules/paint/paint_color_grid.cpp index 355bec0a2..aeb583506 100644 --- a/modules/paint/paint_color_grid.cpp +++ b/modules/paint/paint_color_grid.cpp @@ -24,37 +24,33 @@ SOFTWARE. #include "paint_color_grid.h" -void PaintColorGrid::_enter_tree() { - /* - for child in get_children(): - child.set("custom_styles/normal", StyleBoxFlat.new()) - child.get("custom_styles/normal").set("bg_color", Color(randf(), randf(), randf())) - for child in get_children(): - if child.is_connected("pressed", self, "change_color_to"): - return - child.connect("pressed", self, "change_color_to", [child.get("custom_styles/normal").bg_color]) - */ +#include "scene/gui/button.h" +#include "scene/resources/style_box.h" + +void PaintColorGrid::change_color_to(const Color &color) { + emit_signal("color_change_request", color); } -void PaintColorGrid::change_color_to(Color color) { - /* - emit_signal("color_change_request", color) - */ -} -void PaintColorGrid::add_color_prefab(Color color) { - /* - var dup = get_child(0).duplicate() - add_child(dup) - move_child(dup, 0) - dup.set("custom_styles/normal", StyleBoxFlat.new()) - dup.get("custom_styles/normal").set("bg_color", color) - for child in get_children(): - if child.is_connected("pressed", self, "change_color_to"): - return - child.connect("pressed", self, "change_color_to", [child.get("custom_styles/normal").bg_color]) - */ +void PaintColorGrid::add_color_prefab(const Color &color) { + Button *button = memnew(Button); + add_child(button); + move_child(button, 0); + + Ref style_box; + style_box.instance(); + style_box->set("bg_color", color); + + button->set("custom_styles/normal", style_box); + + Vector binds; + binds.push_back(color); + + button->connect("pressed", this, "change_color_to", binds); } PaintColorGrid::PaintColorGrid() { + for (int i = 0; i < 24; ++i) { + add_color_prefab(Color(Math::randf(), Math::randf(), Math::randf())); + } } PaintColorGrid::~PaintColorGrid() { diff --git a/modules/paint/paint_color_grid.h b/modules/paint/paint_color_grid.h index a60b8eebe..bc78f1179 100644 --- a/modules/paint/paint_color_grid.h +++ b/modules/paint/paint_color_grid.h @@ -31,9 +31,8 @@ class PaintColorGrid : public GridContainer { GDCLASS(PaintColorGrid, GridContainer); public: - void _enter_tree(); - void change_color_to(Color color); - void add_color_prefab(Color color); + void change_color_to(const Color &color); + void add_color_prefab(const Color &color); PaintColorGrid(); ~PaintColorGrid(); diff --git a/modules/paint/paint_layer_button.cpp b/modules/paint/paint_layer_button.cpp index 485af0db1..7b45a3c04 100644 --- a/modules/paint/paint_layer_button.cpp +++ b/modules/paint/paint_layer_button.cpp @@ -24,121 +24,102 @@ SOFTWARE. #include "paint_layer_button.h" +#include "scene/gui/box_container.h" +#include "scene/gui/button.h" +#include "scene/gui/check_button.h" +#include "scene/gui/margin_container.h" +#include "scene/gui/texture_button.h" +#include "scene/resources/style_box.h" + PaintLayerButton::PaintLayerButton() { - /* -[gd_scene load_steps=11 format=2] + set_custom_minimum_size(Size2(0, 32)); + set_mouse_filter(Control::MOUSE_FILTER_IGNORE); -[ext_resource path="res://addons/Godoxel/assets/minidotta_invis.png" type="Texture" id=1] -[ext_resource path="res://addons/Godoxel/assets/minidotta.png" type="Texture" id=2] -[ext_resource path="res://addons/Godoxel/assets/arrow_down.png" type="Texture" id=3] -[ext_resource path="res://addons/Godoxel/assets/arrow_up.png" type="Texture" id=4] -[ext_resource path="res://addons/Godoxel/assets/lock_layer_1.png" type="Texture" id=5] -[ext_resource path="res://addons/Godoxel/assets/unlock_layer.png" type="Texture" id=6] + Ref style_box; + style_box.instance(); + style_box->set("bg_color", Color(0.35, 0.5, 0.77)); + set("custom_styles/panel", style_box); + HBoxContainer *main_box_container = memnew(HBoxContainer); + add_child(main_box_container); -[sub_resource type="StyleBoxFlat" id=4] -bg_color = Color( 0.180392, 0.176471, 0.176471, 1 ) + MarginContainer *left_main_container = memnew(MarginContainer); + left_main_container->set("custom_constants/margin_right", 2); + left_main_container->set("custom_constants/margin_top", 2); + left_main_container->set("custom_constants/margin_left", 2); + left_main_container->set("custom_constants/margin_bottom", 2); + left_main_container->set_h_size_flags(SIZE_EXPAND_FILL); + left_main_container->set_v_size_flags(SIZE_EXPAND_FILL); + main_box_container->add_child(left_main_container); -[sub_resource type="StyleBoxFlat" id=1] -bg_color = Color( 0.25098, 0.25098, 0.25098, 0 ) + // Layer Button + layer_button = memnew(Button); + layer_button->set_text("Layer 1"); + layer_button->set_text_align(Button::ALIGN_RIGHT); + layer_button->set_h_size_flags(SIZE_EXPAND_FILL); + layer_button->set_v_size_flags(SIZE_EXPAND_FILL); + style_box.instance(); + style_box->set("bg_color", Color(0.25, 0.25, 0.25)); + layer_button->set("custom_styles/hover", style_box); + style_box.instance(); + style_box->set("bg_color", Color(0.25, 0.25, 0.25)); + layer_button->set("custom_styles/pressed", style_box); + style_box.instance(); + style_box->set("bg_color", Color(0.25, 0.25, 0.25)); + layer_button->set("custom_styles/focus", style_box); + style_box.instance(); + style_box->set("bg_color", Color(0.25, 0.25, 0.25)); + layer_button->set("custom_styles/disabled", style_box); + style_box.instance(); + style_box->set("bg_color", Color(0.25, 0.25, 0.25)); + layer_button->set("custom_styles/normal", style_box); + left_main_container->add_child(layer_button); -[sub_resource type="StyleBoxFlat" id=2] -bg_color = Color( 0.6, 0.6, 0.6, 0 ) + HBoxContainer *check_container = memnew(HBoxContainer); + check_container->set_mouse_filter(Control::MOUSE_FILTER_IGNORE); + left_main_container->add_child(check_container); -[sub_resource type="StyleBoxFlat" id=3] -bg_color = Color( 0.6, 0.6, 0.6, 0 ) + // Visible Button + visible_button = memnew(CheckButton); + visible_button->set_pressed(true); + //visible_button->set("custom_icons/off", ); //res://addons/Godoxel/assets/minidotta_invis.png + //visible_button->set("custom_icons/on", ); //res://addons/Godoxel/assets/minidotta.png + style_box.instance(); + style_box->set("bg_color", Color(0.6, 0.6, 0.6)); + layer_button->set("custom_styles/normal", style_box); + check_container->add_child(visible_button); -[node name="Layer1" type="Panel"] -show_behind_parent = true -anchor_right = 0.113281 -anchor_bottom = 0.0416667 -margin_bottom = -1.90735e-06 -rect_min_size = Vector2( 0, 32 ) -mouse_filter = 2 -custom_styles/panel = SubResource( 4 ) -__meta__ = { -"_edit_use_anchors_": true -} + // Lock Button + lock_button = memnew(CheckButton); + lock_button->set_pressed(false); + //lock_button->set("custom_icons/off", ); //res://addons/Godoxel/assets/unlock_layer.png + //lock_button->set("custom_icons/on", ); //res://addons/Godoxel/assets/lock_layer_1.png + style_box.instance(); + style_box->set("bg_color", Color(0.6, 0.6, 0.6)); + layer_button->set("custom_styles/normal", style_box); + check_container->add_child(lock_button); -[node name="Select" type="Button" parent="." groups=[ -"layer_button", -]] -anchor_right = 0.827586 -anchor_bottom = 1.0 -custom_styles/hover = SubResource( 1 ) -custom_styles/pressed = SubResource( 1 ) -custom_styles/focus = SubResource( 1 ) -custom_styles/disabled = SubResource( 1 ) -custom_styles/normal = SubResource( 1 ) -text = "Layer 1" -align = 2 -__meta__ = { -"_edit_use_anchors_": true -} + // Right side + VBoxContainer *right_main_container = memnew(VBoxContainer); + main_box_container->add_child(right_main_container); -[node name="Visible" type="CheckButton" parent="."] -anchor_top = 0.5 -anchor_bottom = 0.5 -margin_left = 3.0 -margin_top = -8.5 -margin_right = 19.0 -margin_bottom = 7.5 -custom_icons/off = ExtResource( 1 ) -custom_icons/on = ExtResource( 2 ) -custom_styles/normal = SubResource( 2 ) -pressed = true -__meta__ = { -"_edit_use_anchors_": false -} + up_button = memnew(TextureButton); + up_button->set_expand(true); + up_button->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED); + up_button->set_h_size_flags(SIZE_EXPAND_FILL); + up_button->set_v_size_flags(SIZE_EXPAND_FILL); + //up_button->set_normal_texture(); //res://addons/Godoxel/assets/arrow_up.png + //up_button->set_pressed_texture(); //res://addons/Godoxel/assets/minidotta.png + right_main_container->add_child(up_button); -[node name="Lock" type="CheckButton" parent="."] -anchor_top = 0.5 -anchor_bottom = 0.5 -margin_left = 22.0 -margin_top = -11.0 -margin_right = 46.0 -margin_bottom = 11.0 -custom_icons/off = ExtResource( 6 ) -custom_icons/on = ExtResource( 5 ) -custom_styles/normal = SubResource( 3 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="VBoxContainer" type="VBoxContainer" parent="."] -anchor_left = 1.0 -anchor_right = 1.0 -anchor_bottom = 1.0 -margin_left = -20.0 -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="Up" type="TextureButton" parent="VBoxContainer"] -margin_right = 20.0 -margin_bottom = 14.0 -rect_min_size = Vector2( 20, 0 ) -size_flags_horizontal = 3 -size_flags_vertical = 3 -texture_normal = ExtResource( 4 ) -texture_pressed = ExtResource( 2 ) -expand = true -stretch_mode = 3 - -[node name="Down" type="TextureButton" parent="VBoxContainer"] -margin_top = 18.0 -margin_right = 20.0 -margin_bottom = 32.0 -rect_min_size = Vector2( 20, 0 ) -size_flags_horizontal = 3 -size_flags_vertical = 3 -texture_normal = ExtResource( 3 ) -texture_pressed = ExtResource( 2 ) -expand = true -stretch_mode = 3 - - - */ + down_button = memnew(TextureButton); + down_button->set_expand(true); + down_button->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED); + down_button->set_h_size_flags(SIZE_EXPAND_FILL); + down_button->set_v_size_flags(SIZE_EXPAND_FILL); + //down_button->set_normal_texture(); //res://addons/Godoxel/assets/arrow_up.png + //down_button->set_pressed_texture(); //res://addons/Godoxel/assets/minidotta.png + right_main_container->add_child(down_button); } PaintLayerButton::~PaintLayerButton() { diff --git a/modules/paint/paint_layer_button.h b/modules/paint/paint_layer_button.h index 161623ed0..9cf5611ca 100644 --- a/modules/paint/paint_layer_button.h +++ b/modules/paint/paint_layer_button.h @@ -27,6 +27,10 @@ SOFTWARE. #include "scene/gui/panel_container.h" +class Button; +class CheckButton; +class TextureButton; + class PaintLayerButton : public PanelContainer { GDCLASS(PaintLayerButton, PanelContainer); @@ -34,6 +38,12 @@ public: PaintLayerButton(); ~PaintLayerButton(); + Button *layer_button; + CheckButton *visible_button; + CheckButton *lock_button; + TextureButton *up_button; + TextureButton *down_button; + protected: static void _bind_methods(); }; diff --git a/modules/paint/paint_utilities.cpp b/modules/paint/paint_utilities.cpp index 4c53e6910..7175446eb 100644 --- a/modules/paint/paint_utilities.cpp +++ b/modules/paint/paint_utilities.cpp @@ -24,98 +24,103 @@ SOFTWARE. #include "paint_utilities.h" -Array PaintUtilities::PaintUtilities::get_pixels_in_line(Vector2 from, Vector2 to) { - /* - var dx = to[0] - from[0] - var dy = to[1] - from[1] - var nx = abs(dx) - var ny = abs(dy) - var signX = sign(dx) - var signY = sign(dy) - var p = from - var points : Array = [p] +PoolVector2iArray PaintUtilities::PaintUtilities::get_pixels_in_line(const Vector2i &from, const Vector2i &to) { + int dx = to[0] - from[0]; + int dy = to[1] - from[1]; + int nx = abs(dx); + int ny = abs(dy); + int signX = SGN(dx); + int signY = SGN(dy); - var ix = 0 - var iy = 0 + Vector2i p = from; - while ix < nx || iy < ny: - if (1 + (ix << 1)) * ny < (1 + (iy << 1)) * nx: - p[0] += signX - ix +=1 - else: - p[1] += signY - iy += 1 - points.append(p) - return points - */ + PoolVector2iArray points; + points.append(p); + + int ix = 0; + int iy = 0; + + while (ix < nx || iy < ny) { + if ((1 + (ix << 1)) * ny < (1 + (iy << 1)) * nx) { + p[0] += signX; + ix += 1; + } else { + p[1] += signY; + iy += 1; + } + + points.append(p); + } + + return points; } -int to_1D_v(Vector2 p, float w) { - /* - return p.x + p.y * w - */ -} -int PaintUtilities::to_1D(float x, float y, float w) { - /* - return x + y * w - */ -} -Vector2 PaintUtilities::to_2D(int idx, float w) { - /* - var p = Vector2() - p.x = int(idx) % int(w) - p.y = int(idx / w) - return p - */ +int PaintUtilities::to_1D_v(const Vector2i &p, int w) { + return p.x + p.y * w; } -Color PaintUtilities::color_from_array(PoolRealArray color_array) { - /* - var r = color_array[0] - var g = color_array[1] - var b = color_array[2] - var a = color_array[3] - return Color(r, g, b, a) - */ +int PaintUtilities::to_1D(int x, int y, int w) { + return x + y * w; } + +Vector2i PaintUtilities::to_2D(int idx, int w) { + Vector2i p; + + p.x = idx % w; + p.y = idx / w; + + return p; +} + +Color PaintUtilities::color_from_array(const PoolRealArray &color_array) { + float r = color_array[0]; + float g = color_array[1]; + float b = color_array[2]; + float a = color_array[3]; + + return Color(r, g, b, a); +} + Color PaintUtilities::random_color() { - /* - return Color(randf(), randf(), randf()) - */ + return Color(Math::randf(), Math::randf(), Math::randf()); } + Color PaintUtilities::random_color_alt() { - /* - var rand = randi() % 6 + int rand = Math::rand() % 6; - match rand: - #red - 0: - return Color.red - #blue - 1: - return Color.blue - #green - 2: - return Color.green - #orange - 3: - return Color.orange - #yellow - 4: - return Color.yellow - #purple - 5: - return Color.purple - */ + switch (rand) { + //red + case 0: + return Color(1.00, 0.00, 0.00); + //blue + case 1: + return Color(0.00, 0.00, 1.00); + //green + case 2: + return Color(0.00, 1.00, 0.00); + //orange + case 3: + return Color(1.00, 0.65, 0.00); + //yellow + case 4: + return Color(1.00, 1.00, 0.00); + //purple + case 5: + return Color(0.63, 0.13, 0.94); + } + + return Color(); } -String PaintUtilities::get_line_string(String file, int number) { +String PaintUtilities::get_line_string(const String &file, const int number) { /* return file.get_as_text().split("\n")[number - 1].strip_edges() */ + + return ""; } -void PaintUtilities::printv(Variant variable) { +void PaintUtilities::printv(const Variant &variable) { /* var stack = get_stack()[get_stack().size() - 1] var line = stack.line @@ -130,6 +135,8 @@ void PaintUtilities::printv(Variant variable) { var variable_name = left_p_string.left(right_p) print("%s: %s" % [variable_name, variable]) */ + + ERR_PRINT(variable); } PaintUtilities::PaintUtilities() { diff --git a/modules/paint/paint_utilities.h b/modules/paint/paint_utilities.h index 5b3dea177..989ec61a8 100644 --- a/modules/paint/paint_utilities.h +++ b/modules/paint/paint_utilities.h @@ -35,19 +35,19 @@ class PaintUtilities : public Object { GDCLASS(PaintUtilities, Object); public: - static Array get_pixels_in_line(Vector2 from, Vector2 to); + static PoolVector2iArray get_pixels_in_line(const Vector2i &from, const Vector2i &to); - static int to_1D_v(Vector2 p, float w); - static int to_1D(float x, float y, float w); - static Vector2 to_2D(int idx, float w); + static int to_1D_v(const Vector2i &p, int w); + static int to_1D(int x, int y, int w); + static Vector2i to_2D(int idx, int w); - static Color color_from_array(PoolRealArray color_array); + static Color color_from_array(const PoolRealArray &color_array); static Color random_color(); static Color random_color_alt(); - static String get_line_string(String file, int number); + static String get_line_string(const String &file, const int number); - static void printv(Variant variable); + static void printv(const Variant &variable); PaintUtilities(); ~PaintUtilities();