Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Johannes Braun
glare
Commits
e78c388a
Commit
e78c388a
authored
Aug 21, 2017
by
Johannes Braun
Browse files
Unified scene entity methods
parent
d6eff563
Changes
50
Hide whitespace changes
Inline
Side-by-side
src/executables/pathtracing/main.cpp
View file @
e78c388a
...
...
@@ -126,7 +126,7 @@ void loadScene(const fs::path& path, float scale)
console
::
prompt
(
"Scene could not be loaded: "
+
path
.
string
()
+
". Press [ENTER] to exit."
,
1
);
}
std
::
shared_ptr
<
core
::
Graph
Node
>
cam_node
=
core
::
state
::
graph_root
->
find
<
core
::
Camera
>
();
std
::
shared_ptr
<
core
::
Scene
Node
>
cam_node
=
core
::
state
::
graph_root
->
find
<
core
::
Camera
>
();
if
(
cam_node
)
{
//If there are any cameras in the collada file, take the first one.
...
...
@@ -135,7 +135,7 @@ void loadScene(const fs::path& path, float scale)
else
{
//Add a camera to render from
cam_node
=
std
::
make_shared
<
core
::
Graph
Node
>
(
"custom_camera"
);
cam_node
=
std
::
make_shared
<
core
::
Scene
Node
>
(
"custom_camera"
);
cam_node
->
addComponent
(
core
::
state
::
camera
);
core
::
state
::
graph_root
->
attach
(
cam_node
);
}
...
...
@@ -160,11 +160,11 @@ void drawSceneWindow()
{
ImGui
::
BeginChild
(
"Graph"
,
ImVec2
(
0
,
ImGui
::
GetContentRegionAvail
().
y
*
0.5
f
));
ImGui
::
BeginSelectableTree
(
"tree_scene_graph"
);
core
::
state
::
graph_root
->
g
ui
();
core
::
state
::
graph_root
->
debugG
ui
();
ImGui
::
EndSelectableTree
();
ImGui
::
EndChild
();
ImGui
::
Separator
();
core
::
Graph
Node
*
selected_node
=
reinterpret_cast
<
core
::
Graph
Node
*>
(
ImGui
::
GetSelectableTreeData
(
"tree_scene_graph"
));
core
::
Scene
Node
*
selected_node
=
reinterpret_cast
<
core
::
Scene
Node
*>
(
ImGui
::
GetSelectableTreeData
(
"tree_scene_graph"
));
if
(
selected_node
)
{
ImGui
::
BeginChild
(
"Settings For Tree Node"
);
...
...
@@ -201,7 +201,7 @@ void drawSceneWindow()
if
(
ImGui
::
AntiFeatureButton
(
"Yes"
))
{
if
(
selected_node
)
selected_node
->
de
tachSelf
();
selected_node
->
de
stroy
();
ImGui
::
CloseCurrentPopup
();
}
ImGui
::
SameLine
();
...
...
@@ -222,7 +222,7 @@ void drawSceneWindow()
if
(
ImGui
::
AntiFeatureButton
(
"Attach!"
))
{
if
(
selected_node
)
selected_node
->
attach
(
std
::
make_shared
<
core
::
Graph
Node
>
(
buf
));
selected_node
->
attach
(
std
::
make_shared
<
core
::
Scene
Node
>
(
buf
));
ImGui
::
CloseCurrentPopup
();
}
ImGui
::
SameLine
();
...
...
@@ -297,12 +297,12 @@ void drawSceneWindow()
ImGui
::
DragFloat3
(
"Scale"
,
reinterpret_cast
<
float
*>
(
&
(
selected_node
->
transform
.
scale
)),
0.01
f
);
break
;
case
2
:
// Components (if available)
selected_node
->
iterateComponentsStable
([](
core
::
GraphNod
eComponent
&
component
)
selected_node
->
iterateComponentsStable
([](
core
::
Scen
eComponent
&
component
)
{
ImGui
::
PushID
((
"component-"
+
std
::
to_string
(
component
.
id
())).
c_str
());
if
(
ImGui
::
CollapsingHeader
(
component
.
typeName
().
c_str
()))
{
component
.
on
Gui
();
component
.
debug
Gui
();
if
(
ImGui
::
AntiFeatureButton
(
"Delete"
,
ImVec2
(
ImGui
::
GetContentRegionAvailWidth
(),
24
)))
{
ImGui
::
OpenPopup
(
"del_comp"
);
...
...
@@ -314,7 +314,7 @@ void drawSceneWindow()
if
(
ImGui
::
AntiFeatureButton
(
"Yes"
))
{
component
.
de
tachAs
<
core
::
GraphNodeComponent
>
();
component
.
de
stroy
();
ImGui
::
CloseCurrentPopup
();
}
ImGui
::
SameLine
();
...
...
@@ -339,9 +339,6 @@ void drawSceneWindow()
if
(
ImGui
::
BeginPopup
(
"add_comp"
))
{
ImGui
::
Text
(
"New Component Type?"
);
ImGui
::
BeginChild
(
"component_list"
);
if
(
ImGui
::
Button
(
"Light"
,
ImVec2
(
180
,
24
)))
{
if
(
selected_node
)
...
...
@@ -374,7 +371,6 @@ void drawSceneWindow()
selected_node
->
makeComponent
<
component
::
FramerateCounter
>
();
}
ImGui
::
EndChild
();
if
(
ImGui
::
Button
(
"Cancel"
))
{
ImGui
::
CloseCurrentPopup
();
...
...
src/libraries/components/FramerateCounter.cpp
View file @
e78c388a
...
...
@@ -8,7 +8,7 @@
namespace
glare
::
component
{
FramerateCounter
::
FramerateCounter
()
:
GraphNod
eComponent
(
"Framerate Counter"
)
:
Scen
eComponent
(
"Framerate Counter"
)
{
}
...
...
src/libraries/components/FramerateCounter.h
View file @
e78c388a
...
...
@@ -5,11 +5,12 @@
namespace
glare
::
component
{
class
FramerateCounter
:
public
core
::
GraphNod
eComponent
class
FramerateCounter
:
public
core
::
Scen
eComponent
{
public:
FramerateCounter
();
protected:
void
onUpdate
()
override
;
private:
...
...
src/libraries/components/PlayerController.cpp
View file @
e78c388a
...
...
@@ -12,7 +12,7 @@ namespace glare::component
size_t
PlayerController
::
s_current_id
=
0
;
PlayerController
::
PlayerController
()
:
GraphNod
eComponent
(
"Player Controller"
),
m_id
(
s_current_id
++
)
:
Scen
eComponent
(
"Player Controller"
),
m_id
(
s_current_id
++
)
{
core
::
Callbacks
::
addKeyActionCallback
(
"player_controller"
+
std
::
to_string
(
m_id
),
std
::
bind
(
&
PlayerController
::
onKeyAction
,
this
,
std
::
placeholders
::
_1
,
std
::
placeholders
::
_2
,
std
::
placeholders
::
_3
));
core
::
Callbacks
::
addMouseButtonCallback
(
"player_controller"
+
std
::
to_string
(
m_id
),
std
::
bind
(
&
PlayerController
::
onMouseButtonAction
,
this
,
std
::
placeholders
::
_1
,
std
::
placeholders
::
_2
,
std
::
placeholders
::
_3
));
...
...
@@ -77,7 +77,7 @@ namespace glare::component
}
}
void
PlayerController
::
onGui
()
void
PlayerController
::
on
Debug
Gui
()
{
ImGui
::
DragFloat
(
"Movement Speed"
,
&
m_speed
,
0.5
f
);
ImGui
::
DragFloat
(
"Rotation Speed"
,
&
m_rotation_speed
,
0.01
f
);
...
...
src/libraries/components/PlayerController.h
View file @
e78c388a
...
...
@@ -8,7 +8,7 @@
namespace
glare
::
component
{
class
PlayerController
:
public
core
::
GraphNod
eComponent
,
public
messaging
::
Receiver
class
PlayerController
:
public
core
::
Scen
eComponent
,
public
messaging
::
Receiver
{
public:
PlayerController
();
...
...
@@ -19,12 +19,12 @@ namespace glare::component
void
onKeyAction
(
controls
::
Key
key
,
controls
::
ButtonAction
action
,
controls
::
KeyMods
mods
);
void
onMouseButtonAction
(
controls
::
MouseButton
button
,
controls
::
ButtonAction
action
,
controls
::
KeyMods
mods
);
void
onUpdate
()
override
;
void
onGui
()
override
;
bool
isPossessed
()
const
;
protected:
void
onUpdate
()
override
;
void
onDebugGui
()
override
;
private:
static
size_t
s_current_id
;
size_t
m_id
;
...
...
src/libraries/components/Rotator.cpp
View file @
e78c388a
...
...
@@ -5,7 +5,7 @@ namespace glare::component
{
Rotator
::
Rotator
()
:
GraphNod
eComponent
(
"Rotator"
)
:
Scen
eComponent
(
"Rotator"
)
{
}
...
...
src/libraries/components/Rotator.h
View file @
e78c388a
...
...
@@ -5,12 +5,13 @@
namespace
glare
::
component
{
class
Rotator
:
public
core
::
GraphNod
eComponent
class
Rotator
:
public
core
::
Scen
eComponent
{
public:
Rotator
();
~
Rotator
();
protected:
void
onUpdate
()
override
;
};
...
...
src/libraries/core/audio/sound_source.cpp
View file @
e78c388a
...
...
@@ -3,7 +3,7 @@
namespace
glare
::
core
{
SoundSource
::
SoundSource
()
:
GraphNod
eComponent
(
"Sound Source"
)
:
Scen
eComponent
(
"Sound Source"
)
{
al
::
sourcef
(
m_source
,
al
::
SourceParam
::
ePitch
,
m_pitch
);
al
::
sourcef
(
m_source
,
al
::
SourceParam
::
eGain
,
m_gain
);
...
...
src/libraries/core/audio/sound_source.h
View file @
e78c388a
...
...
@@ -9,17 +9,12 @@ namespace glare::core
/**
* \brief Can be attached to a scene graph to either play a sound locally or globally. Uses the id of a SoundBuffer to play sounds.
*/
class
SoundSource
:
public
GraphNod
eComponent
class
SoundSource
:
public
Scen
eComponent
{
public:
SoundSource
();
SoundSource
(
unsigned
buffer
);
/**
* \brief Important for local sound sources: Updates the position and velocity of this sound source.
*/
void
onUpdate
()
override
;
void
play
()
const
;
void
pause
()
const
;
void
stop
()
const
;
...
...
@@ -29,6 +24,12 @@ namespace glare::core
void
setBuffer
(
unsigned
buffer
);
unsigned
getBuffer
()
const
;
protected:
/**
* \brief Important for local sound sources: Updates the position and velocity of this sound source.
*/
void
onUpdate
()
override
;
private:
al
::
handle
::
source
m_source
;
unsigned
m_buffer_id
=
0
;
...
...
src/libraries/core/graph/base_container.h
View file @
e78c388a
#ifndef INCLUDE_BASE_CONTAINER_H
#define INCLUDE_BASE_CONTAINER_H
#include
<algorithm>
#include
<functional>
#include
<memory>
#include
<map>
...
...
@@ -70,6 +71,8 @@ namespace glare::core
void
ComponentContainer
<
TNode
,
TComponent
>::
addComponent
(
std
::
shared_ptr
<
T
>
component
)
{
component
->
m_owner
=
std
::
enable_shared_from_this
<
TNode
>::
shared_from_this
();
if
(
component
)
component
->
start
();
m_components
[
typeid
(
T
).
name
()].
push_back
(
component
);
}
...
...
@@ -80,10 +83,15 @@ namespace glare::core
if
(
m_components
.
size
()
!=
0
&&
m_components
.
count
(
typeid
(
T
).
name
())
!=
0
)
{
auto
&&
list
=
m_components
[
typeid
(
T
).
name
()];
list
.
erase
(
std
::
remove_if
(
list
.
begin
(),
list
.
end
(),
[
&
component
](
std
::
shared_ptr
<
TComponent
>
current
)
{
auto
rm_it
=
std
::
remove_if
(
list
.
begin
(),
list
.
end
(),
[
&
component
](
std
::
shared_ptr
<
TComponent
>
current
)
{
return
current
.
get
()
==
component
.
get
();
}));
});
if
(
rm_it
!=
list
.
end
())
{
(
*
rm_it
)
->
end
();
list
.
erase
(
rm_it
);
}
if
(
list
.
empty
())
m_components
.
erase
(
typeid
(
T
).
name
());
...
...
src/libraries/core/graph/base_node.h
View file @
e78c388a
...
...
@@ -29,10 +29,6 @@ namespace glare::core
void
clearChildren
();
// Those can be overridden in case you want to be notified when adding/removing nodes.
virtual
void
onNodeAttached
(
std
::
shared_ptr
<
NodeType
>
node
);
virtual
void
onNodeDetached
(
std
::
shared_ptr
<
NodeType
>
node
);
std
::
shared_ptr
<
NodeType
>
parent
()
const
;
const
std
::
vector
<
std
::
shared_ptr
<
NodeType
>>&
children
();
...
...
@@ -73,6 +69,10 @@ namespace glare::core
StableChildIterator
iterateChildren
();
protected:
// Those can be overridden in case you want to be notified when adding/removing nodes.
virtual
void
onAttached
(
std
::
shared_ptr
<
NodeType
>
node
);
virtual
void
onDetached
(
std
::
shared_ptr
<
NodeType
>
node
);
std
::
weak_ptr
<
NodeType
>
m_parent
;
std
::
vector
<
std
::
shared_ptr
<
NodeType
>>
m_children
;
};
...
...
@@ -131,7 +131,7 @@ namespace glare::core
{
node
->
m_parent
=
std
::
enable_shared_from_this
<
NodeType
>::
shared_from_this
();
m_children
.
push_back
(
node
);
on
Node
Attached
(
node
);
onAttached
(
node
);
return
int
(
m_children
.
size
()
-
1
);
}
...
...
@@ -144,7 +144,7 @@ namespace glare::core
{
if
(
!
node
->
m_parent
.
expired
())
node
->
m_parent
.
reset
();
on
Node
Detached
(
node
);
onDetached
(
node
);
m_children
.
erase
(
it
);
}
}
...
...
@@ -164,7 +164,7 @@ namespace glare::core
{
if
(
!
node
->
m_parent
.
expired
())
node
->
m_parent
.
reset
();
on
Node
Detached
(
node
);
onDetached
(
node
);
}
m_children
.
clear
();
}
...
...
@@ -194,12 +194,12 @@ namespace glare::core
}
template
<
typename
NodeType
,
typename
ComponentType
>
void
Node
<
NodeType
,
ComponentType
>::
on
Node
Attached
(
std
::
shared_ptr
<
NodeType
>
node
)
void
Node
<
NodeType
,
ComponentType
>::
onAttached
(
std
::
shared_ptr
<
NodeType
>
node
)
{
}
template
<
typename
NodeType
,
typename
ComponentType
>
void
Node
<
NodeType
,
ComponentType
>::
on
Node
Detached
(
std
::
shared_ptr
<
NodeType
>
node
)
void
Node
<
NodeType
,
ComponentType
>::
onDetached
(
std
::
shared_ptr
<
NodeType
>
node
)
{
}
...
...
@@ -276,7 +276,7 @@ namespace glare::core
template
<
typename
TComponent
>
std
::
shared_ptr
<
NodeType
>
Node
<
NodeType
,
ComponentType
>::
find
()
{
return
find
([](
const
Graph
Node
&
node
)
return
find
([](
const
Scene
Node
&
node
)
{
return
bool
(
node
.
getComponent
<
TComponent
>
());
});
...
...
src/libraries/core/graph/component.h
View file @
e78c388a
...
...
@@ -3,83 +3,92 @@
#include
<memory>
#include
<string>
#include
"entity.h"
namespace
glare
::
core
{
template
<
typename
TNode
,
typename
TComponent
>
class
ComponentContainer
;
class
Graph
Node
;
class
Scene
Node
;
enum
class
DrawMode
;
/**
* \brief Can be attached to a GraphNode to add functionality to it (decorator)
*/
class
GraphNod
eComponent
:
public
std
::
enable_shared_from_this
<
GraphNod
eComponent
>
class
Scen
eComponent
:
public
std
::
enable_shared_from_this
<
Scen
eComponent
>
,
public
Entity
{
friend
class
ComponentContainer
<
Graph
Node
,
GraphNod
eComponent
>
;
friend
class
ComponentContainer
<
Scene
Node
,
Scen
eComponent
>
;
friend
class
GraphNode
;
public:
GraphNod
eComponent
(
const
std
::
string
&
type_name
)
:
m_type_name
(
type_name
)
{}
virtual
~
GraphNod
eComponent
();
Scen
eComponent
(
const
std
::
string
&
type_name
)
:
m_type_name
(
type_name
)
{}
virtual
~
Scen
eComponent
();
template
<
typename
T
>
std
::
shared_ptr
<
T
>
detachAs
();
std
::
shared_ptr
<
SceneNode
>
owner
()
const
;
uint64_t
id
();
const
std
::
string
&
typeName
()
const
;
protected:
/**
* \brief Will get called when the parent node transformation has changed.
*/
virtual
void
onTransform
();
virtual
void
onTransform
()
override
;
/**
* \brief Can be used to draw the component settings via ImGui.
*/
virtual
void
onGui
();
virtual
void
onUpdate
();
virtual
void
onDraw
(
DrawMode
mode
);
virtual
void
onGui
()
override
;
virtual
void
onUpdate
()
override
;
virtual
void
onDestroy
()
override
{
detachAs
<
SceneComponent
>
();
};
virtual
void
onStart
()
override
{}
virtual
void
onEnd
()
override
{}
virtual
void
onActivate
()
override
{}
virtual
void
onDeactivate
()
override
{}
template
<
typename
T
>
std
::
shared_ptr
<
T
>
detachAs
();
std
::
shared_ptr
<
GraphNode
>
owner
()
const
;
uint64_t
id
();
const
std
::
string
&
typeName
()
const
;
virtual
void
onDebugGui
()
override
{}
virtual
void
onDraw
(
DrawMode
mode
=
DrawMode
::
eShaded
)
override
;
private:
std
::
string
m_type_name
;
std
::
weak_ptr
<
Graph
Node
>
m_owner
;
std
::
weak_ptr
<
Scene
Node
>
m_owner
;
uint64_t
m_id
=
0
;
};
inline
GraphNod
eComponent
::~
GraphNod
eComponent
()
inline
Scen
eComponent
::~
Scen
eComponent
()
{
}
inline
void
GraphNod
eComponent
::
onTransform
()
inline
void
Scen
eComponent
::
onTransform
()
{
}
inline
void
GraphNod
eComponent
::
onGui
()
inline
void
Scen
eComponent
::
onGui
()
{
}
inline
void
GraphNod
eComponent
::
onUpdate
()
inline
void
Scen
eComponent
::
onUpdate
()
{
}
inline
void
GraphNod
eComponent
::
onDraw
(
DrawMode
mode
)
inline
void
Scen
eComponent
::
onDraw
(
DrawMode
mode
)
{
}
inline
std
::
shared_ptr
<
Graph
Node
>
GraphNod
eComponent
::
owner
()
const
inline
std
::
shared_ptr
<
Scene
Node
>
Scen
eComponent
::
owner
()
const
{
return
m_owner
.
lock
();
}
template
<
typename
T
>
std
::
shared_ptr
<
T
>
GraphNod
eComponent
::
detachAs
()
std
::
shared_ptr
<
T
>
Scen
eComponent
::
detachAs
()
{
auto
shared
=
std
::
static_pointer_cast
<
T
>
(
shared_from_this
());
m_owner
.
lock
()
->
removeComponent
(
shared
);
return
shared
;
}
inline
uint64_t
GraphNod
eComponent
::
id
()
inline
uint64_t
Scen
eComponent
::
id
()
{
if
(
m_id
==
0
)
{
...
...
@@ -89,12 +98,12 @@ namespace glare::core
return
m_id
;
}
inline
const
std
::
string
&
GraphNod
eComponent
::
typeName
()
const
inline
const
std
::
string
&
Scen
eComponent
::
typeName
()
const
{
return
m_type_name
;
}
}
#include
"node.h"
#include
"
scene_
node.h"
#endif //INCLUDE_COMPONENT_H
src/libraries/core/graph/entity.cpp
0 → 100644
View file @
e78c388a
#include
"entity.h"
namespace
glare
::
core
{
void
Entity
::
start
()
{
onStart
();
setActive
(
true
);
}
void
Entity
::
end
()
{
setActive
(
false
);
onEnd
();
}
void
Entity
::
gui
()
{
onGui
();
}
void
Entity
::
debugGui
()
{
onDebugGui
();
}
void
Entity
::
update
()
{
onUpdate
();
}
void
Entity
::
draw
(
DrawMode
mode
)
{
onDraw
(
mode
);
}
void
Entity
::
setActive
(
bool
active
)
{
if
(
active
!=
m_active
)
{
m_active
=
active
;
m_active
?
onActivate
()
:
onDeactivate
();
}
}
bool
Entity
::
isActive
()
const
{
return
m_active
;
}
void
Entity
::
destroy
()
{
onDestroy
();
end
();
}
void
Entity
::
notifyTransform
()
{
onTransform
();
}
}
\ No newline at end of file
src/libraries/core/graph/entity.h
0 → 100644
View file @
e78c388a
#ifndef INCLUDE_INTERACTABLE_H
#define INCLUDE_INTERACTABLE_H
#include
<string>
namespace
glare
::
core
{
enum
class
DrawMode
{
eShaded
=
0
,
eUnshaded
};
class
Entity
{
public:
void
start
();
void
end
();
void
gui
();
void
debugGui
();
void
update
();
void
draw
(
DrawMode
mode
=
DrawMode
::
eShaded
);
void
setActive
(
bool
active
);
bool
isActive
()
const
;
void
destroy
();
void
notifyTransform
();
private:
virtual
void
onDestroy
()
=
0
;
virtual
void
onStart
()
{}
virtual
void
onEnd
()
{}
virtual
void
onActivate
()
{}
virtual
void
onDeactivate
()
{}
virtual
void
onGui
()
{}
virtual
void
onDebugGui
()
{}
virtual
void
onUpdate
()
{}
virtual
void
onDraw
(
DrawMode
mode
=
DrawMode
::
eShaded
)
{}
virtual
void
onTransform
()
{}
private:
bool
m_active
;
};
}
#endif //!INCLUDE_INTERACTABLE_H
\ No newline at end of file
src/libraries/core/graph/interactable.h
deleted
100644 → 0
View file @
d6eff563
#ifndef INCLUDE_INTERACTABLE_H
#define INCLUDE_INTERACTABLE_H
namespace
glare
::
core
{
enum
class
DrawMode
;
class
Interactable
{
public:
void
onGui
();
void
onUpdate
();
void
onDraw
(
DrawMode
mode
);
/**
* \brief Will get called when this node's transformation has changed.
*/
void
onTransfform
();
};
}
#endif //!INCLUDE_INTERACTABLE_H
\ No newline at end of file
src/libraries/core/graph/node.cpp
→
src/libraries/core/graph/
scene_
node.cpp
View file @
e78c388a
#include
"node.h"
#include
<core/message_tags.h>
#include
<imgui/imgui_glfw.h>
namespace
glare
::
core
{
GraphNode
::
GraphNode
(
std
::
string
name
)
:
m_name
(
name
)
{
}
void
GraphNode
::
gui
()
{
ImGui
::
PushID
((
"Node::"
+
m_name
).
c_str
());