use palettes in for the starting positions tool instead of a popup dialog

This commit is contained in:
gfgtdf 2016-06-17 19:23:24 +02:00
parent 672ef61a34
commit 6424d79f48
9 changed files with 485 additions and 20 deletions

View file

@ -733,6 +733,7 @@ set(wesnoth-main_SRC
editor/toolkit/brush.cpp
editor/palette/editor_palettes.cpp
editor/palette/item_palette.cpp
editor/palette/location_palette.cpp
editor/palette/terrain_palettes.cpp
editor/palette/unit_palette.cpp
editor/editor_display.cpp

View file

@ -280,6 +280,7 @@ wesnoth_sources = Split("""
editor/map/map_fragment.cpp
editor/palette/editor_palettes.cpp
editor/palette/item_palette.cpp
editor/palette/location_palette.cpp
editor/palette/palette_manager.cpp
editor/palette/terrain_palettes.cpp
editor/palette/tristate_button.cpp

View file

@ -400,28 +400,19 @@ editor_action* mouse_action_starting_position::up_left(editor_display& disp, int
}
const unsigned player_starting_at_hex = disp.map().is_starting_position(hex);
std::vector<map_location> starting_positions;
for(int i = 1; i <= gamemap::MAX_PLAYERS; ++i) {
starting_positions.push_back(disp.map().starting_position(i));
}
gui2::teditor_set_starting_position dlg(
player_starting_at_hex, gamemap::MAX_PLAYERS, starting_positions);
dlg.show(disp.video());
unsigned new_player_at_hex = dlg.result(); // 1st player = 1
unsigned new_player_at_hex = std::stoi(location_palette_.selected_item());
editor_action* a = nullptr;
if(new_player_at_hex != player_starting_at_hex) {
if(!new_player_at_hex) {
// Erase current starting position
a = new editor_action_starting_position(map_location(), player_starting_at_hex);
} else {
// Set a starting position
a = new editor_action_starting_position(hex, new_player_at_hex);
}
// Set a starting position
a = new editor_action_starting_position(hex, new_player_at_hex);
}
else {
// Erase current starting position
a = new editor_action_starting_position(map_location(), player_starting_at_hex);
}
update_brush_highlights(disp, hex);

View file

@ -20,6 +20,7 @@
#include "theme.hpp"
#include "editor/palette/editor_palettes.hpp"
#include "editor/palette/terrain_palettes.hpp"
#include "editor/palette/location_palette.hpp"
#include "editor/palette/empty_palette.hpp"
#include <SDL_video.h>
@ -367,8 +368,8 @@ protected:
class mouse_action_starting_position : public mouse_action
{
public:
mouse_action_starting_position(const CKey& key, empty_palette& palette)
: mouse_action(palette, key), click_(false)
mouse_action_starting_position(const CKey& key, location_palette& palette)
: mouse_action(palette, key), click_(false), location_palette_(palette)
{
}
@ -392,6 +393,7 @@ public:
private:
bool click_;
location_palette& location_palette_;
};

View file

@ -0,0 +1,343 @@
/*
Copyright (C) 2003 - 2016 by David White <dave@whitevine.net>
Part of 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-editor"
#include "location_palette.hpp"
#include "gettext.hpp"
#include "marked-up_text.hpp"
#include "tooltips.hpp"
#include "editor/action/mouse/mouse_action.hpp"
#include "wml_separators.hpp"
#include "formula/string_utils.hpp"
class location_palette_item : public gui::widget
{
public:
struct tstate {
tstate()
: selected(false)
, mouseover(false)
{}
bool selected;
bool mouseover;
friend bool operator==(tstate r, tstate l)
{
return r.selected == l.selected && r.mouseover == l.mouseover;
}
};
location_palette_item(CVideo& video, editor::location_palette& parent)
: gui::widget(video, true)
, parent_(parent)
{
}
void draw_contents() override
{
if (state_.mouseover) {
sdl::draw_solid_tinted_rectangle(location().x, location().y, location().w, location().h, 200, 200, 200, 0.1, video().getSurface());
}
if (state_.selected) {
sdl::draw_rectangle(location().x, location().y, location().w, location().h, -1, video().getSurface());
}
font::draw_text(&video(), location(), 16, font::NORMAL_COLOR, desc_.empty() ? id_ : desc_, location().x + 2, location().y, 0);
}
//TODO move to widget
bool hit(int x, int y) const
{
return sdl::point_in_rect(x, y, location());
}
void mouse_up(SDL_MouseButtonEvent const &e)
{
if (!(hit(e.x, e.y)))
return;
if (e.button == SDL_BUTTON_LEFT) {
parent_.select_item(id_);
}
if (e.button == SDL_BUTTON_RIGHT) {
//TODO: implement 'jump to item' or 'delete item' here.
}
}
void handle_event(const SDL_Event& e) override
{
gui::widget::handle_event(e);
if (hidden() || !enabled() || mouse_locked())
return;
tstate start_state = state_;
switch (e.type) {
case SDL_MOUSEBUTTONUP:
mouse_up(e.button);
break;
case SDL_MOUSEMOTION:
state_.mouseover = hit(e.motion.x, e.motion.y);
break;
default:
return;
}
if (!(start_state == state_))
set_dirty(true);
}
void set_item_id(const std::string& id)
{
id_ = id;
bool is_number = std::find_if(id.begin(), id.end(), [](char c) { return !std::isdigit(c); }) == id.end();
if (is_number) {
desc_ = vgettext("Player $side_num", utils::string_map{ {"side_num", id} });
}
else {
desc_ = "";
}
}
void set_selected(bool selected)
{
state_.selected = selected;
}
void draw() { gui::widget::draw(); }
private:
std::string id_;
std::string desc_;
tstate state_;
editor::location_palette& parent_;
};
class location_palette_button : public gui::button
{
public:
location_palette_button(CVideo& video, const SDL_Rect& location, const std::string& text, const std::function<void (void)>& callback)
: gui::button(video, text)
, callback_(callback)
{
this->set_location(location.x, location.y);
this->hide(false);
}
protected:
virtual void mouse_up(const SDL_MouseButtonEvent& e) override
{
gui::button::mouse_up(e);
if (this->pressed()) {
callback_();
}
}
std::function<void (void)> callback_;
};
namespace editor {
location_palette::location_palette(editor_display &gui, const config& /*cfg*/, mouse_action** active_mouse_action)
: common_palette(gui)
, gui_(gui)
, item_size_(20)
//TODO avoid magic number
, item_space_(20 + 3)
, palette_y_(0)
, palette_x_(0)
, nitems_(0)
, nmax_items_(0)
, items_start_(0)
, selected_item_()
, items_()
, active_mouse_action_(active_mouse_action)
, buttons_()
, button_add_()
, button_delete_()
, button_goto_()
, help_handle_(-1)
, disp_(gui)
{
for (int i = 1; i < 10; ++i) {
items_.push_back(std::to_string(i));
}
selected_item_ = items_[0];
}
sdl_handler_vector location_palette::handler_members()
{
sdl_handler_vector h;
for (gui::widget& b : buttons_) {
h.push_back(&b);
}
return h;
}
bool location_palette::scroll_up()
{
unsigned int decrement = 1;
if(items_start_ >= decrement) {
items_start_ -= decrement;
draw();
return true;
}
return false;
}
bool location_palette::can_scroll_up()
{
return (items_start_ != 0);
}
bool location_palette::can_scroll_down()
{
return (items_start_ + nitems_ + 1 <= num_items());
}
bool location_palette::scroll_down()
{
bool end_reached = (!(items_start_ + nitems_ + 1 <= num_items()));
bool scrolled = false;
// move downwards
if(!end_reached) {
items_start_ += 1;
scrolled = true;
set_dirty(true);
}
draw();
return scrolled;
}
void location_palette::adjust_size(const SDL_Rect& target)
{
palette_x_ = target.x;
palette_y_ = target.y;
const int button_height = 30;
int bottom = target.y + target.h - button_height;
button_add_.reset();
button_delete_.reset();
button_goto_.reset();
button_goto_.reset(new location_palette_button(video(), SDL_Rect{ target.x , bottom, target.w - 10, button_height }, _("Go To"), [this]() {
//static_cast<mouse_action_starting_position&>(**active_mouse_action_).
map_location pos = disp_.get_map().starting_position(std::stoi(selected_item_));
if (pos.valid()) {
disp_.scroll_to_tile(pos, display::WARP);
}
}));
/*
button_delete_.reset(new location_palette_button(video(), SDL_Rect{ target.x , bottom -= button_height, target.w - 10, button_height }, _("Delete"), [this]() {
//static_cast<mouse_action_starting_position&>(**active_mouse_action_).
//we currently don'T have access to editor_controller::perfm_delete here whcih we need to delete statign positions.
// maybe we can all something like fire_hotkey here.
}));
*/
const size_t space_for_items = bottom - target.x;
const unsigned items_fitting = static_cast<unsigned> (space_for_items / item_space_);
nitems_ = std::min<int>(items_fitting, items_.size());
if (buttons_.size() != nitems_) {
buttons_.resize(nitems_, &location_palette_item(gui_.video(), *this));
}
set_location(target);
set_dirty(true);
gui_.video().clear_help_string(help_handle_);
help_handle_ = gui_.video().set_help_string(get_help_string());
}
void location_palette::select_item(const std::string& item_id)
{
if (selected_item_ != item_id) {
selected_item_ = item_id;
set_dirty();
}
gui_.video().clear_help_string(help_handle_);
help_handle_ = gui_.video().set_help_string(get_help_string());
}
size_t location_palette::num_items()
{
return items_.size();
}
bool location_palette::is_selected_item(const std::string& id)
{
return selected_item_ == id;
}
void location_palette::draw_contents()
{
if (*active_mouse_action_)
(*active_mouse_action_)->set_mouse_overlay(gui_);
unsigned int y = palette_y_;
const unsigned int x = palette_x_;
const unsigned int starting = items_start_;
unsigned int ending = starting + nitems_;
if(ending > num_items() ){
ending = num_items();
}
gui::button* upscroll_button = gui_.find_action_button("upscroll-button-editor");
if (upscroll_button)
upscroll_button->enable(starting != 0);
gui::button* downscroll_button = gui_.find_action_button("downscroll-button-editor");
if (downscroll_button)
downscroll_button->enable(ending != num_items());
unsigned int counter = starting;
for (unsigned int i = 0 ; i < buttons_.size() ; i++) {
//TODO check if the conditions still hold for the counter variable
//for (unsigned int counter = starting; counter < ending; counter++)
location_palette_item & tile = buttons_[i];
tile.hide(true);
if (i >= ending) continue;
const std::string item_id = items_[counter];
std::stringstream tooltip_text;
const int counter_from_zero = counter - starting;
SDL_Rect dstrect;
dstrect.x = x;
dstrect.y = y;
dstrect.w = location().w - 10;
dstrect.h = item_size_ + 2;
tile.set_location(dstrect);
tile.set_tooltip_string(tooltip_text.str());
tile.set_item_id(item_id);
tile.set_selected(is_selected_item(item_id));
tile.set_dirty(true);
tile.hide(false);
tile.draw();
// Adjust location
y += item_space_;
counter++;
}
update_rect(location());
}
void location_palette::hide(bool hidden) {
widget::hide(hidden);
if (!hidden)
help_handle_ = gui_.video().set_help_string(get_help_string());
else gui_.video().clear_help_string(help_handle_);
for (gui::widget& w : buttons_) {
w.hide(hidden);
}
}
} // end namespace editor

View file

@ -0,0 +1,124 @@
/*
Copyright (C) 2003 - 2016 by David White <dave@whitevine.net>
Part of 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.
*/
#pragma once
#include "editor/editor_display.hpp"
#include "common_palette.hpp"
#include "tristate_button.hpp"
class location_palette_item;
class location_palette_button;
namespace editor {
class location_palette : public common_palette {
public:
location_palette(editor_display &gui, const config& /*cfg*/, mouse_action** active_mouse_action);
virtual sdl_handler_vector handler_members();
void set_start_item(size_t index) { items_start_ = index; }
size_t start_num(void) { return items_start_; }
/** Menu expanding for palette group list */
void expand_palette_groups_menu(std::vector< std::pair<std::string, std::string> >&) override {}
void expand_palette_groups_menu(std::vector<std::string>&) override {}
virtual void set_group(size_t /*index*/) override {}
virtual void next_group() override {}
virtual void prev_group() override {}
virtual const std::vector<item_group>& get_groups() const override { static const std::vector<item_group> empty; return empty; }
virtual void draw() {
widget::draw();
}
virtual void draw_contents();
/**
* Update the size of this widget.
*
* Use if the size_specs have changed.
*/
void adjust_size(const SDL_Rect& target);
virtual bool scroll_up();
virtual bool can_scroll_up();
virtual bool scroll_down();
virtual bool can_scroll_down();
void swap() override {}
bool can_swap() { return false; }
virtual std::string get_help_string() { return ""; }
/** Return the currently selected item. */
const std::string& selected_item() const { return selected_item_; }
virtual void select_item(const std::string& item_id);
private:
/** Scroll the editor-palette to the top. */
void scroll_top();
/** Scroll the editor-palette to the bottom. */
void scroll_bottom();
virtual bool is_selected_item(const std::string& id);
/** Return the number of items in the palette. */
size_t num_items();
void hide(bool hidden) override;
protected:
editor_display &gui_;
int item_size_;
// the heigh of a row, the size of an item including borders.
int item_space_;
private:
unsigned int palette_y_;
unsigned int palette_x_;
protected:
//the number of items visible at the same time.
size_t nitems_;
//
size_t nmax_items_;
//the current scrolling position
size_t items_start_;
private:
std::string selected_item_;
std::vector<std::string> items_;
mouse_action** active_mouse_action_;
boost::ptr_vector<location_palette_item> buttons_;
std::unique_ptr<location_palette_button> button_add_;
std::unique_ptr<location_palette_button> button_delete_;
std::unique_ptr<location_palette_button> button_goto_;
int help_handle_;
editor_display& disp_;
};
} //end namespace editor

View file

@ -32,6 +32,7 @@ palette_manager::palette_manager(editor_display& gui, const config& cfg
unit_palette_(new unit_palette(gui, cfg, active_mouse_action)),
empty_palette_(new empty_palette(gui)),
item_palette_(new item_palette(gui, cfg, active_mouse_action))
, location_palette_(new location_palette(gui, cfg, active_mouse_action))
{
unit_palette_->setup(cfg);
terrain_palette_->setup(cfg);

View file

@ -25,6 +25,7 @@
#include "terrain_palettes.hpp"
#include "unit_palette.hpp"
#include "item_palette.hpp"
#include "location_palette.hpp"
namespace editor {
@ -82,6 +83,7 @@ public:
boost::scoped_ptr<unit_palette> unit_palette_;
boost::scoped_ptr<empty_palette> empty_palette_;
boost::scoped_ptr<item_palette> item_palette_;
boost::scoped_ptr<location_palette> location_palette_;
};
}

View file

@ -75,7 +75,7 @@ void editor_toolkit::init_mouse_actions(context_manager& cmanager)
mouse_actions_.insert(std::make_pair(hotkey::HOTKEY_EDITOR_TOOL_SELECT,
new mouse_action_select(&brush_, key_, *palette_manager_->empty_palette_.get())));
mouse_actions_.insert(std::make_pair(hotkey::HOTKEY_EDITOR_TOOL_STARTING_POSITION,
new mouse_action_starting_position(key_, *palette_manager_->empty_palette_.get())));
new mouse_action_starting_position(key_, *palette_manager_->location_palette_.get())));
mouse_actions_.insert(std::make_pair(hotkey::HOTKEY_EDITOR_TOOL_LABEL,
new mouse_action_map_label(key_, *palette_manager_->empty_palette_.get())));
mouse_actions_.insert(std::make_pair(hotkey::HOTKEY_EDITOR_TOOL_UNIT,