BlinkenArea - GitList
Repositories
Blog
Wiki
Blinker
Code
Commits
Branches
Tags
Search
Tree:
a665cf4
Branches
Tags
master
Blinker
src
noarch
CanvasInput.cpp
first version of canvas module (not yet completely happy with it)
Stefan Schuermans
commited
a665cf4
at 2011-11-19 13:35:10
CanvasInput.cpp
Blame
History
Raw
/* Blinker Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org> Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html a blinkenarea.org project */ #include <string> #include <BlinkenLib/BlinkenFrame.h> #include "Canvas.h" #include "CanvasInput.h" #include "Directory.h" #include "File.h" #include "Position.h" #include "SettingFile.h" #include "Size.h" #include "StreamMgr.h" #include "StreamRecv.h" namespace Blinker { /** * @brief constructor * @param[in] canvas owning canvas * @param[in] dirBase base directory */ Canvas::Input::Input(Canvas &canvas, const Directory &dirBase): m_canvas(canvas), m_fileInStream(dirBase.getFile("instream")), m_filePosition(dirBase.getFile("position")), m_fileSize(dirBase.getFile("size")), m_havePosition(false), m_haveSize(false), m_pInStream(NULL), m_ownFrame(false), m_pFrame(NULL) { // get setting and attach to input stream getPosition(); getSize(); getInStream(); } /// virtual destructor Canvas::Input::~Input() { // detach from input stream and release it releaseInStream(); } /// check for update of configuration void Canvas::Input::updateConfig() { // input stream name file was modified -> re-get input stream if (m_fileInStream.checkModified()) { releaseInStream(); getInStream(); } // position file was modified -> re-get position if (m_filePosition.checkModified()) { getPosition(); } // size file was modified -> re-get size if (m_fileSize.checkModified()) { getSize(); } } /** * @brief set current frame * @param[in] stream stream name * @param[in] pFrame current frame */ void Canvas::Input::setFrame(const std::string &stream, stBlinkenFrame *pFrame) { // prepare frame prepareFrame(pFrame); // notify canvas to redraw m_canvas.redraw(); (void)stream; // unused } /** * @brief set current frame to none * @param[in] stream stream name */ void Canvas::Input::setNoFrame(const std::string &stream) { // throw away prepared frame unprepareFrame(); // notify canvas to redraw m_canvas.redraw(); (void)stream; // unused } /** * @brief draw current frame to canvas * @return if a frame was available and it was drawn */ bool Canvas::Input::draw() { // no position, no frame or no canvas -> leave if (!m_havePosition || !m_pFrame || !m_canvas.m_pCanvas) return false; // draw frame to canvas BlinkenFrameCopyRect(m_canvas.m_pCanvas, m_position.m_y, m_position.m_x, m_pFrame, 0, 0, BlinkenFrameGetHeight(m_pFrame), BlinkenFrameGetWidth(m_pFrame)); return true; } /// get input stream and attach to it void Canvas::Input::getInStream() { // get input stream m_fileInStream.getStr(m_nameInStream); m_pInStream = &m_canvas.m_streamMgr.refStream(m_nameInStream); // attach to input stream if (m_pInStream) m_pInStream->attach(this); // redrawing canvas will happen automaticall because stream will set frame } /// detach from input stream and release it void Canvas::Input::releaseInStream() { // get rid of prepared frame unprepareFrame(); // detach from input stream if (m_pInStream) m_pInStream->detach(this); // unreference stream m_pInStream = NULL; m_canvas.m_streamMgr.unrefStream(m_nameInStream); // notify canvas to redraw m_canvas.redraw(); } /// (re-)get position of stream on canvas void Canvas::Input::getPosition() { std::string strPos; // read position from position file m_havePosition = m_filePosition.getStr(strPos) && m_position.fromStr(strPos); // notify canvas to redraw m_canvas.redraw(); } /// (re-)get size of stream on canvas void Canvas::Input::getSize() { std::string strSize; // read size from size file m_haveSize = m_fileSize.getStr(strSize) && m_size.fromStr(strSize); // update prepared frame updateFrame(); // notify canvas to redraw m_canvas.redraw(); } /// (un-)prepare frame based on current frame of stream void Canvas::Input::updateFrame() { stBlinkenFrame *pFrame; // get current frame from stream if (m_pInStream && m_pInStream->getCurFrame(pFrame)) { // got a frame -> prepare it prepareFrame(pFrame); } else { // no frame -> throw away prepared frame unprepareFrame(); } } /** * @brief prepare frame (scale it if needed) * @param[in] pFrame frame to prepare */ void Canvas::Input::prepareFrame(stBlinkenFrame *pFrame) { // throw away old frame unprepareFrame(); // no scaling needed if (!m_haveSize || ((unsigned int)BlinkenFrameGetWidth(pFrame) == m_size.m_width && (unsigned int)BlinkenFrameGetHeight(pFrame) == m_size.m_height)) { // just remember pointer m_pFrame = pFrame; } // scaling needed else { // clone frame and scale it m_pFrame = BlinkenFrameClone(pFrame); if (m_pFrame) { BlinkenFrameScale(m_pFrame, m_size.m_height, m_size.m_width); m_ownFrame = true; // frame allocated ourself -> free it later } } } /// throw away prepared frame void Canvas::Input::unprepareFrame() { // free frame if it was allocated ourself if (m_ownFrame) { BlinkenFrameFree(m_pFrame); m_ownFrame = false; } // no frame any more m_pFrame = NULL; } } // namespace Blinker