Skip to content

Commit

Permalink
- moved connect/disconnect functions, as if few graph exists, it woul…
Browse files Browse the repository at this point in the history
…d be easier to manage this way

- thanks to above, helpers aren't needed anymore if user doesn't visualise graph using objects
  • Loading branch information
gnysek committed Apr 11, 2022
1 parent 3df7806 commit 5b70501
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 57 deletions.
6 changes: 2 additions & 4 deletions grafy.yyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions objects/obj_editor/Mouse_54.gml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
if (near.v != undefined) {
var _keys = ds_map_keys_to_array(near.v.connections);
for(var i = 0; i < array_length(_keys); i++) {
disconnect_points(global.my_graph, near.name, _keys[i]);
global.my_graph.disconnect(near.name, _keys[i]);
}
}

Expand All @@ -19,7 +19,7 @@
if (node1 > -1) {
node2 = instance_nearest(mouse_x, mouse_y, obj_node);
if (point_distance(mouse_x, mouse_y, node2.x, node2.y) < 50) {
disconnect_points(global.my_graph, node1.name, node2.name);
global.my_graph.disconnect(node1.name, node2.name);
}
} else {
event_perform(ev_mouse, ev_global_left_press);
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ Zawiera te przykład dodawania/edytowania/wyświetlania grafu, który nie jest w

---

Created by Piotr Gnys ([@gnysek](https://github.com/gnysek)).

Managed by [GameMaker's Clan - gmclan.org](https://gmclan.org).

You can freely use, modify, and re-distribute code. Of course it would be nice if you mention author in game credits, but this is not required.

*GameMaker was previously known as GameMaker Studio 2.
16 changes: 1 addition & 15 deletions scripts/connect_points/connect_points.gml
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
/// @param {String} a
/// @param {String} b
function connect_points(gr, a, b){

if (a == b) return false;

var _va = gr.get_vertex(a);
var _vb = gr.get_vertex(b);
var _dist = objects_distance_by_name(a, b);

if (_va and _vb) {
_va.connect(_vb, _dist);
_vb.connect(_va, _dist); // back-version of connection
} else {
throw "one of vertices " + string(a) + "," + string(b) +" was not found";
}

return true;
return gr.connect(a, b, objects_distance_by_name(a, b));
}
4 changes: 2 additions & 2 deletions scripts/connect_points/connect_points.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 0 additions & 16 deletions scripts/disconnect_points/disconnect_points.gml

This file was deleted.

12 changes: 0 additions & 12 deletions scripts/disconnect_points/disconnect_points.yy

This file was deleted.

35 changes: 35 additions & 0 deletions scripts/graph/graph.gml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,43 @@ function graph() constructor {

/// @param {String} vertex
static remove = function(_vertex_id) {
if (self.vertices[? _vertex_id] != undefined) {
self.vertices[? _vertex_id].destroy();
ds_map_delete(self.vertices, _vertex_id);
}
}

static connect = function(a, b, _dist) {
if (a == b) return false;

var _va = self.get_vertex(a);
var _vb = self.get_vertex(b);

if (_va and _vb) {
_va.connect(_vb, _dist);
_vb.connect(_va, _dist); // back-version of connection
} else {
throw "one of vertices " + string(a) + "," + string(b) +" was not found";
}

return true;
}

static disconnect = function(a, b) {
if (a == b) return false;

var _va = self.get_vertex(a);
var _vb = self.get_vertex(b);

if (_va and _vb) {
_va.disconnect(_vb.id);
_vb.disconnect(_va.id);
} else {
throw "one of vertices " + string(a) + "," + string(b) +" was not found";
}

return true;
}

/// @return {Struct.vertex}
static get_vertex = function(id) {
Expand Down
9 changes: 5 additions & 4 deletions scripts/graph_load/graph_load.gml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ function graph_load() {
for(var i = 0; i < _total_nodes; i++) {
var _total_connections = ini_read_real("graph", "connections_" + _nodes_names[i], 0);
for(var j = 0; j < _total_connections; j++) {
connect_points(
g,
_nodes_names[i],
ini_read_string("graph", "connection_" + _nodes_names[i] + "_" + string(j), "") // connection instead connections (mind "s")
var _n1 = _nodes_names[i], _n2 = ini_read_string("graph", "connection_" + _nodes_names[i] + "_" + string(j), ""); // connection instead connections (mind "s")
g.connect(
_n1,
_n2,
objects_distance_by_name(_n1, _n2)
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/objects_distance_by_name/objects_distance_by_name.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5b70501

Please sign in to comment.