Vultraz

Untitled

May 22nd, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.29 KB | None | 0 0
  1. /*
  2.    Copyright (C) 2008 - 2013 by Mark de Wever <[email protected]>
  3.    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2 of the License, or
  8.    (at your option) any later version.
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY.
  11.  
  12.    See the COPYING file for more details.
  13. */
  14.  
  15. #define GETTEXT_DOMAIN "wesnoth-editor"
  16.  
  17. #include "gui/dialogs/custom_tod.hpp"
  18.  
  19. #include "dialogs.hpp"
  20. #include "filesystem.hpp"
  21. #include "filechooser.hpp"
  22. #include "editor/editor_preferences.hpp"
  23. #include "editor/editor_display.hpp"
  24. #include "gui/dialogs/field.hpp"
  25. #include "gui/dialogs/helper.hpp"
  26. #include "gui/widgets/button.hpp"
  27. #include "gui/widgets/label.hpp"
  28. #include "gui/widgets/slider.hpp"
  29. #include "gui/widgets/settings.hpp"
  30. #include "gui/widgets/toggle_button.hpp"
  31. #include "gui/widgets/image.hpp"
  32. #include "gui/widgets/text_box.hpp"
  33. #include "gettext.hpp"
  34. #include "image.hpp"
  35.  
  36. #include <boost/bind.hpp>
  37.  
  38. namespace gui2 {
  39.  
  40. /*WIKI
  41.  * @page = GUIWindowDefinitionWML
  42.  * @order = 2_custom_tod
  43.  *
  44.  * == Custom Schedules ==
  45.  *
  46.  * This shows the dialog to modify tod schedules.
  47.  *
  48.  * @begin{table}{dialog_widgets}
  49.  *
  50.  * current_tod_image & & image & m &
  51.  *         The image for the time of day(ToD). $
  52.  *
  53.  * current_tod_mask & & image & m &
  54.  *         The image mask for the time of day(ToD). $
  55.  *
  56.  * next_tod & & button & m &
  57.  *         Selects the next ToD. $
  58.  *
  59.  * lawful_bonus & & slider & m &
  60.  *         Sets the Lawful Bonus for the current ToD. $
  61.  *
  62.  * tod_red & & slider & m &
  63.  *         Sets the red component of the current ToD. $
  64.  *
  65.  * tod_green & & slider & m &
  66.  *         Sets the green component of the current ToD. $
  67.  *
  68.  * tod_blue & & slider & m &
  69.  *         Sets the blue component of the current ToD. $
  70.  *
  71.  * @end{table}
  72.  */
  73.  
  74. REGISTER_DIALOG(custom_tod)
  75.  
  76. tcustom_tod::tcustom_tod(editor::editor_display* display
  77.         , const std::vector<time_of_day>& tods)
  78.     : tods_(tods)
  79.     , current_tod_(0)
  80.     , current_tod_name_(NULL)
  81.     , current_tod_id_(NULL)
  82.     , current_tod_image_(NULL)
  83.     , current_tod_mask_(NULL)
  84.     , current_tod_sound_(NULL)
  85.     , current_tod_number_(NULL)
  86.     , lawful_bonus_field_(register_integer("lawful_bonus"
  87.                 , true))
  88.     , tod_red_field_(register_integer("tod_red"
  89.                 , true
  90.                 , &preferences::editor::tod_r
  91.                 , &preferences::editor::set_tod_r))
  92.     , tod_green_field_(register_integer("tod_green"
  93.                 , true
  94.                 , &preferences::editor::tod_g
  95.                 , &preferences::editor::set_tod_g))
  96.     , tod_blue_field_(register_integer("tod_blue"
  97.                 , true
  98.                 , &preferences::editor::tod_b
  99.                 , &preferences::editor::set_tod_b))
  100.     , display_(display) {}
  101.  
  102. void tcustom_tod::select_file(const std::string& filename, const std::string& dir, const std::string& vector_attrib)
  103. {
  104.     std::string va = vector_attrib;
  105.     std::string fn = file_name(filename);
  106.     std::string dn = directory_name(fn);
  107.     if(dn.empty()) {
  108.         dn = dir;
  109.     }
  110.  
  111.     int res = dialogs::show_file_chooser_dialog(*display_, dn, _("Choose file"));
  112.     if (res == 0) {
  113.         if (va == "image") {
  114.             tods_[current_tod_].image = dn; }
  115.         else if (va == "mask") {
  116.             tods_[current_tod_].image_mask = dn; }
  117.         else if (va == "sound") {
  118.             tods_[current_tod_].sounds = dn;
  119.         }
  120.     }
  121. }
  122.  
  123. void tcustom_tod::do_next_tod(twindow& window)
  124. {
  125.     current_tod_++;
  126.     current_tod_ %= tods_.size();
  127.     update_selected_tod_info(window);
  128. }
  129.  
  130. void tcustom_tod::do_new_tod(twindow& window)
  131. {
  132.     tods_.insert(tods_.begin()+current_tod_, time_of_day());
  133.     update_selected_tod_info(window);
  134. }
  135.  
  136. void tcustom_tod::do_delete_tod(twindow& window)
  137. {
  138.     assert(tods_.begin()+current_tod_ < tods_.end());
  139.     if (tods_.size() == 1) {
  140.         tods_.at(0) = time_of_day();
  141.         update_selected_tod_info(window);
  142.         return;
  143.     }
  144.     tods_.erase(tods_.begin()+current_tod_);
  145.     if (tods_.begin()+current_tod_ >= tods_.end()) {
  146.         current_tod_ =  tods_.size() - 1;
  147.     }
  148.     update_selected_tod_info(window);
  149. }
  150.  
  151. const std::vector<time_of_day>& tcustom_tod::do_save_schedule() const
  152. {
  153.     return tods_;
  154. }
  155.  
  156. const time_of_day& tcustom_tod::get_selected_tod() const
  157. {
  158.     assert(static_cast<size_t>(current_tod_) < tods_.size());
  159.     return tods_[current_tod_];
  160. }
  161.  
  162. void tcustom_tod::update_tod_display(twindow& window)
  163. {
  164.     image::set_color_adjustment(
  165.               tod_red_field_->get_widget_value(window)
  166.             , tod_green_field_->get_widget_value(window)
  167.             , tod_blue_field_->get_widget_value(window));
  168.  
  169.     // Prevent a floating slice of window appearing alone over the
  170.     // theme UI sidebar after redrawing tiles and before we have a
  171.     // chance to redraw the rest of this window.
  172.     window.undraw();
  173.  
  174.     if(display_) {
  175.         // NOTE: We only really want to re-render the gamemap tiles here.
  176.         // Redrawing everything is a significantly more expensive task.
  177.         // At this time, tiles are the only elements on which ToD tint is
  178.         // meant to have a visible effect. This is very strongly tied to
  179.         // the image caching mechanism.
  180.         //
  181.         // If this ceases to be the case in the future, you'll need to call
  182.         // redraw_everything() instead.
  183.  
  184.         // invalidate all tiles so they are redrawn with the new ToD tint next
  185.         display_->invalidate_all();
  186.         // redraw tiles
  187.         display_->draw();
  188.     }
  189.  
  190.     // Redraw this window again.
  191.     window.set_dirty(true);
  192. }
  193.  
  194. void tcustom_tod::update_lawful_bonus(twindow& window)
  195. {
  196.     tods_[current_tod_].lawful_bonus = lawful_bonus_field_->get_widget_value(window);
  197. }
  198.  
  199. void tcustom_tod::slider_update_callback(twindow& window)
  200. {
  201.     update_tod_display(window);
  202. }
  203.  
  204. void tcustom_tod::update_selected_tod_info(twindow& window)
  205. {
  206.     current_tod_name_->set_value(get_selected_tod().name);
  207.     current_tod_id_->set_value(get_selected_tod().id);
  208.     current_tod_image_->set_image(get_selected_tod().image);
  209.     current_tod_mask_->set_image(get_selected_tod().image_mask);
  210.     current_tod_sound_->set_label(get_selected_tod().sounds);
  211.    
  212.     std::stringstream ss;
  213.     ss << (current_tod_ + 1);
  214.     ss << "/" << tods_.size();
  215.     current_tod_number_->set_label(ss.str());
  216.  
  217.     lawful_bonus_field_->set_widget_value(
  218.               window
  219.             , get_selected_tod().lawful_bonus);
  220.     tod_red_field_->set_widget_value(
  221.               window
  222.             , get_selected_tod().color.r);
  223.     tod_green_field_->set_widget_value(
  224.               window
  225.             , get_selected_tod().color.g);
  226.     tod_blue_field_->set_widget_value(
  227.               window
  228.             , get_selected_tod().color.b);
  229.  
  230.     update_tod_display(window);
  231.     window.invalidate_layout();
  232. }
  233.  
  234. void tcustom_tod::pre_show(CVideo& /*video*/, twindow& window)
  235. {
  236.     assert(!tods_.empty());
  237.     current_tod_name_ = find_widget<ttext_box>(
  238.         &window, "tod_name", false, true);
  239.        
  240.     current_tod_id_ = find_widget<ttext_box>(
  241.         &window, "tod_id", false, true);
  242.  
  243.     current_tod_image_ = find_widget<timage>(
  244.             &window, "current_tod_image", false, true);
  245.            
  246.     current_tod_mask_ = find_widget<timage>(
  247.             &window, "current_tod_mask", false, true);
  248.            
  249.     current_tod_sound_ = find_widget<tlabel>(
  250.         &window, "current_sound", false, true);
  251.  
  252.     current_tod_number_ = find_widget<tlabel>(
  253.         &window, "tod_number", false, true);
  254.  
  255.     tbutton& image_button = find_widget<tbutton>(
  256.             &window, "image_button", false);
  257.     connect_signal_mouse_left_click(image_button, boost::bind(
  258. #ifdef _WIN32
  259.               &tcustom_tod::select_file, this, get_selected_tod().image, "data\core\images\misc", "image"));
  260. #else
  261.               &tcustom_tod::select_file, this, get_selected_tod().image, "data/core/images/misc", "image"));
  262. #endif
  263.            
  264.     tbutton& mask_button = find_widget<tbutton>(
  265.             &window, "mask_button", false);
  266.     connect_signal_mouse_left_click(mask_button, boost::bind(
  267. #ifdef _WIN32
  268.             &tcustom_tod::select_file, this, get_selected_tod().image_mask, "data\core\images", "mask"));
  269. #else
  270.             &tcustom_tod::select_file, this, get_selected_tod().image_mask, "data/core/images", "mask"));
  271. #endif
  272.            
  273.     tbutton& sound_button = find_widget<tbutton>(
  274.             &window, "sound_button", false);
  275.     connect_signal_mouse_left_click(sound_button, boost::bind(
  276. #ifdef _WIN32
  277.             &tcustom_tod::select_file, this, get_selected_tod().sounds, "data\core\sounds\ambient", "sound"));
  278. #else
  279.             &tcustom_tod::select_file, this, get_selected_tod().sounds, "data/core/sounds/ambient", "sound"));
  280. #endif
  281.            
  282.     tbutton& next_tod_button = find_widget<tbutton>(
  283.             &window, "next_tod", false);
  284.     connect_signal_mouse_left_click(next_tod_button, boost::bind(
  285.               &tcustom_tod::do_next_tod
  286.             , this
  287.             , boost::ref(window)));
  288.  
  289.     tbutton& new_button = find_widget<tbutton>(
  290.             &window, "new", false);
  291.     connect_signal_mouse_left_click(new_button, boost::bind(
  292.               &tcustom_tod::do_new_tod
  293.             , this
  294.             , boost::ref(window)));
  295.  
  296.     tbutton& delete_button = find_widget<tbutton>(
  297.             &window, "delete", false);
  298.     connect_signal_mouse_left_click(delete_button, boost::bind(
  299.               &tcustom_tod::do_delete_tod
  300.             , this
  301.             , boost::ref(window)));
  302.            
  303.     tbutton& save_button = find_widget<tbutton>(
  304.             &window, "save", false);
  305.     connect_signal_mouse_left_click(save_button, boost::bind(
  306.               &tcustom_tod::do_save_schedule
  307.             , this));
  308.  
  309.     connect_signal_notify_modified(*(lawful_bonus_field_->widget())
  310.             , boost::bind(
  311.                   &tcustom_tod::update_lawful_bonus
  312.                 , this
  313.                 , boost::ref(window)));
  314.                
  315.     connect_signal_notify_modified(*(tod_red_field_->widget())
  316.             , boost::bind(
  317.                   &tcustom_tod::slider_update_callback
  318.                 , this
  319.                 , boost::ref(window)));
  320.  
  321.     connect_signal_notify_modified(*(tod_green_field_->widget())
  322.             , boost::bind(
  323.                   &tcustom_tod::slider_update_callback
  324.                 , this
  325.                 , boost::ref(window)));
  326.  
  327.     connect_signal_notify_modified(*(tod_blue_field_->widget())
  328.             , boost::bind(
  329.                   &tcustom_tod::slider_update_callback
  330.                 , this
  331.                 , boost::ref(window)));
  332.  
  333.     for (size_t i = 0; i < tods_.size(); ++i) {
  334.  
  335.         time_of_day& tod = tods_[i];
  336.         const int r = tod_red_field_->get_widget_value(window);
  337.         const int g = tod_green_field_->get_widget_value(window);
  338.         const int b = tod_blue_field_->get_widget_value(window);
  339.         if (tod.color.r == r && tod.color.g == g && tod.color.b == b) {
  340.             current_tod_ = i;
  341.             update_selected_tod_info(window);
  342.             return;
  343.         }
  344.     }
  345.  
  346.     update_selected_tod_info(window);
  347. }
  348.  
  349. void tcustom_tod::post_show(twindow& window)
  350. {
  351.     update_tod_display(window);
  352. }
  353.  
  354. } // namespace gui2
Advertisement
Add Comment
Please, Sign In to add comment