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) diff --git a/src/components/ofxDatGuiGroups.h b/src/components/ofxDatGuiGroups.h index 44201e6..9f69bc1 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,35 @@ 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; i 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); + } + } else { + mOption = 0; + setLabel(mName); + } } void setTemplate(ofxDatGuiTemplate* tmplt) @@ -418,6 +457,7 @@ class ofxDatGuiDropdown : public ofxDatGuiGroup { ofLogError() << "ofxDatGuiDropdown->select("<getLabel()); + mOption = cIndex; } } 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/ofxDatGui.cpp b/src/ofxDatGui.cpp index 2e1836a..e249c2a 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,30 +761,33 @@ 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(); - 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 // @@ -799,6 +806,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); 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 {