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("textToCopy " + textToCopy)
}) |
border | selected(selection);
border | selected(selection, textToCopy);
});
@ -76,17 +76,26 @@ int main() {
if (event.is_mouse()) {
auto& mouse = event.mouse();
if (mouse.button == Mouse::Left) {
if (mouse.motion == Mouse::Pressed) {
if (mouse.motion == Mouse::Pressed)
{
selection.startx = mouse.x;
selection.starty = mouse.y;
selection.endx = mouse.x;
selection.endx = mouse.x-1;
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;
}
else if (mouse.motion == Mouse::Moved) {
selection.endx = mouse.x;
else if (mouse.motion == Mouse::Moved)
{
selection.endx = mouse.x-1;
selection.endy = mouse.y;
}

View File

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

View File

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