1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
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 | requestPaint(); |
---|
184 | } |
---|
185 | |
---|
186 | void NeuroDiagram::paint() |
---|
187 | { |
---|
188 | if (drawbackground) |
---|
189 | { |
---|
190 | setColor(ColorDefs::neurobackground); |
---|
191 | clear(); |
---|
192 | } |
---|
193 | |
---|
194 | if (countNeurons() > 0) |
---|
195 | { |
---|
196 | CanvasWindowContainer::paint(); |
---|
197 | } |
---|
198 | else |
---|
199 | { |
---|
200 | setColor(ColorDefs::neuroneuron); |
---|
201 | drawAlignedText(size.x / 2, (size.y - textHeight()) / 2, 0, "[No neural network]"); |
---|
202 | } |
---|
203 | |
---|
204 | if (showing_not_alive_label) |
---|
205 | { |
---|
206 | if (time(0) > showing_not_alive_label) |
---|
207 | showing_not_alive_label = 0; |
---|
208 | else |
---|
209 | { |
---|
210 | setColor(0, 0, 0); |
---|
211 | drawAlignedText(not_alive_location.x, not_alive_location.y, 0, "select a creature"); |
---|
212 | drawAlignedText(not_alive_location.x, not_alive_location.y + textHeight(), 0, "to probe neurons"); |
---|
213 | } |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | void NeuroDiagram::resize(int w, int h) |
---|
218 | { |
---|
219 | CanvasWindowContainer::resize(w, h); |
---|
220 | scroll.autoZoom(); |
---|
221 | } |
---|
222 | |
---|
223 | int NeuroDiagram::countNeurons() |
---|
224 | { |
---|
225 | return scroll.count(); |
---|
226 | } |
---|
227 | |
---|
228 | void NeuroDiagram::addProbe(int i) |
---|
229 | { |
---|
230 | if (i >= countNeurons()) return; |
---|
231 | NeuroProbe *probe = new NeuroProbe(getNS(i)); |
---|
232 | probes += (void*)probe; |
---|
233 | add(probe); |
---|
234 | updatePlugin(); |
---|
235 | requestPaint(); |
---|
236 | } |
---|
237 | |
---|
238 | void NeuroDiagram::onKill(void*obj, intptr_t dummy) |
---|
239 | { |
---|
240 | show(0); |
---|
241 | } |
---|
242 | |
---|
243 | /////////////////////////// |
---|
244 | |
---|
245 | NeuroSymbol::NeuroSymbol(NeuroDiagram &nd, Neuro * _n) |
---|
246 | :selected(0), n(_n), diagram(nd), FramDrawToolkit(nd.getColorDefs()) |
---|
247 | { |
---|
248 | tooltip = "#"; |
---|
249 | tooltip += SString::valueOf((int)n->refno); |
---|
250 | tooltip += " - "; |
---|
251 | label = tooltip; |
---|
252 | addNeuroDescription(tooltip, n); |
---|
253 | label += n->getClassName(); |
---|
254 | if (n->getClassParams().len()) |
---|
255 | { |
---|
256 | tooltip += "\n"; tooltip += n->getClassParams(); |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | void NeuroSymbol::paint() |
---|
261 | { |
---|
262 | if (selected) |
---|
263 | { |
---|
264 | setColor(ColorDefs::neuroselection); |
---|
265 | fillRect(0, 0, size.x, size.y); |
---|
266 | } |
---|
267 | diagram.setClip(); |
---|
268 | diagram.setColor(ColorDefs::neuroneuron); |
---|
269 | drawNeuroSymbol(this, n->getClass(), 0, 0, size.x, size.y); |
---|
270 | |
---|
271 | if (size.y > 4 * textHeight()) |
---|
272 | { |
---|
273 | const char* t = label.c_str(); |
---|
274 | setColor(ColorDefs::neurosymbol); |
---|
275 | drawAlignedText(size.x / 2, size.y - textHeight(), 0, t); |
---|
276 | |
---|
277 | NeuroImpl *ni = NeuroNetImpl::getImpl(n); |
---|
278 | if (ni && (ni->getChannelCount() > 1)) |
---|
279 | { |
---|
280 | drawLine(size.x - size.x / 16, size.y / 2 - size.y / 16, |
---|
281 | size.x - size.x / 8, size.y / 2 + size.y / 16); |
---|
282 | char t[20]; |
---|
283 | sprintf(t, "%d", ni->getChannelCount()); |
---|
284 | moveTo(size.x, size.y / 2 - textHeight()); |
---|
285 | drawText(t); |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | /* |
---|
290 | |
---|
291 | NeuroDiagram |
---|
292 | *........................................ |
---|
293 | . . |
---|
294 | . NeuroSymbol . |
---|
295 | . (pos.x,pos.y)-*......... . |
---|
296 | . . |\ . ^ s . |
---|
297 | . ..._____._| \ . | i . |
---|
298 | . . | \___. | z . |
---|
299 | . __._| / . | e . |
---|
300 | . | . | / . | . . |
---|
301 | . | . |/ . | y . |
---|
302 | . | .......... v . |
---|
303 | . | <--------> . |
---|
304 | . .......... | size.x . |
---|
305 | . . |\ . | . |
---|
306 | . __._| \ . | . |
---|
307 | . . | \___._| . |
---|
308 | . __._| / . . |
---|
309 | . | . | / . . |
---|
310 | . | . |/ . . |
---|
311 | . | .......... . |
---|
312 | . | . |
---|
313 | . |________________... . |
---|
314 | ......................................... |
---|
315 | |
---|
316 | */ |
---|
317 | |
---|
318 | // NeuroSymbol is also responsible for drawing connection lines from its inputs to other NeuroSymbols' outputs |
---|
319 | NeuroSymbol *ns2; |
---|
320 | if (!diagram.isLive()) |
---|
321 | diagram.setColor(ColorDefs::neurolink); |
---|
322 | for (int iw = 0; iw < n->getInputCount(); iw++) |
---|
323 | { |
---|
324 | ns2 = diagram.getNS(n->getInput(iw)->refno); // the other NeuroSymbol (our input will connect to its output) |
---|
325 | |
---|
326 | int yw = inputY(iw); |
---|
327 | int xw = yw / 4; // x coordinate of the first corner point, depends on yw to avoid overlapping between inputs |
---|
328 | drawLine(size.x / 4, yw, xw, yw); // first horizontal segment (to the left) |
---|
329 | if (diagram.isLive()) |
---|
330 | diagram.setWireColor(ns2->n->state, 0); |
---|
331 | // linetype: 1 (default) - draw straight or U-shape depending on layout |
---|
332 | // 0 (debug option) - only draw straight lines |
---|
333 | if ((diagram.linetype != 1) || (ns2->pos.x + ns2->size.x / 2 < pos.x)) |
---|
334 | { // straight line to the other neuron's output (the signal goes forwards) |
---|
335 | ns2->lineTo(ns2->size.x, ns2->size.y / 2); |
---|
336 | } |
---|
337 | else |
---|
338 | { // make an U-shaped loop from 3 segments - vert/horiz/vert (the signal goes backwards) |
---|
339 | int y2; |
---|
340 | int down; |
---|
341 | if (ns2 == this) down = (iw >= ((n->getInputCount()) / 2)); else down = (ns2->pos.y > (pos.y + size.y)); |
---|
342 | if (down) |
---|
343 | { |
---|
344 | y2 = (pos.y + size.y + (size.y - yw) / 3); |
---|
345 | } |
---|
346 | else |
---|
347 | { |
---|
348 | y2 = pos.y - yw / 3; |
---|
349 | if ((ns2->pos.y<pos.y) && (ns2->pos.y>(pos.y - ns2->size.y))) y2 -= pos.y - ns2->pos.y; |
---|
350 | } |
---|
351 | // note: "diagram" uses global coordinate system, so we add "pos" or "ns2->pos" to get NeuroSymbol's global positions |
---|
352 | diagram.lineTo(pos.x + xw, y2); |
---|
353 | diagram.lineTo(ns2->pos.x + ns2->size.x, y2); |
---|
354 | diagram.lineTo(ns2->pos.x + ns2->size.x, ns2->pos.y + ns2->size.y / 2); |
---|
355 | } |
---|
356 | |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | void NeuroSymbol::mouse(int x, int y, int t) |
---|
361 | { |
---|
362 | if ((t & (LeftButton | ShiftButton)) == (LeftButton | ShiftButton)) |
---|
363 | { |
---|
364 | ScrollManager& sc = diagram.scroll; |
---|
365 | sc.setPos2(n->refno, pos.x + x - diagram.symboldragpos.x, pos.y + y - diagram.symboldragpos.y); |
---|
366 | sc.validate(); |
---|
367 | requestPaint(); |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | int NeuroSymbol::mouseclick(int x, int y, int t) |
---|
372 | { |
---|
373 | if ((t & (LeftButton | DblClick)) == (LeftButton | DblClick)) |
---|
374 | { |
---|
375 | if (diagram.isLive()) |
---|
376 | diagram.addProbe(n->refno); |
---|
377 | else |
---|
378 | { |
---|
379 | if (diagram.warn_if_not_alive) |
---|
380 | { |
---|
381 | diagram.showing_not_alive_label = time(0) + 10; |
---|
382 | diagram.not_alive_location.x = pos.x + x; |
---|
383 | diagram.not_alive_location.y = pos.y + y; |
---|
384 | diagram.requestPaint(); |
---|
385 | } |
---|
386 | } |
---|
387 | return LeftButton | DblClick; |
---|
388 | } |
---|
389 | |
---|
390 | if ((t & (LeftButton | ShiftButton)) == (LeftButton | ShiftButton)) |
---|
391 | { |
---|
392 | if (selected) |
---|
393 | diagram.selection.remove(Model::neuroToMap(n->refno)); |
---|
394 | else |
---|
395 | diagram.selection.add(Model::neuroToMap(n->refno)); |
---|
396 | diagram.symboldragpos.set(x, y); |
---|
397 | return LeftButton | ShiftButton; |
---|
398 | } |
---|
399 | |
---|
400 | if (t & LeftButton) |
---|
401 | { |
---|
402 | diagram.selection.set(Model::neuroToMap(n->refno)); |
---|
403 | return LeftButton; |
---|
404 | } |
---|
405 | |
---|
406 | return 0; |
---|
407 | } |
---|
408 | |
---|
409 | // coordinate y of i-th input |
---|
410 | int NeuroSymbol::inputY(int i) |
---|
411 | { |
---|
412 | return (1 + i)*size.y / ((n->getInputCount()) + 1); |
---|
413 | } |
---|
414 | |
---|
415 | SString NeuroSymbol::hint(int x, int y) |
---|
416 | { |
---|
417 | if ((y >= 0) && (y < size.y)) |
---|
418 | if (x<size.x / 4) |
---|
419 | { // inputs? |
---|
420 | if (n->getInputCount()>0) |
---|
421 | { |
---|
422 | int i = (y*n->getInputCount()) / size.y; |
---|
423 | double w; |
---|
424 | Neuro* target = n->getInput(i, w); |
---|
425 | if (target) |
---|
426 | { |
---|
427 | SString t = "connected to #"; |
---|
428 | t += SString::valueOf((int)target->refno); |
---|
429 | t += " - "; |
---|
430 | addNeuroDescription(t, target); |
---|
431 | // if (w!=1.0) |
---|
432 | { |
---|
433 | t += ", weight="; |
---|
434 | t += SString::valueOf(w); |
---|
435 | } |
---|
436 | return t; |
---|
437 | } |
---|
438 | } |
---|
439 | } |
---|
440 | return CanvasWindow::hint(x, y); |
---|
441 | } |
---|
442 | |
---|
443 | ///////////////////////// |
---|
444 | |
---|
445 | NeuroProbe::NeuroProbe(NeuroSymbol* ns) |
---|
446 | :DCanvasWindow(DCanvasWindow::Title + DCanvasWindow::Border + DCanvasWindow::Close + DCanvasWindow::Size, |
---|
447 | ns->getLabel().c_str(), &neurochart, &neurochart) |
---|
448 | { |
---|
449 | holdismine = 0; |
---|
450 | drawing = 0; whichdrawing = -1; |
---|
451 | clientbordersset = 0; |
---|
452 | adjustingvalue = 0; |
---|
453 | link = ns; |
---|
454 | tooltip = SString("Probe for ") + ns->tooltip; |
---|
455 | setPos(ns->getPos().x, ns->getPos().y); |
---|
456 | neurochart.printMinMax(0); |
---|
457 | neurochart.data.setMinMax(-1, 1); |
---|
458 | chnum = 1; chnum2 = 0; chsel = 0; |
---|
459 | chselwidth = 0; |
---|
460 | chselecting = 0; |
---|
461 | updateChannelCount(NeuroNetImpl::getImpl(link->n)); |
---|
462 | } |
---|
463 | |
---|
464 | void NeuroProbe::onClose() |
---|
465 | { |
---|
466 | link->diagram.probes -= this; |
---|
467 | delete this; |
---|
468 | } |
---|
469 | |
---|
470 | NeuroProbe::~NeuroProbe() |
---|
471 | { |
---|
472 | if (holdismine) |
---|
473 | link->n->flags &= ~Neuro::HoldState; |
---|
474 | } |
---|
475 | |
---|
476 | void NeuroProbe::paint() |
---|
477 | { |
---|
478 | static char t[40]; |
---|
479 | if (!clientbordersset) |
---|
480 | { |
---|
481 | clientbordersset = 1; |
---|
482 | setClientBorder(0, 1, 16, textHeight() + 2); // don't use textheight outside paint/mouse events |
---|
483 | } |
---|
484 | int hold = link->n->flags & Neuro::HoldState; |
---|
485 | float state = (float)link->n->state; |
---|
486 | NeuroImpl *ni = 0; |
---|
487 | if (chsel != 0) |
---|
488 | { |
---|
489 | ni = NeuroNetImpl::getImpl(link->n); |
---|
490 | if (chsel < 0) |
---|
491 | { |
---|
492 | int dr = -chsel - 1; |
---|
493 | if (whichdrawing != dr) |
---|
494 | { |
---|
495 | drawing = ni->getDrawing(dr); |
---|
496 | whichdrawing = dr; |
---|
497 | } |
---|
498 | if (drawing) |
---|
499 | { |
---|
500 | int *dr = drawing; |
---|
501 | int w = size.x - 2, h = size.y - clienttop - clientbottom; |
---|
502 | int scale = min(w, h); |
---|
503 | int x0 = clienttop + leftborder + ((w > h) ? (w - h) / 2 : 0); |
---|
504 | int y0 = clientleft + topborder + ((h > w) ? (h - w) / 2 : 0); |
---|
505 | |
---|
506 | while (*dr != NeuroImpl::ENDDRAWING) |
---|
507 | { |
---|
508 | int first = 1; |
---|
509 | unsigned char r, g, b; |
---|
510 | FramDrawToolkit::splitRGB(*(dr++), r, g, b); |
---|
511 | setColor(r, g, b); |
---|
512 | while (*dr != NeuroImpl::ENDDRAWING) |
---|
513 | { |
---|
514 | int x = ((*(dr++))*scale) / (NeuroImpl::MAXDRAWINGXY + 1) + x0; |
---|
515 | int y = ((*(dr++))*scale) / (NeuroImpl::MAXDRAWINGXY + 1) + y0; |
---|
516 | if (first) { moveTo(x, y); first = 0; } |
---|
517 | else lineTo(x, y); |
---|
518 | } |
---|
519 | dr++; |
---|
520 | } |
---|
521 | } |
---|
522 | } |
---|
523 | } |
---|
524 | DCanvasWindow::paintWithClient((chsel < 0) ? 0 : client); |
---|
525 | setColor(0, 0, 0); |
---|
526 | int yline = size.y - 2; |
---|
527 | if (chsel >= 0) |
---|
528 | { |
---|
529 | if (ni) state = (float)ni->getState(chsel); |
---|
530 | yline -= textHeight(); |
---|
531 | int y = mapClientY(neurochart.mapData(state)); |
---|
532 | int x = size.x - 15 - 1; |
---|
533 | drawLine(1, yline, size.x - 2, yline); |
---|
534 | if (hold) |
---|
535 | { |
---|
536 | sprintf(t, "hold: %1.3g", state); |
---|
537 | fillRect(x, y - 1 - 5, 15, 3 + 5 + 5); |
---|
538 | setColor(255, 0, 0); |
---|
539 | fillRect(x + 2, y - 1, 15 - 2 - 2, 3); |
---|
540 | } |
---|
541 | else |
---|
542 | { |
---|
543 | sprintf(t, "signal: %1.3g", state); |
---|
544 | fillRect(x, y - 1, 15, 3); |
---|
545 | } |
---|
546 | drawAlignedText(size.x - textHeight(), yline, 1, t); |
---|
547 | } |
---|
548 | |
---|
549 | if ((chnum > 1) || (chnum2 > 0)) |
---|
550 | { |
---|
551 | if (chselecting) setColor(255, 255, 255); else setColor(0, 70, 0); |
---|
552 | if (chsel < 0) |
---|
553 | sprintf(t, "%c/%c", 'A' - chsel - 1, 'A' + chnum2 - 1); |
---|
554 | else |
---|
555 | sprintf(t, "%d/%d", chsel, chnum); |
---|
556 | moveTo(0, yline - textHeight()); |
---|
557 | chselwidth = textWidth(t); |
---|
558 | drawText(t, -1, getSize().x); |
---|
559 | } |
---|
560 | else |
---|
561 | chselwidth = 0; |
---|
562 | } |
---|
563 | |
---|
564 | void NeuroProbe::mouse(int x, int y, int b) |
---|
565 | { |
---|
566 | if (chselecting) |
---|
567 | { |
---|
568 | int ch = chsel0 + (x - chselx0) / 10; |
---|
569 | if (selectChannel(ch)) requestPaint(); |
---|
570 | b &= ~LeftButton; |
---|
571 | } |
---|
572 | DCanvasWindow::mouse(x, y, b); |
---|
573 | if (adjustingvalue) |
---|
574 | { |
---|
575 | double st = neurochart.unmapData(unmapClientY(y)); |
---|
576 | if (st<-1.0) st = -1.0; else if (st>1.0) st = 1.0; |
---|
577 | if (chsel == 0) |
---|
578 | link->n->state = st; |
---|
579 | else if (chsel >= 0) |
---|
580 | { |
---|
581 | NeuroImpl *ni = NeuroNetImpl::getImpl(link->n); |
---|
582 | if (ni) ni->setCurrentState(st, chsel); |
---|
583 | } |
---|
584 | requestPaint(); |
---|
585 | } |
---|
586 | } |
---|
587 | |
---|
588 | void NeuroProbe::mouseunclick(int x, int y, int b) |
---|
589 | { |
---|
590 | adjustingvalue = 0; |
---|
591 | chselecting = 0; |
---|
592 | DCanvasWindow::mouseunclick(x, y, b); |
---|
593 | } |
---|
594 | |
---|
595 | bool NeuroProbe::insideChSelector(int x, int y) |
---|
596 | { |
---|
597 | if ((x > 0) && (x < chselwidth)) |
---|
598 | { |
---|
599 | int sy = size.y; |
---|
600 | if (chsel >= 0) sy -= textHeight(); |
---|
601 | return ((y<sy) && (y>(sy - textHeight()))); |
---|
602 | } |
---|
603 | return 0; |
---|
604 | } |
---|
605 | |
---|
606 | int NeuroProbe::mouseclick(int x, int y, int b) |
---|
607 | { |
---|
608 | if ((b & LeftButton) && insideChSelector(x, y)) |
---|
609 | { |
---|
610 | chselx0 = x; chsel0 = chsel; |
---|
611 | chselecting = 1; |
---|
612 | requestPaint(); |
---|
613 | return LeftButton; |
---|
614 | } |
---|
615 | int ret = DCanvasWindow::mouseclick(x, y, b); |
---|
616 | if (ret) |
---|
617 | { |
---|
618 | link->diagram.selection.set(Model::neuroToMap(link->n->refno)); |
---|
619 | return ret; |
---|
620 | } |
---|
621 | if (b & LeftButton) |
---|
622 | { |
---|
623 | if (x > size.x - 16) |
---|
624 | { |
---|
625 | link->n->flags |= Neuro::HoldState; |
---|
626 | holdismine = 1; |
---|
627 | adjustingvalue = 1; |
---|
628 | mouse(x, y, b); |
---|
629 | return LeftButton; |
---|
630 | } |
---|
631 | else if (y > size.y - 16) |
---|
632 | { |
---|
633 | link->n->flags ^= Neuro::HoldState; |
---|
634 | holdismine = ((link->n->flags&Neuro::HoldState) != 0); |
---|
635 | requestPaint(); |
---|
636 | return LeftButton; |
---|
637 | } |
---|
638 | } |
---|
639 | return 0; |
---|
640 | } |
---|
641 | |
---|
642 | SString NeuroProbe::hint(int x, int y) |
---|
643 | { |
---|
644 | if ((chsel >= 0) && (x<size.x - 16) && (y>size.y - 16)) |
---|
645 | return SString((link->n->flags&Neuro::HoldState) ? "Click to release" : "Click to hold"); |
---|
646 | else if (insideChSelector(x, y)) |
---|
647 | return SString::sprintf("channel %d of %d (click and drag to switch channels)", chsel, chnum); |
---|
648 | return DCanvasWindow::hint(x, y); |
---|
649 | } |
---|
650 | |
---|
651 | /** @return true == channel changed */ |
---|
652 | bool NeuroProbe::selectChannel(int ch) |
---|
653 | { |
---|
654 | if (ch < -chnum2) ch = -chnum2; else if (ch >= chnum) ch = chnum - 1; |
---|
655 | if (ch == chsel) return false; |
---|
656 | chsel = ch; |
---|
657 | neurochart.data.clear(); |
---|
658 | return true; |
---|
659 | } |
---|
660 | |
---|
661 | void NeuroProbe::updateChannelCount(NeuroImpl *ni) |
---|
662 | { |
---|
663 | if (!ni) return; |
---|
664 | chnum = ni->getChannelCount(); |
---|
665 | chnum2 = ni->getDrawingCount(); |
---|
666 | if (chsel >= chnum) selectChannel(chnum - 1); |
---|
667 | if (chsel < -chnum2) selectChannel(-chnum2); |
---|
668 | } |
---|
669 | |
---|
670 | void NeuroProbe::sampling() |
---|
671 | { |
---|
672 | NeuroImpl *ni = NeuroNetImpl::getImpl(link->n); |
---|
673 | updateChannelCount(ni); |
---|
674 | if (!chsel) |
---|
675 | neurochart.data += (float)(link->n->state); |
---|
676 | else |
---|
677 | neurochart.data += (float)(ni->getState(chsel)); |
---|
678 | whichdrawing = -1; |
---|
679 | } |
---|
680 | |
---|
681 | //// |
---|
682 | |
---|
683 | void NeuroDiagram::probeSampling(void*obj, intptr_t dummy) |
---|
684 | { |
---|
685 | FOREACH(NeuroProbe*,pr,probes) pr->sampling(); |
---|
686 | requestPaint(); |
---|
687 | } |
---|
688 | |
---|
689 | void NeuroDiagram::updatePlugin() |
---|
690 | { |
---|
691 | //int needplugin=(!probes)>0; |
---|
692 | bool needplugin = livewire; |
---|
693 | if (needplugin == pluginactive) return; |
---|
694 | if (needplugin) |
---|
695 | { |
---|
696 | if (!cr) return; |
---|
697 | sim = cr->group->getLibrary().sim; |
---|
698 | pluginnode = sim->l_plugin.add(STATRICKCALLBACK(this, &NeuroDiagram::probeSampling, 0)); |
---|
699 | } |
---|
700 | else |
---|
701 | sim->l_plugin.remove(pluginnode); |
---|
702 | pluginactive = needplugin; |
---|
703 | } |
---|
704 | |
---|
705 | ///////////// |
---|
706 | |
---|
707 | void NeuroDiagramSelection::updateSelection(MultiRange& newsel) |
---|
708 | { |
---|
709 | MultiRange added = getAdded(newsel); |
---|
710 | if (!added.isEmpty()) |
---|
711 | { |
---|
712 | added.shift(Model::mapToNeuro(0)); |
---|
713 | added.intersect(0, diagram.countNeurons() - 1); |
---|
714 | for (int i = 0; i < added.rangeCount(); i++) |
---|
715 | { |
---|
716 | const IRange &r = added.getRange(i); |
---|
717 | for (int j = r.begin; j <= r.end; j++) |
---|
718 | diagram.getNS(j)->selected = 1; |
---|
719 | } |
---|
720 | } |
---|
721 | MultiRange removed = getRemoved(newsel); |
---|
722 | if (!removed.isEmpty()) |
---|
723 | { |
---|
724 | removed.shift(Model::mapToNeuro(0)); |
---|
725 | removed.intersect(0, diagram.countNeurons() - 1); |
---|
726 | for (int i = 0; i < removed.rangeCount(); i++) |
---|
727 | { |
---|
728 | const IRange &r = removed.getRange(i); |
---|
729 | for (int j = r.begin; j <= r.end; j++) |
---|
730 | diagram.getNS(j)->selected = 0; |
---|
731 | } |
---|
732 | } |
---|
733 | if (!diagram.indestructor) diagram.requestPaint(); |
---|
734 | } |
---|