File: outer-menu.h

package info (click to toggle)
crawl 2%3A0.33.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,264 kB
  • sloc: cpp: 358,145; ansic: 27,203; javascript: 9,491; python: 8,359; perl: 3,327; java: 2,667; xml: 2,191; makefile: 1,830; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (110 lines) | stat: -rw-r--r-- 2,732 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
 * @file
 * @brief Menu button used in non-game menus
**/

#pragma once

#include <vector>

#include "ui.h"
#include "tilefont.h"
#include "libconsole.h"

using std::vector;

class MenuButton : public ui::Bin
{
    friend class OuterMenu;
public:
    MenuButton();

    virtual void _render() override;
    virtual ui::SizeReq _get_preferred_size(ui::Widget::Direction, int) override;
    virtual void _allocate_region() override;
    virtual bool on_event(const ui::Event& event) override;

    int id = 0;
    int hotkey = 0;
    colour_t highlight_colour = LIGHTGREY;
#ifndef USE_TILE_LOCAL
    colour_t fg_highlight = BLACK;
#endif
    string description;

    bool activate();

#ifdef USE_TILE_WEB
    void serialize();
#endif

protected:
    bool can_take_focus() override { return true; };

    bool focused = false;
    bool hovered = false;
    bool active = false;
#ifdef USE_TILE_LOCAL
    ShapeBuffer m_buf;
    LineBuffer m_line_buf;
#endif

#ifndef USE_TILE_LOCAL
    void recolour_descendants(const shared_ptr<Widget>& node);
    colour_t fg_normal = LIGHTGREY;
#endif
};

class OuterMenu : public ui::Widget
{
public:
    OuterMenu(bool can_shrink, int width, int height);

#ifdef USE_TILE_WEB
    ~OuterMenu();
    void serialize(string name);
    static void recv_outer_menu_focus(const char *menu_id, int hotkey);
#endif

    virtual void _render() override;
    virtual ui::SizeReq _get_preferred_size(ui::Widget::Direction, int) override;
    virtual void _allocate_region() override;
    virtual bool on_event(const ui::Event& event) override;

    virtual shared_ptr<Widget> get_child_at_offset(int, int) override {
        return m_root;
    }

    void add_button(shared_ptr<MenuButton> btn, int x, int y);
    void add_label(shared_ptr<ui::Text> label, int x, int y);
    MenuButton* get_button(int x, int y);
    MenuButton* get_button_by_id(int id);
    const vector<MenuButton*>& get_buttons() { return m_buttons; };
    void set_initial_focus(MenuButton *btn) {
        ASSERT(!have_allocated);
        m_initial_focus = btn;
    };

    void scroll_button_into_view(MenuButton *btn);

    weak_ptr<OuterMenu> linked_menus[4];
    shared_ptr<ui::Switcher> descriptions;
    const char *menu_id {nullptr};

protected:
    bool scroller_event_hook(const ui::Event& ev);
    bool move_button_focus(int fx, int fy, int dx, int dy, int limit);

    shared_ptr<ui::Grid> m_grid;
    shared_ptr<ui::Widget> m_root;
    vector<MenuButton*> m_buttons;
    vector<pair<formatted_string, coord_def>> m_labels;
    vector<int> m_description_indexes;
    int m_width;
    int m_height;
    MenuButton* m_initial_focus {nullptr};

    bool have_allocated {false};

    static bool focus_button_on_mouseenter;
};