Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
CheckboxWidget.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 "CheckboxWidget.h"
22 #include "CheckboxListener.h"
23 #include "platform/Graphics.h"
24 #include "platform/Image.h"
25 #include <assert.h>
26 
27 using namespace HFCore;
28 
29 #include "debug/CrtDbgNew.h"
30 
32  Image *checkedImage, Image *uncheckedImage, Image *checkboxOver) :
33  mListener(checkboxListener),
34  mId(id),
35  mChecked(false),
36  mCheckedImage(checkedImage),
37  mUncheckedImage(uncheckedImage),
38  mCheckedOverImage(checkboxOver),
39  mAtlasImage(NULL),
40  mCheckedRect(Rect::ZERO()),
41  mUncheckedRect(Rect::ZERO()),
42  mCheckedOverRect(Rect::ZERO())
43 {
44  assert(mListener != NULL);
45  assert(checkedImage != NULL && uncheckedImage != NULL);
46 
47  mDoFinger = true;
48  mUsingAtlas = false;
49 }
50 
52  const Rect &checkedRect, const Rect &uncheckedRect,
53  const Rect &checkboxOverRect) :
54  mListener(checkboxListener),
55  mId(id),
56  mChecked(false),
60  mAtlasImage(atlasImage),
61  mCheckedRect(checkedRect),
62  mUncheckedRect(uncheckedRect),
63  mCheckedOverRect(checkboxOverRect)
64 {
65  assert(mListener != NULL);
66  assert(mAtlasImage != NULL && mCheckedRect != Rect::ZERO()
67  && mUncheckedRect != Rect::ZERO());
68 
69  mDoFinger = true;
70  mUsingAtlas = true;
71 }
72 
73 void CheckboxWidget::setChecked(bool checked, bool tellListener)
74 {
75  mChecked = checked;
76 
77  if (tellListener && mListener)
78  {
80  }
81 }
82 
84 {
85  return mChecked;
86 }
87 
89 {
90  Widget::draw(g);
91 
92  // atlas version:
93  if (mUsingAtlas)
94  {
95  if (mChecked)
96  {
97  fp32_hf x = (mCheckedRect.getWidth() / 2) + mX;
98  fp32_hf y = (mCheckedRect.getHeight() / 2) + mY;
99 
100  mRenderHelper.drawImage(g, mAtlasImage, mCheckedRect, x, y, 1.0f,
103  }
104  else
105  {
106  if (mIsOver && mCheckedOverRect != Rect::ZERO())
107  {
108  fp32_hf x = (mCheckedOverRect.getWidth() / 2) + mX;
109  fp32_hf y = (mCheckedOverRect.getHeight() / 2) + mY;
110 
111  mRenderHelper.drawImage(g, mAtlasImage, mCheckedOverRect, x,
112  y, 1.0f, Graphics::DRAWMODE_NORMAL,
114  }
115  else
116  {
117  fp32_hf x = (mUncheckedRect.getWidth() / 2) + mX;
118  fp32_hf y = (mUncheckedRect.getHeight() / 2) + mY;
119 
120  mRenderHelper.drawImage(g, mAtlasImage, mUncheckedRect, x, y,
123  }
124  }
125  }
126  // per image:
127  else
128  {
129  if (mChecked)
130  {
131  fp32_hf x = ((fp32_hf)mCheckedImage->getWidth() / 2) + mX;
132  fp32_hf y = ((fp32_hf)mCheckedImage->getHeight() / 2) + mY;
133 
134  mRenderHelper.drawImage(g, mCheckedImage, x, y, 1.0f, Graphics::DRAWMODE_NORMAL,
136  }
137  else
138  {
139  if (mIsOver && mCheckedOverImage != NULL)
140  {
141  fp32_hf x = ((fp32_hf)mCheckedOverImage->getWidth() / 2) + mX;
142  fp32_hf y = ((fp32_hf)mCheckedOverImage->getHeight() / 2) + mY;
143 
144  mRenderHelper.drawImage(g, mCheckedOverImage, x, y, 1.0f,
147  }
148  else
149  {
150  fp32_hf x = ((fp32_hf)mUncheckedImage->getWidth() / 2) + mX;
151  fp32_hf y = ((fp32_hf)mUncheckedImage->getHeight() / 2) + mY;
152 
153  mRenderHelper.drawImage(g, mUncheckedImage, x, y, 1.0f, Graphics::DRAWMODE_NORMAL,
155  }
156  }
157  }
158 }
159 
161 {
162  Widget::touchDown(touch);
163 
164  mChecked = !mChecked;
165 
166  if (mListener)
167  {
170  }
171 }
172 
174 {
176 
177  mChecked = !mChecked;
178 
179  if (mListener)
180  {
183  }
184 }
185 
187 {
188  Widget::mouseEnter(mouse);
189 
190  if (mListener)
191  {
194  }
195 }
196 
198 {
199  Widget::mouseLeave(mouse);
200 
201  if (mListener)
202  {
205  }
206 }
bool mUsingAtlas
Flag indicating if using Image atlas or not.
Interface for all rendering related things.
virtual void setBatchUIUpdateNeeded(bool update)
Set BatchUI update needed flag.
Definition: Widget.cpp:503
int32_hf mId
Holds Checkbox ID.
virtual void checkboxMouseLeave(int32_hf id)
Called automatically when a checkboxMouseLeave event occurs.
virtual void mouseEnter(Mouse::MouseInfo mouse)
Called automatically when Mouse enter event occurs.
fp32_hf getWidth() const
Get Rect Width (or maxX).
Definition: Rect.h:168
int int32_hf
A type definition for int.
Definition: HFDataTypes.h:349
CheckboxListener * mListener
Holds pointer to CheckboxListener class.
virtual void mouseLeave(Mouse::MouseInfo mouse)
Called automatically when Mouse leave event occurs.
Definition: Widget.cpp:292
virtual void mouseLeave(Mouse::MouseInfo mouse)
Called automatically when Mouse leave event occurs.
CheckboxWidget(int32_hf id, CheckboxListener *checkboxListener, Image *checkedImage, Image *uncheckedImage, Image *checkboxOver=NULL)
Constructor.
static Rect ZERO()
Initialize Rect with 0 for all members.
Definition: Rect.h:141
Interface for UI CheckboxWidget.
fp32_hf mX
Holds Widget X position.
Rect mCheckedOverRect
Holds Rect for mouse over checkbox Image region within mAtlasImage.
Rect mCheckedRect
Holds Rect for checked checkbox Image region within mAtlasImage.
bool mIsOver
Flag indicating if over or not.
Definition: Widget.h:1070
virtual void touchDown(Touch::TouchInfo touch)
Called automatically when Touch down event occurs.
Image * mCheckedOverImage
Holds pointer to mouse over checkbox Image.
bool mDoFinger
Flag indicating if we should show finger or not.
Definition: Widget.h:1091
A structure that contains Touch event information.
Definition: Touch.h:50
Interface for Resource type Image.
#define NULL
Convenient define for 0.
Definition: HFDataTypes.h:42
Image * mCheckedImage
Holds pointer to checked checkbox Image.
virtual Graphics::MinFilter getMinRenderFilter()
Get Graphics::MinFilter currently set.
Definition: Widget.cpp:488
virtual void checkboxChecked(int32_hf id, bool checked)
Called automatically when a checkboxChecked event occurs.
virtual void mouseEnter(Mouse::MouseInfo mouse)
Called automatically when Mouse enter event occurs.
Definition: Widget.cpp:288
A structure that contains Mouse event information.
Definition: Mouse.h:63
virtual void checkboxMouseEnter(int32_hf id)
Called automatically when a checkboxMouseEnter event occurs.
Interface for CheckboxWidget listener callback methods.
Rect mUncheckedRect
Holds Rect for unchecked checkbox Image region within mAtlasImage.
virtual Graphics::MagFilter getMagRenderFilter()
Get Graphics::MagFilter currently set.
Definition: Widget.cpp:483
Image * mAtlasImage
Holds pointer to atlas Image.
fp32_hf mY
Holds Widget Y position.
Definition: Actor.h:34
Interface for Resource type Image.
Definition: Image.h:63
Draw mode default / normal.
Definition: Graphics.h:143
Image * mUncheckedImage
Holds pointer to unchecked checkbox Image.
void drawImage(Graphics *g, Image *img, fp32_hf x, fp32_hf y, fp32_hf alpha=1.0f, Graphics::DrawMode drawMode=Graphics::DRAWMODE_NORMAL, Graphics::MagFilter magFilter=Graphics::MAGFILTER_LINEAR, Graphics::MinFilter minFilter=Graphics::MINFILTER_LINEAR)
Draw a Image.
Definition: RenderHelper.h:530
Specialized Boundary representing a Rect.
Definition: Rect.h:34
Interface for all rendering related things.
Definition: Graphics.h:120
virtual void mouseButtonDown(Mouse::MouseInfo mouse)
Called automatically when Mouse button down event occurs.
Definition: Widget.cpp:276
virtual int32_hf getHeight()=0
Get the Image original height.
fp32_hf getHeight() const
Get Rect Height (or maxY).
Definition: Rect.h:177
bool mChecked
Flag indicating whether Checkbox is in checked state or not.
virtual void mouseButtonDown(Mouse::MouseInfo mouse)
Called automatically when Mouse button down event occurs.
Interface for CheckboxWidget listener callback methods.
virtual void setChecked(bool checked, bool tellListener=true)
Set Checkbox checked / unchecked state.
virtual void draw(Graphics *g)
Called by the framework once for every iteration of the main loop.
virtual int32_hf getWidth()=0
Get the Image original width.
virtual void touchDown(Touch::TouchInfo touch)
Called automatically when Touch down event occurs.
Definition: Widget.cpp:260
virtual bool isChecked()
Determine whether Checkbox is in checked state.
float fp32_hf
A type definition for float.
Definition: HFDataTypes.h:359
virtual void draw(Graphics *g)
Called by the framework once for every iteration of the main loop.
Definition: Widget.cpp:212