forked from Gargaj/Bonzomatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShaderEditor.cpp
350 lines (288 loc) · 10.3 KB
/
ShaderEditor.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
#include <cstring>
#include "../ShaderEditor.h"
#include "../Renderer.h"
#include "external/scintilla/lexlib/PropSetSimple.h"
#include "Clipboard.h"
ShaderEditor::ShaderEditor( Scintilla::Surface *s )
{
bReadOnly = false;
surfaceWindow = s;
sFontFile = "";
nFontSize = 16;
bHasMouseCapture = false;
nOpacity = 0xC0;
}
void ShaderEditor::SetAStyle(int style, Scintilla::ColourDesired fore, Scintilla::ColourDesired back, int size, const char *face)
{
WndProc(SCI_STYLESETFORE, style, (sptr_t)fore.AsLong());
WndProc(SCI_STYLESETBACK, style, (sptr_t)back.AsLong());
if (size >= 1)
WndProc(SCI_STYLESETSIZE, style, size);
if (face)
WndProc(SCI_STYLESETFONT, style, reinterpret_cast<sptr_t>(face));
}
#define BACKGROUND(x) ( (x) | (nOpacity << 24) )
const size_t NB_FOLDER_STATE = 7;
const size_t FOLDER_TYPE = 0;
const int markersArray[][NB_FOLDER_STATE] = {
{SC_MARKNUM_FOLDEROPEN, SC_MARKNUM_FOLDER, SC_MARKNUM_FOLDERSUB, SC_MARKNUM_FOLDERTAIL, SC_MARKNUM_FOLDEREND, SC_MARKNUM_FOLDEROPENMID, SC_MARKNUM_FOLDERMIDTAIL},
{SC_MARK_MINUS, SC_MARK_PLUS, SC_MARK_EMPTY, SC_MARK_EMPTY, SC_MARK_EMPTY, SC_MARK_EMPTY, SC_MARK_EMPTY},
{SC_MARK_ARROWDOWN, SC_MARK_ARROW, SC_MARK_EMPTY, SC_MARK_EMPTY, SC_MARK_EMPTY, SC_MARK_EMPTY, SC_MARK_EMPTY},
{SC_MARK_CIRCLEMINUS, SC_MARK_CIRCLEPLUS,SC_MARK_VLINE, SC_MARK_LCORNERCURVE, SC_MARK_CIRCLEPLUSCONNECTED, SC_MARK_CIRCLEMINUSCONNECTED, SC_MARK_TCORNERCURVE},
{SC_MARK_BOXMINUS, SC_MARK_BOXPLUS, SC_MARK_VLINE, SC_MARK_LCORNER, SC_MARK_BOXPLUSCONNECTED, SC_MARK_BOXMINUSCONNECTED, SC_MARK_TCORNER}
};
extern const char * shaderKeyword;
extern const char * shaderType;
extern const char * shaderBuiltin;
using namespace Scintilla;
class Scintilla::LexState : public LexInterface {
const LexerModule *lexCurrent;
void SetLexerModule(const LexerModule *lex);
PropSetSimple props;
int interfaceVersion;
public:
int lexLanguage;
explicit LexState(Document *pdoc_);
virtual ~LexState();
void SetLexer(uptr_t wParam);
void SetLexerLanguage(const char *languageName);
const char *DescribeWordListSets();
void SetWordList(int n, const char *wl);
const char *GetName() const;
void *PrivateCall(int operation, void *pointer);
const char *PropertyNames();
int PropertyType(const char *name);
const char *DescribeProperty(const char *name);
void PropSet(const char *key, const char *val);
const char *PropGet(const char *key) const;
int PropGetInt(const char *key, int defaultValue=0) const;
int PropGetExpanded(const char *key, char *result) const;
int LineEndTypesSupported();
int AllocateSubStyles(int styleBase, int numberStyles);
int SubStylesStart(int styleBase);
int SubStylesLength(int styleBase);
int StyleFromSubStyle(int subStyle);
int PrimaryStyleFromStyle(int style);
void FreeSubStyles();
void SetIdentifiers(int style, const char *identifiers);
int DistanceToSecondaryStyles();
const char *GetSubStyleBases();
};
static unsigned int wndID = 1;
void ShaderEditor::Initialise()
{
wMain = (Scintilla::WindowID)(wndID++);
lexState = new Scintilla::LexState( pdoc );
WndProc( SCI_SETBUFFEREDDRAW, NULL, NULL );
WndProc( SCI_SETCODEPAGE, SC_CP_UTF8, NULL );
//WndProc( SCI_SETLEXERLANGUAGE, SCLEX_CPP, NULL );
SetAStyle( STYLE_DEFAULT, 0xFFFFFFFF, BACKGROUND( 0x000000 ), nFontSize, sFontFile.c_str() );
WndProc( SCI_STYLECLEARALL, NULL, NULL );
SetAStyle( STYLE_LINENUMBER, 0xFFC0C0C0, BACKGROUND( 0x000000 ), nFontSize, sFontFile.c_str() );
SetAStyle( STYLE_BRACELIGHT, 0xFF00FF00, BACKGROUND( 0x000000 ), nFontSize, sFontFile.c_str() );
SetAStyle( STYLE_BRACEBAD, 0xFF0000FF, BACKGROUND( 0x000000 ), nFontSize, sFontFile.c_str() );
SetAStyle( STYLE_INDENTGUIDE, 0xFFC0C0C0, BACKGROUND( 0x000000 ), nFontSize, sFontFile.c_str() );
WndProc(SCI_SETFOLDMARGINCOLOUR, 1, BACKGROUND( 0x1A1A1A ));
WndProc(SCI_SETFOLDMARGINHICOLOUR, 1, BACKGROUND( 0x1A1A1A ));
WndProc(SCI_SETSELBACK, 1, BACKGROUND( 0xCC9966 ));
SetReadOnly(false);
for (int i = 0 ; i < NB_FOLDER_STATE ; i++)
{
WndProc(SCI_MARKERDEFINE, markersArray[FOLDER_TYPE][i], markersArray[4][i]);
WndProc(SCI_MARKERSETBACK, markersArray[FOLDER_TYPE][i], 0xFF6A6A6A);
WndProc(SCI_MARKERSETFORE, markersArray[FOLDER_TYPE][i], 0xFF333333);
}
WndProc(SCI_SETUSETABS, bUseSpacesForTabs ? 0 : 1, NULL);
WndProc(SCI_SETTABWIDTH, nTabSize, NULL);
WndProc(SCI_SETINDENTATIONGUIDES, SC_IV_REAL, NULL);
if (bVisibleWhitespace)
{
WndProc(SCI_SETVIEWWS, SCWS_VISIBLEALWAYS, NULL);
WndProc(SCI_SETWHITESPACEFORE, 1, 0x30FFFFFF);
WndProc(SCI_SETWHITESPACESIZE, 2, NULL );
}
lexState->SetLexer( SCLEX_CPP );
lexState->SetWordList(0, shaderKeyword);
lexState->SetWordList(1, shaderType);
lexState->SetWordList(3, shaderBuiltin);
SetAStyle(SCE_C_DEFAULT, 0xFFFFFFFF, BACKGROUND( 0x000000 ), nFontSize, sFontFile.c_str() );
SetAStyle(SCE_C_WORD, 0xFF0066FF, BACKGROUND( 0x000000 ));
SetAStyle(SCE_C_WORD2, 0xFFFFFF00, BACKGROUND( 0x000000 ));
SetAStyle(SCE_C_GLOBALCLASS, 0xFF88FF44, BACKGROUND( 0x000000 ));
SetAStyle(SCE_C_PREPROCESSOR, 0xFFC0C0C0, BACKGROUND( 0x000000 ));
SetAStyle(SCE_C_NUMBER, 0xFF0080FF, BACKGROUND( 0x000000 ));
SetAStyle(SCE_C_OPERATOR, 0xFF00CCFF, BACKGROUND( 0x000000 ));
SetAStyle(SCE_C_COMMENT, 0xFF00FF00, BACKGROUND( 0x000000 ));
SetAStyle(SCE_C_COMMENTLINE, 0xFF00FF00, BACKGROUND( 0x000000 ));
lexState->Colourise( 0, -1 );
//WndProc( SCI_COLOURISE, NULL, NULL );
vs.Refresh( *surfaceWindow, 4 );
}
void ShaderEditor::Initialise( SHADEREDITOR_OPTIONS &options )
{
nFontSize = options.nFontSize;
sFontFile = options.sFontPath;
nOpacity = options.nOpacity;
bUseSpacesForTabs = options.bUseSpacesForTabs;
nTabSize = options.nTabSize;
bVisibleWhitespace = options.bVisibleWhitespace;
Initialise();
SetPosition( options.rect );
}
void ShaderEditor::SetVerticalScrollPos()
{
}
void ShaderEditor::SetHorizontalScrollPos()
{
}
bool ShaderEditor::ModifyScrollBars( int nMax, int nPage )
{
return true;
}
void ShaderEditor::Copy()
{
if (!sel.Empty()) {
SelectionText selectedText;
CopySelectionRange(&selectedText);
CopyToClipboard(selectedText);
}
}
void ShaderEditor::Paste()
{
int n = Clipboard::GetContentsLength();
char * p = new char[n + 1];
memset(p,0,n+1);
Clipboard::GetContents( p, n );
InsertPasteShape(p, n, pasteStream);
delete[] p;
}
void ShaderEditor::ClaimSelection()
{
}
void ShaderEditor::NotifyChange()
{
}
void ShaderEditor::NotifyParent( Scintilla::SCNotification scn )
{
}
void ShaderEditor::CopyToClipboard( const Scintilla::SelectionText &selectedText )
{
Clipboard::Copy( selectedText.Data(), selectedText.Length() );
}
void ShaderEditor::SetMouseCapture( bool on )
{
bHasMouseCapture = on;
}
bool ShaderEditor::HaveMouseCapture()
{
return bHasMouseCapture;
}
sptr_t ShaderEditor::DefWndProc( unsigned int iMessage, uptr_t wParam, sptr_t lParam )
{
return 0;
}
void ShaderEditor::Paint()
{
Renderer::SetTextRenderingViewport( wMain.GetPosition() );
Scintilla::Editor::Paint( surfaceWindow, GetClientRectangle() );
}
void ShaderEditor::SetText( char * buf )
{
WndProc( SCI_SETREADONLY, false, NULL );
WndProc( SCI_CLEARALL, false, NULL );
WndProc( SCI_SETUNDOCOLLECTION, 0, NULL);
WndProc( SCI_ADDTEXT, strlen(buf), (sptr_t)buf );
WndProc( SCI_SETUNDOCOLLECTION, 1, NULL);
WndProc( SCI_SETREADONLY, bReadOnly, NULL );
WndProc( SCI_GOTOPOS, 0, NULL );
if (!bReadOnly)
SetFocusState( true );
}
void ShaderEditor::Tick()
{
Scintilla::Editor::Tick();
}
void ShaderEditor::SetTicking( bool on )
{
}
int ShaderEditor::KeyDown( int key, bool shift, bool ctrl, bool alt, bool *consumed )
{
return Scintilla::Editor::KeyDown( key, shift, ctrl, alt, consumed );
}
void ShaderEditor::AddCharUTF( const char *s, unsigned int len, bool treatAsDBCS )
{
Scintilla::Editor::AddCharUTF( s, len, treatAsDBCS );
}
void ShaderEditor::GetText( char * buf, int len )
{
memset( buf, 0, len );
int lengthDoc = WndProc( SCI_GETLENGTH, NULL, NULL );
Scintilla::TextRange tr;
tr.chrg.cpMin = 0;
tr.chrg.cpMax = Scintilla::Platform::Minimum( len - 1, lengthDoc );
tr.lpstrText = buf;
WndProc(SCI_GETTEXTRANGE, 0, reinterpret_cast<sptr_t>(&tr));
}
void ShaderEditor::NotifyStyleToNeeded(int endStyleNeeded) {
#ifdef SCI_LEXER
if (lexState->lexLanguage != SCLEX_CONTAINER) {
int lineEndStyled = pdoc->LineFromPosition(pdoc->GetEndStyled());
int endStyled = pdoc->LineStart(lineEndStyled);
lexState->Colourise(endStyled, endStyleNeeded);
return;
}
#endif
Scintilla::Editor::NotifyStyleToNeeded(endStyleNeeded);
}
void ShaderEditor::SetReadOnly( bool b )
{
bReadOnly = b;
WndProc( SCI_SETREADONLY, bReadOnly, NULL );
if (bReadOnly)
{
WndProc(SCI_SETVIEWWS, SCWS_INVISIBLE, NULL);
WndProc(SCI_SETMARGINWIDTHN, 0, 0);
WndProc(SCI_SETMARGINWIDTHN, 1, 0);
WndProc( SCI_SETCARETLINEVISIBLE, 0, NULL);
WndProc( SCI_SETCARETFORE, 0, 0);
}
else
{
WndProc(SCI_SETMARGINWIDTHN, 0, 44);//Calculate correct width
WndProc(SCI_SETMARGINWIDTHN, 1, 20);//Calculate correct width
WndProc(SCI_SETMARGINMASKN, 1, SC_MASK_FOLDERS);//Calculate correct width
WndProc( SCI_SETCARETFORE, 0xFFFFFFFF, 0);
WndProc( SCI_SETCARETLINEVISIBLE, 1, NULL);
WndProc( SCI_SETCARETLINEBACK, 0xFFFFFFFF, NULL);
WndProc( SCI_SETCARETLINEBACKALPHA, 0x20, NULL);
}
}
void ShaderEditor::ButtonDown( Scintilla::Point pt, unsigned int curTime, bool shift, bool ctrl, bool alt )
{
Scintilla::PRectangle rect = wMain.GetPosition();
pt.x -= rect.left;
pt.y -= rect.top;
Scintilla::Editor::ButtonDown( pt, curTime, shift, ctrl, alt );
}
void ShaderEditor::ButtonMovePublic( Scintilla::Point pt )
{
Scintilla::PRectangle rect = wMain.GetPosition();
pt.x -= rect.left;
pt.y -= rect.top;
ButtonMove(pt);
}
void ShaderEditor::ButtonUp( Scintilla::Point pt, unsigned int curTime, bool ctrl )
{
Scintilla::PRectangle rect = wMain.GetPosition();
pt.x -= rect.left;
pt.y -= rect.top;
Scintilla::Editor::ButtonUp( pt, curTime, ctrl );
}
Font * ShaderEditor::GetTextFont()
{
return &vs.styles[ STYLE_DEFAULT ].font;
}
void ShaderEditor::SetPosition( Scintilla::PRectangle rect )
{
wMain.SetPosition(rect);
}