The Battle for Wesnoth
1.21.0-dev
Loading...
Searching...
No Matches
editor
editor_main.cpp
Go to the documentation of this file.
1
/*
2
Copyright (C) 2008 - 2025
3
by Tomasz Sniatowski <kailoran@gmail.com>
4
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
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 2 of the License, or
9
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY.
12
13
See the COPYING file for more details.
14
*/
15
16
#define GETTEXT_DOMAIN "wesnoth-editor"
17
18
#include "
editor/controller/editor_controller.hpp
"
19
20
#include "
addon/validation.hpp
"
21
#include "
gettext.hpp
"
22
#include "
gui/dialogs/editor/choose_addon.hpp
"
23
#include "
gui/dialogs/prompt.hpp
"
24
#include "
filesystem.hpp
"
25
#include "
editor/action/action_base.hpp
"
26
#include "
serialization/chrono.hpp
"
27
28
lg::log_domain
log_editor
(
"editor"
);
29
30
namespace
editor
{
31
32
std::string
initialize_addon
()
33
{
34
std::string addon_id =
""
;
35
while
(
true
)
36
{
37
gui2::dialogs::editor_choose_addon
choose(addon_id);
38
if
(choose.
show
()) {
39
break
;
40
}
else
{
41
return
""
;
42
}
43
}
44
45
if
(addon_id ==
"///newaddon///"
) {
46
static
constexpr
std::string_view ts_format =
"%Y-%m-%d_%H-%M-%S"
;
47
std::string
timestamp
=
chrono::format_local_timestamp
(std::chrono::system_clock::now(), ts_format);
48
49
addon_id =
"MyAwesomeAddon-"
+
timestamp
;
50
std::string addon_id_new = addon_id;
51
52
if
(gui2::dialogs::prompt::execute(addon_id_new)) {
53
if
(
addon_filename_legal
(addon_id_new)) {
54
addon_id = addon_id_new;
55
}
56
}
57
}
58
59
if
(addon_id ==
"mainline"
) {
60
return
addon_id;
61
}
62
63
std::string addon_dir =
filesystem::get_addons_dir
() +
"/"
+ addon_id;
64
65
if
(
filesystem::file_exists
(addon_dir)) {
66
return
addon_id;
67
}
68
69
// create folders
70
filesystem::create_directory_if_missing
(addon_dir);
71
filesystem::create_directory_if_missing
(addon_dir +
"/maps"
);
72
filesystem::create_directory_if_missing
(addon_dir +
"/scenarios"
);
73
filesystem::create_directory_if_missing
(addon_dir +
"/images"
);
74
filesystem::create_directory_if_missing
(addon_dir +
"/images/units"
);
75
filesystem::create_directory_if_missing
(addon_dir +
"/masks"
);
76
filesystem::create_directory_if_missing
(addon_dir +
"/music"
);
77
filesystem::create_directory_if_missing
(addon_dir +
"/sounds"
);
78
filesystem::create_directory_if_missing
(addon_dir +
"/translations"
);
79
filesystem::create_directory_if_missing
(addon_dir +
"/units"
);
80
filesystem::create_directory_if_missing
(addon_dir +
"/utils"
);
81
82
// create files
83
// achievements
84
{
85
filesystem::scoped_ostream
stream =
filesystem::ostream_file
(addon_dir +
"/achievements.cfg"
);
86
*stream <<
""
;
87
}
88
89
// _server.pbl
90
{
91
filesystem::scoped_ostream
stream =
filesystem::ostream_file
(addon_dir +
"/_server.pbl"
);
92
*stream <<
""
;
93
}
94
95
// a basic _main.cfg
96
{
97
filesystem::scoped_ostream
stream =
filesystem::ostream_file
(addon_dir +
"/_main.cfg"
);
98
*stream <<
"#textdomain wesnoth-"
<< addon_id <<
"\n"
99
<<
"[textdomain]"
<<
"\n"
100
<<
" name=\"wesnoth-"
<< addon_id <<
"\"\n"
101
<<
" path=\"data/add-ons/"
<< addon_id <<
"/translations\"\n"
102
<<
"[/textdomain]\n"
103
<<
"\n"
104
<<
"[binary_path]\n"
105
<<
" path=data/add-ons/"
<< addon_id <<
"\n"
106
<<
"[/binary_path]\n"
107
<<
"\n"
108
<<
"{~add-ons/"
<< addon_id <<
"/scenarios}\n"
109
<<
"{~add-ons/"
<< addon_id <<
"/utils}\n"
110
<<
"\n"
111
<<
"[units]\n"
112
<<
" {~add-ons/"
<< addon_id <<
"/units}\n"
113
<<
"[/units]\n"
;
114
}
115
116
return
addon_id;
117
}
118
119
EXIT_STATUS
start
(
bool
clear_id,
const
std::string&
filename
,
bool
take_screenshot,
const
std::string& screenshot_filename)
120
{
121
EXIT_STATUS
e
=
EXIT_ERROR
;
122
try
{
123
const
hotkey::scope_changer
h
{
hotkey::scope_editor
};
124
125
editor_controller
editor
(clear_id);
126
127
if
(take_screenshot) {
128
if
(!
filesystem::file_exists
(
filename
)) {
129
ERR_ED
<<
"Map file '"
<<
filename
<<
"' does not exist. Unable to take screenshot!"
;
130
return
EXIT_ERROR
;
131
}
132
editor
.context_manager_->load_map(
filename
,
false
);
133
editor
.do_screenshot(screenshot_filename);
134
return
EXIT_NORMAL
;
135
}
136
137
if
(!
filename
.empty()) {
138
if
(
filesystem::is_directory
(
filename
)) {
139
editor
.context_manager_->set_default_dir(
filename
);
140
editor
.context_manager_->load_map_dialog(
true
);
141
}
else
{
142
editor
.context_manager_->load_map(
filename
,
false
);
143
144
// HACK: this fixes an issue where the button overlays would be missing when
145
// the loaded map appears.
146
//
147
// Do note adding a redraw_everything call to context_manager::refresh_all also
148
// fixes the issue, but I'm pretty sure thats just because editor_controller::
149
// display_redraw_callback gets called, which then calls set_button_state.
150
//
151
// -- vultraz, 2018-02-24
152
editor
.set_button_state();
153
}
154
}
155
156
e
=
editor
.main_loop();
157
}
catch
(
const
editor_exception
&
e
) {
158
ERR_ED
<<
"Editor exception in editor::start: "
<<
e
.what();
159
throw
;
160
}
161
if
(
editor_action::get_instance_count
() != 0) {
162
ERR_ED
<<
"Possibly leaked "
<<
editor_action::get_instance_count
() <<
" action objects"
;
163
}
164
165
return
e
;
166
}
167
168
}
//end namespace editor
action_base.hpp
Base class for editor actions.
choose_addon.hpp
chrono.hpp
editor::editor_action::get_instance_count
static int get_instance_count()
Debugging aid.
Definition
action_base.hpp:101
editor::editor_controller
The editor_controller class contains the mouse and keyboard event handling routines for the editor.
Definition
editor_controller.hpp:46
gui2::dialogs::editor_choose_addon
Definition
choose_addon.hpp:23
gui2::dialogs::modal_dialog::show
bool show(const unsigned auto_close_time=0)
Shows the window.
Definition
modal_dialog.cpp:51
hotkey::scope_changer
Definition
hotkey_command.hpp:289
lg::log_domain
Definition
log.hpp:124
ERR_ED
#define ERR_ED
Definition
editor_common.hpp:34
editor_controller.hpp
log_editor
lg::log_domain log_editor("editor")
filesystem.hpp
Declarations for File-IO.
gettext.hpp
timestamp
static bool timestamp
Definition
log.cpp:89
chrono::format_local_timestamp
auto format_local_timestamp(const std::chrono::system_clock::time_point &time, std::string_view format="%F %T")
Definition
chrono.hpp:72
editor
Manage the empty-palette in the editor.
Definition
action.cpp:31
editor::EXIT_STATUS
EXIT_STATUS
Definition
editor_main.hpp:23
editor::EXIT_ERROR
@ EXIT_ERROR
Definition
editor_main.hpp:27
editor::EXIT_NORMAL
@ EXIT_NORMAL
Definition
editor_main.hpp:24
editor::start
EXIT_STATUS start(bool clear_id, const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
Definition
editor_main.cpp:119
editor::initialize_addon
std::string initialize_addon()
Definition
editor_main.cpp:32
filesystem::file_exists
bool file_exists(const std::string &name)
Returns true if a file or directory with such name already exists.
filesystem::create_directory_if_missing
bool create_directory_if_missing(const std::string &dirname)
Creates a directory if it does not exist already.
filesystem::is_directory
bool is_directory(const std::string &fname)
Returns true if the given file is a directory.
filesystem::ostream_file
filesystem::scoped_ostream ostream_file(const std::string &fname, std::ios_base::openmode mode=std::ios_base::binary, bool create_directory=true)
filesystem::scoped_ostream
std::unique_ptr< std::ostream > scoped_ostream
Definition
filesystem.hpp:53
filesystem::get_addons_dir
std::string get_addons_dir()
Definition
filesystem_common.cpp:205
hotkey::scope_editor
constexpr uint32_t scope_editor
Definition
hotkey_command.hpp:45
prompt.hpp
filename
std::string filename
Filename.
Definition
spritesheet_generator.cpp:67
editor::editor_exception
Definition
editor_common.hpp:42
addon_filename_legal
bool addon_filename_legal(const std::string &name)
Checks whether an add-on file name is legal or not.
Definition
validation.cpp:66
validation.hpp
e
#define e
h
#define h
Generated by
1.9.8