Skip to content

Commit

Permalink
- added ParameterDropDown
Browse files Browse the repository at this point in the history
- added PortalItemDropDown
  • Loading branch information
TimGroeneboom committed Sep 12, 2023
1 parent 11a0be5 commit bf20942
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 4 deletions.
25 changes: 22 additions & 3 deletions demos/webportal/data/portal.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
"Type": "nap::PortalItemIVec3",
"mID": "PortalItemIVec3",
"Parameter": "ParameterIVec3"
},
{
"Type": "nap::PortalItemDropDown",
"mID": "PortalItemDropDown",
"Parameter": "ParameterDropDown"
}
]
}
Expand Down Expand Up @@ -352,6 +357,18 @@
"Clamp": false,
"Minimum": -128,
"Maximum": 128
},
{
"Type": "nap::ParameterDropDown",
"mID": "ParameterDropDown",
"Name": "My DropDown Parameter",
"Items": [
"Apples",
"Bananas",
"Oranges",
"Melons"
],
"SelectedIndex": 1
}
],
"Groups": []
Expand All @@ -373,7 +390,7 @@
"Title": "Portal Demo",
"Width": 1280,
"Height": 720,
"Mode": "Immediate",
"Mode": "FIFO",
"ClearColor": {
"Values": [
0.0,
Expand All @@ -383,7 +400,9 @@
]
},
"Samples": "Four",
"AdditionalSwapImages": 1
"AdditionalSwapImages": 1,
"RestoreSize": true,
"RestorePosition": true
},
{
"Type": "nap::Scene",
Expand Down Expand Up @@ -411,7 +430,7 @@
"Type": "nap::WebSocketTicket",
"mID": "WebSocketTicket",
"UserName": "napuser",
"Password": "letmein!"
"Password": "letmein!2"
}
]
}
Expand Down
46 changes: 46 additions & 0 deletions system_modules/napparameter/src/parameterdropdown.cpp
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);
}
}
}
52 changes: 52 additions & 0 deletions system_modules/napparameter/src/parameterdropdown.h
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:

};
}
20 changes: 20 additions & 0 deletions system_modules/napparametergui/src/parameterguiservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <parametercolor.h>
#include <parameterquat.h>
#include <parameterbutton.h>
#include <parameterdropdown.h>
#include <imgui/imgui.h>
#include <parameterservice.h>

Expand Down Expand Up @@ -291,6 +292,25 @@ namespace nap
if (ImGui::IsItemClicked())
button_parameter->click();
});


registerParameterEditor(RTTI_OF(ParameterDropDown), [](Parameter& parameter)
{
auto* parameter_dropdown = rtti_cast<ParameterDropDown>(&parameter);
int index = parameter_dropdown->getSelectedIndex();

std::vector<rttr::string_view> items(parameter_dropdown->mItems.begin(), parameter_dropdown->mItems.end());

if (ImGui::Combo(parameter.getDisplayName().c_str(), &index, [](void* data, int index, const char** out_text)
{
std::vector<rttr::string_view>* items = (std::vector<rttr::string_view>*)data;
*out_text = (*items)[index].data();
return true;
}, &items, items.size()))
{
parameter_dropdown->setSelectedIndex(index);
}
});
}


Expand Down
91 changes: 91 additions & 0 deletions system_modules/napportal/src/portalitemdropdown.cpp
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;
}
}
69 changes: 69 additions & 0 deletions system_modules/napportal/src/portalitemdropdown.h
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);
};
}
3 changes: 2 additions & 1 deletion system_modules/napportal/src/portalutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace nap
inline constexpr const char* itemMinArgName = "portal_item_min"; ///< Name of the argument containing the minimum portal item value in the portal item message
inline constexpr const char* itemMaxArgName = "portal_item_max"; ///< Name of the argument containing the maximum portal item value in the portal item message
inline constexpr const char* itemClampArgName = "portal_item_clamp"; ///< Name of the argument containing the clamp value in the portal item message
}
inline constexpr const char* dropDownItemNames = "portal_dropdown_item_names";
}

/**
* Enum that describes the type of portal event, which determines the effect of the event
Expand Down

0 comments on commit bf20942

Please sign in to comment.