summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Palettes/Timeline/TimeMeasure.cpp
blob: 9735499a36530c28aaf582cbc110cf220ff5c14e (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/****************************************************************************
**
** Copyright (C) 2016 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "stdafx.h"

#include "TimeMeasure.h"
#include "Renderer.h"
#include "StudioPreferences.h"
#include "StudioUtils.h"
#include "TimelineTimelineLayout.h"
#include "Snapper.h"
#include "UICDMSignals.h"

const long AUTO_TICK_AMNT = 60;
using namespace Q3DStudio;

//=============================================================================
/**
 * Create a new time measure.
 * @param inLayout the layout this is representing, used for modifying time.
 * @param inTimeRatio the current time ratio.
 * @param inIsTransparent true if the background of this control should not be drawn.
 */
CTimeMeasure::CTimeMeasure(CTimelineTimelineLayout *inLayout, double inTimeRatio,
                           bool inFillBackground /*= true */)
    : CBaseMeasure(inTimeRatio, inFillBackground)
    , m_ScrollDir(0)
    , m_TimePerPixel(0)
    , m_IsMouseDown(false)
    , m_TimelineLayout(inLayout)
{
    SetTimeRatio(inTimeRatio);
    SetName("TimeMeasure");

    m_EdgeMargin = 2;
    // the large tickmark is shorter than the medium to leave room for the text
    m_LargeHashOffset = 5;
}

CTimeMeasure::~CTimeMeasure()
{
}

//=============================================================================
/**
 * Set the amount of time that is represented for each pixel.
 * @param inTimePerPixel the amount of time represented for each pixel.
 */
void CTimeMeasure::SetTimeRatio(double inTimeRatio)
{
    m_Ratio = inTimeRatio;

    double theTimePerPixel = (double)(1 / inTimeRatio);

    // Only go through this if it has actually changed
    if (theTimePerPixel != m_TimePerPixel) {
        m_TimePerPixel = theTimePerPixel;

        // Go through the possible hash settings and find the one that best suits the
        // time per pixel.
        double theMillisPerLargeHash = theTimePerPixel * 50;
        if (theMillisPerLargeHash <= 100) // 100ms
            theMillisPerLargeHash = 100;
        else if (theMillisPerLargeHash <= 200) // 200ms
            theMillisPerLargeHash = 200;
        else if (theMillisPerLargeHash <= 500) // .5s
            theMillisPerLargeHash = 500;
        else if (theMillisPerLargeHash <= 1000) // 1s
            theMillisPerLargeHash = 1000;
        else if (theMillisPerLargeHash <= 2000) // 2s
            theMillisPerLargeHash = 2000;
        else if (theMillisPerLargeHash <= 5000) // 5s
            theMillisPerLargeHash = 5000;
        else if (theMillisPerLargeHash <= 10000) // 10s
            theMillisPerLargeHash = 10000;
        else if (theMillisPerLargeHash <= 20000) // 20s
            theMillisPerLargeHash = 20000;
        else if (theMillisPerLargeHash <= 30000) // 30s
            theMillisPerLargeHash = 30000;
        else if (theMillisPerLargeHash <= 60000) // 1m
            theMillisPerLargeHash = 60000;
        else if (theMillisPerLargeHash <= 120000) // 2m
            theMillisPerLargeHash = 120000;
        else if (theMillisPerLargeHash <= 300000) // 5m
            theMillisPerLargeHash = 300000;
        else if (theMillisPerLargeHash <= 600000) // 10m
            theMillisPerLargeHash = 600000;
        else if (theMillisPerLargeHash <= 1200000) // 20m
            theMillisPerLargeHash = 1200000;
        else if (theMillisPerLargeHash <= 1800000) // 30m
            theMillisPerLargeHash = 1800000;
        else if (theMillisPerLargeHash <= 3600000) // 1h
            theMillisPerLargeHash = 3600000;
        else
            theMillisPerLargeHash = 7200000; // 2h

        // Set the distances between the hashes
        m_LargeHashInterval = theMillisPerLargeHash;
        m_MediumHashInterval = theMillisPerLargeHash / 2;
        m_SmallHashInterval = theMillisPerLargeHash / 10;

        // update to StudioPreferences so that the ',' '.' and '<' '>' keys would respond
        // accordingly
        CStudioPreferences::SetTimeAdvanceAmount(static_cast<long>(m_SmallHashInterval));
        CStudioPreferences::SetBigTimeAdvanceAmount(static_cast<long>(m_MediumHashInterval));

        Invalidate();
    }
}
//=============================================================================
/**
 * Get the time formatted as a string.
 * This will figure out the best way to display the time and return it as a
 * string.
 * @param inTime the time to display in milliseconds.
 * @return the time formatted in a string.
 */
Q3DStudio::CString CTimeMeasure::FormatTime(long inTime)
{
    long theHours = inTime / 3600000;
    long theMinutes = inTime % 3600000 / 60000;
    long theSeconds = inTime % 60000 / 1000;
    long theMillis = inTime % 1000;

    bool theHoursOnlyFlag = theHours != 0 && theMinutes == 0 && theSeconds == 0 && theMillis == 0;
    bool theMinutesOnlyFlag =
        !theHoursOnlyFlag && theMinutes != 0 && theSeconds == 0 && theMillis == 0;
    bool theSecondsOnlyFlag = !theMinutesOnlyFlag && theMillis == 0;

    Q3DStudio::CString theTime;
    // If only hours are being displayed then format it as hours.
    if (theHoursOnlyFlag) {
        theTime.Format(_UIC("%dh"), theHours);
    }
    // If only minutes are being displayed then format it as minutes.
    else if (theMinutesOnlyFlag) {
        theTime.Format(_UIC("%dm"), theMinutes);
    }
    // If only seconds are being displayed then format as seconds
    else if (theSecondsOnlyFlag) {
        theTime.Format(_UIC("%ds"), theSeconds);
    }
    // If the intervals are correct then this should only be tenths of seconds, so do that.
    else {
        theTime.Format(_UIC("0.%ds"), theMillis / 100);
    }

    return theTime;
}

//=============================================================================
/**
 * Set the amount of time that this time measure is offset by.
 * @param inTimeOffset the offset time in milliseconds.
 */
void CTimeMeasure::SetTimeOffset(long inTimeOffset)
{
    if (inTimeOffset != m_Offset) {
        m_Offset = inTimeOffset;

        Invalidate();
    }
}

//=============================================================================
/**
 * Notification that the left mouse button was clicked.
 * This tells the timeline to move the playhead to the current loc.
 * @param inPoint the location where the mouse was clicked.
 * @param inFlags the state of the mouse.
 */
bool CTimeMeasure::OnMouseDown(CPt inPoint, Qt::KeyboardModifiers inFlags)
{
    if (!CControl::OnMouseDown(inPoint, inFlags)) {
        m_IsMouseDown = true;

        m_TimelineLayout->OnTimeMeasureMouseDown(inPoint, inFlags);
    }

    return true;
}

//=============================================================================
/**
 * Notification that the mouse is moving over this control.
 * If the mouse was clicked on this control this will drag the playhead.
 * @param inPoint the location where the mouse was clicked.
 * @param inFlags the state of the mouse.
 */
void CTimeMeasure::OnMouseMove(CPt inPoint, Qt::KeyboardModifiers inFlags)
{
    m_TimerConnection = std::shared_ptr<UICDM::ISignalConnection>();

    // subtract out the button width since the playhead is never allowed into that area on the right
    // side
    // of the timeline and use it for the initial autoscrolling place
    if (inPoint.x > 0 && inPoint.x <= GetSize().x - CStudioPreferences::GetDefaultButtonWidth()) {
        CControl::OnMouseMove(inPoint, inFlags);
        if (m_IsMouseDown)
            m_TimelineLayout->OnTimeMeasureMouseDown(inPoint, inFlags);
        m_ScrollDir = 0;
    } else if (m_IsMouseDown) {
        if (inPoint.x < 0)
            m_ScrollDir = -1;
        else if (inPoint.x > GetSize().x - CStudioPreferences::GetDefaultButtonWidth())
            m_ScrollDir = 1;
        m_TimerConnection = ITickTock::GetInstance().AddTimer(
            150, true, std::bind(&CTimeMeasure::OnTimer, this), "CTimeMeasure::OnMouseMove");
        OnTimer();
    }
}

//=============================================================================
/**
 * Call back for the timer that was set in on mouse move
 */
void CTimeMeasure::OnTimer()
{
    CPt theOffset;
    if (m_ScrollDir > 0)
        theOffset.x =
            GetSize().x - 2 * CStudioPreferences::GetDefaultButtonWidth() + AUTO_TICK_AMNT;
    else if (m_ScrollDir < 0)
        theOffset.x = -AUTO_TICK_AMNT;
    m_TimelineLayout->OnTimeMeasureMouseDown(theOffset, 0);
    ;
}

//=============================================================================
/**
 * Notification that the mouse was unclicked.
 * This stops dragging of the playhead if it was dragging it.
 * @param inPoint the location where the mouse was unclicked.
 * @param inFlags the state of the mouse.
 */
void CTimeMeasure::OnMouseUp(CPt inPoint, Qt::KeyboardModifiers inFlags)
{
    CControl::OnMouseUp(inPoint, inFlags);
    m_TimerConnection = std::shared_ptr<UICDM::ISignalConnection>();
    m_IsMouseDown = false;
}

//=============================================================================
/**
 * Notification that the mouse was unclicked.
 * This stops dragging of the playhead if it was dragging it.
 * @param inPoint the location where the mouse was unclicked.
 * @param inFlags the state of the mouse.
 */
void CTimeMeasure::OnMouseRUp(CPt inPoint, Qt::KeyboardModifiers inFlags)
{
    CControl::OnMouseUp(inPoint, inFlags);
    m_TimerConnection = std::shared_ptr<UICDM::ISignalConnection>();
    m_IsMouseDown = false;
}

//=============================================================================
/**
 * Add the tick marks to the snapping list.
 * This uses the user preference for the tick marks and adds them.
 */
void CTimeMeasure::PopulateSnappingList(CSnapper *inSnapper)
{
    // Only if this is supposed to snap to time markers.
    if (CStudioPreferences::IsTimelineSnappingGridActive()) {
        // Check the resolution to snap to
        ESnapGridResolution theResolution = CStudioPreferences::GetTimelineSnappingGridResolution();
        double thePeriodicInterval;
        if (theResolution == SNAPGRID_TICKMARKS) {
            thePeriodicInterval = m_SmallHashInterval;
        } else if (theResolution == SNAPGRID_HALFSECONDS) {
            thePeriodicInterval = m_MediumHashInterval;
        } else {
            thePeriodicInterval = m_LargeHashInterval;
        }

        // Set a periodic interval for snapping
        inSnapper->SetPeriodicInterval(::dtol(thePeriodicInterval));
    }
}

void CTimeMeasure::OnLoseFocus()
{
    m_TimerConnection = std::shared_ptr<UICDM::ISignalConnection>();
    m_IsMouseDown = false;
}

//=============================================================================
/**
 * Draw the time at the specified position.
 * @param inRenderer the renderer to draw to.
 * @param inPosition the position to draw the time to, the time will be centered here.
 * @param inTime the time to draw.
 */
void CTimeMeasure::DrawMeasureText(CRenderer *inRenderer, long inPosition, long inMeasure)
{
    Q3DStudio::CString theTimeFormat(FormatTime(inMeasure));
    // Offset the position by half the text size to center it over the hash.
    const auto textSize = inRenderer->GetTextSize(theTimeFormat.toQString());
    inPosition -= ::dtol(textSize.width() / 2);

    inRenderer->DrawText((float)inPosition, -3, theTimeFormat.toQString(),
                         QRect(0, 0, GetSize().x, GetSize().y),
                         CStudioPreferences::GetRulerTickColor().getQColor());
}

//=============================================================================
/**
 * Calculate the position of a time value on the time measure
 */
long CTimeMeasure::CalculatePos(double inNewValue)
{
    return ::TimeToPos(inNewValue, m_Ratio);
}