Pixel-Composer/scripts/node_svg/node_svg.gml

124 lines
3.2 KiB
Text
Raw Normal View History

function Node_create_SVG_path(_x, _y, path) {
2024-06-06 09:54:13 +02:00
if(!file_exists_empty(path)) return noone;
var node = new Node_SVG(_x, _y, PANEL_GRAPH.getCurrentContext()).skipDefault();
2024-08-08 06:57:51 +02:00
node.inputs[0].setValue(path);
2024-06-06 09:54:13 +02:00
node.doUpdate();
return node;
}
2024-06-06 09:54:13 +02:00
function Node_SVG(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "SVG";
color = COLORS.node_blend_input;
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_Path("Path", self, ""))
2024-06-06 09:54:13 +02:00
.setDisplay(VALUE_DISPLAY.path_load, { filter: "Scalable Vector Graphics|*.svg" });
2024-08-18 06:16:20 +02:00
newInput(1, nodeValue_Float("Scale", self, 1));
2024-06-06 09:54:13 +02:00
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Surface out", self, VALUE_TYPE.surface, noone));
2024-06-06 09:54:13 +02:00
2024-09-04 03:57:11 +02:00
newOutput(1, nodeValue_Output("SVG Struct", self, VALUE_TYPE.struct, {}));
2024-06-06 09:54:13 +02:00
attribute_surface_depth();
rawContent = noone;
content = {};
edit_time = 0;
curr_path = "";
attributes.check_splice = true;
attributes.file_checker = true;
array_push(attributeEditors, [ "File Watcher", function() { return attributes.file_checker; },
new checkBox(function() { attributes.file_checker = !attributes.file_checker; }) ]);
on_drop_file = function(path) {
2024-08-08 06:57:51 +02:00
inputs[0].setValue(path);
2024-06-06 09:54:13 +02:00
if(readFile(path)) {
doUpdate();
return true;
}
return false;
}
2024-06-06 09:54:13 +02:00
function readFile(path) {
2024-06-06 09:54:13 +02:00
curr_path = path;
if(!file_exists_empty(path))
return noone;
edit_time = file_get_modify_s(path);
var ext = string_lower(filename_ext(path));
var _name = filename_name_only(path);
if(ext != ".svg") return;
var _rawContent = file_read_all(path);
var _st = string_pos("<svg", _rawContent);
var _end = string_length(_rawContent);
_rawContent = string_copy(_rawContent, _st, _end - _st + 1);
2024-06-06 09:54:13 +02:00
rawContent = SnapFromXML(_rawContent);
content = svg_parse(rawContent);
return;
}
2024-06-06 09:54:13 +02:00
insp1UpdateTooltip = __txt("Refresh");
insp1UpdateIcon = [ THEME.refresh_icon, 1, COLORS._main_value_positive ];
static onInspector1Update = function() {
2024-06-06 09:54:13 +02:00
readFile(path_get(getInputData(0)));
triggerRender();
}
2024-06-06 09:54:13 +02:00
2024-06-08 06:31:21 +02:00
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
var _scale = getInputData(1);
if(is_instanceof(content, SVG))
content.drawOverlay(hover, active, _x, _y, _s * _scale, _mx, _my, _snx, _sny);
}
static step = function() {
2024-06-06 09:54:13 +02:00
var path = path_get(getInputData(0));
if(!file_exists_empty(path)) return;
var _modi = file_get_modify_s(path);
if(attributes.file_checker && _modi > edit_time) {
readFile(path);
triggerRender();
}
}
2024-06-06 09:54:13 +02:00
static update = function(frame = CURRENT_FRAME) {
2024-06-06 09:54:13 +02:00
var path = path_get(getInputData(0));
if(path != curr_path)
readFile(path);
2024-06-08 06:31:21 +02:00
if(!is_instanceof(content, SVG)) return;
2024-08-08 06:57:51 +02:00
outputs[1].setValue(path);
2024-06-06 09:54:13 +02:00
2024-06-08 06:31:21 +02:00
var _scale = getInputData(1);
2024-08-08 06:57:51 +02:00
var _outsurf = outputs[0].getValue();
2024-06-06 09:54:13 +02:00
2024-06-08 06:31:21 +02:00
var ww = content.width * _scale;
var hh = content.height * _scale;
2024-06-06 09:54:13 +02:00
_outsurf = surface_verify(_outsurf, ww, hh, attrDepth());
surface_set_shader(_outsurf, noone);
2024-06-08 06:31:21 +02:00
content.draw(_scale);
2024-06-06 09:54:13 +02:00
surface_reset_shader();
2024-08-08 06:57:51 +02:00
outputs[0].setValue(_outsurf);
outputs[1].setValue(rawContent);
}
2024-07-03 05:16:46 +02:00
static dropPath = function(path) {
if(is_array(path)) path = array_safe_get(path, 0);
if(!file_exists_empty(path)) return;
2024-08-08 06:57:51 +02:00
inputs[0].setValue(path);
2024-07-03 05:16:46 +02:00
}
2024-06-06 09:54:13 +02:00
}