]> git.tdb.fi Git - libs/gltk.git/blob - source/button.cpp
Style update: add spaces around assignments
[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 Resources &r, const std::string &t):
16         Widget(r),
17         text(),
18         icon(0),
19         pressed(false)
20 {
21         update_style();
22         set_text(t);
23 }
24
25 void Button::set_text(const std::string &t)
26 {
27         text = t;
28 }
29
30 void Button::set_icon(const GL::Texture2D *i)
31 {
32         icon = i;
33 }
34
35 void Button::button_press(int, int, unsigned btn)
36 {
37         if(btn==1)
38         {
39                 pressed = true;
40                 state |= ACTIVE;
41         }
42 }
43
44 void Button::button_release(int x, int y, unsigned btn)
45 {
46         if(pressed && btn==1)
47         {
48                 state &= ~ACTIVE;
49                 pressed = false;
50
51                 if(geom.is_inside_relative(x, y))
52                         signal_clicked.emit();
53         }
54 }
55
56 void Button::pointer_motion(int x, int y)
57 {
58         if(pressed)
59         {
60                 if(!geom.is_inside_relative(x, y))
61                         state &= ~ACTIVE;
62                 else
63                         state |= ACTIVE;
64         }
65 }
66
67 void Button::render_special(const Part &part) const
68 {
69         if(part.get_name()=="text")
70                 text.render(part, geom);
71         if(part.get_name()=="icon" && icon)
72         {
73                 Geometry rgeom;
74                 rgeom.w = icon->get_width();
75                 rgeom.h = icon->get_height();
76                 part.get_alignment().apply(rgeom, geom, part.get_margin());
77
78                 icon->bind();
79                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
80                 imm.color(1.0f, 1.0f, 1.0f);
81                 imm.begin(GL::QUADS);
82                 imm.texcoord(0, 0);
83                 imm.vertex(rgeom.x, rgeom.y);
84                 imm.texcoord(1, 0);
85                 imm.vertex(rgeom.x+rgeom.w, rgeom.y);
86                 imm.texcoord(1, 1);
87                 imm.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
88                 imm.texcoord(0, 1);
89                 imm.vertex(rgeom.x, rgeom.y+rgeom.h);
90                 imm.end();
91                 GL::Texture::unbind();
92         }
93 }
94
95 void Button::on_style_change()
96 {
97         text.set_style(style);
98 }
99
100
101 Button::Loader::Loader(Button &btn):
102         Widget::Loader(btn)
103 {
104         add("text", &Loader::text);
105 }
106
107 void Button::Loader::text(const std::string &t)
108 {
109         static_cast<Button &>(wdg).text = t;
110 }
111
112 } // namespace GLtk
113 } // namespace Msp