Add quick panel service boundary
This commit is contained in:
@@ -5,6 +5,194 @@
|
||||
#include "node_image.h"
|
||||
#include "app.h"
|
||||
|
||||
namespace {
|
||||
|
||||
class LegacyQuickUiServices final : public pp::app::QuickUiServices {
|
||||
public:
|
||||
LegacyQuickUiServices(NodePanelQuick& panel, const NodePanelQuick::MiniState* restore_state = nullptr) noexcept
|
||||
: panel_(panel)
|
||||
, restore_state_(restore_state)
|
||||
{
|
||||
}
|
||||
|
||||
void select_slot(pp::app::QuickUiSlotKind slot_kind, int slot_index, bool fire_event) override
|
||||
{
|
||||
if (slot_kind == pp::app::QuickUiSlotKind::brush) {
|
||||
panel_.set_selected_brush_index(slot_index, fire_event);
|
||||
return;
|
||||
}
|
||||
|
||||
panel_.set_selected_color_index(slot_index, fire_event);
|
||||
}
|
||||
|
||||
void open_slot_popup(pp::app::QuickUiSlotKind slot_kind, int slot_index) override
|
||||
{
|
||||
if (slot_kind == pp::app::QuickUiSlotKind::brush) {
|
||||
open_brush_popup(slot_index);
|
||||
return;
|
||||
}
|
||||
|
||||
open_color_picker(slot_index);
|
||||
}
|
||||
|
||||
void restore_state(int brush_index, int color_index, bool fire_event) override
|
||||
{
|
||||
if (!restore_state_)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < static_cast<int>(panel_.m_button_brushes.size()); i++)
|
||||
{
|
||||
auto b = static_cast<NodeStrokePreview*>(panel_.m_button_brushes[i]->m_children[0].get());
|
||||
b->m_brush = restore_state_->brushes[i];
|
||||
b->draw_stroke();
|
||||
auto c = static_cast<NodeBorder*>(panel_.m_button_colors[i]->m_children[0].get());
|
||||
c->m_color = restore_state_->colors[i];
|
||||
}
|
||||
panel_.set_selected_color_index(color_index, fire_event);
|
||||
panel_.set_selected_brush_index(brush_index, fire_event);
|
||||
}
|
||||
|
||||
void reset_state(bool fire_event) override
|
||||
{
|
||||
for (int i = 0; i < static_cast<int>(panel_.m_button_brushes.size()); i++)
|
||||
{
|
||||
auto b = static_cast<NodeStrokePreview*>(panel_.m_button_brushes[i]->m_children[0].get());
|
||||
b->m_brush = std::make_shared<Brush>();
|
||||
b->m_brush->load_tip("data/brushes/Round-Hard.png", "data/brushes/thumbs/Round-Hard.png");
|
||||
b->draw_stroke();
|
||||
}
|
||||
static_cast<NodeBorder*>(panel_.m_button_colors[0]->m_children[0].get())->m_color = glm::vec4(0, 0, 0, 1);
|
||||
static_cast<NodeBorder*>(panel_.m_button_colors[1]->m_children[0].get())->m_color = glm::vec4(.5, .5, .5, 1);
|
||||
static_cast<NodeBorder*>(panel_.m_button_colors[2]->m_children[0].get())->m_color = glm::vec4(1, 1, 1, 1);
|
||||
panel_.set_selected_brush_index(0, fire_event);
|
||||
panel_.set_selected_color_index(0, fire_event);
|
||||
}
|
||||
|
||||
private:
|
||||
void open_brush_popup(int slot_index)
|
||||
{
|
||||
auto button = panel_.m_button_brushes[slot_index];
|
||||
if (!button)
|
||||
return;
|
||||
|
||||
auto popup = App::I->presets;
|
||||
auto screen = panel_.root()->m_size;
|
||||
glm::vec2 tick_sz = { 16, 32 };
|
||||
glm::vec2 tick_pos = button->m_pos + glm::vec2(button->m_size.x, 0);
|
||||
glm::vec2 popup_pos = { tick_pos.x + tick_sz.x, tick_pos.y };
|
||||
|
||||
auto tick = panel_.root()->add_child<NodeImage>();
|
||||
tick->SetPositioning(YGPositionTypeAbsolute);
|
||||
tick->SetPosition(tick_pos.x, tick_pos.y + (button->m_size.y - tick_sz.y) * 0.5f);
|
||||
tick->SetSize(tick_sz);
|
||||
tick->set_image("data/ui/popup-tick.png");
|
||||
tick->m_scale = { 1, 1 };
|
||||
|
||||
float hh = popup->m_container->m_children.size() > 10 ? (screen.y - 90.f) : 400.f;
|
||||
popup->SetWidth(350);
|
||||
popup->SetHeight(glm::max(hh, 400.f));
|
||||
popup->SetPositioning(YGPositionTypeAbsolute);
|
||||
popup->SetPosition(popup_pos);
|
||||
panel_.root()->add_child(popup);
|
||||
|
||||
panel_.root()->update();
|
||||
popup->tick(0);
|
||||
popup->update();
|
||||
|
||||
if (tick_pos.x + popup->m_size.x > screen.x)
|
||||
{
|
||||
tick_pos = button->m_pos - glm::vec2(tick_sz.x, 0);
|
||||
popup_pos = { tick_pos.x - popup->GetWidth(), tick_pos.y };
|
||||
tick->m_scale.x = -1.f;
|
||||
}
|
||||
popup_pos = glm::clamp(popup_pos, { 0, 0 }, screen - popup->m_size);
|
||||
popup->SetPosition(popup_pos);
|
||||
tick->SetPosition(tick_pos.x, tick_pos.y + (button->m_size.y - tick_sz.y) * 0.5f);
|
||||
popup->update();
|
||||
|
||||
popup->m_mouse_ignore = false;
|
||||
popup->m_flood_events = true;
|
||||
popup->m_capture_children = false;
|
||||
popup->mouse_capture();
|
||||
|
||||
popup->on_popup_close = [tick](Node*) {
|
||||
tick->destroy();
|
||||
};
|
||||
|
||||
auto* panel = &panel_;
|
||||
popup->on_brush_changed = [panel, button](Node*, std::shared_ptr<Brush>& b) {
|
||||
auto pr = static_cast<NodeStrokePreview*>(button->m_children[0].get());
|
||||
*pr->m_brush = *b;
|
||||
pr->m_brush->load();
|
||||
pr->draw_stroke();
|
||||
if (panel->on_brush_change)
|
||||
panel->on_brush_change(button, pr->m_brush);
|
||||
};
|
||||
}
|
||||
|
||||
void open_color_picker(int slot_index)
|
||||
{
|
||||
auto target = panel_.m_button_colors[slot_index];
|
||||
if (!target)
|
||||
return;
|
||||
|
||||
auto popup = panel_.m_picker;
|
||||
auto screen = panel_.root()->m_size;
|
||||
glm::vec2 tick_sz = { 16, 32 };
|
||||
glm::vec2 tick_pos = target->m_pos + glm::vec2(target->m_size.x, 0);
|
||||
glm::vec2 popup_pos = { tick_pos.x + tick_sz.x, tick_pos.y - 140.f };
|
||||
|
||||
auto tick = panel_.root()->add_child<NodeImage>();
|
||||
tick->SetPositioning(YGPositionTypeAbsolute);
|
||||
tick->SetPosition(tick_pos.x, tick_pos.y + (target->m_size.y - tick_sz.y) * 0.5f);
|
||||
tick->SetSize(tick_sz);
|
||||
tick->set_image("data/ui/popup-tick.png");
|
||||
tick->m_scale = { 1, 1 };
|
||||
|
||||
popup->SetPositioning(YGPositionTypeAbsolute);
|
||||
popup->SetPosition(popup_pos);
|
||||
panel_.root()->add_child(popup);
|
||||
|
||||
panel_.root()->update();
|
||||
popup->tick(0);
|
||||
popup->update();
|
||||
|
||||
if (tick_pos.x + popup->m_size.x > screen.x)
|
||||
{
|
||||
tick_pos = target->m_pos - glm::vec2(tick_sz.x, 0);
|
||||
popup_pos = { tick_pos.x - popup->GetWidth(), tick_pos.y - 140.f };
|
||||
tick->m_scale.x = -1.f;
|
||||
}
|
||||
popup_pos = glm::clamp(popup_pos, { 0, 0 }, screen - popup->m_size);
|
||||
popup->SetPosition(popup_pos);
|
||||
tick->SetPosition(tick_pos.x, tick_pos.y + (target->m_size.y - tick_sz.y) * 0.5f);
|
||||
popup->update();
|
||||
|
||||
popup->m_mouse_ignore = false;
|
||||
popup->m_flood_events = true;
|
||||
popup->m_capture_children = false;
|
||||
popup->mouse_capture();
|
||||
|
||||
auto c = static_cast<NodeBorder*>(target->m_children[0].get());
|
||||
panel_.m_picker->set_color(c->m_color);
|
||||
panel_.m_picker->on_popup_close = [tick](Node*) {
|
||||
tick->destroy();
|
||||
};
|
||||
|
||||
auto* panel = &panel_;
|
||||
panel_.m_picker->on_color_change = [panel, c](Node*, glm::vec3 rgb) {
|
||||
c->m_color = glm::vec4(rgb, 1.f);
|
||||
if (panel->on_color_change)
|
||||
panel->on_color_change(panel, rgb);
|
||||
};
|
||||
}
|
||||
|
||||
NodePanelQuick& panel_;
|
||||
const NodePanelQuick::MiniState* restore_state_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
Node* NodePanelQuick::clone_instantiate() const
|
||||
{
|
||||
return new this_class;
|
||||
@@ -90,16 +278,10 @@ void NodePanelQuick::set_state(const MiniState& state, bool fire_event /*= false
|
||||
if (!plan)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
auto b = static_cast<NodeStrokePreview*>(m_button_brushes[i]->m_children[0].get());
|
||||
b->m_brush = state.brushes[i];
|
||||
b->draw_stroke();
|
||||
auto c = static_cast<NodeBorder*>(m_button_colors[i]->m_children[0].get());
|
||||
c->m_color = state.colors[i];
|
||||
}
|
||||
set_selected_color_index(state.color_index, fire_event);
|
||||
set_selected_brush_index(state.brush_index, fire_event);
|
||||
LegacyQuickUiServices services(*this, &state);
|
||||
const auto status = pp::app::execute_quick_ui_plan(plan.value(), services);
|
||||
if (!status.ok())
|
||||
LOG("Quick restore action failed: %s", status.message);
|
||||
}
|
||||
|
||||
void NodePanelQuick::reset_state(bool fire_event /*= false*/)
|
||||
@@ -108,18 +290,10 @@ void NodePanelQuick::reset_state(bool fire_event /*= false*/)
|
||||
if (!plan)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
auto b = static_cast<NodeStrokePreview*>(m_button_brushes[i]->m_children[0].get());
|
||||
b->m_brush = std::make_shared<Brush>();
|
||||
b->m_brush->load_tip("data/brushes/Round-Hard.png", "data/brushes/thumbs/Round-Hard.png");
|
||||
b->draw_stroke();
|
||||
}
|
||||
static_cast<NodeBorder*>(m_button_colors[0]->m_children[0].get())->m_color = glm::vec4(0, 0, 0, 1);
|
||||
static_cast<NodeBorder*>(m_button_colors[1]->m_children[0].get())->m_color = glm::vec4(.5, .5, .5, 1);
|
||||
static_cast<NodeBorder*>(m_button_colors[2]->m_children[0].get())->m_color = glm::vec4(1, 1, 1, 1);
|
||||
set_selected_brush_index(0, fire_event);
|
||||
set_selected_color_index(0, fire_event);
|
||||
LegacyQuickUiServices services(*this);
|
||||
const auto status = pp::app::execute_quick_ui_plan(plan.value(), services);
|
||||
if (!status.ok())
|
||||
LOG("Quick reset action failed: %s", status.message);
|
||||
}
|
||||
|
||||
void NodePanelQuick::init_controls()
|
||||
@@ -226,73 +400,10 @@ void NodePanelQuick::handle_button_brush_click(Node* button)
|
||||
if (!plan)
|
||||
return;
|
||||
|
||||
if (plan.value().updates_selection)
|
||||
{
|
||||
auto b = static_cast<NodeButtonCustom*>(button);
|
||||
b->set_active(true);
|
||||
m_button_brush_current->set_active(false);
|
||||
m_button_brush_current = b;
|
||||
m_button_brush_current_preview = static_cast<NodeStrokePreview*>(button->m_children[0].get());
|
||||
if (on_brush_change)
|
||||
on_brush_change(this, m_button_brush_current_preview->m_brush);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!plan.value().opens_brush_popup)
|
||||
return;
|
||||
|
||||
auto popup = App::I->presets;
|
||||
auto screen = root()->m_size;
|
||||
glm::vec2 tick_sz = { 16, 32 };
|
||||
glm::vec2 tick_pos = button->m_pos + glm::vec2(button->m_size.x, 0);
|
||||
glm::vec2 popup_pos = { tick_pos.x + tick_sz.x, tick_pos.y };
|
||||
|
||||
auto tick = root()->add_child<NodeImage>();
|
||||
tick->SetPositioning(YGPositionTypeAbsolute);
|
||||
tick->SetPosition(tick_pos.x, tick_pos.y + (button->m_size.y - tick_sz.y) * 0.5f);
|
||||
tick->SetSize(tick_sz);
|
||||
tick->set_image("data/ui/popup-tick.png");
|
||||
tick->m_scale = { 1, 1 };
|
||||
|
||||
float hh = popup->m_container->m_children.size() > 10 ? (screen.y - 90.f) : 400.f;
|
||||
popup->SetWidth(350);
|
||||
popup->SetHeight(glm::max(hh, 400.f));
|
||||
popup->SetPositioning(YGPositionTypeAbsolute);
|
||||
popup->SetPosition(popup_pos);
|
||||
root()->add_child(popup);
|
||||
|
||||
root()->update();
|
||||
popup->tick(0);
|
||||
popup->update();
|
||||
|
||||
if (tick_pos.x + popup->m_size.x > screen.x)
|
||||
{
|
||||
tick_pos = button->m_pos - glm::vec2(tick_sz.x, 0);
|
||||
popup_pos = { tick_pos.x - popup->GetWidth(), tick_pos.y };
|
||||
tick->m_scale.x = -1.f;
|
||||
}
|
||||
popup_pos = glm::clamp(popup_pos, { 0, 0 }, screen - popup->m_size);
|
||||
popup->SetPosition(popup_pos);
|
||||
tick->SetPosition(tick_pos.x, tick_pos.y + (button->m_size.y - tick_sz.y) * 0.5f);
|
||||
popup->update();
|
||||
|
||||
popup->m_mouse_ignore = false;
|
||||
popup->m_flood_events = true;
|
||||
popup->m_capture_children = false;
|
||||
popup->mouse_capture();
|
||||
|
||||
popup->on_popup_close = [this, tick](Node*) {
|
||||
tick->destroy();
|
||||
};
|
||||
|
||||
popup->on_brush_changed = [this, button](Node* target, std::shared_ptr<Brush>& b) {
|
||||
auto pr = static_cast<NodeStrokePreview*>(button->m_children[0].get());
|
||||
*pr->m_brush = *b;
|
||||
pr->m_brush->load();
|
||||
pr->draw_stroke();
|
||||
if (on_brush_change)
|
||||
on_brush_change(button, pr->m_brush);
|
||||
};
|
||||
LegacyQuickUiServices services(*this);
|
||||
const auto status = pp::app::execute_quick_ui_plan(plan.value(), services);
|
||||
if (!status.ok())
|
||||
LOG("Quick brush action failed: %s", status.message);
|
||||
}
|
||||
|
||||
void NodePanelQuick::handle_button_color_click(Node* target)
|
||||
@@ -307,68 +418,8 @@ void NodePanelQuick::handle_button_color_click(Node* target)
|
||||
if (!plan)
|
||||
return;
|
||||
|
||||
if (plan.value().updates_selection)
|
||||
{
|
||||
auto button = static_cast<NodeButtonCustom*>(target);
|
||||
button->set_active(true);
|
||||
m_button_color_current->set_active(false);
|
||||
m_button_color_current = button;
|
||||
m_button_color_current_inner = static_cast<NodeBorder*>(m_button_color_current->m_children[0].get());
|
||||
if (on_color_change)
|
||||
on_color_change(this, m_button_color_current_inner->m_color);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!plan.value().opens_color_picker)
|
||||
return;
|
||||
|
||||
auto popup = m_picker;
|
||||
auto screen = root()->m_size;
|
||||
glm::vec2 tick_sz = { 16, 32 };
|
||||
glm::vec2 tick_pos = target->m_pos + glm::vec2(target->m_size.x, 0);
|
||||
glm::vec2 popup_pos = { tick_pos.x + tick_sz.x, tick_pos.y - 140.f };
|
||||
|
||||
auto tick = root()->add_child<NodeImage>();
|
||||
tick->SetPositioning(YGPositionTypeAbsolute);
|
||||
tick->SetPosition(tick_pos.x, tick_pos.y + (target->m_size.y - tick_sz.y) * 0.5f);
|
||||
tick->SetSize(tick_sz);
|
||||
tick->set_image("data/ui/popup-tick.png");
|
||||
tick->m_scale = { 1, 1 };
|
||||
|
||||
//float hh = popup->m_container->m_children.size() > 10 ? (screen.y / App::I->zoom - 90.f) : 400.f;
|
||||
//popup->SetHeight(glm::max(hh, 400.f));
|
||||
popup->SetPositioning(YGPositionTypeAbsolute);
|
||||
popup->SetPosition(popup_pos);
|
||||
root()->add_child(popup);
|
||||
|
||||
root()->update();
|
||||
popup->tick(0);
|
||||
popup->update();
|
||||
|
||||
if (tick_pos.x + popup->m_size.x > screen.x)
|
||||
{
|
||||
tick_pos = target->m_pos - glm::vec2(tick_sz.x, 0);
|
||||
popup_pos = { tick_pos.x - popup->GetWidth(), tick_pos.y - 140.f };
|
||||
tick->m_scale.x = -1.f;
|
||||
}
|
||||
popup_pos = glm::clamp(popup_pos, { 0, 0 }, screen - popup->m_size);
|
||||
popup->SetPosition(popup_pos);
|
||||
tick->SetPosition(tick_pos.x, tick_pos.y + (target->m_size.y - tick_sz.y) * 0.5f);
|
||||
popup->update();
|
||||
|
||||
popup->m_mouse_ignore = false;
|
||||
popup->m_flood_events = true;
|
||||
popup->m_capture_children = false;
|
||||
popup->mouse_capture();
|
||||
|
||||
auto c = static_cast<NodeBorder*>(target->m_children[0].get());
|
||||
m_picker->set_color(c->m_color);
|
||||
m_picker->on_popup_close = [this, tick](Node*) {
|
||||
tick->destroy();
|
||||
};
|
||||
m_picker->on_color_change = [this, c](Node*, glm::vec3 rgb) {
|
||||
c->m_color = glm::vec4(rgb, 1.f);
|
||||
if (on_color_change)
|
||||
on_color_change(this, rgb);
|
||||
};
|
||||
LegacyQuickUiServices services(*this);
|
||||
const auto status = pp::app::execute_quick_ui_plan(plan.value(), services);
|
||||
if (!status.ok())
|
||||
LOG("Quick color action failed: %s", status.message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user