File: uipulldownmenu.cpp

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (425 lines) | stat: -rw-r--r-- 11,875 bytes parent folder | download
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
/*
===========================================================================
Copyright (C) 2023 the OpenMoHAA team

This file is part of OpenMoHAA source code.

OpenMoHAA source code 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.

OpenMoHAA source code 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 OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

#include "../qcommon/localization.h"
#include "ui_local.h"

Event EV_Pulldown_ChildKilled
(
    "_ev_pulldown_child_killed",
    EV_DEFAULT,
    NULL,
    NULL,
    "Signaled when the sub menu is killed for one reason or another"
);

Event EV_Pulldown_HighlightBGColor
(
    "highlight_bgcolor",
    EV_DEFAULT,
    "fff",
    "r g b",
    "Set the background color of the highlighted text when a pulldown is used"
);

Event EV_Pulldown_HighlightFGColor
(
    "highlight_fgcolor",
    EV_DEFAULT,
    "fff",
    "r g b",
    "Set the foreground color of the highlighted text when a pulldown is used"
);

CLASS_DECLARATION(UIWidget, UIPulldownMenu, NULL) {
    {&W_LeftMouseDown,              &UIPulldownMenu::MousePressed    },
    {&W_LeftMouseUp,                &UIPulldownMenu::MouseReleased   },
    {&W_LeftMouseDragged,           &UIPulldownMenu::MouseDragged    },
    {&EV_Pulldown_ChildKilled,      &UIPulldownMenu::ChildKilled     },
    {&EV_Pulldown_HighlightBGColor, &UIPulldownMenu::HighlightBGColor},
    {&EV_Pulldown_HighlightFGColor, &UIPulldownMenu::HighlightFGColor},
    {NULL,                          NULL                             }
};

static UIPopupMenu *MenuFromPoint(UIPoint2D& p)
{
    UIWidget *res = uWinMan.FindResponder(p);
    if (res && res->isSubclassOf(UIPopupMenu)) {
        return (UIPopupMenu *)res;
    }

    return NULL;
}

UIPulldownMenu::UIPulldownMenu()
{
    m_listener           = NULL;
    m_submenu            = -1;
    m_highlightBGColor.r = 0.02f;
    m_highlightBGColor.g = 0.07f;
    m_highlightBGColor.b = 0.005f;
    m_highlightBGColor.a = 1.0f;
    m_highlightFGColor   = (UColor)UHudColor;

    // uninitialized in original game
    m_submenuptr = NULL;

    // Added in OPM
    //  This is so the window manager knows it can be activated
    //  and appropriately handle this as the first responder
    m_canactivate = true;
}

UIPulldownMenu::~UIPulldownMenu()
{
    for (int i = m_desc.NumObjects(); i > 0; i--) {
        uipull_describe *updb = m_desc.ObjectAt(i);
        // FIXME: possible OG bug, memory leak? updb->desc is not cleared
        m_desc.RemoveObjectAt(i);
        delete updb;
    }
}

UIRect2D UIPulldownMenu::getAlignmentRect(UIWidget *parent)
{
    if (!parent) {
        parent = (m_parent == NULL) ? &uWinMan : m_parent;
    }

    UIRect2D parentRect = parent->getClientFrame();
    if (m_bVirtual) {
        m_vVirtualScale[0] = (float)uid.vidWidth / SCREEN_WIDTH;
        m_vVirtualScale[1] = (float)uid.vidHeight / SCREEN_HEIGHT;
    }

    int maxheight = m_font->getHeight(m_bVirtual ? m_vVirtualScale : NULL);
    for (int i = 1; i <= m_desc.NumObjects(); i++) {
        uipull_describe *uipd = m_desc.ObjectAt(i);
        if (uipd && uipd->material) {
            int h = uii.Rend_GetShaderHeight(uipd->material->GetMaterial());
            if (h > maxheight) {
                maxheight = h;
            }
        }
    }

    return UIRect2D(parentRect.pos.x, parentRect.pos.y, parentRect.size.width, (maxheight + 4) * m_vVirtualScale[1]);
}

float UIPulldownMenu::getDescWidth(uipull_describe *desc)
{
    UIReggedMaterial *mat = desc->material;
    if (mat) {
        return uii.Rend_GetShaderWidth(mat->GetMaterial());
    }

    return m_font->getWidth(desc->title, -1) + 8.0f;
}

float UIPulldownMenu::getDescHeight(uipull_describe *desc)
{
    UIReggedMaterial *mat = desc->material;
    if (mat) {
        return uii.Rend_GetShaderHeight(mat->GetMaterial());
    }

    return m_font->getHeight(m_bVirtual ? m_vVirtualScale : NULL);
}

uipull_describe *UIPulldownMenu::getPulldown(str title)
{
    for (int i = 1; i <= m_desc.NumObjects(); i++) {
        uipull_describe *pd = m_desc.ObjectAt(i);
        if (pd->title == title) {
            return pd;
        }
    }

    return NULL;
}

void UIPulldownMenu::HighlightBGColor(Event *ev)
{
    m_highlightBGColor.r = ev->GetFloat(1);
    m_highlightBGColor.g = ev->GetFloat(2);
    m_highlightBGColor.b = ev->GetFloat(3);
    m_highlightBGColor.a = 1.0f;
}

void UIPulldownMenu::HighlightFGColor(Event *ev)
{
    m_highlightFGColor.r = ev->GetFloat(1);
    m_highlightFGColor.g = ev->GetFloat(2);
    m_highlightFGColor.b = ev->GetFloat(3);
    m_highlightFGColor.a = 1.0f;
}

void UIPulldownMenu::Create(UIWidget *parent, Listener *listener, const UIRect2D& rect)
{
    m_listener = listener;
    InitFrame(parent, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height, 0);
}

void UIPulldownMenu::CreateAligned(UIWidget *parent, Listener *listener)
{
    setFont("verdana-12");
    UIRect2D rect = getAlignmentRect(parent);
    Create(parent, listener, rect);
}

void UIPulldownMenu::MousePressed(Event *ev)
{
    uWinMan.setFirstResponder(this);
    MouseDragged(ev);
}

void UIPulldownMenu::MouseDragged(Event *ev)
{
    int              newSubMenu = -1;
    UIRect2D         subRect;
    uipull_describe *subdesc = NULL;

    UIPoint2D point = MouseEventToClientPoint(ev);
    if (getClientFrame().contains(point)) {
        float atx = m_vVirtualScale[0] * 2;
        for (int i = 1; i <= m_desc.NumObjects(); i++) {
            uipull_describe *desc  = m_desc.ObjectAt(i);
            float            width = getDescWidth(desc) * m_vVirtualScale[0];
            if (atx <= point.x && atx + width > point.x) {
                newSubMenu          = i;
                subdesc             = desc;
                subRect.pos.x       = atx;
                subRect.pos.y       = 0.0f;
                subRect.size.width  = width;
                subRect.size.height = m_frame.size.height;
                break;
            }
            atx += width;
        }
    }

    if (newSubMenu == m_submenu || newSubMenu == -1) {
        if (m_submenu == -1) {
            return;
        }

        UIPoint2D cursorLocation(point.x + m_screenframe.pos.x, point.y + m_screenframe.pos.y);

        UIPopupMenu *menu = MenuFromPoint(cursorLocation);
        if (!menu) {
            UIPopupMenu *submenu = m_submenuptr;
            while (submenu->m_submenu != -1) {
                submenu = submenu->m_submenuptr;
            }

            submenu->MouseMoved(ev);
        } else {
            while (menu->m_parentMenu) {
                menu = menu->m_parentMenu;
            }
            if (menu == m_submenuptr) {
                menu->MouseMoved(ev);
            }
        }

        return;
    }

    if (m_submenu != -1) {
        m_submenuptr->Disconnect(this, W_Destroyed);
        if (m_submenuptr) {
            delete m_submenuptr;
        }
    }

    m_submenu    = newSubMenu;
    m_submenuptr = new UIPopupMenu();
    subRect.pos.x += m_screenframe.pos.x;
    subRect.pos.y += m_screenframe.pos.y;
    m_submenuptr->Create(&subdesc->desc, m_listener, subRect, UIP_WHERE_DOWN, m_bVirtual, -1.0f);
    m_submenuptr->Connect(this, W_Destroyed, EV_Pulldown_ChildKilled);
}

void UIPulldownMenu::MouseReleased(Event *ev)
{
    if (uWinMan.getFirstResponder() == this) {
        uWinMan.setFirstResponder(NULL);
        UIPoint2D    point(ev->GetFloat(1), ev->GetFloat(2));
        UIPopupMenu *menu = MenuFromPoint(point);
        if (menu) {
            menu->MouseReleased(ev);
        }
    }

    if (m_submenu != -1) {
        m_submenuptr->Disconnect(this, W_Destroyed);
        delete m_submenuptr;
        m_submenu = -1;
    }
}

void UIPulldownMenu::ChildKilled(Event *ev)
{
    if (m_submenu != -1) {
        m_submenu = -1;
    }

    if (uWinMan.getFirstResponder() == this) {
        uWinMan.setFirstResponder(NULL);
    }
}

void UIPulldownMenu::AddUIPopupDescribe(const char *title, uipopup_describe *d)
{
    if (!title) {
        return;
    }

    uipull_describe *pd = getPulldown(title);
    if (!pd) {
        pd = new uipull_describe(title, NULL, NULL);
        m_desc.AddObject(pd);
    }

    pd->desc.AddObject(d);
}

void UIPulldownMenu::setHighlightFGColor(UColor c)
{
    m_highlightFGColor = c;
}

void UIPulldownMenu::setHighlightBGColor(UColor c)
{
    m_highlightBGColor = c;
}

void UIPulldownMenu::setPopupHighlightFGColor(str menu, UColor c)
{
    uipull_describe *up = getPulldown(menu);
    if (up) {
        up->highlightFGColor = c;
    }
}

void UIPulldownMenu::setPopupHighlightBGColor(str menu, UColor c)
{
    uipull_describe *up = getPulldown(menu);
    if (up) {
        up->highlightBGColor = c;
    }
}

void UIPulldownMenu::setPopupFGColor(str menu, UColor c)
{
    uipull_describe *up = getPulldown(menu);
    if (up) {
        up->FGColor = c;
    }
}

void UIPulldownMenu::setPopupBGColor(str menu, UColor c)
{
    uipull_describe *up = getPulldown(menu);
    if (up) {
        up->BGColor = c;
    }
}

void UIPulldownMenu::setPulldownShader(const char *title, UIReggedMaterial *mat)
{
    if (!title) {
        return;
    }

    uipull_describe *pd = getPulldown(title);
    if (!pd) {
        pd = new uipull_describe(title, NULL, NULL);
        m_desc.AddObject(pd);
    }

    pd->material = mat;
    Realign();
}

void UIPulldownMenu::setSelectedPulldownShader(const char *title, UIReggedMaterial *mat)
{
    if (!title) {
        return;
    }

    uipull_describe *pd = getPulldown(title);
    if (!pd) {
        pd = new uipull_describe(title, NULL, NULL);
        m_desc.AddObject(pd);
    }

    pd->selected_material = mat;
    Realign();
}

void UIPulldownMenu::Realign(void)
{
    UIRect2D rect = getAlignmentRect(NULL);
    setFrame(rect);
}

void UIPulldownMenu::Draw(void)
{
    float atx = 0.0f;
    for (int i = 1; i <= m_desc.NumObjects(); i++) {
        uipull_describe *desc   = m_desc.ObjectAt(i);
        float            width  = getDescWidth(desc) * m_vVirtualScale[0];
        float            height = getDescHeight(desc) * m_vVirtualScale[1];

        // Set font color based on whether the current item is selected
        if (m_submenu == i) {
            // Selected item
            m_font->setColor(m_highlightFGColor);
            if (desc->selected_material) {
                uii.Rend_DrawPicStretched(
                    atx, 0.0f, width, height, 0.0f, 0.0f, 1.0f, 1.0f, desc->selected_material->GetMaterial()
                );
            } else {
                DrawBox(atx, 0.0f, width, m_frame.size.height, m_highlightBGColor, 1.0f);
            }
        } else {
            // Unselected item
            m_font->setColor(m_foreground_color);
        }

        if (desc->material) {
            if (m_submenu != i) {
                uii.Rend_DrawPicStretched(
                    atx, 0.0f, width, height, 0.0f, 0.0f, 1.0f, 1.0f, desc->material->GetMaterial()
                );
            }
        } else {
            const char *text      = Sys_LV_CL_ConvertString(desc->title);
            float       text_xpos = m_vVirtualScale[0] * 4.0f + atx;
            float       text_ypos = m_vVirtualScale[1] * 2.0f;
            m_font->Print(text_xpos, text_ypos, text, -1, m_bVirtual ? m_vVirtualScale : NULL);
        }

        atx += width;
    }
}