storyscreen/part.cpp

Go to the documentation of this file.
00001 /* $Id: part.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2009 - 2012 by Ignacio R. Morelle <shadowm2006@gmail.com>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /**
00017  * @file
00018  * Storyscreen parts and floating images representation.
00019  */
00020 
00021 #include "global.hpp"
00022 #include "asserts.hpp"
00023 #include "foreach.hpp"
00024 #include "log.hpp"
00025 #include "resources.hpp"
00026 #include "storyscreen/part.hpp"
00027 
00028 #include "config.hpp"
00029 #include "gamestatus.hpp"
00030 #include "game_events.hpp"
00031 #include "image.hpp"
00032 #include "serialization/string_utils.hpp"
00033 #include "util.hpp"
00034 #include "variable.hpp"
00035 #include "video.hpp"
00036 
00037 namespace storyscreen {
00038 
00039 floating_image::floating_image(const floating_image& fi)
00040     : file_()
00041     , x_(0)
00042     , y_(0)
00043     , delay_(0)
00044     , autoscaled_(false)
00045     , centered_(false)
00046 {
00047     this->assign(fi);
00048 }
00049 
00050 floating_image::floating_image(const config& cfg)
00051     : file_(cfg["file"])
00052     , x_(cfg["x"])
00053     , y_(cfg["y"])
00054     , delay_(cfg["delay"])
00055     , autoscaled_(cfg["scaled"].to_bool())
00056     , centered_(cfg["centered"].to_bool())
00057 {
00058 }
00059 
00060 void floating_image::assign(const floating_image& fi)
00061 {
00062     if(&fi == this)
00063         return;
00064 
00065     file_ = fi.file_; x_ = fi.x_; y_ = fi.y_; delay_ = fi.delay_;
00066     autoscaled_ = fi.autoscaled_; centered_ = fi.centered_;
00067 }
00068 
00069 floating_image::render_input floating_image::get_render_input(double scale, SDL_Rect& dst_rect) const
00070 {
00071     render_input ri = {
00072         {0,0,0,0},
00073         file_.empty() ? NULL : image::get_image(file_)
00074     };
00075 
00076     if(!ri.image.null()) {
00077         if(autoscaled_) {
00078             ri.image = scale_surface(
00079                 ri.image,
00080                 static_cast<int>(ri.image->w * scale),
00081                 static_cast<int>(ri.image->h * scale)
00082             );
00083         }
00084 
00085         ri.rect.x = static_cast<int>(x_*scale) + dst_rect.x;
00086         ri.rect.y = static_cast<int>(y_*scale) + dst_rect.y;
00087         ri.rect.w = ri.image->w;
00088         ri.rect.h = ri.image->h;
00089 
00090         if(centered_) {
00091             ri.rect.x -= ri.rect.w / 2;
00092             ri.rect.y -= ri.rect.h / 2;
00093         }
00094     }
00095     return ri;
00096 }
00097 
00098 part::part(const vconfig &part_cfg)
00099     : scale_background_(true)
00100     , background_file_()
00101     , show_title_()
00102     , text_()
00103     , text_title_()
00104     , text_block_loc_(part::BLOCK_BOTTOM)
00105     , title_alignment_(part::TEXT_LEFT)
00106     , music_()
00107     , sound_()
00108     , floating_images_()
00109 {
00110     resolve_wml(part_cfg);
00111 }
00112 
00113 part::BLOCK_LOCATION part::string_tblock_loc(const std::string& s)
00114 {
00115     if(s.empty() != true) {
00116         if(s == "top") {
00117             return part::BLOCK_TOP;
00118         }
00119         else if (s == "middle") {
00120             return part::BLOCK_MIDDLE;
00121         }
00122     }
00123     return part::BLOCK_BOTTOM;
00124 }
00125 
00126 part::TEXT_ALIGNMENT part::string_title_align(const std::string& s)
00127 {
00128     if(s.empty() != true) {
00129         if(s == "right") {
00130             return part::TEXT_RIGHT;
00131         }
00132         else if(s == "center") {
00133             return part::TEXT_CENTERED;
00134         }
00135     }
00136     return part::TEXT_LEFT;
00137 }
00138 
00139 void part::resolve_wml(const vconfig &cfg)
00140 {
00141     if(cfg.null()) {
00142         return;
00143     }
00144 
00145     if(cfg.has_attribute("background")) {
00146         background_file_ = cfg["background"].str();
00147     }
00148     if(cfg.has_attribute("scale_background")) {
00149         scale_background_ = cfg["scale_background"].to_bool(true);
00150     }
00151     if(cfg.has_attribute("show_title")) {
00152         show_title_ = cfg["show_title"].to_bool();
00153     }
00154     if(cfg.has_attribute("story")) {
00155         text_ = cfg["story"].str();
00156     }
00157     if(cfg.has_attribute("title")) {
00158         text_title_ = cfg["title"].str();
00159         if(!cfg.has_attribute("show_title")) {
00160             show_title_ = true;
00161         }
00162     }
00163     if(cfg.has_attribute("text_layout")) {
00164         text_block_loc_ = string_tblock_loc(cfg["text_layout"]);
00165     }
00166     if(cfg.has_attribute("title_alignment")) {
00167         title_alignment_ = string_title_align(cfg["title_alignment"]);
00168     }
00169     if(cfg.has_attribute("music")) {
00170         music_ = cfg["music"].str();
00171     }
00172     if(cfg.has_attribute("sound")) {
00173         sound_ = cfg["sound"].str();
00174     }
00175 
00176     // Execution flow/branching/[image]
00177     for(vconfig::all_children_iterator i = cfg.ordered_begin(); i != cfg.ordered_end(); ++ i) {
00178         // i->first and i->second are goddamn temporaries; do not make references
00179         const std::string key = i->first;
00180         const vconfig node = i->second;
00181 
00182         // [image]
00183         if(key == "image") {
00184             floating_images_.push_back(node.get_parsed_config());
00185         }
00186         // [if]
00187         else if(key == "if") {
00188             const std::string branch_label =
00189                 game_events::conditional_passed(node) ?
00190                 "then" : "else";
00191             if(node.has_child(branch_label)) {
00192                 const vconfig branch = node.child(branch_label);
00193                 resolve_wml(branch);
00194             }
00195         }
00196         // [switch]
00197         else if(key == "switch") {
00198             const std::string var_name = node["variable"];
00199             const std::string var_actual_value = resources::state_of_game->get_variable_const(var_name);
00200             bool case_not_found = true;
00201 
00202             for(vconfig::all_children_iterator j = node.ordered_begin(); j != node.ordered_end(); ++j) {
00203                 if(j->first != "case") continue;
00204 
00205                 // Enter all matching cases.
00206                 const std::string var_expected_value = (j->second)["value"];
00207                 if(var_actual_value == var_expected_value) {
00208                     case_not_found = false;
00209                     resolve_wml(j->second);
00210                 }
00211             }
00212 
00213             if(case_not_found) {
00214                 for(vconfig::all_children_iterator j = node.ordered_begin(); j != node.ordered_end(); ++j) {
00215                     if(j->first != "else") continue;
00216 
00217                     // Enter all elses.
00218                     resolve_wml(j->second);
00219                 }
00220             }
00221         }
00222         // [deprecated_message]
00223         else if(key == "deprecated_message") {
00224             // Won't appear until the scenario start event finishes.
00225             game_events::handle_deprecated_message(node.get_parsed_config());
00226         }
00227         // [wml_message]
00228         else if(key == "wml_message") {
00229             // Pass to game events handler. As with [deprecated_message],
00230             // it won't appear until the scenario start event is complete.
00231             game_events::handle_wml_log_message(node.get_parsed_config());
00232         }
00233     }
00234 }
00235 
00236 } // end namespace storyscreen
00237 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:10 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs