/
selecter.py
128 lines (92 loc) · 3.05 KB
/
selecter.py
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
from bge import logic, render
from mathutils import geometry, Vector
# from http://blender.stackexchange.com/a/28133/599
# and http://blender.stackexchange.com/a/2156/599
b = None
a_w = None
b_w = None
def get_world_coords(coords):
cont = logic.getCurrentController()
ray = cont.sensors["MouseRay"]
ray_start = ray.raySource
ray_end = ray.rayTarget
plane_origin = Vector((0, 0, 0))
plane_normal = Vector((0, 0, 1))
intersection = geometry.intersect_line_plane(ray_start, ray_end, plane_origin, plane_normal)
if intersection:
return intersection
def box_selection_vis(cont):
global b
scene = logic.getCurrentScene()
cam = scene.active_camera
own = cont.owner
click = cont.sensors["MouseClick"]
if click.positive:
if own['held']:
a = Vector(logic.mouse.position)
draw_box(a, b)
else:
a = Vector(logic.mouse.position)
b = a.copy()
own['held'] = True
else:
if own['held']:
own['held'] = False
def box_selection(cont):
global b_w
global a_w
scene = logic.getCurrentScene()
cam = scene.active_camera
own = cont.owner
click = cont.sensors["MouseClick"]
if click.positive:
if own['held']:
a_w = get_world_coords(Vector(logic.mouse.position))
else:
a_w = get_world_coords(Vector(logic.mouse.position))
b_w = a_w.copy()
own['held'] = True
else:
if own['held']:
select_inside(a_w, b_w)
own['held'] = False
def select_inside(a, b):
cont = logic.getCurrentController()
shift = cont.sensors["Keyboard"]
p1 = Vector((a.x, a.y))
p2 = Vector((b.x, a.y))
p3 = Vector((b.x, b.y))
p4 = Vector((a.x, b.y))
scene = logic.getCurrentScene()
cam = scene.active_camera
for obj in scene.objects:
if "ant" in obj:
# if both points are in the same place, deselect everything
if a == b:
obj["selected"] = False
else:
# remove existing selection unless shift is held
if not shift.positive:
if obj.get("selected", False):
obj["selected"] = False
if geometry.intersect_point_quad_2d(obj.worldPosition.xy, p1, p2, p3, p4):
print("selected", obj)
obj["selected"] = True
def gui_space(p):
p = Vector(p).copy()
p.x = p.x*16 - 8
p.y = p.y*9 - 4.5
p.y *= -1
return p
def draw_box(a, b):
color = (1,1,1)
a = gui_space(a)
b = gui_space(b)
p1 = Vector((a.x, a.y)).to_3d()
p2 = Vector((b.x, a.y)).to_3d()
p3 = Vector((b.x, b.y)).to_3d()
p4 = Vector((a.x, b.y)).to_3d()
render.drawLine(p1, p2, color)
render.drawLine(p2, p3, color)
render.drawLine(p3, p4, color)
render.drawLine(p4, p1, color)