Converted Terrain Layers dialog to GUI2

This commit is contained in:
Charles Dang 2016-11-24 01:46:33 +11:00
parent 3203a8de10
commit 376925bc9b
8 changed files with 671 additions and 96 deletions

View file

@ -0,0 +1,446 @@
#textdomain wesnoth-lib
###
### Definition of the window to display terrain graphics layers
###
#define _GUI_LAYER_LIST
[listbox]
id = "layer_list"
definition = "default"
[header]
[row]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
label = _ "#"
definition = "default_small"
linked_group = "index"
[/label]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
label = _ "Image"
definition = "default_small"
linked_group = "image_used"
[/label]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
label = _ "Name"
definition = "default_small"
linked_group = "full_image_and_name"
[/label]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
label = _ "Loc"
definition = "default_small"
linked_group = "loc"
[/label]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
label = _ "Layer"
definition = "default_small"
linked_group = "layer"
[/label]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
label = _ "Base.x"
definition = "default_small"
linked_group = "base_x"
[/label]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
label = _ "Base.y"
definition = "default_small"
linked_group = "base_y"
[/label]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
label = _ "Center"
definition = "default_small"
linked_group = "center"
[/label]
[/column]
[/row]
[/header]
[list_definition]
[row]
[column]
vertical_grow = "true"
horizontal_grow = "true"
[toggle_panel]
definition = "default"
[grid]
[row]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
id = "index"
definition = "default"
linked_group = "index"
[/label]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[image]
id = "image_used"
definition = "default"
linked_group = "image_used"
[/image]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[grid]
linked_group = "full_image_and_name"
[row]
[column]
grow_factor = 0
horizontal_grow = "true"
[image]
id = "image_full"
definition = "default"
[/image]
[/column]
[column]
grow_factor = 1
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
id = "name"
definition = "default_small"
[/label]
[/column]
[/row]
[/grid]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
id = "loc"
definition = "default_small"
linked_group = "loc"
[/label]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
id = "layer"
definition = "default_small"
linked_group = "layer"
[/label]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
id = "base_x"
definition = "default_small"
linked_group = "base_x"
[/label]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
id = "base_y"
definition = "default_small"
linked_group = "base_y"
[/label]
[/column]
[column]
border = "all"
border_size = 5
horizontal_grow = "true"
[label]
id = "center"
definition = "default_small"
linked_group = "center"
[/label]
[/column]
[/row]
[/grid]
[/toggle_panel]
[/column]
[/row]
[/list_definition]
[/listbox]
#enddef
[window]
id = "terrain_layers"
description = "Terrain layers dialog."
[resolution]
definition = "default"
automatic_placement = "true"
vertical_placement = "center"
horizontal_placement = "center"
maximum_height = 600
[linked_group]
id = "index"
fixed_width = "true"
[/linked_group]
[linked_group]
id = "image_used"
fixed_width = "true"
[/linked_group]
[linked_group]
id = "full_image_and_name"
fixed_width = "true"
[/linked_group]
[linked_group]
id = "loc"
fixed_width = "true"
[/linked_group]
[linked_group]
id = "layer"
fixed_width = "true"
[/linked_group]
[linked_group]
id = "base_x"
fixed_width = "true"
[/linked_group]
[linked_group]
id = "base_y"
fixed_width = "true"
[/linked_group]
[linked_group]
id = "center"
fixed_width = "true"
[/linked_group]
[tooltip]
id = "tooltip"
[/tooltip]
[helptip]
id = "tooltip"
[/helptip]
[grid]
[row]
grow_factor = 0
[column]
grow_factor = 1
border = "all"
border_size = 5
horizontal_alignment = "left"
[label]
definition = "title"
label = _ "Terrain Layers"
[/label]
[/column]
[/row]
[row]
grow_factor = 1
[column]
horizontal_grow = "true"
vertical_grow = "true"
[grid]
[row]
[column]
grow_factor = 1
border = "all"
border_size = 5
horizontal_grow = "true"
vertical_grow = "true"
{_GUI_LAYER_LIST}
[/column]
[column]
grow_factor = 0
horizontal_grow = "true"
vertical_grow = "true"
[grid]
[row]
grow_factor = 0
[column]
grow_factor = 0
border = "all"
border_size = 5
horizontal_grow = "true"
vertical_grow = "true"
[label]
label = _ "Flags"
definition = "default"
[/label]
[/column]
[/row]
[row]
grow_factor = 1
[column]
grow_factor = 0
border = "all"
border_size = 5
horizontal_grow = "true"
vertical_grow = "true"
[label]
id = "flags"
definition = "default"
wrap = "true"
[/label]
[/column]
[/row]
[/grid]
[/column]
[/row]
[/grid]
[/column]
[/row]
[row]
grow_factor = 0
[column]
grow_factor = 0
border = "all"
border_size = 5
horizontal_alignment = "right"
[button]
id = "ok"
definition = "default"
label = _ "Close"
[/button]
[/column]
[/row]
[/grid]
[/resolution]
[/window]
#undef _GUI_LAYER_LIST

View file

@ -648,6 +648,8 @@
<Unit filename="../../src/gui/dialogs/sp_options_configure.hpp" />
<Unit filename="../../src/gui/dialogs/statistics_dialog.cpp" />
<Unit filename="../../src/gui/dialogs/statistics_dialog.hpp" />
<Unit filename="../../src/gui/dialogs/terrain_layers.cpp" />
<Unit filename="../../src/gui/dialogs/terrain_layers.hpp" />
<Unit filename="../../src/gui/dialogs/theme_list.cpp" />
<Unit filename="../../src/gui/dialogs/theme_list.hpp" />
<Unit filename="../../src/gui/dialogs/title_screen.cpp" />

View file

@ -816,6 +816,7 @@ set(wesnoth-main_SRC
gui/dialogs/statistics_dialog.cpp
gui/dialogs/multiplayer/synced_choice_wait.cpp
gui/dialogs/theme_list.cpp
gui/dialogs/terrain_layers.cpp
gui/dialogs/title_screen.cpp
gui/dialogs/transient_message.cpp
gui/dialogs/unit_advance.cpp

View file

@ -415,6 +415,7 @@ wesnoth_sources = Split("""
gui/dialogs/statistics_dialog.cpp
gui/dialogs/theme_list.cpp
gui/dialogs/tooltip.cpp
gui/dialogs/terrain_layers.cpp
gui/dialogs/title_screen.cpp
gui/dialogs/transient_message.cpp
gui/dialogs/unit_advance.cpp

View file

@ -0,0 +1,158 @@
/*
Copyright (C) 2016 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/dialogs/terrain_layers.hpp"
#include "display.hpp"
#include "formatter.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/widgets/label.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else
#include "gui/widgets/listbox.hpp"
#endif
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "image.hpp"
namespace gui2
{
namespace dialogs
{
REGISTER_DIALOG(terrain_layers)
terrain_layers::terrain_layers(display_t& disp, const map_location& loc)
: tile_(nullptr)
, tile_logs_()
{
terrain_builder& builder = disp.get_builder();
tile_ = builder.get_tile(loc);
assert(tile_);
const std::string& tod_id = disp.get_time_of_day(loc).id;
tile_->rebuild_cache(tod_id, &tile_logs_);
}
void terrain_layers::pre_show(window& window)
{
//
// List terrain flags
//
std::vector<std::string> flags(tile_->flags.begin(), tile_->flags.end());
for(auto& flag : flags) {
flag = (formatter() << font::unicode_bullet << " " << flag).str();
}
find_widget<label>(&window, "flags", false).set_label(utils::join(flags, "\n"));
//
// Generate terrain list
//
listbox& list = find_widget<listbox>(&window, "layer_list", false);
int order = 1;
for(const terrain_builder::tile::log_details det : tile_logs_) {
const terrain_builder::tile::rule_image_rand& ri = *det.first;
const terrain_builder::rule_image_variant& variant = *det.second;
// TODO: also use random image variations (not just take 1st)
const image::locator& img = variant.images.front().get_first_frame();
const std::string& name = img.get_filename();
// TODO: deal with (rarely used) ~modifications
//const std::string& modif = img.get_modifications();
const map_location& loc_cut = img.get_loc();
std::map<std::string, string_map> data;
string_map item;
item["label"] = (formatter() << (ri->is_background() ? "B ": "F ") << order).str();
data.emplace("index", item);
std::ostringstream image_steam;
const int tz = game_config::tile_size;
SDL_Rect r = sdl::create_rect(0,0,tz,tz);
surface surf = image::get_image(img.get_filename());
// calculate which part of the image the terrain engine uses
if(loc_cut.valid()) {
// copied from image.cpp : load_image_sub_file()
r = sdl::create_rect(
((tz * 3) / 4) * loc_cut.x
, tz * loc_cut.y + (tz / 2) * (loc_cut.x % 2)
, tz
, tz
);
if(img.get_center_x() >= 0 && img.get_center_y() >= 0) {
r.x += surf->w / 2 - img.get_center_x();
r.y += surf->h / 2 - img.get_center_y();
}
}
image_steam << "terrain/foreground.png";
// Cut and mask the image
// ~CROP and ~BLIT have limitations, we do some math to avoid them
// TODO: ^ eh? what limitations?
SDL_Rect r2 = sdl::intersect_rects(r, sdl::create_rect(0,0,surf->w,surf->h));
if(r2.w > 0 && r2.h > 0) {
image_steam
<< "~BLIT(" << name
<< "~CROP("
<< r2.x << "," << r2.y << ","
<< r2.w << "," << r2.h << ")"
<< "," << r2.x - r.x << "," << r2.y - r.y
<< ")"
<< "~MASK(" << "terrain/alphamask.png" << ")";
}
item["label"] = image_steam.str();
data.emplace("image_used", item);
item["label"] = name + "~SCALE(72,72)";
data.emplace("image_full", item);
item["label"] = name;
data.emplace("name", item);
item["label"] = (formatter() << img.get_loc()).str();
data.emplace("loc", item);
item["label"] = std::to_string(ri->layer);
data.emplace("layer", item);
item["label"] = std::to_string(ri->basex);
data.emplace("base_x", item);
item["label"] = std::to_string(ri->basey);
data.emplace("base_y", item);
item["label"] = (formatter() << ri->center_x << ", " << ri->center_y).str();
data.emplace("center", item);
++order;
list.add_row(data);
}
}
} // namespace dialogs
} // namespace gui2

View file

@ -0,0 +1,56 @@
/*
Copyright (C) 2016 by the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef GUI_DIALOGS_TERRAIN_LAYERS_HPP_INCLUDED
#define GUI_DIALOGS_TERRAIN_LAYERS_HPP_INCLUDED
#include "gui/dialogs/modal_dialog.hpp"
#include "terrain/builder.hpp"
class CVideo;
class display;
namespace gui2
{
namespace dialogs
{
class terrain_layers : public modal_dialog
{
// Since the 'display' type name clashes with the standard GUI2 static display
// function name, we need a type alias.
using display_t = display;
public:
terrain_layers(display_t& disp, const map_location& loc);
static void display(display_t& disp, const map_location& loc, CVideo& video)
{
terrain_layers(disp, loc).show(video);
}
private:
/** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
virtual const std::string& window_id() const;
/** Inherited from modal_dialog. */
void pre_show(window& window);
terrain_builder::tile* tile_;
terrain_builder::tile::logs tile_logs_;
};
} // namespace dialogs
} // namespace gui2
#endif /* ! GUI_DIALOGS_TERRAIN_LAYERS_HPP_INCLUDED */

View file

@ -54,6 +54,7 @@
#include "gui/dialogs/statistics_dialog.hpp"
#include "gui/dialogs/edit_text.hpp"
#include "gui/dialogs/game_stats.hpp"
#include "gui/dialogs/terrain_layers.hpp"
#include "gui/dialogs/unit_create.hpp"
#include "gui/dialogs/unit_list.hpp"
#include "gui/dialogs/unit_recall.hpp"
@ -1501,105 +1502,12 @@ void console_handler::do_foreground() {
}
void console_handler::do_layers() {
const mouse_handler& mousehandler = menu_handler_.pc_.get_mouse_handler_base();
const map_location &loc = mousehandler.get_last_hex();
std::vector<std::string> layers;
//NOTE: columns reflect WML keys, don't translate them
std::string heading = std::string(1,HEADING_PREFIX) +
"^#" + COLUMN_SEPARATOR + // 0
"Image" + COLUMN_SEPARATOR + // 1
"Name" + COLUMN_SEPARATOR + // 2
"Loc" + COLUMN_SEPARATOR + // 3
"Layer" + COLUMN_SEPARATOR + // 4
"Base.x" + COLUMN_SEPARATOR + // 5
"Base.y" + COLUMN_SEPARATOR + // 6
"Center" // 7
;
layers.push_back(heading);
display& disp = *(menu_handler_.gui_);
terrain_builder& builder = disp.get_builder();
terrain_builder::tile* tile = builder.get_tile(loc);
const std::string& tod_id = disp.get_time_of_day(loc).id;
terrain_builder::tile::logs tile_logs;
tile->rebuild_cache(tod_id, &tile_logs);
const mouse_handler& mousehandler = menu_handler_.pc_.get_mouse_handler_base();
const map_location& loc = mousehandler.get_last_hex();
int order = 1;
for(const terrain_builder::tile::log_details det : tile_logs) {
const terrain_builder::tile::rule_image_rand& ri = *det.first;
const terrain_builder::rule_image_variant& variant = *det.second;
/** @todo also use random image variations (not just take 1st) */
const image::locator& img = variant.images.front().get_first_frame();
const std::string& name = img.get_filename();
/** @todo deal with (rarely used) ~modifications */
//const std::string& modif = img.get_modifications();
const map_location& loc_cut = img.get_loc();
std::ostringstream str;
int tz = game_config::tile_size;
SDL_Rect r = sdl::create_rect(0,0,tz,tz);
surface surf = image::get_image(img.get_filename());
// calculate which part of the image the terrain engine uses
if(loc_cut.valid()) {
// copied from image.cpp : load_image_sub_file()
r = sdl::create_rect(
((tz*3) / 4) * loc_cut.x
, tz * loc_cut.y + (tz / 2) * (loc_cut.x % 2)
, tz, tz);
if(img.get_center_x() >= 0 && img.get_center_y()>= 0){
r.x += surf->w/2 - img.get_center_x();
r.y += surf->h/2 - img.get_center_y();
}
}
str << (ri->is_background() ? "B ": "F ") << order
<< COLUMN_SEPARATOR
<< IMAGE_PREFIX << "terrain/foreground.png";
// cut and mask the image
// ~CROP and ~BLIT have limitations, we do some math to avoid them
SDL_Rect r2 = sdl::intersect_rects(r, sdl::create_rect(0,0,surf->w,surf->h));
if(r2.w > 0 && r2.h > 0) {
str << "~BLIT("
<< name << "~CROP("
<< r2.x << "," << r2.y << ","
<< r2.w << "," << r2.h
<< ")"
<< "," << r2.x-r.x << "," << r2.y-r.y
<< ")"
<< "~MASK(" << "terrain/alphamask.png" << ")";
}
str << COLUMN_SEPARATOR
<< IMAGE_PREFIX << name << "~SCALE(72,72)"
<< IMG_TEXT_SEPARATOR << name
<< COLUMN_SEPARATOR << img.get_loc()
<< COLUMN_SEPARATOR << ri->layer
<< COLUMN_SEPARATOR << ri->basex
<< COLUMN_SEPARATOR << ri->basey
<< COLUMN_SEPARATOR
<< ri->center_x << ", " << ri->center_y;
layers.push_back(str.str());
++order;
}
std::vector<std::string> flags(tile->flags.begin(),tile->flags.end());
std::ostringstream info;
// NOTE using ", " also allows better word wrapping
info << "Flags :" << utils::join(flags, ", ");
{
gui::dialog menu(menu_handler_.gui_->video(), _("Layers"), info.str(), gui::OK_CANCEL);
menu.set_menu(layers);
menu.show();
}
gui2::dialogs::terrain_layers::display(disp, loc, disp.video());
}
void console_handler::do_fps() {
preferences::set_show_fps(!preferences::show_fps());

View file

@ -88,6 +88,7 @@
#include "gui/dialogs/sp_options_configure.hpp"
#include "gui/dialogs/statistics_dialog.hpp"
#include "gui/dialogs/theme_list.hpp"
#include "gui/dialogs/terrain_layers.hpp"
#include "gui/dialogs/title_screen.hpp"
#include "gui/dialogs/tooltip.hpp"
#include "gui/dialogs/transient_message.hpp"
@ -443,6 +444,7 @@ BOOST_AUTO_TEST_CASE(test_gui2)
test<sp_options_configure>();
test<statistics_dialog>();
test<theme_list>();
//test<terrain_layers>();
//test<title_screen>();
test<transient_message>();
//test<unit_advance>();
@ -499,6 +501,7 @@ BOOST_AUTO_TEST_CASE(test_gui2)
"end_credits",
"mp_staging",
"mp_join_game",
"terrain_layers",
};
std::sort(list.begin(), list.end());
std::sort(omitted.begin(), omitted.end());