]> git.tdb.fi Git - libs/gltk.git/blob - source/button.cpp
Store the Resources reference only in Root widget
[libs/gltk.git] / source / button.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007, 2009  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::button_press(int, int, unsigned btn)
34 {
35         if(btn==1)
36         {
37                 pressed = true;
38                 state |= ACTIVE;
39         }
40 }
41
42 void Button::button_release(int x, int y, unsigned btn)
43 {
44         if(pressed && btn==1)
45         {
46                 state &= ~ACTIVE;
47                 pressed = false;
48
49                 if(geom.is_inside_relative(x, y))
50                         signal_clicked.emit();
51         }
52 }
53
54 void Button::pointer_motion(int x, int y)
55 {
56         if(pressed)
57         {
58                 if(!geom.is_inside_relative(x, y))
59                         state &= ~ACTIVE;
60                 else
61                         state |= ACTIVE;
62         }
63 }
64
65 void Button::render_special(const Part &part) const
66 {
67         if(part.get_name()=="text")
68                 text.render(part, geom);
69         if(part.get_name()=="icon" && icon)
70         {
71                 Geometry rgeom;
72                 rgeom.w = icon->get_width();
73                 rgeom.h = icon->get_height();
74                 part.get_alignment().apply(rgeom, geom, part.get_margin());
75
76                 icon->bind();
77                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
78                 imm.color(1.0f, 1.0f, 1.0f);
79                 imm.begin(GL::QUADS);
80                 imm.texcoord(0, 0);
81                 imm.vertex(rgeom.x, rgeom.y);
82                 imm.texcoord(1, 0);
83                 imm.vertex(rgeom.x+rgeom.w, rgeom.y);
84                 imm.texcoord(1, 1);
85                 imm.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
86                 imm.texcoord(0, 1);
87                 imm.vertex(rgeom.x, rgeom.y+rgeom.h);
88                 imm.end();
89                 GL::Texture::unbind();
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