]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/button.cpp
Use triangle strips instead of quads
[libs/gltk.git] / source / button.cpp
index 243225964caa37752a8ee1979c398577f96482a8..86b73c9daa0c3f8ba9a689ebf628f102667c97f7 100644 (file)
+#include <msp/gl/meshbuilder.h>
 #include "button.h"
 #include "part.h"
+#include "style.h"
+
+using namespace std;
 
 namespace Msp {
 namespace GLtk {
 
-Button::Button(const Resources &r, const std::string &t):
-       Widget(r)
+Button::Button(const std::string &t):
+       text(),
+       icon(0),
+       pressed(false)
 {
        set_text(t);
-       update_style();
+}
+
+void Button::autosize_special(const Part &part, Geometry &ageom) const
+{
+       if(part.get_name()=="text")
+               text.autosize(part, ageom);
+       else if(part.get_name()=="icon" && icon)
+       {
+               const Sides &margin = part.get_margin();
+               ageom.w = max(ageom.w, icon->get_width()+margin.left+margin.right);
+               ageom.h = max(ageom.h, icon->get_height()+margin.top+margin.bottom);
+       }
 }
 
 void Button::set_text(const std::string &t)
 {
-       text=t;
+       text = t;
+       signal_autosize_changed.emit();
+       rebuild();
 }
 
-void Button::render_part(const Part &part) const
+void Button::set_icon(const GL::Texture2D *i)
+{
+       icon = i;
+       rebuild();
+}
+
+void Button::rebuild_special(const Part &part)
 {
        if(part.get_name()=="text")
+               text.build(part, state, geom, part_cache);
+       if(part.get_name()=="icon")
        {
-               render_text(part, text);
-               /*const GL::Font *const font=style->get_font();
+               if(icon)
+               {
+                       Geometry rgeom;
+                       rgeom.w = icon->get_width();
+                       rgeom.h = icon->get_height();
+                       part.get_alignment().apply(rgeom, geom, part.get_margin());
 
-               const float font_size=font->get_default_size();
-               unsigned text_w=static_cast<unsigned>(font->get_string_width(text)*font_size);
+                       GL::MeshBuilder bld(part_cache.create_mesh(part, *icon));
+                       bld.color(1.0f, 1.0f, 1.0f);
+                       bld.begin(GL::TRIANGLE_STRIP);
+                       bld.texcoord(0, 1);
+                       bld.vertex(rgeom.x, rgeom.y+rgeom.h);
+                       bld.texcoord(0, 0);
+                       bld.vertex(rgeom.x, rgeom.y);
+                       bld.texcoord(1, 1);
+                       bld.vertex(rgeom.x+rgeom.w, rgeom.y+rgeom.h);
+                       bld.texcoord(1, 0);
+                       bld.vertex(rgeom.x+rgeom.w, rgeom.y);
+                       bld.end();
+               }
+       }
+}
 
-               part.get_alignment().apply(geom, text_w, static_cast<unsigned>(font_size));
+void Button::button_press(int, int, unsigned btn)
+{
+       if(btn==1)
+       {
+               pressed = true;
+               set_state(ACTIVE);
+       }
+}
 
-               GL::push_matrix();
-               GL::scale_uniform(font_size);
+void Button::button_release(int x, int y, unsigned btn)
+{
+       if(pressed && btn==1)
+       {
+               clear_state(ACTIVE);
+               pressed = false;
 
-               const Color &color=style->get_font_color();
-               glColor3f(color.r, color.g, color.b);
-               font->draw_string(text);
-               glColor3f(1, 1, 1);
+               if(geom.is_inside_relative(x, y))
+                       signal_clicked.emit();
+       }
+}
 
-               GL::pop_matrix();*/
+void Button::pointer_motion(int x, int y)
+{
+       if(pressed)
+       {
+               if(!geom.is_inside_relative(x, y))
+                       clear_state(ACTIVE);
+               else
+                       set_state(ACTIVE);
        }
-       else
-               Widget::render_part(part);
 }
 
-void Button::on_button_press(int, int, unsigned btn)
+void Button::on_style_change()
 {
-       if(btn==1)
-               state=ACTIVE;
+       text.set_style(style);
 }
 
-void Button::on_button_release(int, int, unsigned btn)
+
+Button::Loader::Loader(Button &btn):
+       DataFile::DerivedObjectLoader<Button, Widget::Loader>(btn)
 {
-       if(btn==1/* && x>=0 && y>=0 && x<static_cast<int>(geom.w) && y<static_cast<int>(geom.h)*/)
-       {
-               state=NORMAL;
-               signal_clicked.emit();
-       }
+       add("text", &Loader::text);
+}
+
+void Button::Loader::text(const std::string &t)
+{
+       obj.text = t;
 }
 
 } // namespace GLtk