neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
app.hpp
Go to the documentation of this file.
1// app.hpp
2/*
3 neogfx C++ App/Game Engine
4 Copyright (c) 2015, 2020 Leigh Johnston. All Rights Reserved.
5
6 This program is free software: you can redistribute it and / or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#pragma once
21
22#include <neogfx/neogfx.hpp>
23#include <map>
24#include <optional>
25#include <boost/pool/pool_alloc.hpp>
26#include <neolib/core/map.hpp>
32#include <neogfx/app/i_app.hpp>
33#include <neogfx/app/style.hpp>
34#include <neogfx/app/action.hpp>
36#include <neogfx/app/i_help.hpp>
37
38#ifdef _WIN32
39#pragma comment(linker, "/include:nrc_neogfx_icons")
40#pragma comment(linker, "/include:nrc_neogfx_resources")
41#endif
42
43namespace neogfx
44{
45 class app_thread : public async_thread
46 {
47 friend class app;
48 public:
50 private:
52 };
53
55 {
56 struct invalid_options : std::runtime_error { invalid_options(std::string const& aReason) : std::runtime_error("Invalid program options: " + aReason) {} };
57 public:
58 program_options(int argc, char* argv[]);
59 public:
60 const boost::program_options::variables_map& options() const final;
61 bool console() const final;
62 neogfx::renderer renderer() const final;
65 bool turbo() const final;
66 bool nest() const final;
67 private:
68 boost::program_options::variables_map iOptions;
69 };
70
71 class app : public neolib::application<object<i_app>>, private i_keyboard_handler
72 {
74 public:
75 define_declared_event(ExecutionStarted, execution_started)
76 define_declared_event(NameChanged, name_changed)
77 define_declared_event(CurrentStyleChanged, current_style_changed, style_aspect)
78 private:
79 class loader
80 {
81 public:
82 loader(const neogfx::program_options& aProgramOptions, app& aApp);
83 ~loader();
84 private:
85 app& iApp;
86 };
87 private:
88 typedef std::map<std::string, style> style_list;
90 typedef std::vector<i_mnemonic*> mnemonic_list;
91 public:
92 struct no_instance : std::logic_error { no_instance() : std::logic_error("neogfx::app::no_instance") {} };
93 struct action_not_found : std::runtime_error { action_not_found() : std::runtime_error("neogfx::app::action_not_found") {} };
94 struct style_not_found : std::runtime_error { style_not_found() : std::runtime_error("neogfx::app::style_not_found") {} };
95 struct style_exists : std::runtime_error { style_exists() : std::runtime_error("neogfx::app::style_exists") {} };
96 public:
98 template <typename... Args>
99 app(Args&&... aArgs) :
100 app{ static_cast<const neolib::i_application_info&>(neolib::application_info{ std::forward<Args>(aArgs)... }) } {}
102 public:
103 static app& instance();
104 public:
105 app_thread& thread() const noexcept;;
106 public:
107 const i_program_options& program_options() const noexcept override;
108 std::string const& name() const noexcept override;
109 void set_name(std::string const& aName) override;
110 int exec(bool aQuitWhenLastWindowClosed = true) override;
111 bool in_exec() const override;
112 void quit(int aResultCode = 0) override;
113 public:
116 public:
117 const i_texture& default_window_icon() const override;
118 void set_default_window_icon(const i_texture& aIcon) override;
119 void set_default_window_icon(const i_image& aIcon) override;
120 const i_style& current_style() const override;
121 i_style& current_style() override;
122 i_style& change_style(std::string const& aStyleName) override;
123 i_style& register_style(const i_style& aStyle) override;
124 public:
125 void clear_translations();
126 void load_translations();
127 void load_translations(std::filesystem::path const& aTranslationFile);
128 i_string const& translate(i_string const& aTranslatableString, i_string const& aContext = string{}, std::int64_t aPlurality = 1) const override;
129 public:
139 i_action& action_cut() override;
144 i_action& add_action(i_action& aAction) override;
145 i_action& add_action(i_ref_ptr<i_action> const& aAction) override;
146 i_action& add_action(i_string const& aText) override;
147 i_action& add_action(i_string const& aText, i_string const& aImageUri, dimension aDpiScaleFactor = 1.0, texture_sampling aSampling = texture_sampling::Normal) override;
148 i_action& add_action(i_string const& aText, const i_texture& aImage) override;
149 i_action& add_action(i_string const& aText, const i_image& aImage) override;
150 void remove_action(i_action& aAction) override;
151 i_action& find_action(i_string const& aText) override;
152 void add_mnemonic(i_mnemonic& aMnemonic) override;
153 void remove_mnemonic(i_mnemonic& aMnemonic) override;
154 public:
155 i_menu& add_standard_menu(i_menu& aParentMenu, standard_menu aStandardMenu) override;
156 public:
157 i_help& help() const override;
158 public:
159 bool process_events() override;
162 public:
163 bool discover(const uuid& aId, void*& aObject) override;
164 private:
165 bool do_process_events();
166 private:
167 bool key_pressed(scan_code_e aScanCode, key_code_e aKeyCode, key_modifiers_e aKeyModifiers) override;
168 bool key_released(scan_code_e aScanCode, key_code_e aKeyCode, key_modifiers_e aKeyModifiers) override;
169 bool text_input(i_string const& aText) override;
170 bool sys_text_input(i_string const& aText) override;
171 private:
172 std::unique_ptr<app_thread> iThread;
173 neogfx::program_options iProgramOptions;
174 std::unique_ptr<loader> iLoader;
175 std::string iName;
176 bool iQuitWhenLastWindowClosed;
177 bool iInExec;
178 std::optional<int> iQuitResultCode;
179 texture iDefaultWindowIcon;
180 style_list iStyles;
181 style_list::iterator iCurrentStyle;
182 action_list iActions;
183 neolib::callback_timer iStandardActionManager;
184 mnemonic_list iMnemonics;
186 std::vector<std::pair<key_code_e, key_modifiers_e>> iKeySequence;
187 mutable std::unique_ptr<i_help> iHelp;
188 std::map<std::string, std::map<std::string, std::map<std::pair<std::int64_t, std::int64_t>, string>>> iTranslations;
189 // standard actions
190 public:
205 };
206}
i_action & add_action(i_action &aAction) override
void remove_mnemonic(i_mnemonic &aMnemonic) override
action actionDelete
Definition app.hpp:203
action actionRedo
Definition app.hpp:199
i_action & action_cut() override
action actionUndo
Definition app.hpp:198
action actionFileSave
Definition app.hpp:195
i_action & add_action(i_string const &aText) override
i_action & find_action(i_string const &aText) override
action actionFileExit
Definition app.hpp:197
i_action & action_paste() override
i_action & action_select_all() override
bool process_events() override
i_action & add_action(i_string const &aText, const i_image &aImage) override
bool process_events(i_event_processing_context &aContext) override
i_help & help() const override
i_action & action_file_close() override
i_action & action_delete() override
action actionFileClose
Definition app.hpp:193
app(Args &&... aArgs)
Definition app.hpp:99
action actionFileNew
Definition app.hpp:191
i_action & action_file_exit() override
action actionFileOpen
Definition app.hpp:192
static app & instance()
void add_mnemonic(i_mnemonic &aMnemonic) override
action actionCopy
Definition app.hpp:201
i_action & action_file_save_all() override
i_action & add_action(i_string const &aText, i_string const &aImageUri, dimension aDpiScaleFactor=1.0, texture_sampling aSampling=texture_sampling::Normal) override
action actionFileSaveAll
Definition app.hpp:196
bool discover(const uuid &aId, void *&aObject) override
action actionPaste
Definition app.hpp:202
i_menu & add_standard_menu(i_menu &aParentMenu, standard_menu aStandardMenu) override
i_action & action_redo() override
i_action & action_file_save() override
i_action & action_undo() override
action actionCut
Definition app.hpp:200
void remove_action(i_action &aAction) override
action actionSelectAll
Definition app.hpp:204
app(const neolib::i_application_info &aAppInfo)
i_action & action_file_close_all() override
i_action & add_action(i_string const &aText, const i_texture &aImage) override
app_thread & thread() const noexcept
action actionFileCloseAll
Definition app.hpp:194
i_action & add_action(i_ref_ptr< i_action > const &aAction) override
i_action & action_copy() override
i_action & action_file_new() override
i_event_processing_context & event_processing_context() override
i_action & action_file_open() override
bool console() const final
bool turbo() const final
const boost::program_options::variables_map & options() const final
program_options(int argc, char *argv[])
std::optional< size_u32 > full_screen() const final
std::optional< size_u32 > dpi_override() const final
friend class async_thread
bool do_work(yield_type aYieldType=yield_type::NoYield) override
standard_menu
Definition i_app.hpp:45
style_aspect
Definition i_style.hpp:31
default_geometry_value_type dimension
translatable_string translate(string const &aTranslatableString)
basic_size< uint32_t > size_u32
dimension x2_dpi_scale_factor(dimension aPpi)
dimension xn_dpi_scale_factor(dimension aPpi)
texture_sampling
Definition i_texture.hpp:42
Definition plf_hive.h:79
#define meta_object(...)
Definition i_object.hpp:28
#define define_declared_event(name, declName,...)
Definition event.hpp:195