summaryrefslogtreecommitdiff
path: root/src/button.cxx
blob: fe934ed77fa4ba763d14d94f1af64c218c638f27 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * This file is part of the planetblupi source code
 * Copyright (C) 1997, Daniel Roux & EPSITEC SA
 * Copyright (C) 2017-2019, Mathieu Schroeter
 * http://epsitec.ch; http://www.blupi.org; http://github.com/blupi-games
 *
 * 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 <stdio.h>
#include <stdlib.h>

#include "button.h"
#include "decor.h"
#include "def.h"
#include "event.h"
#include "gettext.h"
#include "misc.h"
#include "pixmap.h"
#include "sound.h"

CButton::CButton ()
{
  m_type       = 0;
  m_bEnable    = true;
  m_bHide      = false;
  m_state      = 0;
  m_mouseState = 0;
  m_nbMenu     = 0;
  m_nbToolTips = 0;
  m_selMenu    = 0;
  m_bMouseDown = false;
  m_message    = static_cast<Uint32> (-1);
  m_pPixmap    = nullptr;
  m_pDecor     = nullptr;
  m_pSound     = nullptr;
  m_toolTips   = nullptr;
}

CButton::~CButton () {}

// Crée un nouveau bouton.

bool
CButton::Create (
  CPixmap * pPixmap, CSound * pSound, Point pos, Sint32 type, Sint32 * pMenu,
  Sint32 nbMenu, const char ** pToolTips, Sint32 region, Uint32 message,
  bool isRightReading)
{
  Point  iconDim;
  Sint32 i, icon;

  static Sint32 ttypes[] = {
    DIMBUTTONX, DIMBUTTONY, // button00.bmp
  };

  if (type < 0 || type > 0)
    return false;

  iconDim.x = ttypes[type * 2 + 0];
  iconDim.y = ttypes[type * 2 + 1];

  m_pPixmap    = pPixmap;
  m_pSound     = pSound;
  m_type       = type;
  m_bEnable    = true;
  m_bHide      = false;
  m_message    = message;
  m_pos        = pos;
  m_dim        = iconDim;
  m_nbMenu     = nbMenu;
  m_selMenu    = 0;
  m_state      = 0;
  m_mouseState = 0;
  m_bMouseDown = false;

  m_nbToolTips = 0;
  while (pToolTips[m_nbToolTips])
    ++m_nbToolTips;

  for (i = 0; i < nbMenu; i++)
  {
    icon = pMenu[i];

    if (isRightReading)
    {
      if (icon == 51) // right arrow
        icon = 50;
      else if (icon == 50) // left arrow
        icon = 51;
      else if (icon == 40) // stop
        icon = 109;
    }

    if (region == 1) // palmiers ?
    {
      if (icon == 0)
        icon = 90; // sol normal
      if (icon == 1)
        icon = 91; // sol inflammable
      if (icon == 2)
        icon = 92; // sol inculte
      if (icon == 7)
        icon = 9; // plante
      if (icon == 8)
        icon = 10; // arbre
    }

    if (region == 2) // hiver ?
    {
      if (icon == 0)
        icon = 96; // sol normal
      if (icon == 1)
        icon = 97; // sol inflammable
      if (icon == 2)
        icon = 98; // sol inculte
      if (icon == 8)
        icon = 99; // arbre
    }

    if (region == 3) // sapin ?
    {
      if (icon == 0)
        icon = 102; // sol normal
      if (icon == 1)
        icon = 103; // sol inflammable
      if (icon == 2)
        icon = 104; // sol inculte
      if (icon == 8)
        icon = 105; // arbre
    }

    m_iconMenu[i] = icon;
  }

  m_toolTips = pToolTips;

  return true;
}

// Dessine un bouton dans son état.

void
CButton::Draw ()
{
  Sint32 i;
  Point  pos = this->m_pos;
  Rect   rect;

  if (m_bHide) // bouton caché ?
  {
    rect.left   = pos.x;
    rect.right  = pos.x + m_dim.x;
    rect.top    = pos.y;
    rect.bottom = pos.y + m_dim.y;
    m_pPixmap->DrawPart (-1, CHBACK, pos, rect); // dessine le fond
    return;
  }

  if (m_bEnable) // bouton actif ?
    m_pPixmap->DrawIcon (-1, CHBUTTON + m_type, m_mouseState, pos);
  else
    m_pPixmap->DrawIcon (-1, CHBUTTON + m_type, 4, pos);

  if (m_nbMenu == 0)
    return;

  if (m_nbMenu > 0)
  {
    m_pPixmap->DrawIcon (-1, CHBUTTON + m_type, m_iconMenu[m_selMenu] + 6, pos);
  }

  if (m_nbMenu == 1 || !m_bEnable || !m_bMouseDown)
    return;

  pos.x += IsRightReading () ? -m_dim.x - 2 : m_dim.x + 2;
  for (i = 0; i < m_nbMenu; i++)
  {
    m_pPixmap->DrawIcon (-1, CHBUTTON + m_type, i == m_selMenu ? 1 : 0, pos);
    m_pPixmap->DrawIcon (-1, CHBUTTON + m_type, m_iconMenu[i] + 6, pos);
    pos.x += IsRightReading () ? -m_dim.x - 1 : m_dim.x - 1;
  }
}

Sint32
CButton::GetState ()
{
  return m_state;
}

void
CButton::SetState (Sint32 state)
{
  m_state      = state;
  m_mouseState = state;
}

Sint32
CButton::GetMenu ()
{
  return m_selMenu;
}

void
CButton::SetMenu (Sint32 menu)
{
  m_selMenu = menu;
}

bool
CButton::GetEnable ()
{
  return m_bEnable;
}

void
CButton::SetEnable (bool bEnable)
{
  m_bEnable = bEnable;
}

bool
CButton::GetHide ()
{
  return m_bHide;
}

void
CButton::SetHide (bool bHide)
{
  m_bHide = bHide;
}

// Traitement d'un événement.

bool
CButton::TreatEvent (const SDL_Event & event)
{
  Point pos;

  if (m_bHide || !m_bEnable)
    return false;

  // pos = ConvLongToPos(lParam);

  switch (event.type)
  {
  case SDL_MOUSEBUTTONDOWN:
    if (
      event.button.button != SDL_BUTTON_LEFT &&
      event.button.button != SDL_BUTTON_RIGHT)
      break;

    pos.x = event.button.x;
    pos.y = event.button.y;
    if (MouseDown (pos))
      return true;
    break;

  case SDL_MOUSEMOTION:
    pos.x = event.motion.x;
    pos.y = event.motion.y;
    if (MouseMove (pos))
      return true;
    break;

  case SDL_MOUSEBUTTONUP:
    if (
      event.button.button != SDL_BUTTON_LEFT &&
      event.button.button != SDL_BUTTON_RIGHT)
      break;

    pos.x = event.button.x;
    pos.y = event.button.y;
    if (MouseUp (pos)) // (*)
      return false;
    break;
  }

  return false;
}

// (*) Tous les boutons doivent recevoir l'événement BUTTONUP !

// Indique si la souris est sur ce bouton.

bool
CButton::MouseOnButton (Point pos)
{
  return Detect (pos);
}

bool
CButton::IsInMenu (const Point & pos)
{
  Sint32 width = m_dim.x;

  if (m_nbMenu > 1 && m_bMouseDown) // submenu expanded ?
    width += 2 + (m_dim.x + (IsRightReading () ? 1 : -1)) * m_nbMenu;

  if (IsRightReading () && m_nbMenu > 1 && m_bMouseDown)
  {
    if (pos.x > m_pos.x + m_dim.x || pos.x < m_pos.x - width + m_dim.x)
      return false;
  }
  else
  {
    if (pos.x < m_pos.x || pos.x > m_pos.x + width)
      return false;
  }

  if (pos.y < m_pos.y || pos.y > m_pos.y + m_dim.y)
    return false;

  return true;
}

// Retourne le tooltips pour un bouton, en fonction
// de la position de la souris.

const char *
CButton::GetToolTips (Point pos)
{
  Sint32 rank;

  if (m_bHide || !m_bEnable)
    return nullptr;

  if (!this->IsInMenu (pos))
    return nullptr;

  rank = (pos.x - (m_pos.x + 2 + 1)) / (m_dim.x - 1);
  if (rank < 0)
    rank = 0;
  if (rank > m_nbToolTips)
    return nullptr;

  if (m_nbMenu > 1)
  {
    if (m_bMouseDown && rank > 0)
      rank--;
    else
      rank = m_selMenu;
  }

  return gettext (m_toolTips[rank]);
}

// Détecte si la souris est dans le bouton.

bool
CButton::Detect (Point pos)
{
  if (m_bHide || !m_bEnable)
    return false;

  return this->IsInMenu (pos);
}

// Bouton de la souris pressé.

bool
CButton::MouseDown (Point pos)
{
  if (!Detect (pos))
    return false;

  m_mouseState = 1;
  m_bMouseDown = true;

  CEvent::PushUserEvent (EV_UPDATE);

  m_pSound->PlayImage (SOUND_CLICK, pos);
  return true;
}

// Souris déplacés.

bool
CButton::MouseMove (Point pos)
{
  bool   bDetect;
  Sint32 iState, iMenu;

  iState = m_mouseState;
  iMenu  = m_selMenu;

  bDetect = Detect (pos);

  if (m_bMouseDown)
  {
    if (bDetect)
      m_mouseState = 1; // pressé
    else
      m_mouseState = m_state;
  }
  else
  {
    if (bDetect)
      m_mouseState = m_state + 2; // survollé
    else
      m_mouseState = m_state;
  }

  if (
    m_nbMenu > 1 && m_bMouseDown &&
    (IsRightReading () ? pos.x < m_pos.x
                       : pos.x > m_pos.x + m_dim.x + 2)) // in sub-menu ?
  {
    m_selMenu = IsRightReading ()
                  ? (m_pos.x - 2 - pos.x) / (m_dim.x + 1)
                  : (pos.x - (m_pos.x + m_dim.x + 2)) / (m_dim.x - 1);
    if (m_selMenu >= m_nbMenu)
      m_selMenu = m_nbMenu - 1;
  }

  if (iState != m_mouseState || iMenu != m_selMenu)
    CEvent::PushUserEvent (EV_UPDATE);

  return m_bMouseDown;
}

// Bouton de la souris relâché.

bool
CButton::MouseUp (Point pos)
{
  bool bDetect;

  bDetect = Detect (pos);

  m_mouseState = m_state;
  m_bMouseDown = false;

  if (!bDetect)
    return false;

  if (m_message != static_cast<Uint32> (-1))
    CEvent::PushUserEvent (m_message);

  return true;
}