1// Aseprite UI Library
2// Copyright (C) 2018-2022 Igara Studio S.A.
3// Copyright (C) 2001-2016 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "ui/overlay_manager.h"
13
14#include "ui/manager.h"
15#include "ui/overlay.h"
16
17#include <algorithm>
18
19namespace ui {
20
21static bool zorder_less_than(const OverlayRef& a, const OverlayRef& b) {
22 return *a < *b;
23}
24
25OverlayManager* OverlayManager::m_singleton = nullptr;
26
27OverlayManager* OverlayManager::instance()
28{
29 if (m_singleton == nullptr)
30 m_singleton = new OverlayManager;
31 return m_singleton;
32}
33
34void OverlayManager::destroyInstance()
35{
36 delete m_singleton;
37}
38
39OverlayManager::OverlayManager()
40{
41}
42
43OverlayManager::~OverlayManager()
44{
45}
46
47void OverlayManager::addOverlay(const OverlayRef& overlay)
48{
49 iterator it = std::lower_bound(begin(), end(), overlay, zorder_less_than);
50 m_overlays.insert(it, overlay);
51}
52
53void OverlayManager::removeOverlay(const OverlayRef& overlay)
54{
55 if (overlay)
56 overlay->restoreOverlappedArea(gfx::Rect());
57
58 iterator it = std::find(begin(), end(), overlay);
59 ASSERT(it != end());
60 if (it != end())
61 m_overlays.erase(it);
62}
63
64void OverlayManager::restoreOverlappedAreas(const gfx::Rect& restoreBounds)
65{
66 if (m_overlays.empty())
67 return;
68
69 for (auto& overlay : *this)
70 overlay->restoreOverlappedArea(restoreBounds);
71}
72
73void OverlayManager::drawOverlays()
74{
75 if (m_overlays.empty())
76 return;
77
78 for (auto& overlay : *this)
79 overlay->captureOverlappedArea();
80
81 for (auto& overlay : *this)
82 overlay->drawOverlay();
83}
84
85} // namespace ui
86