[DEV] first step of the checkbox ==> TODO : use an herited button...

This commit is contained in:
Edouard DUPIN 2014-01-24 20:10:02 +01:00
parent dbe7585f8b
commit 0a441228f1
5 changed files with 264 additions and 102 deletions

View File

@ -39,7 +39,7 @@ static ewol::Widget* Create(void) {
} }
void ewol::widget::Button::init(ewol::widget::Manager& _widgetManager) { void ewol::widget::Button::init(ewol::widget::Manager& _widgetManager) {
_widgetManager.addWidgetCreator(__class__,&Create); _widgetManager.addWidgetCreator(__class__, &Create);
} }
ewol::widget::Button::Button(const std::string& _shaperName) : ewol::widget::Button::Button(const std::string& _shaperName) :

View File

@ -9,8 +9,21 @@
#include <ewol/widget/CheckBox.h> #include <ewol/widget/CheckBox.h>
#include <ewol/widget/Manager.h> #include <ewol/widget/Manager.h>
const char* const ewol::widget::CheckBox::eventPressed = "pressed";
const char* const ewol::widget::CheckBox::eventDown = "down";
const char* const ewol::widget::CheckBox::eventUp = "up";
const char* const ewol::widget::CheckBox::eventEnter = "enter";
const char* const ewol::widget::CheckBox::eventValue = "value";
const char * const ewol::widget::CheckBox::eventClicked = "clicked"; const char* const ewol::widget::CheckBox::configValue = "value";
const char* const ewol::widget::CheckBox::configShaper = "shaper";
// DEFINE for the shader display system :
#define STATUS_UP (0)
#define STATUS_HOVER (2)
#define STATUS_PRESSED (1)
#define STATUS_DOWN (3)
#undef __class__ #undef __class__
#define __class__ "CheckBox" #define __class__ "CheckBox"
@ -23,14 +36,29 @@ void ewol::widget::CheckBox::init(ewol::widget::Manager& _widgetManager) {
_widgetManager.addWidgetCreator(__class__,&Create); _widgetManager.addWidgetCreator(__class__,&Create);
} }
ewol::widget::CheckBox::CheckBox(const std::string& _newLabel) { ewol::widget::CheckBox::CheckBox(const std::string& _shaperName) :
m_shaper(_shaperName),
m_mouseHover(false),
m_buttonPressed(false),
m_selectableAreaPos(0,0),
m_selectableAreaSize(0,0),
m_value(false) {
addObjectType("ewol::widget::CheckBox"); addObjectType("ewol::widget::CheckBox");
m_label = _newLabel; // add basic Event generated :
addEventId(eventClicked); addEventId(eventPressed);
m_textColorFg = etk::color::black; addEventId(eventDown);
m_textColorBg = etk::color::white; addEventId(eventUp);
m_value = false; addEventId(eventEnter);
addEventId(eventValue);
// add configuration
registerConfig(configValue, "bool", NULL, "Basic value of the widget");
registerConfig(configShaper, "string", NULL, "the display name for config file");
// shaper satatus update:
CheckStatus();
// This widget can have the focus ...
setCanHaveFocus(true); setCanHaveFocus(true);
// Limit event at 1:
setMouseLimit(1); setMouseLimit(1);
} }
@ -39,102 +67,182 @@ ewol::widget::CheckBox::~CheckBox(void) {
} }
void ewol::widget::CheckBox::setShaperName(const std::string& _shaperName) {
EWOL_WARNING("set shaper name : '" << _shaperName << "'");
m_shaper.setSource(_shaperName);
markToRedraw();
}
void ewol::widget::CheckBox::calculateSize(const vec2& _availlable) {
vec2 padding = m_shaper.getPadding();
ewol::Padding ret = calculateSizePadded(_availlable, ewol::Padding(padding.x(), padding.y(), padding.x(), padding.y()));
//EWOL_DEBUG(" configuring : origin=" << origin << " size=" << subElementSize << "");
m_selectableAreaPos = vec2(ret.xLeft(), ret.yButtom());
m_selectableAreaSize = m_size - (m_selectableAreaPos + vec2(ret.xRight(), ret.yTop()));
}
void ewol::widget::CheckBox::calculateMinMaxSize(void) { void ewol::widget::CheckBox::calculateMinMaxSize(void) {
vec3 minSize = m_oObjectText.calculateSize(m_label); vec2 padding = m_shaper.getPadding();
float boxSize = etk_max(20, minSize.y()) + 5; calculateMinMaxSizePadded(ewol::Padding(padding.x(), padding.y(), padding.x(), padding.y()));
m_minSize.setX(boxSize+minSize.x());
m_minSize.setY(etk_max(boxSize, minSize.y())+3);
markToRedraw();
}
void ewol::widget::CheckBox::setLabel(std::string _newLabel) {
m_label = _newLabel;
markToRedraw();
}
void ewol::widget::CheckBox::setValue(bool _val) {
if (m_value == _val) {
return;
}
m_value = _val;
markToRedraw();
}
bool ewol::widget::CheckBox::getValue(void) {
return m_value;
} }
void ewol::widget::CheckBox::onDraw(void) { void ewol::widget::CheckBox::onDraw(void) {
m_oObjectDecoration.draw(); // draw the shaaper (if needed indeed)
m_oObjectText.draw(); m_shaper.draw();
} }
void ewol::widget::CheckBox::onRegenerateDisplay(void) { void ewol::widget::CheckBox::onRegenerateDisplay(void) {
ewol::widget::Container2::onRegenerateDisplay();
if (true == needRedraw()) { if (true == needRedraw()) {
m_oObjectDecoration.clear(); vec2 padding = m_shaper.getPadding();
m_oObjectText.clear(); m_shaper.clear();
m_shaper.setOrigin(vec2ClipInt32(m_selectableAreaPos));
vec3 minSize = m_oObjectText.calculateSize(m_label); m_shaper.setSize(vec2ClipInt32(m_selectableAreaSize));
float boxSize = etk_max(20, minSize.y()) + 5; m_shaper.setInsidePos(vec2ClipInt32(m_selectableAreaPos+padding));
//int32_t fontWidth = ewol::getWidth(fontId, m_label.c_str()); m_shaper.setInsideSize(vec2ClipInt32(m_selectableAreaSize-padding*2.0f));
int32_t posy = (m_size.y() - minSize.y() - 6)/2 + 3; }
//int32_t posx = (m_size.x - fontWidth - 6)/2 + 25; }
void ewol::widget::CheckBox::setValue(bool _val) {
vec3 textPos(boxSize+5, posy, 0); if (m_value != _val) {
m_oObjectText.setPos(textPos); m_value = _val;
m_oObjectText.print(m_label); if (m_value == false) {
m_idWidgetDisplayed = convertId(0);
m_oObjectDecoration.setColor(m_textColorBg); } else {
m_oObjectDecoration.setPos(vec3(2.5f, 2.5f, 0.0f) ); m_idWidgetDisplayed = convertId(1);
m_oObjectDecoration.rectangleWidth(vec3(boxSize, boxSize, 0.0f) );
if (m_value) {
m_oObjectDecoration.setColor(m_textColorFg);
m_oObjectDecoration.setPos(vec3(2.5f, 2.5f, 0.0f) );
m_oObjectDecoration.setThickness(3);
m_oObjectDecoration.lineRel(vec3(boxSize, boxSize, 0.0f) );
} }
CheckStatus();
markToRedraw();
} }
} }
bool ewol::widget::CheckBox::onEventInput(const ewol::event::Input& _event) { bool ewol::widget::CheckBox::onEventInput(const ewol::event::Input& _event) {
//EWOL_DEBUG("Event on checkbox ..."); EWOL_VERBOSE("Event on BT : " << _event);
if (1 == _event.getId()) {
if (ewol::key::statusSingle == _event.getStatus()) { bool previousHoverState = m_mouseHover;
if(true == m_value) { if( ewol::key::statusLeave == _event.getStatus()
m_value = false; || ewol::key::statusAbort == _event.getStatus()) {
generateEventId(eventClicked, "false"); m_mouseHover = false;
} else { m_buttonPressed = false;
m_value = true; } else {
generateEventId(eventClicked, "true"); vec2 relativePos = relativePosition(_event.getPos());
} // prevent error from ouside the button
keepFocus(); if( relativePos.x() < m_selectableAreaPos.x()
markToRedraw(); || relativePos.y() < m_selectableAreaPos.y()
return true; || relativePos.x() > m_selectableAreaPos.x() + m_selectableAreaSize.x()
|| relativePos.y() > m_selectableAreaPos.y() + m_selectableAreaSize.y() ) {
m_mouseHover = false;
m_buttonPressed = false;
} else {
m_mouseHover = true;
} }
} }
return false; bool previousPressed = m_buttonPressed;
EWOL_VERBOSE("Event on BT ... mouse hover : " << m_mouseHover);
if (true == m_mouseHover) {
if (1 == _event.getId()) {
if(ewol::key::statusDown == _event.getStatus()) {
EWOL_VERBOSE(getName() << " : Generate event : " << eventDown);
generateEventId(eventDown);
m_buttonPressed = true;
markToRedraw();
}
if(ewol::key::statusUp == _event.getStatus()) {
EWOL_VERBOSE(getName() << " : Generate event : " << eventUp);
generateEventId(eventUp);
m_buttonPressed = false;
markToRedraw();
}
if(ewol::key::statusSingle == _event.getStatus()) {
// inverse value :
setValue((m_value)?false:true);
EWOL_VERBOSE(getName() << " : Generate event : " << eventPressed);
generateEventId(eventPressed);
EWOL_VERBOSE(getName() << " : Generate event : " << eventValue << " val=" << m_value );
generateEventId(eventValue, std::to_string(m_value));
markToRedraw();
}
}
}
if( m_mouseHover != previousHoverState
|| m_buttonPressed != previousPressed) {
CheckStatus();
}
return m_mouseHover;
} }
bool ewol::widget::CheckBox::onEventEntry(const ewol::event::Entry& _event) { bool ewol::widget::CheckBox::onEventEntry(const ewol::event::Entry& _event) {
//EWOL_DEBUG("BT PRESSED : \"" << UTF8_data << "\" size=" << strlen(UTF8_data)); //EWOL_DEBUG("BT PRESSED : \"" << UTF8_data << "\" size=" << strlen(UTF8_data));
if( _event.getType() == ewol::key::keyboardChar if( _event.getType() == ewol::key::keyboardChar
&& _event.getStatus() == ewol::key::statusDown && _event.getStatus() == ewol::key::statusDown
&& ( _event.getChar() == '\r' && _event.getChar() == '\r') {
|| _event.getChar() == ' ') generateEventId(eventEnter);
) {
if(true == m_value) {
m_value = false;
generateEventId(eventClicked, "false");
} else {
m_value = true;
generateEventId(eventClicked, "true");
}
markToRedraw();
return true; return true;
} }
return false; return false;
} }
void ewol::widget::CheckBox::CheckStatus(void) {
if (true == m_buttonPressed) {
changeStatusIn(STATUS_PRESSED);
} else {
if (true == m_mouseHover) {
changeStatusIn(STATUS_HOVER);
} else {
if (true == m_value) {
changeStatusIn(STATUS_DOWN);
} else {
changeStatusIn(STATUS_UP);
}
}
}
}
void ewol::widget::CheckBox::changeStatusIn(int32_t _newStatusId) {
if (true == m_shaper.changeStatusIn(_newStatusId) ) {
periodicCallEnable();
markToRedraw();
}
}
void ewol::widget::CheckBox::periodicCall(const ewol::event::Time& _event) {
if (false == m_shaper.periodicCall(_event) ) {
periodicCallDisable();
}
markToRedraw();
}
bool ewol::widget::CheckBox::onSetConfig(const ewol::object::Config& _conf) {
if (true == ewol::widget::Container2::onSetConfig(_conf)) {
return true;
}
if (_conf.getConfig() == configValue) {
setValue(std::stob(_conf.getData()));
return true;
}
if (_conf.getConfig() == configShaper) {
setShaperName(_conf.getData());
return true;
}
return false;
}
bool ewol::widget::CheckBox::onGetConfig(const char* _config, std::string& _result) const {
if (true == ewol::widget::Container2::onGetConfig(_config, _result)) {
return true;
}
if (_config == configValue) {
_result = std::to_string(getValue());
return true;
}
if (_config == configShaper) {
_result = m_shaper.getSource();
return true;
}
return false;
}

View File

@ -11,40 +11,83 @@
#include <etk/types.h> #include <etk/types.h>
#include <ewol/debug.h> #include <ewol/debug.h>
#include <ewol/compositing/Text.h> #include <ewol/compositing/Shaper.h>
#include <ewol/compositing/Drawing.h> #include <ewol/widget/Container2.h>
#include <ewol/widget/Widget.h>
#include <ewol/widget/Manager.h> #include <ewol/widget/Manager.h>
namespace ewol { namespace ewol {
namespace widget { namespace widget {
class CheckBox : public ewol::Widget { class CheckBox : public ewol::widget::Container2 {
public:
// Event list of properties
static const char* const eventClicked;
public: public:
//! @not-in-doc
static void init(ewol::widget::Manager& _widgetManager); static void init(ewol::widget::Manager& _widgetManager);
public: // Event list of properties
CheckBox(const std::string& newLabel = "No Label"); static const char* const eventPressed;
virtual ~CheckBox(void); static const char* const eventDown;
void setLabel(std::string newLabel); static const char* const eventUp;
void setValue(bool val); static const char* const eventEnter;
bool getValue(void); static const char* const eventValue;
// Config list of properties
static const char* const configValue;
static const char* const configShaper;
private: private:
ewol::compositing::Text m_oObjectText; ewol::compositing::Shaper m_shaper; //!< Compositing theme.
ewol::compositing::Drawing m_oObjectDecoration; bool m_mouseHover; //!< Flag to know where the mouse is (inside the displayed widget (if not fill)).
std::string m_label; bool m_buttonPressed; //!< Flag to know if the button is curently pressed.
bool m_value; // hover area :
etk::Color<> m_textColorFg; //!< Text color vec2 m_selectableAreaPos; //!< Start position of the events
etk::Color<> m_textColorBg; //!< Background color vec2 m_selectableAreaSize; //!< size of the event positions
public:
/**
* @brief Main checkbox constructor
* @param[in] _shaperName Shaper file properties
*/
CheckBox(const std::string& _shaperName="THEME:GUI:CheckBox.conf");
/**
* @brief main destructor.
*/
virtual ~CheckBox(void);
/**
* @brief set the shaper name (use the contructer one this permit to not noad unused shaper)
* @param[in] _shaperName The new shaper filename
*/
void setShaperName(const std::string& _shaperName);
protected:
bool m_value; //!< Current state of the checkbox.
public:
/**
* @brief set the current value of the checkbox (check or not)
* @param[in] _val New value of the button
*/
void setValue(bool _val);
/**
* @brief get the current button value.
* @return True : The checkbox is active.
* @return false : The checkbox is disable.
*/
bool getValue(void) const {
return m_value;
};
protected:
/**
* @brief internal system to change the property of the current status
* @param[in] _newStatusId new state
*/
void changeStatusIn(int32_t _newStatusId);
/**
* @brief update the status with the internal satte of the button ...
*/
void CheckStatus(void);
protected: // Derived function protected: // Derived function
virtual void onDraw(void); virtual void onDraw(void);
public: // Derived function public: // Derived function
virtual void calculateMinMaxSize(void); virtual void calculateMinMaxSize(void);
virtual void calculateSize(const vec2& _availlable);
virtual void onRegenerateDisplay(void); virtual void onRegenerateDisplay(void);
virtual bool onEventInput(const ewol::event::Input& _event); virtual bool onEventInput(const ewol::event::Input& _event);
virtual bool onEventEntry(const ewol::event::Entry& _event); virtual bool onEventEntry(const ewol::event::Entry& _event);
virtual void periodicCall(const ewol::event::Time& _event);
}; };
}; };
}; };

View File

@ -172,7 +172,7 @@ void ewol::widget::Container2::onRegenerateDisplay(void) {
m_subWidget[m_idWidgetDisplayed]->onRegenerateDisplay(); m_subWidget[m_idWidgetDisplayed]->onRegenerateDisplay();
} }
} }
/*
ewol::Widget* ewol::widget::Container2::getWidgetAtPos(const vec2& _pos) { ewol::Widget* ewol::widget::Container2::getWidgetAtPos(const vec2& _pos) {
if (isHide() == false) { if (isHide() == false) {
if (m_subWidget[m_idWidgetDisplayed] != NULL) { if (m_subWidget[m_idWidgetDisplayed] != NULL) {
@ -181,7 +181,7 @@ ewol::Widget* ewol::widget::Container2::getWidgetAtPos(const vec2& _pos) {
} }
return NULL; return NULL;
} }
*/
bool ewol::widget::Container2::loadXML(exml::Element* _node) { bool ewol::widget::Container2::loadXML(exml::Element* _node) {
if (NULL == _node) { if (NULL == _node) {

View File

@ -138,6 +138,17 @@ namespace ewol {
* @brief Called when parsing a XML and detect the presence of a second Widget * @brief Called when parsing a XML and detect the presence of a second Widget
*/ */
virtual void onDetectPresenceToggleWidget(void) {} virtual void onDetectPresenceToggleWidget(void) {}
/**
* @brief convert ID of the widget if not existed
* @param[in] _id Id of the widget to display.
* @return the id of the widget displayable
*/
int32_t convertId(int32_t _id) {
if (m_subWidget[_id] == NULL) {
return (_id+1)%2;
}
return _id;
}
public: // Derived function public: // Derived function
virtual void systemDraw(const ewol::DrawProperty& _displayProp); virtual void systemDraw(const ewol::DrawProperty& _displayProp);
virtual void onRegenerateDisplay(void); virtual void onRegenerateDisplay(void);
@ -148,7 +159,7 @@ namespace ewol {
virtual void calculateMinMaxSize(void) { virtual void calculateMinMaxSize(void) {
calculateMinMaxSizePadded(); calculateMinMaxSizePadded();
} }
virtual ewol::Widget* getWidgetAtPos(const vec2& _pos); //virtual ewol::Widget* getWidgetAtPos(const vec2& _pos);
virtual ewol::Widget* getWidgetNamed(const std::string& _widgetName); virtual ewol::Widget* getWidgetNamed(const std::string& _widgetName);
virtual bool loadXML(exml::Element* _node); virtual bool loadXML(exml::Element* _node);
virtual void setOffset(const vec2& _newVal); virtual void setOffset(const vec2& _newVal);