From 92f69a228d381f437301fab4089f5423f6187a47 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 1 Aug 2023 14:20:42 +0300 Subject: [PATCH] Add td_api::storyAreaPosition. --- CMakeLists.txt | 3 ++ td/generate/scheme/td_api.tl | 9 ++++ td/telegram/MediaAreaCoordinates.cpp | 75 ++++++++++++++++++++++++++++ td/telegram/MediaAreaCoordinates.h | 58 +++++++++++++++++++++ td/telegram/MediaAreaCoordinates.hpp | 45 +++++++++++++++++ 5 files changed, 190 insertions(+) create mode 100644 td/telegram/MediaAreaCoordinates.cpp create mode 100644 td/telegram/MediaAreaCoordinates.h create mode 100644 td/telegram/MediaAreaCoordinates.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ac02d40fd..b3987f3f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -386,6 +386,7 @@ set(TDLIB_SOURCE td/telegram/Location.cpp td/telegram/logevent/LogEventHelper.cpp td/telegram/Logging.cpp + td/telegram/MediaAreaCoordinates.cpp td/telegram/MessageContent.cpp td/telegram/MessageContentType.cpp td/telegram/MessageDb.cpp @@ -659,6 +660,7 @@ set(TDLIB_SOURCE td/telegram/logevent/LogEventHelper.h td/telegram/logevent/SecretChatEvent.h td/telegram/Logging.h + td/telegram/MediaAreaCoordinates.h td/telegram/MessageContent.h td/telegram/MessageContentType.h td/telegram/MessageCopyOptions.h @@ -832,6 +834,7 @@ set(TDLIB_SOURCE td/telegram/Game.hpp td/telegram/InputInvoice.hpp td/telegram/InputMessageText.hpp + td/telegram/MediaAreaCoordinates.hpp td/telegram/MessageEntity.hpp td/telegram/MessageExtendedMedia.hpp td/telegram/MessageReaction.hpp diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 7664d9289..135e4329a 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4907,6 +4907,15 @@ messageLink link:string is_public:Bool = MessageLink; messageLinkInfo is_public:Bool chat_id:int53 message_thread_id:int53 message:message media_timestamp:int32 for_album:Bool = MessageLinkInfo; +//@description Describes a clickable rectangle area on a story media +//@x_percentage The abscissa of the rectangle's center, as a percentage of the media width +//@y_percentage The ordinate of the rectangle's center, as a percentage of the media height +//@width_percentage The width of the rectangle, as a percentage of the media width +//@height_percentage The ordinate of the rectangle's center, as a percentage of the media height +//@rotation_angle Clockwise rotation angle of the rectangle, in degrees; 0-360 +storyAreaPosition x_percentage:double y_percentage:double width_percentage:double height_percentage:double rotation_angle:double = StoryAreaPosition; + + //@description Describes a video file sent in a story //@duration Duration of the video, in seconds //@width Video width diff --git a/td/telegram/MediaAreaCoordinates.cpp b/td/telegram/MediaAreaCoordinates.cpp new file mode 100644 index 000000000..bb20c2d15 --- /dev/null +++ b/td/telegram/MediaAreaCoordinates.cpp @@ -0,0 +1,75 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/telegram/MediaAreaCoordinates.h" + +#include + +namespace td { + +static double fix_double(double &value, double max_value = 100.0) { + if (!std::isfinite(value) || value < 0.0) { + return 0.0; + } + if (value > max_value) { + return max_value; + } + return value; +} + +void MediaAreaCoordinates::init(double x, double y, double width, double height, double rotation_angle) { + x_ = fix_double(x); + y_ = fix_double(y); + width_ = fix_double(width); + height_ = fix_double(height); + rotation_angle_ = fix_double(rotation_angle, 360.0); +} + +MediaAreaCoordinates::MediaAreaCoordinates( + const telegram_api::object_ptr &coordinates) { + if (coordinates == nullptr) { + return; + } + init(coordinates->x_, coordinates->y_, coordinates->w_, coordinates->h_, coordinates->rotation_); +} + +MediaAreaCoordinates::MediaAreaCoordinates(const td_api::object_ptr &position) { + if (position == nullptr) { + return; + } + + init(position->x_percentage_, position->y_percentage_, position->width_percentage_, position->height_percentage_, + position->rotation_angle_); +} + +td_api::object_ptr MediaAreaCoordinates::get_story_area_position_object() const { + CHECK(is_valid()); + return td_api::make_object(x_, y_, width_, height_, rotation_angle_); +} + +telegram_api::object_ptr MediaAreaCoordinates::get_input_media_area_coordinates() + const { + CHECK(is_valid()); + return telegram_api::make_object(x_, y_, width_, height_, rotation_angle_); +} + +bool operator==(const MediaAreaCoordinates &lhs, const MediaAreaCoordinates &rhs) { + return std::abs(lhs.x_ - rhs.x_) < 1e-6 && std::abs(lhs.y_ - rhs.y_) < 1e-6 && + std::abs(lhs.width_ - rhs.width_) < 1e-6 && std::abs(lhs.height_ - rhs.height_) < 1e-6 && + std::abs(lhs.rotation_angle_ - rhs.rotation_angle_) < 1e-6; +} + +bool operator!=(const MediaAreaCoordinates &lhs, const MediaAreaCoordinates &rhs) { + return !(lhs == rhs); +} + +StringBuilder &operator<<(StringBuilder &string_builder, const MediaAreaCoordinates &coordinates) { + return string_builder << "StoryAreaPosition[" << coordinates.x_ << ", " << coordinates.y_ << ", " + << coordinates.width_ << ", " << coordinates.height_ << ", " << coordinates.rotation_angle_ + << ']'; +} + +} // namespace td diff --git a/td/telegram/MediaAreaCoordinates.h b/td/telegram/MediaAreaCoordinates.h new file mode 100644 index 000000000..9b4dd1108 --- /dev/null +++ b/td/telegram/MediaAreaCoordinates.h @@ -0,0 +1,58 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" + +#include "td/utils/common.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +class MediaAreaCoordinates { + double x_ = 0.0; + double y_ = 0.0; + double width_ = 0.0; + double height_ = 0.0; + double rotation_angle_ = 0.0; + + friend bool operator==(const MediaAreaCoordinates &lhs, const MediaAreaCoordinates &rhs); + friend bool operator!=(const MediaAreaCoordinates &lhs, const MediaAreaCoordinates &rhs); + + friend StringBuilder &operator<<(StringBuilder &string_builder, const MediaAreaCoordinates &coordinates); + + void init(double x, double y, double width, double height, double rotation_angle); + + public: + MediaAreaCoordinates() = default; + + explicit MediaAreaCoordinates(const telegram_api::object_ptr &coordinates); + + explicit MediaAreaCoordinates(const td_api::object_ptr &position); + + td_api::object_ptr get_story_area_position_object() const; + + telegram_api::object_ptr get_input_media_area_coordinates() const; + + bool is_valid() const { + return width_ > 0.0 && height_ > 0.0; + } + + template + void store(StorerT &storer) const; + + template + void parse(ParserT &parser); +}; + +bool operator==(const MediaAreaCoordinates &lhs, const MediaAreaCoordinates &rhs); +bool operator!=(const MediaAreaCoordinates &lhs, const MediaAreaCoordinates &rhs); + +StringBuilder &operator<<(StringBuilder &string_builder, const MediaAreaCoordinates &coordinates); + +} // namespace td diff --git a/td/telegram/MediaAreaCoordinates.hpp b/td/telegram/MediaAreaCoordinates.hpp new file mode 100644 index 000000000..d30bfbdd2 --- /dev/null +++ b/td/telegram/MediaAreaCoordinates.hpp @@ -0,0 +1,45 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/MediaAreaCoordinates.h" + +#include "td/utils/tl_helpers.h" + +namespace td { + +template +void MediaAreaCoordinates::store(StorerT &storer) const { + using td::store; + BEGIN_STORE_FLAGS(); + END_STORE_FLAGS(); + store(x_, storer); + store(y_, storer); + store(width_, storer); + store(height_, storer); + store(rotation_angle_, storer); +} + +template +void MediaAreaCoordinates::parse(ParserT &parser) { + using td::parse; + BEGIN_PARSE_FLAGS(); + END_PARSE_FLAGS(); + double x; + double y; + double width; + double height; + double rotation_angle; + parse(x, parser); + parse(y, parser); + parse(width, parser); + parse(height, parser); + parse(rotation_angle, parser); + init(x, y, width, height, rotation_angle); +} + +} // namespace td