Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Record hit point during picking and expose it via Invokable interface.
  • Loading branch information
ewencp committed Jun 1, 2011
1 parent d38b285 commit 39d729b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
17 changes: 15 additions & 2 deletions liboh/plugins/js/scripts/std/graphics/graphics.em
Expand Up @@ -90,9 +90,22 @@ function() {
this.invoke('screenshot');
};

/** Request a screenshot be taken and stored on disk. */
/** Pick at the given location for an object. Stores the hit point which can be retrieved with pickedPosition(). */
std.graphics.Graphics.prototype.pick = function(x, y) {
return this.invoke('pick', x, y);
var result = this.invoke('pick', x, y);
if (result.object) {
this._pickedPosition = new util.Vec3(result.position.x, result.position.y, result.position.z);
return result.object;
}
else {
this._pickedPosition = undefined;
return undefined;
}
};

/** */
std.graphics.Graphics.prototype.pickedPosition = function() {
return this._pickedPosition;
};

/** Request the bounding box for the object be enabled or disabled. */
Expand Down
14 changes: 12 additions & 2 deletions libproxyobject/plugins/ogre/OgreSystem.cpp
Expand Up @@ -572,8 +572,18 @@ boost::any OgreSystem::pick(vector<boost::any>& params) {

float x = Invokable::anyAsNumeric(params[1]);
float y = Invokable::anyAsNumeric(params[2]);
SpaceObjectReference result = mMouseHandler->pick(Vector2f(x,y), 1);
return Invokable::asAny(result);
Vector3f hitPoint;
SpaceObjectReference result = mMouseHandler->pick(Vector2f(x,y), 1, &hitPoint);

Invokable::Dict pick_result;
pick_result["object"] = Invokable::asAny(result);
Invokable::Dict pick_position;
pick_position["x"] = Invokable::asAny(hitPoint.x);
pick_position["y"] = Invokable::asAny(hitPoint.y);
pick_position["z"] = Invokable::asAny(hitPoint.z);
pick_result["position"] = Invokable::asAny(pick_position);

return Invokable::asAny(pick_result);
}


Expand Down
9 changes: 5 additions & 4 deletions libproxyobject/plugins/ogre/OgreSystemMouseHandler.cpp
Expand Up @@ -99,7 +99,7 @@ void OgreSystemMouseHandler::mouseOverWebView(Camera *cam, Time time, float xPix
}
}

ProxyEntity* OgreSystemMouseHandler::hoverEntity (Camera *cam, Time time, float xPixel, float yPixel, bool mousedown, int *hitCount,int which) {
ProxyEntity* OgreSystemMouseHandler::hoverEntity (Camera *cam, Time time, float xPixel, float yPixel, bool mousedown, int *hitCount,int which, Vector3f* hitPointOut) {
Vector3d pos = cam->getPosition();
Vector3f dir (pixelToDirection(cam, xPixel, yPixel));
SILOG(input,detailed,"OgreSystemMouseHandler::hoverEntity: X is "<<xPixel<<"; Y is "<<yPixel<<"; pos = "<<pos<<"; dir = "<<dir);
Expand All @@ -126,6 +126,7 @@ ProxyEntity* OgreSystemMouseHandler::hoverEntity (Camera *cam, Time time, float
return NULL; // FIXME: should try again.
}
}
if (hitPointOut != NULL) *hitPointOut = Vector3f(pos) + dir.normal()*dist;
return mouseOverEntity;
}
return NULL;
Expand All @@ -146,16 +147,16 @@ bool OgreSystemMouseHandler::recentMouseInRange(float x, float y, float *lastX,
return true;
}

SpaceObjectReference OgreSystemMouseHandler::pick(Vector2f p, int direction) {
SpaceObjectReference OgreSystemMouseHandler::pick(Vector2f p, int direction, Vector3f* hitPointOut) {
if (!mParent||!mParent->mPrimaryCamera) SpaceObjectReference::null();

Camera *camera = mParent->mPrimaryCamera;
Time time = mParent->simTime();

int numObjectsUnderCursor=0;
ProxyEntity *mouseOver = hoverEntity(camera, time, p.x, p.y, true, &numObjectsUnderCursor, mWhichRayObject);
ProxyEntity *mouseOver = hoverEntity(camera, time, p.x, p.y, true, &numObjectsUnderCursor, mWhichRayObject, hitPointOut);
if (recentMouseInRange(p.x, p.y, &mLastHitX, &mLastHitY)==false||numObjectsUnderCursor!=mLastHitCount)
mouseOver = hoverEntity(camera, time, p.x, p.y, true, &mLastHitCount, mWhichRayObject=0);
mouseOver = hoverEntity(camera, time, p.x, p.y, true, &mLastHitCount, mWhichRayObject=0, hitPointOut);
if (mouseOver)
return mouseOver->getProxyPtr()->getObjectReference();

Expand Down
4 changes: 2 additions & 2 deletions libproxyobject/plugins/ogre/OgreSystemMouseHandler.hpp
Expand Up @@ -60,7 +60,7 @@ class OgreSystemMouseHandler {
void setDelegate(Invokable* del);

// FIXME no reason for this to be in this class.
SpaceObjectReference pick(Vector2f p, int direction);
SpaceObjectReference pick(Vector2f p, int direction, Vector3f* hitPointOut=NULL);

//FIXME should this be public?
WebView* mUIWidgetView;
Expand All @@ -75,7 +75,7 @@ class OgreSystemMouseHandler {
Sirikata::Input::Modifier getCurrentModifiers() const;

void mouseOverWebView(Camera *cam, Time time, float xPixel, float yPixel, bool mousedown, bool mouseup);
ProxyEntity* hoverEntity (Camera *cam, Time time, float xPixel, float yPixel, bool mousedown, int *hitCount,int which=0);
ProxyEntity* hoverEntity (Camera *cam, Time time, float xPixel, float yPixel, bool mousedown, int *hitCount,int which=0, Vector3f* hitPointOut=NULL);

bool recentMouseInRange(float x, float y, float *lastX, float *lastY);

Expand Down

0 comments on commit 39d729b

Please sign in to comment.