File: AccordionPanel.cpp

package info (click to toggle)
freeorion 0.5.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 194,920 kB
  • sloc: cpp: 186,821; python: 40,979; ansic: 1,164; xml: 721; makefile: 32; sh: 7
file content (124 lines) | stat: -rw-r--r-- 4,653 bytes parent folder | download | duplicates (2)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "AccordionPanel.h"

#include "ClientUI.h"
#include "CUIControls.h"

namespace {
    constexpr int EXPAND_BUTTON_SIZE = 20;
}

AccordionPanel::AccordionPanel(GG::X w, GG::Y h, bool is_button_on_left) :
    GG::Control(GG::X0, GG::Y0, w, h, GG::INTERACTIVE),
    m_is_left(is_button_on_left),
    m_interior_color(ClientUI::WndColor())
{}

void AccordionPanel::CompleteConstruction() {
    GG::Control::CompleteConstruction();
    boost::filesystem::path button_texture_dir = ClientUI::ArtDir() / "icons" / "buttons";

    m_expand_button = Wnd::Create<CUIButton>(
        GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "downarrownormal.png")),
        GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "downarrowclicked.png")),
        GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "downarrowmouseover.png")));
    m_expand_button->SetMinSize(GG::Pt(GG::X(EXPAND_BUTTON_SIZE), GG::Y(EXPAND_BUTTON_SIZE)));
    m_expand_button->NonClientChild(true);

    AttachChild(m_expand_button);

    // Don't call the virtual function, derived classes may not be completed
    AccordionPanel::DoLayout();
    InitBuffer();
}

void AccordionPanel::InitBuffer() {
    const auto sz = Size();
    m_border_buffer.clear();
    m_border_buffer.store(0.0f,        0.0f);
    m_border_buffer.store(Value(sz.x), 0.0f);
    m_border_buffer.store(Value(sz.x), Value(sz.y));
    m_border_buffer.store(0.0f,        Value(sz.y));
    m_border_buffer.store(0.0f,        0.0f);
    m_border_buffer.createServerBuffer();
}

GG::Pt AccordionPanel::ClientUpperLeft() const noexcept
{ return UpperLeft() + GG::Pt((m_is_left ? GG::X(EXPAND_BUTTON_SIZE + m_border_margin) : GG::X0), GG::Y0); }

GG::Pt AccordionPanel::ClientLowerRight() const noexcept
{ return LowerRight() - GG::Pt((m_is_left ? GG::X0 : GG::X(EXPAND_BUTTON_SIZE + m_border_margin)), GG::Y0); }

void AccordionPanel::SetInteriorColor(GG::Clr c)
{ m_interior_color = c; }

void AccordionPanel::SetBorderMargin(int margin)
{ m_border_margin = std::max<int>(0, margin); }

void AccordionPanel::Render() {
    if (Height() < GG::Y1 || Width() < GG::X1)
        return;

    GG::Pt ul = UpperLeft();

    glPushMatrix();
    glLoadIdentity();
    glTranslatef(static_cast<GLfloat>(Value(ul.x)), static_cast<GLfloat>(Value(ul.y)), 0.0f);
    glDisable(GL_TEXTURE_2D);
    glLineWidth(1.0f);
    glEnableClientState(GL_VERTEX_ARRAY);

    m_border_buffer.activate();
    glColor(m_interior_color);
    glDrawArrays(GL_TRIANGLE_FAN,   0, m_border_buffer.size() - 1);
    glColor(ClientUI::WndOuterBorderColor());
    glDrawArrays(GL_LINE_STRIP,     0, m_border_buffer.size());

    glEnable(GL_TEXTURE_2D);
    glPopMatrix();
    glDisableClientState(GL_VERTEX_ARRAY);
}

void AccordionPanel::MouseWheel(GG::Pt pt, int move, GG::Flags<GG::ModKey> mod_keys)
{ ForwardEventToParent(); }

void AccordionPanel::SizeMove(GG::Pt ul, GG::Pt lr) {
    GG::Pt old_size = GG::Wnd::Size();

    GG::Wnd::SizeMove(ul, lr);

    if (old_size != GG::Wnd::Size()) {
        DoLayout();
        InitBuffer();
    }
}

void AccordionPanel::SetCollapsed(bool collapsed) {
    if (collapsed == m_collapsed)
        return;

    boost::filesystem::path button_texture_dir = ClientUI::ArtDir() / "icons" / "buttons";

    m_collapsed = collapsed;
    if (!collapsed) {
        m_expand_button->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "uparrownormal.png"   )));
        m_expand_button->SetPressedGraphic  (GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "uparrowclicked.png"  )));
        m_expand_button->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "uparrowmouseover.png")));
    } else {
        m_expand_button->SetUnpressedGraphic(GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "downarrownormal.png"   )));
        m_expand_button->SetPressedGraphic  (GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "downarrowclicked.png"  )));
        m_expand_button->SetRolloverGraphic (GG::SubTexture(ClientUI::GetTexture(button_texture_dir / "downarrowmouseover.png")));
    }

    ExpandCollapseSignal();
}

bool AccordionPanel::IsCollapsed() const
{ return m_collapsed; }

void AccordionPanel::DoLayout() {
    GG::Pt expand_button_ul(m_is_left ? GG::X(-(EXPAND_BUTTON_SIZE + m_border_margin))
                            : (Width() + GG::X(-(EXPAND_BUTTON_SIZE + m_border_margin))), GG::Y0);
    GG::Pt expand_button_lr = expand_button_ul + GG::Pt(GG::X(EXPAND_BUTTON_SIZE), GG::Y(EXPAND_BUTTON_SIZE));
    Wnd::MoveChildUp(m_expand_button.get());
    m_expand_button->SizeMove(expand_button_ul, expand_button_lr);
}