-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathQDSpy_probeCenter.py
More file actions
175 lines (154 loc) · 6.05 KB
/
QDSpy_probeCenter.py
File metadata and controls
175 lines (154 loc) · 6.05 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
QDSpy module - program that generate and display on live the probe center
Copyright (c) 2017-2024 Tom Boissonnet, Thomas Euler
All rights reserved.
2024-06-30 - Reformatted (using Ruff)
***********************************************************************
***********************************************************************
TODO: Still uses pyglet directly ...
***********************************************************************
***********************************************************************
"""
# ---------------------------------------------------------------------
import pyglet
from math import sin, cos, pi
import QDSpy_stim_support as ssp
import Libraries.multiprocess_helper as mpr
import Graphics.renderer_opengl as rdr
# ---------------------------------------------------------------------
class ProbeCenter(object):
def __init__(
self,
_View,
_Stage,
width=50,
height=50,
intensity=127,
interval=1.0,
nVertice=100,
):
self.View = _View
self.Stage = _Stage
self.winWidth = self.View.winPre.width
self.winHeight = self.View.winPre.height
self.height = height
self.width = width
self.intensity = intensity
self.interval = interval
self.nVertice = nVertice
self.vertices = self.getEllipseVertices()
self.shiftedVertices = self.vertices
self.dxLast = 0
self.dyLast = 0
self.color = (128 + intensity, 128 + intensity, 128 + intensity, 255) * nVertice
self.highIntensity = True
self.View.clear(_RGB=[127, 127, 127])
def invertContrast(self, dt):
self.highIntensity = not self.highIntensity
if self.highIntensity:
self.color = (
128 + self.intensity,
128 + self.intensity,
128 + self.intensity,
255,
) * self.nVertice
else:
self.color = (
128 - self.intensity,
128 - self.intensity,
128 - self.intensity,
255,
) * self.nVertice
def setShiftedVertices(self, dx, dy):
self.dxLast = dx
self.dyLast = dy
newVertices = ()
for i in range(len(self.vertices)):
if i % 2 == 0:
newVertices += (self.vertices[i] + self.dxLast,)
else:
newVertices += (self.vertices[i] + self.dyLast,)
self.shiftedVertices = newVertices
def getEllipseVertices(self):
angle = 2 * pi / self.nVertice
originVertices = ()
for i in range(self.nVertice):
originVertices += (
int(self.width / 2 * cos(i * angle)),
int(self.height / 2 * sin(i * angle)),
)
return originVertices
def updateEllipseVertices(self):
self.vertices = self.getEllipseVertices()
self.shiftedVertices = self.vertices
self.setShiftedVertices(self.dxLast, self.dyLast)
# ---------------------------------------------------------------------
def probe_main(data, _Sync, _View, _Stage):
Win = _View.winPre
Batch = _View.createBatch(_isScrOvl=False)
probe = ProbeCenter(_View, _Stage, width=data[0], height=data[1], intensity=data[2])
"""
firstClick = True # First click is to focus the window, without ending the probe
"""
event_loop = pyglet.app.EventLoop()
xScale = _Stage.scalX_umPerPix * _Stage.winXCorrFact * Win.scale
yScale = _Stage.scalY_umPerPix * _Stage.winXCorrFact * Win.scale
def cleanExit():
event_loop.exit()
pyglet.clock.unschedule(checkCancel)
pyglet.clock.unschedule(probe.invertContrast)
Win.remove_handler("on_mouse_press", on_mouse_press)
Win.remove_handler("on_draw", on_draw)
Win.remove_handler("on_mouse_drag", on_mouse_drag)
"""
Win.remove_handler("on_mouse_motion", on_mouse_motion)
"""
_View.clear(_RGB=[0, 0, 0])
_View.present()
_View.winPre.set_mouse_visible(False)
def checkCancel(dt):
nonlocal probe
# Check if GUI requests terminating the program
if _Sync.Request.value in [mpr.CANCELING, mpr.TERMINATING]:
_Sync.setStateSafe(mpr.CANCELING)
cleanExit()
_Sync.setStateSafe(mpr.IDLE)
# Check if probe parameters have changed and if so, change probe
if _Sync.pipeSrv.poll():
data = _Sync.pipeSrv.recv()
if data[0] == mpr.PipeValType.toSrv_probeParams:
probe.width = data[2][0]
probe.height = data[2][1]
probe.intensity = data[2][2]
probe.updateEllipseVertices()
pyglet.clock.unschedule(probe.invertContrast)
pyglet.clock.schedule_interval(probe.invertContrast, data[2][3])
@Win.event
def on_mouse_press(x, y, button, modifiers):
if button & pyglet.window.mouse.RIGHT:
posX = int(probe.dxLast * xScale + _Stage.centOffX_pix)
posY = int(probe.dyLast * yScale + _Stage.centOffY_pix)
ssp.Log.write(
"DATA", "{'probeX': " + str(posX) + ", 'probeY': " + str(posY) + "}"
)
cleanExit()
@Win.event
def on_mouse_drag(_x, _y, dx, dy, buttons, modifiers):
if buttons & pyglet.window.mouse.LEFT:
x = int((_x - _Stage.centOffX_pix - Win.width / 2) / xScale)
y = int((_y - _Stage.centOffY_pix - Win.height / 2) / yScale)
probe.setShiftedVertices(x, y)
@Win.event
def on_draw():
_View.clear(_RGB=[127, 127, 127])
Batch.replace_object_data_non_indexed(
[], probe.shiftedVertices, probe.color, probe.color, _mode=rdr.MODE_POLYGON
)
Batch.draw(_Stage, _View, False)
pyglet.clock.schedule_interval(checkCancel, 0.1)
pyglet.clock.schedule_interval(probe.invertContrast, data[3])
Win.set_mouse_visible(True)
event_loop.run()
# ---------------------------------------------------------------------