Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pacman.cpp « Controls « windirstat - github.com/windirstat/windirstat.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f9fd0f24aabe8f867bb3e364c5e07cba239e5efd (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// pacman.cpp - Implementation of CPacman
//
// WinDirStat - Directory Statistics
// Copyright (C) 2003-2005 Bernhard Seifert
// Copyright (C) 2004-2017 WinDirStat Team (windirstat.net)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

#include "stdafx.h"
#include "selectobject.h"
#include "pacman.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

namespace
{
    const ULONGLONG UPDATEINTERVAL = 40;  // ms
    const double MOUTHSPEED = 0.0030; // aperture alteration / ms
}

CPacman::CPacman()
    : m_bgcolor(::GetSysColor(COLOR_WINDOW))
    , m_speed(0.0005)
    , m_moving(false)
    , m_readJobs(0)
    , m_toTheRight(true)
    , m_position(0)
    , m_mouthOpening(false)
    , m_aperture(0)
    , m_lastUpdate(0)
{
    Reset();
}

void CPacman::Reset()
{
    m_toTheRight = true;
    m_position = 0;
    m_mouthOpening = true;
    m_aperture = 0;
}

void CPacman::SetBackgroundColor(COLORREF color)
{
    m_bgcolor = color;
}

void CPacman::SetSpeed(double speed)
{
    m_speed = speed;
}

void CPacman::Start(bool start)
{
    m_moving = start;
    m_lastUpdate = _GetTickCount64();
}

bool CPacman::Drive(ULONGLONG readJobs)
{
    m_readJobs = (double)readJobs;

    if(!m_moving)
    {
        return false;
    }

    const ULONGLONG now = _GetTickCount64();
    const ULONGLONG delta = now - m_lastUpdate;

    if(delta < UPDATEINTERVAL)
    {
        return false;
    }

    m_lastUpdate = now;

    UpdatePosition(m_position, m_toTheRight, m_speed * delta);
    UpdatePosition(m_aperture, m_mouthOpening, MOUTHSPEED * delta);

    return true;
}

void CPacman::Draw(CDC *pdc, const CRect& rect)
{
    pdc->FillSolidRect(rect, m_bgcolor);

    CRect rc = rect;
    rc.DeflateRect(5, 1);

    if(rc.Height() % 2 == 0)
    {
        rc.bottom--;
    }

    int diameter = rc.Height();

    int left = rc.left + (int)(m_position * (rc.Width() - diameter));
    rc.left = left;
    rc.right = left + diameter;

    CPen pen(PS_SOLID, 1, RGB(0,0,0));
    CSelectObject sopen(pdc, &pen);

    CBrush brush(CalculateColor());
    CSelectObject sobrush(pdc, &brush);

    CPoint ptStart;
    CPoint ptEnd;
    int hmiddle = rc.top + diameter / 2;

    int mouthcy = (int)(m_aperture * m_aperture * diameter);
    int upperMouthcy = mouthcy;
    int lowerMouthcy = mouthcy;

    if(m_toTheRight)
    {
        ptStart.x = ptEnd.x = rc.right;
        ptStart.y   = hmiddle - upperMouthcy;
        ptEnd.y     = hmiddle + lowerMouthcy;
    }
    else
    {
        ptStart.x = ptEnd.x = rc.left;
        ptStart.y   = hmiddle + lowerMouthcy;
        ptEnd.y     = hmiddle - upperMouthcy;
    }

    pdc->Pie(rc, ptStart, ptEnd);
}


void CPacman::UpdatePosition(double& position, bool& up, double diff)
{
    ASSERT(diff >= 0.0);
    ASSERT(position >= 0.0);
    ASSERT(position <= 1.0);

    while(diff > 0.0)
    {
        if(up)
        {
            if(position + diff > 1.0)
            {
                diff = position + diff - 1.0;
                position = 1.0;
                up = false;
            }
            else
            {
                position += diff;
                diff = 0;
            }
        }
        else
        {
            if(position - diff < 0.0)
            {
                diff = - (position - diff);
                position = 0.0;
                up = true;
            }
            else
            {
                position -= diff;
                diff = 0;
            }
        }
    }
}

COLORREF CPacman::CalculateColor()
{
    static const double pi2 = (3.1415926535897932384626433832795 / 2);

    ASSERT(m_readJobs >= 0);
    double a = atan(m_readJobs / 18) / pi2;
    ASSERT(a >= 0.0);
    ASSERT(a <= 1.0);

/*
    // a == 1 --> yellow
    // a == 0 --> green

    int red = (int)(a * 255);

    return RGB(red, 255, 0);
*/
    // a == 1 --> yellow
    // a == 0 --> bgcolor

    int red     = (int)(a * 255 + (1 - a) * RGB_GET_RVALUE(m_bgcolor));
    int green   = (int)(a * 255 + (1 - a) * RGB_GET_GVALUE(m_bgcolor));
    int blue    = (int)(          (1 - a) * RGB_GET_BVALUE(m_bgcolor));

    return RGB(red, green, blue);
}