We can select some text

This commit is contained in:
Clement Roblot 2024-08-02 21:28:35 +07:00
parent 9dfbdd168b
commit e9d13e2c2c
3 changed files with 27 additions and 15 deletions

View File

@ -68,7 +68,7 @@ int main() {
text("select_end " + std::to_string(selection.endx) + ";" + std::to_string(selection.endy)), text("select_end " + std::to_string(selection.endx) + ";" + std::to_string(selection.endy)),
text("textToCopy " + textToCopy) text("textToCopy " + textToCopy)
}) | }) |
border | selected(selection); border | selected(selection, textToCopy);
}); });
@ -76,17 +76,26 @@ int main() {
if (event.is_mouse()) { if (event.is_mouse()) {
auto& mouse = event.mouse(); auto& mouse = event.mouse();
if (mouse.button == Mouse::Left) { if (mouse.button == Mouse::Left) {
if (mouse.motion == Mouse::Pressed) {
if (mouse.motion == Mouse::Pressed)
{
selection.startx = mouse.x; selection.startx = mouse.x;
selection.starty = mouse.y; selection.starty = mouse.y;
selection.endx = mouse.x; selection.endx = mouse.x-1;
selection.endy = mouse.y; selection.endy = mouse.y;
} else if (mouse.motion == Mouse::Released) {
selection.endx = mouse.x; // screen.PixelAt(mouse.x, mouse.y).blink = true;
// screen.PixelAt(mouse.x, mouse.y).character = "K";
textToCopy += screen.PixelAt(mouse.x, mouse.y).character;
}
else if (mouse.motion == Mouse::Released)
{
selection.endx = mouse.x-1;
selection.endy = mouse.y; selection.endy = mouse.y;
} }
else if (mouse.motion == Mouse::Moved) { else if (mouse.motion == Mouse::Moved)
selection.endx = mouse.x; {
selection.endx = mouse.x-1;
selection.endy = mouse.y; selection.endy = mouse.y;
} }

View File

@ -104,8 +104,8 @@ Element canvas(std::function<void(Canvas&)>);
Element bold(Element); Element bold(Element);
Element dim(Element); Element dim(Element);
Element inverted(Element); Element inverted(Element);
Element selected(Region &selection, Element); Element selected(Region &selection, std::string &destination, Element);
Decorator selected(Region &selection); Decorator selected(Region &selection, std::string &destination);
Element underlined(Element); Element underlined(Element);
Element underlinedDouble(Element); Element underlinedDouble(Element);
Element blink(Element); Element blink(Element);

View File

@ -16,20 +16,23 @@ namespace {
class Selected : public NodeDecorator { class Selected : public NodeDecorator {
public: public:
using NodeDecorator::NodeDecorator; using NodeDecorator::NodeDecorator;
Selected(Element child, Region &selection) Selected(Element child, Region &selection, std::string &destination)
: NodeDecorator(std::move(child)), selection_(selection) {} : NodeDecorator(std::move(child)), selection_(selection), destination_(destination) {}
void Render(Screen& screen) override { void Render(Screen& screen) override {
Node::Render(screen); Node::Render(screen);
destination_ = "";
for (int y = selection_.starty; y <= selection_.endy; ++y) { for (int y = selection_.starty; y <= selection_.endy; ++y) {
for (int x = selection_.startx; x <= selection_.endx; ++x) { for (int x = selection_.startx; x <= selection_.endx; ++x) {
screen.PixelAt(x, y).inverted ^= true; screen.PixelAt(x, y).inverted ^= true;
destination_ += screen.PixelAt(x, y).character;
} }
} }
} }
private: private:
Region &selection_; Region &selection_;
std::string &destination_;
}; };
} // namespace } // namespace
@ -37,12 +40,12 @@ private:
/// colors. /// colors.
/// @ingroup dom /// @ingroup dom
Element selected(Region &selection, Element child) { Element selected(Region &selection, std::string &destination, Element child) {
return std::make_shared<Selected>(std::move(child), selection); return std::make_shared<Selected>(std::move(child), selection, destination);
} }
Decorator selected(Region &selection) { Decorator selected(Region &selection, std::string &destination) {
return [&selection](Element child) { return selected(selection, std::move(child)); }; return [&selection, &destination](Element child) { return selected(selection, destination, std::move(child)); };
} }
} // namespace ftxui } // namespace ftxui