Loading...
Searching...
No Matches
PointerAddresses.hpp
1#pragma once
2
3#include <Gfx/GfxExecContext.hpp>
4#include <Gfx/GfxInputDevice.hpp>
5#include <Gfx/Graph/PointerState.hpp>
6#include <Gfx/Graph/TouchHistoryNode.hpp>
7
8#include <ossia/network/base/device.hpp>
9#include <ossia/network/base/node_attributes.hpp>
10#include <ossia/network/generic/generic_node.hpp>
11
12#include <memory>
13#include <vector>
14
15namespace Gfx
16{
37{
38 ossia::net::parameter_base* clip{};
39 ossia::net::parameter_base* points{};
41 std::shared_ptr<score::gfx::PointerState> pointers
42 = std::make_shared<score::gfx::PointerState>();
43
45 void add_clip(ossia::net::device_base& dev, ossia::net::node_base& cursor)
46 {
47 auto node = std::make_unique<ossia::net::generic_node>("clip", dev, cursor);
48 clip = node->create_parameter(ossia::val_type::VEC2F);
49 clip->set_domain(ossia::make_domain(-1.f, 1.f));
50 clip->push_value(ossia::vec2f{0.f, 0.f});
51 ossia::net::set_description(*node, "Clip space, [-1, 1], y up");
52 cursor.add_child(std::move(node));
53 }
54
57 ossia::net::device_base& dev, ossia::net::node_base& root,
58 GfxExecutionAction* context)
59 {
60 auto touch = std::make_unique<ossia::net::generic_node>("touch", dev, root);
61 {
62 auto node = std::make_unique<ossia::net::generic_node>("points", dev, *touch);
63 points = node->create_parameter(ossia::val_type::LIST);
64 ossia::net::set_description(
65 *node, "32 pointers, [x, y, pressure] in clip space, y up; slot 0 is the "
66 "mouse or first finger");
67 points->set_value_quiet(to_value(pointers->snapshot()));
68 points->add_callback([p = pointers](const ossia::value& v) { from_value(*p, v); });
69 touch->add_child(std::move(node));
70 }
71 if(context)
72 {
73 auto node = std::make_unique<ossia::net::generic_node>("history", dev, *touch);
74 ossia::net::set_description(
75 *node, "vertexshaderart's touch history: the last 240 frames of "
76 "touch/points, 32x240 RGBA32F (x, y, pressure, time), newest at v = 0");
77 node->set_parameter(std::make_unique<simple_texture_input_parameter>(
78 new score::gfx::TouchHistoryNode{pointers}, context, *node));
79 touch->add_child(std::move(node));
80 }
81 root.add_child(std::move(touch));
82 }
83
86 {
87 if(points)
88 points->push_value(to_value(state.snapshot()));
89 }
90
93 void move_slot(int slot, float x, float y)
94 {
95 if(!points || slot < 0 || slot >= score::gfx::PointerState::slots)
96 return;
97 const ossia::value cur = points->value();
98 std::vector<ossia::value> l;
99 if(auto* p = cur.target<std::vector<ossia::value>>())
100 l = *p;
101 l.resize(score::gfx::PointerState::slots, ossia::vec3f{0.f, 0.f, 0.f});
102 float pressure = 0.f;
103 if(auto* p = l[slot].target<ossia::vec3f>())
104 pressure = (*p)[2];
105 l[slot] = ossia::vec3f{x, y, pressure};
106 points->push_value(std::move(l));
107 }
108
109 void set_clip(float x, float y)
110 {
111 if(clip)
112 clip->push_value(ossia::vec2f{x, y});
113 }
114
115 static ossia::value
116 to_value(const std::array<score::gfx::PointerState::Slot, score::gfx::PointerState::slots>& s)
117 {
118 std::vector<ossia::value> l;
119 l.reserve(s.size());
120 for(const auto& p : s)
121 l.emplace_back(ossia::vec3f{p.x, p.y, p.pressure});
122 return l;
123 }
124
125 static void from_value(score::gfx::PointerState& state, const ossia::value& v)
126 {
127 auto* l = v.target<std::vector<ossia::value>>();
128 if(!l)
129 return;
130 const int n = std::min<int>(int(l->size()), score::gfx::PointerState::slots);
131 for(int i = 0; i < n; i++)
132 {
133 const auto& e = (*l)[i];
134 if(auto* p = e.target<ossia::vec3f>())
135 {
136 state.move(i, (*p)[0], (*p)[1]);
137 state.press(i, (*p)[2]);
138 }
139 else if(auto* q = e.target<ossia::vec2f>())
140 {
141 state.move(i, (*q)[0], (*q)[1]);
142 }
143 else if(auto* sub = e.target<std::vector<ossia::value>>(); sub && sub->size() >= 2)
144 {
145 state.move(i, ossia::convert<float>((*sub)[0]), ossia::convert<float>((*sub)[1]));
146 if(sub->size() >= 3)
147 state.press(i, ossia::convert<float>((*sub)[2]));
148 }
149 }
150 }
151};
152}
Definition GfxExecContext.hpp:19
Binds the rendering pipeline to ossia processes.
Definition AssetTable.cpp:7
Definition PointerAddresses.hpp:37
void move_slot(int slot, float x, float y)
Definition PointerAddresses.hpp:93
void publish(const score::gfx::PointerState &state)
Publish the window's decoded pointers on touch/points.
Definition PointerAddresses.hpp:85
void add_touch(ossia::net::device_base &dev, ossia::net::node_base &root, GfxExecutionAction *context)
touch/points and touch/history, under root.
Definition PointerAddresses.hpp:56
void add_clip(ossia::net::device_base &dev, ossia::net::node_base &cursor)
cursor/clip, under an existing cursor node.
Definition PointerAddresses.hpp:45
std::shared_ptr< score::gfx::PointerState > pointers
What touch/history records; touch/points writes it.
Definition PointerAddresses.hpp:42
Definition PointerState.hpp:24
The pointers of an output as a texture: vertexshaderart's touch history (greggman/vertexshaderart,...
Definition TouchHistoryNode.hpp:46