-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
302 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
// Local Includes | ||
#include "parameterdropdown.h" | ||
|
||
RTTI_BEGIN_CLASS(nap::ParameterDropDown) | ||
RTTI_PROPERTY("Items", &nap::ParameterDropDown::mItems, nap::rtti::EPropertyMetaData::Default) | ||
RTTI_PROPERTY("SelectedIndex", &nap::ParameterDropDown::mSelectedIndex, nap::rtti::EPropertyMetaData::Default) | ||
RTTI_END_CLASS | ||
|
||
namespace nap | ||
{ | ||
|
||
void ParameterDropDown::setValue(const Parameter& value) | ||
{ | ||
const ParameterDropDown* derived_type = rtti_cast<const ParameterDropDown>(&value); | ||
assert(derived_type != nullptr); | ||
|
||
mItems = derived_type->mItems; | ||
mSelectedIndex = derived_type->mSelectedIndex; | ||
} | ||
|
||
|
||
void ParameterDropDown::setSelectedIndex(int selectedIndex) | ||
{ | ||
if(mSelectedIndex!=selectedIndex && selectedIndex < mItems.size()) | ||
{ | ||
mSelectedIndex = selectedIndex; | ||
indexChanged.trigger(mSelectedIndex); | ||
} | ||
} | ||
|
||
|
||
void ParameterDropDown::setItems(const std::vector<std::string> &items) | ||
{ | ||
mItems = items; | ||
itemsChanged.trigger(mItems); | ||
if(mSelectedIndex >= mItems.size()) | ||
{ | ||
mSelectedIndex = mItems.size() - 1; | ||
indexChanged.trigger(mSelectedIndex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#pragma once | ||
|
||
// Local Includes | ||
#include "parameter.h" | ||
|
||
// External Includes | ||
#include <nap/signalslot.h> | ||
|
||
namespace nap | ||
{ | ||
/** | ||
* | ||
*/ | ||
class NAPAPI ParameterDropDown : public Parameter | ||
{ | ||
RTTI_ENABLE(Parameter) | ||
public: | ||
/** | ||
* Set the value for this parameter from another parameter | ||
* @param value The parameter to set the value from | ||
*/ | ||
virtual void setValue(const Parameter& value) override; | ||
|
||
/** | ||
* @param | ||
*/ | ||
void setSelectedIndex(int selectedIndex); | ||
|
||
/** | ||
* @return | ||
*/ | ||
int getSelectedIndex(){ return mSelectedIndex; } | ||
|
||
/** | ||
* | ||
*/ | ||
void setItems(const std::vector<std::string>& items); | ||
|
||
// Signals | ||
Signal<int> indexChanged; | ||
Signal<const std::vector<std::string>&> itemsChanged; | ||
public: | ||
std::vector<std::string> mItems; ///< | ||
int mSelectedIndex = 0; ///< | ||
private: | ||
|
||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
// Local Includes | ||
#include "portalitemdropdown.h" | ||
#include "portalutils.h" | ||
|
||
// External Includes | ||
#include <apivalue.h> | ||
|
||
RTTI_BEGIN_CLASS(nap::PortalItemDropDown) | ||
RTTI_PROPERTY("Parameter", &nap::PortalItemDropDown::mParameter, nap::rtti::EPropertyMetaData::Required) | ||
RTTI_END_CLASS | ||
|
||
////////////////////////////////////////////////////////////////////////// | ||
|
||
namespace nap | ||
{ | ||
|
||
bool PortalItemDropDown::init(utility::ErrorState& errorState) | ||
{ | ||
mParameter->itemsChanged.connect(mItemsChangedSlot); | ||
mParameter->indexChanged.connect(mIndexChangedSlot); | ||
|
||
return true; | ||
} | ||
|
||
|
||
void PortalItemDropDown::onDestroy() | ||
{ | ||
mParameter->itemsChanged.disconnect(mItemsChangedSlot); | ||
mParameter->indexChanged.disconnect(mIndexChangedSlot); | ||
} | ||
|
||
|
||
void PortalItemDropDown::onIndexChanged(int newIndex) | ||
{ | ||
updateSignal(*this); | ||
} | ||
|
||
|
||
void PortalItemDropDown::onItemsChanged(const std::vector<std::string> &newItems) | ||
{ | ||
updateSignal(*this); | ||
} | ||
|
||
|
||
bool PortalItemDropDown::processUpdate(const APIEvent& event, utility::ErrorState& error) | ||
{ | ||
const APIArgument* selected_index_arg = event.getArgumentByName(nap::portal::itemValueArgName); | ||
if (!error.check(selected_index_arg != nullptr, "%s: update event missing argument %s", mID.c_str(), nap::portal::itemValueArgName)) | ||
return false; | ||
|
||
const rtti::TypeInfo selected_index_type = selected_index_arg->getValueType(); | ||
if (!error.check(selected_index_type == RTTI_OF(int), "%s: cannot process value type %s", mID.c_str(), selected_index_type.get_name().data())) | ||
return false; | ||
|
||
int index = selected_index_arg->asInt(); | ||
|
||
mParameter->indexChanged.disconnect(mIndexChangedSlot); | ||
|
||
if(index!=mParameter->getSelectedIndex()) | ||
mParameter->setSelectedIndex(index); | ||
|
||
mParameter->indexChanged.connect(mIndexChangedSlot); | ||
|
||
return true; | ||
}; | ||
|
||
|
||
APIEventPtr PortalItemDropDown::getDescriptor() const | ||
{ | ||
APIEventPtr event = std::make_unique<APIEvent>(mParameter->getDisplayName(), mID); | ||
event->addArgument<APIString>(nap::portal::itemTypeArgName, get_type().get_name().data()); | ||
event->addArgument<APIValue<std::vector<std::string>>>(nap::portal::dropDownItemNames, mParameter->mItems); | ||
event->addArgument<APIValue<int>>(nap::portal::itemValueArgName, mParameter->mSelectedIndex); | ||
|
||
return event; | ||
} | ||
|
||
|
||
APIEventPtr PortalItemDropDown::getValue() const | ||
{ | ||
APIEventPtr event = std::make_unique<APIEvent>(mParameter->getDisplayName(), mID); | ||
event->addArgument<APIStringArray>(nap::portal::dropDownItemNames, mParameter->mItems); | ||
event->addArgument<APIInt>(nap::portal::itemValueArgName, mParameter->mSelectedIndex); | ||
|
||
return event; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#pragma once | ||
|
||
// Local Includes | ||
#include "portalitem.h" | ||
|
||
// External Includes | ||
#include <apievent.h> | ||
#include <parameterdropdown.h> | ||
|
||
namespace nap | ||
{ | ||
/** | ||
* | ||
*/ | ||
class PortalItemDropDown : public PortalItem | ||
{ | ||
RTTI_ENABLE(PortalItem) | ||
|
||
public: | ||
|
||
/** | ||
* | ||
* @param errorState | ||
* @return | ||
*/ | ||
bool init(utility::ErrorState& errorState) override; | ||
|
||
/** | ||
* | ||
*/ | ||
void onDestroy() override; | ||
|
||
/** | ||
* Processes an update type API event. | ||
* @param event The event to be processed | ||
* @param error contains information when processing fails | ||
* @return if the event was processed successfully | ||
*/ | ||
virtual bool processUpdate(const APIEvent& event, utility::ErrorState& error) override; | ||
|
||
/** | ||
* @return the descriptor of the portal item as an API event | ||
*/ | ||
virtual APIEventPtr getDescriptor() const override; | ||
|
||
/** | ||
* @return the current value of the portal item as an API event | ||
*/ | ||
virtual APIEventPtr getValue() const override; | ||
|
||
ResourcePtr<ParameterDropDown> mParameter; ///< | ||
private: | ||
/** | ||
* | ||
*/ | ||
Slot<int> mIndexChangedSlot = { this, &PortalItemDropDown::onIndexChanged }; | ||
void onIndexChanged(int newIndex); | ||
|
||
/** | ||
* | ||
*/ | ||
Slot<const std::vector<std::string>&> mItemsChangedSlot = { this, &PortalItemDropDown::onItemsChanged }; | ||
void onItemsChanged(const std::vector<std::string>& newItems); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters