Skip to content

Commit 37a8616

Browse files
committed
added int random option
1 parent f094f44 commit 37a8616

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/objects/math/SimpleRandom.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ SimpleRandom::SimpleRandom() :
5959

6060
bang = false;
6161

62+
forceInt = false;
63+
64+
loaded = false;
65+
6266
}
6367

6468
//--------------------------------------------------------------
@@ -69,6 +73,11 @@ void SimpleRandom::newObject(){
6973
this->addInlet(VP_LINK_NUMERIC,"min");
7074
this->addInlet(VP_LINK_NUMERIC,"max");
7175
this->addOutlet(VP_LINK_NUMERIC,"random");
76+
77+
this->setCustomVar(static_cast<float>(lastMinRange.get()),"MIN");
78+
this->setCustomVar(static_cast<float>(lastMaxRange.get()),"MAX");
79+
this->setCustomVar(static_cast<float>(forceInt),"FORCE_INT");
80+
7281
}
7382

7483
//--------------------------------------------------------------
@@ -86,7 +95,12 @@ void SimpleRandom::updateObjectContent(map<int,shared_ptr<PatchObject>> &patchOb
8695
}
8796
}
8897
if(bang){
89-
*(float *)&_outletParams[0] = ofRandom(*(float *)&_inletParams[1],*(float *)&_inletParams[2]);
98+
if(forceInt){
99+
*(float *)&_outletParams[0] = floor(ofRandom(lastMinRange.get(),lastMaxRange.get()));
100+
}else{
101+
*(float *)&_outletParams[0] = ofRandom(lastMinRange.get(),lastMaxRange.get());
102+
}
103+
90104
}
91105

92106
if(this->inletsConnected[1] || this->inletsConnected[2]){
@@ -95,6 +109,14 @@ void SimpleRandom::updateObjectContent(map<int,shared_ptr<PatchObject>> &patchOb
95109
lastMaxRange.get() = *(float *)&_inletParams[2];
96110
}
97111
}
112+
113+
if(!loaded){
114+
loaded = true;
115+
116+
lastMinRange.get() = this->getCustomVar("MIN");
117+
lastMaxRange.get() = this->getCustomVar("MAX");
118+
forceInt = static_cast<bool>(floor(this->getCustomVar("FORCE_INT")));
119+
}
98120
}
99121

100122
//--------------------------------------------------------------
@@ -136,10 +158,18 @@ void SimpleRandom::drawObjectNodeGui( ImGuiEx::NodeCanvas& _nodeCanvas ){
136158
void SimpleRandom::drawObjectNodeConfig(){
137159
ImGui::Spacing();
138160
ImGui::PushItemWidth(130*scaleFactor);
139-
ImGui::DragFloat("min", &lastMinRange.get());
161+
if(ImGui::DragFloat("min", &lastMinRange.get())){
162+
this->setCustomVar(static_cast<float>(lastMinRange.get()),"MIN");
163+
}
140164
ImGui::Spacing();
141165
ImGui::PushItemWidth(130*scaleFactor);
142-
ImGui::DragFloat("max", &lastMaxRange.get());
166+
if(ImGui::DragFloat("max", &lastMaxRange.get())){
167+
this->setCustomVar(static_cast<float>(lastMaxRange.get()),"MAX");
168+
}
169+
ImGui::Spacing();
170+
if(ImGui::Checkbox("Force Integer",&forceInt)){
171+
this->setCustomVar(static_cast<float>(forceInt),"FORCE_INT");
172+
}
143173

144174
ImGuiEx::ObjectInfo(
145175
"Standard Range controlled random number generator.",

src/objects/math/SimpleRandom.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class SimpleRandom : public PatchObject {
5656

5757

5858
bool bang;
59+
bool forceInt;
60+
61+
bool loaded;
5962

6063
protected:
6164

0 commit comments

Comments
 (0)