// Ryzom - MMORPG Framework
// Copyright (C) 2010 Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
#include "nel/gui/group_scrolltext.h"
#include "nel/gui/group_list.h"
#include "nel/gui/view_text.h"
#include "nel/gui/ctrl_scroll.h"
#include "nel/gui/ctrl_button.h"
#include "nel/gui/action_handler.h"
#include "nel/misc/i18n.h"
#include "nel/gui/widget_manager.h"
NLMISC_REGISTER_OBJECT(CViewBase, CGroupScrollText, std::string, "scroll_text");
namespace NLGUI
{
//========================================================================
CGroupScrollText::CGroupScrollText(const TCtorParam ¶m) :
CInterfaceGroup(param),
_List(NULL),
_ScrollBar(NULL),
_ButtonAdd(NULL),
_ButtonSub(NULL),
_Settuped(false),
_InvertScrollBar(true),
_ListHeight(0),
_StartHeight(0),
_EllapsedTime(0)
{
_IsGroupScrollText = true;
}
//========================================================================
CGroupScrollText::~CGroupScrollText()
{
}
std::string CGroupScrollText::getProperty( const std::string &name ) const
{
if( name == "invert_scroll_bar" )
return NLMISC::toString( _InvertScrollBar );
else
return CInterfaceGroup::getProperty( name );
}
void CGroupScrollText::setProperty( const std::string &name, const std::string &value )
{
if( name == "invert_scroll_bar" )
{
bool b;
if( NLMISC::fromString( value, b ) )
_InvertScrollBar = b;
return;
}
else
CInterfaceGroup::setProperty( name, value );
}
xmlNodePtr CGroupScrollText::serialize( xmlNodePtr parentNode, const char *type ) const
{
xmlNodePtr node = CInterfaceGroup::serialize( parentNode, type );
if( node == NULL )
return NULL;
xmlSetProp( node, BAD_CAST "type", BAD_CAST "scroll_text" );
xmlSetProp( node, BAD_CAST "invert_scroll_bar", BAD_CAST NLMISC::toString( _InvertScrollBar ).c_str() );
return node;
}
//========================================================================
bool CGroupScrollText::parse(xmlNodePtr cur,CInterfaceGroup *parentGroup)
{
if(!CInterfaceGroup::parse(cur, parentGroup))
return false;
CXMLAutoPtr ptr;
// invert scroll bar?
ptr = xmlGetProp (cur, (xmlChar*)"invert_scroll_bar");
if(ptr) _InvertScrollBar= convertBool(ptr);
return true;
}
//========================================================================
void CGroupScrollText::updateCoords()
{
CInterfaceGroup::updateCoords();
if (!_Settuped) setup();
updateScrollBar();
// re-update of scrollbar
if(_ScrollBar)
_ScrollBar->updateCoords();
}
//========================================================================
void CGroupScrollText::updateScrollBar()
{
if (_List && _ScrollBar)
{
if (_List->getHReal() < _List->getMaxHReal())
{
_ScrollBar->setActive(false);
}
else
{
_ScrollBar->setActive(true);
}
}
}
//========================================================================
void CGroupScrollText::checkCoords ()
{
// update scrollbar if necessary
if (_List)
{
if (_List->getH() != _ListHeight) // see if the scrollbar should be updated
{
invalidateCoords();
_ListHeight = _List->getH();
}
}
CInterfaceGroup::checkCoords();
}
//========================================================================
void CGroupScrollText::draw()
{
CInterfaceGroup::draw();
}
//========================================================================
void CGroupScrollText::clearViews()
{
CInterfaceGroup::clearViews();
}
//========================================================================
bool CGroupScrollText::handleEvent(const NLGUI::CEventDescriptor &event)
{
if (event.getType() == NLGUI::CEventDescriptor::mouse)
{
const NLGUI::CEventDescriptorMouse &eventDesc = (const NLGUI::CEventDescriptorMouse &)event;
//
if (_List && _ScrollBar)
{
if (eventDesc.getEventTypeExtended() == NLGUI::CEventDescriptorMouse::mousewheel)
{
if (isIn(eventDesc.getX(), eventDesc.getY()))
{
sint32 h = _List->getMaxHReal() / 2;
if (h == 0) h = 1;
_ScrollBar->moveTargetY(- eventDesc.getWheel() * h);
return true;
}
}
}
}
if (CInterfaceGroup::handleEvent(event)) return true;
return false;
}
//========================================================================
void CGroupScrollText::setup()
{
// bind to the controls
_ScrollBar = dynamic_cast(CInterfaceGroup::getCtrl("scroll_bar"));
_ButtonAdd = dynamic_cast(CInterfaceGroup::getCtrl("button_add"));
_ButtonSub = dynamic_cast(CInterfaceGroup::getCtrl("button_sub"));
_List = dynamic_cast(CInterfaceGroup::getGroup("text_list"));
if(_ScrollBar == NULL)
nlwarning(" scroll bar 'scroll_bar' missing or bad type.(%s)",this->_Id.c_str());
// Add and sub button are not required
/*
if(buttonAdd == NULL)
nlwarning("Interface: CGroupScrollText: button 'button_add' missing or bad type");
if(buttonSub == NULL)
nlwarning("Interface: CGroupScrollText: button 'button_sub' missing or bad type");
*/
if(_List == NULL)
nlwarning(" group list 'text_list' missing or bad type");
// actions
if (_ButtonAdd) _ButtonAdd->setActionOnClockTick("gst_add");
if (_ButtonSub) _ButtonSub->setActionOnClockTick("gst_sub");
// bind the scrollbar to the list
if (_ScrollBar)
{
_ScrollBar->setTarget(_List);
//_ScrollBar->setInverted(_InvertScrollBar);
}
_Settuped = true;
}
//========================================================================
void CGroupScrollText::elementCaptured(CCtrlBase *capturedElement)
{
if (capturedElement == _ButtonAdd || capturedElement == _ButtonSub)
{
// reset the counters for increase
_EllapsedTime = 0;
_StartHeight = getH();
}
}
// ***************************************************************************
// ***************************************************************************
// Actions Handlers
// ***************************************************************************
// ***************************************************************************
// ***************************************************************************
class CSTUp : public IActionHandler
{
public:
virtual void execute (CCtrlBase *pCaller, const std::string &/* Params */)
{
const CWidgetManager::SInterfaceTimes × = CWidgetManager::getInstance()->getInterfaceTimes();
CGroupScrollText *pST = dynamic_cast(pCaller->getParent());
if (pST == NULL) return;
if (pST->getList() == NULL) return;
// get the font height from the text template of the list
const CViewText *vt = pST->getList()->getTextTemplatePtr();
if (!vt) return;
pST->_EllapsedTime += times.frameDiffMs;
// pST->setH(std::min((sint32) pST->getMaxHeight(), (sint32) (pST->_StartHeight + pST->_EllapsedTime / 9)));
pST->setH((sint32) (pST->_StartHeight + pST->_EllapsedTime / 9));
pST->invalidateCoords();
}
};
REGISTER_ACTION_HANDLER (CSTUp, "gst_add");
// ***************************************************************************
class CSTDown : public IActionHandler
{
public:
virtual void execute (CCtrlBase *pCaller, const std::string &/* Params */)
{
const CWidgetManager::SInterfaceTimes × = CWidgetManager::getInstance()->getInterfaceTimes();
CGroupScrollText *pST = dynamic_cast(pCaller->getParent());
if (pST == NULL) return;
if (pST->getList() == NULL) return;
// get the font height from the text template of the list
const CViewText *vt = pST->getList()->getTextTemplatePtr();
if (!vt) return;
pST->_EllapsedTime += times.frameDiffMs;
// pST->setH(std::max((sint32) pST->getMinHeight(), (sint32) (pST->_StartHeight - pST->_EllapsedTime / 9)));
pST->setH((sint32) (pST->_StartHeight - pST->_EllapsedTime / 9));
pST->invalidateCoords();
}
};
REGISTER_ACTION_HANDLER (CSTDown, "gst_sub");
}