Skip to content

Commit 0cfb4e9

Browse files
committed
Switched from messages to events for notifications.
Updated example with event callbacks.
1 parent 898638e commit 0cfb4e9

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

example-textInput/src/ofApp.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ void ofApp::setup() {
4343
multilineTextInput.bounds.height = 500;
4444
multilineTextInput.multiline = true;
4545

46+
// Add event listeners.
47+
ofAddListener(monoLineTextInput.editingBegan, this, &ofApp::editingBegan);
48+
ofAddListener(monoLineTextInput.editingEnded, this, &ofApp::editingEnded);
49+
ofAddListener(monoLineTextInput.textChanged, this, &ofApp::textChanged);
4650
}
4751

4852
//--------------------------------------------------------------
@@ -103,3 +107,18 @@ void ofApp::gotMessage(ofMessage msg) {
103107
void ofApp::dragEvent(ofDragInfo dragInfo) {
104108

105109
}
110+
111+
//--------------------------------------------------------------
112+
void ofApp::editingBegan(const void* sender) {
113+
cout << "Editing began with text: " << ((ofxTextInputField *)sender)->text << endl;
114+
}
115+
116+
//--------------------------------------------------------------
117+
void ofApp::editingEnded(const void* sender) {
118+
cout << "Editing ended with text: " << ((ofxTextInputField *)sender)->text << endl;
119+
}
120+
121+
//--------------------------------------------------------------
122+
void ofApp::textChanged(const void* sender, string& text) {
123+
cout << "Text changed to: " << text << endl;
124+
}

example-textInput/src/ofApp.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ class ofApp : public ofBaseApp{
2121
void windowResized(int w, int h);
2222
void dragEvent(ofDragInfo dragInfo);
2323
void gotMessage(ofMessage msg);
24-
24+
25+
void editingBegan(const void* sender);
26+
void editingEnded(const void* sender);
27+
void textChanged(const void* sender, string& text);
2528

2629
ofxTextInputField monoLineTextInput, multilineTextInput;
2730

src/ofxTextInputField.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void ofxTextInputField::beginEditing() {
110110
if(!isEditing){
111111
ofAddListener(ofEvents().keyPressed, this, &ofxTextInputField::keyPressed);
112112
ofAddListener(ofEvents().keyReleased, this, &ofxTextInputField::keyReleased);
113-
ofSendMessage(TEXTFIELD_IS_ACTIVE);
113+
ofNotifyEvent(editingBegan, this);
114114
isEditing = true;
115115
drawCursor = true;
116116
if(autoClear){
@@ -127,8 +127,7 @@ void ofxTextInputField::endEditing() {
127127
if(isEditing){
128128
ofRemoveListener(ofEvents().keyPressed, this, &ofxTextInputField::keyPressed);
129129
ofRemoveListener(ofEvents().keyReleased, this, &ofxTextInputField::keyReleased);
130-
ofSendMessage(TEXTFIELD_IS_INACTIVE);
131-
ofNotifyEvent(textChanged, text, this);
130+
ofNotifyEvent(editingEnded, this);
132131
isEditing = false;
133132
drawCursor = false;
134133
}
@@ -450,6 +449,7 @@ void ofxTextInputField::keyPressed(ofKeyEventArgs& args) {
450449
}
451450
}
452451

452+
ofNotifyEvent(textChanged, text, this);
453453
return;
454454
}
455455

@@ -469,6 +469,8 @@ void ofxTextInputField::keyPressed(ofKeyEventArgs& args) {
469469
text.insert(text.begin()+cursorPosition, key);
470470
}
471471
cursorPosition++;
472+
473+
ofNotifyEvent(textChanged, text, this);
472474
}
473475

474476

@@ -485,6 +487,8 @@ void ofxTextInputField::keyPressed(ofKeyEventArgs& args) {
485487
--cursorPosition;
486488
}
487489
}
490+
491+
ofNotifyEvent(textChanged, text, this);
488492
}
489493

490494
if (key==OF_KEY_DEL) {
@@ -499,6 +503,8 @@ void ofxTextInputField::keyPressed(ofKeyEventArgs& args) {
499503
text.erase(text.begin()+cursorPosition);
500504
}
501505
}
506+
507+
ofNotifyEvent(textChanged, text, this);
502508
}
503509

504510
if (key==OF_KEY_LEFT){

src/ofxTextInputField.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
#include OFX_TEXTFIELD_FONT_INCLUDE
3232
#endif
3333

34-
#define TEXTFIELD_IS_ACTIVE "textfieldIsActive"
35-
#define TEXTFIELD_IS_INACTIVE "textfieldIsInactive"
36-
3734
#ifdef OF_VERSION_MINOR
3835
#if OF_VERSION_MINOR>=8 || OF_VERSION_MAJOR>0
3936
#define USE_GLFW_CLIPBOARD
@@ -83,7 +80,10 @@ class ofxTextInputField {
8380
int selectionEnd;
8481
bool selecting;
8582

83+
ofEvent<void> editingBegan;
84+
ofEvent<void> editingEnded;
8685
ofEvent<string> textChanged;
86+
8787
void keyPressed(ofKeyEventArgs &a);
8888
void keyReleased(ofKeyEventArgs &a);
8989

0 commit comments

Comments
 (0)