[151] | 1 | // This file is a part of the Framsticks GDK. |
---|
[197] | 2 | // Copyright (C) 1999-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
[151] | 3 | // Refer to http://www.framsticks.com/ for further information. |
---|
| 4 | |
---|
[147] | 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); |
---|
[151] | 93 | FOREACH(NeuroProbe*,pr,probes) |
---|
| 94 | delete pr; |
---|
[147] | 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 | |
---|
[162] | 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 | |
---|
[147] | 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 | { |
---|
[151] | 322 | ns2 = diagram.getNS(n->getInput(iw)->refno); // the other NeuroSymbol (our input will connect to its output) |
---|
[147] | 323 | |
---|
| 324 | int yw = inputY(iw); |
---|
[151] | 325 | int xw = yw / 4; // x coordinate of the first corner point, depends on yw to avoid overlapping between inputs |
---|
[147] | 326 | drawLine(size.x / 4, yw, xw, yw); // first horizontal segment (to the left) |
---|
| 327 | diagram.setWireColor(ns2->n->state, 0); |
---|
[213] | 328 | // linetype: 1 (default) - draw straight or U-shape depending on layout |
---|
| 329 | // 0 (debug option) - only draw straight lines |
---|
[147] | 330 | if ((diagram.linetype != 1) || (ns2->pos.x + ns2->size.x / 2 < pos.x)) |
---|
[151] | 331 | { // straight line to the other neuron's output (the signal goes forwards) |
---|
[147] | 332 | ns2->lineTo(ns2->size.x, ns2->size.y / 2); |
---|
| 333 | } |
---|
| 334 | else |
---|
[151] | 335 | { // make an U-shaped loop from 3 segments - vert/horiz/vert (the signal goes backwards) |
---|
[147] | 336 | int y2; |
---|
| 337 | int down; |
---|
| 338 | if (ns2 == this) down = (iw >= ((n->getInputCount()) / 2)); else down = (ns2->pos.y > (pos.y + size.y)); |
---|
| 339 | if (down) |
---|
| 340 | { |
---|
| 341 | y2 = (pos.y + size.y + (size.y - yw) / 3); |
---|
| 342 | } |
---|
| 343 | else |
---|
| 344 | { |
---|
| 345 | y2 = pos.y - yw / 3; |
---|
| 346 | if ((ns2->pos.y<pos.y) && (ns2->pos.y>(pos.y - ns2->size.y))) y2 -= pos.y - ns2->pos.y; |
---|
| 347 | } |
---|
| 348 | // note: "diagram" uses global coordinate system, so we add "pos" or "ns2->pos" to get NeuroSymbol's global positions |
---|
| 349 | diagram.lineTo(pos.x + xw, y2); |
---|
| 350 | diagram.lineTo(ns2->pos.x + ns2->size.x, y2); |
---|
| 351 | diagram.lineTo(ns2->pos.x + ns2->size.x, ns2->pos.y + ns2->size.y / 2); |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | } |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | void NeuroSymbol::mouse(int x, int y, int t) |
---|
| 358 | { |
---|
| 359 | if ((t & (LeftButton | ShiftButton)) == (LeftButton | ShiftButton)) |
---|
| 360 | { |
---|
| 361 | ScrollManager& sc = diagram.scroll; |
---|
| 362 | sc.setPos2(n->refno, pos.x + x - diagram.symboldragpos.x, pos.y + y - diagram.symboldragpos.y); |
---|
| 363 | sc.validate(); |
---|
| 364 | requestPaint(); |
---|
| 365 | } |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | int NeuroSymbol::mouseclick(int x, int y, int t) |
---|
| 369 | { |
---|
| 370 | if ((t & (LeftButton | DblClick)) == (LeftButton | DblClick)) |
---|
| 371 | { |
---|
| 372 | if (diagram.isLive()) |
---|
| 373 | diagram.addProbe(n->refno); |
---|
| 374 | else |
---|
| 375 | { |
---|
| 376 | if (diagram.warn_if_not_alive) |
---|
| 377 | { |
---|
| 378 | diagram.showing_not_alive_label = time(0) + 10; |
---|
| 379 | diagram.not_alive_location.x = pos.x + x; |
---|
| 380 | diagram.not_alive_location.y = pos.y + y; |
---|
| 381 | diagram.requestPaint(); |
---|
| 382 | } |
---|
| 383 | } |
---|
| 384 | return LeftButton | DblClick; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | if ((t & (LeftButton | ShiftButton)) == (LeftButton | ShiftButton)) |
---|
| 388 | { |
---|
| 389 | if (selected) |
---|
| 390 | diagram.selection.remove(Model::neuroToMap(n->refno)); |
---|
| 391 | else |
---|
| 392 | diagram.selection.add(Model::neuroToMap(n->refno)); |
---|
| 393 | diagram.symboldragpos.set(x, y); |
---|
| 394 | return LeftButton | ShiftButton; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | if (t & LeftButton) |
---|
| 398 | { |
---|
| 399 | diagram.selection.set(Model::neuroToMap(n->refno)); |
---|
| 400 | return LeftButton; |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | return 0; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | // coordinate y of i-th input |
---|
| 407 | int NeuroSymbol::inputY(int i) |
---|
| 408 | { |
---|
| 409 | return (1 + i)*size.y / ((n->getInputCount()) + 1); |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | SString NeuroSymbol::hint(int x, int y) |
---|
| 413 | { |
---|
| 414 | if ((y >= 0) && (y < size.y)) |
---|
| 415 | if (x<size.x / 4) |
---|
| 416 | { // inputs? |
---|
| 417 | if (n->getInputCount()>0) |
---|
| 418 | { |
---|
| 419 | int i = (y*n->getInputCount()) / size.y; |
---|
| 420 | double w; |
---|
| 421 | Neuro* target = n->getInput(i, w); |
---|
| 422 | if (target) |
---|
| 423 | { |
---|
| 424 | SString t = "connected to #"; |
---|
| 425 | t += SString::valueOf((int)target->refno); |
---|
| 426 | t += " - "; |
---|
| 427 | addNeuroDescription(t, target); |
---|
| 428 | // if (w!=1.0) |
---|
| 429 | { |
---|
| 430 | t += ", weight="; |
---|
| 431 | t += SString::valueOf(w); |
---|
| 432 | } |
---|
| 433 | return t; |
---|
| 434 | } |
---|
| 435 | } |
---|
| 436 | } |
---|
| 437 | return CanvasWindow::hint(x, y); |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | ///////////////////////// |
---|
| 441 | |
---|
| 442 | NeuroProbe::NeuroProbe(NeuroSymbol* ns) |
---|
| 443 | :DCanvasWindow(DCanvasWindow::Title + DCanvasWindow::Border + DCanvasWindow::Close + DCanvasWindow::Size, |
---|
| 444 | (const char*)ns->getLabel(), &neurochart, &neurochart) |
---|
| 445 | { |
---|
| 446 | holdismine = 0; |
---|
| 447 | drawing = 0; whichdrawing = -1; |
---|
| 448 | clientbordersset = 0; |
---|
| 449 | adjustingvalue = 0; |
---|
| 450 | link = ns; |
---|
| 451 | tooltip = SString("Probe for ") + ns->tooltip; |
---|
| 452 | setPos(ns->getPos().x, ns->getPos().y); |
---|
| 453 | neurochart.printMinMax(0); |
---|
| 454 | neurochart.data.setMinMax(-1, 1); |
---|
| 455 | chnum = 1; chnum2 = 0; chsel = 0; |
---|
| 456 | chselwidth = 0; |
---|
| 457 | chselecting = 0; |
---|
| 458 | updateChannelCount(NeuroNetImpl::getImpl(link->n)); |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | void NeuroProbe::onClose() |
---|
| 462 | { |
---|
| 463 | link->diagram.probes -= this; |
---|
| 464 | delete this; |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | NeuroProbe::~NeuroProbe() |
---|
| 468 | { |
---|
| 469 | if (holdismine) |
---|
| 470 | link->n->flags &= ~Neuro::HoldState; |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | void NeuroProbe::paint() |
---|
| 474 | { |
---|
| 475 | static char t[40]; |
---|
| 476 | if (!clientbordersset) |
---|
| 477 | { |
---|
| 478 | clientbordersset = 1; |
---|
| 479 | setClientBorder(0, 1, 16, textHeight() + 2); // don't use textheight outside paint/mouse events |
---|
| 480 | } |
---|
| 481 | int hold = link->n->flags & Neuro::HoldState; |
---|
| 482 | float state = (float)link->n->state; |
---|
| 483 | NeuroImpl *ni = 0; |
---|
| 484 | if (chsel != 0) |
---|
| 485 | { |
---|
| 486 | ni = NeuroNetImpl::getImpl(link->n); |
---|
| 487 | if (chsel < 0) |
---|
| 488 | { |
---|
| 489 | int dr = -chsel - 1; |
---|
| 490 | if (whichdrawing != dr) |
---|
| 491 | { |
---|
| 492 | drawing = ni->getDrawing(dr); |
---|
| 493 | whichdrawing = dr; |
---|
| 494 | } |
---|
| 495 | if (drawing) |
---|
| 496 | { |
---|
| 497 | int *dr = drawing; |
---|
| 498 | int w = size.x - 2, h = size.y - clienttop - clientbottom; |
---|
| 499 | int scale = min(w, h); |
---|
| 500 | int x0 = clienttop + leftborder + ((w > h) ? (w - h) / 2 : 0); |
---|
| 501 | int y0 = clientleft + topborder + ((h > w) ? (h - w) / 2 : 0); |
---|
| 502 | |
---|
| 503 | while (*dr != NeuroImpl::ENDDRAWING) |
---|
| 504 | { |
---|
| 505 | int first = 1; |
---|
| 506 | unsigned char r, g, b; |
---|
| 507 | FramDrawToolkit::splitRGB(*(dr++), r, g, b); |
---|
| 508 | setColor(r, g, b); |
---|
| 509 | while (*dr != NeuroImpl::ENDDRAWING) |
---|
| 510 | { |
---|
| 511 | int x = ((*(dr++))*scale) / (NeuroImpl::MAXDRAWINGXY + 1) + x0; |
---|
| 512 | int y = ((*(dr++))*scale) / (NeuroImpl::MAXDRAWINGXY + 1) + y0; |
---|
| 513 | if (first) { moveTo(x, y); first = 0; } |
---|
| 514 | else lineTo(x, y); |
---|
| 515 | } |
---|
| 516 | dr++; |
---|
| 517 | } |
---|
| 518 | } |
---|
| 519 | } |
---|
| 520 | } |
---|
| 521 | DCanvasWindow::paintWithClient((chsel < 0) ? 0 : client); |
---|
| 522 | setColor(0, 0, 0); |
---|
| 523 | int yline = size.y - 2; |
---|
| 524 | if (chsel >= 0) |
---|
| 525 | { |
---|
| 526 | if (ni) state = (float)ni->getState(chsel); |
---|
| 527 | yline -= textHeight(); |
---|
| 528 | int y = mapClientY(neurochart.mapData(state)); |
---|
| 529 | int x = size.x - 15 - 1; |
---|
| 530 | drawLine(1, yline, size.x - 2, yline); |
---|
| 531 | if (hold) |
---|
| 532 | { |
---|
| 533 | sprintf(t, "hold: %1.3g", state); |
---|
| 534 | fillRect(x, y - 1 - 5, 15, 3 + 5 + 5); |
---|
| 535 | setColor(255, 0, 0); |
---|
| 536 | fillRect(x + 2, y - 1, 15 - 2 - 2, 3); |
---|
| 537 | } |
---|
| 538 | else |
---|
| 539 | { |
---|
| 540 | sprintf(t, "signal: %1.3g", state); |
---|
| 541 | fillRect(x, y - 1, 15, 3); |
---|
| 542 | } |
---|
| 543 | drawAlignedText(size.x - textHeight(), yline, 1, t); |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | if ((chnum > 1) || (chnum2 > 0)) |
---|
| 547 | { |
---|
| 548 | if (chselecting) setColor(255, 255, 255); else setColor(0, 70, 0); |
---|
| 549 | if (chsel < 0) |
---|
| 550 | sprintf(t, "%c/%c", 'A' - chsel - 1, 'A' + chnum2 - 1); |
---|
| 551 | else |
---|
| 552 | sprintf(t, "%d/%d", chsel, chnum); |
---|
| 553 | moveTo(0, yline - textHeight()); |
---|
| 554 | chselwidth = textWidth(t); |
---|
| 555 | drawText(t, -1, getSize().x); |
---|
| 556 | } |
---|
| 557 | else |
---|
| 558 | chselwidth = 0; |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | void NeuroProbe::mouse(int x, int y, int b) |
---|
| 562 | { |
---|
| 563 | if (chselecting) |
---|
| 564 | { |
---|
| 565 | int ch = chsel0 + (x - chselx0) / 10; |
---|
| 566 | if (selectChannel(ch)) requestPaint(); |
---|
| 567 | b &= ~LeftButton; |
---|
| 568 | } |
---|
| 569 | DCanvasWindow::mouse(x, y, b); |
---|
| 570 | if (adjustingvalue) |
---|
| 571 | { |
---|
| 572 | double st = neurochart.unmapData(unmapClientY(y)); |
---|
| 573 | if (st<-1.0) st = -1.0; else if (st>1.0) st = 1.0; |
---|
| 574 | if (chsel == 0) |
---|
| 575 | link->n->state = st; |
---|
| 576 | else if (chsel >= 0) |
---|
| 577 | { |
---|
| 578 | NeuroImpl *ni = NeuroNetImpl::getImpl(link->n); |
---|
[151] | 579 | if (ni) ni->setCurrentState(st, chsel); |
---|
[147] | 580 | } |
---|
| 581 | requestPaint(); |
---|
| 582 | } |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | void NeuroProbe::mouseunclick(int x, int y, int b) |
---|
| 586 | { |
---|
| 587 | adjustingvalue = 0; |
---|
| 588 | chselecting = 0; |
---|
| 589 | DCanvasWindow::mouseunclick(x, y, b); |
---|
| 590 | } |
---|
| 591 | |
---|
| 592 | bool NeuroProbe::insideChSelector(int x, int y) |
---|
| 593 | { |
---|
| 594 | if ((x > 0) && (x < chselwidth)) |
---|
| 595 | { |
---|
| 596 | int sy = size.y; |
---|
| 597 | if (chsel >= 0) sy -= textHeight(); |
---|
| 598 | return ((y<sy) && (y>(sy - textHeight()))); |
---|
| 599 | } |
---|
| 600 | return 0; |
---|
| 601 | } |
---|
| 602 | |
---|
| 603 | int NeuroProbe::mouseclick(int x, int y, int b) |
---|
| 604 | { |
---|
| 605 | if ((b & LeftButton) && insideChSelector(x, y)) |
---|
| 606 | { |
---|
| 607 | chselx0 = x; chsel0 = chsel; |
---|
| 608 | chselecting = 1; |
---|
| 609 | requestPaint(); |
---|
| 610 | return LeftButton; |
---|
| 611 | } |
---|
| 612 | int ret = DCanvasWindow::mouseclick(x, y, b); |
---|
| 613 | if (ret) |
---|
| 614 | { |
---|
| 615 | link->diagram.selection.set(Model::neuroToMap(link->n->refno)); |
---|
| 616 | return ret; |
---|
| 617 | } |
---|
| 618 | if (b & LeftButton) |
---|
| 619 | { |
---|
| 620 | if (x > size.x - 16) |
---|
| 621 | { |
---|
| 622 | link->n->flags |= Neuro::HoldState; |
---|
| 623 | holdismine = 1; |
---|
| 624 | adjustingvalue = 1; |
---|
| 625 | mouse(x, y, b); |
---|
| 626 | return LeftButton; |
---|
| 627 | } |
---|
| 628 | else if (y > size.y - 16) |
---|
| 629 | { |
---|
| 630 | link->n->flags ^= Neuro::HoldState; |
---|
| 631 | holdismine = ((link->n->flags&Neuro::HoldState) != 0); |
---|
| 632 | requestPaint(); |
---|
| 633 | return LeftButton; |
---|
| 634 | } |
---|
| 635 | } |
---|
| 636 | return 0; |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | SString NeuroProbe::hint(int x, int y) |
---|
| 640 | { |
---|
| 641 | if ((chsel >= 0) && (x<size.x - 16) && (y>size.y - 16)) |
---|
| 642 | return SString((link->n->flags&Neuro::HoldState) ? "Click to release" : "Click to hold"); |
---|
| 643 | else if (insideChSelector(x, y)) |
---|
| 644 | return SString::sprintf("channel %d of %d (click and drag to switch channels)", chsel, chnum); |
---|
| 645 | return DCanvasWindow::hint(x, y); |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | /** @return true == channel changed */ |
---|
| 649 | bool NeuroProbe::selectChannel(int ch) |
---|
| 650 | { |
---|
| 651 | if (ch < -chnum2) ch = -chnum2; else if (ch >= chnum) ch = chnum - 1; |
---|
| 652 | if (ch == chsel) return false; |
---|
| 653 | chsel = ch; |
---|
| 654 | neurochart.data.clear(); |
---|
| 655 | return true; |
---|
| 656 | } |
---|
| 657 | |
---|
| 658 | void NeuroProbe::updateChannelCount(NeuroImpl *ni) |
---|
| 659 | { |
---|
| 660 | if (!ni) return; |
---|
| 661 | chnum = ni->getChannelCount(); |
---|
| 662 | chnum2 = ni->getDrawingCount(); |
---|
| 663 | if (chsel >= chnum) selectChannel(chnum - 1); |
---|
| 664 | if (chsel < -chnum2) selectChannel(-chnum2); |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | void NeuroProbe::sampling() |
---|
| 668 | { |
---|
| 669 | NeuroImpl *ni = NeuroNetImpl::getImpl(link->n); |
---|
| 670 | updateChannelCount(ni); |
---|
| 671 | if (!chsel) |
---|
| 672 | neurochart.data += (float)(link->n->state); |
---|
| 673 | else |
---|
| 674 | neurochart.data += (float)(ni->getState(chsel)); |
---|
| 675 | whichdrawing = -1; |
---|
| 676 | } |
---|
| 677 | |
---|
| 678 | //// |
---|
| 679 | |
---|
| 680 | void NeuroDiagram::probeSampling(void*obj, long dummy) |
---|
| 681 | { |
---|
[151] | 682 | FOREACH(NeuroProbe*,pr,probes) pr->sampling(); |
---|
[147] | 683 | requestPaint(); |
---|
| 684 | } |
---|
| 685 | |
---|
| 686 | void NeuroDiagram::updatePlugin() |
---|
| 687 | { |
---|
| 688 | //int needplugin=(!probes)>0; |
---|
| 689 | bool needplugin = livewire; |
---|
| 690 | if (needplugin == pluginactive) return; |
---|
| 691 | if (needplugin) |
---|
| 692 | { |
---|
| 693 | if (!cr) return; |
---|
| 694 | sim = cr->group->getLibrary().sim; |
---|
| 695 | pluginnode = sim->l_plugin.add(STATRICKCALLBACK(this, &NeuroDiagram::probeSampling, 0)); |
---|
| 696 | } |
---|
| 697 | else |
---|
| 698 | sim->l_plugin.remove(pluginnode); |
---|
| 699 | pluginactive = needplugin; |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | ///////////// |
---|
| 703 | |
---|
| 704 | void NeuroDiagramSelection::updateSelection(MultiRange& newsel) |
---|
| 705 | { |
---|
| 706 | MultiRange added = getAdded(newsel); |
---|
| 707 | if (!added.isEmpty()) |
---|
| 708 | { |
---|
| 709 | added.shift(Model::mapToNeuro(0)); |
---|
| 710 | added.intersect(0, diagram.countNeurons() - 1); |
---|
| 711 | for (int i = 0; i < added.rangeCount(); i++) |
---|
| 712 | { |
---|
| 713 | const IRange &r = added.getRange(i); |
---|
| 714 | for (int j = r.begin; j <= r.end; j++) |
---|
| 715 | diagram.getNS(j)->selected = 1; |
---|
| 716 | } |
---|
| 717 | } |
---|
| 718 | MultiRange removed = getRemoved(newsel); |
---|
| 719 | if (!removed.isEmpty()) |
---|
| 720 | { |
---|
| 721 | removed.shift(Model::mapToNeuro(0)); |
---|
| 722 | removed.intersect(0, diagram.countNeurons() - 1); |
---|
| 723 | for (int i = 0; i < removed.rangeCount(); i++) |
---|
| 724 | { |
---|
| 725 | const IRange &r = removed.getRange(i); |
---|
| 726 | for (int j = r.begin; j <= r.end; j++) |
---|
| 727 | diagram.getNS(j)->selected = 0; |
---|
| 728 | } |
---|
| 729 | } |
---|
| 730 | if (!diagram.indestructor) diagram.requestPaint(); |
---|
| 731 | } |
---|