Biggest issue was how to get all the resources for CEGUI, but I used a
custom ResourceProvider that helped out.

Sure I can submit the code, but we use CEGUI strictly from lua through
a plugin system, so some things in the code is probably a little bit
too specific for that.
Another issue was how to load several layouts, CEGUI doest really have
a scenegraph, so the naming convention is important...

I have attached the files related to the CEGui rendering/event handling stuff.


We still use producer so one class is actually using RenderSurface to
determine size of renderable surface.


Example of use (not tested/compiled code):

Producer::RenderSurface *rs = viewer->getCamera(0)->getRenderSurface();
m_c3dCEGUI = new C3DCEGUI::c3dCEGUI(rs);

 m_c3dCEGUI->init(lua); // we use it together with our lua scripting system

 // Add the CEGUI root-node
root->addChild(m_c3dCEGUI->getOsgNode());

Should do it.
To enable it:
m_c3dCEGUI->setToggleKey(osgGA::GUIEventAdapter::KEY_F1); // is also default

When running, pressing CTRL+F1 will enable CEGUI.

/Anders


On 4/19/07, Ivan BolĨina <[EMAIL PROTECTED]> wrote:
Delta3d has OSG-CEGUI integration solved.. I seems simple...


2007/4/19, E. Wing <[EMAIL PROTECTED]>:
> > From: "Anders Backman"
>
> > Nothing that I know about.
> > Marco was working on osgWidgets for quite some time ago, but I guess it
has
> > stalled? Marco?
> >
> > Other than that I could recommend CEGui, which is  rendered using
OpenGL.
> > We are using it together with a custom drawable that renders the  CEGUI
> > widgets.
> > It has sliders buttons, lua scripting, etc...
> >
> > /Anders
>
> So I'm curious how you implemented the custom drawable for this. Any
> sticky issues? Did you have to wrap everything in CEGui? (sounds like
> a bit of effort)
> Do you have any code examples (or better yet, a public library)
> available for this?
>
> Thanks,
> Eric
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://openscenegraph.net/mailman/listinfo/osg-users
> http://www.openscenegraph.org/
>


_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/



--


________________________________________________________________
Anders Backman               Email:    [EMAIL PROTECTED]
HPC2N/VRlab                  Phone:    +46 (0)90-786 9936
Umea university              Cellular: +46 (0)70-392 64 67
S-901 87 UMEA SWEDEN         Fax:      +46 90-786 6126
                              http://www.cs.umu.se/~andersb
#include "c3dCEGUI.h"

#include <osg/Notify>
#include "c3dResourceProvider.h"
#include <CEGUIPropertySet.h>
#include <CEGUISystem.h>
#include <CEGUISchemeManager.h>
#include <CEGUIWindowManager.h>
#include <elements/CEGUIFrameWindow.h>
#include <elements/CEGUIGUISheet.h>
#include <CEGUIFontManager.h>
#include <CEGUIWindow.h>
#include <CEGUIUDim.h>
#include <CEGUIImageset.h>
#include <CEGUIScheme.h>
#include <CEGUIScriptModule.h>
#include <CEGUIExceptions.h>
#include <OpenGLGUIRenderer/OpenGLRenderer.h>
#include <osg/Geode>
#include <falagard/CEGUIFalWidgetLookManager.h>
#include "CEGUILua.h"
#include "CEGUIDefaultResourceProvider.h"
#include <osg/Switch>


extern "C" {
#include <lauxlib.h>
}

#include "c3dCEGUIDrawable.h"


#include <CEGUI.h>

using namespace C3DCEGUI;


bool 
c3dCEGUI::ToggleCEGUIEventHandler::handle(
      const osgGA::GUIEventAdapter& ea,
      osgGA::GUIActionAdapter& aa, 
      osg::Object* obj, 
      osg::NodeVisitor* nv)
{
  osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(nv);
  if (!ev)
    return false;

  switch(ea.getEventType()) {
    case(osgGA::GUIEventAdapter::KEYDOWN):
      if (ea.getKey() == m_cegui->getToggleKey() ) {
                                
        m_cegui->setEnable(!m_cegui->getEnable());
        ev->setEventHandled(true);

                                // If CTRL button is pressed simulataniously 
with the CEGUI toggle button
                                // the keyboard events is also propagated to 
the rest of the system
                                if (ea.getModKeyMask() && 
osgGA::GUIEventAdapter::MODKEY_CTRL)
                                        m_cegui->setEnableKeyPropagation(false);
                                else
                                        m_cegui->setEnableKeyPropagation(true);

        return true;
      }
      break;
    default:
      return false;
  }
  return false;
}


c3dCEGUI::c3dCEGUI(Producer::RenderSurface *rs, int w, int h) : 
m_initialized(false), 
        m_width(w), m_height(h), m_rs(rs), m_toggle_key(0)

{
  setToggleKey();
}



void c3dCEGUI::setEnable(bool flag)
{
  m_root_node->setValue(0, flag);
}


void c3dCEGUI::init(lua_State *lua_state, unsigned int toggle_key)
{
  if (m_initialized)
    return;

  m_root_node = new osg::Switch();
  m_root_node->setAllChildrenOff();

  m_toggle_event_handler = new ToggleCEGUIEventHandler(this);

  // Add an eventhandler that toggles the CEGUI on/off
  m_root_node->setEventCallback( m_toggle_event_handler.get() );

        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
        camera->setViewMatrix(osg::Matrix::identity());
        camera->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
        camera->setClearMask(GL_DEPTH_BUFFER_BIT);
        camera->setRenderOrder(osg::Camera::POST_RENDER);

  m_root_node->addChild(camera.get());

  m_renderer = 
    new CEGUI::OpenGLRenderer(0,m_width/2,m_height/2);


  CEGUI::LuaScriptModule* script_module = new CEGUI::LuaScriptModule(lua_state);

/*  std::string cfg_file = osgDB::findDataFile("init.cfg");
  if (cfg_file.empty()) {
    throw std::runtime_error("Unable to find file: init.cfg" );
  }
*/

        C3DCEGUI::c3dResourceProvider *provider = new 
C3DCEGUI::c3dResourceProvider;
  new CEGUI::System(m_renderer,provider,0,script_module);//, cfg_file);         
 


  m_ui = CEGUI::System::getSingletonPtr();


  // tell CEGUI to use this scripting module
  CEGUI::System::getSingleton().setScriptingModule(script_module);


  // Create the OSG rendernode
  osg::Geode *geode = new osg::Geode();
  geode->setName("CEUIDrawable_Geode");

  osg::StateSet* stateset = geode->getOrCreateStateSet();
  stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);

  stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
  stateset->setRenderBinDetails(100,"RenderBin");
  stateset->setMode(GL_BLEND,osg::StateAttribute::ON);

  stateset->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);


  geode->setStateSet(stateset);

  osg::ref_ptr<c3dCEGUIDrawable> ce_drawable = new c3dCEGUIDrawable(m_ui);
  geode->addDrawable( ce_drawable.get() ); //add our osg node here

  m_cegui_mouseKeyboardEventHandler = new 
c3dCEGUIMouseKeyboardEventHandler(m_rs.get());
        ce_drawable->setEventCallback(m_cegui_mouseKeyboardEventHandler.get());

  camera->addChild( geode );

  // initialise the required dirs for the DefaultResourceProvider
  CEGUI::DefaultResourceProvider* rp = 
static_cast<CEGUI::DefaultResourceProvider*>
    (CEGUI::System::getSingleton().getResourceProvider());


        // Imagesets
        if (getenv("CEGUI_IMAGESETS_FILE_PATH"))
                setResourceGroupEnvironmentVariable("imagesets", 
"CEGUI_IMAGESETS_FILE_PATH");
        else
                setResourceGroupEnvironmentVariable("imagesets", 
"CEGUI_FILE_PATH");

        // Schemes
        if (getenv("CEGUI_SCHEMES_FILE_PATH"))
                setResourceGroupEnvironmentVariable("schemes", 
"CEGUI_SCHEMES_FILE_PATH");
        else
                setResourceGroupEnvironmentVariable("schemes", 
"CEGUI_FILE_PATH");

        // fonts
        if (getenv("CEGUI_FONTS_FILE_PATH"))
                setResourceGroupEnvironmentVariable("fonts", 
"CEGUI_FONTS_FILE_PATH");
        else
                setResourceGroupEnvironmentVariable("fonts", "CEGUI_FILE_PATH");

        // layouts
        if (getenv("CEGUI_LAYOUTS_FILE_PATH"))
                setResourceGroupEnvironmentVariable("layouts", 
"CEGUI_LAYOUTS_FILE_PATH");
        else
                setResourceGroupEnvironmentVariable("layouts", 
"CEGUI_FILE_PATH");

        // looknfeel
        if (getenv("CEGUI_LOOKNFEEL_FILE_PATH"))
                setResourceGroupEnvironmentVariable("looknfeel", 
"CEGUI_LOOKNFEEL_FILE_PATH");
        else
                setResourceGroupEnvironmentVariable("looknfeel", 
"CEGUI_FILE_PATH");

        // lua
        if (getenv("CEGUI_LUA_FILE_PATH"))
                setResourceGroupEnvironmentVariable("lua", 
"CEGUI_LUA_FILE_PATH");
        else
                setResourceGroupEnvironmentVariable("lua", "CEGUI_FILE_PATH");

  // set the default resource groups to be used
  CEGUI::Imageset::setDefaultResourceGroup("imagesets");
  CEGUI::Font::setDefaultResourceGroup("fonts");
  CEGUI::Scheme::setDefaultResourceGroup("schemes");
  CEGUI::WidgetLookManager::setDefaultResourceGroup("looknfeel");
  CEGUI::WindowManager::setDefaultResourceGroup("layouts");
  CEGUI::ScriptModule::setDefaultResourceGroup("lua");

  //loadSchema("WindowsLook.scheme");

  //CEGUI::FontManager::getSingleton().createFont( 
"DejaVuSans-10.font");//fkp-16.font" );


  //m_ui->setDefaultFont("fkp-16.font");
  //m_ui->setDefaultMouseCursor("WindowsLook", "MouseArrow");
  // loadFont("");




  m_initialized = true;
}

bool c3dCEGUI::setResourceGroupEnvironmentVariable(const std::string& 
resourceGroup, const std::string& env_variable)
{

        C3DCEGUI::c3dResourceProvider* rp = 
static_cast<C3DCEGUI::c3dResourceProvider*>
    (CEGUI::System::getSingleton().getResourceProvider());

  rp->setResourceGroupEnvironmentVariable(resourceGroup, env_variable);

  return true;
}


bool c3dCEGUI::loadScheme(const std::string& filename)
{

        std::string file = filename;
        if (!filename.length())
                file = "WindowsLook.scheme";

        bool status = false;
        try {
                status = CEGUI::SchemeManager::getSingleton().loadScheme(file);
        } catch(CEGUI::AlreadyExistsException& )
        {
                status = false;
        }
        
  return status;

}

bool c3dCEGUI::loadFont(const std::string& filename)
{
        std::string file = filename;
        if (!filename.length())
                file = "DejaVuSans-8.font";

        bool status = false;
        try {
                status = CEGUI::FontManager::getSingleton().createFont(file) != 
0L;
        } 
        catch(CEGUI::AlreadyExistsException& )
        {
                status = false;
        }
  return status;
}


bool c3dCEGUI::loadImageset(const std::string& filename)
{

        bool status = false;
        try {
                status = 
CEGUI::ImagesetManager::getSingleton().createImageset(filename) != 0L;
        } 
        catch(CEGUI::AlreadyExistsException& e)
        {
                status = false;
        }
        return status;
}

bool c3dCEGUI::loadLooknFeel(const std::string& filename)
{

        bool status = false;
        try {
                
CEGUI::WidgetLookManager::getSingleton().parseLookNFeelSpecification(filename);
        } 
        catch(CEGUI::AlreadyExistsException& e)
        {
                status = false;
        }
        return status;
}


bool c3dCEGUI::loadLayout(const std::string& filename)
{
        // create an empty window initially
        CEGUI::Window *root = CEGUI::System::getSingleton().getGUISheet();

        if (!root) {
                root = new CEGUI::GUISheet("DefaultWindow", "C3DRoot" );
                CEGUI::System::getSingleton().setGUISheet(root);
        }
                
  CEGUI::Window* loadedLayout = 
CEGUI::WindowManager::getSingleton().loadWindowLayout(filename);
        if (!loadedLayout)
                return false;
        // Now only add the children of the loaded window to the already 
existing root window
        unsigned int n = loadedLayout->getChildCount();


        for (unsigned int i=0; i < n; i++)
        {
        
                // CEGUI seems to only allow a node to have one parent, so when 
this child is added to root, it 
                // is at the same time removed from loadedLayout.
                CEGUI::Window *child = loadedLayout->getChildAtIdx(0);
                root->addChildWindow(child);
        }
        // What happens to the loadedLayout?
        // I think that CEGUI has some sort of reference system, but Im not 
sure.

//  if (myRoot) {
        //      CEGUI::Window *oldRoot = 
CEGUI::System::getSingleton().getGUISheet();
        //      if (oldRoot)
                        //oldRoot->addChildWindow(myRoot);
        //      else
                //      CEGUI::System::getSingleton().setGUISheet(myRoot);
        //CEGUI::WindowManager::getSingleton().writeWindowLayoutToStream(*root, 
std::cerr, true);

        
        return true;
  //}
  //return false;
}

void c3dCEGUI::setSize(int width, int height)
{
  m_width = width;
  m_height = height;

  m_renderer->setDisplaySize( CEGUI::Size(width, height) );

  //mMouseListener->SetWindowSize( width , height );
}

void c3dCEGUI::shutdown()
{
  if (!m_initialized)
    return;
}

c3dCEGUI::~c3dCEGUI()
{
  shutdown();

  delete m_renderer;
  m_renderer = 0;
}



#ifndef __c3dCEGUI__
#define __c3dCEGUI__

#include <osg/MatrixTransform>
#include <osg/Switch>
#include <osg/Drawable>
#include <Producer/RenderSurface>
#include <osgGA/GUIEventHandler>
#include "LuaPlugins/export.h"
#include <osgDB/FileUtils>
#include "c3dCEGUIMouseKeyboardEventHandler.h"


extern "C" {
#include <lua.h>
}

namespace CEGUI
{
  class System;
  class OpenGLRenderer;
  class Window;
}


namespace C3DCEGUI  {

  class GuiRenderer;

  class LUAPLUGINS_API c3dCEGUI : public osg::Referenced {
  public:
    c3dCEGUI(Producer::RenderSurface *rs, int w=1024,int 
h=768);//,dtGUI::BaseScriptModule*);

    /*!
      Turns the GUI explicitly on/off
    */
    void setEnable(bool flag);

    /// Returns true if the GUI is enabled
    bool getEnable() const { return m_root_node->getValue(0);  }
    
    /// Set which button that enables/disables the UI
    void setToggleKey(unsigned int toggle_key=osgGA::GUIEventAdapter::KEY_F1) { 
m_toggle_key = toggle_key; }

                /*! 
                        Set which button that enables/disables the UI
                        CTRL+this key will enable gui and disable 
keyPropagation (will not propagate keyboard events to the rest of the system)
                */
    unsigned int getToggleKey() const { return m_toggle_key; }

                /*!
                        If set to true, the keyboard events is captured by 
CEGUI and also propagated to the rest of
                        the system.
                        If false, only cegui will catch the keyboard presses.
                */
                void setEnableKeyPropagation(bool flag) { 
m_cegui_mouseKeyboardEventHandler->setEnableKeyPropagation(flag); }
                
                /// Return true if keyboard event is propagated from CEGUI
                bool getEnableKeyPropagation() const { return 
m_cegui_mouseKeyboardEventHandler->getEnableKeyPropagation(); }

    ///Get a pointer to the underlying CEGUI::System
    CEGUI::System* getUI() {return m_ui;}

    ///Get a pointer to the underlying Renderer
    CEGUI::OpenGLRenderer* getRenderer() {return m_renderer;}

    void init(lua_State *lua_state, unsigned int 
toggle_key=osgGA::GUIEventAdapter::KEY_F12);

    void shutdown();

    void showDemo();
    void showDemo1();


    osg::Node *getOsgNode() { return m_root_node.get(); }

    bool loadScheme(const std::string& filename="WindowsLook.scheme");
    bool loadFont(const std::string& filename="DejaVuSans-8.font");
    bool loadLayout(const std::string& filename);
                bool loadImageset(const std::string& filename);
                bool loadLooknFeel(const std::string& filename);


    void setSize(int width, int height);

  protected:
    virtual ~c3dCEGUI();

                bool setResourceGroupEnvironmentVariable(const std::string& 
resourceGroup, const std::string& env_variable);

    struct ToggleCEGUIEventHandler : public osgGA::GUIEventHandler
    {
      ToggleCEGUIEventHandler(c3dCEGUI *cegui) : m_cegui(cegui) {}

      /** do customized Event code. */
      virtual bool handle(const osgGA::GUIEventAdapter& ea,
                          osgGA::GUIActionAdapter& aa, 
                          osg::Object* obj, 
                          osg::NodeVisitor* nv);

          
        osg::observer_ptr<c3dCEGUI> m_cegui;
    };

  private:
    osg::ref_ptr<osg::Switch> m_root_node;
    CEGUI::System *m_ui;
    
    unsigned int m_toggle_key;

    CEGUI::OpenGLRenderer *m_renderer;
    int m_height, m_width;
    bool m_initialized;
    osg::ref_ptr<Producer::RenderSurface> m_rs;
    osg::ref_ptr<ToggleCEGUIEventHandler> m_toggle_event_handler;
                osg::ref_ptr<c3dCEGUIMouseKeyboardEventHandler> 
m_cegui_mouseKeyboardEventHandler;
  };

}

#endif
#include "c3dCEGUIdrawable.h"
#include <CEGUISystem.h>

using namespace C3DCEGUI;

c3dCEGUIDrawable::c3dCEGUIDrawable(CEGUI::System *ui) : m_ui(ui), m_last_time(0)
{
  this->setSupportsDisplayList(false);
  this->setUseDisplayList(false);
}


void c3dCEGUIDrawable::drawImplementation(osg::State& state) const
{
  //tell the UI to update and to render
  if (!m_ui) return;       


  //§glPushAttrib(GL_ALL_ATTRIB_BITS);

        //glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
        glPushAttrib(GL_ALL_ATTRIB_BITS);

        state.setActiveTextureUnit(0);

//      glActiveTextureARB(GL_TEXTURE0);

        state.disableAllVertexArrays();

  double t = state.getFrameStamp()->getReferenceTime();
  m_ui->injectTimePulse(t-m_last_time);
  m_last_time = t;
  m_ui->getSingletonPtr()->renderGUI();

  glPopAttrib();

  state.checkGLErrors("CEGUIDrawable::drawImplementation");
}

osg::Object* c3dCEGUIDrawable::cloneType() const
{
  return new c3dCEGUIDrawable(m_ui); 
}

osg::Object* c3dCEGUIDrawable::clone(const osg::CopyOp& copyop) const
{
  return new c3dCEGUIDrawable(*this,copyop);        
}



c3dCEGUIDrawable::~c3dCEGUIDrawable()
{
}
#ifndef __c3dCEGUIDrawable_h__
#define __c3dCEGUIDrawable_h__

#include <osg/Drawable>
namespace CEGUI {
  class System;
}

namespace C3DCEGUI {



/// Custom drawable that draws the CEGUI in OpenGL
class c3dCEGUIDrawable : public osg::Drawable
{
public:
  /// COPY constructor
  c3dCEGUIDrawable(const c3dCEGUIDrawable& drawable,const osg::CopyOp& 
copyop=osg::CopyOp::SHALLOW_COPY) {}
  
  /// Constructor
  c3dCEGUIDrawable(CEGUI::System *ui);

  virtual osg::Object* cloneType() const;
  virtual osg::Object* clone(const osg::CopyOp& copyop) const;
  
  /// Draws the CEGUI
  virtual void drawImplementation(osg::State& state) const;

protected:

  /// Destructor
  virtual ~c3dCEGUIDrawable();

private:
  mutable double m_last_time;
  CEGUI::System *m_ui;
};

}  // namespace c3dCEGUI
#endif
#include "c3dCEGUIMouseKeyboardEventHandler.h"
#include <CEGUIWindow.h>

using namespace C3DCEGUI;

CEGUI::uint c3dCEGUIMouseKeyboardEventHandler::producerToCEGUIKey(int 
producer_key)
{
  switch(producer_key) {

  case(osgGA::GUIEventAdapter::KEY_Tab):
    return CEGUI::Key::Tab;
    break;
    /*      case(osgGA::GUIEventAdapter::KEY_Linefeed):
    return CEGUI::Key;
    break;
    */
    /*      case(osgGA::GUIEventAdapter::KEY_Clear):
    return CEGUI::Key::cl;
    break;
    */
  case(osgGA::GUIEventAdapter::KEY_Pause):
    return CEGUI::Key::Pause;
    break;

  case(osgGA::GUIEventAdapter::KEY_Scroll_Lock):
    return CEGUI::Key::ScrollLock;
    break;


  case(osgGA::GUIEventAdapter::KEY_Escape):
    return CEGUI::Key::Escape;
    break;

  case(osgGA::GUIEventAdapter::KEY_Delete):
    return CEGUI::Key::Delete;
    break;

  case(osgGA::GUIEventAdapter::KEY_Home):
    return CEGUI::Key::Home;
    break;

  case(osgGA::GUIEventAdapter::KEY_Left):
    return CEGUI::Key::ArrowLeft;
    break;

  case(osgGA::GUIEventAdapter::KEY_Right):
    return CEGUI::Key::ArrowRight;
    break;

  case(osgGA::GUIEventAdapter::KEY_Up):
    return CEGUI::Key::ArrowUp;
    break;

  case(osgGA::GUIEventAdapter::KEY_Down):
    return CEGUI::Key::ArrowDown;
    break;

  case(osgGA::GUIEventAdapter::KEY_Page_Up):
    return CEGUI::Key::PageUp;
    break;

  case(osgGA::GUIEventAdapter::KEY_Page_Down):
    return CEGUI::Key::PageDown;
    break;

  case(osgGA::GUIEventAdapter::KEY_Insert):
    return CEGUI::Key::Insert;
    break;

  case(osgGA::GUIEventAdapter::KEY_Num_Lock):
    return CEGUI::Key::NumLock;
    break;

  case(osgGA::GUIEventAdapter::KEY_Alt_L):
    return CEGUI::Key::LeftAlt;
    break;

  case(osgGA::GUIEventAdapter::KEY_Alt_R):
    return CEGUI::Key::RightAlt;
    break;


  case(osgGA::GUIEventAdapter::MODKEY_LEFT_SHIFT):
    return CEGUI::Key::LeftShift;
    break;

  case(osgGA::GUIEventAdapter::MODKEY_RIGHT_SHIFT):
    return CEGUI::Key::RightShift;
    break;

  case(osgGA::GUIEventAdapter::MODKEY_LEFT_CTRL):
    return CEGUI::Key::LeftControl;
    break;

  case(osgGA::GUIEventAdapter::MODKEY_RIGHT_CTRL):
    return CEGUI::Key::RightControl;
    break;

    /*     case(osgGA::GUIEventAdapter::MODKEY_CTRL):
    return CEGUI::SystemKey::Control;
    break;

    case(osgGA::GUIEventAdapter::MODKEY_SHIFT):
    return CEGUI::SystemKey::Shift;
    break;

    case(osgGA::GUIEventAdapter::MODKEY_ALT):
    return CEGUI::SystemKey::Alt;
    break;
    */
  case(osgGA::GUIEventAdapter::KEY_BackSpace):
    return CEGUI::Key::Backspace;
    break;

  case(osgGA::GUIEventAdapter::KEY_F1):
    return CEGUI::Key::F1;
    break;

  case(osgGA::GUIEventAdapter::KEY_F2):
    return CEGUI::Key::F2;
    break;

  case(osgGA::GUIEventAdapter::KEY_F3):
    return CEGUI::Key::F3;
    break;

  case(osgGA::GUIEventAdapter::KEY_F4):
    return CEGUI::Key::F4;
    break;
  case(osgGA::GUIEventAdapter::KEY_F5):
    return CEGUI::Key::F5;
    break;
  case(osgGA::GUIEventAdapter::KEY_F6):
    return CEGUI::Key::F6;
    break;
  case(osgGA::GUIEventAdapter::KEY_F7):
    return CEGUI::Key::F7;
    break;
  case(osgGA::GUIEventAdapter::KEY_F8):
    return CEGUI::Key::F8;
    break;
  case(osgGA::GUIEventAdapter::KEY_F9):
    return CEGUI::Key::F9;
    break;
  case(osgGA::GUIEventAdapter::KEY_F10):
    return CEGUI::Key::F10;
    break;
  case(osgGA::GUIEventAdapter::KEY_F11):
    return CEGUI::Key::F11;
    break;
  case(osgGA::GUIEventAdapter::KEY_F12):
    return CEGUI::Key::F12;
    break;

  default:
    return CEGUI::Key::Scan(producer_key);
    break;
  }
  return CEGUI::Key::Scan(0);
}

bool  c3dCEGUIMouseKeyboardEventHandler::isMouseEventCaptured() const
{
        CEGUI::Window *wc = CEGUI::Window::getCaptureWindow ();
        return wc != 0L;
}

bool 
c3dCEGUIMouseKeyboardEventHandler::handle(
  const osgGA::GUIEventAdapter& ea,
  osgGA::GUIActionAdapter& aa, 
  osg::Object* obj, 
  osg::NodeVisitor* nv)
{
  osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(nv);
  c3dCEGUIDrawable* cd = dynamic_cast<c3dCEGUIDrawable*>(obj);
        
  if (!ev || !cd) return false;


  float px=ea.getX(), py=ea.getY();

  _producer_to_cegui(px,py);
  _convert_to_screen(px,py);

  switch(ea.getEventType())
  {
    case(osgGA::GUIEventAdapter::FRAME):
    {
      CEGUI::OpenGLRenderer *renderer = static_cast<CEGUI::OpenGLRenderer 
*>(CEGUI::System::getSingletonPtr()->getRenderer());
      renderer->setDisplaySize(CEGUI::Size(m_rs->getWindowWidth(), 
m_rs->getWindowHeight()) );
      CEGUI::System::getSingleton().injectTimePulse(1/60);
      return false;

    }

    case(osgGA::GUIEventAdapter::DRAG):
                case(osgGA::GUIEventAdapter::MOVE): {
                
                        
CEGUI::System::getSingleton().injectMousePosition(px,py);

                        if (isMouseEventCaptured()) {
                                ev->setEventHandled(true);
                                return true;
                        }
                        else
                                return false;
                                                                                
                                                                                
          }
    case(osgGA::GUIEventAdapter::PUSH):
      {
        CEGUI::System::getSingleton().injectMousePosition(px,py);

        if (ea.getButton() == 
          osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)  // left

          
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);

        else if (ea.getButton() == 
          osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)  // middle

          
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);

        else if (ea.getButton() == 
          osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)  // right

          
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);

                                if (isMouseEventCaptured()) {
                                        ev->setEventHandled(true);
                                        return true;
                                }
                                else
                                        return false;
      }
    case(osgGA::GUIEventAdapter::RELEASE):
      {
        CEGUI::System::getSingleton().injectMousePosition(px,py);

        if (ea.getButton() == 
          osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)  // left

          CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton);

        else if (ea.getButton() == 
          osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)  // middle

          
CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::MiddleButton);

        else if (ea.getButton() == 
          osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)  // right

          CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton);

                                if (isMouseEventCaptured()) {
                                        ev->setEventHandled(true);
                                        return true;
                                }
                                else
                                        return false;
      }
    case(osgGA::GUIEventAdapter::DOUBLECLICK):
      {
        // do we need to do something special here to handle double 
        //click???  Will just assume button down for now.
        CEGUI::System::getSingleton().injectMousePosition(ea.getX(), 
          ea.getY());

        if (ea.getButton() == 
          osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)  // left

          
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);

        else if (ea.getButton() == 
          osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)  // middle

          
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);

        else if (ea.getButton() == 
          osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)  // right

          
CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);

                                if (isMouseEventCaptured()) {
                                        ev->setEventHandled(true);
                                        return true;
                                }
                                else
                                        return false;
      }
    case(osgGA::GUIEventAdapter::KEYDOWN):

      
CEGUI::System::getSingleton().injectKeyDown(producerToCEGUIKey(ea.getKey()));
      //static_cast<CEGUI::uint>(ea.getKey()) );
      CEGUI::System::getSingleton().injectChar( 
        static_cast<CEGUI::utf32>( ea.getKey() ) );

                        if (!getEnableKeyPropagation()){
                                ev->setEventHandled(true);
                                return true;
                        }
                        else
                                return false;

    case(osgGA::GUIEventAdapter::KEYUP):
      
CEGUI::System::getSingleton().injectKeyUp(producerToCEGUIKey(ea.getKey()));

      //      CEGUI::System::getSingleton().injectKeyUp( 
      //        static_cast<CEGUI::uint>(ea.getKey()) );

                        if (!getEnableKeyPropagation()){
                                ev->setEventHandled(true);
                                return true;
                        }
                        else
                                return false;

                default:
      break;
  }

  return false;
}
#ifndef __c3dCEGUIMouseKeyboardEventHandler_h__
#define __c3dCEGUIMouseKeyboardEventHandler_h__

#include <osgGA/GuiEventHandler>
#include <Producer/KeyboardMouse>
#include <osgGA/EventVisitor>
#include <CEGUISystem.h>
#include "c3dCEGUIDrawable.h"
#include <OpenGLGUIRenderer/OpenGLRenderer.h>



namespace C3DCEGUI {


  struct c3dCEGUIMouseKeyboardEventHandler : public osgGA::GUIEventHandler
  {
    c3dCEGUIMouseKeyboardEventHandler(Producer::RenderSurface *rs) : m_rs(rs), 
m_propagate_keys(true) {}


    void _producer_to_cegui(float& mx, float& my)
    {
      // --- horizontal math
      // ph = 1.0 - -1.0 = 2.0    // total horizontal distance in producer
      // ch = 1.0 -  0.0 = 1.0    // total horizontal distance in ce

      // --- vertical math
      // pv = 1.0 - -1.0 =  2.0   // total vertical distance in producer
      // cv = 0.0 -  1.0 = -1.0   // total vertical distance in ce

      // cex = cx + px * (ch/2 / ph/2) = 0.5 + px * ( 0.5/1.0) = 0.5 + px*0.5
      // cey = cy + py * (cv/2 / pv/2) = 0.5 + py * (-0.5/1.0) = 0.5 - py*0.5
      // where cx is the "center" x value in ce
      // and   cy is the "center" y value in ce
      float cex(0.5 + mx*0.5);
      float cey(0.5 - my*0.5);
      mx = cex;
      my = cey;
    }

    /** takes normalized CEGUI coordinates and converts
    * into pixel values for the screen.
    */
    void _convert_to_screen(float& mx, float& my)
    {
      unsigned int width(  m_rs->getWindowWidth()  );
      unsigned int height( m_rs->getWindowHeight() );

      // the above yields the same results as the code below
      //int wx(0), wy(0);
      //unsigned int width(0), height(0);
      //_kbm->getRenderSurface()->getWindowRectangle(wx,wy,width,height);

      float pixel_x = (float)width  * mx;
      float pixel_y = (float)height * my;
      mx = pixel_x;
      my = pixel_y;
    }

    CEGUI::uint producerToCEGUIKey(int producer_key);

    /** do customized Event code. */
    virtual bool 
      handle(const osgGA::GUIEventAdapter& ea,
             osgGA::GUIActionAdapter& aa, 
             osg::Object* obj, 
             osg::NodeVisitor* nv);

                void setEnableKeyPropagation(bool flag) {  m_propagate_keys= 
flag; }

                bool getEnableKeyPropagation() const { return m_propagate_keys; 
}

        
                bool isMouseEventCaptured() const;
        private:
    osg::ref_ptr<Producer::RenderSurface> m_rs;
                bool m_propagate_keys;
  };

}  // namespace c3dCEGUI
#endif
#include "c3dResourceProvider.h"
#include "CEGUIExceptions.h"

#include <fstream>

// Start of CEGUI namespace section
using namespace C3DCEGUI;
using namespace CEGUI;

void c3dResourceProvider::loadRawDataContainer(const String& filename, 
RawDataContainer& output, const String& resourceGroup)
{
        if (filename.empty())
        {
                throw InvalidRequestException(
                        "c3dResourceProvider::load - Filename supplied for data 
loading must be valid");
        }

        String final_filename(getFinalFilename(filename, resourceGroup));

        std::ifstream dataFile(final_filename.c_str(), 
std::ios::binary|std::ios::ate);
        if( dataFile.fail())
        {
                throw InvalidRequestException(
                        "c3dResourceProvider::load - " + filename + " does not 
exist");
        }
        std::streampos size = dataFile.tellg();
        dataFile.seekg (0, std::ios::beg);

        unsigned char* buffer = new unsigned char [size];

        try {
                dataFile.read(reinterpret_cast<char*>(buffer), size);
        }
        catch(std::ifstream::failure e) {
                delete [] buffer;
                throw GenericException(
                        "c3dResourceProvider::loadRawDataContainer - Problem 
reading " + filename);
        }

        dataFile.close();

        output.setData(buffer);
        output.setSize(size);
}

void c3dResourceProvider::unloadRawDataContainer(RawDataContainer& data)
{
        uint8* ptr = data.getDataPtr();
        delete [] ptr;
        data.setData(0);
        data.setSize(0);
}


void c3dResourceProvider::setResourceGroupFilePath(const std::string& 
resourceGroup, const std::string& pathList)
{
        ResourceGroupMap::iterator it;

        it=m_resourceGroups.find(resourceGroup);
        if (it == m_resourceGroups.end()) {
                vrutils::FilePathContainer *fp = new vrutils::FilePathContainer;
                fp->setFilePathList(pathList);
                m_resourceGroups[resourceGroup] = fp;
        }
        else {
                it->second->setFilePathList(pathList);
        }
}

void c3dResourceProvider::setResourceGroupEnvironmentVariable(const 
std::string& resourceGroup, const std::string& environment_variable)
{
        ResourceGroupMap::iterator it;

        it=m_resourceGroups.find(resourceGroup);
        if (it == m_resourceGroups.end()) {
                vrutils::FilePathContainer *fp = new vrutils::FilePathContainer;
                fp->setEnvironmentVariable(environment_variable);
                m_resourceGroups[resourceGroup] = fp;
        }
        else {
                it->second->setEnvironmentVariable(environment_variable);
        }
}


void c3dResourceProvider::clearResourceGroupFilePath(const std::string& 
resourceGroup)
{
        ResourceGroupMap::iterator it;

        it=m_resourceGroups.find(resourceGroup);
        if (it != m_resourceGroups.end()) {
                m_resourceGroups.erase(it);
        }
        return;
}

String c3dResourceProvider::getFinalFilename(const String& filename, const 
String& resourceGroup) const
{

        ResourceGroupMap::const_iterator it;

        it=m_resourceGroups.find(resourceGroup.c_str());
        if (it == m_resourceGroups.end())
                return "";



        String final_filename;
        std::string path = it->second->findFile(filename.c_str());
        //std::cerr << "Trying to load " << resourceGroup << ": " << path << 
std::endl;
        
        final_filename = path.c_str();

        // return result
        return final_filename;
}

#ifndef __c3dResourceProvider_h__
#define __c3dResourceProvider_h__

#include "CEGUIBase.h"
#include "CEGUIResourceProvider.h"
#include <vrutils/os/FilePathContainer.h>
#include <osg/ref_ptr>

#include <map>

#if defined(_MSC_VER)
#       pragma warning(push)
#       pragma warning(disable : 4251)
#endif

// Start of CEGUI namespace section
namespace C3DCEGUI
{
        class c3dResourceProvider : public CEGUI::ResourceProvider
        {
        public:
                
/*************************************************************************
                Construction and Destruction
                
*************************************************************************/
                c3dResourceProvider() {}
                ~c3dResourceProvider(void) {}

                /*!
                \brief
                Set the path list for a specified resourcegroup

                \param resourceGroup
                The resource group identifier whose path list is to be set.

                \param pathList
                Semicolon/colon separated list of paths where to search for 
datafiles for the specified resourceGroup
                
                \a resourceGroup
                Name of resourceGroup

                \return
                Nothing.
                */
                void setResourceGroupFilePath(const std::string& resourceGroup, 
const std::string& pathList);

                void setResourceGroupEnvironmentVariable(const std::string& 
resourceGroup, const std::string& environment_variable);


                /*!
                \brief
                clears any currently set directory for the specified resource 
group
                identifier.

                \param resourceGroup
                The resource group identifier for which the associated 
directory is to
                be cleared.
                */
                void clearResourceGroupFilePath(const std::string& 
resourceGroup);

                void loadRawDataContainer(const CEGUI::String& filename, 
CEGUI::RawDataContainer& output, const CEGUI::String& resourceGroup);
                void unloadRawDataContainer(CEGUI::RawDataContainer& data);

        protected:

                CEGUI::String getFinalFilename(const CEGUI::String& filename, 
const CEGUI::String& resourceGroup) const;

                typedef std::map<CEGUI::String, 
osg::ref_ptr<vrutils::FilePathContainer>> ResourceGroupMap;
                ResourceGroupMap    m_resourceGroups;
        };

} // End of  c3dCEGUI namespace section

#if defined(_MSC_VER)
#       pragma warning(pop)
#endif

#endif  // end of guard 
_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/

Reply via email to