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