Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
HyperlinkWidget.cpp
1 /*
2  Harmonic Flow Framework
3  Copyright (C) 2011-2018 Andreas Widen <andreas@harmonicflow.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "HyperlinkWidget.h"
22 #include "WidgetManager.h"
23 #include "platform/Environment.h"
24 #include "platform/Graphics.h"
25 #include <assert.h>
26 
27 using namespace HFCore;
28 
29 #include "debug/CrtDbgNew.h"
30 
32  Font *font, UString &label, Color color, Color overColor) :
33  ButtonWidget(id, buttonListener, font, label),
34  mColor(color), mOverColor(overColor)
35 {
36  assert(buttonListener != NULL);
37  assert(font != NULL);
38  assert(label.length() != 0);
39 
40  mDoFinger = true;
41  mUnderlineOffset = 3;
42  mUnderlineSize = 1;
43 }
44 
45 //////////////////////////////////////////////////////////////////////////
46 //////////////////////////////////////////////////////////////////////////
48 {
49 }
50 
52 {
53  fp32_hf fontX = mX; // BUTTON_LABEL_LEFT
54  fp32_hf fontY = mY;
55 
56  if (mFont != NULL && mLabel.length() != 0)
57  {
58  fp32_hf labelw = (fp32_hf)mFont->getStringWidth(mLabel) / 2;
59  fp32_hf width = (mWidth / 2) - 2;
60 
62  {
63  fontX = width - labelw + mX;
64  }
66  {
67  fontX = mX + (mWidth - mFont->getStringWidth(mLabel));
68  }
69 
70  fontY = (mHeight / 2 - 1) + mY;
71  }
72 
73  if (mIsDown)
74  {
75  fontX += 1;
76  fontY += 1;
77  }
78 
79  Color color = mColor;
80  if (mIsOver)
81  {
82  color = mOverColor;
83  }
84 
85  // draw label:
86  drawLabel(g, fontX, fontY, color, true);
87 
88  // draw underline:
89  if (mIsOver)
90  {
91  int32_hf underlineY = (int32_hf)fontY + ((int32_hf)mHeight / 2);
92  for (int32_hf i = 0; i < mUnderlineSize; i++)
93  {
94  mRenderHelper.drawRect(g, fontX, (fp32_hf)(underlineY + mUnderlineOffset + i),
95  (fp32_hf)(mFont->getStringWidth(mLabel)), 1, color);
96  }
97  }
98 }
99 
100 void HyperlinkWidget::drawLabel(Graphics *g, fp32_hf x, fp32_hf y,
101  Color color, bool colorize)
102 {
103  mRenderHelper.beginBatch(g, Graphics::BATCHMODE_STATIC);
104  mRenderHelper.drawString(g, mFont, color, mLabel, (int32_hf)x, (int32_hf)y);
105  mRenderHelper.endBatch(g, true);
106 }
void endBatch(Graphics *g, bool render)
End current Batch.
virtual int32_hf getStringWidth(const UString &str, fp32_hf scale=1.0f)=0
Get the width of str.
Interface for all rendering related things.
fp32_hf drawString(Graphics *g, Font *font, Color color, const UString &string, int32_hf x, int32_hf y, fp32_hf scale=1.0f)
Draw a string.
Button label text alignment CENTER.
Definition: ButtonWidget.h:125
UString mLabel
Holds Button label text.
Definition: ButtonWidget.h:280
Interface for unicode string class UString.
Definition: UString.h:35
Interface for UI ButtonWidget.
Definition: ButtonWidget.h:116
int int32_hf
A type definition for int.
Definition: HFDataTypes.h:349
int32_hf mLabelJustify
Holds Button label justify. Valid values are: ButtonWidget::BUTTON_LABEL_LEFT, ButtonWidget::BUTTON_L...
Definition: ButtonWidget.h:288
Interface for ButtonWidget listener callback methods.
void drawRect(Graphics *g, fp32_hf x0, fp32_hf y0, fp32_hf w, fp32_hf h, Color color, Graphics::DrawMode drawMode=Graphics::DRAWMODE_NORMAL)
Draw a Rect.
fp32_hf mX
Holds Widget X position.
bool mIsOver
Flag indicating if over or not.
Definition: Widget.h:1070
int32_hf mUnderlineSize
Holds underline size.
virtual ~HyperlinkWidget()
Destructor.
bool mDoFinger
Flag indicating if we should show finger or not.
Definition: Widget.h:1091
fp32_hf width()
Get Widget width.
Definition: Widget.h:994
virtual void draw(Graphics *g)
Called by the framework once for every iteration of the main loop.
Button label text alignment RIGHT.
Definition: ButtonWidget.h:126
#define NULL
Convenient define for 0.
Definition: HFDataTypes.h:42
Color mOverColor
Holds over Color.
Useful for static content.
Definition: Graphics.h:199
Font * mFont
Holds pointer to Font to use for label text.
Definition: ButtonWidget.h:293
int32_hf length() const
Get the length of the string.
Definition: UString.cpp:178
Interface for RGBA Color.
Definition: Color.h:51
Interface for UI HyperlinkWidget.
fp32_hf mY
Holds Widget Y position.
int32_hf mUnderlineOffset
Holds underline offset.
Definition: Actor.h:34
Interface for Resource type Font.
Definition: Font.h:84
Singleton interface for platform specific things.
Color mColor
Holds normal Color.
Interface for all rendering related things.
Definition: Graphics.h:120
WidgetManager manages Widget&#39;s and interacts with Game interface.
void beginBatch(Graphics *g, Graphics::BatchMode mode, Rect clipRect=Rect::ZERO())
Begin Batch using specified Graphics::BatchMode and optional clipping Rect.
bool mIsDown
Flag indicating if down or not.
Definition: Widget.h:1065
HyperlinkWidget(int32_hf id, ButtonListener *buttonListener, Font *font, UString &label, Color color, Color overColor)
Constructor.
fp32_hf mWidth
Holds Widget width.
float fp32_hf
A type definition for float.
Definition: HFDataTypes.h:359
fp32_hf mHeight
Holds Widget height.