~ubuntu-branches/ubuntu/edgy/pouetchess/edgy-updates

« back to all changes in this revision

Viewing changes to src/sxmlgui/GUIButton.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc (mr_pouit)
  • Date: 2006-07-15 15:45:57 UTC
  • Revision ID: james.westby@ubuntu.com-20060715154557-3paxq02hx4od0wm4
Tags: upstream-0.2.0
ImportĀ upstreamĀ versionĀ 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "EasyGL.h"
 
2
 
 
3
GUIButton::GUIButton(const std::string &callback) :  GUIAlphaElement(callback), GUIClippedRectangle()
 
4
{
 
5
  setBordersColor(0.0f, 0.0f, 0.0f);
 
6
  setDimensions(40, 22);
 
7
  setPosition(0.5, 0.5);
 
8
  setColor(100, 150, 190);
 
9
 
 
10
  widgetType     = WT_BUTTON;
 
11
 
 
12
  drawBackground = true;
 
13
  drawBounds     = true;
 
14
  bounce         = true;
 
15
}
 
16
 
 
17
bool GUIButton::loadXMLSettings(const TiXmlElement *element)
 
18
{
 
19
  if(!XMLArbiter::inspectElementInfo(element, "Button"))
 
20
    return Logger::writeErrorLog("Need a Button node in the xml file");
 
21
 
 
22
  enableBounce(XMLArbiter::analyzeBooleanAttr(element, "bounce", true));
 
23
  return GUIAlphaElement::loadXMLSettings(element) &&
 
24
         GUIClippedRectangle::loadXMLClippedRectangleInfo(element);
 
25
}
 
26
 
 
27
void GUIButton::enableBounce(bool bounce_){ bounce = bounce_; }
 
28
bool GUIButton::bounceEnabled()           { return bounce;    }
 
29
 
 
30
void GUIButton::render(float clockTick)
 
31
{
 
32
  if(!parent || !visible)
 
33
    return;
 
34
 
 
35
  modifyCurrentAlpha(clockTick);
 
36
  bgColor = color;
 
37
 
 
38
  Tuple3f tempColor    = label.getColor();
 
39
  float   displacement = 2.0f*(pressed || clicked)*bounce;
 
40
  int     xCenter      = (windowBounds.x + windowBounds.z)/2,
 
41
          yCenter      = (windowBounds.y + windowBounds.w)/2;
 
42
 
 
43
  glTranslatef(displacement, displacement, 0.0);
 
44
  renderClippedBounds();
 
45
  label.printCenteredXY(xCenter, yCenter);
 
46
  glTranslatef(-displacement, -displacement, 0.0f);
 
47
}
 
48
 
 
49
const void GUIButton::computeWindowBounds()
 
50
{
 
51
  if(parent && update)
 
52
  {
 
53
    GUIRectangle::computeWindowBounds();
 
54
    label.computeDimensions();
 
55
 
 
56
    int width  = windowBounds.z - windowBounds.x,
 
57
        height = windowBounds.w - windowBounds.y;
 
58
 
 
59
    if(width  <= label.getWidth() + 2*clipSize)
 
60
    {
 
61
      if(anchor == AT_CENTER)
 
62
      {
 
63
        width = (label.getWidth() - width)/2 + clipSize + 2;
 
64
        windowBounds.x -=width;
 
65
        windowBounds.z +=width;
 
66
      }
 
67
      if((anchor == AT_CORNERLU) || (anchor == AT_CORNERLD))
 
68
      {
 
69
        width = (label.getWidth() - width)/2 + clipSize + 2;
 
70
        windowBounds.z +=2*width;
 
71
      }
 
72
    }
 
73
 
 
74
    if(height + 2*clipSize <  label.getHeight())
 
75
    {
 
76
 
 
77
      height = (label.getHeight() - height)/2 + clipSize + 2;
 
78
      windowBounds.y -= height;
 
79
      windowBounds.w += height;
 
80
    }
 
81
 
 
82
    computeClippedBounds(windowBounds);
 
83
  }
 
84
}