From 743714b94df001ac9126bbfc932dc235eb5cdd50 Mon Sep 17 00:00:00 2001 From: Tobias Markus Date: Fri, 29 Dec 2023 21:27:53 +0100 Subject: [PATCH] Move some direction functions from scripting class to game object class --- src/object/gradient.cpp | 60 +++++++++++++++++++++++++++----------- src/object/gradient.hpp | 2 ++ src/scripting/gradient.cpp | 26 ++--------------- 3 files changed, 47 insertions(+), 41 deletions(-) diff --git a/src/object/gradient.cpp b/src/object/gradient.cpp index f0210442b2f..7b659f59589 100644 --- a/src/object/gradient.cpp +++ b/src/object/gradient.cpp @@ -67,26 +67,11 @@ Gradient::Gradient(const ReaderMapping& reader) : std::string direction; if (reader.get("direction", direction)) { - if (direction == "horizontal") - { - m_gradient_direction = HORIZONTAL; - } - else if (direction == "horizontal_sector") - { - m_gradient_direction = HORIZONTAL_SECTOR; - } - else if (direction == "vertical_sector") - { - m_gradient_direction = VERTICAL_SECTOR; - } - else - { - m_gradient_direction = VERTICAL; - } + set_direction(direction); } else { - m_gradient_direction = VERTICAL; + set_direction(VERTICAL); } if (reader.get("top_color", bkgd_top_color)) { @@ -209,12 +194,53 @@ Gradient::fade_gradient(const Color& top, const Color& bottom, float time) m_fade_time = time; } +std::string +Gradient::get_direction_string() const +{ + if (m_gradient_direction == HORIZONTAL) + return "horizontal"; + if (m_gradient_direction == VERTICAL) + return "vertical"; + if (m_gradient_direction == HORIZONTAL_SECTOR) + return "horizontal_sector"; + if (m_gradient_direction == VERTICAL_SECTOR) + return "vertical_sector"; + + return nullptr; +} + void Gradient::set_direction(const GradientDirection& direction) { m_gradient_direction = direction; } +void +Gradient::set_direction(const std::string& direction) +{ + if (direction == "horizontal") + { + m_gradient_direction = HORIZONTAL; + } + else if (direction == "horizontal_sector") + { + m_gradient_direction = HORIZONTAL_SECTOR; + } + else if (direction == "vertical_sector") + { + m_gradient_direction = VERTICAL_SECTOR; + } + else if (direction == "vertical") + { + m_gradient_direction = VERTICAL; + } + else + { + log_info << "Invalid direction for gradient \"" << direction << "\""; + m_gradient_direction = VERTICAL; + } +} + void Gradient::draw(DrawingContext& context) { diff --git a/src/object/gradient.hpp b/src/object/gradient.hpp index 6539a6d5cf0..bb654963dc6 100644 --- a/src/object/gradient.hpp +++ b/src/object/gradient.hpp @@ -57,7 +57,9 @@ class Gradient final : Color get_gradient_bottom() const { return m_gradient_bottom; } GradientDirection get_direction() const { return m_gradient_direction; } + std::string get_direction_string() const; void set_direction(const GradientDirection& direction); + void set_direction(const std::string& direction); void set_layer(int layer) { m_layer = layer; } int get_layer() const { return m_layer; } diff --git a/src/scripting/gradient.cpp b/src/scripting/gradient.cpp index d73d8775864..0df9839a2f8 100644 --- a/src/scripting/gradient.cpp +++ b/src/scripting/gradient.cpp @@ -24,36 +24,14 @@ void Gradient::set_direction(const std::string& direction) { SCRIPT_GUARD_VOID; - - if (direction == "horizontal") - object.set_direction(GradientDirection::HORIZONTAL); - else if (direction == "vertical") - object.set_direction(GradientDirection::VERTICAL); - else if (direction == "horizontal_sector") - object.set_direction(GradientDirection::HORIZONTAL_SECTOR); - else if (direction == "vertical_sector") - object.set_direction(GradientDirection::VERTICAL_SECTOR); - else - log_info << "Invalid direction for gradient \"" << direction << "\""; + object.set_direction(direction); } std::string Gradient::get_direction() const { SCRIPT_GUARD_DEFAULT; - - auto direction = object.get_direction(); - - if (direction == GradientDirection::HORIZONTAL) - return "horizontal"; - if (direction == GradientDirection::VERTICAL) - return "vertical"; - if (direction == GradientDirection::HORIZONTAL_SECTOR) - return "horizontal_sector"; - if (direction == GradientDirection::VERTICAL_SECTOR) - return "vertical_sector"; - - return nullptr; + return object.get_direction_string(); } void