#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::iterator i; string t; int l; switch (key) { case OF_KEY_DEL: if (selected) deleteSelection(); else { if (insertionPoint.column0) 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.columninsertionPoint.column) { insertionPoint.row++; insertionPoint.column=min(insertionPoint.column,(int)text[insertionPoint.row].size()); } } break; case OF_KEY_DOWN: if (insertionPoint.row(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;iselectionStart.row) { vector::iterator i=text.begin()+selectionStart.row+1; vector::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;isetup(); } //-------------------------------------------------------------- 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){ }