JdSoft/DigitNum/DigitNum.cpp
2021-09-12 22:11:31 +02:00

342 lines
11 KiB
C++

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <math.h>
#include "DigitNum.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TDigitNum *)
{
new TDigitNum(NULL);
}
//---------------------------------------------------------------------------
__fastcall TDigitNum::TDigitNum(TComponent* Owner)
: TGraphicControl(Owner)
{
Width = 96; // Establecemos valores por defecto
Height = 33;
vCerosIzq = false;
BorderStyle = bsSingle;
cLED_ON =clRed;
cLED_OFF=clBtnFace;
cLED_BRDon=clBlack;
cLED_BRDoff=clGray;
FValor = 0;
// Obtenemos la longitud de la frase. ( En d¡gitos )
FLen = 4;
FBrush = new TBrush;
FBrush -> Color = clBtnFace;
FBrush -> Style = bsClear;
FBrush -> OnChange = CambioAtributo;
Invalidate();
}
//---------------------------------------------------------------------------
namespace Digitnum
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TDigitNum)};
RegisterComponents("JD soft.", classes, 0);
}
}
//---------------------------------------------------------------------------
__fastcall TDigitNum::~TDigitNum()
{
delete FBrush;
}
//---------------------------------------------------------------------------
void __fastcall TDigitNum::SetCadena( int NuevaCadena )
{
FValor = NuevaCadena;
Invalidate();
}
//---------------------------------------------------------------------------
// Este procedimiento es llamado cada vez que se modifica
// la brocha, el lápiz o el tipo de letra
void __fastcall TDigitNum::CambioAtributo( TObject * Sender )
{
Invalidate(); // tras lo cual habrá que redibujar el componente
}
//---------------------------------------------------------------------------
void __fastcall TDigitNum::SetBrush( TBrush * NuevoValor )
{
FBrush -> Assign( NuevoValor );
Invalidate(); // y volver a dibujar el componente
}
//---------------------------------------------------------------------------
void __fastcall TDigitNum::SetLedOn( TColor NuevoValor )
{
cLED_ON = ( NuevoValor );
Invalidate(); // y volver a dibujar el componente
}
//---------------------------------------------------------------------------
void __fastcall TDigitNum::SetLedOff( TColor NuevoValor )
{
cLED_OFF = ( NuevoValor );
Invalidate(); // y volver a dibujar el componente
}
//---------------------------------------------------------------------------
void __fastcall TDigitNum::SetLedBrdOn( TColor NuevoValor )
{
cLED_BRDon = ( NuevoValor );
Invalidate(); // y volver a dibujar el componente
}
//---------------------------------------------------------------------------
void __fastcall TDigitNum::SetLedBrdOff( TColor NuevoValor )
{
cLED_BRDoff = ( NuevoValor );
Invalidate(); // y volver a dibujar el componente
}
void __fastcall TDigitNum::SetNumDigit( int NuevoValor )
{
if ( NuevoValor > 0 )
{
FLen = NuevoValor;
Invalidate(); // y volver a dibujar el componente
}
}
//---------------------------------------------------------------------------
void __fastcall TDigitNum::Paint()
{
TRect Area;
// Preparamos un RECT definiendo el área ocupada por el componente.
Area = GetClientRect();
// Dibujar en el Canvas del componente
// Establecer la brocha, lápiz y tipo de letra a usar
Canvas -> Brush = FBrush;
// Rellenamos el área con la trama y color seleccionados
Canvas -> FillRect( Area );
DibujaLD();
}
//---------------------------------------------------------------------------
void __fastcall TDigitNum::SetCerosIzq( bool NuevoValor )
{
vCerosIzq = NuevoValor;
Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TDigitNum::DibujaLD( void )
{
// Proceso de redibujado de los digitos...
int AnchoDigito, i, Digito;
// ¿ Cuanto espacio le corresponde a cada dígito... ?
AnchoDigito = Width / FLen;
// Numero de digitos de la cifra...
int Digitos = 1;
while ( FValor >= (int)pow10(Digitos) ) Digitos++;
i = 0;
DibujaDigito( (( FValor % (int)pow10(i+1) ) / (int)pow10(i) ), AnchoDigito, AnchoDigito * (FLen - i - 1) );
for ( i = 1; i < FLen ; i++ )
{
Digito = (( FValor % (int)pow10(i+1) ) / (int)pow10(i) );
if ( !CerosIzq && i >= Digitos )//!Digito && !CerosIzq )
DibujaDigito( 10, AnchoDigito, AnchoDigito * (FLen - i - 1) );
else
DibujaDigito( Digito, AnchoDigito, AnchoDigito * (FLen - i - 1) );
}
}
//---------------------------------------------------------------------------
void __fastcall TDigitNum::DibujaDigito( short int Digito, short int AnchoDigit, short int PosInicial )
{
int PMedio, PVert, Medio, MedioL, MedioH;
POINT P1[5], P2[7], P3[5], P4[5], P5[5], P6[5], P7[5];
PMedio = (Height-1) / 8;
PVert = AnchoDigit / 5;
Medio = (Height-1)/2;//( ( ( PMedio * 6 ) - ( PMedio * 4 ) ) / 2 ) + ( PMedio*4 );
MedioL = Medio - ( PMedio / 2 );
MedioH = Medio + ( PMedio / 2 );
/* _ _ _ _ _ _ _
|_| _ _ _ _ |
1 __ |_| |#|#|#|_| |
_ _ _ _ ___/ |_| |#|_|#|_| |
|_| |_| |_| 4 |2| 5 ______ |_| |#|#|#|_| |
|_| |_| |_| 6 |_| 7 ___ |_| |#|_|#|_| |
\__ |_| |#|#|#|_| |
3 |_|_ _ _ _ _ _|
*/
// 1
// ______________
// \ /
// \__________/
//
P1[0] . x = 1 + 1 + PosInicial; P1[0] . y = 0;
P1[1] . x = PVert * 4 - 1 + PosInicial; P1[1] . y = 0;
P1[2] . x = PVert * 3 - 1 + PosInicial; P1[2] . y = PMedio*1 - 1;
P1[3] . x = PVert * 1 + 1 + PosInicial; P1[3] . y = PMedio*1 - 1;
P1[4] . x = 1 + 1 + PosInicial; P1[4] . y = 0;
// 2
// __________
// / \
// \__________/
//
P2[0] . x = 2 + PosInicial; P2[0] . y = Medio ;
P2[1] . x = PVert * 1 + 1 + PosInicial; P2[1] . y = MedioL + 1;
P2[2] . x = PVert * 3 - 1 + PosInicial; P2[2] . y = MedioL + 1;
P2[3] . x = PVert * 4 - 2 + PosInicial; P2[3] . y = Medio ;
P2[4] . x = PVert * 3 - 1 + PosInicial; P2[4] . y = MedioH - 1;
P2[5] . x = PVert * 1 + 1 + PosInicial; P2[5] . y = MedioH - 1;
P2[6] . x = 2 + PosInicial; P2[6] . y = Medio ;
// 3
// __________
// / \
// /____________\
//
P3[0] . x = PVert * 1 + 1 + PosInicial; P3[0] . y = PMedio*7 + 1;
P3[1] . x = PVert * 3 - 1 + PosInicial; P3[1] . y = PMedio*7 + 1;
P3[2] . x = PVert * 4 - 1 + PosInicial; P3[2] . y = PMedio*8 ;
P3[3] . x = PVert * 0 + 2 + PosInicial; P3[3] . y = PMedio*8 ;
P3[4] . x = PVert * 1 + 1 + PosInicial; P3[4] . y = PMedio*7 + 1;
// .
// |\
// | |
// 4 | |
// | |
// |/
// ·
P4[0] . x = 0 + PosInicial; P4[0] . y = 1;
P4[1] . x = PVert * 1 - 1 + PosInicial; P4[1] . y = PMedio*1 + 1;
P4[2] . x = PVert * 1 - 1 + PosInicial; P4[2] . y = MedioL - 1;
P4[3] . x = 0 + PosInicial; P4[3] . y = Medio - 1;
P4[4] . x = 0 + PosInicial; P4[4] . y = 1;
// .
// /|
// | |
// 5 | |
// | |
// \|
// ·
P5[0] . x = PVert * 4 + 1 + PosInicial; P5[0] . y = 1;
P5[1] . x = PVert * 4 + 1 + PosInicial; P5[1] . y = Medio - 1;
P5[2] . x = PVert * 3 + 1 + PosInicial; P5[2] . y = MedioL - 1;
P5[3] . x = PVert * 3 + 1 + PosInicial; P5[3] . y = PMedio*1 + 1;
P5[4] . x = PVert * 4 + 1 + PosInicial; P5[4] . y = 1;
// .
// |\
// | |
// 6 | |
// | |
// |/
// ·
P6[0] . x = 0 + PosInicial; P6[0] . y = Medio + 1;
P6[1] . x = PVert * 1 - 1 + PosInicial; P6[1] . y = MedioH + 1;
P6[2] . x = PVert * 1 - 1 + PosInicial; P6[2] . y = PMedio*7 - 1;
P6[3] . x = 0 + PosInicial; P6[3] . y = PMedio*8 - 1;
P6[4] . x = 0 + PosInicial; P6[4] . y = MedioH + 1;
// .
// /|
// | |
// 7 | |
// | |
// \|
// ·
P7[0] . x = PVert * 4 + 1 + PosInicial; P7[0] . y = Medio + 1;
P7[1] . x = PVert * 4 + 1 + PosInicial; P7[1] . y = PMedio*8 - 1;
P7[2] . x = PVert * 3 + 1 + PosInicial; P7[2] . y = PMedio*7 - 1;
P7[3] . x = PVert * 3 + 1 + PosInicial; P7[3] . y = MedioH + 1;
P7[4] . x = PVert * 4 + 1 + PosInicial; P7[4] . y = Medio + 1;
// 1 === 0, 2, 3, 5, 6, 7, 8, 9,
if ( Digito != 1 && Digito != 4 && Digito != 10 )
{
Canvas -> Brush -> Color = cLED_ON;
Canvas -> Pen -> Color = cLED_BRDon;
} else {
Canvas -> Brush -> Color = cLED_OFF;
Canvas -> Pen -> Color = cLED_BRDoff;
}
Canvas -> Polygon( P1, 4 );
// 2 === 2, 3, 4, 5, 6, 8, 9
if ( Digito != 0 && Digito != 1 && Digito != 7 && Digito != 10 )
{
Canvas -> Brush -> Color = cLED_ON;
Canvas -> Pen -> Color = cLED_BRDon;
} else {
Canvas -> Brush -> Color = cLED_OFF;
Canvas -> Pen -> Color = cLED_BRDoff;
}
Canvas -> Polygon( P2, 6 );
// 3 === 0, 2, 3, 5, 6, 8, 9
if ( Digito != 1 && Digito != 4 && Digito != 7 && Digito != 10 )
{
Canvas -> Brush -> Color = cLED_ON;
Canvas -> Pen -> Color = cLED_BRDon;
} else {
Canvas -> Brush -> Color = cLED_OFF;
Canvas -> Pen -> Color = cLED_BRDoff;
}
Canvas -> Polygon( P3, 4 );
// 4 === 0, 4, 5, 6, 8, 9
if ( Digito != 1 && Digito != 2 && Digito != 3&& Digito != 7 && Digito != 10 )
{
Canvas -> Brush -> Color = cLED_ON;
Canvas -> Pen -> Color = cLED_BRDon;
} else {
Canvas -> Brush -> Color = cLED_OFF;
Canvas -> Pen -> Color = cLED_BRDoff;
}
Canvas -> Polygon( P4, 4 );
// 5 === 0, 1, 2, 3, 4, 7, 8, 9
if ( Digito != 5 && Digito != 6 && Digito != 10 )
{
Canvas -> Brush -> Color = cLED_ON;
Canvas -> Pen -> Color = cLED_BRDon;
} else {
Canvas -> Brush -> Color = cLED_OFF;
Canvas -> Pen -> Color = cLED_BRDoff;
}
Canvas -> Polygon( P5, 4 );
// 6 === 0, 2, 6, 8
if ( Digito == 0 || Digito == 2 || Digito == 6 || Digito == 8 )
{
Canvas -> Brush -> Color = cLED_ON;
Canvas -> Pen -> Color = cLED_BRDon;
} else {
Canvas -> Brush -> Color = cLED_OFF;
Canvas -> Pen -> Color = cLED_BRDoff;
}
Canvas -> Polygon( P6, 4 );
// 7 === 0, 1, 3, 4, 5, 6, 7, 8, 9
if ( Digito != 2 && Digito != 10 )
{
Canvas -> Brush -> Color = cLED_ON;
Canvas -> Pen -> Color = cLED_BRDon;
} else {
Canvas -> Brush -> Color = cLED_OFF;
Canvas -> Pen -> Color = cLED_BRDoff;
}
Canvas -> Polygon( P7, 4 );
}
//---------------------------------------------------------------------------
void __fastcall TDigitNum::SetBorderStyle( TBorderStyle NuevoValor )
{
FBorderStyle = NuevoValor;
Invalidate(); // y volver a dibujar el componente
}