Keep a button command alive until it is pressed again

Keep a button command alive until it is pressed again

julian_spaeth5V8T8
Observer Observer
698 Views
4 Replies
Message 1 of 5

Keep a button command alive until it is pressed again

julian_spaeth5V8T8
Observer
Observer

I would like to create a button that when pressed creates a command that should stay alive until the button is pressed again (or ESC).
A similar behavior exists with the Orbit button.
In my code, however, the command is generated after the button is pressed and immediately destroyed again. It is impossible for me to keep the button in a "pressed" state.

Currently I remember the state of the button internally, but this has the disadvantage that the button is not shown as pressed (active) in the UI and also brings other problems.

 

 

Ptr<Application> app;
Ptr<UserInterface> ui;
Ptr<CommandDefinition> cmdDefinition;
Ptr<CommandControl> cmdControl;

// CommandExecuted event handler.
class MyExecuteEventHander : public adsk::core::CommandEventHandler
{
public:
	void notify(const Ptr<CommandEventArgs>& eventArgs) override
	{
		ui->messageBox("command executed");
	}
};

// CommandDestroyed event handler.
class MyDestroyEventHander : public adsk::core::CommandEventHandler
{
public:
	void notify(const Ptr<CommandEventArgs>& eventArgs) override
	{
		ui->messageBox("command destroyed");
	}
};

// CommandCreated event handler
class MyCommandCreatedEventHandler : public adsk::core::CommandCreatedEventHandler
{
public:
	void notify(const Ptr<CommandCreatedEventArgs>& eventArgs) override
	{
		if (eventArgs)
		{
			auto command = eventArgs->command();
			auto executeEventhHandler = command->execute()->add(&myExecuteEventHandler);
			auto destroyEventHandler = command->destroy()->add(&myDestroyEventHandler);
		}
	}
	MyExecuteEventHander myExecuteEventHandler;
	MyDestroyEventHander myDestroyEventHandler;
} _commandCreatedEventHandler;

extern "C" XI_EXPORT bool run(const char* context)
{
	app = Application::get();
	if (!app)
		return false;

	ui = app->userInterface();
	if (!ui)
		return false;

	Ptr<CommandDefinitions> commandDefinitions = app->userInterface()->commandDefinitions();
	cmdDefinition = commandDefinitions->addButtonDefinition("mybutton", "Button", "");
	cmdDefinition->commandCreated()->add(&_commandCreatedEventHandler);

	cmdControl = app->userInterface()->workspaces()->itemById("FusionSolidEnvironment")->toolbarPanels()->itemById("UtilityPanel")->controls()->addCommand(cmdDefinition);

	return true;
}

 

 

Thanks in advance.

0 Likes
699 Views
4 Replies
Replies (4)
Message 2 of 5

Jorge_Jaramillo
Collaborator
Collaborator

@Jorge_Jaramillo - this post is being edited to remove PII.

 

Hi,

 

I beleive there is not a way to keep an command activated until you want to disable it.

There could be only one active command at the moment.  Once a second is order to be display, the systems closed the one that is active.

The "special" actions as the Orbit button are not exposed in the current API.

 

Maybe you can implement what you want, keeping the state in a add-in variable, but without having the command displayed.  Once you click to open again the command, it could remember the status it was before closing it from the add-in variable.

 

Regards,

Jorge Jaramillo

 

0 Likes
Message 3 of 5

julian-spaeth
Explorer
Explorer

Hi,

 

thank you for your reply.

I do not want to keep the command active. It would be fine for me if the command is deavtivated and later activated again when another event occurs. But with a button, the command is destroyed right after it is created. So I have no possibility to use the status 'deactivated' or 'activated'.

0 Likes
Message 4 of 5

Jorge_Jaramillo
Collaborator
Collaborator

@Jorge_Jaramillo - this post is being edited to remove PII.

 

Hi,

 

You can use a BoolValueCommandInput as the displayed button in the command, and set a global variable in you add-in to set it's initial value. 

When the command is displayed set the BoolValueCommandInput's value from the variable.

Then with the InputChangeEventHandler or the execute EventHandler you update the variable values with the status of the command input.

This way the global variable always contains the status and you can use in other function inside the add-in to make what you need.

 

I hope this could help.

 

Best regards,

Jorge Jaramillo

 

0 Likes
Message 5 of 5

BrianEkins
Mentor
Mentor

I believe all commands behave the way you described. When you click a button, it remains "depressed" while that command is active. Only one command can be active at a time, so running another command will terminate the currently running command. Clicking escape executes the default selection command, which terminates the current command.

 

A "depressed" button uses a different icon to indicate the command is active. You can supply the dark icon in the various sizes. For example, the 16x16 icon is 16x16-dark.png. If you don't supply it, Fusion will automatically create it when needed. It replaces any transparent areas with blue, so depending on your icon, you may not see much difference, if any. Since it's a completely different image, you can make your dark icon anything you want so it's very obvious when your command is active.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes