Skip to content

Commit

Permalink
#5231: Reorganise BulgePatch command to separate algorithm and UI
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed May 22, 2020
1 parent 7993dcd commit 0158137
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 40 deletions.
2 changes: 1 addition & 1 deletion install/menu.xml
Expand Up @@ -302,7 +302,7 @@
<menuItem name="capselection" caption="Cap Selection" command="CapCurrentCurve" icon="curve_cap.png" />
<menuSeparator />
<menuItem name="stitchTextures" caption="Stitch Patch Textures" command="StitchPatchTexture" />
<menuItem name="bulgePatch" caption="Bulge patch" command="BulgePatch" />
<menuItem name="bulgePatch" caption="Bulge Patch..." command="BulgePatchDialog" />
</subMenu>

<subMenu name="curve" caption="&amp;Curve">
Expand Down
2 changes: 1 addition & 1 deletion radiant/patch/PatchModule.cpp
Expand Up @@ -105,7 +105,7 @@ void PatchModule::registerPatchCommands()
GlobalCommandSystem().addCommand("CapCurrentCurve", selection::algorithm::capPatch);
GlobalCommandSystem().addCommand("ThickenPatch", selection::algorithm::thickenPatches);
GlobalCommandSystem().addCommand("StitchPatchTexture", patch::algorithm::stitchTextures);
GlobalCommandSystem().addCommand("BulgePatch", patch::algorithm::bulge);
GlobalCommandSystem().addCommand("BulgePatch", patch::algorithm::bulge, { cmd::ARGTYPE_DOUBLE });
}

module::StaticModule<PatchModule> patchModule;
Expand Down
64 changes: 30 additions & 34 deletions radiant/patch/algorithm/General.cpp
Expand Up @@ -6,8 +6,6 @@
#include "ipatch.h"
#include "patch/PatchNode.h"
#include "patch/Patch.h"
#include "wxutil/dialog/MessageBox.h"
#include "ui/patch/BulgePatchDialog.h"
#include "ui/surfaceinspector/SurfaceInspector.h"
#include "selection/algorithm/Primitives.h"

Expand Down Expand Up @@ -94,22 +92,21 @@ void stitchTextures(const cmd::ArgumentList& args)
UndoableCommand undo("patchStitchTexture");

// Fetch the instances from the selectionsystem
const scene::INodePtr& targetNode =
GlobalSelectionSystem().ultimateSelected();

const scene::INodePtr& sourceNode =
GlobalSelectionSystem().penultimateSelected();
auto targetNode = GlobalSelectionSystem().ultimateSelected();
auto sourceNode = GlobalSelectionSystem().penultimateSelected();

// Cast the instances onto a patch
Patch* source = Node_getPatch(sourceNode);
Patch* target = Node_getPatch(targetNode);

if (source != NULL && target != NULL) {
if (source && target)
{
// Stitch the texture leaving the source patch intact
target->stitchTextureFrom(*source);
}
else {
wxutil::Messagebox::ShowError(_("Cannot stitch textures. \nCould not cast nodes to patches."));
else
{
throw cmd::ExecutionFailure(_("Cannot stitch textures. \nCould not cast nodes to patches."));
}

SceneChangeNotify();
Expand All @@ -118,45 +115,44 @@ void stitchTextures(const cmd::ArgumentList& args)
}
else
{
wxutil::Messagebox::ShowError(_("Cannot stitch patch textures. \nExactly 2 patches must be selected."));
throw cmd::ExecutionFailure(_("Cannot stitch patch textures. \nExactly 2 patches must be selected."));
}
}

void bulge(const cmd::ArgumentList& args)
{
if (args.size() != 1)
{
rWarning() << "Usage: BulgePatch <maxNoiseAmplitude>" << std::endl;
return;
}

// Get the list of selected patches
PatchPtrVector patches = selection::algorithm::getSelectedPatches();

if (!patches.empty())
if (patches.empty())
{
int maxValue = 16;
throw cmd::ExecutionFailure(_("Cannot bulge patch. No patches selected."));
}

// Ask the user to enter a noise value
if (ui::BulgePatchDialog::QueryPatchNoise(maxValue))
{
UndoableCommand cmd("BulgePatch");
double maxValue = args[0].getDouble();

// Cycle through all patches and apply the bulge algorithm
for (PatchPtrVector::iterator p = patches.begin(); p != patches.end(); ++p)
{
Patch& patch = (*p)->getPatchInternal();
UndoableCommand cmd("BulgePatch");

patch.undoSave();
// Cycle through all patches and apply the bulge algorithm
for (const PatchNodePtr& p : patches)
{
Patch& patch = p->getPatchInternal();

for (PatchControlIter i = patch.begin(); i != patch.end(); ++i)
{
PatchControl& control = *i;
int randomNumber = int(maxValue * (float(std::rand()) / float(RAND_MAX)));
control.vertex.set(control.vertex.x(), control.vertex.y(), control.vertex.z() + randomNumber);
}
patch.undoSave();

patch.controlPointsChanged();
}
for (PatchControl& control : patch)
{
int randomNumber = int(maxValue * (float(std::rand()) / float(RAND_MAX)));
control.vertex.set(control.vertex.x(), control.vertex.y(), control.vertex.z() + randomNumber);
}
}
else
{
throw cmd::ExecutionFailure(_("Cannot bulge patch. No patches selected."));

patch.controlPointsChanged();
}
}

Expand Down
3 changes: 3 additions & 0 deletions radiant/ui/UserInterfaceModule.cpp
Expand Up @@ -42,6 +42,7 @@
#include "ui/filters/FilterOrthoContextMenuItem.h"
#include "uimanager/colourscheme/ColourSchemeEditor.h"
#include "ui/layers/CreateLayerDialog.h"
#include "ui/patch/BulgePatchDialog.h"

namespace ui
{
Expand Down Expand Up @@ -264,6 +265,8 @@ void UserInterfaceModule::registerUICommands()
// Register the "create layer" command
GlobalCommandSystem().addCommand("CreateNewLayer", CreateLayerDialog::CreateNewLayer,
{ cmd::ARGTYPE_STRING | cmd::ARGTYPE_OPTIONAL });

GlobalCommandSystem().addCommand("BulgePatchDialog", BulgePatchDialog::BulgePatchCmd);
}

// Static module registration
Expand Down
23 changes: 21 additions & 2 deletions radiant/ui/patch/BulgePatchDialog.cpp
Expand Up @@ -3,13 +3,15 @@
#include "i18n.h"
#include "imainframe.h"
#include "string/convert.h"
#include "selectionlib.h"
#include "wxutil/dialog/MessageBox.h"

namespace
{
const char* WINDOW_TITLE = N_("Bulge Patch");
const char* LABEL_NOISE = N_("Noise:");

const int NOISE = 16;
const float NOISE = 16;
}

namespace ui {
Expand All @@ -22,7 +24,7 @@ BulgePatchDialog::BulgePatchDialog() :
setElementValue(_noiseHandle, string::to_string(NOISE));
}

bool BulgePatchDialog::QueryPatchNoise(int& noise)
bool BulgePatchDialog::QueryPatchNoise(float& noise)
{
// Instantiate a dialog and run the dialog routine
BulgePatchDialog* dialog = new BulgePatchDialog;
Expand All @@ -40,4 +42,21 @@ bool BulgePatchDialog::QueryPatchNoise(int& noise)
return false; // cancelled
}

void BulgePatchDialog::BulgePatchCmd(const cmd::ArgumentList& args)
{
if (GlobalSelectionSystem().getSelectionInfo().patchCount == 0)
{
wxutil::Messagebox::ShowError(_("Cannot bulge patch. No patches selected."));
return;
}

float maxValue = NOISE;

// Ask the user to enter a noise value and dispatch to the algorithm
if (QueryPatchNoise(maxValue))
{
GlobalCommandSystem().executeCommand("BulgePatch", cmd::Argument(maxValue));
}
}

} // namespace
7 changes: 5 additions & 2 deletions radiant/ui/patch/BulgePatchDialog.h
Expand Up @@ -20,8 +20,11 @@ class BulgePatchDialog :
BulgePatchDialog();

// Shows the dialog, returns TRUE if the user selected OK.
// the given integer reference is then filled with the chosen value
static bool QueryPatchNoise(int& noise);
// the given float reference is then filled with the chosen value
static bool QueryPatchNoise(float& noise);

// Command target for BulgePatchDialog
static void BulgePatchCmd(const cmd::ArgumentList& args);
};

} // namespace ui

0 comments on commit 0158137

Please sign in to comment.