Skip to content

Commit

Permalink
simplified members
Browse files Browse the repository at this point in the history
  • Loading branch information
fx-lange committed Oct 25, 2015
1 parent 3892e04 commit cb42f0c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
29 changes: 12 additions & 17 deletions src/ofxNumEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ ofxNumEdit<Type>::ofxNumEdit(){
bMousePressed = false;
mouseInside = false;
bRegisteredForKeyEvents = false;
selectIdx1 = -1;
selectIdx2 = -1;
mousePressedPos = -1;
selectStartX = -1;
selectStartPos = -1;
selectEndPos = -1;
Expand Down Expand Up @@ -77,7 +76,7 @@ Type ofxNumEdit<Type>::getMax(){
}

template<typename Type>
void ofxNumEdit<Type>::calculateSelectionArea(){
void ofxNumEdit<Type>::calculateSelectionArea(int selectIdx1, int selectIdx2){
std::string preSelectStr, selectStr;

if(selectIdx1 <= selectIdx2){
Expand Down Expand Up @@ -119,10 +118,10 @@ bool ofxNumEdit<Type>::mousePressed(ofMouseEventArgs & args){
}

float cursorX = args.x - (b.x + b.width - textPadding - valueStrWidth);
int cursorIdx = ofMap(cursorX,0,valueStrWidth,0,valueStr.size(),true);
selectIdx1 = selectIdx2 = cursorIdx;
int cursorPos = ofMap(cursorX,0,valueStrWidth,0,valueStr.size(),true);
mousePressedPos = cursorPos;

calculateSelectionArea();
calculateSelectionArea(cursorPos, cursorPos);

pressCounter++;

Expand All @@ -140,9 +139,8 @@ bool ofxNumEdit<Type>::mouseDragged(ofMouseEventArgs & args){
return false;

float cursorX = args.x - (b.x + b.width - textPadding - valueStrWidth);
int cursorIdx = ofMap(cursorX,0,valueStrWidth,0,valueStr.size(),true);
selectIdx2 = cursorIdx;
calculateSelectionArea();
int cursorPos = ofMap(cursorX,0,valueStrWidth,0,valueStr.size(),true);
calculateSelectionArea(mousePressedPos,cursorPos);
return false;
}

Expand All @@ -154,9 +152,7 @@ bool ofxNumEdit<Type>::mouseReleased(ofMouseEventArgs & args){
if(bGuiActive){
if(pressCounter == 1 && !hasSelectedText()){
//activated panel without selecting an area => select all
selectIdx1 = 0;
selectIdx2 = valueStr.size();
calculateSelectionArea();
calculateSelectionArea(0, valueStr.size());
}
}

Expand Down Expand Up @@ -239,8 +235,7 @@ void ofxNumEdit<Type>::keyPressed(ofKeyEventArgs & args){

if(newCursorIdx != -1){
//set cursor
selectIdx1 = selectIdx2 = newCursorIdx;
calculateSelectionArea();
calculateSelectionArea(newCursorIdx,newCursorIdx);
}
}
}
Expand Down Expand Up @@ -332,7 +327,7 @@ void ofxNumEdit<Type>::render(){

template<typename Type>
bool ofxNumEdit<Type>::hasSelectedText(){
return selectIdx1 != selectIdx2;
return selectStartPos != selectEndPos;
}

template<typename Type>
Expand Down Expand Up @@ -417,8 +412,8 @@ void ofxNumEdit<Type>::valueChanged(Type & value){
valueStr = ofToString(value);
valueStrWidth = getTextBoundingBox(valueStr,0,0).width;
if(bGuiActive){
selectIdx1 = selectIdx2 = valueStr.size();
calculateSelectionArea();
int cursorPos = valueStr.size();
calculateSelectionArea(cursorPos,cursorPos);
}
}
setNeedsRedraw();
Expand Down
4 changes: 2 additions & 2 deletions src/ofxNumEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ofxNumEdit : public ofxBaseGui{
void setValue(std::string valStr);
bool bChangedInternally;

int selectIdx1, selectIdx2; //set by mouse interaction
int mousePressedPos; //set by mouse interaction
bool hasSelectedText();

virtual void drawSelectedArea();
Expand All @@ -77,7 +77,7 @@ class ofxNumEdit : public ofxBaseGui{

float selectStartX, selectWidth; //calculated from select indices
int selectStartPos, selectEndPos;
void calculateSelectionArea();
void calculateSelectionArea(int selectIdx1, int selectIdx2);

int pressCounter;

Expand Down

0 comments on commit cb42f0c

Please sign in to comment.