editor2: brush selector...

...(mostly reused code from the old editor, a solid candidate for a
rewrite in the future)
This commit is contained in:
Tomasz Śniatowski 2008-08-13 18:32:51 +01:00
parent c763fc1dcf
commit d826e56664
12 changed files with 124 additions and 97 deletions

View file

@ -2,6 +2,7 @@
[brush]
name= _ "Single Hex"
image="editor/brush-1.png"
[relative]
x=0
y=0
@ -12,6 +13,7 @@
[brush]
radius=1
name= _ "Radius 1 Hex"
image="editor/brush-2.png"
[relative]
x=0
y=0
@ -21,21 +23,22 @@
[brush]
radius=2
name= _ "Radius 2 Hex"
image="editor/brush-3.png"
[relative]
x=0
y=0
[/relative]
[/brush]
[brush]
radius=3
name= _ "Radius 3 Hex"
[relative]
x=0
y=0
[/relative]
[/brush]
#[brush]
# radius=3
# name= _ "Radius 3 Hex"
# [relative]
# x=0
# y=0
# [/relative]
#[/brush]
#
[brush]
[relative]
x=-1
@ -50,37 +53,36 @@
y=0
[/relative]
name= _ "Hex Line NW-SE"
image="editor/brush-nw-se.png"
[/brush]
[brush]
[relative]
x=-1
y=0
[/relative]
[relative]
x=1
y=-1
[/relative]
[relative]
x=0
y=0
[/relative]
name= _ "Hex Line NW-SE"
[/brush]
[brush]
[relative]
x=0
y=1
[/relative]
[relative]
x=0
y=-1
[/relative]
[relative]
x=0
y=0
[/relative]
name= _ "Hex Line N-S"
[/brush]
#
#[brush]
# [relative]
# x=-1
# y=0
# [/relative]
# [relative]
# x=1
# y=-1
# [/relative]
# x=0
# [/relative]
# name= _ "Hex Line NW-SE"
#[/brush]
#
#[brush]
# [relative]
# x=0
# y=1
# [/relative]
# [relative]
# x=0
# y=-1
# [/relative]
# [relative]
# x=0
# y=0
# [/relative]
# name= _ "Hex Line N-S"
#[/brush]

View file

@ -178,7 +178,7 @@
auto_tooltip=yes
ref=top-right-panel
#harcoded since the brushes are above, which are hardcoded in src/editor/editor_layout.cpp
rect="=+15,200,+24,+24"
rect="=+15,180,+24,+24"
xanchor=right
yanchor=fixed
[/menu]

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -27,6 +27,7 @@ brush::brush()
}
brush::brush(const config& cfg)
: name_(cfg["name"]), image_(cfg["image"])
{
int radius = lexical_cast_default<int>(cfg["radius"], 0);
if (radius > 0) {

View file

@ -23,11 +23,15 @@
namespace editor2 {
/**
* The brush class represents a single brush -- a set of relative locations around a "hotspot",
* and related info such as the icon image. It is constructed from WML -- the [brush] tag.
*/
class brush
{
public:
/**
* Construct a default (empty) brush. Note that not even the hotspot is affected by default
* Construct a default (empty) brush. Note that not even the hotspot is affected by default,
*/
brush();
@ -47,8 +51,24 @@ public:
*/
std::set<gamemap::location> project(const gamemap::location& hotspot) const;
/**
* @return the name of this brush
*/
const std::string name() const { return name_; }
/**
* @return the image of this brush
*/
const std::string image() const { return image_; }
protected:
/**
* The relative locations of the brush
*/
std::set<gamemap::location> relative_tiles_;
std::string name_;
std::string image_;
};

View file

@ -61,7 +61,6 @@ editor_controller::editor_controller(const config &game_config, CVideo& video)
adjust_sizes(gui(), *size_specs_);
palette_ = new terrain_palette(gui(), *size_specs_, get_map(), game_config,
foreground_terrain_, background_terrain_);
//brush_bar_ = new brush_bar(gui(), *size_specs_);
const config::child_list& children = game_config.get_children("brush");
foreach (const config* i, game_config.get_children("brush")) {
brushes_.push_back(brush(*i));
@ -72,6 +71,7 @@ editor_controller::editor_controller(const config &game_config, CVideo& video)
brushes_[0].add_relative_location(0, 0);
}
brush_ = &brushes_[0];
brush_bar_ = new brush_bar(gui(), *size_specs_, brushes_, &brush_);
mouse_actions_.insert(std::make_pair(hotkey::HOTKEY_EDITOR_TOOL_PAINT,
new mouse_action_paint(foreground_terrain_, &brush_, key_)));
@ -99,9 +99,11 @@ editor_controller::editor_controller(const config &game_config, CVideo& video)
cursor::set(cursor::NORMAL);
gui_->invalidate_game_status();
palette_->adjust_size();
brush_bar_->adjust_size();
refresh_all();
gui_->draw();
palette_->draw(true);
brush_bar_->draw(true);
load_tooltips();
redraw_toolbar();
events::raise_draw_event();
@ -127,6 +129,7 @@ void editor_controller::load_tooltips()
editor_controller::~editor_controller()
{
delete palette_;
delete brush_bar_;
delete size_specs_;
delete floating_label_manager_;
delete map_generator_;
@ -814,7 +817,9 @@ void editor_controller::display_redraw_callback(display&)
{
adjust_sizes(gui(), *size_specs_);
palette_->adjust_size();
brush_bar_->adjust_size();
palette_->draw(true);
brush_bar_->draw(true);
//display::redraw_everything removes our custom tooltips so reload them
load_tooltips();
gui().invalidate_all();

View file

@ -33,7 +33,7 @@ size_specs::size_specs() {
terrain_width = default_palette_width;
brush_x = 0;
brush_y = 0;
brush_padding = 5;
brush_padding = 2;
palette_x = 0;
palette_y = 0;
palette_w = 10;
@ -42,8 +42,8 @@ size_specs::size_specs() {
void adjust_sizes(const display &disp, size_specs &sizes) {
//! @todo Hardcoded coordinates for brush selection, make it themeable
sizes.brush_x = disp.map_outside_area().w + 22;
sizes.brush_y = 165;
sizes.brush_x = disp.map_outside_area().w + 8;
sizes.brush_y = 245;
//! @todo Hardcoded coordinates for terrain palette, make it themeable
sizes.palette_x = disp.map_outside_area().w + 16;
sizes.palette_y = sizes.brush_y + 132 + 10;

View file

@ -12,14 +12,15 @@
See the COPYING file for more details.
*/
//! @file editor/editor_palettes.cpp
//! Manage the terrain-palette in the editor.
#include "SDL.h"
#include "SDL_keysym.h"
/**
* Manage the terrain-palette in the editor.
* Note: this is a near-straight rip from the old editor.
*/
#include "editor_common.hpp"
#include "editor_palettes.hpp"
#include "editor_layout.hpp"
#include "../config.hpp"
#include "../sdl_utils.hpp"
#include "../serialization/string_utils.hpp"
@ -31,6 +32,9 @@
#include "../util.hpp"
#include "../video.hpp"
#include "SDL.h"
#include "SDL_keysym.h"
#include <cassert>
namespace editor2 {
@ -107,7 +111,7 @@ terrain_palette::terrain_palette(display &gui, const size_specs &sizes,
terrains_ = terrain_map_["all"];
if(terrains_.empty()) {
std::cerr << "No terrain found.\n";
ERR_ED << "No terrain found.\n";
}
else {
selected_fg_terrain_ = terrains_[0];
@ -224,7 +228,7 @@ void terrain_palette::set_group(const std::string& id)
{
terrains_ = terrain_map_[id];
if(terrains_.empty()) {
std::cerr << "No terrain found.\n";
ERR_ED << "No terrain found.\n";
}
scroll_top();
}
@ -410,7 +414,7 @@ void terrain_palette::draw(bool force) {
surface base_image(image::get_image(base_filename));
if(base_image == NULL) {
std::cerr << "image for terrain " << counter << ": '" << base_filename << "' not found\n";
ERR_ED << "image for terrain " << counter << ": '" << base_filename << "' not found\n";
return;
}
@ -427,7 +431,7 @@ void terrain_palette::draw(bool force) {
const std::string filename = "terrain/" + map_.get_terrain_info(terrain).editor_image() + ".png";
surface image(image::get_image(filename));
if(image == NULL) {
std::cerr << "image for terrain " << counter << ": '" << filename << "' not found\n";
ERR_ED << "image for terrain " << counter << ": '" << filename << "' not found\n";
return;
}
@ -505,33 +509,36 @@ void terrain_palette::load_tooltips()
// restorer_.restore();
// }
brush_bar::brush_bar(display &gui, const size_specs &sizes)
: gui::widget(gui.video()), size_specs_(sizes), gui_(gui), selected_(0), total_brush_(3),
size_(30) {
brush_bar::brush_bar(display &gui, const size_specs &sizes,
std::vector<brush>& brushes, brush** the_brush)
: gui::widget(gui.video()), size_specs_(sizes), gui_(gui),
selected_(0), brushes_(brushes), the_brush_(the_brush),
size_(30) {
adjust_size();
}
void brush_bar::adjust_size() {// TODO
set_location(size_specs_.brush_x, size_specs_.brush_y);
set_measurements(size_ * total_brush_ + (total_brush_ - 1) * size_specs_.brush_padding, size_);
set_measurements(size_ * brushes_.size() + (brushes_.size() - 1) * size_specs_.brush_padding, size_);
set_dirty();
}
unsigned int brush_bar::selected_brush_size() {
return selected_ + 1;
return selected_;
}
void brush_bar::select_brush_size(int new_size) {
assert(new_size > 0 && new_size <= total_brush_);
selected_ = new_size - 1;
void brush_bar::select_brush(int index) {
assert(index > 0 && index < brushes_.size());
selected_ = index;
}
void brush_bar::left_mouse_click(const int mousex, const int mousey) {
int index = selected_index(mousex, mousey);
if(index >= 0) {
if (static_cast<unsigned>(index) != selected_) {
if (index != selected_) {
set_dirty();
selected_ = index;
*the_brush_ = &brushes_[index];
}
}
}
@ -577,26 +584,20 @@ void brush_bar::draw(bool force) {
// Everything will be redrawn even though only one little part may
// have changed, but that happens so seldom so we'll settle with this.
SDL_Surface* const screen = gui_.video().getSurface();
for (int i = 1; i <= total_brush_; i++) {
std::stringstream filename;
filename << "editor/brush-" << i << ".png";
surface image(image::get_image(filename.str()));
for (size_t i = 0; i < brushes_.size(); i++) {
std::string filename = brushes_[i].image();
surface image(image::get_image(filename));
if (image == NULL) {
std::cerr << "Image " << filename.str() << " not found." << std::endl;
ERR_ED << "Image " << filename << " not found." << std::endl;
continue;
}
if (static_cast<unsigned>(image->w) != size_ ||
static_cast<unsigned>(image->h) != size_) {
if (static_cast<unsigned>(image->w) != size_
|| static_cast<unsigned>(image->h) != size_) {
image.assign(scale_surface(image, size_, size_));
}
SDL_Rect dstrect;
dstrect.x = x;
dstrect.y = size_specs_.brush_y;
dstrect.w = image->w;
dstrect.h = image->h;
SDL_Rect dstrect = {x, size_specs_.brush_y, image->w, image->h};
SDL_BlitSurface(image, NULL, screen, &dstrect);
const Uint32 color = static_cast<unsigned>(i) == selected_brush_size() ?
const Uint32 color = i == selected_brush_size() ?
SDL_MapRGB(screen->format,0xFF,0x00,0x00) :
SDL_MapRGB(screen->format,0x00,0x00,0x00);
draw_rectangle(dstrect.x, dstrect.y, image->w, image->h, color, screen);
@ -606,23 +607,20 @@ void brush_bar::draw(bool force) {
set_dirty(false);
}
int brush_bar::selected_index(const int x, const int y) const {
int brush_bar::selected_index(int x, int y) const {
const int bar_x = size_specs_.brush_x;
const int bar_y = size_specs_.brush_y;
if ((x < bar_x || static_cast<unsigned>(x) > bar_x + size_ * total_brush_ +
total_brush_ * size_specs_.brush_padding) ||
(y < bar_y || static_cast<unsigned>(y) > bar_y + size_)) {
if ((x < bar_x || x > bar_x + size_ * brushes_.size() +
brushes_.size() * size_specs_.brush_padding) ||
(y < bar_y || y > bar_y + size_)) {
return -1;
}
for(int i = 0; i < total_brush_; i++) {
const int px = bar_x + size_ * i + i * size_specs_.brush_padding;
if(x >= px && static_cast<unsigned>(x) <= px + size_ &&
y >= bar_y && static_cast<unsigned>(y) <= bar_y + size_) {
for(int i = 0; i < brushes_.size(); i++) {
int px = bar_x + size_ * i + i * size_specs_.brush_padding;
if (x >= px && x <= px + size_ && y >= bar_y && y <= bar_y + size_) {
return i;
}
}

View file

@ -12,8 +12,10 @@
See the COPYING file for more details.
*/
//! @file editor/editor_palettes.hpp
//!
/**
* Manage the terrain-palette in the editor.
* Note: this is a near-straight rip from the old editor.
*/
#ifndef EDITOR_PALETTES_H_INCLUDED
#define EDITOR_PALETTES_H_INCLUDED
@ -26,6 +28,7 @@
#include "../display.hpp"
#include "../map.hpp"
#include "../widgets/widget.hpp"
#include "brush.hpp"
#include "editor_layout.hpp"
#include <vector>
@ -154,13 +157,13 @@ private:
//! A bar where the brush is drawn
class brush_bar : public gui::widget {
public:
brush_bar(display &gui, const size_specs &sizes);
brush_bar(display &gui, const size_specs &sizes, std::vector<brush>& brushes, brush** the_brush);
//! Return the size of currently selected brush.
unsigned int selected_brush_size();
//! Select a brush size.
void select_brush_size(int new_size);
void select_brush(int index);
//! Draw the palette. If force is true, everything
//! will be redrawn, even though it is not dirty.
@ -184,7 +187,8 @@ private:
const size_specs &size_specs_;
display &gui_;
unsigned int selected_;
const int total_brush_;
std::vector<brush> brushes_;
brush** the_brush_;
const size_t size_;
};

View file

@ -12,9 +12,6 @@
See the COPYING file for more details.
*/
//! @file action.hpp
//! Editor action classes
#ifndef EDITOR2_TERRAIN_GROUP_HPP
#define EDITOR2_TERRAIN_GROUP_HPP