Skip to content

Commit 3ff8f46

Browse files
committed
gh-14: rework console mng
1 parent 941ead7 commit 3ff8f46

File tree

20 files changed

+368
-639
lines changed

20 files changed

+368
-639
lines changed

engine/code/asset/asset_meta.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace wmoge {
5050
};
5151

5252
/**
53-
* @class AssetDataDesc
53+
* @class AssetDataMeta
5454
* @brief Describes asset associated data stored in asset system
5555
*/
5656
struct AssetDataMeta {
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@
2525
/* SOFTWARE. */
2626
/**********************************************************************************/
2727

28-
#include "console_storage.hpp"
28+
#include "_rtti.hpp"
29+
30+
#include "console/console_objects.hpp"
2931

3032
namespace wmoge {
3133

34+
void rtti_console() {
35+
rtti_type<ConsoleObject>();
36+
rtti_type<ConsoleVar>();
37+
rtti_type<ConsoleCmd>();
38+
rtti_type<ConsoleTrigger>();
39+
}
40+
3241
}// namespace wmoge
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@
2929

3030
namespace wmoge {
3131

32+
void rtti_console();
33+
3234
}// namespace wmoge
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**********************************************************************************/
2+
/* Wmoge game engine */
3+
/* Available at github https://github.com/EgorOrachyov/wmoge */
4+
/**********************************************************************************/
5+
/* MIT License */
6+
/* */
7+
/* Copyright (c) 2023 Egor Orachyov */
8+
/* */
9+
/* Permission is hereby granted, free of charge, to any person obtaining a copy */
10+
/* of this software and associated documentation files (the "Software"), to deal */
11+
/* in the Software without restriction, including without limitation the rights */
12+
/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13+
/* copies of the Software, and to permit persons to whom the Software is */
14+
/* furnished to do so, subject to the following conditions: */
15+
/* */
16+
/* The above copyright notice and this permission notice shall be included in all */
17+
/* copies or substantial portions of the Software. */
18+
/* */
19+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20+
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21+
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22+
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23+
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24+
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25+
/* SOFTWARE. */
26+
/**********************************************************************************/
27+
28+
#include "console_manager.hpp"
29+
30+
namespace wmoge {
31+
32+
void ConsoleManager::add_object(Ref<ConsoleObject> object) {
33+
if (has_object(object->get_name())) {
34+
WG_LOG_ERROR("duplicated object registration " << object->get_name());
35+
return;
36+
}
37+
38+
m_objects[object->get_name()] = std::move(object);
39+
}
40+
41+
Status ConsoleManager::set_var(Strid name, Var value) {
42+
auto obj = try_find_object(name);
43+
ConsoleVar* var = dynamic_cast<ConsoleVar*>(obj.get());
44+
45+
if (!var) {
46+
WG_LOG_ERROR("faield to find and cast var " << name);
47+
return StatusCode::InvalidParameter;
48+
}
49+
50+
if (var->get_value_type() != value.type()) {
51+
WG_LOG_ERROR("mismatched types of var to set " << name);
52+
return StatusCode::InvalidParameter;
53+
}
54+
55+
var->m_value = std::move(value);
56+
57+
return WG_OK;
58+
}
59+
60+
Status ConsoleManager::set_trigger(Strid name, bool value) {
61+
auto obj = try_find_object(name);
62+
ConsoleTrigger* trigger = dynamic_cast<ConsoleTrigger*>(obj.get());
63+
64+
if (!trigger) {
65+
WG_LOG_ERROR("faield to find and cast trigger " << name);
66+
return StatusCode::InvalidParameter;
67+
}
68+
69+
if (!trigger->m_triggered && value) {
70+
m_triggered.push_back(std::move(obj.cast<ConsoleTrigger>()));
71+
}
72+
73+
trigger->m_triggered = value;
74+
75+
return WG_OK;
76+
}
77+
78+
Status ConsoleManager::exec_command(Strid name, array_view<std::string> args) {
79+
auto obj = try_find_object(name);
80+
ConsoleCmd* cmd = dynamic_cast<ConsoleCmd*>(obj.get());
81+
82+
if (!cmd) {
83+
WG_LOG_ERROR("faield to find and cast cmd " << name);
84+
return StatusCode::InvalidParameter;
85+
}
86+
87+
return cmd->m_on_execute(args);
88+
}
89+
90+
Ref<ConsoleObject> ConsoleManager::try_find_object(Strid name) {
91+
auto query = m_objects.find(name);
92+
if (query == m_objects.end()) {
93+
return {};
94+
}
95+
return query->second;
96+
}
97+
98+
bool ConsoleManager::has_object(Strid name) {
99+
auto query = m_objects.find(name);
100+
return query != m_objects.end();
101+
}
102+
103+
void ConsoleManager::update() {
104+
for (const auto& trigger : m_triggered) {
105+
trigger->m_triggered = false;
106+
}
107+
m_triggered.clear();
108+
}
109+
110+
}// namespace wmoge
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**********************************************************************************/
2+
/* Wmoge game engine */
3+
/* Available at github https://github.com/EgorOrachyov/wmoge */
4+
/**********************************************************************************/
5+
/* MIT License */
6+
/* */
7+
/* Copyright (c) 2023 Egor Orachyov */
8+
/* */
9+
/* Permission is hereby granted, free of charge, to any person obtaining a copy */
10+
/* of this software and associated documentation files (the "Software"), to deal */
11+
/* in the Software without restriction, including without limitation the rights */
12+
/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13+
/* copies of the Software, and to permit persons to whom the Software is */
14+
/* furnished to do so, subject to the following conditions: */
15+
/* */
16+
/* The above copyright notice and this permission notice shall be included in all */
17+
/* copies or substantial portions of the Software. */
18+
/* */
19+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20+
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21+
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22+
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23+
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24+
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25+
/* SOFTWARE. */
26+
/**********************************************************************************/
27+
28+
#pragma once
29+
30+
#include "console/console_objects.hpp"
31+
#include "core/flat_map.hpp"
32+
33+
#include <vector>
34+
35+
namespace wmoge {
36+
37+
/**
38+
* @brief ConsoleManager
39+
* @class Stores and processes all console objects
40+
*/
41+
class ConsoleManager {
42+
public:
43+
void add_object(Ref<ConsoleObject> object);
44+
Status set_var(Strid name, Var value);
45+
Status set_trigger(Strid name, bool value);
46+
Status exec_command(Strid name, array_view<std::string> args);
47+
Ref<ConsoleObject> try_find_object(Strid name);
48+
bool has_object(Strid name);
49+
void update();
50+
51+
private:
52+
flat_map<Strid, Ref<ConsoleObject>> m_objects;
53+
std::vector<Ref<ConsoleTrigger>> m_triggered;
54+
};
55+
56+
}// namespace wmoge

engine/code/console/console_objects.hpp

Lines changed: 109 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,60 +27,138 @@
2727

2828
#pragma once
2929

30+
#include "core/array_view.hpp"
31+
#include "core/ref.hpp"
3032
#include "core/string_id.hpp"
3133
#include "core/var.hpp"
34+
#include "rtti/traits.hpp"
3235

3336
#include <functional>
3437
#include <string>
3538
#include <vector>
3639

3740
namespace wmoge {
3841

39-
enum class ConsoleObjType {
40-
Var, // Console variable holding single value
41-
Cmd, // Console command which can execute custom code with arguments
42-
Trigger,// Console trigger which can be triggered once in frame
43-
Slider, // Console slider for range of value with step movement
44-
Selector// Console selector to chose from pre-defined set of value
45-
};
46-
47-
class ConsoleObj {
42+
/**
43+
* @class ConsoleObject
44+
* @brief Base class for any console object
45+
*/
46+
class ConsoleObject : public RttiObject {
4847
public:
49-
private:
50-
std::string m_name;
48+
WG_RTTI_CLASS(ConsoleObject, RttiObject)
49+
50+
ConsoleObject() = default;
51+
52+
ConsoleObject(Strid name, std::string help)
53+
: m_name(name), m_help(std::move(help)) {
54+
}
55+
56+
[[nodiscard]] const Strid& get_name() const { return m_name; }
57+
[[nodiscard]] const std::string& get_help() const { return m_help; }
58+
59+
protected:
60+
friend class ConsoleManager;
61+
62+
Strid m_name;
5163
std::string m_help;
52-
std::string m_id;
5364
};
5465

55-
class ConsoleVar {
66+
WG_RTTI_CLASS_BEGIN(ConsoleObject) {
67+
WG_RTTI_FIELD(m_name, {});
68+
WG_RTTI_FIELD(m_help, {});
69+
}
70+
WG_RTTI_END;
71+
72+
/**
73+
* @class ConsoleVar
74+
* @brief Console var which can hold some value
75+
*/
76+
class ConsoleVar : public ConsoleObject {
5677
public:
57-
private:
78+
WG_RTTI_CLASS(ConsoleVar, ConsoleObject)
79+
80+
ConsoleVar() = default;
81+
82+
ConsoleVar(Strid name, std::string help, Var value)
83+
: ConsoleObject(name, std::move(help)),
84+
m_value(value),
85+
m_defaul_value(value) {
86+
}
87+
88+
[[nodiscard]] const Var& get_value() const { return m_value; }
89+
[[nodiscard]] const Var& get_defaul_value() const { return m_defaul_value; }
90+
[[nodiscard]] VarType get_value_type() const { return m_defaul_value.type(); }
91+
92+
protected:
93+
friend class ConsoleManager;
94+
5895
Var m_value;
96+
Var m_defaul_value;
5997
};
6098

61-
class ConsoleCmd {
62-
public:
63-
private:
64-
};
99+
WG_RTTI_CLASS_BEGIN(ConsoleVar) {
100+
WG_RTTI_FACTORY();
101+
WG_RTTI_FIELD(m_value, {});
102+
WG_RTTI_FIELD(m_defaul_value, {});
103+
}
104+
WG_RTTI_END;
65105

66-
class ConsoleTrigger {
106+
/**
107+
* @class ConsoleCmd
108+
* @brief Console command which can be executed from console
109+
*/
110+
class ConsoleCmd : public ConsoleObject {
67111
public:
68-
private:
69-
};
112+
WG_RTTI_CLASS(ConsoleCmd, ConsoleObject)
70113

71-
class ConsoleSlider {
72-
public:
73-
private:
74-
Var m_min;
75-
Var m_max;
76-
Var m_step;
77-
Var m_value;
114+
using OnExecute = std::function<Status(array_view<std::string> args)>;
115+
116+
ConsoleCmd() = default;
117+
118+
ConsoleCmd(Strid name, std::string help, OnExecute on_execute)
119+
: ConsoleObject(name, std::move(help)),
120+
m_on_execute(std::move(on_execute)) {
121+
}
122+
123+
[[nodiscard]] const OnExecute& get_on_execute() const { return m_on_execute; }
124+
125+
protected:
126+
friend class ConsoleManager;
127+
128+
OnExecute m_on_execute;
78129
};
79130

80-
class ConsoleSelector {
131+
WG_RTTI_CLASS_BEGIN(ConsoleCmd) {
132+
WG_RTTI_FACTORY();
133+
}
134+
WG_RTTI_END;
135+
136+
/**
137+
* @class ConsoleTrigger
138+
* @brief Console trigger which can be triggered for one frame
139+
*/
140+
class ConsoleTrigger : public ConsoleObject {
81141
public:
82-
private:
83-
std::vector<Var> m_variants;
142+
WG_RTTI_CLASS(ConsoleTrigger, ConsoleObject)
143+
144+
ConsoleTrigger() = default;
145+
146+
ConsoleTrigger(Strid name, std::string help)
147+
: ConsoleObject(name, std::move(help)) {
148+
}
149+
150+
[[nodiscard]] bool is_triggered() const { return m_triggered; }
151+
152+
protected:
153+
friend class ConsoleManager;
154+
155+
bool m_triggered = false;
84156
};
85157

158+
WG_RTTI_CLASS_BEGIN(ConsoleTrigger) {
159+
WG_RTTI_FACTORY();
160+
WG_RTTI_FIELD(m_triggered, {});
161+
}
162+
WG_RTTI_END;
163+
86164
}// namespace wmoge

engine/code/core/date_time.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
namespace wmoge {
4242

4343
/**
44-
* @brief DateTimeTm
45-
* @class Decomposed time struct
44+
* @class DateTimeTm
45+
* @brief Decomposed time struct
4646
*/
4747
struct DateTimeTm {
4848
int year = 1900;

0 commit comments

Comments
 (0)