Acheron
21-03-2011 13:01:03
Hi I'm new to MOGRE, and almost got MOGRE Intermediate tutorial 4 working, but the values from the MOIS mouse pointer seem to be incorrect. Basically you can draw the box if you click in the top left of the window. The class is derived from the one in the MOgre demo. So is there a bug in MOIS or am I doing something wrong?
using System;
using System.Collections.Generic;
using System.Text;
using MOIS;
using Mogre;
namespace Mogre.Demo.ExampleApplication
{
class Program
{
static void Main(string[] args)
{
try
{
VolumeSelectionApplication app = new VolumeSelectionApplication();
app.Go();
}
catch (System.Runtime.InteropServices.SEHException)
{
// Check if it's an Ogre Exception
if (OgreException.IsThrown)
ExampleApplication.Example.ShowOgreException();
else
throw;
}
}
}
class VolumeSelectionApplication : Mogre.Demo.ExampleApplication.Example
{
Vector2 mStart, mStop;
PlaneBoundedVolumeListSceneQuery mVolQuery;
List<MovableObject> mSelected = new List<MovableObject>();
SelectionRectangle mRect;
bool bSelecting;
public override void CreateFrameListener()
{
base.CreateFrameListener();
Root.Singleton.FrameStarted += FrameStarted;
}
public override bool UseBufferedInput
{
get { return true; }
}
public override void CreateInput()
{
// this is completely overridden from the base class
LogManager.Singleton.LogMessage("*** Initializing OIS ***");
MOIS.ParamList paramList = new MOIS.ParamList();
paramList.Insert("w32_mouse", "DISCL_FOREGROUND");
paramList.Insert("w32_mouse", "DISCL_NONEXCLUSIVE");
IntPtr windowHnd;
window.GetCustomAttribute("WINDOW", out windowHnd);
paramList.Insert("WINDOW", windowHnd.ToString());
inputManager = MOIS.InputManager.CreateInputSystem(paramList);
//Create all devices (We only catch joystick exceptions here, as, most people have Key/Mouse)
inputKeyboard = (MOIS.Keyboard)inputManager.CreateInputObject(MOIS.Type.OISKeyboard, UseBufferedInput);
inputMouse = (MOIS.Mouse)inputManager.CreateInputObject(MOIS.Type.OISMouse, UseBufferedInput);
inputMouse.MousePressed += new MouseListener.MousePressedHandler(OnMousePressed);
inputMouse.MouseReleased += new MouseListener.MouseReleasedHandler(OnMouseReleased);
inputMouse.MouseMoved += new MouseListener.MouseMovedHandler(OnMouseMoved);
}
protected bool OnMousePressed(MouseEvent evt, MouseButtonID button)
{
switch (button)
{
case MOIS.MouseButtonID.MB_Left:
mStart.x = (float)evt.state.X.abs / (float)evt.state.width;
mStart.y = (float)evt.state.Y.abs / (float)evt.state.height;
mStop = mStart;
bSelecting = true;
mRect.Clear();
mRect.Visible = true;
mDebugText = evt.state.X.abs + ", " + evt.state.Y.abs;
break;
}
return true;
}
protected bool OnMouseReleased(MouseEvent evt, MouseButtonID button)
{
switch (button)
{
case MOIS.MouseButtonID.MB_Left:
performSelection(mStart, mStop);
bSelecting = false;
mRect.Visible = false;
break;
}
return true;
}
protected bool OnMouseMoved(MouseEvent evt)
{
if (bSelecting)
{
mStop.x = evt.state.X.abs / (float)evt.state.width;
mStop.y = evt.state.Y.abs / (float)evt.state.height;
mRect.setCorners(mStart, mStop);
}
return true;
}
protected override void HandleInput(FrameEvent evt)
{
// this is completely overridden from the base class, to prevent it doing the camera movement stuff
inputKeyboard.Capture();
inputMouse.Capture();
if (inputKeyboard.IsKeyDown(MOIS.KeyCode.KC_ESCAPE))
{
// stop rendering loop
shutDown = true;
}
// update performance stats once per second
if (statDelay < 0.0f && showDebugOverlay)
{
UpdateStats();
statDelay = 1.0f;
}
else
{
statDelay -= evt.timeSinceLastFrame;
}
// turn off debug text when delay ends
if (debugTextDelay < 0.0f)
{
debugTextDelay = 0.0f;
mDebugText = "";
}
else if (debugTextDelay > 0.0f)
{
debugTextDelay -= evt.timeSinceLastFrame;
}
}
bool FrameStarted(FrameEvent evt)
{
return true;
}
public override void DestroyScene()
{
sceneMgr.DestroyQuery(mVolQuery);
mRect.Dispose();
}
public override void CreateScene()
{
camera.SetPosition(-60, 100, -60);
camera.LookAt(60, 0, 60);
sceneMgr.AmbientLight = ColourValue.White;
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
Entity ent = sceneMgr.CreateEntity("Robot" + (i + j * 10), "robot.mesh");
//Mogre.StringConverter.ToString(i + j * 10)
SceneNode node = sceneMgr.RootSceneNode.CreateChildSceneNode(new Vector3(i * 15, 0, j * 15));
node.AttachObject(ent);
node.SetScale(0.1f, 0.1f, 0.1f);
}
}
mRect = new SelectionRectangle("Selection SelectionRectangle");
sceneMgr.RootSceneNode.CreateChildSceneNode().AttachObject(mRect);
}
void performSelection(Vector2 first, Vector2 second)
{
}
void selectObject(MovableObject obj)
{
obj.ParentSceneNode.ShowBoundingBox = true;
mSelected.Add(obj);
}
void deselectObjects()
{
foreach (var obj in mSelected)
{
obj.ParentSceneNode.ShowBoundingBox = false;
}
mSelected.Clear();
}
static void swap(ref float x, ref float y)
{
float tmp = x;
x = y;
y = tmp;
}
}
public class SelectionRectangle : ManualObject
{
public SelectionRectangle(String name)
: base(name)
{
RenderQueueGroup = (byte)RenderQueueGroupID.RENDER_QUEUE_OVERLAY; // when using this, ensure Depth Check is Off in the material
UseIdentityProjection = true;
UseIdentityView = true;
QueryFlags = 0;
}
/**
* Sets the corners of the SelectionRectangle. Every parameter should be in the
* range [0, 1] representing a percentage of the screen the SelectionRectangle
* should take up.
*/
void setCorners(float left, float top, float right, float bottom)
{
left = left * 2 - 1;
right = right * 2 - 1;
top = 1 - top * 2;
bottom = 1 - bottom * 2;
Clear();
Begin("", RenderOperation.OperationTypes.OT_LINE_STRIP);
Position(left, top, -1);
Position(right, top, -1);
Position(right, bottom, -1);
Position(left, bottom, -1);
Position(left, top, -1);
End();
BoundingBox.SetInfinite();
}
public void setCorners(Vector2 topLeft, Vector2 bottomRight)
{
setCorners(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
}
}
}