From f9b445119f66e2fc7588477c932185279920d250 Mon Sep 17 00:00:00 2001 From: Olathuss Date: Thu, 6 Mar 2025 12:29:40 -0700 Subject: [PATCH 1/5] gameTSCtrl mouse refactor GameTSCtrl mouse refactoring to make consistent with scripting call backs. --- Engine/source/T3D/gameTSCtrl.cpp | 280 +++++++++++++++++++++++++++++-- Engine/source/T3D/gameTSCtrl.h | 26 +++ 2 files changed, 292 insertions(+), 14 deletions(-) diff --git a/Engine/source/T3D/gameTSCtrl.cpp b/Engine/source/T3D/gameTSCtrl.cpp index f4f5543db..d1c7dd2b5 100644 --- a/Engine/source/T3D/gameTSCtrl.cpp +++ b/Engine/source/T3D/gameTSCtrl.cpp @@ -45,6 +45,175 @@ ConsoleDocClass( GameTSCtrl, "@brief The main 3D viewport for a Torque 3D game.\n\n" "@ingroup Gui3D\n"); +IMPLEMENT_CALLBACK(GameTSCtrl, onMouseDown, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), + "@brief Callback that occurs whenever the left mouse button is pressed while in this control.\n\n" + "@param screenPosition Position of screen when mouse was pressed during this callback.\n\n" + "@param worldPosition Position of world when mouse was pressed during this callback.\n\n" + "@param clickVector Vector for the click when mouse was pressed during this callback.\n\n" + "@tsexample\n" + "// Mouse was pressed down in this control, causing the callback\n" + "GameTSCtrl::onMouseDown(%this,%screenPosition,%worldPosition,%clickVector)\n" + "{\n" + " // Code to call when a mouse event occurs.\n" + "}\n" + "@endtsexample\n\n" +); + +IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseDown, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), + "@brief Callback that occurs whenever the right mouse button is pressed while in this control.\n\n" + "@param screenPosition Position of screen when mouse was pressed during this callback.\n\n" + "@param worldPosition Position of world when mouse was pressed during this callback.\n\n" + "@param clickVector Vector for the click when mouse was pressed during this callback.\n\n" + "@tsexample\n" + "// Mouse was pressed down in this control, causing the callback\n" + "GameTSCtrl::onRightMouseDown(%this,%screenPosition,%worldPosition,%clickVector)\n" + "{\n" + " // Code to call when a mouse event occurs.\n" + "}\n" + "@endtsexample\n\n" +); + +IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseDown, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), + "@brief Callback that occurs whenever the middle mouse button is pressed while in this control.\n\n" + "@param screenPosition Position of screen when mouse was pressed during this callback.\n\n" + "@param worldPosition Position of world when mouse was pressed during this callback.\n\n" + "@param clickVector Vector for the click when mouse was pressed during this callback.\n\n" + "@tsexample\n" + "// Mouse was pressed down in this control, causing the callback\n" + "GameTSCtrl::onMiddleMouseDown(%this,%screenPosition,%worldPosition,%clickVector)\n" + "{\n" + " // Code to call when a mouse event occurs.\n" + "}\n" + "@endtsexample\n\n" +); + +IMPLEMENT_CALLBACK(GameTSCtrl, onMouseUp, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), + "@brief Callback that occurs whenever the left mouse button is released while in this control.\n\n" + "@param screenPosition Position of screen when mouse was released during this callback.\n\n" + "@param worldPosition Position of world when mouse was released during this callback.\n\n" + "@param clickVector Vector for the click when mouse was released during this callback.\n\n" + "@tsexample\n" + "// Mouse was released down in this control, causing the callback\n" + "GameTSCtrl::onMouseUp(%this,%screenPosition,%worldPosition,%clickVector)\n" + "{\n" + " // Code to call when a mouse event occurs.\n" + "}\n" + "@endtsexample\n\n" +); + +IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseUp, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), + "@brief Callback that occurs whenever the right mouse button is released while in this control.\n\n" + "@param screenPosition Position of screen when mouse was released during this callback.\n\n" + "@param worldPosition Position of world when mouse was released during this callback.\n\n" + "@param clickVector Vector for the click when mouse was released during this callback.\n\n" + "@tsexample\n" + "// Mouse was released down in this control, causing the callback\n" + "GameTSCtrl::onRightMouseUp(%this,%screenPosition,%worldPosition,%clickVector)\n" + "{\n" + " // Code to call when a mouse event occurs.\n" + "}\n" + "@endtsexample\n\n" +); + +IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseUp, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), + "@brief Callback that occurs whenever the middle mouse button is released while in this control.\n\n" + "@param screenPosition Position of screen when mouse was released during this callback.\n\n" + "@param worldPosition Position of world when mouse was released during this callback.\n\n" + "@param clickVector Vector for the click when mouse was released during this callback.\n\n" + "@tsexample\n" + "// Mouse was released down in this control, causing the callback\n" + "GameTSCtrl::onMiddleMouseUp(%this,%screenPosition,%worldPosition,%clickVector)\n" + "{\n" + " // Code to call when a mouse event occurs.\n" + "}\n" + "@endtsexample\n\n" +); + +IMPLEMENT_CALLBACK(GameTSCtrl, onMouseDragged, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), + "@brief Callback that occurs whenever the left mouse button is dragged while in this control.\n\n" + "@param screenPosition Position of screen when mouse was dragged during this callback.\n\n" + "@param worldPosition Position of world when mouse was dragged during this callback.\n\n" + "@param clickVector Vector for the click when mouse was dragged during this callback.\n\n" + "@tsexample\n" + "// Mouse was dragged down in this control, causing the callback\n" + "GameTSCtrl::onMouseDragged(%this,%screenPosition,%worldPosition,%clickVector)\n" + "{\n" + " // Code to call when a mouse event occurs.\n" + "}\n" + "@endtsexample\n\n" +); + +IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseDragged, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), + "@brief Callback that occurs whenever the right mouse button is dragged while in this control.\n\n" + "@param screenPosition Position of screen when mouse was dragged during this callback.\n\n" + "@param worldPosition Position of world when mouse was dragged during this callback.\n\n" + "@param clickVector Vector for the click when mouse was dragged during this callback.\n\n" + "@tsexample\n" + "// Mouse was dragged down in this control, causing the callback\n" + "GameTSCtrl::onRightMouseDragged(%this,%screenPosition,%worldPosition,%clickVector)\n" + "{\n" + " // Code to call when a mouse event occurs.\n" + "}\n" + "@endtsexample\n\n" +); + +IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseDragged, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), + "@brief Callback that occurs whenever the middle mouse button is dragged while in this control.\n\n" + "@param screenPosition Position of screen when mouse was dragged during this callback.\n\n" + "@param worldPosition Position of world when mouse was dragged during this callback.\n\n" + "@param clickVector Vector for the click when mouse was dragged during this callback.\n\n" + "@tsexample\n" + "// Mouse was dragged down in this control, causing the callback\n" + "GameTSCtrl::onMiddleMouseDragged(%this,%screenPosition,%worldPosition,%clickVector)\n" + "{\n" + " // Code to call when a mouse event occurs.\n" + "}\n" + "@endtsexample\n\n" +); + +IMPLEMENT_CALLBACK(GameTSCtrl, onMouseWheelUp, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), + "@brief Callback that occurs whenever the mouse wheel up is pressed while in this control.\n\n" + "@param screenPosition Position of screen when mouse wheel up was pressed during this callback.\n\n" + "@param worldPosition Position of world when mouse wheel up was pressed during this callback.\n\n" + "@param clickVector Vector for the click when mouse wheel up was pressed during this callback.\n\n" + "@tsexample\n" + "// Mouse wheel up was pressed in this control, causing the callback\n" + "GameTSCtrl::onMouseWheelUp(%this,%screenPosition,%worldPosition,%clickVector)\n" + "{\n" + " // Code to call when a mouse event occurs.\n" + "}\n" + "@endtsexample\n\n" +); + +IMPLEMENT_CALLBACK(GameTSCtrl, onMouseWheelDown, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), + "@brief Callback that occurs whenever the mouse wheel down is pressed while in this control.\n\n" + "@param screenPosition Position of screen when mouse wheel down was pressed during this callback.\n\n" + "@param worldPosition Position of world when mouse wheel down was pressed during this callback.\n\n" + "@param clickVector Vector for the click when mouse wheel down was pressed during this callback.\n\n" + "@tsexample\n" + "// Mouse wheel down was pressed in this control, causing the callback\n" + "GameTSCtrl::onMouseWheelDown(%this,%screenPosition,%worldPosition,%clickVector)\n" + "{\n" + " // Code to call when a mouse event occurs.\n" + "}\n" + "@endtsexample\n\n" +); + +IMPLEMENT_CALLBACK(GameTSCtrl, onMouseMove, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), + "@brief Callback that occurs whenever the mouse is moved (without dragging) while in this control.\n\n" + "@param screenPosition Position of screen when mouse was moved during this callback.\n\n" + "@param worldPosition Position of world when mouse was moved during this callback.\n\n" + "@param clickVector Vector for the click when mouse was moved during this callback.\n\n" + "@tsexample\n" + "// Mouse was moved down in this control, causing the callback\n" + "GameTSCtrl::onMouseMove(%this,%screenPosition,%worldPosition,%clickVector)\n" + "{\n" + " // Code to call when a mouse event occurs.\n" + "}\n" + "@endtsexample\n\n" + "@see GuiTSCtrl\n\n" +); + GameTSCtrl::GameTSCtrl() { } @@ -96,46 +265,129 @@ void GameTSCtrl::makeScriptCall(const char *func, const GuiEvent &evt) const Con::executef( (SimObject*)this, func, sp, wp, vec ); } +//------------------------------------------------------------------------------ +void GameTSCtrl::sendMouseEvent(const char *name, const GuiEvent &event) +{ + // write screen position + char* sp = Con::getArgBuffer(32); + dSprintf(sp, 32, "%d %d", event.mousePoint.x, event.mousePoint.y); + + // write world position + char* wp = Con::getArgBuffer(32); + Point3F camPos; + mLastCameraQuery.cameraMatrix.getColumn(3, &camPos); + dSprintf(wp, 32, "%g %g %g", camPos.x, camPos.y, camPos.z); + + // write click vector + char* vec = Con::getArgBuffer(32); + Point3F fp(event.mousePoint.x, event.mousePoint.y, 1.0); + Point3F ray; + unproject(fp, &ray); + ray -= camPos; + ray.normalizeSafe(); + dSprintf(vec, 32, "%g %g %g", ray.x, ray.y, ray.z); + + if (dStricmp(name, "onMouseDown") == 0) + onMouseDown_callback(sp, wp, vec); + else if (dStricmp(name, "onRightMouseDown") == 0) + onRightMouseDown_callback(sp, wp, vec); + else if (dStricmp(name, "onMiddleMouseDown") == 0) + onMiddleMouseDown_callback(sp, wp, vec); + else if (dStricmp(name, "onMouseUp") == 0) + onMouseUp_callback(sp, wp, vec); + else if (dStricmp(name, "onRightMouseUp") == 0) + onRightMouseUp_callback(sp, wp, vec); + else if (dStricmp(name, "onMiddleMouseUp") == 0) + onMiddleMouseUp_callback(sp, wp, vec); + else if (dStricmp(name, "onMouseDragged") == 0) + onMouseDragged_callback(sp, wp, vec); + else if (dStricmp(name, "onRightMouseDragged") == 0) + onRightMouseDragged_callback(sp, wp, vec); + else if (dStricmp(name, "onMiddleMouseDragged") == 0) + onMiddleMouseDragged_callback(sp, wp, vec); + else if (dStricmp(name, "onMouseWheelUp") == 0) + onMouseWheelUp_callback(sp, wp, vec); + else if (dStricmp(name, "onMouseWheelDown") == 0) + onMouseWheelDown_callback(sp, wp, vec); + else if (dStricmp(name, "onMouseMove") == 0) + onMouseMove_callback(sp, wp, vec); +} + void GameTSCtrl::onMouseDown(const GuiEvent &evt) { Parent::onMouseDown(evt); - if( isMethod( "onMouseDown" ) ) - makeScriptCall( "onMouseDown", evt ); + + sendMouseEvent("onMouseDown", evt); } void GameTSCtrl::onRightMouseDown(const GuiEvent &evt) { Parent::onRightMouseDown(evt); - if( isMethod( "onRightMouseDown" ) ) - makeScriptCall( "onRightMouseDown", evt ); + + sendMouseEvent("onRightMouseDown", evt); } void GameTSCtrl::onMiddleMouseDown(const GuiEvent &evt) { Parent::onMiddleMouseDown(evt); - if( isMethod( "onMiddleMouseDown" ) ) - makeScriptCall( "onMiddleMouseDown", evt ); + + sendMouseEvent("onMiddleMouseDown", evt); } void GameTSCtrl::onMouseUp(const GuiEvent &evt) { Parent::onMouseUp(evt); - if( isMethod( "onMouseUp" ) ) - makeScriptCall( "onMouseUp", evt ); + + sendMouseEvent("onMouseUp", evt); } void GameTSCtrl::onRightMouseUp(const GuiEvent &evt) { Parent::onRightMouseUp(evt); - if( isMethod( "onRightMouseUp" ) ) - makeScriptCall( "onRightMouseUp", evt ); + + sendMouseEvent("onRightMouseUp", evt); } void GameTSCtrl::onMiddleMouseUp(const GuiEvent &evt) { Parent::onMiddleMouseUp(evt); - if( isMethod( "onMiddleMouseUp" ) ) - makeScriptCall( "onMiddleMouseUp", evt ); + + sendMouseEvent("onMiddleMouseUp", evt); +} + +void GameTSCtrl::onMouseDragged(const GuiEvent &evt) +{ + Parent::onMouseDragged(evt); + + sendMouseEvent("onMouseDragged", evt); +} + +void GameTSCtrl::onRightMouseDragged(const GuiEvent &evt) +{ + Parent::onRightMouseDragged(evt); + + sendMouseEvent("onRightMouseDragged", evt); +} + +void GameTSCtrl::onMiddleMouseDragged(const GuiEvent &evt) +{ + Parent::onMiddleMouseDragged(evt); + + sendMouseEvent("onMiddleMouseDragged", evt); +} + +bool GameTSCtrl::onMouseWheelUp(const GuiEvent &evt) +{ + sendMouseEvent("onMouseWheelUp", evt); + + return Parent::onMouseWheelUp(evt); +} + +bool GameTSCtrl::onMouseWheelDown(const GuiEvent &evt) +{ + sendMouseEvent("onMouseWheelDown", evt); + + return Parent::onMouseWheelDown(evt); } void GameTSCtrl::onMouseMove(const GuiEvent &evt) @@ -158,8 +410,8 @@ void GameTSCtrl::onMouseMove(const GuiEvent &evt) lineTestEnd = pos + vec * 1000; } } - if (isMethod("onMouseMove")) - makeScriptCall("onMouseMove", evt); + + sendMouseEvent("onMouseMove", evt); } void GameTSCtrl::onRender(Point2I offset, const RectI &updateRect) diff --git a/Engine/source/T3D/gameTSCtrl.h b/Engine/source/T3D/gameTSCtrl.h index f3c3c7d08..39a00c8c7 100644 --- a/Engine/source/T3D/gameTSCtrl.h +++ b/Engine/source/T3D/gameTSCtrl.h @@ -41,9 +41,28 @@ private: void makeScriptCall(const char *func, const GuiEvent &evt) const; + void sendMouseEvent(const char *name, const GuiEvent &evt); + public: GameTSCtrl(); + /// @name Callbacks + /// @{ + + DECLARE_CALLBACK(void, onMouseDown, (const char* screenPosition, const char* worldPosition, const char* clickVector)); + DECLARE_CALLBACK(void, onRightMouseDown, (const char* screenPosition, const char* worldPosition, const char* clickVector)); + DECLARE_CALLBACK(void, onMiddleMouseDown, (const char* screenPosition, const char* worldPosition, const char* clickVector)); + DECLARE_CALLBACK(void, onMouseUp, (const char* screenPosition, const char* worldPosition, const char* clickVector)); + DECLARE_CALLBACK(void, onRightMouseUp, (const char* screenPosition, const char* worldPosition, const char* clickVector)); + DECLARE_CALLBACK(void, onMiddleMouseUp, (const char* screenPosition, const char* worldPosition, const char* clickVector)); + DECLARE_CALLBACK(void, onMouseDragged, (const char* screenPosition, const char* worldPosition, const char* clickVector)); + DECLARE_CALLBACK(void, onRightMouseDragged, (const char* screenPosition, const char* worldPosition, const char* clickVector)); + DECLARE_CALLBACK(void, onMiddleMouseDragged, (const char* screenPosition, const char* worldPosition, const char* clickVector)); + DECLARE_CALLBACK(void, onMouseWheelUp, (const char* screenPosition, const char* worldPosition, const char* clickVector)); + DECLARE_CALLBACK(void, onMouseWheelDown, (const char* screenPosition, const char* worldPosition, const char* clickVector)); + DECLARE_CALLBACK(void, onMouseMove, (const char* screenPosition, const char* worldPosition, const char* clickVector)); + /// } + DECLARE_CONOBJECT(GameTSCtrl); DECLARE_DESCRIPTION( "A control that renders a 3D view from the current control object." ); @@ -59,6 +78,13 @@ public: void onRightMouseUp(const GuiEvent &evt) override; void onMiddleMouseUp(const GuiEvent &evt) override; + void onMouseDragged(const GuiEvent &evt); + void onRightMouseDragged(const GuiEvent &evt); + void onMiddleMouseDragged(const GuiEvent &evt); + + bool onMouseWheelUp(const GuiEvent &evt); + bool onMouseWheelDown(const GuiEvent &evt); + void onMouseMove(const GuiEvent &evt) override; void onRender(Point2I offset, const RectI &updateRect) override; From 96786020a2a1f5164cd0ed5e87d3528dc7a5b9af Mon Sep 17 00:00:00 2001 From: Olathuss Date: Tue, 11 Mar 2025 09:40:22 -0600 Subject: [PATCH 2/5] Remove document reference Removed incorrect document reference, as parent GuiTSCtrl does not implement mouse callback. --- Engine/source/T3D/gameTSCtrl.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Engine/source/T3D/gameTSCtrl.cpp b/Engine/source/T3D/gameTSCtrl.cpp index d1c7dd2b5..8f103db85 100644 --- a/Engine/source/T3D/gameTSCtrl.cpp +++ b/Engine/source/T3D/gameTSCtrl.cpp @@ -211,7 +211,6 @@ IMPLEMENT_CALLBACK(GameTSCtrl, onMouseMove, void, (const char* screenPosition, c " // Code to call when a mouse event occurs.\n" "}\n" "@endtsexample\n\n" - "@see GuiTSCtrl\n\n" ); GameTSCtrl::GameTSCtrl() From 32bc068f7f57f141c19a2d8f554028d5ea48500f Mon Sep 17 00:00:00 2001 From: Olathuss Date: Mon, 24 Mar 2025 12:04:36 -0600 Subject: [PATCH 3/5] Added PlatformTimer for mouse callbacks Added PlatformTimer to GameTSCtrl to limit mouse move/drag callbacks. --- Engine/source/T3D/gameTSCtrl.cpp | 25 +++++++++++++++++++++---- Engine/source/T3D/gameTSCtrl.h | 3 +++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Engine/source/T3D/gameTSCtrl.cpp b/Engine/source/T3D/gameTSCtrl.cpp index 8f103db85..f2e1f0b7a 100644 --- a/Engine/source/T3D/gameTSCtrl.cpp +++ b/Engine/source/T3D/gameTSCtrl.cpp @@ -215,6 +215,7 @@ IMPLEMENT_CALLBACK(GameTSCtrl, onMouseMove, void, (const char* screenPosition, c GameTSCtrl::GameTSCtrl() { + mFrameTime = PlatformTimer::create(); } //--------------------------------------------------------------------------- @@ -358,21 +359,33 @@ void GameTSCtrl::onMouseDragged(const GuiEvent &evt) { Parent::onMouseDragged(evt); - sendMouseEvent("onMouseDragged", evt); + if (mFrameTime && mFrameTime->getElapsedMs() > 16) + { + sendMouseEvent("onMouseDragged", evt); + mFrameTime->reset(); + } } void GameTSCtrl::onRightMouseDragged(const GuiEvent &evt) { Parent::onRightMouseDragged(evt); - sendMouseEvent("onRightMouseDragged", evt); + if (mFrameTime && mFrameTime->getElapsedMs() > 16) + { + sendMouseEvent("onRightMouseDragged", evt); + mFrameTime->reset(); + } } void GameTSCtrl::onMiddleMouseDragged(const GuiEvent &evt) { Parent::onMiddleMouseDragged(evt); - sendMouseEvent("onMiddleMouseDragged", evt); + if (mFrameTime && mFrameTime->getElapsedMs() > 16) + { + sendMouseEvent("onMiddleMouseDragged", evt); + mFrameTime->reset(); + } } bool GameTSCtrl::onMouseWheelUp(const GuiEvent &evt) @@ -410,7 +423,11 @@ void GameTSCtrl::onMouseMove(const GuiEvent &evt) } } - sendMouseEvent("onMouseMove", evt); + if (mFrameTime->getElapsedMs() > 16) + { + sendMouseEvent("onMouseMove", evt); + mFrameTime->reset(); + } } void GameTSCtrl::onRender(Point2I offset, const RectI &updateRect) diff --git a/Engine/source/T3D/gameTSCtrl.h b/Engine/source/T3D/gameTSCtrl.h index 39a00c8c7..72f2b2633 100644 --- a/Engine/source/T3D/gameTSCtrl.h +++ b/Engine/source/T3D/gameTSCtrl.h @@ -43,6 +43,9 @@ private: void sendMouseEvent(const char *name, const GuiEvent &evt); +protected: + PlatformTimer *mFrameTime; + public: GameTSCtrl(); From df5ffd9e10fdc401d4d06d7330ecd925bd5b19f9 Mon Sep 17 00:00:00 2001 From: Olathuss Date: Tue, 25 Mar 2025 10:47:35 -0600 Subject: [PATCH 4/5] Remove extraneous function Remove extraneous function makeScriptCall which is no longer used. --- Engine/source/T3D/gameTSCtrl.cpp | 25 ------------------------- Engine/source/T3D/gameTSCtrl.h | 2 -- 2 files changed, 27 deletions(-) diff --git a/Engine/source/T3D/gameTSCtrl.cpp b/Engine/source/T3D/gameTSCtrl.cpp index f2e1f0b7a..22ae4a288 100644 --- a/Engine/source/T3D/gameTSCtrl.cpp +++ b/Engine/source/T3D/gameTSCtrl.cpp @@ -240,31 +240,6 @@ void GameTSCtrl::renderWorld(const RectI &updateRect) GameRenderWorld(); } -//--------------------------------------------------------------------------- -void GameTSCtrl::makeScriptCall(const char *func, const GuiEvent &evt) const -{ - // write screen position - char *sp = Con::getArgBuffer(32); - dSprintf(sp, 32, "%d %d", evt.mousePoint.x, evt.mousePoint.y); - - // write world position - char *wp = Con::getArgBuffer(32); - Point3F camPos; - mLastCameraQuery.cameraMatrix.getColumn(3, &camPos); - dSprintf(wp, 32, "%g %g %g", camPos.x, camPos.y, camPos.z); - - // write click vector - char *vec = Con::getArgBuffer(32); - Point3F fp(evt.mousePoint.x, evt.mousePoint.y, 1.0); - Point3F ray; - unproject(fp, &ray); - ray -= camPos; - ray.normalizeSafe(); - dSprintf(vec, 32, "%g %g %g", ray.x, ray.y, ray.z); - - Con::executef( (SimObject*)this, func, sp, wp, vec ); -} - //------------------------------------------------------------------------------ void GameTSCtrl::sendMouseEvent(const char *name, const GuiEvent &event) { diff --git a/Engine/source/T3D/gameTSCtrl.h b/Engine/source/T3D/gameTSCtrl.h index 72f2b2633..765a09430 100644 --- a/Engine/source/T3D/gameTSCtrl.h +++ b/Engine/source/T3D/gameTSCtrl.h @@ -39,8 +39,6 @@ class GameTSCtrl : public GuiTSCtrl private: typedef GuiTSCtrl Parent; - void makeScriptCall(const char *func, const GuiEvent &evt) const; - void sendMouseEvent(const char *name, const GuiEvent &evt); protected: From 3dabbc83ff7c1b198d0d31dfc08380622519e766 Mon Sep 17 00:00:00 2001 From: Olathuss Date: Tue, 25 Mar 2025 11:06:35 -0600 Subject: [PATCH 5/5] Fixed callbacks to replace char* with Point classes Replacing char* in callbacks with Point2I and Point3F classes for better readability and overhead --- Engine/source/T3D/gameTSCtrl.cpp | 67 +++++++++++++++----------------- Engine/source/T3D/gameTSCtrl.h | 24 ++++++------ 2 files changed, 43 insertions(+), 48 deletions(-) diff --git a/Engine/source/T3D/gameTSCtrl.cpp b/Engine/source/T3D/gameTSCtrl.cpp index 22ae4a288..65fa2a72b 100644 --- a/Engine/source/T3D/gameTSCtrl.cpp +++ b/Engine/source/T3D/gameTSCtrl.cpp @@ -45,7 +45,7 @@ ConsoleDocClass( GameTSCtrl, "@brief The main 3D viewport for a Torque 3D game.\n\n" "@ingroup Gui3D\n"); -IMPLEMENT_CALLBACK(GameTSCtrl, onMouseDown, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), +IMPLEMENT_CALLBACK(GameTSCtrl, onMouseDown, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector), "@brief Callback that occurs whenever the left mouse button is pressed while in this control.\n\n" "@param screenPosition Position of screen when mouse was pressed during this callback.\n\n" "@param worldPosition Position of world when mouse was pressed during this callback.\n\n" @@ -59,7 +59,7 @@ IMPLEMENT_CALLBACK(GameTSCtrl, onMouseDown, void, (const char* screenPosition, c "@endtsexample\n\n" ); -IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseDown, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), +IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseDown, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector), "@brief Callback that occurs whenever the right mouse button is pressed while in this control.\n\n" "@param screenPosition Position of screen when mouse was pressed during this callback.\n\n" "@param worldPosition Position of world when mouse was pressed during this callback.\n\n" @@ -73,7 +73,7 @@ IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseDown, void, (const char* screenPositi "@endtsexample\n\n" ); -IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseDown, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), +IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseDown, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector), "@brief Callback that occurs whenever the middle mouse button is pressed while in this control.\n\n" "@param screenPosition Position of screen when mouse was pressed during this callback.\n\n" "@param worldPosition Position of world when mouse was pressed during this callback.\n\n" @@ -87,7 +87,7 @@ IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseDown, void, (const char* screenPosit "@endtsexample\n\n" ); -IMPLEMENT_CALLBACK(GameTSCtrl, onMouseUp, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), +IMPLEMENT_CALLBACK(GameTSCtrl, onMouseUp, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector), "@brief Callback that occurs whenever the left mouse button is released while in this control.\n\n" "@param screenPosition Position of screen when mouse was released during this callback.\n\n" "@param worldPosition Position of world when mouse was released during this callback.\n\n" @@ -101,7 +101,7 @@ IMPLEMENT_CALLBACK(GameTSCtrl, onMouseUp, void, (const char* screenPosition, con "@endtsexample\n\n" ); -IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseUp, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), +IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseUp, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector), "@brief Callback that occurs whenever the right mouse button is released while in this control.\n\n" "@param screenPosition Position of screen when mouse was released during this callback.\n\n" "@param worldPosition Position of world when mouse was released during this callback.\n\n" @@ -115,7 +115,7 @@ IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseUp, void, (const char* screenPosition "@endtsexample\n\n" ); -IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseUp, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), +IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseUp, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector), "@brief Callback that occurs whenever the middle mouse button is released while in this control.\n\n" "@param screenPosition Position of screen when mouse was released during this callback.\n\n" "@param worldPosition Position of world when mouse was released during this callback.\n\n" @@ -129,7 +129,7 @@ IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseUp, void, (const char* screenPositio "@endtsexample\n\n" ); -IMPLEMENT_CALLBACK(GameTSCtrl, onMouseDragged, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), +IMPLEMENT_CALLBACK(GameTSCtrl, onMouseDragged, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector), "@brief Callback that occurs whenever the left mouse button is dragged while in this control.\n\n" "@param screenPosition Position of screen when mouse was dragged during this callback.\n\n" "@param worldPosition Position of world when mouse was dragged during this callback.\n\n" @@ -143,7 +143,7 @@ IMPLEMENT_CALLBACK(GameTSCtrl, onMouseDragged, void, (const char* screenPosition "@endtsexample\n\n" ); -IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseDragged, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), +IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseDragged, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector), "@brief Callback that occurs whenever the right mouse button is dragged while in this control.\n\n" "@param screenPosition Position of screen when mouse was dragged during this callback.\n\n" "@param worldPosition Position of world when mouse was dragged during this callback.\n\n" @@ -157,7 +157,7 @@ IMPLEMENT_CALLBACK(GameTSCtrl, onRightMouseDragged, void, (const char* screenPos "@endtsexample\n\n" ); -IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseDragged, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), +IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseDragged, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector), "@brief Callback that occurs whenever the middle mouse button is dragged while in this control.\n\n" "@param screenPosition Position of screen when mouse was dragged during this callback.\n\n" "@param worldPosition Position of world when mouse was dragged during this callback.\n\n" @@ -171,7 +171,7 @@ IMPLEMENT_CALLBACK(GameTSCtrl, onMiddleMouseDragged, void, (const char* screenPo "@endtsexample\n\n" ); -IMPLEMENT_CALLBACK(GameTSCtrl, onMouseWheelUp, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), +IMPLEMENT_CALLBACK(GameTSCtrl, onMouseWheelUp, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector), "@brief Callback that occurs whenever the mouse wheel up is pressed while in this control.\n\n" "@param screenPosition Position of screen when mouse wheel up was pressed during this callback.\n\n" "@param worldPosition Position of world when mouse wheel up was pressed during this callback.\n\n" @@ -185,7 +185,7 @@ IMPLEMENT_CALLBACK(GameTSCtrl, onMouseWheelUp, void, (const char* screenPosition "@endtsexample\n\n" ); -IMPLEMENT_CALLBACK(GameTSCtrl, onMouseWheelDown, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), +IMPLEMENT_CALLBACK(GameTSCtrl, onMouseWheelDown, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector), "@brief Callback that occurs whenever the mouse wheel down is pressed while in this control.\n\n" "@param screenPosition Position of screen when mouse wheel down was pressed during this callback.\n\n" "@param worldPosition Position of world when mouse wheel down was pressed during this callback.\n\n" @@ -199,7 +199,7 @@ IMPLEMENT_CALLBACK(GameTSCtrl, onMouseWheelDown, void, (const char* screenPositi "@endtsexample\n\n" ); -IMPLEMENT_CALLBACK(GameTSCtrl, onMouseMove, void, (const char* screenPosition, const char* worldPosition, const char* clickVector), (screenPosition, worldPosition, clickVector), +IMPLEMENT_CALLBACK(GameTSCtrl, onMouseMove, void, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector), (screenPosition, worldPosition, clickVector), "@brief Callback that occurs whenever the mouse is moved (without dragging) while in this control.\n\n" "@param screenPosition Position of screen when mouse was moved during this callback.\n\n" "@param worldPosition Position of world when mouse was moved during this callback.\n\n" @@ -244,48 +244,43 @@ void GameTSCtrl::renderWorld(const RectI &updateRect) void GameTSCtrl::sendMouseEvent(const char *name, const GuiEvent &event) { // write screen position - char* sp = Con::getArgBuffer(32); - dSprintf(sp, 32, "%d %d", event.mousePoint.x, event.mousePoint.y); + Point2I screenPosition = event.mousePoint; // write world position - char* wp = Con::getArgBuffer(32); - Point3F camPos; - mLastCameraQuery.cameraMatrix.getColumn(3, &camPos); - dSprintf(wp, 32, "%g %g %g", camPos.x, camPos.y, camPos.z); + Point3F worldPosition; + mLastCameraQuery.cameraMatrix.getColumn(3, &worldPosition); // write click vector - char* vec = Con::getArgBuffer(32); Point3F fp(event.mousePoint.x, event.mousePoint.y, 1.0); - Point3F ray; - unproject(fp, &ray); - ray -= camPos; - ray.normalizeSafe(); - dSprintf(vec, 32, "%g %g %g", ray.x, ray.y, ray.z); + Point3F clickVector; + unproject(fp, &clickVector); + clickVector -= worldPosition; + clickVector.normalizeSafe(); if (dStricmp(name, "onMouseDown") == 0) - onMouseDown_callback(sp, wp, vec); + onMouseDown_callback(screenPosition, worldPosition, clickVector); else if (dStricmp(name, "onRightMouseDown") == 0) - onRightMouseDown_callback(sp, wp, vec); + onRightMouseDown_callback(screenPosition, worldPosition, clickVector); else if (dStricmp(name, "onMiddleMouseDown") == 0) - onMiddleMouseDown_callback(sp, wp, vec); + onMiddleMouseDown_callback(screenPosition, worldPosition, clickVector); else if (dStricmp(name, "onMouseUp") == 0) - onMouseUp_callback(sp, wp, vec); + onMouseUp_callback(screenPosition, worldPosition, clickVector); else if (dStricmp(name, "onRightMouseUp") == 0) - onRightMouseUp_callback(sp, wp, vec); + onRightMouseUp_callback(screenPosition, worldPosition, clickVector); else if (dStricmp(name, "onMiddleMouseUp") == 0) - onMiddleMouseUp_callback(sp, wp, vec); + onMiddleMouseUp_callback(screenPosition, worldPosition, clickVector); else if (dStricmp(name, "onMouseDragged") == 0) - onMouseDragged_callback(sp, wp, vec); + onMouseDragged_callback(screenPosition, worldPosition, clickVector); else if (dStricmp(name, "onRightMouseDragged") == 0) - onRightMouseDragged_callback(sp, wp, vec); + onRightMouseDragged_callback(screenPosition, worldPosition, clickVector); else if (dStricmp(name, "onMiddleMouseDragged") == 0) - onMiddleMouseDragged_callback(sp, wp, vec); + onMiddleMouseDragged_callback(screenPosition, worldPosition, clickVector); else if (dStricmp(name, "onMouseWheelUp") == 0) - onMouseWheelUp_callback(sp, wp, vec); + onMouseWheelUp_callback(screenPosition, worldPosition, clickVector); else if (dStricmp(name, "onMouseWheelDown") == 0) - onMouseWheelDown_callback(sp, wp, vec); + onMouseWheelDown_callback(screenPosition, worldPosition, clickVector); else if (dStricmp(name, "onMouseMove") == 0) - onMouseMove_callback(sp, wp, vec); + onMouseMove_callback(screenPosition, worldPosition, clickVector); } void GameTSCtrl::onMouseDown(const GuiEvent &evt) diff --git a/Engine/source/T3D/gameTSCtrl.h b/Engine/source/T3D/gameTSCtrl.h index 765a09430..c3be615f8 100644 --- a/Engine/source/T3D/gameTSCtrl.h +++ b/Engine/source/T3D/gameTSCtrl.h @@ -50,18 +50,18 @@ public: /// @name Callbacks /// @{ - DECLARE_CALLBACK(void, onMouseDown, (const char* screenPosition, const char* worldPosition, const char* clickVector)); - DECLARE_CALLBACK(void, onRightMouseDown, (const char* screenPosition, const char* worldPosition, const char* clickVector)); - DECLARE_CALLBACK(void, onMiddleMouseDown, (const char* screenPosition, const char* worldPosition, const char* clickVector)); - DECLARE_CALLBACK(void, onMouseUp, (const char* screenPosition, const char* worldPosition, const char* clickVector)); - DECLARE_CALLBACK(void, onRightMouseUp, (const char* screenPosition, const char* worldPosition, const char* clickVector)); - DECLARE_CALLBACK(void, onMiddleMouseUp, (const char* screenPosition, const char* worldPosition, const char* clickVector)); - DECLARE_CALLBACK(void, onMouseDragged, (const char* screenPosition, const char* worldPosition, const char* clickVector)); - DECLARE_CALLBACK(void, onRightMouseDragged, (const char* screenPosition, const char* worldPosition, const char* clickVector)); - DECLARE_CALLBACK(void, onMiddleMouseDragged, (const char* screenPosition, const char* worldPosition, const char* clickVector)); - DECLARE_CALLBACK(void, onMouseWheelUp, (const char* screenPosition, const char* worldPosition, const char* clickVector)); - DECLARE_CALLBACK(void, onMouseWheelDown, (const char* screenPosition, const char* worldPosition, const char* clickVector)); - DECLARE_CALLBACK(void, onMouseMove, (const char* screenPosition, const char* worldPosition, const char* clickVector)); + DECLARE_CALLBACK(void, onMouseDown, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector)); + DECLARE_CALLBACK(void, onRightMouseDown, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector)); + DECLARE_CALLBACK(void, onMiddleMouseDown, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector)); + DECLARE_CALLBACK(void, onMouseUp, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector)); + DECLARE_CALLBACK(void, onRightMouseUp, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector)); + DECLARE_CALLBACK(void, onMiddleMouseUp, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector)); + DECLARE_CALLBACK(void, onMouseDragged, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector)); + DECLARE_CALLBACK(void, onRightMouseDragged, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector)); + DECLARE_CALLBACK(void, onMiddleMouseDragged, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector)); + DECLARE_CALLBACK(void, onMouseWheelUp, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector)); + DECLARE_CALLBACK(void, onMouseWheelDown, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector)); + DECLARE_CALLBACK(void, onMouseMove, (const Point2I& screenPosition, const Point3F& worldPosition, const Point3F& clickVector)); /// } DECLARE_CONOBJECT(GameTSCtrl);