finalcut/examples/mouse.cpp

602 lines
16 KiB
C++
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* mouse.cpp - A small mouse-controlled drawing program *
* *
* This file is part of the FINAL CUT widget toolkit *
2017-11-04 07:03:53 +01:00
* *
2020-01-03 01:33:18 +01:00
* Copyright 2017-2020 Markus Gans *
2017-11-04 07:03:53 +01:00
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
2017-11-04 07:03:53 +01:00
* the License, or (at your option) any later version. *
* *
* FINAL CUT is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
2017-11-04 07:03:53 +01:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
2017-03-17 23:22:13 +01:00
#include <final/final.h>
2017-03-19 17:18:07 +01:00
namespace fc = finalcut::fc;
2019-08-11 18:15:57 +02:00
using finalcut::FColorPair;
using finalcut::FRect;
using finalcut::FPoint;
using finalcut::FSize;
2017-03-19 17:18:07 +01:00
//----------------------------------------------------------------------
// class ColorChooser
//----------------------------------------------------------------------
2020-04-14 23:46:42 +02:00
class ColorChooser final : public finalcut::FWidget
2017-03-19 17:18:07 +01:00
{
2017-09-11 03:06:02 +02:00
public:
// Constructor
explicit ColorChooser (finalcut::FWidget* = nullptr);
// Disable copy constructor
ColorChooser (const ColorChooser&) = delete;
2017-09-11 03:06:02 +02:00
// Destructor
2020-02-19 21:59:13 +01:00
~ColorChooser() override;
2017-03-19 17:18:07 +01:00
// Disable copy assignment operator (=)
ColorChooser& operator = (const ColorChooser&) = delete;
2017-09-11 03:06:02 +02:00
// Accessors
2018-11-13 02:51:41 +01:00
FColor getForeground();
FColor getBackground();
2017-03-19 17:18:07 +01:00
2017-09-11 03:06:02 +02:00
private:
// Mutator
void setSize (const FSize&, bool = true) override;
2017-09-11 03:06:02 +02:00
// Method
2019-08-06 23:45:28 +02:00
void draw() override;
void drawBorder() override;
2017-03-19 17:18:07 +01:00
2017-09-11 03:06:02 +02:00
// Event handler
2019-08-06 23:45:28 +02:00
void onMouseDown (finalcut::FMouseEvent*) override;
2017-03-19 17:18:07 +01:00
// Data members
FColor fg_color{fc::White};
FColor bg_color{fc::Black};
finalcut::FLabel headline{this};
2017-03-19 17:18:07 +01:00
};
2017-03-19 17:18:07 +01:00
//----------------------------------------------------------------------
ColorChooser::ColorChooser (finalcut::FWidget* parent)
: FWidget{parent}
2017-03-19 17:18:07 +01:00
{
FWidget::setSize (FSize{8, 12});
setFixedSize (FSize{8, 12});
2017-03-19 17:18:07 +01:00
unsetFocusable();
// Text label
headline.setGeometry (FPoint{1, 1}, FSize{8, 1});
headline.setEmphasis();
headline.setAlignment (fc::alignCenter);
headline << "Color";
2017-03-19 17:18:07 +01:00
}
//----------------------------------------------------------------------
ColorChooser::~ColorChooser()
{ }
//----------------------------------------------------------------------
inline FColor ColorChooser::getForeground()
2017-03-19 17:18:07 +01:00
{
return fg_color;
}
2017-03-19 17:18:07 +01:00
//----------------------------------------------------------------------
inline FColor ColorChooser::getBackground()
{
return bg_color;
}
2017-03-19 17:18:07 +01:00
//----------------------------------------------------------------------
void ColorChooser::setSize (const FSize& size, bool adjust)
{
// Avoids calling a virtual function from the constructor
// (CERT, OOP50-CPP)
FWidget::setSize (size, adjust);
2017-03-19 17:18:07 +01:00
}
//----------------------------------------------------------------------
void ColorChooser::draw()
{
2020-06-06 21:10:06 +02:00
useParentWidgetColor();
headline.setBackgroundColor(getBackgroundColor());
2017-03-19 17:18:07 +01:00
setColor();
drawBorder();
2017-03-19 17:18:07 +01:00
2019-08-25 22:16:00 +02:00
for (FColor c{0}; c < 16; c++)
2017-03-19 17:18:07 +01:00
{
print() << FPoint{2 + (c / 8) * 3, 3 + c % 8};
2017-03-19 17:18:07 +01:00
if ( c < 6 )
setColor (fc::LightGray, c);
2017-03-19 17:18:07 +01:00
else if ( c > 8 )
setColor (fc::DarkGray, c);
2017-03-19 17:18:07 +01:00
else
setColor (fc::White, c);
2017-03-19 17:18:07 +01:00
if ( c == bg_color )
{
print() << L' ' << wchar_t(fc::Times) << L' ';
2017-03-19 17:18:07 +01:00
}
else
print (" ");
}
}
//----------------------------------------------------------------------
void ColorChooser::drawBorder()
{
finalcut::drawBorder (this, FRect{FPoint{1, 2}, FSize{8, 10}});
}
2017-03-19 17:18:07 +01:00
//----------------------------------------------------------------------
void ColorChooser::onMouseDown (finalcut::FMouseEvent* ev)
2017-03-19 17:18:07 +01:00
{
const int mouse_x = ev->getX();
const int mouse_y = ev->getY();
2017-03-19 17:18:07 +01:00
if ( ev->getButton() == fc::MiddleButton )
return;
for (int c{0}; c < 16; c++)
{
const int xmin = 2 + (c / 8) * 3;
const int xmax = 4 + (c / 8) * 3;
const int y = 3 + c % 8;
if ( mouse_x >= xmin && mouse_x <= xmax && mouse_y == y )
{
if ( ev->getButton() == fc::LeftButton )
bg_color = FColor(c);
else if ( ev->getButton() == fc::RightButton )
fg_color = FColor(c);
redraw();
emitCallback("clicked");
}
}
2017-03-19 17:18:07 +01:00
}
//----------------------------------------------------------------------
// class Brushes
//----------------------------------------------------------------------
2020-04-14 23:46:42 +02:00
class Brushes final : public finalcut::FWidget
2017-03-19 17:18:07 +01:00
{
2017-09-11 03:06:02 +02:00
public:
// Constructor
explicit Brushes (finalcut::FWidget* = nullptr);
// Disable copy constructor
Brushes (const Brushes&) = delete;
2017-09-11 03:06:02 +02:00
// Destructor
2020-02-19 21:59:13 +01:00
~Brushes() override;
2017-03-19 17:18:07 +01:00
// Disable copy assignment operator (=)
Brushes& operator = (const Brushes&) = delete;
2017-09-11 03:06:02 +02:00
// Accessor
wchar_t getBrush();
2017-03-19 17:18:07 +01:00
2017-09-11 03:06:02 +02:00
// Mutators
2018-11-13 02:51:41 +01:00
void setForeground (FColor);
void setBackground (FColor);
2017-03-19 17:18:07 +01:00
2017-09-11 03:06:02 +02:00
private:
// Mutator
void setSize (const FSize&, bool = true) override;
2017-09-11 03:06:02 +02:00
// Method
2019-08-06 23:45:28 +02:00
void draw() override;
void drawBorder() override;
2017-03-19 17:18:07 +01:00
2017-09-11 03:06:02 +02:00
// Event handler
2019-08-06 23:45:28 +02:00
void onMouseDown (finalcut::FMouseEvent*) override;
2017-03-19 17:18:07 +01:00
// Data members
wchar_t brush{L' '};
FColor fg_color{fc::White};
FColor bg_color{fc::Black};
finalcut::FLabel headline{this};
2017-03-19 17:18:07 +01:00
};
//----------------------------------------------------------------------
Brushes::Brushes (finalcut::FWidget* parent)
: FWidget{parent}
2017-03-19 17:18:07 +01:00
{
FWidget::setSize (FSize{8, 4});
setFixedSize (FSize{8, 4});
2017-03-19 17:18:07 +01:00
unsetFocusable();
// Text label
headline.setGeometry(FPoint{1, 1}, FSize{8, 1});
headline.setEmphasis();
headline.setAlignment (fc::alignCenter);
headline << "Brush";
2017-03-19 17:18:07 +01:00
}
//----------------------------------------------------------------------
Brushes::~Brushes()
{ }
//----------------------------------------------------------------------
void Brushes::setSize (const FSize& size, bool adjust)
{
// Avoids calling a virtual function from the constructor
// (CERT, OOP50-CPP)
FWidget::setSize (size, adjust);
}
2017-03-19 17:18:07 +01:00
//----------------------------------------------------------------------
void Brushes::draw()
{
2020-01-03 01:33:18 +01:00
int pos{0};
2020-06-06 21:10:06 +02:00
useParentWidgetColor();
headline.setBackgroundColor(getBackgroundColor());
2017-03-19 17:18:07 +01:00
setColor();
drawBorder();
print() << FPoint{2, 3}
<< FColorPair{fg_color, bg_color} << " "
<< finalcut::FString{3, fc::MediumShade};
2017-03-19 17:18:07 +01:00
2020-01-03 01:33:18 +01:00
if ( brush != L' ' )
2017-03-19 17:18:07 +01:00
pos = 3;
setColor();
print() << FPoint{3 + pos, 2}
<< fc::BlackDownPointingTriangle
<< FPoint{3 + pos, 4}
<< fc::BlackUpPointingTriangle;
2017-03-19 17:18:07 +01:00
}
//----------------------------------------------------------------------
void Brushes::drawBorder()
{
finalcut::drawBorder (this, FRect{FPoint{1, 2}, FSize{8, 3}});
}
2017-03-19 17:18:07 +01:00
//----------------------------------------------------------------------
void Brushes::onMouseDown (finalcut::FMouseEvent* ev)
2017-03-19 17:18:07 +01:00
{
const int mouse_x = ev->getX();
const int mouse_y = ev->getY();
2017-03-19 17:18:07 +01:00
if ( ev->getButton() != fc::LeftButton )
2017-03-19 17:18:07 +01:00
return;
if ( mouse_x >= 2 && mouse_x <= 4 && mouse_y == 3 )
{
brush = L' ';
redraw();
}
else if ( mouse_x >= 5 && mouse_x <= 7 && mouse_y == 3 )
{
brush = fc::MediumShade;
2017-03-19 17:18:07 +01:00
redraw();
}
}
//----------------------------------------------------------------------
inline wchar_t Brushes::getBrush()
{
return brush;
}
//----------------------------------------------------------------------
2018-11-13 02:51:41 +01:00
inline void Brushes::setForeground (FColor color)
2017-03-19 17:18:07 +01:00
{
fg_color = color;
}
//----------------------------------------------------------------------
2018-11-13 02:51:41 +01:00
inline void Brushes::setBackground (FColor color)
2017-03-19 17:18:07 +01:00
{
bg_color = color;
}
//----------------------------------------------------------------------
// class MouseDraw
//----------------------------------------------------------------------
2020-04-14 23:46:42 +02:00
class MouseDraw final : public finalcut::FDialog
2017-03-19 17:18:07 +01:00
{
2017-09-11 03:06:02 +02:00
public:
// Using-declaration
using FWidget::setGeometry;
// Constructor
explicit MouseDraw (finalcut::FWidget* = nullptr);
// Disable copy constructor
MouseDraw (const MouseDraw&) = delete;
2017-09-11 03:06:02 +02:00
// Destructor
2020-02-19 21:59:13 +01:00
~MouseDraw() override;
2017-09-11 03:06:02 +02:00
// Disable copy assignment operator (=)
MouseDraw& operator = (const MouseDraw&) = delete;
2017-09-11 03:06:02 +02:00
// Methods
void setGeometry (const FPoint&, const FSize&, bool = true) override;
2017-09-11 03:06:02 +02:00
// Event handlers
2019-08-06 23:45:28 +02:00
void onKeyPress (finalcut::FKeyEvent*) override;
void onClose (finalcut::FCloseEvent*) override;
2017-09-11 03:06:02 +02:00
private:
// Methods
2019-08-06 23:45:28 +02:00
void draw() override;
2017-09-11 03:06:02 +02:00
void drawBrush (int, int, bool = false);
void drawCanvas();
2020-06-06 21:10:06 +02:00
void createCanvas();
2019-08-06 23:45:28 +02:00
void adjustSize() override;
2017-09-11 03:06:02 +02:00
// Event handler
2019-08-06 23:45:28 +02:00
void onMouseDown (finalcut::FMouseEvent*) override;
void onMouseMove (finalcut::FMouseEvent*) override;
2017-09-11 03:06:02 +02:00
// Callback methods
2020-04-13 12:40:11 +02:00
void cb_colorChanged (const finalcut::FWidget*, const FDataPtr);
2017-09-11 03:06:02 +02:00
// Data members
2019-10-08 04:37:19 +02:00
FTermArea* canvas{nullptr};
ColorChooser c_chooser{this};
Brushes brush{this};
2017-03-19 17:18:07 +01:00
};
2019-09-08 02:04:24 +02:00
2017-03-19 17:18:07 +01:00
//----------------------------------------------------------------------
MouseDraw::MouseDraw (finalcut::FWidget* parent)
: finalcut::FDialog{parent}
2017-03-19 17:18:07 +01:00
{
2020-04-19 20:38:52 +02:00
FDialog::setText ("Drawing with the mouse");
c_chooser.setPos (FPoint{1, 1});
c_chooser.addCallback
2017-03-19 17:18:07 +01:00
(
"clicked",
F_METHOD_CALLBACK (this, &MouseDraw::cb_colorChanged)
2017-03-19 17:18:07 +01:00
);
brush.setPos (FPoint{1, 12});
2017-03-19 17:18:07 +01:00
}
//----------------------------------------------------------------------
MouseDraw::~MouseDraw()
{ }
//----------------------------------------------------------------------
void MouseDraw::setGeometry ( const FPoint& p, const FSize& s, bool adjust)
2017-03-19 17:18:07 +01:00
{
finalcut::FDialog::setGeometry (p, s, adjust);
const std::size_t w = s.getWidth();
const std::size_t h = s.getHeight();
const finalcut::FRect scroll_geometry (FPoint{0, 0}, FSize{w - 11, h - 3});
2020-06-06 21:10:06 +02:00
if ( ! canvas )
return;
const FSize no_shadow{0, 0};
const int old_w = canvas->width;
const int old_h = canvas->height;
2017-03-19 17:18:07 +01:00
resizeArea (scroll_geometry, no_shadow, canvas);
if ( old_w != canvas->width || old_h != canvas->height )
{
setColor(getForegroundColor(), getBackgroundColor());
clearArea (canvas, ' ');
}
}
//----------------------------------------------------------------------
void MouseDraw::onKeyPress (finalcut::FKeyEvent* ev)
2017-03-19 17:18:07 +01:00
{
if ( ! ev )
return;
if ( ev->key() == 'q' )
{
close();
ev->accept();
}
else
finalcut::FDialog::onKeyPress(ev);
2017-03-19 17:18:07 +01:00
}
//----------------------------------------------------------------------
void MouseDraw::onClose (finalcut::FCloseEvent* ev)
2017-03-19 17:18:07 +01:00
{
finalcut::FApplication::closeConfirmationDialog (this, ev);
2017-03-19 17:18:07 +01:00
}
//----------------------------------------------------------------------
void MouseDraw::draw()
{
const int y_max = int(getHeight());
finalcut::FDialog::draw();
2017-03-19 17:18:07 +01:00
setColor();
if ( finalcut::FTerm::isNewFont() )
2017-11-03 05:04:27 +01:00
{
2019-08-25 22:16:00 +02:00
for (int y{2}; y < y_max; y++)
2017-11-03 05:04:27 +01:00
{
print() << FPoint{10, y}
<< fc::NF_rev_border_line_right;
2017-11-03 05:04:27 +01:00
}
2017-03-19 17:18:07 +01:00
print() << FPoint{10, y_max}
<< fc::NF_rev_border_corner_lower_right;
2017-11-03 05:04:27 +01:00
}
else
2017-03-19 17:18:07 +01:00
{
print() << FPoint{10, 2}
<< fc::BoxDrawingsDownAndHorizontal;
2017-11-03 05:04:27 +01:00
2019-08-25 22:16:00 +02:00
for (int y{3}; y < y_max; y++)
2017-11-03 05:04:27 +01:00
{
print() << FPoint{10, y} << fc::BoxDrawingsVertical;
2017-11-03 05:04:27 +01:00
}
print() << FPoint{10, y_max}
<< fc::BoxDrawingsUpAndHorizontal;
2017-03-19 17:18:07 +01:00
}
drawCanvas();
}
//----------------------------------------------------------------------
void MouseDraw::drawBrush (int x, int y, bool swap_color)
{
const int Cols = int(getWidth());
const int Lines = int(getHeight());
2017-03-19 17:18:07 +01:00
if ( x > 10 && x < Cols && y > 2 && y < Lines )
{
if ( swap_color )
setColor (c_chooser.getBackground(), c_chooser.getForeground());
2017-03-19 17:18:07 +01:00
else
setColor (c_chooser.getForeground(), c_chooser.getBackground());
2017-03-19 17:18:07 +01:00
// set canvas print cursor position
canvas->cursor_x = x - canvas->offset_left - 10;
canvas->cursor_y = y - canvas->offset_top - 2;
// print on canvas
print (canvas, brush.getBrush());
2017-03-19 17:18:07 +01:00
// copy canvas to the dialog
drawCanvas();
}
}
//----------------------------------------------------------------------
void MouseDraw::drawCanvas()
{
if ( ! hasPrintArea() )
finalcut::FVTerm::getPrintArea();
2017-03-19 17:18:07 +01:00
2020-06-06 21:10:06 +02:00
// Create canvas after initializing the desktop and color theme
if ( ! canvas )
createCanvas();
2017-03-19 17:18:07 +01:00
if ( ! (hasPrintArea() && canvas) )
return;
auto printarea = getCurrentPrintArea();
const int ax = 9 + getTermX() - printarea->offset_left;
const int ay = 1 + getTermY() - printarea->offset_top;
const int y_end = canvas->height;
const int x_end = canvas->width;
const int w_line_len = printarea->width + printarea->right_shadow;
2017-03-19 17:18:07 +01:00
2019-08-25 22:16:00 +02:00
for (int y{0}; y < y_end; y++) // line loop
2017-03-19 17:18:07 +01:00
{
2020-04-13 12:40:11 +02:00
const finalcut::FChar* canvaschar{}; // canvas character
2019-10-08 04:37:19 +02:00
finalcut::FChar* winchar{}; // window character
canvaschar = &canvas->data[y * x_end];
winchar = &printarea->data[(ay + y) * w_line_len + ax];
std::memcpy ( winchar
, canvaschar
2019-10-08 04:37:19 +02:00
, sizeof(finalcut::FChar) * unsigned(x_end) );
2017-03-19 17:18:07 +01:00
if ( int(printarea->changes[ay + y].xmin) > ax )
printarea->changes[ay + y].xmin = uInt(ax);
2017-03-19 17:18:07 +01:00
if ( int(printarea->changes[ay + y].xmax) < ax + x_end - 1 )
printarea->changes[ay + y].xmax = uInt(ax + x_end - 1);
2017-03-19 17:18:07 +01:00
}
printarea->has_changes = true;
2017-03-19 17:18:07 +01:00
}
2020-06-06 21:10:06 +02:00
//----------------------------------------------------------------------
void MouseDraw::createCanvas()
{
FSize no_shadow{0, 0};
finalcut::FRect scroll_geometry{0, 0, 1, 1};
createArea (scroll_geometry, no_shadow, canvas);
adjustSize();
}
2017-03-19 17:18:07 +01:00
//----------------------------------------------------------------------
void MouseDraw::adjustSize()
{
2020-04-13 12:40:11 +02:00
const std::size_t w{60};
const std::size_t h{18};
const int x = 1 + int((getParentWidget()->getWidth() - w) / 2);
const int y = 1 + int((getParentWidget()->getHeight() - h) / 2);
setGeometry (FPoint{x, y}, FSize{w, h}, false);
finalcut::FDialog::adjustSize();
2017-03-19 17:18:07 +01:00
}
//----------------------------------------------------------------------
void MouseDraw::onMouseDown (finalcut::FMouseEvent* ev)
2017-03-19 17:18:07 +01:00
{
finalcut::FDialog::onMouseDown(ev);
2017-03-19 17:18:07 +01:00
if ( ev->getButton() != fc::LeftButton
&& ev->getButton() != fc::RightButton )
2017-03-19 17:18:07 +01:00
return;
drawBrush ( ev->getX()
, ev->getY()
, ev->getButton() == fc::RightButton );
2017-03-19 17:18:07 +01:00
}
//----------------------------------------------------------------------
void MouseDraw::onMouseMove (finalcut::FMouseEvent* ev)
2017-03-19 17:18:07 +01:00
{
FDialog::onMouseMove(ev);
if ( ev->getButton() != fc::LeftButton
&& ev->getButton() != fc::RightButton )
2017-03-19 17:18:07 +01:00
return;
drawBrush ( ev->getX()
, ev->getY()
, ev->getButton() == fc::RightButton);
2017-03-19 17:18:07 +01:00
}
//----------------------------------------------------------------------
2020-04-13 12:40:11 +02:00
void MouseDraw::cb_colorChanged (const finalcut::FWidget*, const FDataPtr)
2017-03-19 17:18:07 +01:00
{
brush.setForeground (c_chooser.getForeground());
brush.setBackground (c_chooser.getBackground());
brush.redraw();
2017-03-19 17:18:07 +01:00
}
2017-03-17 23:22:13 +01:00
//----------------------------------------------------------------------
// main part
//----------------------------------------------------------------------
int main (int argc, char* argv[])
{
// Create the application object
finalcut::FApplication app{argc, argv};
2017-03-17 23:22:13 +01:00
// Create a simple dialog box
MouseDraw mouse_draw{&app};
mouse_draw.setGeometry (FPoint{12, 4}, FSize{60, 18});
2017-03-17 23:22:13 +01:00
// Set dialog object mouse_draw as main widget
2020-04-13 12:40:11 +02:00
finalcut::FWidget::setMainWidget(&mouse_draw);
// Show and start the application
2017-03-17 23:22:13 +01:00
mouse_draw.show();
return app.exec();
}