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