summaryrefslogtreecommitdiffstats
path: root/src/ui/shortcut.cpp
blob: 5164a10b7da6bcb8cbbb56d9c6bc704fe6334310 (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
/*
 * This file is part of the Colobot: Gold Edition source code
 * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
 * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
 *
 * 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 3 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, see http://gnu.org/licenses
 */


#include "ui/shortcut.h"

#include "common/event.h"
#include "common/misc.h"

#include "graphics/engine/engine.h"
#include "graphics/core/device.h"

#include <math.h>

namespace Ui {

// Object's constructor.

CShortcut::CShortcut() : CControl()
{
    m_time = 0.0f;
}

// Object's destructor.

CShortcut::~CShortcut()
{
}


// Creates a new button.

bool CShortcut::Create(Math::Point pos, Math::Point dim, int icon, EventType eventType)
{
    if ( eventType == EVENT_NULL )  eventType = GetUniqueEventType();

    CControl::Create(pos, dim, icon, eventType);
    return true;
}


// Management of an event.

bool CShortcut::EventProcess(const Event &event)
{
    CControl::EventProcess(event);

    if ( event.type == EVENT_FRAME )
    {
        m_time += event.rTime;
    }

    if ( event.type == EVENT_MOUSE_BUTTON_DOWN  &&
            event.mouseButton.button == MOUSE_BUTTON_LEFT)
    {
        if ( CControl::Detect(event.mousePos) )
        {
            Event newEvent = event;
            newEvent.type = m_eventType;
            m_event->AddEvent(newEvent);
            return false;
        }
    }

    return true;
}


// Draws the button.

void CShortcut::Draw()
{
    float       zoom;
    int         icon, mode;

    icon = 0;
    zoom = 0.8f;
    mode = Gfx::ENG_RSTATE_TTEXTURE_WHITE;
    if ( m_state & STATE_HILIGHT )
    {
        icon = 4;
        zoom = 0.9f;
        mode = Gfx::ENG_RSTATE_NORMAL;
    }
    if ( m_state & STATE_CHECK )
    {
        icon = 1;
        zoom = 0.8f;
        mode = Gfx::ENG_RSTATE_NORMAL;
    }
    if ( m_state & STATE_PRESS )
    {
        icon = 1;
        zoom = 1.0f;
        mode = Gfx::ENG_RSTATE_NORMAL;
    }
    if ( m_icon == 6 || m_icon == 7 )  // pause or film?
    {
        icon = -1;  // no bottom
        zoom = 1.0f;
    }

    m_engine->SetTexture("button3.png");

    if ( icon != -1 )
    {
        m_engine->SetState(mode);
        DrawVertex(icon, 0.95f);
    }

    m_engine->SetState(Gfx::ENG_RSTATE_TTEXTURE_BLACK);
    DrawVertex(m_icon, zoom);

    if ( m_state & STATE_FRAME )
    {
        Math::Point p1, p2, c, uv1, uv2;
        float   dp;

        m_engine->SetTexture("button2.png");
        m_engine->SetState(Gfx::ENG_RSTATE_TTEXTURE_WHITE);

        zoom = 0.9f+sinf(m_time*8.0f)*0.1f;

        p1.x = m_pos.x;
        p1.y = m_pos.y;
        p2.x = m_pos.x + m_dim.x;
        p2.y = m_pos.y + m_dim.y;

        c.x = (p1.x+p2.x)/2.0f;
        c.y = (p1.y+p2.y)/2.0f;  // center

        p1.x = (p1.x-c.x)*zoom + c.x;
        p1.y = (p1.y-c.y)*zoom + c.y;
        p2.x = (p2.x-c.x)*zoom + c.x;
        p2.y = (p2.y-c.y)*zoom + c.y;

        p2.x -= p1.x;
        p2.y -= p1.y;

        uv1.x = 176.0f/256.0f;
        uv1.y = 224.0f/256.0f;
        uv2.x = 192.0f/256.0f;
        uv2.y = 240.0f/256.0f;

        dp = 0.5f/256.0f;
        uv1.x += dp;
        uv1.y += dp;
        uv2.x -= dp;
        uv2.y -= dp;

        DrawIcon(p1, p2, uv1, uv2);
    }

    if ( (m_state & STATE_RUN) && Math::Mod(m_time, 0.7f) >= 0.3f )
    {
        Math::Point uv1, uv2;
        float   dp;

        m_engine->SetTexture("button3.png");
        m_engine->SetState(Gfx::ENG_RSTATE_TTEXTURE_WHITE);

        uv1.x = 160.0f/256.0f;
        uv1.y =   0.0f/256.0f;
        uv2.x = 192.0f/256.0f;
        uv2.y =  32.0f/256.0f;

        dp = 0.5f/256.0f;
        uv1.x += dp;
        uv1.y += dp;
        uv2.x -= dp;
        uv2.y -= dp;

        DrawIcon(m_pos, m_dim, uv1, uv2);
    }
}

// Draw the vertex array.

void CShortcut::DrawVertex(int icon, float zoom)
{
    Gfx::CDevice* device;
    Gfx::Vertex  vertex[4];  // 2 triangles
    Math::Point     p1, p2, c;
    Math::Vector    n;
    float       u1, u2, v1, v2, dp;

    device = m_engine->GetDevice();

    p1.x = m_pos.x;
    p1.y = m_pos.y;
    p2.x = m_pos.x + m_dim.x;
    p2.y = m_pos.y + m_dim.y;

    c.x = (p1.x+p2.x)/2.0f;
    c.y = (p1.y+p2.y)/2.0f;  // center

    p1.x = (p1.x-c.x)*zoom + c.x;
    p1.y = (p1.y-c.y)*zoom + c.y;

    p2.x = (p2.x-c.x)*zoom + c.x;
    p2.y = (p2.y-c.y)*zoom + c.y;

    u1 = (32.0f/256.0f)*(icon%8);
    v1 = (32.0f/256.0f)*(icon/8);  // u-v texture
    u2 = (32.0f/256.0f)+u1;
    v2 = (32.0f/256.0f)+v1;

    dp = 0.5f/256.0f;
    u1 += dp;
    v1 += dp;
    u2 -= dp;
    v2 -= dp;

    n = Math::Vector(0.0f, 0.0f, -1.0f);  // normal

    vertex[0] = Gfx::Vertex(Math::Vector(p1.x, p1.y, 0.0f), n, Math::Point(u1, v2));
    vertex[1] = Gfx::Vertex(Math::Vector(p1.x, p2.y, 0.0f), n, Math::Point(u1, v1));
    vertex[2] = Gfx::Vertex(Math::Vector(p2.x, p1.y, 0.0f), n, Math::Point(u2, v2));
    vertex[3] = Gfx::Vertex(Math::Vector(p2.x, p2.y, 0.0f), n, Math::Point(u2, v1));

    device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, vertex, 4);
    m_engine->AddStatisticTriangle(2);
}

}