Skip to content

Commit

Permalink
Hide battery status if the device doesn't have a battery
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jyrkive committed Oct 30, 2018
1 parent 8d5dfea commit 34d93d4
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 36 deletions.
3 changes: 3 additions & 0 deletions data/game_config.cfg
Expand Up @@ -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"

Expand Down
13 changes: 2 additions & 11 deletions data/themes/_initial.cfg
Expand Up @@ -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]
Expand Down
15 changes: 3 additions & 12 deletions data/themes/default.cfg
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/game_config.cpp
Expand Up @@ -195,6 +195,9 @@ std::string
// orbs and hp/xp bar
orb,
energy,
// top bar icons
battery_icon,
time_icon,
// flags
flag,
flag_icon,
Expand Down Expand Up @@ -328,6 +331,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();

Expand Down
3 changes: 3 additions & 0 deletions src/game_config.hpp
Expand Up @@ -104,6 +104,9 @@ namespace game_config
// orbs and hp/xp bar
orb,
energy,
// top bar icons
battery_icon,
time_icon,
// flags
flag,
flag_icon,
Expand Down
22 changes: 14 additions & 8 deletions src/reports.cpp
Expand Up @@ -36,6 +36,7 @@
#include <ctime>
#include <iomanip>
#include <boost/dynamic_bitset.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>

static void add_text(config &report, const std::string &text,
Expand Down Expand Up @@ -1542,6 +1543,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()
Expand All @@ -1550,20 +1554,22 @@ REPORT_GENERATOR(report_clock, /*rc*/)

std::time_t t = std::time(nullptr);
ss << std::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)
Expand Down
47 changes: 44 additions & 3 deletions src/theme.cpp
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 16 additions & 2 deletions src/theme.hpp
Expand Up @@ -23,6 +23,7 @@
#include "config.hpp"
#include "generic_event.hpp"

#include <memory>
#include <SDL_rect.h>

struct _rect { std::size_t x1,y1,x2,y2; };
Expand All @@ -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_; }

Expand Down Expand Up @@ -141,6 +142,15 @@ 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 Expand Up @@ -238,6 +248,10 @@ class theme
};

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);

Expand Down Expand Up @@ -298,7 +312,7 @@ class theme
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_;

Expand Down

0 comments on commit 34d93d4

Please sign in to comment.