Subversion Repositories Games.Descent

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
/*
2
 * Portions of this file are copyright Rebirth contributors and licensed as
3
 * described in COPYING.txt.
4
 * Portions of this file are copyright Parallax Software and licensed
5
 * according to the Parallax license below.
6
 * See COPYING.txt for license details.
7
 
8
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9
SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
10
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12
IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14
FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
16
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
18
*/
19
 
20
/*
21
 *
22
 * Routines for manipulating the button gadgets.
23
 *
24
 */
25
 
26
#include <stdlib.h>
27
#include <string.h>
28
 
29
#include "strutil.h"
30
#include "u_mem.h"
31
#include "maths.h"
32
#include "pstypes.h"
33
#include "event.h"
34
#include "gr.h"
35
#include "ui.h"
36
#include "mouse.h"
37
#include "key.h"
38
 
39
namespace dcx {
40
 
41
#define Middle(x) ((2*(x)+1)/4)
42
 
43
#define BUTTON_EXTRA_WIDTH  15
44
#define BUTTON_EXTRA_HEIGHT 2
45
 
46
int ui_button_any_drawn = 0;
47
 
48
void ui_get_button_size(const grs_font &cv_font, const char *text, int &width, int &height)
49
{
50
        gr_get_string_size(cv_font, text, &width, &height, nullptr);
51
        width += BUTTON_EXTRA_WIDTH * 2 + 6;
52
        height += BUTTON_EXTRA_HEIGHT * 2 + 6;
53
}
54
 
55
 
56
void ui_draw_button(UI_DIALOG *dlg, UI_GADGET_BUTTON * button)
57
{
58
#if 0  //ndef OGL
59
        if ((button->status==1) || (button->position != button->oldposition))
60
#endif
61
        {
62
                ui_button_any_drawn = 1;
63
                gr_set_current_canvas( button->canvas );
64
                auto &canvas = *grd_curcanv;
65
                color_palette_index color{0};
66
 
67
                gr_set_fontcolor(canvas, dlg->keyboard_focus_gadget == button
68
                        ? CRED
69
                        : (!button->user_function && button->dim_if_no_function
70
                                ? CGREY
71
                                : CBLACK
72
                        ), -1);
73
 
74
                button->status = 0;
75
                if (!button->text.empty())
76
                {
77
                        unsigned offset;
78
                        if (button->position == 0)
79
                        {
80
                                ui_draw_box_out(canvas, 0, 0, button->width-1, button->height-1);
81
                                offset = 0;
82
                        }
83
                        else
84
                        {
85
                                ui_draw_box_in(canvas, 0, 0, button->width-1, button->height-1);
86
                                offset = 1;
87
                        }
88
                        ui_string_centered(canvas, Middle(button->width) + offset, Middle(button->height) + offset, button->text.c_str());
89
                } else {
90
                        unsigned left, top, right, bottom;
91
                        if (button->position == 0)
92
                        {
93
                                left = top = 1;
94
                                right = button->width - 1;
95
                                bottom = button->height - 1;
96
                        }
97
                        else
98
                        {
99
                                left = top = 2;
100
                                right = button->width;
101
                                bottom = button->height;
102
                        }
103
                        gr_rect(canvas, 0, 0, button->width, button->height, CBLACK);
104
                        gr_rect(canvas, left, top, right, bottom, color);
105
                }
106
        }
107
}
108
 
109
std::unique_ptr<UI_GADGET_BUTTON> ui_add_gadget_button(UI_DIALOG * dlg, short x, short y, short w, short h, const char * text, int (*function_to_call)())
110
{
111
        std::unique_ptr<UI_GADGET_BUTTON> button{ui_gadget_add<UI_GADGET_BUTTON>( dlg, x, y, x+w-1, y+h-1)};
112
 
113
        if ( text )
114
        {
115
                button->text = text;
116
        } else {
117
                button->text.clear();
118
        }
119
        button->width = w;
120
        button->height = h;
121
        button->position = 0;
122
        button->oldposition = 0;
123
        button->pressed = 0;
124
        button->user_function = function_to_call;
125
        button->user_function1 = NULL;
126
        button->hotkey1= -1;
127
        button->dim_if_no_function = 0;
128
        return button;
129
}
130
 
131
 
132
window_event_result ui_button_do(UI_DIALOG *dlg, UI_GADGET_BUTTON * button,const d_event &event)
133
{
134
        window_event_result rval = window_event_result::ignored;
135
 
136
        button->oldposition = button->position;
137
        button->pressed = 0;
138
 
139
        if (event.type == EVENT_MOUSE_BUTTON_DOWN || event.type == EVENT_MOUSE_BUTTON_UP)
140
        {
141
                int OnMe;
142
 
143
                OnMe = ui_mouse_on_gadget( button );
144
 
145
                if (B1_JUST_PRESSED && OnMe)
146
                {
147
                        button->position = 1;
148
                        rval = window_event_result::handled;
149
                }
150
                else if (B1_JUST_RELEASED)
151
                {
152
                        if ((button->position == 1) && OnMe)
153
                                button->pressed = 1;
154
 
155
                        button->position = 0;
156
                }
157
        }
158
 
159
 
160
        if (event.type == EVENT_KEY_COMMAND)
161
        {
162
                int keypress;
163
 
164
                keypress = event_key_get(event);
165
 
166
                if      ((keypress == button->hotkey) ||
167
                        ((keypress == button->hotkey1) && button->user_function1) ||
168
                        ((dlg->keyboard_focus_gadget==button) && ((keypress==KEY_SPACEBAR) || (keypress==KEY_ENTER)) ))
169
                {
170
                        button->position = 2;
171
                        rval = window_event_result::handled;
172
                }
173
        }
174
        else if (event.type == EVENT_KEY_RELEASE)
175
        {
176
                int keypress;
177
 
178
                keypress = event_key_get(event);
179
 
180
                button->position = 0;
181
 
182
                if      ((keypress == button->hotkey) ||
183
                        ((dlg->keyboard_focus_gadget==button) && ((keypress==KEY_SPACEBAR) || (keypress==KEY_ENTER)) ))
184
                        button->pressed = 1;
185
 
186
                if ((keypress == button->hotkey1) && button->user_function1)
187
                {
188
                        button->user_function1();
189
                        rval = window_event_result::handled;
190
                }
191
        }
192
 
193
        if (event.type == EVENT_WINDOW_DRAW)
194
                ui_draw_button( dlg, button );
195
 
196
        if (button->pressed && button->user_function )
197
        {
198
                button->user_function();
199
                return window_event_result::handled;
200
        }
201
        else if (button->pressed)
202
        {
203
                rval = ui_gadget_send_event(dlg, EVENT_UI_GADGET_PRESSED, button);
204
                if (rval == window_event_result::ignored)
205
                        rval = window_event_result::handled;
206
        }
207
 
208
        return rval;
209
}
210
 
211
}