-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI_SquareLatticeEditor.cpp
410 lines (356 loc) · 15.1 KB
/
GUI_SquareLatticeEditor.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* /////////////////////////////////////////////////////////////////////////////
NetSuite: NetClamp/NetSim/NetFit Neural Network Modeling and Dynamic Clamp Software
Copyright (C) 2011-2014 E. Brady Trexler, Ph.D.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Please direct correspondence to ebtrexler _at_ gothamsci _dot_ com
///////////////////////////////////////////////////////////////////////////// */
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "GUI_NetworkForm.h"
#include "GUI_SquareLatticeEditor.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GR32_Image"
#pragma resource "*.dfm"
#include "GR32_Polygons.hpp"
TGUI_SquareLatticeForm *GUI_SquareLatticeForm;
TGUI_SquareLatticeForm *GetSquareLatticeForm()
{
static TGUI_SquareLatticeForm *form = NULL;
if (!form) {
form = new TGUI_SquareLatticeForm(NULL);
}
return form;
}
//---------------------------------------------------------------------------
__fastcall TGUI_SquareLatticeForm::TGUI_SquareLatticeForm(TComponent* Owner)
: TForm(Owner)
{
NetworkHeight = 600;
NetworkWidth = 600;
ImageSize = 80;
Margin = ImageSize / 4;
// StretchFilter = sfNearest;
LastX = LastY = 0;
CellNumber = SynapseNumber = ElectrodeNumber = 0;
NetworkImage = new TBitmap32;
CellBmp = new TBitmap32;
SynapseBmpHORZ = new TBitmap32;
SynapseBmpVERT = new TBitmap32;
SynapseBmpTLBR = new TBitmap32;
SynapseBmpBLTR = new TBitmap32;
ElectrodeBmp = new TBitmap32;
CellHitTestBmp = new TBitmap32;
SynapseHitTestBmp = new TBitmap32;
ElectrodeHitTestBmp = new TBitmap32;
NetworkImage->SetSize(NetworkWidth, NetworkHeight);
// NetworkImage->StretchFilter = StretchFilter;
NetworkImage->DrawMode = dmOpaque;
NetworkImage->Clear(clWhite32);
NetworkPaintBox32->SetBounds(0,0,
std::min(NetworkImage->Width, Width),
std::min(NetworkImage->Height, Height));
NetworkPaintBox32->Buffer->SetSize(
std::min(NetworkImage->Width, Width),
std::min(NetworkImage->Height, Height));
TBitmap32 *temp = new TBitmap32;
// load cell image
temp->LoadFromResourceName((int)HInstance, L"BMP_CELL");
CellBmp->DrawMode = dmTransparent;
CellBmp->OuterColor = temp->Pixel[1][1];
CellBmp->SetSize(ImageSize, ImageSize);
CellBmp->Clear(clWhite32);
CellBmp->Draw(CellBmp->BoundsRect(),temp->BoundsRect(),temp);
// load 4 directional synapse images
temp->LoadFromResourceName((int)HInstance, L"BMP_synapseHORZ");
SynapseBmpHORZ->DrawMode = dmTransparent;
SynapseBmpHORZ->OuterColor = temp->Pixel[1][1];
SynapseBmpHORZ->SetSize(ImageSize, ImageSize);
SynapseBmpHORZ->Clear(clWhite32);
SynapseBmpHORZ->Draw(SynapseBmpHORZ->BoundsRect(),temp->BoundsRect(),temp);
temp->LoadFromResourceName((int)HInstance, L"BMP_synapseVERT");
SynapseBmpVERT->DrawMode = dmTransparent;
SynapseBmpVERT->OuterColor = temp->Pixel[1][1];
SynapseBmpVERT->SetSize(ImageSize, ImageSize);
SynapseBmpVERT->Clear(clWhite32);
SynapseBmpVERT->Draw(SynapseBmpVERT->BoundsRect(),temp->BoundsRect(),temp);
temp->LoadFromResourceName((int)HInstance, L"BMP_synapseTLBR");
SynapseBmpTLBR->DrawMode = dmTransparent;
SynapseBmpTLBR->OuterColor = temp->Pixel[1][1];
SynapseBmpTLBR->SetSize(ImageSize, ImageSize);
SynapseBmpTLBR->Clear(clWhite32);
SynapseBmpTLBR->Draw(SynapseBmpTLBR->BoundsRect(),temp->BoundsRect(),temp);
temp->LoadFromResourceName((int)HInstance, L"BMP_synapseBLTR");
SynapseBmpBLTR->DrawMode = dmTransparent;
SynapseBmpBLTR->OuterColor = temp->Pixel[1][1];
SynapseBmpBLTR->SetSize(ImageSize, ImageSize);
SynapseBmpBLTR->Clear(clWhite32);
SynapseBmpBLTR->Draw(SynapseBmpBLTR->BoundsRect(),temp->BoundsRect(),temp);
// load electrode image
temp->LoadFromResourceName((int)HInstance, L"BMP_ELECTRODE");
ElectrodeBmp->DrawMode = dmTransparent;
ElectrodeBmp->OuterColor = temp->Pixel[1][1];
ElectrodeBmp->SetSize(ImageSize, ImageSize);
ElectrodeBmp->Clear(clWhite32);
ElectrodeBmp->Draw(ElectrodeBmp->BoundsRect(),temp->BoundsRect(),temp);
// make cell hittest image
CellHitTestBmp->DrawMode = dmTransparent;
CellHitTestBmp->OuterColor = clWhite32;
CellHitTestBmp->SetSize(ImageSize, ImageSize);
CellHitTestBmp->Clear(clWhite32);
TArrayOfFixedPoint pts;
Circle(pts, ImageSize/2, ImageSize/2);
PolygonXS(CellHitTestBmp, pts, clTrRed32);
// make synapse hittest image
temp->LoadFromResourceName((int)HInstance, L"BMP_CHOOSESYNAPSE");
SynapseHitTestBmp->DrawMode = dmTransparent;
SynapseHitTestBmp->OuterColor = temp->Pixel[1][1];;
SynapseHitTestBmp->SetSize(ImageSize, ImageSize);
SynapseHitTestBmp->Clear(clWhite32);
SynapseHitTestBmp->Draw(SynapseHitTestBmp->BoundsRect(),temp->BoundsRect(),temp);
// make electrode hittest image
ElectrodeHitTestBmp->DrawMode = dmTransparent;
ElectrodeHitTestBmp->OuterColor = clWhite32;
ElectrodeHitTestBmp->SetSize(ImageSize, ImageSize);
ElectrodeHitTestBmp->Clear(clWhite32);
pts.Length = 3;
pts[0] = FixedPoint(0,ImageSize);
pts[1] = FixedPoint(ImageSize, ImageSize/2);
pts[2] = FixedPoint(ImageSize/2, 0);
PolygonXS(ElectrodeHitTestBmp, pts, clTrBlue32);
UpdateDisplay();
}
//---------------------------------------------------------------------------
void __fastcall TGUI_SquareLatticeForm::UpdateDisplay()
{
NetworkImage->Clear(clWhite32);
int X, X1, Y, Y1;
NetworkImage->Font->Height = 14;
NetworkImage->Font->Style = TFontStyles() << fsBold;
// draw cells
TCellsMapConstIterator c_iter;
TCellsMap const &cells = GetNet()->GetCells();
for (c_iter = cells.begin(); c_iter != cells.end(); ++c_iter) {
X = ((TRTBase *)c_iter->second.get())->GetX();
Y = ((TRTBase *)c_iter->second.get())->GetY();
UnicodeString name(((TRTBase *)c_iter->second.get())->Name().c_str());
NetworkImage->Draw(X - ImageSize/2, Y - ImageSize/2, CellBmp);
int th = NetworkImage->TextHeight(name)/2; // text height / 2 for centering
int te = NetworkImage->TextWidth(name)/2; // text width / 2 for centering
NetworkImage->Textout(X - te, Y - th, name);
}
// draw synapses
TSynapsesMapConstIterator s_iter;
TSynapsesMap const &synapses = GetNet()->GetSynapses();
for (s_iter = synapses.begin(); s_iter != synapses.end(); ++s_iter) {
X = ((TSynapse *)s_iter->second.get())->Pre()->GetX();
Y = ((TSynapse *)s_iter->second.get())->Pre()->GetY();
X1 = ((TSynapse *)s_iter->second.get())->Post()->GetX();
Y1 = ((TSynapse *)s_iter->second.get())->Post()->GetY();
UnicodeString name(((TRTBase *)s_iter->second.get())->Name().c_str());
int xdiff = (X - X1);
int ydiff = (Y - Y1);
if (xdiff == 0) {
ChooseBmp = SynapseBmpVERT;
} else if (ydiff == 0){
ChooseBmp = SynapseBmpHORZ;
} else if ((xdiff < 0) == (ydiff < 0)){ // same sign
ChooseBmp = SynapseBmpTLBR;
} else if ((xdiff < 0) != (ydiff < 0)) { // opposite sign
ChooseBmp = SynapseBmpBLTR;
}
int midX = X+(X1-X)/2;
int midY = Y+(Y1-Y)/2;
NetworkImage->Draw(midX - ImageSize/2, midY -ImageSize/2, ChooseBmp);
int th = NetworkImage->TextHeight(name)/2; // text height / 2 for centering
int te = NetworkImage->TextWidth(name)/2; // text width / 2 for centering
NetworkImage->Textout(midX - te, midY - th, name);
}
// draw electrodes
TElectrodesMapConstIterator e_iter;
TElectrodesMap const &electrodes = GetNet()->GetElectrodes();
for (e_iter = electrodes.begin(); e_iter != electrodes.end(); ++e_iter) {
X = ((TRTBase *)e_iter->second.get())->GetX();
Y = ((TRTBase *)e_iter->second.get())->GetY();
UnicodeString name(((TRTBase *)e_iter->second.get())->Name().c_str());
NetworkImage->Draw(X, Y - ImageSize, ElectrodeBmp);
int th = NetworkImage->TextHeight(name); // text height
int te = NetworkImage->TextWidth(name); // text width
NetworkImage->Textout(X + 3*ImageSize/4- te, Y - 7*ImageSize/8 + th, name);
}
NetworkPaintBox32->Buffer->Draw(0,0, NetworkImage);
NetworkPaintBox32->Invalidate();
}
//---------------------------------------------------------------------------
std::wstring __fastcall TGUI_SquareLatticeForm::CellGridHitTestAndCoerce(int &X, int &Y)
{
int spacing = ImageSize + Margin;
int fcX = ImageSize/2 + Margin; int fcY = fcX; //position of first cell.
double spotX = X;
spotX -= (double)fcX;
spotX /= (double)spacing;
int gridX = floor(spotX);
spotX -= (double) gridX; // now spotX is between 0 and 1;
if (spotX < 0.5) {
X = gridX * spacing + fcX;
}
else {
X = (gridX + 1) * spacing + fcX;
}
double spotY = Y;
spotY -= (double)fcY;
spotY /= (double)spacing;
int gridY = floor(spotY);
spotY -= (double) gridY; // now spotY is between 0 and 1;
if (spotY < 0.5) {
Y = gridY * spacing + fcY;
}
else {
Y = (gridY + 1) * spacing + fcY;
}
// search cells to see if one exists at (X,Y) already
return GetNet()->CellGridHitTest(X, Y);
}
//---------------------------------------------------------------------------
void __fastcall TGUI_SquareLatticeForm::PlaceObject(TBitmap32* bmp, int X, int Y)
{
if (NetworkPaintBox32->Width < bmp->Width) return;
if (NetworkPaintBox32->Height < bmp->Height) return;
int halfwidth = bmp->Width/2;
int halfheight = bmp->Height/2;
int lastleft = std::max(LastX-halfwidth, 0);
int lasttop = std::max(LastY-halfheight, 0);
int lastright = std::min(LastX + halfwidth, NetworkPaintBox32->Width);
int lastbottom = std::min(LastY + halfheight, NetworkPaintBox32->Height);
NetworkPaintBox32->Buffer->Draw(
Rect(lastleft, lasttop, lastright, lastbottom),
Rect(lastleft, lasttop, lastright, lastbottom),
NetworkImage);
int left = std::max(X-halfwidth, 0);
int top = std::max(Y-halfheight, 0);
int right = std::min(X + halfwidth,NetworkPaintBox32->Width);
int bottom = std::min(Y + halfheight, NetworkPaintBox32->Height);
Types::TRect dst = Rect(left, top, right, bottom);
int pickleft = X >= halfwidth ? 0 : halfwidth - X;
int picktop = Y >= halfheight ? 0 : halfheight - Y;
int pickright = (X + halfwidth) > NetworkPaintBox32->Width ?
bmp->Width - ((X + halfheight) - NetworkPaintBox32->Width) : bmp->Width;
int pickbottom = (Y + halfheight) > NetworkPaintBox32->Height ?
bmp->Height - ((Y + halfheight) - NetworkPaintBox32->Height) : bmp->Height;
Types::TRect src = Rect(pickleft, picktop, pickright, pickbottom);
NetworkPaintBox32->Buffer->Draw(dst, src, bmp);
NetworkPaintBox32->Flush(Rect(std::min(lastleft,left),
std::min(lasttop,top),
std::max(lastright,right),
std::max(lastbottom,bottom)));
}
void __fastcall TGUI_SquareLatticeForm::EditObject(TBitmap32* bmp, int X, int Y)
{
//
}
void __fastcall TGUI_SquareLatticeForm::NetworkPaintBox32MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if ((!NetworkPaintBox32->Buffer->Empty()) && (NetworkGUI->ModelMouseMode < NoMouseObject)){
CellGridHitTestAndCoerce(X, Y);
switch (NetworkGUI->ModelMouseMode)
{
case PositionCell: {
PlaceObject(CellHitTestBmp, X, Y);
} ; break;
case PositionElectrode: {
PlaceObject(ElectrodeHitTestBmp, X, Y);
} ; break;
case PositionSynapse1:
case PositionSynapse2: {
PlaceObject(SynapseHitTestBmp, X, Y);
} ; break;
case EditAnObject: {
// EditObject(ChooseBmp, X, Y);
}; break;
default: break;
};
LastX = X;
LastY = Y;
}
}
//---------------------------------------------------------------------------
void __fastcall TGUI_SquareLatticeForm::NetworkPaintBox32MouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
if ((!NetworkPaintBox32->Buffer->Empty()) && (NetworkGUI->ModelMouseMode < NoMouseObject)){
std::wstring cellname = CellGridHitTestAndCoerce(X, Y);
bool over_existing_cell = (cellname != L"");
switch (NetworkGUI->ModelMouseMode)
{
case PositionCell: {
if (!over_existing_cell){
// get cell type and name from dialog -- create cell in net and add to treeview
if (NetworkGUI->CreateCell(X, Y))
NetworkImage->Draw(X-ImageSize/2, Y-ImageSize/2, CellBmp);
}
} ; break;
case PositionElectrode: {
if (over_existing_cell) {
if (NetworkGUI->CreateElectrode(cellname, X, Y)) {
NetworkImage->Draw(X, Y - ImageSize, ElectrodeBmp);
}
}
}; break;
case PositionSynapse1: {
if (over_existing_cell) {
FirstCellName = cellname;
X1 = X;
Y1 = Y;
NetworkPaintBox32->Buffer->Draw(X-ImageSize/2, Y-ImageSize/2, SynapseHitTestBmp);
NetworkGUI->ModelMouseMode = PositionSynapse2;
return;
}
}; break;
case PositionSynapse2: {
if (over_existing_cell) {
if (cellname != FirstCellName){
SecondCellName = cellname;
int xdiff = (X - X1);
int ydiff = (Y - Y1);
int properspacing = ImageSize + Margin;
if ((abs(xdiff)> properspacing) || (abs(ydiff) > properspacing)) {
Application->MessageBoxA(L"Cells must be adjacent", L"Cannot complete synapse");
return;
}
if (xdiff == 0) {
ChooseBmp = SynapseBmpVERT;
} else if (ydiff == 0){
ChooseBmp = SynapseBmpHORZ;
} else if ((xdiff < 0) == (ydiff < 0)){ // same sign
ChooseBmp = SynapseBmpTLBR;
} else if ((xdiff < 0) != (ydiff < 0)) { // opposite sign
ChooseBmp = SynapseBmpBLTR;
}
int midX = X+(X1-X)/2;
int midY = Y+(Y1-Y)/2;
if (NetworkGUI->CreateSynapse(FirstCellName, SecondCellName, midX, midY)){
NetworkImage->Draw(midX - ImageSize/2, midY -ImageSize/2, ChooseBmp);
}
}
}
}; break;
case EditAnObject: {
EditObject(ChooseBmp, X, Y);
}; break;
default: break;
};
NetworkGUI->ModelMouseMode = NoMouseObject;
UpdateDisplay();
}
}