Browse Source

Work on example

Josh Engebretson 8 years ago
parent
commit
8b80c73a8a

+ 67 - 17
FeatureExamples/CPlusPlus/Source/HelloGui3D.cpp

@@ -38,6 +38,7 @@
 #include <Atomic/UI/UIEvents.h>
 #include <Atomic/UI/UIFontDescription.h>
 #include <Atomic/UI/UIView.h>
+#include <Atomic/UI/UIComponent.h>
 #include <Atomic/UI/UILayout.h>
 #include <Atomic/UI/UICheckBox.h>
 #include <Atomic/UI/UITextField.h>
@@ -64,7 +65,7 @@ void HelloGui3D::Start()
 
     SimpleCreateInstructions();
 
-    // Create "Hello GUI"
+    // Create 2D "Hello GUI"
     CreateUI();
 
     // Create the scene content
@@ -136,10 +137,6 @@ void HelloGui3D::CreateScene()
     // optimizing manner
     scene_->CreateComponent<Octree>();
 
-    SharedPtr<Material> material(new Material(context_));
-    material->SetTechnique(0, cache->GetResource<Technique>("Techniques/Diff.xml"));
-    material->SetTexture(Atomic::TU_DIFFUSE, view3D_->GetRenderTexture());
-
     // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
     // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
     // (100 x 100 world units)
@@ -147,7 +144,6 @@ void HelloGui3D::CreateScene()
     planeNode->SetScale(Vector3(5.0f, 1.0f, 5.0f));
     StaticModel* planeObject = planeNode->CreateComponent<StaticModel>();
     planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl"));
-    planeObject->SetMaterial(material);
 
     // Create a directional light to the world so that we can see something. The light scene node's orientation controls the
     // light direction; we will use the SetDirection() function which calculates the orientation from a forward direction vector.
@@ -157,6 +153,10 @@ void HelloGui3D::CreateScene()
     Light* light = lightNode->CreateComponent<Light>();
     light->SetLightType(LIGHT_DIRECTIONAL);
 
+    uiComponent_ = planeNode->CreateComponent<UIComponent>();
+    uiComponent_->SetStaticModel(planeObject);
+    uiComponent_->SetUIView(CreateUI(true));
+
     // Create a scene node for the camera, which we will move around
     // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
     cameraNode_ = scene_->CreateChild("Camera");
@@ -167,12 +167,15 @@ void HelloGui3D::CreateScene()
 }
 
 
-void HelloGui3D::CreateUI()
+UIView* HelloGui3D::CreateUI(bool renderToTexture)
 {
-    int size = 192;
+    UIView* view = renderToTexture ? new UIView(context_) : FeatureExamples::GetUIView();
 
-    view3D_ = new UIView(context_, true);
-    view3D_->SetRenderToTexture(true, size, size);
+    if (renderToTexture)
+    {
+        view3D_ = view;
+        view->SetRenderToTexture(true);
+    }
 
     UILayout* layout = new UILayout(context_);
     layout->SetAxis(UI_AXIS_Y);
@@ -191,20 +194,53 @@ void HelloGui3D::CreateUI()
     UIEditField* edit = new UIEditField(context_);
     layout->AddChild(edit);
     edit->SetId("EditField");
+    edit->SetText("This is a test");
+
+    SharedPtr<UIWindow> window( new UIWindow(context_) );
+    window->SetSettings( (UI_WINDOW_SETTINGS) (UI_WINDOW_SETTINGS_TITLEBAR | UI_WINDOW_SETTINGS_CLOSE_BUTTON));
+
+    window->SetText("Hello Atomic GUI!");
 
-    window_ = new UIWindow(context_);
-    window_->SetSettings( (UI_WINDOW_SETTINGS) (UI_WINDOW_SETTINGS_TITLEBAR | UI_WINDOW_SETTINGS_CLOSE_BUTTON));
+    window->AddChild(layout);
 
-    window_->SetText("Hello Atomic GUI!");
+    window->SetSize(renderToTexture ? view->GetWidth() : 512, renderToTexture ? view->GetHeight() : 512);
 
-    window_->AddChild(layout);
+    view->AddChild(window);
+    window->Center();
 
-    window_->SetSize(size, size);
+    return view;
 
-    view3D_->AddChild(window_);
-    window_->Center();
 }
 
+bool HelloGui3D::Raycast(float maxDistance, Vector3& hitPos, Drawable*& hitDrawable)
+{
+    hitDrawable = 0;
+
+    Input* input = GetSubsystem<Input>();
+    IntVector2 pos = input->GetMousePosition();
+    // Check the cursor is visible and there is no UI element in front of the cursor
+    if (!input->IsMouseVisible())
+        return false;
+
+    Graphics* graphics = GetSubsystem<Graphics>();
+    Camera* camera = cameraNode_->GetComponent<Camera>();
+    Ray cameraRay = camera->GetScreenRay((float)pos.x_ / graphics->GetWidth(), (float)pos.y_ / graphics->GetHeight());
+    // Pick only geometry objects, not eg. zones or lights, only get the first (closest) hit
+    PODVector<RayQueryResult> results;
+    RayOctreeQuery query(results, cameraRay, RAY_TRIANGLE, maxDistance, DRAWABLE_GEOMETRY);
+    scene_->GetComponent<Octree>()->RaycastSingle(query);
+    if (results.Size())
+    {
+        RayQueryResult& result = results[0];
+        hitPos = result.position_;
+        hitDrawable = result.drawable_;
+        return uiComponent_->GetStaticModel() == hitDrawable;
+    }
+
+    return false;
+}
+
+
 void HelloGui3D::SubscribeToEvents()
 {
     // Subscribe HandleUpdate() function for processing update events
@@ -216,6 +252,7 @@ void HelloGui3D::SubscribeToEvents()
 
 void HelloGui3D::HandleWidgetEvent(StringHash eventType, VariantMap& eventData)
 {
+    /*
     using namespace WidgetEvent;
 
     if (eventData[P_TYPE] == UI_EVENT_TYPE_CLICK)
@@ -227,6 +264,7 @@ void HelloGui3D::HandleWidgetEvent(StringHash eventType, VariantMap& eventData)
         }
 
     }
+    */
 }
 
 void HelloGui3D::HandleWidgetDeleted(StringHash eventType, VariantMap& eventData)
@@ -242,6 +280,18 @@ void HelloGui3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
     // Take the frame time step, which is stored as a float
     float timeStep = eventData[P_TIMESTEP].GetFloat();
 
+    Vector3 hitPos;
+    Drawable* hitDrawable;
+
+    if (Raycast(250.0f, hitPos, hitDrawable))
+    {
+        view3D_->SetActive(true);
+    }
+    else
+    {
+        view3D_->SetActive(false);
+    }
+
     // Move the camera, scale movement with time step
     MoveCamera(timeStep);
 }

+ 5 - 2
FeatureExamples/CPlusPlus/Source/HelloGui3D.h

@@ -28,6 +28,7 @@
 namespace Atomic
 {
 class UIWindow;
+class UIComponent;
 }
 
 class HelloGui3D : public Sample
@@ -46,7 +47,7 @@ protected:
 private:
 
     void CreateScene();
-    void CreateUI();
+    UIView* CreateUI(bool renderToTexture = false);
     /// Set up a viewport for displaying the scene.
     void SetupViewport();
     /// Subscribe to application-wide logic update events.
@@ -57,9 +58,11 @@ private:
     void MoveCamera(float timeStep);
 
     void HandleWidgetEvent(StringHash eventType, VariantMap& eventData);
-
     void HandleWidgetDeleted(StringHash eventType, VariantMap& eventData);
 
+    bool Raycast(float maxDistance, Vector3& hitPos, Drawable*& hitDrawable);
+
     WeakPtr<UIView> view3D_;
+    WeakPtr<UIComponent> uiComponent_;
     WeakPtr<UIWindow> window_;
 };