AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
ACursorSelectable.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12//
   13// Created by alex2 on 30.11.2020.
   14//
   15
   16#pragma once
   17
   18
   19#include <AUI/Common/AString.h>
   20#include <glm/glm.hpp>
   21#include <AUI/Font/AFontStyle.h>
   22#include <AUI/Render/ATextLayoutHelper.h>
   23#include <AUI/Render/RenderHints.h>
   24#include <AUI/Platform/AInput.h>
   25#include <AUI/View/AView.h>
   26
   27class API_AUI_VIEWS ACursorSelectable {
   28public:
   29    virtual ~ACursorSelectable();
   30
   31    struct Selection
   32    {
   33        unsigned begin;
   34        unsigned end;
   35
   36        [[nodiscard]]
   37        bool operator==(const Selection& rhs) const noexcept = default;
   38
   39        [[nodiscard]]
   40        bool operator!=(const Selection& rhs) const noexcept = default;
   41
   42        [[nodiscard]]
   43        bool empty() const noexcept {
   44            return begin == end;
   45        }
   46    };
   47
   56    [[nodiscard]] virtual const AString& getText() const = 0;
   57
   61    [[nodiscard]] virtual size_t length() const = 0;
   62    [[nodiscard]] AString selectedText() const
   63    {
   64        if (!hasSelection())
   65            return {};
   66        auto t = getText();
   67        return {t.begin() + selection().begin, t.begin() + selection().end };
   68    }
   69
   73    [[nodiscard]] Selection selection() const;
   74
   78    [[nodiscard]] bool hasSelection() const;
   79
   83    [[nodiscard]] virtual unsigned cursorIndexByPos(glm::ivec2 pos) = 0;
   84
   85    [[nodiscard]] virtual glm::ivec2 getPosByIndex(size_t index) = 0;
   86
   93    [[nodiscard]] virtual glm::ivec2 getCursorPosition() = 0;
   94
   95
   99    void selectAll();
  100
  104    void clearSelection();
  105
  106    void setSelection(int cursorIndex) {
  107        mCursorIndex = cursorIndex;
  108        mCursorSelection.reset();
  109        onSelectionChanged();
  110    }
  111
  112    void setSelection(Selection selection) {
  113        mCursorIndex = selection.begin;
  114        mCursorSelection = selection.end;
  115        onSelectionChanged();
  116    }
  117
  118protected:
  119    unsigned mCursorIndex = 0;
  120    AOptional<unsigned> mCursorSelection;
  121
  122    virtual bool isLButtonPressed() = 0;
  123    virtual AString getDisplayText() = 0;
  124    virtual void cursorSelectableRedraw() = 0;
  125    virtual void onSelectionChanged() = 0;
  126
  127
  128    void handleMouseDoubleClicked(const APointerPressedEvent& event);
  129    void handleMousePressed(const APointerPressedEvent& event);
  130    void handleMouseReleased(const APointerReleasedEvent& event);
  131    void handleMouseMove(const glm::ivec2& pos);
  132
  133    template<aui::invocable Callback>
  134    void drawSelectionBeforeAndAfter(IRenderer& render, std::span<ARect<int>> rects, Callback&& drawText) {
  135        if (rects.empty()) {
  136            drawText();
  137            return;
  138        }
  139
  140        auto drawRects = [&] {
  141            for (auto r : rects) {
  142                render.rectangle(ASolidBrush{}, r.p1, r.size());
  143            }
  144        };
  145        {
  146            RenderHints::PushColor c(render);
  147            render.setColor(AColor(1.f) - AColor(0x0078d700u));
  148            drawRects();
  149        }
  150
  151        drawText();
  152
  153        render.setBlending(Blending::INVERSE_DST);
  154        AUI_DEFER { render.setBlending(Blending::NORMAL); };
  155        drawRects();
  156    }
  157
  158private:
  159    bool mIgnoreSelection = false;
  160
  161};
  162
  163inline std::ostream& operator<<(std::ostream& o, const ACursorSelectable::Selection& e) noexcept{
  164    o << "Selection";
  165    if (e.empty()) {
  166        o << "{" << e.begin << "}";
  167    } else {
  168        o << "[" << e.begin << ";" << e.end << ")";
  169    }
  170    return o;
  171}
Definition ACursorSelectable.h:27
virtual size_t length() const =0
virtual unsigned cursorIndexByPos(glm::ivec2 pos)=0
void clearSelection()
Remove selection from the text field.
virtual glm::ivec2 getCursorPosition()=0
bool hasSelection() const
virtual const AString & getText() const =0
Selection selection() const
void selectAll()
Select whole text in the text field.
Represents a Unicode character string.
Definition AString.h:38
void setColor(const AColor &color)
Sets the color which is multiplied with any brush. Unlike setColorForced, the new color is multiplied...
Definition IRenderer.h:432
virtual void setBlending(Blending blending)=0
Sets blending mode.
virtual void rectangle(const ABrush &brush, glm::vec2 position, glm::vec2 size)=0
Draws simple rectangle.
class_of c
Selects views that are of the specified classes.
Definition class_of.h:84
#define AUI_DEFER
Defers execution of the next block to the end of current block (RAII scope).
Definition kAUI.h:196
Definition ACursorSelectable.h:32