AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AButton.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#pragma once
   13
   14#include "AView.h"
   15#include "AUI/Common/AString.h"
   16#include "AUI/Render/IRenderer.h"
   17#include "AUI/Layout/AHorizontalLayout.h"
   18#include "ALabel.h"
   19#include "AViewContainer.h"
   20#include "AUI/ASS/Selector/AAssSelector.h"
   21#include "AUI/Layout/AStackedLayout.h"
   22#include "AUI/Util/UIBuildingHelpers.h"
   23
   31class API_AUI_VIEWS AButton : public AAbstractLabel {
   32public:
   33    AButton();
   34
   35    explicit AButton(AString text) noexcept: AAbstractLabel(std::move(text)) {}
   36
   37    virtual ~AButton() = default;
   38
   39    [[nodiscard]]
   40    bool isDefault() const noexcept {
   41        return mDefault;
   42    }
   43
   44    void setDefault(bool isDefault = true);
   45
   46    bool consumesClick(const glm::ivec2& pos) override;
   47
   48signals:
   49    emits<bool> defaultState;
   50    emits<> becameDefault;
   51    emits<> noLongerDefault;
   52
   53private:
   54    AFieldSignalEmitter<bool> mDefault = AFieldSignalEmitter<bool>(defaultState, becameDefault, noLongerDefault);
   55};
   56
   60class AButtonEx : public AViewContainer {
   61public:
   62    AButtonEx() {
   63        addAssName(".btn");
   64    }
   65
   66    ~AButtonEx() override = default;
   67};
   68
   69namespace declarative {
   73struct Button : aui::ui_building::view_container_layout<AStackedLayout, AButtonEx> {
   83    Button(AString text) : layouted_container_factory<AStackedLayout, AButtonEx>({ Label { std::move(text) } }) {}
   84
   94    Button(const char* text) : layouted_container_factory<AStackedLayout, AButtonEx>({ Label { text } }) {}
   95
  108    template <typename... Views>
  109    Button(Views&&... views)
  110        : layouted_container_factory<AStackedLayout, AButtonEx>(Horizontal { std::forward<Views>(views)... } ) {}
  111};
  112}   // namespace declarative
  113
  114namespace ass::button {
  115struct Default : IAssSubSelector {
  116private:
  117    _unique<IAssSubSelector> mWrapped;
  118
  119    public:
  120        template<typename T>
  121        Default(T value): mWrapped(new T(std::move(value))) {}
  122
  123
  124        bool isPossiblyApplicable(AView* view) override {
  125            return mWrapped->isPossiblyApplicable(view) && dynamic_cast<AButton*>(view);
  126        }
  127
  128        bool isStateApplicable(AView* view) override {
  129            if (!mWrapped->isStateApplicable(view))
  130                return false;
  131
  132            if (auto c = dynamic_cast<AButton*>(view)) {
  133                return c->isDefault();
  134            }
  135            return false;
  136        }
  137
  138        void setupConnections(AView* view, const _<AAssHelper>& helper) override {
  139            IAssSubSelector::setupConnections(view, helper);
  140            mWrapped->setupConnections(view, helper);
  141
  142            if (auto c = dynamic_cast<AButton*>(view)) {
  143                c->defaultState.clearAllOutgoingConnectionsWith(helper.get());
  144                AObject::connect(c->defaultState, slot(helper)::onInvalidateStateAss);
  145            }
  146        }
  147    };
  148}
auto text() const
Label's text property.
Definition AAbstractLabel.h:37
Unlike AButton, AButtonEx is a container which looks like a button.
Definition AButton.h:60
Button with text, which can be pushed to make some action.
Definition AButton.h:31
bool consumesClick(const glm::ivec2 &pos) override
Determines whether this AView processes this click or passes it thru.
Stores a value and observes it's changes, emitting a signal.
Definition AFieldSignalEmitter.h:21
Places views in a stack (along z axis).
Definition AStackedLayout.h:37
Represents a Unicode character string.
Definition AString.h:38
Base class of all UI objects.
Definition AView.h:78
void addAssName(const AString &assName)
Adds an ASS class to this AView.
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
Definition AAssSelector.h:28
class_of c
Selects views that are of the specified classes.
Definition class_of.h:84
static decltype(auto) connect(const Signal &signal, Object *object, Function &&function)
Connects signal to the slot of the specified object.
Definition AObject.h:86
ASignal< Args... > emits
A signal declaration.
Definition ASignal.h:577
#define slot(v)
Passes some variable and type of the variable separated by comma. It's convenient to use with the con...
Definition kAUI.h:88
Button(const char *text)
Basic label initializer.
Definition AButton.h:94
Button(AString text)
Basic label initializer.
Definition AButton.h:83
Button(Views &&... views)
An explicit form of AButton where you can put any views in it, i.e., icons.
Definition AButton.h:109
Declarative form of ALabel.
Definition ALabel.h:35