From 8f6f543758fb1b2b17ae2db519198d420eb9982d Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 14 Dec 2007 16:20:48 +0000 Subject: [PATCH] Refactor filling from Part to Alignment Implement VSlider Make [HV]Slider loabable from datafiles --- source/entry.cpp | 13 ++----- source/geometry.cpp | 10 ++++++ source/geometry.h | 4 +-- source/hslider.cpp | 14 ++++---- source/hslider.h | 2 +- source/panel.cpp | 4 +++ source/part.cpp | 14 +++----- source/part.h | 6 +--- source/slider.cpp | 14 ++++++++ source/slider.h | 12 +++++-- source/vslider.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++ source/vslider.h | 37 +++++++++++++++++++ 12 files changed, 180 insertions(+), 36 deletions(-) create mode 100644 source/vslider.cpp create mode 100644 source/vslider.h diff --git a/source/entry.cpp b/source/entry.cpp index eed2c39..7e503b3 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -81,21 +81,14 @@ void Entry::render_special(const Part &part) const const GL::Font *const font=style->get_font(); const float font_size=font->get_default_size(); - const Geometry &pgeom=part.get_geometry(); - unsigned gw=pgeom.w; - unsigned gh=(part.get_fill_y() ? geom.h : pgeom.h); - - Geometry rgeom; + Geometry rgeom=part.get_geometry(); + rgeom.x=static_cast(font->get_string_width(text.substr(0, edit_pos))*font_size); rgeom.w=static_cast(font->get_string_width(text)*font_size); - rgeom.h=(part.get_fill_y() ? geom.h : pgeom.h); part.get_alignment().apply(rgeom, geom, part.get_margin()); - rgeom.x+=static_cast(font->get_string_width(text.substr(0, edit_pos))*font_size); GL::push_matrix(); GL::translate(rgeom.x, rgeom.y, 0); - - part.get_graphic(state)->render(gw, gh); - + part.get_graphic(state)->render(part.get_geometry().w, rgeom.h); GL::pop_matrix(); } } diff --git a/source/geometry.cpp b/source/geometry.cpp index 81efcd0..126a0fc 100644 --- a/source/geometry.cpp +++ b/source/geometry.cpp @@ -36,12 +36,22 @@ Sides::Loader::Loader(Sides &s): void Alignment::apply(Geometry &geom, const Geometry &parent) const { + if(parent.w>geom.w) + geom.w+=static_cast((parent.w-geom.w)*w); + if(parent.h>geom.h) + geom.h+=static_cast((parent.h-geom.h)*h); + geom.x+=static_cast((parent.w-geom.w)*x); geom.y+=static_cast((parent.h-geom.h)*y); } void Alignment::apply(Geometry &geom, const Geometry &parent, const Sides &margin) const { + if(parent.w>geom.w) + geom.w+=static_cast((parent.w-geom.w)*w); + if(parent.h>geom.h) + geom.h+=static_cast((parent.h-geom.h)*h); + geom.x+=static_cast(margin.left+(parent.w-margin.left-margin.right-geom.w)*x); geom.y+=static_cast(margin.bottom+(parent.h-margin.bottom-margin.top-geom.h)*y); } diff --git a/source/geometry.h b/source/geometry.h index 6559414..6fa8e7f 100644 --- a/source/geometry.h +++ b/source/geometry.h @@ -56,9 +56,9 @@ Performs alignment of nested geometries, such as widgets and their parts. struct Alignment { float x, y; + float w, h; - Alignment(): x(0), y(0) { } - Alignment(float x_, float y_): x(x_), y(y_) { } + Alignment(): x(0), y(0), w(1), h(1) { } void apply(Geometry &, const Geometry &) const; void apply(Geometry &, const Geometry &, const Sides &) const; }; diff --git a/source/hslider.cpp b/source/hslider.cpp index d764797..3b68dac 100644 --- a/source/hslider.cpp +++ b/source/hslider.cpp @@ -59,13 +59,15 @@ void HSlider::render_special(const Part &part) const { if(part.get_name()=="slider") { - const Geometry &pgeom=part.get_geometry(); - unsigned gw=pgeom.w; - unsigned gh=(part.get_fill_y() ? geom.h : pgeom.h); + Alignment align=part.get_alignment(); + align.x=(value-min)/(max-min); + + Geometry pgeom=part.get_geometry(); + align.apply(pgeom, geom, part.get_margin()); + GL::push_matrix(); - GL::translate((geom.w-gw)*(value-min)/(max-min), (geom.h-gh)*(part.get_alignment().y+1)/2, 0); - const Graphic *graphic=part.get_graphic(state); - graphic->render(gw, gh); + GL::translate(pgeom.x, pgeom.y, 0); + part.get_graphic(state)->render(pgeom.w, pgeom.h); GL::pop_matrix(); } } diff --git a/source/hslider.h b/source/hslider.h index 310cc4e..8f0be9f 100644 --- a/source/hslider.h +++ b/source/hslider.h @@ -15,7 +15,7 @@ namespace GLtk { /** Horizontal slider widget. A special part named "slider" will be positioned at -the current value of the widget. The fill_x attribute is ignored. +the current value of the widget. */ class HSlider: public Slider { diff --git a/source/panel.cpp b/source/panel.cpp index bfd0aa8..91562ce 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -8,9 +8,11 @@ Distributed under the LGPL #include #include "button.h" #include "entry.h" +#include "hslider.h" #include "label.h" #include "panel.h" #include "part.h" +#include "vslider.h" using namespace std; @@ -181,8 +183,10 @@ Panel::Loader::Loader(Panel &p, map &m): { add("button", &Loader::child