1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22 
23 #include "../Precompiled.h"
24 
25 #include "../Core/Context.h"
26 #include "../Graphics/DebugRenderer.h"
27 #include "../IO/Log.h"
28 #include "../Navigation/NavArea.h"
29 #include "../Scene/Node.h"
30 
31 namespace Urho3D
32 {
33 
34 static const unsigned MAX_NAV_AREA_ID = 255;
35 static const Vector3 DEFAULT_BOUNDING_BOX_MIN(-10.0f, -10.0f, -10.0f);
36 static const Vector3 DEFAULT_BOUNDING_BOX_MAX(10.0f, 10.0f, 10.0f);
37 static const unsigned DEFAULT_AREA_ID = 0;
38 
39 extern const char* NAVIGATION_CATEGORY;
40 
NavArea(Context * context)41 NavArea::NavArea(Context* context) :
42     Component(context),
43     areaID_(DEFAULT_AREA_ID),
44     boundingBox_(DEFAULT_BOUNDING_BOX_MIN, DEFAULT_BOUNDING_BOX_MAX)
45 {
46 }
47 
~NavArea()48 NavArea::~NavArea()
49 {
50 }
51 
RegisterObject(Context * context)52 void NavArea::RegisterObject(Context* context)
53 {
54     context->RegisterFactory<NavArea>(NAVIGATION_CATEGORY);
55 
56     URHO3D_COPY_BASE_ATTRIBUTES(Component);
57     URHO3D_ATTRIBUTE("Bounding Box Min", Vector3, boundingBox_.min_, DEFAULT_BOUNDING_BOX_MIN, AM_DEFAULT);
58     URHO3D_ATTRIBUTE("Bounding Box Max", Vector3, boundingBox_.max_, DEFAULT_BOUNDING_BOX_MAX, AM_DEFAULT);
59     URHO3D_ACCESSOR_ATTRIBUTE("Area ID", GetAreaID, SetAreaID, unsigned, DEFAULT_AREA_ID, AM_DEFAULT);
60 }
61 
SetAreaID(unsigned newID)62 void NavArea::SetAreaID(unsigned newID)
63 {
64     if (newID > MAX_NAV_AREA_ID)
65         URHO3D_LOGERRORF("NavArea Area ID %u exceeds maximum value of %u", newID, MAX_NAV_AREA_ID);
66     areaID_ = (unsigned char)newID;
67     MarkNetworkUpdate();
68 }
69 
GetWorldBoundingBox() const70 BoundingBox NavArea::GetWorldBoundingBox() const
71 {
72     Matrix3x4 mat;
73     mat.SetTranslation(node_->GetWorldPosition());
74     return boundingBox_.Transformed(mat);
75 }
76 
DrawDebugGeometry(DebugRenderer * debug,bool depthTest)77 void NavArea::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
78 {
79     if (debug && IsEnabledEffective())
80     {
81         Matrix3x4 mat;
82         mat.SetTranslation(node_->GetWorldPosition());
83         debug->AddBoundingBox(boundingBox_, mat, Color::GREEN, depthTest);
84         debug->AddBoundingBox(boundingBox_, mat, Color(0.0f, 1.0f, 0.0f, 0.15f), true, true);
85     }
86 }
87 
88 }
89