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