]> git.tdb.fi Git - libs/gltk.git/blob - source/button.cpp
Rework how widget ownership works in Container
[libs/gltk.git] / source / button.cpp
1 #include <msp/gl/meshbuilder.h>
2 #include "button.h"
3 #include "part.h"
4 #include "style.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GLtk {
10
11 Button::Button(const std::string &t):
12         text(),
13         icon(0),
14         pressed(false)
15 {
16         input_type = INPUT_NAVIGATION;
17         set_text(t);
18 }
19
20 void Button::autosize_special(const Part &part, Geometry &ageom) const
21 {
22         if(part.get_name()=="text")
23                 text.autosize(part, ageom);
24         else if(part.get_name()=="icon" && icon)
25         {
26                 const Sides &margin = part.get_margin();
27                 ageom.w = max(ageom.w, icon->get_width()+margin.left+margin.right);
28                 ageom.h = max(ageom.h, icon->get_height()+margin.top+margin.bottom);
29         }
30 }
31
32 void Button::set_text(const std::string &t)
33 {
34         text = t;
35         signal_autosize_changed.emit();
36         rebuild();
37 }
38
39 void Button::set_icon(const GL::Texture2D *i)
40 {
41         icon = i;
42         rebuild();
43 }
44
45 void Button::rebuild_special(const Part &part)
46 {
47         if(part.get_name()=="text")
48                 text.build(part, state, geom, part_cache);
49         if(part.get_name()=="icon")
50         {
51                 if(icon)
52                 {
53                         Geometry rgeom;
54                         rgeom.w = icon->get_width();
55                         rgeom.h = icon->get_height();
56                         part.get_alignment().apply(rgeom, geom, part.get_margin());
57
58                         GL::MeshBuilder bld(part_cache.create_mesh(part, *icon));
59                         bld.color(1.0f, 1.0f, 1.0f);
60                         bld.begin(GL::TRIANGLE_STRIP);
61                         bld.texcoord(0, 1);
62                         bld.vertex(rgeom.x, rgeom.y+rgeom.h);
63                         bld.texcoord(0, 0);
64                         bld.vertex(rgeom.x, rgeom.y);
65                         bld.texcoord(1, 1);
66                         bld.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
67                         bld.texcoord(1, 0);
68                         bld.vertex(rgeom.x+rgeom.w, rgeom.y);
69                         bld.end();
70                 }
71         }
72 }
73
74 void Button::button_press(int, int, unsigned btn)
75 {
76         if(btn==1)
77         {
78                 pressed = true;
79                 set_state(ACTIVE);
80         }
81 }
82
83 void Button::button_release(int x, int y, unsigned btn)
84 {
85         if(pressed && btn==1)
86         {
87                 clear_state(ACTIVE);
88                 pressed = false;
89
90                 if(geom.is_inside_relative(x, y))
91                         signal_clicked.emit();
92         }
93 }
94
95 void Button::pointer_motion(int x, int y)
96 {
97         if(pressed)
98         {
99                 if(!geom.is_inside_relative(x, y))
100                         clear_state(ACTIVE);
101                 else
102                         set_state(ACTIVE);
103         }
104 }
105
106 bool Button::navigate(Navigation nav)
107 {
108         if(nav==NAV_ACTIVATE)
109                 signal_clicked.emit();
110         else
111                 return false;
112
113         return true;
114 }
115
116 void Button::on_style_change()
117 {
118         text.set_style(style);
119 }
120
121
122 Button::Loader::Loader(Button &btn):
123         DataFile::DerivedObjectLoader<Button, Widget::Loader>(btn)
124 {
125         add("text", &Loader::text);
126 }
127
128 void Button::Loader::text(const std::string &t)
129 {
130         obj.text = t;
131 }
132
133 } // namespace GLtk
134 } // namespace Msp