Hide battery status if the device doesn't have a battery

The strategy is two-fold. We move the rendering of the icon into the
battery status report where it can be disabled at runtime, and allow the
countdown/clock to move to the position of the battery status if the device
doesn't have a battery. I also moved rendering of the clock icon to the
time report to allow the icon to move together with the text.

I needed to change theme::status_ to store pointers to status items because
otherwise the countdown object loses its type when inserted to the map. And
to be able to use std::unique_ptr inside the theme class, I had to make the
class non-copyable and movable.
This commit is contained in:
Jyrki Vesterinen 2018-08-11 23:25:33 +03:00
parent b36295ca9b
commit e58e57d1d0
8 changed files with 91 additions and 36 deletions

View file

@ -69,6 +69,9 @@
orb="misc/orb.png"
energy="misc/bar-energy.png"
battery_icon="themes/battery.png"
time_icon="themes/sand-clock.png"
flag="flags/flag-[1~4].png:150"
flag_icon="flags/flag-icon.png"

View file

@ -387,23 +387,14 @@
#define COUNTDOWN_THEME
{STATUS_BOX_BORDERLESS +3 =+0 +90 +15 timeout battery-box-topright fixed fixed}
[label]
id=time-icon
#icon=themes/units.png
icon=themes/sand-clock.png
ref=timeout-box-center
rect="=+1,=-1,+17,+17"
xanchor=fixed
yanchor=fixed
[/label]
#enddef
#define COUNTDOWN_THEME_STATUS FONT_SMALL_SIZE
[report_countdown]
id=report_timeout
font_size={FONT_SMALL_SIZE}
ref=time-icon
rect="+0,=-2,+65,+20"
ref=timeout-box-center
rect="=,=-3,+80,+20"
xanchor=fixed
yanchor=fixed
[/report_countdown]

View file

@ -107,7 +107,7 @@
{STATUS_BOX_BORDERLESS +3 =+0 +70 +15 villages gold-box-topright fixed fixed}
{STATUS_BOX_BORDERLESS +3 =+0 +65 +15 units villages-box-topright fixed fixed}
{STATUS_BOX_BORDERLESS +3 =+0 +85 +15 upkeep units-box-topright fixed fixed}
{STATUS_BOX_BORDERLESS +3 =+0 +85 +15 income upkeep-box-topright fixed fixed}
{STATUS_BOX_BORDERLESS +3 =+0 +60 +15 income upkeep-box-topright fixed fixed}
{STATUS_BOX_BORDERLESS +3 =+0 +85 +15 battery income-box-topright fixed fixed}
{COUNTDOWN_THEME}
[menu]
@ -217,15 +217,6 @@
xanchor=fixed
yanchor=fixed
[/label]
[label]
id=battery-icon
icon=themes/battery.png
text= _ "battery"
ref=battery-box-center
rect="=+1,=-1,+17,+17"
xanchor=fixed
yanchor=fixed
[/label]
[status]
# The size of these rectangles only accommodates hex coordinates
@ -358,8 +349,8 @@
[battery]
id=status-battery
font_size={DEFAULT_FONT_SMALL}
ref=battery-icon
rect="+4,=-2,+60,+16"
ref=battery-box-center
rect="=+5,=-3,+80,+18"
xanchor=fixed
yanchor=fixed
prefix="" #wmllint: ignore

View file

@ -212,6 +212,9 @@ std::string
// orbs and hp/xp bar
orb,
energy,
// top bar icons
battery_icon,
time_icon,
// flags
flag,
flag_icon,
@ -344,6 +347,9 @@ void load_config(const config &v)
orb = i["orb"].str();
energy = i["energy"].str();
battery_icon = i["battery_icon"].str();
time_icon = i["time_icon"].str();
flag = i["flag"].str();
flag_icon = i["flag_icon"].str();

View file

@ -101,6 +101,9 @@ namespace game_config
// orbs and hp/xp bar
orb,
energy,
// top bar icons
battery_icon,
time_icon,
// flags
flag,
flag_icon,

View file

@ -36,6 +36,7 @@
#include <ctime>
#include <iomanip>
#include <boost/dynamic_bitset.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include "utils/io.hpp"
@ -1533,6 +1534,9 @@ REPORT_GENERATOR(edit_left_button_function)
REPORT_GENERATOR(report_clock, /*rc*/)
{
config report;
add_image(report, game_config::images::time_icon, "");
std::ostringstream ss;
const char* format = preferences::use_twelve_hour_clock_format()
@ -1541,20 +1545,22 @@ REPORT_GENERATOR(report_clock, /*rc*/)
time_t t = std::time(nullptr);
ss << utils::put_time(std::localtime(&t), format);
add_text(report, ss.str(), _("Clock"));
return text_report(ss.str(), _("Clock"));
return report;
}
REPORT_GENERATOR(battery, /*rc*/)
{
std::ostringstream ss;
if (desktop::battery_info::does_device_have_battery()) {
ss << boost::lexical_cast<std::string>(static_cast<int>(desktop::battery_info::get_battery_percentage())) + "%";
}
return text_report(ss.str(), _("Battery"));
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"));
}
return report;
}
REPORT_GENERATOR(report_countdown, rc)

View file

@ -16,6 +16,8 @@
#include "theme.hpp"
#include "desktop/battery_info.hpp"
#include "display.hpp"
#include "gettext.hpp"
#include "hotkey/hotkey_command.hpp"
#include "hotkey/hotkey_item.hpp"
@ -24,6 +26,8 @@
#include "serialization/string_utils.hpp"
#include "wml_exception.hpp"
#include <utility>
static lg::log_domain log_display("display");
#define DBG_DP LOG_STREAM(debug, log_display)
#define LOG_DP LOG_STREAM(info, log_display)
@ -456,6 +460,16 @@ 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()) {
return status_item::location(screen);
} else {
return display::get_singleton()->get_theme().
get_status_item("battery")->location(screen);
}
}
theme::panel::panel(const config& cfg)
: object(cfg)
, image_(cfg["image"])
@ -583,6 +597,29 @@ theme::theme(const config& cfg, const SDL_Rect& screen)
set_resolution(screen);
}
theme& theme::operator=(theme&& other)
{
theme_reset_event_ = other.theme_reset_event_;
known_themes = std::move(other.known_themes);
cur_theme = std::move(other.cur_theme);
cfg_ = std::move(other.cfg_);
panels_ = std::move(other.panels_);
labels_ = std::move(other.labels_);
menus_ = std::move(other.menus_);
actions_ = std::move(other.actions_);
sliders_ = std::move(other.sliders_);
context_ = other.context_;
action_context_ = other.action_context_;
status_ = std::move(other.status_);
main_map_ = other.main_map_;
mini_map_ = other.mini_map_;
unit_image_ = other.unit_image_;
palette_ = other.palette_;
border_ = other.border_;
return *this;
}
bool theme::set_resolution(const SDL_Rect& screen)
{
bool result = false;
@ -668,7 +705,11 @@ 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()) {
status_.emplace(i.key, status_item(i.cfg));
if(i.key != "report_countdown") {
status_[i.key].reset(new status_item(i.cfg));
} else {
status_[i.key].reset(new countdown(i.cfg));
}
}
if(const config& unit_image_cfg = status_cfg.child("unit_image")) {
unit_image_ = object(unit_image_cfg);
@ -862,9 +903,9 @@ theme::object& theme::find_element(const std::string& id)
const theme::status_item* theme::get_status_item(const std::string& key) const
{
const std::map<std::string, status_item>::const_iterator i = status_.find(key);
const auto& i = status_.find(key);
if(i != status_.end())
return &i->second;
return i->second.get();
else
return nullptr;
}

View file

@ -23,6 +23,7 @@
#include "config.hpp"
#include "generic_event.hpp"
#include <memory>
#include <SDL_rect.h>
struct _rect { size_t x1,y1,x2,y2; };
@ -44,7 +45,7 @@ class theme
object(const config& cfg);
virtual ~object() { }
SDL_Rect& location(const SDL_Rect& screen) const;
virtual SDL_Rect& location(const SDL_Rect& screen) const;
const SDL_Rect& get_location() const { return loc_; }
const std::string& get_id() const { return id_; }
@ -141,6 +142,15 @@ public:
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:
@ -238,6 +248,10 @@ public:
};
explicit theme(const config& cfg, const SDL_Rect& screen);
theme(const theme&) = delete;
theme& operator=(const theme&) = delete;
theme& operator=(theme&&);
bool set_resolution(const SDL_Rect& screen);
void modify(const config &cfg);
@ -298,7 +312,7 @@ private:
menu context_;
action action_context_;
std::map<std::string,status_item> status_;
std::map<std::string, std::unique_ptr<status_item>> status_;
object main_map_, mini_map_, unit_image_, palette_;