#include "testApp.h" //-------------------------------------------------------------- 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=""; windowResized(400,400); insX=2;insY=2; scrollY=false;scrollX=false; scrollPosY=0;scrollPosX=0; scrollSelY=scrollSelX=false; } void editorWindow::screenResize(){ w=ofGetWidth();h=ofGetHeight(); screen.allocate(w-(insX*2)-(scrollY?15:0),h-(insY*2)-(scrollX?15:0)); printf("editor window resized to %ix%i\n",w,h); changed=true; } void editorWindow::draw(){ bool sY=(((text.size()*12)+(insY*2))>h); if (sY!=scrollY||w!=ofGetWidth()||h!=ofGetHeight()) { scrollY=sY; screenResize(); } if (changed) drawScreen(); ofSetColor(255,255,255); screen.draw(insX,insY); if (!selected) { //draw insertion point if ((ofGetElapsedTimeMillis()/300)%2) { ofRect((insertionPoint.column*8)+insX,(insertionPoint.row*12)+insY,2,10); } } if (scrollY) { if (scrollX) { ofLine(0,h-15,w-15,h-15); ofLine(w-15,0,w-15,h-15); } else ofLine(w-15,0,w-15,h); } else if (scrollX) ofLine(0,h-15,w,h-15); if (scrollY) { float yBarSize=(((float)h-(insX*2)-15)/(text.size()*12))*((float)h-(insX*2)-15); float yBarPos=2; //+ actual scroll amount if (scrollSelY) ofFill(); else ofNoFill(); ofRect(w-12,yBarPos,11,yBarSize); ofFill(); } } void editorWindow::drawScreen(){ screen.begin(); ofBackground(0); ofDisableAlphaBlending(); ofSetColor(255,255,255); for (int i=0;i::iterator i; string t; int l; unsigned char *c; 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.rowgetClipboard(); if (c>0) insertText(string(reinterpret_cast(c))); break; default: if (key==9) key=32; //convert tab to space for simplicity if (key>31 && key<127) { 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){ clickX=x; clickY=y; if (scrollY&&x>(h-16)) { scrollSelY=true; } else { selected=false; changed=true; } } void editorWindow::mouseReleased(int x, int y, int button){ if (scrollSelY) { scrollSelY=false; } else { if (clickX=x&&clickY==y) { insertionPoint=clickPos(x,y); } changed=true; } } void editorWindow::mouseDragged(int x, int y, int button){ if (scrollSelY) { } else { 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; changed=true; } } texPt editorWindow::clickPos(int x,int y) { texPt t; t.row=max(0,min((int)text.size()-1,(y-insY)/12)); t.column=max(0,min((int)text[t.row].size(),(x-insX)/8)); return t; } void editorWindow::insertText(string t){ // printf("%s\n",t.c_str()); for (int i=0;i lines; int l=0; while(std::getline(in, line)) { lines.push_back(line); } //for (int i=0;i::iterator i=text.begin()+insertionPoint.row+1; text[insertionPoint.row].erase(insertionPoint.column); text[insertionPoint.row].insert(insertionPoint.column,lines[0]); for (int j=1;jinsertionPoint.row+j) text.insert(i,lines[j]); //crashes on last iter? else text.push_back(lines[j]); i++; } insertionPoint.row+=lines.size()-1; if (text.size()>insertionPoint.column) { insertionPoint.column=text[insertionPoint.row].size(); text[insertionPoint.row].append(t1); } } } 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){ }