Removed or commented out (for reference) all uses of the GUI1 button widget

And removed said widget.
This commit is contained in:
Charles Dang 2018-03-26 13:11:48 +11:00
parent e0c9d7b9f6
commit 34797ed1a2
19 changed files with 43 additions and 796 deletions

View file

@ -3456,13 +3456,6 @@
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)Whiteboard\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)Whiteboard\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\widgets\button.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)Widgets\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)Widgets\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)Widgets\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)Widgets\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)Widgets\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\widgets\widget.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)Widgets\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)Widgets\</ObjectFileName>
@ -4066,7 +4059,6 @@
<ClInclude Include="..\..\src\whiteboard\typedefs.hpp" />
<ClInclude Include="..\..\src\whiteboard\utility.hpp" />
<ClInclude Include="..\..\src\whiteboard\visitor.hpp" />
<ClInclude Include="..\..\src\widgets\button.hpp" />
<ClInclude Include="..\..\src\widgets\widget.hpp" />
<ClInclude Include="..\..\src\wml_exception.hpp" />
<ClInclude Include="..\..\src\wml_separators.hpp" />

View file

@ -986,9 +986,6 @@
<ClCompile Include="..\..\src\whiteboard\utility.cpp">
<Filter>Whiteboard</Filter>
</ClCompile>
<ClCompile Include="..\..\src\widgets\button.cpp">
<Filter>Widgets</Filter>
</ClCompile>
<ClCompile Include="..\..\src\widgets\widget.cpp">
<Filter>Widgets</Filter>
</ClCompile>
@ -2453,9 +2450,6 @@
<ClInclude Include="..\..\src\whiteboard\visitor.hpp">
<Filter>Whiteboard</Filter>
</ClInclude>
<ClInclude Include="..\..\src\widgets\button.hpp">
<Filter>Widgets</Filter>
</ClInclude>
<ClInclude Include="..\..\src\widgets\widget.hpp">
<Filter>Widgets</Filter>
</ClInclude>

View file

@ -52,6 +52,5 @@ time_of_day.cpp
tooltips.cpp
utils/make_enum.cpp
video.cpp
widgets/button.cpp
widgets/widget.cpp
wml_exception.cpp

View file

@ -731,16 +731,6 @@ surface display::screenshot(bool map_screenshot)
return res;
}
std::shared_ptr<gui::button> display::find_action_button(const std::string& /*id*/)
{
return nullptr;
}
std::shared_ptr<gui::button> display::find_menu_button(const std::string& /*id*/)
{
return nullptr;
}
static const std::string& get_direction(std::size_t n)
{
static const std::array<std::string, 6> dirs {{"-n", "-ne", "-se", "-s", "-sw", "-nw"}};

View file

@ -66,7 +66,6 @@ class manager;
#include "time_of_day.hpp"
#include "utils/functional.hpp"
#include "video.hpp"
#include "widgets/button.hpp"
#include <SDL_rect.h>
#include <boost/circular_buffer.hpp>
@ -443,19 +442,6 @@ public:
void set_theme(config theme_cfg);
/**
* Retrieves a pointer to a theme UI button.
*
* @note The returned pointer may either be nullptr, meaning the button
* isn't defined by the current theme, or point to a valid
* gui::button object. However, the objects retrieved will be
* destroyed and recreated by draw() method calls. Do *NOT* store
* these pointers for longer than strictly necessary to
* accomplish a specific task before the next screen refresh.
*/
std::shared_ptr<gui::button> find_action_button(const std::string& id);
std::shared_ptr<gui::button> find_menu_button(const std::string& id);
void draw_minimap_units();
/**

View file

@ -648,10 +648,12 @@ bool editor_controller::do_execute_command(const hotkey::hotkey_command& cmd, in
//TODO mark the map as changed
sound::play_music_once(music_tracks_[index].id());
get_current_map_context().add_to_playlist(music_tracks_[index]);
#if 0
std::vector<config> items;
items.emplace_back("id", "editor-playlist");
std::shared_ptr<gui::button> b = gui_->find_menu_button("menu-playlist");
show_menu(items, b->location().x +1, b->location().y + b->height() +1, false, *gui_);
#endif
return true;
}
case SCHEDULE:

View file

@ -127,12 +127,14 @@ void editor_palette<Item>::set_group(const std::string& id)
for (const item_group& group : groups_) {
if (group.id == id) {
found = true;
#if 0
std::shared_ptr<gui::button> palette_menu_button = gui_.find_menu_button("menu-editor-terrain");
if (palette_menu_button) {
//palette_menu_button->set_label(group.name);
palette_menu_button->set_tooltip_string(group.name);
palette_menu_button->set_overlay(group.icon);
}
#endif
}
}
assert(found);
@ -234,7 +236,7 @@ template<class Item>
void editor_palette<Item>::draw_contents()
{
toolkit_.set_mouseover_overlay(gui_);
#if 0
std::shared_ptr<gui::button> palette_menu_button = gui_.find_menu_button("menu-editor-terrain");
if (palette_menu_button) {
@ -244,19 +246,20 @@ void editor_palette<Item>::draw_contents()
palette_menu_button->set_tooltip_string(name);
palette_menu_button->set_overlay(icon);
}
#endif
unsigned int y = palette_y_;
unsigned int x = palette_x_;
int starting = items_start_;
int ending = std::min<int>(starting + nitems_, num_items());
#if 0
std::shared_ptr<gui::button> upscroll_button = gui_.find_action_button("upscroll-button-editor");
if (upscroll_button)
upscroll_button->enable(starting != 0);
std::shared_ptr<gui::button> downscroll_button = gui_.find_action_button("downscroll-button-editor");
if (downscroll_button)
downscroll_button->enable(ending != num_items());
#endif
int counter = starting;
for (int i = 0, size = num_visible_items(); i < size ; ++i) {

View file

@ -45,6 +45,7 @@ public:
void hide(bool /*hidden*/) override
{
#if 0
std::shared_ptr<gui::button> upscroll_button = gui_.find_action_button("upscroll-button-editor");
upscroll_button->enable(false);
std::shared_ptr<gui::button> downscroll_button = gui_.find_action_button("downscroll-button-editor");
@ -52,6 +53,7 @@ public:
std::shared_ptr<gui::button> palette_menu_button = gui_.find_menu_button("menu-editor-terrain");
palette_menu_button->set_overlay("");
palette_menu_button->enable(false);
#endif
}
std::vector<gui::widget>* get_widgets() { return nullptr; }

View file

@ -126,29 +126,6 @@ private:
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 (callback_) {
if (this->pressed()) {
callback_();
}
}
}
std::function<void (void)> callback_;
};
namespace editor {
location_palette::location_palette(editor_display &gui, const config& /*cfg*/,
editor_toolkit &toolkit)
@ -163,9 +140,9 @@ location_palette::location_palette(editor_display &gui, const config& /*cfg*/,
, items_()
, toolkit_(toolkit)
, buttons_()
, button_add_()
, button_delete_()
, button_goto_()
//, button_add_()
//, button_delete_()
//, button_goto_()
, help_handle_(-1)
, disp_(gui)
{
@ -181,9 +158,11 @@ sdl_handler_vector location_palette::handler_members()
for (gui::widget& b : buttons_) {
h.push_back(&b);
}
#if 0
if (button_add_) { h.push_back(button_add_.get()); }
if (button_delete_) { h.push_back(button_delete_.get()); }
if (button_goto_) { h.push_back(button_goto_.get()); }
#endif
return h;
}
@ -193,9 +172,11 @@ void location_palette::hide(bool hidden)
disp_.video().clear_help_string(help_handle_);
#if 0
std::shared_ptr<gui::button> palette_menu_button = disp_.find_menu_button("menu-editor-terrain");
palette_menu_button->set_overlay("");
palette_menu_button->enable(false);
#endif
for(auto& w : handler_members()) {
static_cast<gui::widget&>(*w).hide(hidden);
@ -244,6 +225,7 @@ void location_palette::adjust_size(const SDL_Rect& target)
const int button_height = 22;
const int button_y = 30;
int bottom = target.y + target.h;
#if 0
if (!button_goto_) {
button_goto_.reset(new location_palette_button(video(), SDL_Rect{ target.x , bottom -= button_y, target.w - 10, button_height }, _("Go To"), [this]() {
//static_cast<mouse_action_starting_position&>(toolkit_.get_mouse_action()). ??
@ -265,7 +247,7 @@ void location_palette::adjust_size(const SDL_Rect& target)
button_add_->set_location(SDL_Rect{ target.x , bottom -= button_y, target.w - 10, button_height });
button_delete_->set_location(SDL_Rect{ target.x , bottom -= button_y, target.w - 10, button_height });
}
#endif
const int space_for_items = bottom - target.y;
const int items_fitting = space_for_items / item_space_;
if (num_visible_items() != items_fitting) {
@ -312,6 +294,7 @@ void location_palette::draw_contents()
const int x = palette_x_;
const int starting = items_start_;
int ending = std::min<int>(starting + num_visible_items(), num_items());
#if 0
std::shared_ptr<gui::button> upscroll_button = disp_.find_action_button("upscroll-button-editor");
if (upscroll_button)
upscroll_button->enable(starting != 0);
@ -328,6 +311,7 @@ void location_palette::draw_contents()
if (button_delete_) {
button_delete_->set_dirty(true);
}
#endif
for (int i = 0, size = num_visible_items(); i < size; i++) {
location_palette_item & tile = buttons_[i];
@ -365,9 +349,11 @@ void location_palette::draw_contents()
std::vector<std::string> location_palette::action_pressed() const
{
std::vector<std::string> res;
#if 0
if (button_delete_ && button_delete_->pressed()) {
res.push_back("editor-remove-location");
}
#endif
return res;
}

View file

@ -21,7 +21,6 @@
#include <boost/ptr_container/ptr_vector.hpp>
class location_palette_item;
class location_palette_button;
namespace editor {
@ -116,9 +115,11 @@ private:
std::vector<std::string> items_;
editor_toolkit& toolkit_;
boost::ptr_vector<location_palette_item> buttons_;
#if 0
std::unique_ptr<location_palette_button> button_add_;
std::unique_ptr<location_palette_button> button_delete_;
std::unique_ptr<location_palette_button> button_goto_;
#endif
int help_handle_;
editor_display& disp_;
};

View file

@ -130,6 +130,7 @@ void palette_manager::draw_contents()
tooltips::clear_tooltips(loc);
#if 0
std::shared_ptr<gui::button> upscroll_button = gui_.find_action_button("upscroll-button-editor");
if (upscroll_button)
upscroll_button->hide(false);
@ -139,6 +140,7 @@ void palette_manager::draw_contents()
std::shared_ptr<gui::button> palette_menu_button = gui_.find_menu_button("menu-editor-terrain");
if (palette_menu_button)
palette_menu_button->hide(false);
#endif
// bg_restore(loc);
active_palette().set_dirty(true);

View file

@ -1286,8 +1286,10 @@ void play_controller::show_objectives() const
void play_controller::toggle_skipping_replay()
{
skip_replay_ = !skip_replay_;
#if 0
const std::shared_ptr<gui::button> skip_animation_button = get_display().find_action_button("skip-animation");
if(skip_animation_button) {
skip_animation_button->set_check(skip_replay_);
}
#endif
}

View file

@ -206,8 +206,10 @@ void playmp_controller::set_end_scenario_button()
{
// Modify the end-turn button
if (! is_host()) {
#if 0
std::shared_ptr<gui::button> btn_end = gui_->find_action_button("button-endturn");
btn_end->enable(false);
#endif
}
gui_->get_theme().refresh_title2("button-endturn", "title2");
}

View file

@ -36,7 +36,6 @@
#include "team.hpp" // for team, team::CONTROLLER::AI, etc
#include "wesnothd_connection_error.hpp"
#include "whiteboard/manager.hpp" // for manager
#include "widgets/button.hpp" // for button
#include <cassert> // for assert
#include <ctime> // for time
@ -351,10 +350,12 @@ turn_info::PROCESS_DATA_RESULT turn_info::process_network_data(const config& cfg
if(chat_only) {
return PROCESS_CANNOT_HANDLE;
}
#if 0
std::shared_ptr<gui::button> btn_end = display::get_singleton()->find_action_button("button-endturn");
if(btn_end) {
btn_end->enable(true);
}
#endif
return PROCESS_END_LINGER;
}

View file

@ -107,6 +107,7 @@ void replay_controller::rebuild_replay_theme()
}
}
#if 0
std::shared_ptr<gui::button> replay_controller::play_button()
{
return controller_.get_display().find_action_button("button-playreplay");
@ -136,12 +137,14 @@ std::shared_ptr<gui::button> replay_controller::play_move_button()
{
return controller_.get_display().find_action_button("button-nextmove");
}
#endif
void replay_controller::update_replay_ui()
{
//check if we have all buttons - if someone messed with theme then some buttons may be missing
//if any of the buttons is missing, we just disable every one
if(!replay_ui_has_all_buttons()) {
#if 0
std::shared_ptr<gui::button> play_b = play_button(), stop_b = stop_button(),
reset_b = reset_button(), play_turn_b = play_turn_button(),
play_side_b = play_side_button(), play_move_b = play_move_button();
@ -169,6 +172,7 @@ void replay_controller::update_replay_ui()
if (play_move_b) {
play_move_b->enable(false);
}
#endif
}
}
@ -176,38 +180,15 @@ void replay_controller::replay_ui_playback_should_start()
{
if(!replay_ui_has_all_buttons())
return;
play_button()->enable(false);
reset_button()->enable(false);
play_turn_button()->enable(false);
play_side_button()->enable(false);
play_move_button()->enable(false);
}
void replay_controller::replay_ui_playback_should_stop()
{
if(!replay_ui_has_all_buttons())
return;
if(!resources::recorder->at_end()) {
play_button()->enable(true);
reset_button()->enable(true);
play_turn_button()->enable(true);
play_side_button()->enable(true);
play_move_button()->enable(true);
play_button()->release();
play_turn_button()->release();
play_side_button()->release();
play_move_button()->release();
} else {
reset_button()->enable(true);
stop_button()->enable(false);
}
if(stop_condition_->should_stop()) {
//user interrupted
stop_button()->release();
//stop_button()->release();
}
}
@ -215,12 +196,6 @@ void replay_controller::reset_replay_ui()
{
if(!replay_ui_has_all_buttons())
return;
play_button()->enable(true);
stop_button()->enable(true);
reset_button()->enable(true);
play_turn_button()->enable(true);
play_side_button()->enable(true);
}
@ -264,9 +239,11 @@ void replay_controller::handle_generic_event(const std::string& name)
} else {
add_replay_theme();
}
#if 0
if(std::shared_ptr<gui::button> skip_animation_button = controller_.get_display().find_action_button("skip-animation")) {
skip_animation_button->set_check(controller_.is_skipping_replay());
}
#endif
}
bool replay_controller::recorder_at_end() const

View file

@ -69,16 +69,17 @@ private:
void replay_ui_playback_should_start();
void replay_ui_playback_should_stop();
#if 0
std::shared_ptr<gui::button> play_button();
std::shared_ptr<gui::button> stop_button();
std::shared_ptr<gui::button> reset_button();
std::shared_ptr<gui::button> play_turn_button();
std::shared_ptr<gui::button> play_side_button();
std::shared_ptr<gui::button> play_move_button();
#endif
bool replay_ui_has_all_buttons() {
return play_button() && stop_button() && reset_button() &&
play_turn_button() && play_side_button();
return true;
}
play_controller& controller_;
std::unique_ptr<replay_stop_condition> stop_condition_;

View file

@ -53,7 +53,6 @@
#include "version.hpp" // for version_info
#include "video.hpp" // for CVideo
#include "wesconfig.h" // for PACKAGE
#include "widgets/button.hpp" // for button
#include "wml_exception.hpp" // for wml_exception
#ifdef _WIN32
@ -1162,9 +1161,6 @@ int main(int argc, char** argv)
} catch(config::error& e) {
std::cerr << e.message << "\n";
error_exit(1);
} catch(gui::button::error&) {
std::cerr << "Could not create button: Image could not be found\n";
error_exit(1);
} catch(CVideo::quit&) {
// just means the game should quit
} catch(return_to_play_side_exception&) {

View file

@ -1,592 +0,0 @@
/*
Copyright (C) 2003 - 2018 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-lib"
#include "widgets/button.hpp"
#include "filesystem.hpp"
#include "font/sdl_ttf.hpp"
#include "game_config.hpp"
#include "game_errors.hpp"
#include "image.hpp"
#include "log.hpp"
#include "font/marked-up_text.hpp"
#include "font/standard_colors.hpp"
#include "sdl/rect.hpp"
#include "serialization/string_utils.hpp"
#include "sound.hpp"
#include "video.hpp"
#include "wml_separators.hpp"
#include <boost/algorithm/string/predicate.hpp>
static lg::log_domain log_display("display");
#define ERR_DP LOG_STREAM(err, log_display)
namespace gui {
const int font_size = font::SIZE_NORMAL;
const int horizontal_padding = font::SIZE_SMALL;
const int checkbox_horizontal_padding = font::SIZE_SMALL / 2;
const int vertical_padding = font::SIZE_SMALL / 2;
button::button(CVideo& video, const std::string& label, button::TYPE type,
std::string button_image_name, SPACE_CONSUMPTION spacing,
const bool auto_join, std::string overlay_image)
: widget(video, auto_join), type_(type),
label_text_(label),
image_(nullptr), pressedImage_(nullptr), activeImage_(nullptr), pressedActiveImage_(nullptr),
disabledImage_(nullptr), pressedDisabledImage_(nullptr),
overlayImage_(nullptr), overlayPressedImage_(nullptr), overlayActiveImage_(nullptr),
state_(NORMAL), pressed_(false),
spacing_(spacing), base_height_(0), base_width_(0),
button_image_name_(), button_overlay_image_name_(overlay_image),
button_image_path_suffix_()
{
if (button_image_name.empty()) {
switch (type_) {
case TYPE_PRESS:
button_image_name_ = "buttons/button_normal/button_H22";
break;
case TYPE_TURBO:
button_image_name_ = "buttons/button_menu/menu_button_copper_H20";
break;
case TYPE_CHECK:
button_image_name_ = "buttons/checkbox";
break;
case TYPE_RADIO:
button_image_name_ = "buttons/radiobox";
break;
default:
break;
}
} else {
button_image_name_ = "buttons/" + button_image_name;
}
load_images();
}
void button::load_images() {
std::string size_postfix;
switch (location().h) {
case 25:
size_postfix = "_25";
break;
case 30:
size_postfix = "_30";
break;
case 60:
size_postfix = "_60";
break;
default:
break;
}
surface button_image(image::get_image(button_image_name_ + ".png" + button_image_path_suffix_));
surface pressed_image(image::get_image(button_image_name_ + "-pressed.png"+ button_image_path_suffix_));
surface active_image(image::get_image(button_image_name_ + "-active.png"+ button_image_path_suffix_));
surface disabled_image;
if (filesystem::file_exists(game_config::path + "/images/" + button_image_name_ + "-disabled.png"))
disabled_image.assign((image::get_image(button_image_name_ + "-disabled.png"+ button_image_path_suffix_)));
surface pressed_disabled_image, pressed_active_image, touched_image;
if (!button_overlay_image_name_.empty()) {
if (button_overlay_image_name_.length() > size_postfix.length() &&
boost::algorithm::ends_with(button_overlay_image_name_, size_postfix)) {
button_overlay_image_name_.resize(button_overlay_image_name_.length() - size_postfix.length());
}
overlayImage_.assign(image::get_image(button_overlay_image_name_ + size_postfix + ".png"+ button_image_path_suffix_));
overlayPressedImage_.assign(image::get_image(button_overlay_image_name_ + size_postfix + "-pressed.png"+ button_image_path_suffix_));
if (filesystem::file_exists(game_config::path + "/images/" + button_overlay_image_name_ + size_postfix + "-active.png"))
overlayActiveImage_.assign(image::get_image(button_overlay_image_name_ + size_postfix + "-active.png"+ button_image_path_suffix_));
if (filesystem::file_exists(game_config::path + "/images/" + button_overlay_image_name_ + size_postfix + "-disabled.png"))
overlayDisabledImage_.assign(image::get_image(button_overlay_image_name_ + size_postfix + "-disabled.png"+ button_image_path_suffix_));
if (overlayDisabledImage_.null())
overlayDisabledImage_ = image::get_image(button_overlay_image_name_ + size_postfix + ".png~GS()" + button_image_path_suffix_);
if (filesystem::file_exists(game_config::path + "/images/" + button_overlay_image_name_ + size_postfix + "-disabled-pressed.png"))
overlayPressedDisabledImage_.assign(image::get_image(button_overlay_image_name_ + size_postfix + "-disabled-pressed.png"+ button_image_path_suffix_));
if (overlayPressedDisabledImage_.null())
overlayPressedDisabledImage_ = image::get_image(button_overlay_image_name_ + size_postfix + "-pressed.png~GS()"+ button_image_path_suffix_);
} else {
overlayImage_.assign(nullptr);
}
if (disabled_image == nullptr) {
disabled_image = image::get_image(button_image_name_ + ".png~GS()" + button_image_path_suffix_);
}
if (pressed_image.null())
pressed_image.assign(button_image);
if (active_image.null())
active_image.assign(button_image);
if (type_ == TYPE_CHECK || type_ == TYPE_RADIO) {
touched_image.assign(image::get_image(button_image_name_ + "-touched.png"+ button_image_path_suffix_));
if (touched_image.null())
touched_image.assign(pressed_image);
pressed_active_image.assign(image::get_image(button_image_name_ + "-active-pressed.png"+ button_image_path_suffix_));
if (pressed_active_image.null())
pressed_active_image.assign(pressed_image);
if (filesystem::file_exists(game_config::path + "/images/" + button_image_name_ + size_postfix + "-disabled-pressed.png"))
pressed_disabled_image.assign(image::get_image(button_image_name_ + "-disabled-pressed.png"+ button_image_path_suffix_));
if (pressed_disabled_image.null())
pressed_disabled_image = image::get_image(button_image_name_ + "-pressed.png~GS()"+ button_image_path_suffix_);
}
if (button_image.null()) {
std::string err_msg = "error initializing button images! file name: ";
err_msg += button_image_name_;
err_msg += ".png";
ERR_DP << err_msg << std::endl;
throw game::error(err_msg);
}
base_height_ = button_image->h;
base_width_ = button_image->w;
if (type_ != TYPE_IMAGE) {
set_label(label_text_);
}
if(type_ == TYPE_PRESS || type_ == TYPE_TURBO) {
image_.assign(scale_surface(button_image,location().w,location().h));
pressedImage_.assign(scale_surface(pressed_image,location().w,location().h));
activeImage_.assign(scale_surface(active_image,location().w,location().h));
disabledImage_.assign(scale_surface(disabled_image,location().w,location().h));
} else {
image_.assign(scale_surface(button_image,button_image->w,button_image->h));
activeImage_.assign(scale_surface(active_image,button_image->w,button_image->h));
disabledImage_.assign(scale_surface(disabled_image,button_image->w,button_image->h));
pressedImage_.assign(scale_surface(pressed_image,button_image->w,button_image->h));
if (type_ == TYPE_CHECK || type_ == TYPE_RADIO) {
pressedDisabledImage_.assign(scale_surface(pressed_disabled_image,button_image->w,button_image->h));
pressedActiveImage_.assign(scale_surface(pressed_active_image, button_image->w, button_image->h));
touchedImage_.assign(scale_surface(touched_image, button_image->w, button_image->h));
}
}
if (type_ == TYPE_IMAGE){
calculate_size();
}
}
button::~button()
{
}
void button::calculate_size()
{
if (type_ == TYPE_IMAGE){
SDL_Rect loc_image = location();
loc_image.h = image_->h;
loc_image.w = image_->w;
set_location(loc_image);
return;
}
const SDL_Rect& loc = location();
bool change_size = loc.h == 0 || loc.w == 0;
if (!change_size) {
unsigned w = loc.w - (type_ == TYPE_PRESS || type_ == TYPE_TURBO ? horizontal_padding : checkbox_horizontal_padding + base_width_);
if (type_ != TYPE_IMAGE)
{
int fs = font_size;
int style = TTF_STYLE_NORMAL;
std::string::const_iterator i_beg = label_text_.begin(), i_end = label_text_.end(),
i = font::parse_markup(i_beg, i_end, &fs, nullptr, &style);
if (i != i_end) {
std::string tmp(i, i_end);
label_text_.erase(i - i_beg, i_end - i_beg);
label_text_ += font::make_text_ellipsis(tmp, fs, w, style);
}
}
}
if (type_ != TYPE_IMAGE){
textRect_ = font::draw_text(nullptr, video().screen_area(), font_size,
font::BUTTON_COLOR, label_text_, 0, 0);
}
// TODO: There's a weird text clipping bug, allowing the code below to run fixes it.
// The proper fix should possibly be in the draw_contents() function.
#if 0
if (!change_size)
return;
#endif
set_height(std::max(textRect_.h+vertical_padding,base_height_));
if(type_ == TYPE_PRESS || type_ == TYPE_TURBO) {
if(spacing_ == MINIMUM_SPACE) {
set_width(textRect_.w + horizontal_padding);
} else {
set_width(std::max(textRect_.w+horizontal_padding,base_width_));
}
} else {
if(label_text_.empty()) {
set_width(base_width_);
} else {
set_width(checkbox_horizontal_padding + textRect_.w + base_width_);
}
}
}
void button::set_check(bool check)
{
if (type_ != TYPE_CHECK && type_ != TYPE_RADIO && type_ != TYPE_IMAGE)
return;
STATE new_state;
if (check) {
new_state = (state_ == ACTIVE || state_ == PRESSED_ACTIVE)? PRESSED_ACTIVE : PRESSED;
} else {
new_state = (state_ == ACTIVE || state_ == PRESSED_ACTIVE)? ACTIVE : NORMAL;
}
if (state_ != new_state) {
state_ = new_state;
set_dirty();
}
}
void button::set_active(bool active)
{
if ((state_ == NORMAL) && active) {
state_ = ACTIVE;
set_dirty();
} else if ((state_ == ACTIVE) && !active) {
state_ = NORMAL;
set_dirty();
}
}
bool button::checked() const
{
return state_ == PRESSED || state_ == PRESSED_ACTIVE || state_ == TOUCHED_PRESSED;
}
void button::enable(bool new_val)
{
if(new_val != enabled())
{
pressed_ = false;
// check buttons should keep their state
if(type_ != TYPE_CHECK) {
state_ = NORMAL;
}
widget::enable(new_val);
}
}
void button::draw_contents()
{
surface image = image_;
const int image_w = image_->w;
int offset = 0;
switch(state_) {
case ACTIVE:
image = activeImage_;
break;
case PRESSED:
image = pressedImage_;
if (type_ == TYPE_PRESS)
offset = 1;
break;
case PRESSED_ACTIVE:
image = pressedActiveImage_;
break;
case TOUCHED_NORMAL:
case TOUCHED_PRESSED:
image = touchedImage_;
break;
default:
break;
}
const SDL_Rect& loc = location();
SDL_Rect clipArea = loc;
const int texty = loc.y + loc.h / 2 - textRect_.h / 2 + offset;
int textx;
if (type_ != TYPE_CHECK && type_ != TYPE_RADIO && type_ != TYPE_IMAGE)
textx = loc.x + image->w / 2 - textRect_.w / 2 + offset;
else {
clipArea.w += image_w + checkbox_horizontal_padding;
textx = loc.x + image_w + checkbox_horizontal_padding / 2;
}
color_t button_color = font::BUTTON_COLOR;
if (!enabled()) {
if (state_ == PRESSED || state_ == PRESSED_ACTIVE)
image = pressedDisabledImage_;
else image = disabledImage_;
button_color = font::GRAY_COLOR;
}
if (!overlayImage_.null()) {
surface noverlay = make_neutral_surface(
enabled() ? overlayImage_ : overlayDisabledImage_);
if (!overlayPressedImage_.null()) {
switch (state_) {
case ACTIVE:
if (!overlayActiveImage_.null())
noverlay = make_neutral_surface(overlayActiveImage_);
break;
case PRESSED:
case PRESSED_ACTIVE:
case TOUCHED_NORMAL:
case TOUCHED_PRESSED:
noverlay = make_neutral_surface( enabled() ?
overlayPressedImage_ : overlayPressedDisabledImage_);
break;
default:
break;
}
}
surface nimage = make_neutral_surface(image);
sdl_blit(noverlay, nullptr, nimage, nullptr);
image = nimage;
}
video().blit_surface(loc.x, loc.y, image);
if (type_ != TYPE_IMAGE){
clipArea.x += offset;
clipArea.y += offset;
clipArea.w -= 2*offset;
clipArea.h -= 2*offset;
font::draw_text(&video(), clipArea, font_size, button_color, label_text_, textx, texty);
}
}
bool button::hit(int x, int y) const
{
return sdl::point_in_rect(x,y,location());
}
static bool is_valid_image(const std::string& str) { return !str.empty() && str[0] != IMAGE_PREFIX; }
void button::set_image(const std::string& image_file)
{
if(!is_valid_image(image_file)) {
return;
}
button_image_name_ = "buttons/" + image_file;
load_images();
set_dirty();
}
void button::set_overlay(const std::string& image_file)
{
// We allow empty paths for overlays
if(image_file[0] == IMAGE_PREFIX) {
return;
}
button_overlay_image_name_ = image_file;
load_images();
set_dirty();
}
void button::set_label(const std::string& val)
{
label_text_ = val;
//if we have a list of items, use the first one that isn't an image
if (std::find(label_text_.begin(), label_text_.end(), COLUMN_SEPARATOR) != label_text_.end()) {
const std::vector<std::string>& items = utils::split(label_text_, COLUMN_SEPARATOR);
const std::vector<std::string>::const_iterator i = std::find_if(items.begin(),items.end(),is_valid_image);
if(i != items.end()) {
label_text_ = *i;
}
}
calculate_size();
set_dirty(true);
}
void button::mouse_motion(const SDL_MouseMotionEvent& event)
{
if (hit(event.x, event.y)) {
// the cursor is over the widget
if (state_ == NORMAL)
state_ = ACTIVE;
else if (state_ == PRESSED && (type_ == TYPE_CHECK || type_ == TYPE_RADIO))
state_ = PRESSED_ACTIVE;
} else {
// the cursor is not over the widget
if (type_ == TYPE_CHECK || type_ == TYPE_RADIO) {
switch (state_) {
case TOUCHED_NORMAL:
state_ = NORMAL;
break;
case TOUCHED_PRESSED:
state_ = PRESSED;
break;
case PRESSED_ACTIVE:
state_ = PRESSED;
break;
case ACTIVE:
state_ = NORMAL;
break;
default:
break;
}
} else if ((type_ != TYPE_IMAGE) || state_ != PRESSED)
state_ = NORMAL;
}
}
void button::mouse_down(const SDL_MouseButtonEvent& event)
{
if (hit(event.x, event.y) && event.button == SDL_BUTTON_LEFT) {
switch (type_) {
case TYPE_RADIO:
case TYPE_CHECK:
if (state_ == PRESSED_ACTIVE)
state_ = TOUCHED_PRESSED;
else if (state_ == ACTIVE)
state_ = TOUCHED_NORMAL;
break;
case TYPE_TURBO:
sound::play_UI_sound(game_config::sounds::button_press);
state_ = PRESSED;
break;
default:
state_ = PRESSED;
break;
}
}
}
void button::release(){
state_ = NORMAL;
draw_contents();
}
void button::mouse_up(const SDL_MouseButtonEvent& event)
{
if (!(hit(event.x, event.y) && event.button == SDL_BUTTON_LEFT))
return;
// the user has stopped pressing the mouse left button while on the widget
switch (type_) {
case TYPE_CHECK:
switch (state_) {
case TOUCHED_NORMAL:
state_ = PRESSED_ACTIVE;
pressed_ = true;
break;
case TOUCHED_PRESSED:
state_ = ACTIVE;
pressed_ = true;
break;
default:
break;
}
if (pressed_) sound::play_UI_sound(game_config::sounds::checkbox_release);
break;
case TYPE_RADIO:
if (state_ == TOUCHED_NORMAL || state_ == TOUCHED_PRESSED) {
state_ = PRESSED_ACTIVE;
pressed_ = true;
// exit(0);
sound::play_UI_sound(game_config::sounds::checkbox_release);
}
//} else if (state_ == TOUCHED_PRESSED) {
// state_ = PRESSED_ACTIVE;
//}
break;
case TYPE_PRESS:
if (state_ == PRESSED) {
state_ = ACTIVE;
pressed_ = true;
sound::play_UI_sound(game_config::sounds::button_press);
}
break;
case TYPE_TURBO:
state_ = ACTIVE;
break;
case TYPE_IMAGE:
pressed_ = true;
sound::play_UI_sound(game_config::sounds::button_press);
break;
}
}
void button::handle_event(const SDL_Event& event)
{
gui::widget::handle_event(event);
if (hidden() || !enabled())
return;
STATE start_state = state_;
if (!mouse_locked())
{
switch(event.type) {
case SDL_MOUSEBUTTONDOWN:
mouse_down(event.button);
break;
case SDL_MOUSEBUTTONUP:
mouse_up(event.button);
break;
case SDL_MOUSEMOTION:
mouse_motion(event.motion);
break;
default:
return;
}
}
if (start_state != state_)
set_dirty(true);
}
bool button::pressed()
{
if (type_ != TYPE_TURBO) {
const bool res = pressed_;
pressed_ = false;
return res;
} else
return state_ == PRESSED || state_ == PRESSED_ACTIVE || state_ == TOUCHED_PRESSED;
}
}

View file

@ -1,97 +0,0 @@
/*
Copyright (C) 2003 - 2018 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 "widget.hpp"
#include "exceptions.hpp"
namespace gui {
class button : public widget
{
public:
struct error : public game::error {
error()
: game::error("GUI1 button error")
{}
};
enum TYPE { TYPE_PRESS, TYPE_CHECK, TYPE_TURBO, TYPE_IMAGE, TYPE_RADIO };
TYPE get_type() const { return type_; }
enum SPACE_CONSUMPTION { DEFAULT_SPACE, MINIMUM_SPACE };
button(CVideo& video, const std::string& label, TYPE type=TYPE_PRESS,
std::string button_image="", SPACE_CONSUMPTION spacing=DEFAULT_SPACE,
const bool auto_join=true, std::string overlay_image="");
/** Default implementation, but defined out-of-line for efficiency reasons. */
virtual ~button();
void set_check(bool check);
void set_active(bool active);
bool checked() const;
void set_label(const std::string& val);
void set_image(const std::string& image_file_base);
void set_overlay(const std::string& image_file_base);
void set_image_path_suffix(const std::string& suffix) { button_image_path_suffix_ = suffix; load_images(); }
bool pressed();
bool hit(int x, int y) const;
virtual void enable(bool new_val=true);
void release();
protected:
virtual void handle_event(const SDL_Event& event);
virtual void mouse_motion(const SDL_MouseMotionEvent& event);
virtual void mouse_down(const SDL_MouseButtonEvent& event);
virtual void mouse_up(const SDL_MouseButtonEvent& event);
virtual void draw_contents();
TYPE type_;
private:
void load_images();
void calculate_size();
std::string label_text_;
surface image_, pressedImage_, activeImage_, pressedActiveImage_,
touchedImage_, disabledImage_, pressedDisabledImage_,
overlayImage_, overlayPressedImage_, overlayPressedDisabledImage_, overlayDisabledImage_,
overlayActiveImage_;
SDL_Rect textRect_;
enum STATE { UNINIT, NORMAL, ACTIVE, PRESSED, PRESSED_ACTIVE, TOUCHED_NORMAL, TOUCHED_PRESSED };
STATE state_;
bool pressed_;
SPACE_CONSUMPTION spacing_;
int base_height_, base_width_;
std::string button_image_name_;
std::string button_overlay_image_name_;
std::string button_image_path_suffix_;
}; //end class button
}