-
Notifications
You must be signed in to change notification settings - Fork 2
/
sofa_visual_grid.gd
131 lines (109 loc) · 4.17 KB
/
sofa_visual_grid.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
tool
extends Node
##
## @author: Christoph Haas, Pit Henrich
## @desc: SOFA VisualGrid
##
## https://github.com/sofa-framework/sofa/blob/master/Sofa/Component/Visual/src/sofa/component/visual/VisualGrid.h
##
func get_class() -> String:
return "SofaVisualGrid"
func is_class(clazz: String) -> bool:
return .is_class(clazz) or (clazz == get_class())
func _get_property_list() -> Array:
return _registry.gen_property_list()
func _set(property: String, value) -> bool:
return _registry.handle_set(property, value)
func _get(property: String):
return _registry.handle_get(property)
const PropertyWrapperRegistry = preload("res://addons/sofa_godot_plugin/property_wrappers/property_wrapper_registry.gd")
const PyProgram = preload("res://addons/sofa_godot_plugin/sofa_python/python/py_program.gd")
const OBJECT_NAME = "visual_grid"
var _visual_grid: VisualGrid
var _registry = PropertyWrapperRegistry.new(self)
func _init():
set_name(OBJECT_NAME)
_visual_grid = VisualGrid.new(self, _registry)
func process_python(program: PyProgram, parent_identifier: String, indent_depth: int):
_visual_grid.process(program, parent_identifier, indent_depth)
func _process(delta):
_visual_grid.draw()
## <assignee> = <node>.addObject("VisualGrid", name=<name>, ...)
class VisualGrid:
extends "res://addons/sofa_godot_plugin/sofa_python/sofa_components/sofa_object_base.gd"
const DebugDraw = preload("res://addons/sofa_godot_plugin/debug_draw.gd")
const MODULE = "Sofa.Component.Visual"
const TYPE = "VisualGrid"
const cat_visual_grid = "SOFA VisualGrid"
var _debug_drawer: DebugDraw
func _init(node: Node, registry: PropertyWrapperRegistry).(TYPE, node, registry):
pass
# @override
func setup():
_debug_drawer = DebugDraw.new()
_node.add_child(_debug_drawer)
.setup()
# @override
func _setup_properties():
## Plane of the grid
_registry.make_enum({"x": "x", "y": "y", "z": "z"}, "plane").select("z").category(cat_visual_grid)
## Size of the squared grid
_registry.make_float(10.0, "size").category(cat_visual_grid)
## Number of subdivisions
_registry.make_int(16, "subdivisions").category(cat_visual_grid)
## Color of the lines in the grid
_registry.make_color(Color(0.34, 0.34, 0.34, 1.0), "color").category(cat_visual_grid)
## Thickness of the lines in the grid
_registry.make_float(1.0, "thickness").category(cat_visual_grid)
## Display the grid or not
_registry.make_bool(true, "draw").category(cat_visual_grid)
# @override
func _setup_statement():
add_sofa_plugin(MODULE)
var args = arguments().with_registry(_registry)
args.add_plain("name").position(1).bind(_node, "get_name")
## Plane of the grid
args.add_property("plane")
## Size of the squared grid
args.add_property("size")
## Number of subdivisions
args.add_property("nbSubdiv", "subdivisions")
## Color of the lines in the grid
args.add_property("color")
## Thickness of the lines in the grid
args.add_property("thickness")
## Display the grid or not
args.add_property("draw")
func draw():
if not _registry.get_value("draw"):
return
var plane = _registry.get_value("plane")
var color = _registry.get_value("color")
var size = _registry.get_value("size")
var subdivisions = _registry.get_value("subdivisions")
var step_size = size / subdivisions
for h in range (-subdivisions/2, subdivisions/2 + 1):
var a_begin = Vector3.ZERO
var a_end = Vector3.ZERO
var b_begin = Vector3.ZERO
var b_end = Vector3.ZERO
match plane:
"x":
a_begin = Vector3(0, -size/2, h*step_size)
a_end = Vector3(0, size/2, h*step_size)
b_begin = Vector3(0, h*step_size, -size/2)
b_end = Vector3(0, h*step_size, size/2)
"y":
a_begin = Vector3(-size/2, 0, h*step_size)
a_end = Vector3( size/2, 0, h*step_size)
b_begin = Vector3(h*step_size, 0, -size/2)
b_end = Vector3(h*step_size, 0, size/2)
"z":
a_begin = Vector3(-size/2, h*step_size, 0)
a_end = Vector3( size/2, h*step_size, 0)
b_begin = Vector3(h*step_size, -size/2, 0)
b_end = Vector3(h*step_size, size/2, 0)
_:
assert(false, "Unknonwn plane")
_debug_drawer.draw_line(a_begin, a_end, color)
_debug_drawer.draw_line(b_begin, b_end, color)