Mutating events in event handlers

I have just one last problem (for now), it’s a simple matter I hope. I have been trying to learn about const and & in C++. I get the idea and I think I have to break or work around some constarints to get this to work; I would like to do so in the least offensive manner. Here’s the function that was fine until the argument got the const qualifier. The intent is to switch axes on the drag, and amplify it to get better feel for some custom switches:

void My2HSlideSwitch::onDragMove(const event::DragMove& e)
	float temp = e.mouseDelta.y;
//	e.mouseDelta.y = 8 * e.mouseDelta.x;

//	e.mouseDelta.y = temp;

Without the commented lines it works, after a fashion - wrong axis and too slow, but I am on the air. I’ve tried a few plausible things after some research and TBH a few implausible things. This trick may not be at all the way to accomplish my goal, nevertheless I would benefit from seeing how this would be expressed correctly as I am attempting it.


My C++ isn’t that strong either, but my guess is you have to create your own instance of event::DragMove if you want to change anything before calling svgslider::onDragMove.

The correct way is to make a copy.

void My2HSlideSwitch::onDragMove(const event::DragMove& e) {
	event::DragMove eClone = e;
	eClone.mouseDelta.x = 0.f;
	eClone.mouseDelta.y = 8.f * e.mouseDelta.x;

Andrew is of course correct. To answer the question “why does this API specify a const reference in the first place?” you may have to wait until you are an intermediate c++ programmer, by which time it will be second nature.

If you want to dig in more, you’ll find that every event (derived classes from event::Base) has a non-const pointer to event::Context that can be mutated despite your event being const. This means that in your event handler, you can pass information up to your Widget’s parents or the event handler itself. For example, e.stopPropagating() tells parents to stop calling further event handlers. e.consume(Widget* w) tells the event handler that a Widget has been clicked, dragged, etc. In other words, the data in each event object is local, and the data in the event context is global.

Shouldn’t svgslider::onDragMove(e) be called with eClone?

Yes, corrected

Thank you! That makes sense, esp. after the correction: the original solution threw me into a deep state of confusion. I wrote a few small programs to isolate and explore this matter and was about to ask the same question re: eClone.

C++ is a bit less straight ahead compared to C around types and definitions/declarations.

^ understatement :wink: