]> git.tdb.fi Git - libs/gltk.git/blob - source/button.cpp
Add Text class with multiline support
[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(style),
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                 //render_text(part, text);
71                 text.render(part, geom);
72         if(part.get_name()=="icon" && icon)
73         {
74                 Geometry rgeom;
75                 rgeom.w=icon->get_width();
76                 rgeom.h=icon->get_height();
77                 part.get_alignment().apply(rgeom, geom, part.get_margin());
78
79                 icon->bind();
80                 GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
81                 imm.color(1.0f, 1.0f, 1.0f);
82                 imm.begin(GL::QUADS);
83                 imm.texcoord(0, 0);
84                 imm.vertex(rgeom.x, rgeom.y);
85                 imm.texcoord(1, 0);
86                 imm.vertex(rgeom.x+rgeom.w, rgeom.y);
87                 imm.texcoord(1, 1);
88                 imm.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
89                 imm.texcoord(0, 1);
90                 imm.vertex(rgeom.x, rgeom.y+rgeom.h);
91                 imm.end();
92                 GL::Texture::unbind();
93         }
94 }
95
96
97 Button::Loader::Loader(Button &btn):
98         Widget::Loader(btn)
99 {
100         add("text", &Loader::text);
101 }
102
103 void Button::Loader::text(const std::string &t)
104 {
105         static_cast<Button &>(wdg).text=t;
106 }
107
108 } // namespace GLtk
109 } // namespace Msp