1 | // This file is a part of the Framsticks GDK. |
---|
2 | // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.framsticks.com/ for further information. |
---|
4 | |
---|
5 | #include "neurodiagram.h" |
---|
6 | #include "nn_layout.h" |
---|
7 | #include <frams/neuro/neurolibrary.h> |
---|
8 | #include <frams/mech/mechworld.h> |
---|
9 | #include <frams/util/multirange.h> |
---|
10 | #include "canvasutil.h" |
---|
11 | #include <frams/neuro/neuroimpl.h> |
---|
12 | #include <frams/model/modelobj.h> |
---|
13 | #include <frams/simul/simul.h> |
---|
14 | #include "common/nonstd_time.h" |
---|
15 | |
---|
16 | #define FIELDSTRUCT NeuroDiagram |
---|
17 | ParamEntry neurodiagram_paramtab[] = |
---|
18 | { |
---|
19 | { "NeuroDiagram", 1, 4, "NeuroDiagram", "Can be used as the client object in the Window.", }, |
---|
20 | { "new", 0, PARAM_USERHIDDEN, "create new NeuroDiagram", "p oNeuroDiagram", PROCEDURE(p_new), }, |
---|
21 | { "showCreature", 0, PARAM_USERHIDDEN + PARAM_NOSTATIC, "show dynamic NN", "p(oCreature)", PROCEDURE(p_showcr), }, |
---|
22 | { "showModel", 0, PARAM_USERHIDDEN + PARAM_NOSTATIC, "show static NN", "p(oModel)", PROCEDURE(p_showmod), }, |
---|
23 | { "hide", 0, PARAM_USERHIDDEN + PARAM_NOSTATIC, "hide NN", "p()", PROCEDURE(p_hide), }, |
---|
24 | { 0, 0, 0, }, |
---|
25 | }; |
---|
26 | #undef FIELDSTRUCT |
---|
27 | |
---|
28 | Param neurodiagram_param(neurodiagram_paramtab, 0); |
---|
29 | |
---|
30 | static struct ColorDefs colordefs; |
---|
31 | |
---|
32 | void NeuroDiagram::p_new(ExtValue*args, ExtValue*ret) |
---|
33 | { |
---|
34 | NeuroDiagram *d = new NeuroDiagram(&colordefs); |
---|
35 | d->drawbackground = false; |
---|
36 | ret->setObject(ExtObject(&neurodiagram_param, d)); |
---|
37 | } |
---|
38 | |
---|
39 | void NeuroDiagram::p_showcr(ExtValue*args, ExtValue*ret) |
---|
40 | { |
---|
41 | Creature *cr = 0; |
---|
42 | if (args->type == TObj) |
---|
43 | { |
---|
44 | const ExtObject& o = args->getObject(); |
---|
45 | cr = (Creature*)o.getTarget(); |
---|
46 | } |
---|
47 | showLive(cr); |
---|
48 | } |
---|
49 | |
---|
50 | void NeuroDiagram::p_showmod(ExtValue*args, ExtValue*ret) |
---|
51 | { |
---|
52 | Model *mod = ModelObj::fromObject(args[0]); |
---|
53 | show(mod); |
---|
54 | } |
---|
55 | |
---|
56 | static void addNeuroDescription(SString &t, Neuro *n) |
---|
57 | { |
---|
58 | static Param par; |
---|
59 | SString c(n->getClassName()); |
---|
60 | NeuroClass* cl = n->getClass(); |
---|
61 | t += c; |
---|
62 | t += " ("; |
---|
63 | if (cl) |
---|
64 | t += cl->getLongName(); |
---|
65 | else |
---|
66 | t += "Unknown"; |
---|
67 | t += ")"; |
---|
68 | } |
---|
69 | |
---|
70 | NeuroDiagram::NeuroDiagram(ColorDefs *cd) |
---|
71 | :FramDrawToolkit(cd), livewire(false), indestructor(false), showing_not_alive_label(false), o(0), |
---|
72 | warn_if_not_alive(true), selection(*this), drawbackground(true), linetype(true), layouttype(2) |
---|
73 | { |
---|
74 | scroll.setMargin(10, 10); // appropriate size should be adjusted |
---|
75 | dontPaintOutside(0); |
---|
76 | add(&scroll); |
---|
77 | pluginactive = false; |
---|
78 | FramDrawToolkit::setBackColor(ColorDefs::neurobackground); |
---|
79 | } |
---|
80 | |
---|
81 | NeuroDiagram::~NeuroDiagram() |
---|
82 | { |
---|
83 | indestructor = 1; |
---|
84 | hide(); |
---|
85 | remove(&scroll); |
---|
86 | updatePlugin(); |
---|
87 | } |
---|
88 | |
---|
89 | void NeuroDiagram::hide() |
---|
90 | { |
---|
91 | showing_not_alive_label = 0; |
---|
92 | if (o) o->delmodel_list.remove(killnode); |
---|
93 | FOREACH(NeuroProbe*,pr,probes) |
---|
94 | delete pr; |
---|
95 | probes.clear(); |
---|
96 | selection.clear(0); |
---|
97 | cr = 0; |
---|
98 | livewire = false; |
---|
99 | } |
---|
100 | |
---|
101 | class NNLayoutState_Neurodiagram : public NNLayoutState |
---|
102 | { |
---|
103 | public: |
---|
104 | NeuroDiagram *nd; |
---|
105 | NNLayoutState_Neurodiagram(NeuroDiagram *_nd) :nd(_nd) {} |
---|
106 | |
---|
107 | int GetElements() |
---|
108 | { |
---|
109 | return nd->scroll.count(); |
---|
110 | } |
---|
111 | |
---|
112 | int *GetXYWH(int el) |
---|
113 | { |
---|
114 | return &nd->scroll.getInfo(el)->pos.x; |
---|
115 | } |
---|
116 | |
---|
117 | void SetXYWH(int el, int x, int y, int w, int h) |
---|
118 | { |
---|
119 | ScrollInfo *si = nd->scroll.getInfo(el); |
---|
120 | si->pos.set(x, y); si->size.set(w, h); |
---|
121 | } |
---|
122 | |
---|
123 | int GetInputs(int el) |
---|
124 | { |
---|
125 | return nd->getNS(el)->n->getInputCount(); |
---|
126 | } |
---|
127 | |
---|
128 | int GetLink(int el, int i) |
---|
129 | { |
---|
130 | return nd->getNS(el)->n->getInput(i)->refno; |
---|
131 | } |
---|
132 | |
---|
133 | int *GetLinkXY(int el, int i) |
---|
134 | { |
---|
135 | static int XY[2]; |
---|
136 | int *xywh = GetXYWH(el); |
---|
137 | XY[0] = 0; |
---|
138 | XY[1] = ((1 + i)*xywh[3]) / (GetInputs(el) + 1); |
---|
139 | return XY; |
---|
140 | } |
---|
141 | }; |
---|
142 | |
---|
143 | |
---|
144 | void NeuroDiagram::show(Model *o_) |
---|
145 | { |
---|
146 | hide(); |
---|
147 | o = o_; |
---|
148 | scroll.removeAll(); |
---|
149 | if (o) |
---|
150 | { |
---|
151 | Neuro *n; |
---|
152 | int i; |
---|
153 | killnode = o->delmodel_list.add(STATRICKCALLBACK(this, &NeuroDiagram::onKill, 0)); |
---|
154 | |
---|
155 | // create symbol objects |
---|
156 | for (i = 0; n = o->getNeuro(i); i++) |
---|
157 | { |
---|
158 | NeuroSymbol *ns = new NeuroSymbol(*this, n); |
---|
159 | scroll.add(ns, 1); // autodel |
---|
160 | } |
---|
161 | if (i) |
---|
162 | { |
---|
163 | struct NNLayoutFunction *nnfun = &nn_layout_functions[layouttype]; |
---|
164 | NNLayoutState_Neurodiagram nn(this); |
---|
165 | nnfun->doLayout(&nn); |
---|
166 | } |
---|
167 | scroll.invalidate(); |
---|
168 | scroll.autoZoom(); |
---|
169 | } |
---|
170 | |
---|
171 | updatePlugin(); |
---|
172 | requestPaint(); |
---|
173 | } |
---|
174 | |
---|
175 | void NeuroDiagram::showLive(Creature *_cr) |
---|
176 | { |
---|
177 | showing_not_alive_label = 0; |
---|
178 | if (!_cr) { show(0); return; } |
---|
179 | show(&_cr->getModel()); |
---|
180 | cr = _cr; |
---|
181 | livewire = true; |
---|
182 | updatePlugin(); |
---|
183 | } |
---|
184 | |
---|
185 | void NeuroDiagram::paint() |
---|
186 | { |
---|
187 | if (drawbackground) |
---|
188 | { |
---|
189 | setColor(ColorDefs::neurobackground); |
---|
190 | clear(); |
---|
191 | } |
---|
192 | |
---|
193 | if (countNeurons() > 0) |
---|
194 | { |
---|
195 | CanvasWindowContainer::paint(); |
---|
196 | } |
---|
197 | else |
---|
198 | { |
---|
199 | setColor(ColorDefs::neuroneuron); |
---|
200 | drawAlignedText(size.x / 2, (size.y - textHeight()) / 2, 0, "[No neural network]"); |
---|
201 | } |
---|
202 | |
---|
203 | if (showing_not_alive_label) |
---|
204 | { |
---|
205 | if (time(0) > showing_not_alive_label) |
---|
206 | showing_not_alive_label = 0; |
---|
207 | else |
---|
208 | { |
---|
209 | setColor(0, 0, 0); |
---|
210 | drawAlignedText(not_alive_location.x, not_alive_location.y, 0, "select a creature"); |
---|
211 | drawAlignedText(not_alive_location.x, not_alive_location.y + textHeight(), 0, "to probe neurons"); |
---|
212 | } |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | void NeuroDiagram::resize(int w, int h) |
---|
217 | { |
---|
218 | CanvasWindowContainer::resize(w, h); |
---|
219 | scroll.autoZoom(); |
---|
220 | } |
---|
221 | |
---|
222 | int NeuroDiagram::countNeurons() |
---|
223 | { |
---|
224 | return scroll.count(); |
---|
225 | } |
---|
226 | |
---|
227 | void NeuroDiagram::addProbe(int i) |
---|
228 | { |
---|
229 | if (i >= countNeurons()) return; |
---|
230 | NeuroProbe *probe = new NeuroProbe(getNS(i)); |
---|
231 | probes += (void*)probe; |
---|
232 | add(probe); |
---|
233 | updatePlugin(); |
---|
234 | requestPaint(); |
---|
235 | } |
---|
236 | |
---|
237 | void NeuroDiagram::onKill(void*obj, long dummy) |
---|
238 | { |
---|
239 | show(0); |
---|
240 | } |
---|
241 | |
---|
242 | /////////////////////////// |
---|
243 | |
---|
244 | NeuroSymbol::NeuroSymbol(NeuroDiagram &nd, Neuro * _n) |
---|
245 | :selected(0), n(_n), diagram(nd) |
---|
246 | { |
---|
247 | tooltip = "#"; |
---|
248 | tooltip += SString::valueOf((int)n->refno); |
---|
249 | tooltip += " - "; |
---|
250 | label = tooltip; |
---|
251 | addNeuroDescription(tooltip, n); |
---|
252 | label += n->getClassName(); |
---|
253 | if (n->getClassParams().len()) |
---|
254 | { |
---|
255 | tooltip += "\n"; tooltip += n->getClassParams(); |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | void NeuroSymbol::paint() |
---|
260 | { |
---|
261 | if (selected) |
---|
262 | { |
---|
263 | setColor(255, 255, 255); |
---|
264 | fillRect(0, 0, size.x, size.y); |
---|
265 | } |
---|
266 | diagram.setClip(); |
---|
267 | diagram.setColor(ColorDefs::neuroneuron); |
---|
268 | drawNeuroSymbol(this, n->getClass(), 0, 0, size.x, size.y); |
---|
269 | |
---|
270 | if (size.y > 4 * textHeight()) |
---|
271 | { |
---|
272 | const char* t = label; |
---|
273 | drawAlignedText(size.x / 2, size.y - textHeight(), 0, t); |
---|
274 | |
---|
275 | NeuroImpl *ni = NeuroNetImpl::getImpl(n); |
---|
276 | if (ni && (ni->getChannelCount() > 1)) |
---|
277 | { |
---|
278 | drawLine(size.x - size.x / 16, size.y / 2 - size.y / 16, |
---|
279 | size.x - size.x / 8, size.y / 2 + size.y / 16); |
---|
280 | char t[20]; |
---|
281 | sprintf(t, "%d", ni->getChannelCount()); |
---|
282 | moveTo(size.x, size.y / 2 - textHeight()); |
---|
283 | drawText(t); |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | /* |
---|
288 | |
---|
289 | NeuroDiagram |
---|
290 | *........................................ |
---|
291 | . . |
---|
292 | . NeuroSymbol . |
---|
293 | . (pos.x,pos.y)-*......... . |
---|
294 | . . |\ . ^ s . |
---|
295 | . ..._____._| \ . | i . |
---|
296 | . . | \___. | z . |
---|
297 | . __._| / . | e . |
---|
298 | . | . | / . | . . |
---|
299 | . | . |/ . | y . |
---|
300 | . | .......... v . |
---|
301 | . | <--------> . |
---|
302 | . .......... | size.x . |
---|
303 | . . |\ . | . |
---|
304 | . __._| \ . | . |
---|
305 | . . | \___._| . |
---|
306 | . __._| / . . |
---|
307 | . | . | / . . |
---|
308 | . | . |/ . . |
---|
309 | . | .......... . |
---|
310 | . | . |
---|
311 | . |________________... . |
---|
312 | ......................................... |
---|
313 | |
---|
314 | */ |
---|
315 | |
---|
316 | // NeuroSymbol is also responsible for drawing connection lines from its inputs to other NeuroSymbols' outputs |
---|
317 | NeuroSymbol *ns2; |
---|
318 | if (!diagram.isLive()) |
---|
319 | diagram.setColor(ColorDefs::neurolink); |
---|
320 | for (int iw = 0; iw < n->getInputCount(); iw++) |
---|
321 | { |
---|
322 | ns2 = diagram.getNS(n->getInput(iw)->refno); // the other NeuroSymbol (our input will connect to its output) |
---|
323 | |
---|
324 | int yw = inputY(iw); |
---|
325 | int xw = yw / 4; // x coordinate of the first corner point, depends on yw to avoid overlapping between inputs |
---|
326 | drawLine(size.x / 4, yw, xw, yw); // first horizontal segment (to the left) |
---|
327 | diagram.setWireColor(ns2->n->state, 0); |
---|
328 | if ((diagram.linetype != 1) || (ns2->pos.x + ns2->size.x / 2 < pos.x)) |
---|
329 | { // straight line to the other neuron's output (the signal goes forwards) |
---|
330 | ns2->lineTo(ns2->size.x, ns2->size.y / 2); |
---|
331 | } |
---|
332 | else |
---|
333 | { // make an U-shaped loop from 3 segments - vert/horiz/vert (the signal goes backwards) |
---|
334 | int y2; |
---|
335 | int down; |
---|
336 | if (ns2 == this) down = (iw >= ((n->getInputCount()) / 2)); else down = (ns2->pos.y > (pos.y + size.y)); |
---|
337 | if (down) |
---|
338 | { |
---|
339 | y2 = (pos.y + size.y + (size.y - yw) / 3); |
---|
340 | } |
---|
341 | else |
---|
342 | { |
---|
343 | y2 = pos.y - yw / 3; |
---|
344 | if ((ns2->pos.y<pos.y) && (ns2->pos.y>(pos.y - ns2->size.y))) y2 -= pos.y - ns2->pos.y; |
---|
345 | } |
---|
346 | // note: "diagram" uses global coordinate system, so we add "pos" or "ns2->pos" to get NeuroSymbol's global positions |
---|
347 | diagram.lineTo(pos.x + xw, y2); |
---|
348 | diagram.lineTo(ns2->pos.x + ns2->size.x, y2); |
---|
349 | diagram.lineTo(ns2->pos.x + ns2->size.x, ns2->pos.y + ns2->size.y / 2); |
---|
350 | } |
---|
351 | |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | void NeuroSymbol::mouse(int x, int y, int t) |
---|
356 | { |
---|
357 | if ((t & (LeftButton | ShiftButton)) == (LeftButton | ShiftButton)) |
---|
358 | { |
---|
359 | ScrollManager& sc = diagram.scroll; |
---|
360 | sc.setPos2(n->refno, pos.x + x - diagram.symboldragpos.x, pos.y + y - diagram.symboldragpos.y); |
---|
361 | sc.validate(); |
---|
362 | requestPaint(); |
---|
363 | } |
---|
364 | } |
---|
365 | |
---|
366 | int NeuroSymbol::mouseclick(int x, int y, int t) |
---|
367 | { |
---|
368 | if ((t & (LeftButton | DblClick)) == (LeftButton | DblClick)) |
---|
369 | { |
---|
370 | if (diagram.isLive()) |
---|
371 | diagram.addProbe(n->refno); |
---|
372 | else |
---|
373 | { |
---|
374 | if (diagram.warn_if_not_alive) |
---|
375 | { |
---|
376 | diagram.showing_not_alive_label = time(0) + 10; |
---|
377 | diagram.not_alive_location.x = pos.x + x; |
---|
378 | diagram.not_alive_location.y = pos.y + y; |
---|
379 | diagram.requestPaint(); |
---|
380 | } |
---|
381 | } |
---|
382 | return LeftButton | DblClick; |
---|
383 | } |
---|
384 | |
---|
385 | if ((t & (LeftButton | ShiftButton)) == (LeftButton | ShiftButton)) |
---|
386 | { |
---|
387 | if (selected) |
---|
388 | diagram.selection.remove(Model::neuroToMap(n->refno)); |
---|
389 | else |
---|
390 | diagram.selection.add(Model::neuroToMap(n->refno)); |
---|
391 | diagram.symboldragpos.set(x, y); |
---|
392 | return LeftButton | ShiftButton; |
---|
393 | } |
---|
394 | |
---|
395 | if (t & LeftButton) |
---|
396 | { |
---|
397 | diagram.selection.set(Model::neuroToMap(n->refno)); |
---|
398 | return LeftButton; |
---|
399 | } |
---|
400 | |
---|
401 | return 0; |
---|
402 | } |
---|
403 | |
---|
404 | // coordinate y of i-th input |
---|
405 | int NeuroSymbol::inputY(int i) |
---|
406 | { |
---|
407 | return (1 + i)*size.y / ((n->getInputCount()) + 1); |
---|
408 | } |
---|
409 | |
---|
410 | SString NeuroSymbol::hint(int x, int y) |
---|
411 | { |
---|
412 | if ((y >= 0) && (y < size.y)) |
---|
413 | if (x<size.x / 4) |
---|
414 | { // inputs? |
---|
415 | if (n->getInputCount()>0) |
---|
416 | { |
---|
417 | int i = (y*n->getInputCount()) / size.y; |
---|
418 | double w; |
---|
419 | Neuro* target = n->getInput(i, w); |
---|
420 | if (target) |
---|
421 | { |
---|
422 | SString t = "connected to #"; |
---|
423 | t += SString::valueOf((int)target->refno); |
---|
424 | t += " - "; |
---|
425 | addNeuroDescription(t, target); |
---|
426 | // if (w!=1.0) |
---|
427 | { |
---|
428 | t += ", weight="; |
---|
429 | t += SString::valueOf(w); |
---|
430 | } |
---|
431 | return t; |
---|
432 | } |
---|
433 | } |
---|
434 | } |
---|
435 | return CanvasWindow::hint(x, y); |
---|
436 | } |
---|
437 | |
---|
438 | ///////////////////////// |
---|
439 | |
---|
440 | NeuroProbe::NeuroProbe(NeuroSymbol* ns) |
---|
441 | :DCanvasWindow(DCanvasWindow::Title + DCanvasWindow::Border + DCanvasWindow::Close + DCanvasWindow::Size, |
---|
442 | (const char*)ns->getLabel(), &neurochart, &neurochart) |
---|
443 | { |
---|
444 | holdismine = 0; |
---|
445 | drawing = 0; whichdrawing = -1; |
---|
446 | clientbordersset = 0; |
---|
447 | adjustingvalue = 0; |
---|
448 | link = ns; |
---|
449 | tooltip = SString("Probe for ") + ns->tooltip; |
---|
450 | setPos(ns->getPos().x, ns->getPos().y); |
---|
451 | neurochart.printMinMax(0); |
---|
452 | neurochart.data.setMinMax(-1, 1); |
---|
453 | chnum = 1; chnum2 = 0; chsel = 0; |
---|
454 | chselwidth = 0; |
---|
455 | chselecting = 0; |
---|
456 | updateChannelCount(NeuroNetImpl::getImpl(link->n)); |
---|
457 | } |
---|
458 | |
---|
459 | void NeuroProbe::onClose() |
---|
460 | { |
---|
461 | link->diagram.probes -= this; |
---|
462 | delete this; |
---|
463 | } |
---|
464 | |
---|
465 | NeuroProbe::~NeuroProbe() |
---|
466 | { |
---|
467 | if (holdismine) |
---|
468 | link->n->flags &= ~Neuro::HoldState; |
---|
469 | } |
---|
470 | |
---|
471 | void NeuroProbe::paint() |
---|
472 | { |
---|
473 | static char t[40]; |
---|
474 | if (!clientbordersset) |
---|
475 | { |
---|
476 | clientbordersset = 1; |
---|
477 | setClientBorder(0, 1, 16, textHeight() + 2); // don't use textheight outside paint/mouse events |
---|
478 | } |
---|
479 | int hold = link->n->flags & Neuro::HoldState; |
---|
480 | float state = (float)link->n->state; |
---|
481 | NeuroImpl *ni = 0; |
---|
482 | if (chsel != 0) |
---|
483 | { |
---|
484 | ni = NeuroNetImpl::getImpl(link->n); |
---|
485 | if (chsel < 0) |
---|
486 | { |
---|
487 | int dr = -chsel - 1; |
---|
488 | if (whichdrawing != dr) |
---|
489 | { |
---|
490 | drawing = ni->getDrawing(dr); |
---|
491 | whichdrawing = dr; |
---|
492 | } |
---|
493 | if (drawing) |
---|
494 | { |
---|
495 | int *dr = drawing; |
---|
496 | int w = size.x - 2, h = size.y - clienttop - clientbottom; |
---|
497 | int scale = min(w, h); |
---|
498 | int x0 = clienttop + leftborder + ((w > h) ? (w - h) / 2 : 0); |
---|
499 | int y0 = clientleft + topborder + ((h > w) ? (h - w) / 2 : 0); |
---|
500 | |
---|
501 | while (*dr != NeuroImpl::ENDDRAWING) |
---|
502 | { |
---|
503 | int first = 1; |
---|
504 | unsigned char r, g, b; |
---|
505 | FramDrawToolkit::splitRGB(*(dr++), r, g, b); |
---|
506 | setColor(r, g, b); |
---|
507 | while (*dr != NeuroImpl::ENDDRAWING) |
---|
508 | { |
---|
509 | int x = ((*(dr++))*scale) / (NeuroImpl::MAXDRAWINGXY + 1) + x0; |
---|
510 | int y = ((*(dr++))*scale) / (NeuroImpl::MAXDRAWINGXY + 1) + y0; |
---|
511 | if (first) { moveTo(x, y); first = 0; } |
---|
512 | else lineTo(x, y); |
---|
513 | } |
---|
514 | dr++; |
---|
515 | } |
---|
516 | } |
---|
517 | } |
---|
518 | } |
---|
519 | DCanvasWindow::paintWithClient((chsel < 0) ? 0 : client); |
---|
520 | setColor(0, 0, 0); |
---|
521 | int yline = size.y - 2; |
---|
522 | if (chsel >= 0) |
---|
523 | { |
---|
524 | if (ni) state = (float)ni->getState(chsel); |
---|
525 | yline -= textHeight(); |
---|
526 | int y = mapClientY(neurochart.mapData(state)); |
---|
527 | int x = size.x - 15 - 1; |
---|
528 | drawLine(1, yline, size.x - 2, yline); |
---|
529 | if (hold) |
---|
530 | { |
---|
531 | sprintf(t, "hold: %1.3g", state); |
---|
532 | fillRect(x, y - 1 - 5, 15, 3 + 5 + 5); |
---|
533 | setColor(255, 0, 0); |
---|
534 | fillRect(x + 2, y - 1, 15 - 2 - 2, 3); |
---|
535 | } |
---|
536 | else |
---|
537 | { |
---|
538 | sprintf(t, "signal: %1.3g", state); |
---|
539 | fillRect(x, y - 1, 15, 3); |
---|
540 | } |
---|
541 | drawAlignedText(size.x - textHeight(), yline, 1, t); |
---|
542 | } |
---|
543 | |
---|
544 | if ((chnum > 1) || (chnum2 > 0)) |
---|
545 | { |
---|
546 | if (chselecting) setColor(255, 255, 255); else setColor(0, 70, 0); |
---|
547 | if (chsel < 0) |
---|
548 | sprintf(t, "%c/%c", 'A' - chsel - 1, 'A' + chnum2 - 1); |
---|
549 | else |
---|
550 | sprintf(t, "%d/%d", chsel, chnum); |
---|
551 | moveTo(0, yline - textHeight()); |
---|
552 | chselwidth = textWidth(t); |
---|
553 | drawText(t, -1, getSize().x); |
---|
554 | } |
---|
555 | else |
---|
556 | chselwidth = 0; |
---|
557 | } |
---|
558 | |
---|
559 | void NeuroProbe::mouse(int x, int y, int b) |
---|
560 | { |
---|
561 | if (chselecting) |
---|
562 | { |
---|
563 | int ch = chsel0 + (x - chselx0) / 10; |
---|
564 | if (selectChannel(ch)) requestPaint(); |
---|
565 | b &= ~LeftButton; |
---|
566 | } |
---|
567 | DCanvasWindow::mouse(x, y, b); |
---|
568 | if (adjustingvalue) |
---|
569 | { |
---|
570 | double st = neurochart.unmapData(unmapClientY(y)); |
---|
571 | if (st<-1.0) st = -1.0; else if (st>1.0) st = 1.0; |
---|
572 | if (chsel == 0) |
---|
573 | link->n->state = st; |
---|
574 | else if (chsel >= 0) |
---|
575 | { |
---|
576 | NeuroImpl *ni = NeuroNetImpl::getImpl(link->n); |
---|
577 | if (ni) ni->setCurrentState(st, chsel); |
---|
578 | } |
---|
579 | requestPaint(); |
---|
580 | } |
---|
581 | } |
---|
582 | |
---|
583 | void NeuroProbe::mouseunclick(int x, int y, int b) |
---|
584 | { |
---|
585 | adjustingvalue = 0; |
---|
586 | chselecting = 0; |
---|
587 | DCanvasWindow::mouseunclick(x, y, b); |
---|
588 | } |
---|
589 | |
---|
590 | bool NeuroProbe::insideChSelector(int x, int y) |
---|
591 | { |
---|
592 | if ((x > 0) && (x < chselwidth)) |
---|
593 | { |
---|
594 | int sy = size.y; |
---|
595 | if (chsel >= 0) sy -= textHeight(); |
---|
596 | return ((y<sy) && (y>(sy - textHeight()))); |
---|
597 | } |
---|
598 | return 0; |
---|
599 | } |
---|
600 | |
---|
601 | int NeuroProbe::mouseclick(int x, int y, int b) |
---|
602 | { |
---|
603 | if ((b & LeftButton) && insideChSelector(x, y)) |
---|
604 | { |
---|
605 | chselx0 = x; chsel0 = chsel; |
---|
606 | chselecting = 1; |
---|
607 | requestPaint(); |
---|
608 | return LeftButton; |
---|
609 | } |
---|
610 | int ret = DCanvasWindow::mouseclick(x, y, b); |
---|
611 | if (ret) |
---|
612 | { |
---|
613 | link->diagram.selection.set(Model::neuroToMap(link->n->refno)); |
---|
614 | return ret; |
---|
615 | } |
---|
616 | if (b & LeftButton) |
---|
617 | { |
---|
618 | if (x > size.x - 16) |
---|
619 | { |
---|
620 | link->n->flags |= Neuro::HoldState; |
---|
621 | holdismine = 1; |
---|
622 | adjustingvalue = 1; |
---|
623 | mouse(x, y, b); |
---|
624 | return LeftButton; |
---|
625 | } |
---|
626 | else if (y > size.y - 16) |
---|
627 | { |
---|
628 | link->n->flags ^= Neuro::HoldState; |
---|
629 | holdismine = ((link->n->flags&Neuro::HoldState) != 0); |
---|
630 | requestPaint(); |
---|
631 | return LeftButton; |
---|
632 | } |
---|
633 | } |
---|
634 | return 0; |
---|
635 | } |
---|
636 | |
---|
637 | SString NeuroProbe::hint(int x, int y) |
---|
638 | { |
---|
639 | if ((chsel >= 0) && (x<size.x - 16) && (y>size.y - 16)) |
---|
640 | return SString((link->n->flags&Neuro::HoldState) ? "Click to release" : "Click to hold"); |
---|
641 | else if (insideChSelector(x, y)) |
---|
642 | return SString::sprintf("channel %d of %d (click and drag to switch channels)", chsel, chnum); |
---|
643 | return DCanvasWindow::hint(x, y); |
---|
644 | } |
---|
645 | |
---|
646 | /** @return true == channel changed */ |
---|
647 | bool NeuroProbe::selectChannel(int ch) |
---|
648 | { |
---|
649 | if (ch < -chnum2) ch = -chnum2; else if (ch >= chnum) ch = chnum - 1; |
---|
650 | if (ch == chsel) return false; |
---|
651 | chsel = ch; |
---|
652 | neurochart.data.clear(); |
---|
653 | return true; |
---|
654 | } |
---|
655 | |
---|
656 | void NeuroProbe::updateChannelCount(NeuroImpl *ni) |
---|
657 | { |
---|
658 | if (!ni) return; |
---|
659 | chnum = ni->getChannelCount(); |
---|
660 | chnum2 = ni->getDrawingCount(); |
---|
661 | if (chsel >= chnum) selectChannel(chnum - 1); |
---|
662 | if (chsel < -chnum2) selectChannel(-chnum2); |
---|
663 | } |
---|
664 | |
---|
665 | void NeuroProbe::sampling() |
---|
666 | { |
---|
667 | NeuroImpl *ni = NeuroNetImpl::getImpl(link->n); |
---|
668 | updateChannelCount(ni); |
---|
669 | if (!chsel) |
---|
670 | neurochart.data += (float)(link->n->state); |
---|
671 | else |
---|
672 | neurochart.data += (float)(ni->getState(chsel)); |
---|
673 | whichdrawing = -1; |
---|
674 | } |
---|
675 | |
---|
676 | //// |
---|
677 | |
---|
678 | void NeuroDiagram::probeSampling(void*obj, long dummy) |
---|
679 | { |
---|
680 | FOREACH(NeuroProbe*,pr,probes) pr->sampling(); |
---|
681 | requestPaint(); |
---|
682 | } |
---|
683 | |
---|
684 | void NeuroDiagram::updatePlugin() |
---|
685 | { |
---|
686 | //int needplugin=(!probes)>0; |
---|
687 | bool needplugin = livewire; |
---|
688 | if (needplugin == pluginactive) return; |
---|
689 | if (needplugin) |
---|
690 | { |
---|
691 | if (!cr) return; |
---|
692 | sim = cr->group->getLibrary().sim; |
---|
693 | pluginnode = sim->l_plugin.add(STATRICKCALLBACK(this, &NeuroDiagram::probeSampling, 0)); |
---|
694 | } |
---|
695 | else |
---|
696 | sim->l_plugin.remove(pluginnode); |
---|
697 | pluginactive = needplugin; |
---|
698 | } |
---|
699 | |
---|
700 | ///////////// |
---|
701 | |
---|
702 | void NeuroDiagramSelection::updateSelection(MultiRange& newsel) |
---|
703 | { |
---|
704 | MultiRange added = getAdded(newsel); |
---|
705 | if (!added.isEmpty()) |
---|
706 | { |
---|
707 | added.shift(Model::mapToNeuro(0)); |
---|
708 | added.intersect(0, diagram.countNeurons() - 1); |
---|
709 | for (int i = 0; i < added.rangeCount(); i++) |
---|
710 | { |
---|
711 | const IRange &r = added.getRange(i); |
---|
712 | for (int j = r.begin; j <= r.end; j++) |
---|
713 | diagram.getNS(j)->selected = 1; |
---|
714 | } |
---|
715 | } |
---|
716 | MultiRange removed = getRemoved(newsel); |
---|
717 | if (!removed.isEmpty()) |
---|
718 | { |
---|
719 | removed.shift(Model::mapToNeuro(0)); |
---|
720 | removed.intersect(0, diagram.countNeurons() - 1); |
---|
721 | for (int i = 0; i < removed.rangeCount(); i++) |
---|
722 | { |
---|
723 | const IRange &r = removed.getRange(i); |
---|
724 | for (int j = r.begin; j <= r.end; j++) |
---|
725 | diagram.getNS(j)->selected = 0; |
---|
726 | } |
---|
727 | } |
---|
728 | if (!diagram.indestructor) diagram.requestPaint(); |
---|
729 | } |
---|