Skip to content

Commit

Permalink
Better implementation of placing the clock (resolves #3582)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrkive committed Nov 18, 2018
1 parent 4285371 commit e9a6b95
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 37 deletions.
2 changes: 1 addition & 1 deletion data/themes/_initial.cfg
Expand Up @@ -391,7 +391,7 @@

#define COUNTDOWN_THEME_STATUS FONT_SMALL_SIZE
[report_countdown]
id=report_timeout
id=report_countdown
font_size={FONT_SMALL_SIZE}
ref=timeout-box-center
rect="=,=-3,+80,+20"
Expand Down
13 changes: 13 additions & 0 deletions data/themes/default.cfg
Expand Up @@ -501,6 +501,19 @@
[/unit_weapons]
[/status]

# If the device has no battery, remove the battery charge indicator
# and move the timer to its position.
[no_battery]
[remove]
id=battery
[/remove]
[change]
id=report_countdown
ref=battery-box-center
rect="=+5,=-3,+80,+18"
[/change]
[/no_battery]

{REPLAY_THEME {DEFAULT_FONT_SMALL}}
[/resolution]

Expand Down
6 changes: 2 additions & 4 deletions src/reports.cpp
Expand Up @@ -1569,10 +1569,8 @@ REPORT_GENERATOR(battery, /*rc*/)
{
config report;

if(desktop::battery_info::does_device_have_battery()) {
add_image(report, game_config::images::battery_icon, "");
add_text(report, (boost::format("%.0f %%") % desktop::battery_info::get_battery_percentage()).str(), _("Battery"));
}
add_image(report, game_config::images::battery_icon, "");
add_text(report, (boost::format("%.0f %%") % desktop::battery_info::get_battery_percentage()).str(), _("Battery"));

return report;
}
Expand Down
50 changes: 27 additions & 23 deletions src/theme.cpp
Expand Up @@ -26,6 +26,7 @@
#include "serialization/string_utils.hpp"
#include "wml_exception.hpp"

#include <sstream>
#include <utility>

static lg::log_domain log_display("display");
Expand Down Expand Up @@ -460,19 +461,6 @@ theme::status_item::status_item(const config& cfg)
}
}

SDL_Rect& theme::countdown::location(const SDL_Rect& screen) const
{
if(!desktop::battery_info::does_device_have_battery()) {
const object* battery = display::get_singleton()->get_theme().
get_status_item("battery");
if(battery != nullptr) {
return battery->location(screen);
}
}

return status_item::location(screen);
}

theme::panel::panel(const config& cfg)
: object(cfg)
, image_(cfg["image"])
Expand Down Expand Up @@ -708,11 +696,7 @@ void theme::add_object(const config& cfg)

if(const config& status_cfg = cfg.child("status")) {
for(const config::any_child& i : status_cfg.all_children_range()) {
if(i.key != "report_countdown") {
status_[i.key].reset(new status_item(i.cfg));
} else {
status_[i.key].reset(new countdown(i.cfg));
}
status_[i.key].reset(new status_item(i.cfg));
}
if(const config& unit_image_cfg = status_cfg.child("unit_image")) {
unit_image_ = object(unit_image_cfg);
Expand Down Expand Up @@ -771,40 +755,54 @@ void theme::add_object(const config& cfg)
if(const config& c = cfg.child("main_map_border")) {
border_ = border_t(c);
}

if(!desktop::battery_info::does_device_have_battery()) {
if(const config& c = cfg.child("no_battery")) {
modify(c);
}
}
}

void theme::remove_object(const std::string& id)
{
for(std::vector<theme::panel>::iterator p = panels_.begin(); p != panels_.end(); ++p) {
if(status_.erase(id) > 0u) {
return;
}

for(auto p = panels_.begin(); p != panels_.end(); ++p) {
if(p->get_id() == id) {
panels_.erase(p);
return;
}
}
for(std::vector<theme::label>::iterator l = labels_.begin(); l != labels_.end(); ++l) {
for(auto l = labels_.begin(); l != labels_.end(); ++l) {
if(l->get_id() == id) {
labels_.erase(l);
return;
}
}
for(std::vector<theme::menu>::iterator m = menus_.begin(); m != menus_.end(); ++m) {
for(auto m = menus_.begin(); m != menus_.end(); ++m) {
if(m->get_id() == id) {
menus_.erase(m);
return;
}
}
for(std::vector<theme::action>::iterator a = actions_.begin(); a != actions_.end(); ++a) {
for(auto a = actions_.begin(); a != actions_.end(); ++a) {
if(a->get_id() == id) {
actions_.erase(a);
return;
}
}
for(std::vector<theme::slider>::iterator s = sliders_.begin(); s != sliders_.end(); ++s) {
for(auto s = sliders_.begin(); s != sliders_.end(); ++s) {
if(s->get_id() == id) {
sliders_.erase(s);
return;
}
}

std::stringstream stream;
stream << "theme object " << id << " not found";
throw config::error(stream.str());
}

void theme::set_object_location(theme::object& element, std::string rect_str, std::string ref_id)
Expand Down Expand Up @@ -869,6 +867,12 @@ theme::object& theme::find_element(const std::string& id)
{
static theme::object empty_object;
theme::object* res = &empty_object;

auto status_item_it = status_.find(id);
if(status_item_it != status_.end()) {
res = status_item_it->second.get();
}

for(std::vector<theme::panel>::iterator p = panels_.begin(); p != panels_.end(); ++p) {
if(p->get_id() == id) {
res = &(*p);
Expand Down
9 changes: 0 additions & 9 deletions src/theme.hpp
Expand Up @@ -142,15 +142,6 @@ class theme
color_t font_rgb_;
};

class countdown : public status_item
{
public:
explicit countdown(const config& cfg) : status_item(cfg)
{}

SDL_Rect& location(const SDL_Rect& screen) const override;
};

class panel : public object
{
public:
Expand Down

0 comments on commit e9a6b95

Please sign in to comment.