From 4a3e58d440a64641ceef162aa2594657d9252b13 Mon Sep 17 00:00:00 2001 From: Minjie Zheng Date: Tue, 27 Oct 2015 20:27:02 -0500 Subject: [PATCH 1/7] Add additional functionalities to ofxDatGuiDropdown - reset options after creation - select an option by name --- src/components/ofxDatGuiGroups.h | 43 +++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/components/ofxDatGuiGroups.h b/src/components/ofxDatGuiGroups.h index 44201e6..1821e12 100644 --- a/src/components/ofxDatGuiGroups.h +++ b/src/components/ofxDatGuiGroups.h @@ -391,6 +391,20 @@ class ofxDatGuiDropdown : public ofxDatGuiGroup { mOption = 0; mType = ofxDatGuiType::DROPDOWN; mStripeColor = mTemplate->dropdown.color.stripe; + setOptions(options); + } + + ~ofxDatGuiDropdown() { + clearOptions(); + } + + static ofxDatGuiDropdown* getInstance() + { + return new ofxDatGuiDropdown("X"); + } + + void setOptions(const vector& options) { + clearOptions(); for(int i=0; isetIndex(children.size()); @@ -398,10 +412,30 @@ class ofxDatGuiDropdown : public ofxDatGuiGroup { children.push_back(opt); } } - - static ofxDatGuiDropdown* getInstance() - { - return new ofxDatGuiDropdown("X"); + + void clearOptions() { + if (children.size() > 0) { + for (int i=0; igetName().compare(option)==0) { + optionIndex = i; + break; + } + } + if (optionIndex == -1) { + mOption=0; + setLabel(mName); + } else { + select(optionIndex); + } } void setTemplate(ofxDatGuiTemplate* tmplt) @@ -418,6 +452,7 @@ class ofxDatGuiDropdown : public ofxDatGuiGroup { ofLogError() << "ofxDatGuiDropdown->select("<getLabel()); + mOption = cIndex; } } From 7a44ba0204c34bd241cfa410617a2b99109fc403 Mon Sep 17 00:00:00 2001 From: Minjie Zheng Date: Tue, 27 Oct 2015 22:45:24 -0500 Subject: [PATCH 2/7] Update select option to auto select first item if it exists --- src/components/ofxDatGuiGroups.h | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/components/ofxDatGuiGroups.h b/src/components/ofxDatGuiGroups.h index 1821e12..2258a70 100644 --- a/src/components/ofxDatGuiGroups.h +++ b/src/components/ofxDatGuiGroups.h @@ -423,18 +423,22 @@ class ofxDatGuiDropdown : public ofxDatGuiGroup { } void selectOption(const string& option) { - int optionIndex = -1; - for (int i=0; igetName().compare(option)==0) { - optionIndex = i; - break; + if (children.size() > 0) { + int optionIndex = -1; + for (int i = 0; i < children.size(); i++) { + if (getChildAt(i)->getName().compare(option) == 0) { + optionIndex = i; + break; + } + } + if (optionIndex != -1) { + select(optionIndex); + } else { + select(mOption); } - } - if (optionIndex == -1) { - mOption=0; - setLabel(mName); } else { - select(optionIndex); + mOption = 0; + setLabel(mName); } } From 5be1f157608ff7a2ce8a501896cd392b1718b444 Mon Sep 17 00:00:00 2001 From: Minjie Zheng Date: Thu, 29 Oct 2015 17:25:15 -0500 Subject: [PATCH 3/7] Add customizable label to width ratio to components --- src/core/ofxDatGuiComponent.cpp | 2 +- src/templates/ofxDatGuiTemplate.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/ofxDatGuiComponent.cpp b/src/core/ofxDatGuiComponent.cpp index 75d2a8c..98f0fa1 100644 --- a/src/core/ofxDatGuiComponent.cpp +++ b/src/core/ofxDatGuiComponent.cpp @@ -130,7 +130,7 @@ void ofxDatGuiComponent::setTemplate(ofxDatGuiTemplate* tmplt) void ofxDatGuiComponent::setWidth(int w) { mRow.width = w; - mRow.lWidth=mRow.width*.35; + mRow.lWidth=mRow.width * mTemplate->row.label.lWidthRatio; if (mRow.lWidth > mTemplate->row.label.maxAreaWidth) mRow.lWidth = mTemplate->row.label.maxAreaWidth; mRow.inputX=mRow.lWidth; mRow.rWidth=mRow.width-mRow.inputX; diff --git a/src/templates/ofxDatGuiTemplate.h b/src/templates/ofxDatGuiTemplate.h index d597747..ca935c2 100755 --- a/src/templates/ofxDatGuiTemplate.h +++ b/src/templates/ofxDatGuiTemplate.h @@ -56,6 +56,7 @@ class ofxDatGuiTemplate{ float stripeWidth = 2.0f; struct { float maxAreaWidth = 240.0f; + float lWidthRatio = 0.35f; bool forceUpperCase = true; } label; struct { From 8fce342cd0e1abcc4fe71e8f7ad6a3996a906051 Mon Sep 17 00:00:00 2001 From: Minjie Zheng Date: Thu, 29 Oct 2015 17:49:47 -0500 Subject: [PATCH 4/7] Change GUIs to acquire focus on mouse over --- src/ofxDatGui.cpp | 36 ++++++++++++++++++++++++++++++------ src/ofxDatGui.h | 1 + 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/ofxDatGui.cpp b/src/ofxDatGui.cpp index 2e1836a..2550e26 100644 --- a/src/ofxDatGui.cpp +++ b/src/ofxDatGui.cpp @@ -740,12 +740,16 @@ void ofxDatGui::update() if (mAlignmentChanged) setGuiAlignment(); // first check for gui focus change // - if (ofGetMousePressed() && mActiveGui->isMoving() == false){ + if (!mActiveGui->isMoving()){ ofPoint mouse = ofPoint(ofGetMouseX(), ofGetMouseY()); - for (int i=mGuis.size()-1; i>-1; i--){ + for (auto gui = mGuis.rbegin(); gui != mGuis.rend(); gui++){ // ignore guis that are invisible // - if (mGuis[i]->getVisible() && mGuis[i]->hitTest(mouse)){ - if (mGuis[i] != mActiveGui) mGuis[i]->focus(); + if ((*gui)->getVisible() && (*gui)->hitTest(mouse)) { + if ((*gui) != mActiveGui) { + // let the previous gui process loss of focus + mActiveGui->onFocusLost(); + (*gui)->focus(); + } break; } } @@ -757,9 +761,9 @@ void ofxDatGui::update() } else { mMoving = false; // this gui has focus so let's see if any of its components were interacted with // - if (mExpanded == false){ + if (!mExpanded){ mGuiFooter->update(); - } else{ + } else { int activeItemIndex = -1; for (int i=0; iupdate(); @@ -799,6 +803,26 @@ void ofxDatGui::update() trash.clear(); } +void ofxDatGui::onFocusLost() { + // losing focus is essentially the mouse leaving even if the mouse is actually in the unfocused gui + // so manually update the the components as if the mouse left + if (mGuiHeader != nullptr) { + mGuiHeader->onMouseLeave(ofPoint(0,0)); + } + if (mGuiFooter != nullptr) { + mGuiFooter->onMouseLeave(ofPoint(0,0)); + mGuiFooter->onFocusLost(); + } + for (auto* item : items) { + for (auto* child : item->children) { + child->onMouseLeave(ofPoint(0,0)); + child->onFocusLost(); + } + item->onMouseLeave(ofPoint(0,0)); + item->onFocusLost(); + } +} + void ofxDatGui::draw() { if (mVisible == false) return; diff --git a/src/ofxDatGui.h b/src/ofxDatGui.h index 6929a11..2893ee2 100644 --- a/src/ofxDatGui.h +++ b/src/ofxDatGui.h @@ -35,6 +35,7 @@ class ofxDatGui : public ofxDatGuiInteractiveObject void draw(); void focus(); void update(); + void onFocusLost(); bool isMoving(); void setWidth(int width); From c7f92d0a3e0e3edd0a0566eb3e3f73f95136261a Mon Sep 17 00:00:00 2001 From: Minjie Zheng Date: Thu, 29 Oct 2015 18:47:30 -0500 Subject: [PATCH 5/7] Fix toggle still being drawn even when invisible --- src/components/ofxDatGuiButton.h | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/components/ofxDatGuiButton.h b/src/components/ofxDatGuiButton.h index a5f97d3..cf37509 100644 --- a/src/components/ofxDatGuiButton.h +++ b/src/components/ofxDatGuiButton.h @@ -127,17 +127,19 @@ class ofxDatGuiToggle : public ofxDatGuiButton { void draw() { - ofxDatGuiButton::drawBkgd(); - ofxDatGuiComponent::drawLabel(); - ofxDatGuiComponent::drawStripe(); - ofPushStyle(); + if (mVisible) { + ofxDatGuiButton::drawBkgd(); + ofxDatGuiComponent::drawLabel(); + ofxDatGuiComponent::drawStripe(); + ofPushStyle(); ofSetColor(mTemplate->row.color.label); - if (mEnabled == true){ - radioOn.draw(x+mIcon.x, y+mIcon.y, mIcon.size, mIcon.size); - } else{ - radioOff.draw(x+mIcon.x, y+mIcon.y, mIcon.size, mIcon.size); + if (mEnabled) { + radioOn.draw(x + mIcon.x, y + mIcon.y, mIcon.size, mIcon.size); + } else { + radioOff.draw(x + mIcon.x, y + mIcon.y, mIcon.size, mIcon.size); } - ofPopStyle(); + ofPopStyle(); + } } void onMouseRelease(ofPoint m) From 894d0bcb137e8b1e541063a1804a587ee58c9cad Mon Sep 17 00:00:00 2001 From: Minjie Zheng Date: Thu, 29 Oct 2015 18:48:03 -0500 Subject: [PATCH 6/7] Fix components still processing events when invisible --- src/ofxDatGui.cpp | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/ofxDatGui.cpp b/src/ofxDatGui.cpp index 2550e26..e249c2a 100644 --- a/src/ofxDatGui.cpp +++ b/src/ofxDatGui.cpp @@ -766,25 +766,28 @@ void ofxDatGui::update() } else { int activeItemIndex = -1; for (int i=0; iupdate(); - if (items[i]->getFocused()) { - activeItemIndex = i; - if (mGuiHeader != nullptr && mGuiHeader->getDraggable() && mGuiHeader->getPressed()){ - // track that we're moving to force preserve focus // - mMoving = true; - ofPoint mouse = ofPoint(ofGetMouseX(), ofGetMouseY()); - moveGui(mouse - mGuiHeader->dragOffset); - } - break; - } else if (items[i]->getIsExpanded()){ - // check if one of its children has focus // - for (int j=0; jchildren.size(); j++) { - if (items[i]->children[j]->getFocused()){ - activeItemIndex = i; - break; + // only update if the item is visible + if (items[i]->getVisible()) { + items[i]->update(); + if (items[i]->getFocused()) { + activeItemIndex = i; + if (mGuiHeader != nullptr && mGuiHeader->getDraggable() && mGuiHeader->getPressed()) { + // track that we're moving to force preserve focus // + mMoving = true; + ofPoint mouse = ofPoint(ofGetMouseX(), ofGetMouseY()); + moveGui(mouse - mGuiHeader->dragOffset); + } + break; + } else if (items[i]->getIsExpanded()) { + // check if one of its children has focus // + for (int j = 0; j < items[i]->children.size(); j++) { + if (items[i]->children[j]->getFocused()) { + activeItemIndex = i; + break; + } } + if (activeItemIndex != -1) break; } - if (activeItemIndex != -1) break; } } // update the remaining components // From 4fc25c41d83dc3eb7fef0b7a4aa91aa7f47a9c5e Mon Sep 17 00:00:00 2001 From: Minjie Zheng Date: Mon, 16 Nov 2015 23:32:54 -0600 Subject: [PATCH 7/7] Fix option index not resetting after clearing all options --- src/components/ofxDatGuiGroups.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/ofxDatGuiGroups.h b/src/components/ofxDatGuiGroups.h index 2258a70..9f69bc1 100644 --- a/src/components/ofxDatGuiGroups.h +++ b/src/components/ofxDatGuiGroups.h @@ -420,6 +420,7 @@ class ofxDatGuiDropdown : public ofxDatGuiGroup { } children.clear(); } + mOption = 0; } void selectOption(const string& option) {