]> git.tdb.fi Git - libs/gltk.git/blob - source/button.cpp
Rearrange members
[libs/gltk.git] / source / button.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007-2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/gl/immediate.h>
9 #include "button.h"
10 #include "part.h"
11
12 namespace Msp {
13 namespace GLtk {
14
15 Button::Button(const std::string &t):
16         text(),
17         icon(0),
18         pressed(false)
19 {
20         set_text(t);
21 }
22
23 void Button::set_text(const std::string &t)
24 {
25         text = t;
26 }
27
28 void Button::set_icon(const GL::Texture2D *i)
29 {
30         icon = i;
31 }
32
33 void Button::render_special(const Part &part) const
34 {
35         if(part.get_name()=="text")
36                 text.render(part, geom);
37         if(part.get_name()=="icon" && icon)
38         {
39                 Geometry rgeom;
40                 rgeom.w = icon->get_width();
41                 rgeom.h = icon->get_height();
42                 part.get_alignment().apply(rgeom, geom, part.get_margin());
43
44                 icon->bind();
45                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
46                 imm.color(1.0f, 1.0f, 1.0f);
47                 imm.begin(GL::QUADS);
48                 imm.texcoord(0, 0);
49                 imm.vertex(rgeom.x, rgeom.y);
50                 imm.texcoord(1, 0);
51                 imm.vertex(rgeom.x+rgeom.w, rgeom.y);
52                 imm.texcoord(1, 1);
53                 imm.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
54                 imm.texcoord(0, 1);
55                 imm.vertex(rgeom.x, rgeom.y+rgeom.h);
56                 imm.end();
57                 GL::Texture::unbind();
58         }
59 }
60
61 void Button::button_press(int, int, unsigned btn)
62 {
63         if(btn==1)
64         {
65                 pressed = true;
66                 state |= ACTIVE;
67         }
68 }
69
70 void Button::button_release(int x, int y, unsigned btn)
71 {
72         if(pressed && btn==1)
73         {
74                 state &= ~ACTIVE;
75                 pressed = false;
76
77                 if(geom.is_inside_relative(x, y))
78                         signal_clicked.emit();
79         }
80 }
81
82 void Button::pointer_motion(int x, int y)
83 {
84         if(pressed)
85         {
86                 if(!geom.is_inside_relative(x, y))
87                         state &= ~ACTIVE;
88                 else
89                         state |= ACTIVE;
90         }
91 }
92
93 void Button::on_style_change()
94 {
95         text.set_style(style);
96 }
97
98
99 Button::Loader::Loader(Button &btn):
100         Widget::Loader(btn)
101 {
102         add("text", &Loader::text);
103 }
104
105 void Button::Loader::text(const std::string &t)
106 {
107         static_cast<Button &>(wdg).text = t;
108 }
109
110 } // namespace GLtk
111 } // namespace Msp