summaryrefslogtreecommitdiff
path: root/src/testApp.cpp
blob: df59d6e559627debb38a6d5d40fb185b6f7318df (plain)
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
#include "testApp.h"
//--------------------------------------------------------------

/*
text editor functionality:

keep text as 1 string? or as a vector of strings?
prob faster to use a vector of strings for display
makes it more awkward for selections? keep track of which line/char to start and end

alternative - go through text and seperate into lines either by width or by /n every time it changes
keep a display copy with /n's inserted?

alternative - just have a horizontal scroll bar (contextual)
maybe draw into an FBO in order to put a window in a window

method to retreive text as a single string with \n's

set box size - draw scrollbar & set scrollpoint - drag to scroll

load & save

architecture :- text editor in a box or a window?

insertion point/ selection - set with mouse -

get and set clipboard (ofxFensterManager.h)


vector insert a problem performance wise?
you need to use an iterator to insert into a vector anyway- may as well use a list


*/
editorWindow::~editorWindow(){
	cout << "editor window destroyed" << endl;

}
void editorWindow::setup(){
    ofxFenster* win=ofxFensterManager::get()->createFenster(0, 0,  400, 400, OF_WINDOW);
	win->setWindowTitle("editor");
	win->addListener(this);
	selected=false;
	changed=false;
	text.push_back(string(""));
	output="";
}
void editorWindow::draw(){
    ofDisableAlphaBlending();
    ofSetColor(255,255,255);
    for (int i=0;i<text.size();i++) {
        ofDrawBitmapString(text[i],10,15+(i*12));
    }
    if (selected) {
        ofEnableAlphaBlending();
        ofSetColor(0,128,255,160);
        //draw selection
        if (selectionStart.row==selectionEnd.row) {
            ofRect((selectionStart.column*8)+10,(selectionStart.row*12)+5,(selectionEnd.column-selectionStart.column)*8,12);
        }
        else {
            ofRect((selectionStart.column*8)+10,(selectionStart.row*12)+5,(text[selectionStart.row].size()-selectionStart.column)*8,12);
            for (int i=selectionStart.row+1;i<selectionEnd.row;i++) {
                ofRect(10,(i*12)+5,(text[i].size())*8,12);
            }
            ofRect(10,(selectionEnd.row*12)+5,(selectionEnd.column)*8,12);
        }
        ofDisableAlphaBlending();
    }
    else {
        //draw insertion point
        if ((ofGetElapsedTimeMillis()/300)%2) {
            ofRect((insertionPoint.column*8)+10,(insertionPoint.row*12)+5,2,10);
        }
    }

}
void editorWindow::keyPressed(int key){
	printf("%i\n",key);
	changed=true;
	vector<string>::iterator i;
	string t;
	int l;
	switch (key) {
	case OF_KEY_DEL:
	    if (selected) deleteSelection();
	    else {
		if (insertionPoint.column<text[insertionPoint.row].size()-1) {
		    text[insertionPoint.row].erase(insertionPoint.column,1);
		}
		else if (insertionPoint.row<text.size()-1) {
		    text[insertionPoint.row]+=text[insertionPoint.row+1];
		    i=text.begin()+insertionPoint.row;
		    text.erase(i);
		}
	    }
	    break;
	case OF_KEY_BACKSPACE:
	    if (selected) deleteSelection();
	    else {
		if (insertionPoint.column) {
		    text[insertionPoint.row].erase(insertionPoint.column-1,1);
		    insertionPoint.column--;
		}
		else if (insertionPoint.row) {
		    insertionPoint.column=text[insertionPoint.row-1].size();
		    text[insertionPoint.row-1]+=text[insertionPoint.row];
		    i=text.begin()+insertionPoint.row;
		    text.erase(i);
		    insertionPoint.row--;
		}
	    }
	    break;
	case OF_KEY_RETURN:
	    deleteSelection();
		i=text.begin()+insertionPoint.row+1;
		t=text[insertionPoint.row].substr(insertionPoint.column);
		text[insertionPoint.row].erase(insertionPoint.column);
	    text.insert(i,t);
	    insertionPoint.row++;
	    insertionPoint.column=0;
	    break;
	case OF_KEY_LEFT:
	    if (insertionPoint.column>0) insertionPoint.column--;
		else if (insertionPoint.row>0) {
		    insertionPoint.row--;
		    insertionPoint.column=text[insertionPoint.row].size();
		}
	    break;
	case OF_KEY_UP: 
	    if (insertionPoint.row>0) {
		insertionPoint.row--;
		insertionPoint.column=min(insertionPoint.column,(int)text[insertionPoint.row].size());
	    }
	    break;
	case OF_KEY_RIGHT:
	    if (insertionPoint.column<text[insertionPoint.row].size()) {
		insertionPoint.column++;
	    }
	    else {
		if (text.size()>insertionPoint.column) {
		    insertionPoint.row++;
		    insertionPoint.column=min(insertionPoint.column,(int)text[insertionPoint.row].size());
		}
	    }
	    break;
	case OF_KEY_DOWN:
	    if (insertionPoint.row<text.size()-1) {
		insertionPoint.row++;
		insertionPoint.column=min(insertionPoint.column,(int)text[insertionPoint.row].size());
	    }
	    break;
	case 24: //ctrl-x
		setClipboard(getSelection());
		deleteSelection();
		break;
	case 3: //ctrl-c
		setClipboard(getSelection());
		break;
	case 22: //ctrl-v
		deleteSelection();
		insertText(string(reinterpret_cast<const char *>(ofxFensterManager::get()->getClipboard())));
		break;
	default:
	    if (key>-1 && key<256) {
		deleteSelection();
		text[insertionPoint.row].insert(insertionPoint.column,1,(char)key);
		insertionPoint.column++;
	    }
    }
}
void editorWindow::keyReleased(int key){
    //key repeat?
}
void editorWindow::mousePressed(int x, int y, int button){
   selected=false;
    clickX=x;
    clickY=y;
}
void editorWindow::mouseReleased(int x, int y, int button){
    if (clickX=x&&clickY==y) {
        insertionPoint=clickPos(x,y);
    }
}
void editorWindow::mouseDragged(int x, int y, int button){
	texPt c1=clickPos(clickX,clickY);
	texPt c2=clickPos(x,y);
    if ((c1.row==c2.row&&c2.column>c1.column)||c2.row>c1.row) {
        selectionStart=c1;
        selectionEnd=c2;
    }
    else {
        selectionStart=c2;
        selectionEnd=c1;
    }
    if (selectionStart.row!=selectionEnd.row||selectionStart.column!=selectionEnd.column) selected=true;
}
texPt editorWindow::clickPos(int x,int y) {
    texPt t;
    t.row=max(0,min((int)text.size()-1,(y-5)/12));
    t.column=max(0,min((int)text[t.row].size(),(x-10)/8));
    return t;
}
void editorWindow::insertText(string text){
	string line;
	int l=0;
	//while(std::getline(text, line)) {
		//if (line==0) 
	//}
}
void editorWindow::setClipboard(string text){
	char * cstr = new char [text.size()+1];
	strcpy (cstr, text.c_str());
	ofxFensterManager::get()->setClipboard(cstr);
	delete[] cstr;
}
string editorWindow::getSelection(){
	string t=text[selectionStart.row].substr(selectionStart.column);
	if (selectionStart.row==selectionEnd.row) {
		t.erase(selectionEnd.column-selectionStart.column);
	}
	else {
		for (int i=selectionStart.row+1;i<selectionEnd.row;i++) {
			t+="\n";
			t+=text[i];
		}
		t+=text[selectionEnd.row].substr(0,selectionEnd.column);
	}
	return t;
}
void editorWindow::deleteSelection(){
    if (selected){
        string t=text[selectionEnd.row].substr(selectionEnd.column);
        text[selectionStart.row].erase(selectionStart.column);
        text[selectionStart.row]+=t;
        if (selectionEnd.row>selectionStart.row) {
            vector<string>::iterator i=text.begin()+selectionStart.row+1;
            vector<string>::iterator j=text.begin()+selectionEnd.row+1;
            text.erase(i,j);
        }
        insertionPoint=selectionStart;
        selected=false;
    }
}
string editorWindow::getText(){
	if (changed) {
		output =="";
		for (int i=0;i<text.size();i++) {
			output +=text[i];
			output +="\n";
		}
		changed=false;
	}
	return output;
}
//--------------------------------------------------------------

void testApp::setup(){
    ofBackground(0);
	ofSetColor(255);
    editorWin=new editorWindow();
	editorWin->setup();
}

//--------------------------------------------------------------
void testApp::update(){
}

//--------------------------------------------------------------
void testApp::draw(){

}

//--------------------------------------------------------------
void testApp::keyPressed(int key){

}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){

}