From 5bcd894429e7a85164458261965a4c9e3c1f2d2e Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sun, 17 Jan 2021 04:56:47 +0300 Subject: [PATCH] Added initial implementation of arc painter. --- CMakeLists.txt | 2 + ui/paint/arcs.cpp | 92 ++++++++++++++++++++++++++++++++++++++++ ui/paint/arcs.h | 68 +++++++++++++++++++++++++++++ ui/widgets/widgets.style | 11 +++++ 4 files changed, 173 insertions(+) create mode 100644 ui/paint/arcs.cpp create mode 100644 ui/paint/arcs.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 56b575a..770ba1a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,6 +69,8 @@ PRIVATE ui/layers/layer_manager.h ui/layers/layer_widget.cpp ui/layers/layer_widget.h + ui/paint/arcs.cpp + ui/paint/arcs.h ui/paint/blob.cpp ui/paint/blob.h ui/paint/blobs.cpp diff --git a/ui/paint/arcs.cpp b/ui/paint/arcs.cpp new file mode 100644 index 0000000..9538b99 --- /dev/null +++ b/ui/paint/arcs.cpp @@ -0,0 +1,92 @@ +// This file is part of Desktop App Toolkit, +// a set of libraries for developing nice desktop applications. +// +// For license and copyright information please follow this link: +// https://github.com/desktop-app/legal/blob/master/LEGAL +// +#include "ui/paint/arcs.h" + +#include "ui/painter.h" + +namespace Ui::Paint { + +ArcsAnimation::ArcsAnimation( + const style::ArcsAnimation &st, + int count, + VerticalDirection direction, + int centerX, + int startY) +: _st(st) +, _center(centerX) +, _start(startY) +, _horizontalDirection(HorizontalDirection::None) +, _verticalDirection(direction) +, _startAngle((st.deltaAngle + + ((direction == VerticalDirection::Up) ? 90 : 270)) * 16) +, _spanAngle(-st.deltaAngle * 2 * 16) { + initArcs(count); +} + +ArcsAnimation::ArcsAnimation( + const style::ArcsAnimation &st, + int count, + HorizontalDirection direction, + int startX, + int centerY) +: _st(st) +, _center(centerY) +, _start(startX) +, _horizontalDirection(direction) +, _verticalDirection(VerticalDirection::None) +, _startAngle((st.deltaAngle + + ((direction == HorizontalDirection::Left) ? 180 : 0)) * 16) +, _spanAngle(-st.deltaAngle * 2 * 16) { + initArcs(count); +} + +void ArcsAnimation::initArcs(int count) { + _arcs.reserve(count); + + for (auto i = 0; i < count; i++) { + auto arc = Arc{ .rect = computeArcRect(i) }; + _arcs.push_back(std::move(arc)); + } +} + +QRectF ArcsAnimation::computeArcRect(int index) const { + const auto w = _st.startWidth + _st.deltaWidth * index; + const auto h = _st.startHeight + _st.deltaHeight * index; + if (_horizontalDirection != HorizontalDirection::None) { + auto rect = QRectF(0, _center - h / 2.0, w, h); + if (_horizontalDirection == HorizontalDirection::Right) { + rect.moveRight(_start + index * _st.space); + } else { + rect.moveLeft(_start - index * _st.space); + } + return rect; + } else if (_verticalDirection != VerticalDirection::None) { + auto rect = QRectF(_center - w / 2.0, 0, w, h); + if (_verticalDirection == VerticalDirection::Up) { + rect.moveTop(_start - index * _st.space); + } else { + rect.moveBottom(_start + index * _st.space); + } + return rect; + } + return QRectF(); +} + +void ArcsAnimation::paint(Painter &p, std::optional colorOverride) { + PainterHighQualityEnabler hq(p); + QPen pen; + pen.setWidth(_st.stroke); + pen.setCapStyle(Qt::RoundCap); + pen.setColor(colorOverride ? (*colorOverride) : _st.fg->c); + p.setPen(pen); + for (const auto &arc : _arcs) { + p.drawArc(arc.rect, _startAngle, _spanAngle); + } +} + + +} // namespace Ui::Paint diff --git a/ui/paint/arcs.h b/ui/paint/arcs.h new file mode 100644 index 0000000..1c4988e --- /dev/null +++ b/ui/paint/arcs.h @@ -0,0 +1,68 @@ +// This file is part of Desktop App Toolkit, +// a set of libraries for developing nice desktop applications. +// +// For license and copyright information please follow this link: +// https://github.com/desktop-app/legal/blob/master/LEGAL +// +#pragma once + +#include "styles/style_widgets.h" + +class Painter; + +namespace Ui::Paint { + +class ArcsAnimation { +public: + + enum class HorizontalDirection { + Left, + Right, + None, + }; + + enum class VerticalDirection { + Up, + Down, + None, + }; + + ArcsAnimation( + const style::ArcsAnimation &st, + int count, + VerticalDirection direction, + int centerX, + int startY); + + ArcsAnimation( + const style::ArcsAnimation &st, + int count, + HorizontalDirection direction, + int startX, + int centerY); + + void paint( + Painter &p, + std::optional colorOverride = std::nullopt); + +private: + struct Arc { + QRectF rect; + }; + + void initArcs(int count); + QRectF computeArcRect(int index) const; + + const style::ArcsAnimation &_st; + const int _center; + const int _start; + const HorizontalDirection _horizontalDirection; + const VerticalDirection _verticalDirection; + const int _startAngle; + const int _spanAngle; + + std::vector _arcs; + +}; + +} // namespace Ui::Paint diff --git a/ui/widgets/widgets.style b/ui/widgets/widgets.style index 11671a9..36f0e4d 100644 --- a/ui/widgets/widgets.style +++ b/ui/widgets/widgets.style @@ -410,6 +410,17 @@ CrossLineAnimation { stroke: pixels; } +ArcsAnimation { + fg: color; + stroke: pixels; + space: pixels; + deltaAngle: int; + deltaHeight: pixels; + deltaWidth: pixels; + startHeight: pixels; + startWidth: pixels; +} + MultiSelectItem { padding: margins; maxWidth: pixels;