summaryrefslogtreecommitdiff
path: root/src/T2DLL/T2DlgItemGageBase.cpp
blob: 02ff085618eff2939828a38849039279ade12a90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "T2DlgItemGageBase.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

T2DlgItemGageBase::T2DlgItemGageBase(T2TowerDoc* towerDoc, T2ImageObj* imageObj, CPalette* palette)
    : T2DlgItem(towerDoc, imageObj, palette)
{
}

/*virtual*/ BOOL T2DlgItemGageBase::OnT2DlgItemEraseBkgnd(CDC* dc) {
    CRect rect;
    GetClientRect(rect);

    int saved = dc->SaveDC();
    dc->SelectPalette(mPalette, false);
    dc->RealizePalette();

    CRect theInterior = rect;
    if (IsDrawInterior())
        DrawInterior(dc, theInterior);

    dc->RestoreDC(saved);
    return true;
}

/*virtual*/ void T2DlgItemGageBase::DrawInterior(CDC* dc, const CRect& rect) {
    dc->FillSolidRect(rect, RGB(255, 255, 255));

    int pos = CalcPos(rect, GetMinValue(), GetMaxValue(), GetValue());
    CRect rect2(rect.left, rect.top, rect.left + pos, rect.bottom);
    dc->FillSolidRect(rect2, GetGageColor(GetValue()));

    DrawBorderLines(dc, rect);
}

void T2DlgItemGageBase::DrawBorderLines(CDC* dc, const CRect& rect) {
    CPen pen;
    pen.CreatePen(PS_SOLID, 0, PALETTEINDEX(255));

    HPEN oldPenH = *dc->SelectObject(&pen);

    int theBlue = CalcPos(rect, GetMinValue(), GetMaxValue(), GetBlueValue());
    dc->MoveTo(theBlue, rect.top);
    dc->LineTo(theBlue, rect.bottom - 1);

    int theYellow = CalcPos(rect, GetMinValue(), GetMaxValue(), GetYellowValue());
    dc->MoveTo(theYellow, rect.top);
    dc->LineTo(theYellow, rect.bottom - 1);

    dc->MoveTo(rect.left, rect.top);
    dc->LineTo(rect.left, rect.bottom - 1);
    dc->LineTo(rect.right - 1, rect.bottom - 1);
    dc->LineTo(rect.right - 1, rect.top);
    dc->LineTo(rect.left, rect.top);

    dc->SelectObject(oldPenH);
}

void T2DlgItemGageBase::DrawValueByText(CDC* dc, const CRect& inRect) {
    CRect theFrameRect = inRect;
    theFrameRect.DeflateRect(2, 0);

    CString theString;
    theString.Format("%d", GetValue());

    CFont theFont;
    theFont.CreateFont(
        -(inRect.Height() - 4),
        0,
        0,
        0,
        FW_BOLD,
        false,
        false,
        false,
        DEFAULT_CHARSET,
        OUT_TT_PRECIS,
        CLIP_TT_ALWAYS,
        PROOF_QUALITY,
        DEFAULT_PITCH,
        "Arial"
        );

    HFONT theOldFont = *dc->SelectObject(&theFont);
    dc->SetBkMode(TRANSPARENT);
    dc->SetTextColor(PALETTERGB(255, 255, 255));
    for (int x = -1; x <= 1; x++) {
        for (int y = -1; y <= 1; y++) {
            CRect shadowRect = theFrameRect;
            shadowRect.OffsetRect(x, y);
            dc->DrawText(theString, shadowRect, DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
        }
    }

    CRect textRect = theFrameRect;
    dc->SetTextColor(PALETTEINDEX(255));
    dc->DrawText(theString, textRect, DT_RIGHT | DT_VCENTER | DT_SINGLELINE);

    dc->SelectObject(theOldFont);
}

int T2DlgItemGageBase::CalcPos(const CRect& rect, int minValue, int maxValue, int value) {
    return (minValue < maxValue)
        ? (rect.left + ((rect.Width() * (value - minValue)) / (maxValue - minValue)))
        : 0;
}