1 | // Aseprite |
2 | // Copyright (C) 2020-2022 Igara Studio S.A. |
3 | // |
4 | // This program is distributed under the terms of |
5 | // the End-User License Agreement for Aseprite. |
6 | |
7 | #ifdef HAVE_CONFIG_H |
8 | #include "config.h" |
9 | #endif |
10 | |
11 | #include "app/ui/user_data_view.h" |
12 | |
13 | #include "app/color.h" |
14 | #include "app/pref/preferences.h" |
15 | #include "app/ui/color_button.h" |
16 | #include "base/scoped_value.h" |
17 | #include "doc/user_data.h" |
18 | #include "ui/base.h" |
19 | #include "ui/entry.h" |
20 | #include "ui/grid.h" |
21 | #include "ui/label.h" |
22 | #include "ui/widget.h" |
23 | |
24 | namespace app { |
25 | |
26 | UserDataView::UserDataView(Option<bool>& visibility) |
27 | : m_visibility(visibility) |
28 | { |
29 | } |
30 | |
31 | void UserDataView::configureAndSet(const doc::UserData& userData, ui::Grid* parent) |
32 | { |
33 | base::ScopedValue<bool> switchSelf(m_selfUpdate, true, m_selfUpdate); |
34 | |
35 | if (!m_isConfigured) { |
36 | // Find the correct hspan to add to an arbitrary grid column count: |
37 | // Example with grid columns count = 4: |
38 | // |
39 | // <------------------ columns = 4 -----------------> |
40 | // |
41 | // | | | | | |
42 | // | Color: =========== color picker ============ |
43 | // | User Data: =========== text entry ============= |
44 | // |
45 | // | hspan1 = 1 | hspan2 = 3 | |
46 | std::vector<ui::Grid::Info> childrenInfo(parent->children().size()); |
47 | int i = 0; |
48 | int columnCount = 0; |
49 | for(auto child : parent->children()) { |
50 | childrenInfo[i] = parent->getChildInfo(child); |
51 | if (columnCount < childrenInfo[i].col) |
52 | columnCount = childrenInfo[i].col; |
53 | i++; |
54 | } |
55 | int hspan1 = 1; |
56 | int hspan2 = columnCount; |
57 | int vspan = 1; |
58 | parent->addChildInCell(colorLabel(), hspan1, vspan, ui::LEFT); |
59 | parent->addChildInCell(color(), hspan2, vspan, ui::HORIZONTAL); |
60 | parent->addChildInCell(entryLabel(), hspan1, vspan, ui::LEFT); |
61 | parent->addChildInCell(entry(), hspan2, vspan, ui::HORIZONTAL); |
62 | color()->Change.connect([this]{ onColorChange(); }); |
63 | entry()->Change.connect([this]{ onEntryChange(); }); |
64 | m_isConfigured = true; |
65 | } |
66 | m_userData = userData; |
67 | color_t c = userData.color(); |
68 | color()->setColor(Color::fromRgb(rgba_getr(c),rgba_getg(c), rgba_getb(c), rgba_geta(c))); |
69 | entry()->setText(m_userData.text()); |
70 | setVisible(isVisible()); |
71 | } |
72 | |
73 | void UserDataView::toggleVisibility() |
74 | { |
75 | setVisible(!isVisible()); |
76 | } |
77 | |
78 | void UserDataView::setVisible(bool state, bool saveAsDefault) |
79 | { |
80 | colorLabel()->setVisible(state); |
81 | color()->setVisible(state); |
82 | entryLabel()->setVisible(state); |
83 | entry()->setVisible(state); |
84 | if (saveAsDefault) |
85 | m_visibility.setValue(state); |
86 | } |
87 | |
88 | void UserDataView::onEntryChange() |
89 | { |
90 | if (entry()->text() != m_userData.text()) { |
91 | m_userData.setText(entry()->text()); |
92 | if (!m_selfUpdate) |
93 | UserDataChange(); |
94 | } |
95 | } |
96 | |
97 | void UserDataView::onColorChange() |
98 | { |
99 | color_t c = m_userData.color(); |
100 | app::Color oldColor = app::Color::fromRgb(rgba_getr(c), rgba_getg(c), rgba_getb(c), rgba_geta(c)); |
101 | app::Color newColor = color()->getColor(); |
102 | if (newColor != oldColor) { |
103 | m_userData.setColor(rgba(newColor.getRed(), |
104 | newColor.getGreen(), |
105 | newColor.getBlue(), |
106 | newColor.getAlpha())); |
107 | if (!m_selfUpdate) |
108 | UserDataChange(); |
109 | } |
110 | } |
111 | |
112 | } // namespace app |
113 | |