-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGLEditor.cpp
More file actions
1699 lines (1404 loc) · 42.7 KB
/
GLEditor.cpp
File metadata and controls
1699 lines (1404 loc) · 42.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <GL/openglut.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <sys/stat.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include "UT_String.h"
#include "luaInterface.h"
#include "GLEditor.h"
#include "PolyGlyph.h"
#include "assert.h"
#include "PraxisTexture.h"
#define GLEDITOR_DELETE 127
#define GLEDITOR_BACKSPACE 8
#define GLEDITOR_RETURN 13
#define GLEDITOR_CUT 24
#define GLEDITOR_COPY 3
#define GLEDITOR_PASTE 22
#define GLEDITOR_SAVE 19
PraxisTexture * GLEditor::m_pTexture = 0;
int GLEditor::m_nDesiredTextureSize = 512;
bool GLEditor::m_bUpdateRequired = true;
bool GLEditor::m_bNativeControl = true;
// static so we share between workspaces
string GLEditor::m_CopyBuffer;
int GLEditor::m_nRenderMode = GLEditor::RenderMode_Direct_Polyglyph;
float GLEditor::m_CursorWidth = 0.0f;
float GLEditor::m_CharWidth = 0.0f;
float GLEditor::m_CharHeight = 0.0f;
PolyGlyph * GLEditor::m_PolyGlyph = 0;
int GLEditor::m_VisibleLines = 20;
int GLEditor::m_VisibleColumns = 55;
GLEditor::GLEditor():
m_Position(0),
m_HighlightAnchor(0),
m_Selection(false),
m_OpenChars("([<{"),
m_CloseChars(")]>}"),
m_TopTextPosition(0),
m_LeftTextPosition(0),
m_Width(0),
m_Height(0)
{
if(m_PolyGlyph == 0)
m_PolyGlyph = new PolyGlyph("Bitstream-Vera-Sans-Mono.ttf");
//m_CharWidth=StrokeWidth('#')+1;
m_CharWidth=m_PolyGlyph->CharacterWidth('#'); // + 1?? Why? Tears?
m_CharHeight=m_PolyGlyph->CharacterHeight('#'); // * 1.5
m_CursorWidth=m_CharWidth/3.0f;
m_ParenthesesHighlight[0]=-1;
m_ParenthesesHighlight[1]=-1;
m_LuaBlockHighlight[0]=-1;
m_LuaBlockHighlight[1]=-1;
m_sName = "Untitled";
m_sParentName = "";
}
GLEditor::~GLEditor()
{
//delete m_PolyGlyph;
}
void GLEditor::Load(const string & sFilename)
{
ClearAllText();
SetText(ReadFileToString(sFilename));
SetName(sFilename);
}
string GLEditor::ReadFileToString(const string & sFilename)
{
string text,line;
std::ifstream file (sFilename.c_str());
if (file.is_open())
{
while ( !file.eof() )
{
std::getline(file,line);
// Prune the '\r' if present
if(line.length() > 0)
if(line[line.length()-1] == '\r')
line = line.substr(0, line.length()-1);
line = line + std::string("\n");
text = text + line;
}
file.close();
}
return text;
}
string GLEditor::ReadLinesFromString(const string & sText, int nFirstLine, int nLastLine)
{
string::size_type nBegin = 0;
for(int i = 1; i < nFirstLine; i++)
{
nBegin = sText.find("\n",nBegin)+1;
}
string::size_type nEnd = 0;
for(int i = 0; i < nLastLine; i++)
{
nEnd = sText.find("\n",nEnd)+1;
if(nEnd == 0)
nEnd = sText.length();
}
return sText.substr(nBegin, nEnd - nBegin);
}
static bool FileExists(const std::string& sFilename)
{
struct stat buf;
if (stat(sFilename.c_str(), &buf) != -1)
return true;
return false;
}
int g_nSaveNumber = 0;
static string MakeSaveFilename()
{
stringstream ss;
ss << "save";
ss << g_nSaveNumber;
ss << ".lua";
string sFilename = ss.str();
return sFilename;
}
static string MakeBackupFilename(const string & sName, int nNumber)
{
stringstream ss;
#ifdef __PRAXIS_WINDOWS__
ss << "bak\\";
#endif
#ifdef __PRAXIS_LINUX__
ss << "bak/";
#endif
ss << sName;
ss << ".bak";
ss << nNumber;
string sFilename = ss.str();
return sFilename;
}
static string MakeAvailableBackupFilename(const string & sName)
{
int i = 1;
string sFilename = MakeBackupFilename(sName, i);
while(FileExists(sFilename))
{
i++;
sFilename = MakeBackupFilename(sName, i);
}
return sFilename;
}
void GLEditor::SaveAs(const string & sFilename)
{
if(FileExists(sFilename))
{
string sFilename2 = MakeAvailableBackupFilename(sFilename);
rename(sFilename.c_str(), sFilename2.c_str());
}
std::ofstream file(sFilename.c_str());
file << m_Text;
file.close();
SetName(sFilename);
}
void GLEditor::Save()
{
SaveAs(m_sName);
}
void GLEditor::Reshape(unsigned int w,unsigned int h)
{
m_Width=w;
m_Height=h;
}
int GLEditor::CurrentLine()
{
return GetLine(m_Position);
}
int GLEditor::GetFirstVisiblePosition()
{
return m_TopTextPosition;
}
int GLEditor::GetLastVisiblePosition()
{
// Count m_VisibleLines down from m_TopTextPosition
int nPosition = m_TopTextPosition;
for(int i = 0; i < m_VisibleLines-1; i++) // do 1 less line
{
nPosition = LineEnd(nPosition) + 1;
// nPosition must always be a valid position for the string insert function.
// the insert function accepts from 0 to the length.
if(nPosition > m_Text.length())
nPosition = m_Text.length();
}
nPosition = LineEnd(nPosition); // Should always be at the end of a line.
return nPosition;
}
void GLEditor::SetCurrentLine(int line)
{
m_Position=0;
int count=0;
for (unsigned int i=0; i<m_Text.size(); i++)
{
if (m_Text[i]=='\n') count++;
if (count<=line) m_Position++;
}
if (m_Position<GetFirstVisiblePosition())
m_TopTextPosition=LineStart(m_Position);
if (m_Position>GetLastVisiblePosition())
m_TopTextPosition=LineEnd(m_TopTextPosition)+1; // This is the offending line!!
m_Position=LineStart(m_Position);
}
int GLEditor::GetLinePosition(int line)
{
int position=0;
int count=0;
for (unsigned int i=0; i<m_Text.size(); i++)
{
if (m_Text[i]=='\n') count++;
if (count<=line) position++;
}
position=LineStart(position);
return position;
}
void GLEditor::SetText(const string& s)
{
m_Text=s;
ProcessTabs();
m_bUpdateRequired = true;
Update();
}
void GLEditor::InsertText(const string & s)
{
m_Text.insert(m_Position, s);
m_Position += s.length();
if(m_Position>GetLastVisiblePosition())
m_TopTextPosition=LineEnd(m_TopTextPosition)+1;
m_bUpdateRequired = true;
Update();
}
void GLEditor::InsertTextAt(const string &s, int pos)
{
m_Text.insert(pos, s);
m_bUpdateRequired = true;
Update();
}
void GLEditor::ClearAllText()
{
m_Text="";
m_Position=0;
SetCurrentLine(0);
m_Selection=false;
}
void SetNaturalRasterPos()
{
GLint viewport[4];
GLdouble modelMatrix[16];
GLdouble projMatrix[16];
GLdouble winX, winY, winZ;
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);
gluProject(0,0,0, modelMatrix, projMatrix, viewport, &winX, &winY, &winZ);
glRasterPos2d(winX, winY);
}
void GLEditor::SetRenderMode(int nRenderMode)
{
if(nRenderMode < 0) nRenderMode = 0;
if(nRenderMode > 5) nRenderMode = 5;
GLEditor::m_nRenderMode = nRenderMode;
GLEditor::m_PolyGlyph->ClearCache();
if(GLEditor::m_nRenderMode == GLEditor::RenderMode_Texture_Bitmap)
{
// Smaller font, so can fit more text in the texture
GLEditor::m_VisibleLines = 30;
GLEditor::m_VisibleColumns = 65;
}
else
{
GLEditor::m_VisibleLines = 20;
GLEditor::m_VisibleColumns = 55;
}
m_bUpdateRequired = true;
}
void GLEditor::StrokeCharacter(wchar_t c, float dx, float dy)
{
switch(m_nRenderMode)
{
case RenderMode_Texture_Polyglyph:
case RenderMode_Direct_Polyglyph:
m_PolyGlyph->Render(c, 1.0f, 1.0f, 1.0f, 1.0f, dx, dy);
break;
case RenderMode_Texture_Stroke:
case RenderMode_Direct_Stroke:
glLineWidth(1.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glPushMatrix();
glScalef(30,30,30);
glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, c);
glPopMatrix();
//glTranslatef(StrokeWidth(c),0,0);
glTranslatef(m_CharWidth,0,0);
glLineWidth(1.0f);
break;
case RenderMode_Texture_Bitmap:
glLineWidth(1.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glPushMatrix();
SetNaturalRasterPos();
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, c);
glPopMatrix();
//glTranslatef(StrokeWidth(c),0,0);
glTranslatef(m_CharWidth,0,0);
glLineWidth(1.0f);
break;
case RenderMode_Direct_Bitmap:
glLineWidth(1.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glPushMatrix();
SetNaturalRasterPos();
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c);
glPopMatrix();
//glTranslatef(StrokeWidth(c),0,0);
glTranslatef(m_CharWidth,0,0);
glLineWidth(1.0f);
break;
}
}
void GLEditor::SetCharColor(int r, int g, int b, int a)
{
m_fCharRed = (float)r / 255.0f;
m_fCharGreen = (float)g / 255.0f;
m_fCharBlue = (float)b / 255.0f;
m_fCharAlpha = (float)a / 255.0f;
}
//float GLEditor::StrokeWidth(wchar_t c)
//{
// return m_PolyGlyph->CharacterWidth(c);
//}
string GLEditor::GetText()
{
//if (m_Selection) return m_Text.substr(m_HighlightStart,m_HighlightEnd-m_HighlightStart);
if (m_Selection)
return m_Text.substr(SelectionBegin(),SelectionEnd()-SelectionBegin());
if(m_LuaBlockHighlight[0] != -1 && m_LuaBlockHighlight[1] != -1)
return m_Text.substr(m_LuaBlockHighlight[0], m_LuaBlockHighlight[1] - m_LuaBlockHighlight[0]);
return m_Text;
}
string GLEditor::GetLuaBlock()
{
if(m_LuaBlockHighlight[0] != -1 && m_LuaBlockHighlight[1] != -1)
return m_Text.substr(m_LuaBlockHighlight[0], m_LuaBlockHighlight[1] - m_LuaBlockHighlight[0]);
return "";
}
string GLEditor::GetSelectedText()
{
if (m_Selection)
return m_Text.substr(SelectionBegin(),SelectionEnd()-SelectionBegin());
return "";
}
string GLEditor::GetCurrentLineText()
{
return GetLineText(m_Position);
}
string GLEditor::GetLineText(int nPosition)
{
int nStart = LineStart(nPosition);
int nEnd = LineEnd(nPosition);
return m_Text.substr(nStart, nEnd - nStart);
}
string GLEditor::GetLineTextToPosition(int nPosition)
{
int nStart = LineStart(nPosition);
int nEnd = nPosition;
return m_Text.substr(nStart, nEnd - nStart);
}
string GLEditor::GetLineTextFromPosition(int nPosition)
{
int nStart = nPosition;
int nEnd = LineEnd(nPosition);
return m_Text.substr(nStart, nEnd - nStart);
}
string GLEditor::GetSExpr()
{
if (m_ParenthesesHighlight[0]<m_ParenthesesHighlight[1])
{
return m_Text.substr(m_ParenthesesHighlight[0],m_ParenthesesHighlight[1]+1-m_ParenthesesHighlight[0]);
}
return "";
}
void GLEditor::DrawCharBlock()
{
glBegin(GL_QUADS);
glVertex3f(m_CharWidth * 1.00, -m_CharHeight * 0.1, 0);
glVertex3f(m_CharWidth * 1.00, m_CharHeight * 0.9, 0);
glVertex3f(0, m_CharHeight * 0.9, 0);
glVertex3f(0, -m_CharHeight * 0.1, 0);
glEnd();
}
void GLEditor::DrawCursor()
{
glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
float half = m_CursorWidth/2.0f;
glBegin(GL_QUADS);
glVertex2f(half,0);
glVertex2f(half,m_CharHeight);
glVertex2f(-half,m_CharHeight);
glVertex2f(-half,0);
glEnd();
}
void GLEditor::GetBB(float &minX, float &minY, float &maxX, float &maxY)
{
minX = 0.0f;
minY = -(m_VisibleLines-1) * m_CharHeight;
maxX = m_VisibleColumns * m_CharWidth;
maxY = m_CharHeight;
}
void GLEditor::Update()
{
if(!m_bUpdateRequired)
return;
// Keep m_Position in bounds of the file
if (m_Position<0) m_Position=0;
if (m_Position>m_Text.size()) m_Position=m_Text.size();
// Move the view toward the cursor.
// The fix will be instant if the cursor is above the view,...
if (m_Position<m_TopTextPosition)
m_TopTextPosition=LineStart(m_Position);
// ...gradual if the cursor is below.
// Could add a while loop, but I want to see the effect.
//if(m_Position>GetLastVisiblePosition())
while(m_Position>GetLastVisiblePosition()) // beware infinite loops?
m_TopTextPosition=LineEnd(m_TopTextPosition)+1;
// Set the left text position according to the position of the cursor
// within the current line
int nOffset = CurrentColumn();
if (nOffset > m_LeftTextPosition+m_VisibleColumns)
m_LeftTextPosition=nOffset-m_VisibleColumns;
// else
// m_LeftTextPosition=0;
if (nOffset < m_LeftTextPosition)
m_LeftTextPosition = nOffset;
m_LuaBlockHighlight[0]=-1;
m_LuaBlockHighlight[1]=-1;
// temporarily do this manually
// IndexPair paren = ParseParentheses();
// m_ParenthesesHighlight[0] = paren.n1;
// m_ParenthesesHighlight[1] = paren.n2;
ParseLuaBlock();
}
void GLEditor::ResizeTexture(int nNewSize)
{
if(nNewSize < 2) nNewSize = 2;
if(nNewSize > 1024) nNewSize = 1024;
m_nDesiredTextureSize = nNewSize;
m_bUpdateRequired = true;
}
float g_fEdLeftMargin = 0.01f;
float g_fEdRightMargin = 0.75f;
float g_fEdBottomMargin = 0.05f;
float g_fEdTopMargin = 0.95f;
void GLEditor::Render()
{
bool bDirectRenderMode = (m_nRenderMode <= RenderMode_Direct_Bitmap);
if(bDirectRenderMode)
{
int nLeftMargin = m_Width * g_fEdLeftMargin;
int nWidth = m_Width * g_fEdRightMargin - nLeftMargin;
int nBottomMargin = m_Height * g_fEdBottomMargin;
int nHeight = m_Height * g_fEdTopMargin - nBottomMargin;
glViewport(nLeftMargin, nBottomMargin, nWidth, nHeight);
RenderBuffer(true);
}
else
{
if(m_pTexture == 0)
{
m_pTexture = new PraxisTexture(m_nDesiredTextureSize);
m_bUpdateRequired = true;
}
else if(m_pTexture->nSize != m_nDesiredTextureSize)
{
delete m_pTexture;
m_pTexture = new PraxisTexture(m_nDesiredTextureSize);
m_bUpdateRequired = true;
}
if(m_bUpdateRequired)
{
m_pTexture->Begin();
//m_pTexture->Resume();
glViewport(0, 0, m_pTexture->nSize, m_pTexture->nSize);
RenderBuffer(false);
m_pTexture->End();
m_bUpdateRequired = false;
}
RenderTexture();
}
}
void GLEditor::RenderTexture()
{
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_pTexture->nTextureID);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0,m_Width,0,m_Height,0,10);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// glRasterPos3d(2,5,0);
// glDrawPixels(g_pEditTexture->nSize, g_pEditTexture->nSize, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)g_pEditTexture->pixels);
{
int nSize = m_pTexture->nSize;
int nLeftMargin = 10;
int nBottomMargin = 35;
int nMinX = nLeftMargin;
int nMaxX = nMinX + nSize;
int nMinY = nBottomMargin;
int nMaxY = nMinY + nSize;
glColor4f(1,1,1,0.5);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f( nMinX, nMinY, 0);
glTexCoord2f(1,0);
glVertex3f( nMaxX, nMinY, 0);
glTexCoord2f(1,1);
glVertex3f( nMaxX, nMaxY, 0);
glTexCoord2f(0,1);
glVertex3f( nMinX, nMaxY, 0);
glEnd();
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void GLEditor::RenderBuffer(bool bBackground)
{
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPolygonMode(GL_FRONT,GL_FILL);
float minX,minY,maxX,maxY; GetBB(minX,minY,maxX,maxY);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(minX, maxX, minY, maxY, 0,10);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPushMatrix();
luaCall("edPrerenderBg()", "onerrorgl");
if(bBackground)
{
glColor4f(0,0,0,0.7f);
glBegin(GL_QUADS);
glVertex3f( minX, minY, 0);
glVertex3f( maxX, minY, 0);
glVertex3f( maxX, maxY, 0);
glVertex3f( minX, maxY, 0);
glEnd();
}
luaCall("edPostrenderBg()", "onerrorgl");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(minX, maxX, minY - (m_CharHeight * 0.2f), maxY, 0,10);
glMatrixMode(GL_MODELVIEW);
RenderChars();
glPopMatrix();
glPopMatrix();
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void GLEditor::RenderChars()
{
// I can push this to Lua first.
// Or Lisp.
if(!m_bNativeControl)
{
if(!luaCall("edRenderChars()", "onerrorgl"))
{
// Revert to native control on error.
// Optional?
m_bNativeControl = true;
}
return;
}
glPushMatrix();
unsigned int xcount=0;
float xpos=0;
float ypos=0;
bool drawncursor=false;
for(int n=GetFirstVisiblePosition(); n < GetLastVisiblePosition(); n++)
{
if(m_Text[n]!='\n')
{
if ((int)n>=m_ParenthesesHighlight[0] &&
(int)n<=m_ParenthesesHighlight[1]) // draw parentheses highlight
{
glColor4f(0,0,1,0.5f);
DrawCharBlock();
glColor4f(0.7,0.7,0.7,1);
}
if ((int)n>=m_LuaBlockHighlight[0] &&
(int)n<=m_LuaBlockHighlight[1]) // draw lua block highlight
{
//glColor4f(0,1,1,0.5f);
int nDistToStart = n - m_LuaBlockHighlight[0];
int nDistToEnd = m_LuaBlockHighlight[1] - n;
float fAlpha1 = 0.5f - (nDistToStart * 0.1f);
float fAlpha2 = 0.5f - (nDistToEnd * 0.1f);
float fAlpha = fAlpha1;
if(fAlpha2 > fAlpha1) fAlpha = fAlpha2;
if(fAlpha > 0.0f)
{
glColor4f(0,1,1, fAlpha);
DrawCharBlock();
glColor4f(0.7,0.7,0.7,1);
}
}
if (m_Selection && n>=SelectionBegin() && n<SelectionEnd()) // draw selection highlight
{
glColor4f(0,1,0,0.5f);
DrawCharBlock();
glColor4f(0.7,0.7,0.7,1);
}
}
if (m_Position==n) // draw cursor
{
DrawCursor();
glColor4f(0.7,0.7,0.7,1);
drawncursor=true;
}
if(m_Text[n]=='\n')
{
// Put the matrix back to the top left of the document
glPopMatrix();
glPushMatrix();
xpos=0;
xcount=0;
ypos-=m_CharHeight;
//lineCount++;
// Move the matrix down to the next line to be drawn
glTranslatef(0,ypos,0);
//glTranslatef(0,-lineCount * m_CharHeight,0);
}
else
{
if (xcount>=m_LeftTextPosition && xcount < m_LeftTextPosition + m_VisibleColumns)
{
StrokeCharacter(m_Text[n], 0,0);
xpos+=m_CharWidth;
}
xcount++;
}
}
// draw cursor if we have no text, or if we're at the end of the buffer
if (!drawncursor)
{
DrawCursor();
glColor4f(0.7,0.7,0.7,1);
}
glPopMatrix();
}
void GLEditor::MoveViewUp()
{
}
void GLEditor::MoveViewDown()
{
}
void GLEditor::MoveCursorUp()
{
m_Position = GetUp(m_Position);
#if 0
if ((int)LineStart(m_Position) > 0) // if we're not on the first line
{
int nOffset = CurrentColumn();
m_Position = LineStart(m_Position);
m_Position--;
int nLineEnd = m_Position;
m_Position = LineStart(m_Position);
int nLineBegin = m_Position;
if(nLineBegin + nOffset > nLineEnd)
m_Position = nLineEnd;
else
m_Position = nLineBegin + nOffset;
}
#endif
}
void GLEditor::MoveCursorDown()
{
m_Position = GetDown(m_Position);
#if 0
if (LineEnd(m_Position) < m_Text.size()) // if we're not on the last line
{
int nOffset = CurrentColumn();
m_Position = LineEnd(m_Position);
m_Position++;
int nLineBegin = m_Position;
int nLineEnd = LineEnd(m_Position);
if(nLineBegin + nOffset > nLineEnd)
m_Position = nLineEnd;
else
m_Position = nLineBegin + nOffset;
}
#endif
}
void GLEditor::MoveCursorLeft()
{
m_Position = GetLeft(m_Position);
// if (m_Position>0) m_Position--;
}
void GLEditor::MoveCursorRight()
{
m_Position = GetRight(m_Position);
//if (!m_Text.empty()) m_Position++;
}
void GLEditor::MoveCursorToStart()
{
m_Position = 0;
}
void GLEditor::MoveCursorToEnd()
{
m_Position = m_Text.length();
}
void GLEditor::MoveCursorToStartOfLine()
{
m_Position=LineStart(m_Position);
}
void GLEditor::MoveCursorToEndOfLine()
{
m_Position=LineEnd(m_Position);
}
void GLEditor::MoveCursorUpAPage()
{
for (unsigned int n=0; n<m_VisibleLines+1; n++)
{
MoveCursorUp();
}
}
void GLEditor::MoveCursorDownAPage()
{
for (unsigned int n=0; n<m_VisibleLines+1; n++)
{
MoveCursorDown();
}
}
void GLEditor::InsertNewline()
{
EraseSelection();
m_Text.insert(m_Position,"\n");
m_Position++;
if(m_Position>GetLastVisiblePosition())
m_TopTextPosition=LineEnd(m_TopTextPosition)+1;
Update();
}
void GLEditor::Delete()
{
if (m_Selection)
{
EraseSelection();
}
else
{
m_Text.erase(m_Position,1);
}
Update();
}
// probe for references
// none found
#if 1
void GLEditor::Backspace()
{
if (m_Selection)
{
EraseSelection();
}
else
{
if(m_Position>0)
{
m_Text.erase(m_Position-1,1);
m_Position--;
}
}
Update();
}
#endif
void GLEditor::InsertTab()
{
m_Text.insert(m_Position," ");
m_Position+=2;
}
void GLEditor::InsertKey(int key)
{
if(key != 0)
{